source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcCachedRepresentationDao.java @ 3459

Last change on this file since 3459 was 3459, checked in by olhsha, 11 years ago

making deleters safe in any DAO (except notebooks). that is before delete the resources if checked if it is mentioned in ANY of the joint tables where he is one of the columns. if "yes" then deleting does not happen. Earlier, it was checked only if it is used by a higher-lever resource (i.e. only one joint table was checked).

also, all resource_id row-mappers are moved to the base JdbcResource? class.

File size: 4.4 KB
Line 
1/*
2 * Copyright (C) 2013 DASISH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18package eu.dasish.annotation.backend.dao.impl;
19
20import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
21import eu.dasish.annotation.schema.CachedRepresentationInfo;
22import java.lang.String;
23import java.sql.ResultSet;
24import java.sql.SQLException;
25import java.util.HashMap;
26import java.util.List;
27import java.util.Map;
28import java.util.UUID;
29import javax.sql.DataSource;
30import org.springframework.jdbc.core.RowMapper;
31
32/**
33 *
34 * @author olhsha
35 */
36public class JdbcCachedRepresentationDao extends JdbcResourceDao implements CachedRepresentationDao {
37   
38   
39    public JdbcCachedRepresentationDao(DataSource dataSource) {
40        setDataSource(dataSource);
41        internalIdName = cached_representation_id;
42        resourceTableName = cachedRepresentationTableName;
43    }
44
45 
46
47    /////////////////////////// GETTERS  ////////////////////////////////////////
48    @Override
49    public CachedRepresentationInfo getCachedRepresentationInfo(Number internalID) {
50
51        String sql = "SELECT " + cachedRepresentationStar + " FROM " + cachedRepresentationTableName + " WHERE " + cached_representation_id + "= ?";
52        List<CachedRepresentationInfo> result = getSimpleJdbcTemplate().query(sql, cachedRepresentationRowMapper, internalID);
53
54        if (result == null) {
55            return null;
56        }
57        if (result.isEmpty()) {
58            return null;
59        }
60        return result.get(0);
61    }
62    private final RowMapper<CachedRepresentationInfo> cachedRepresentationRowMapper = new RowMapper<CachedRepresentationInfo>() {
63        @Override
64        public CachedRepresentationInfo mapRow(ResultSet rs, int rowNumber) throws SQLException {
65            CachedRepresentationInfo result = new CachedRepresentationInfo();
66            result.setMimeType(rs.getString(mime_type));
67            result.setRef(rs.getString(external_id));
68            result.setTool(rs.getString(tool));
69            result.setType(rs.getString(type_));
70            // TODO add "where is the file when the schema" is updated!!!!s
71            return result;
72        }
73    };
74   
75    //////////////////////////////////////
76
77    private boolean cachedIsInUse(Number cachedID) {     
78        String sql = "SELECT " + version_id + " FROM " + versionsCachedRepresentationsTableName + " WHERE " + cached_representation_id + "= ? LIMIT 1";
79        List<Number> result = getSimpleJdbcTemplate().query(sql, versionIDRowMapper, cachedID);
80        if (result.size() > 0) {
81            return true;
82        }
83        return false;
84    }
85   
86 
87
88    //////////////////////// ADDERS ///////////////////////////////
89    @Override
90    public Number addCachedRepresentationInfo(CachedRepresentationInfo cached) {
91
92        UUID externalIdentifier = UUID.randomUUID();
93        Map<String, Object> params = new HashMap<String, Object>();
94        params.put("externalId", externalIdentifier.toString());
95        params.put("mime_type", cached.getMimeType());
96        params.put("tool", cached.getTool());
97        params.put("type", cached.getType());
98        String sql = "INSERT INTO " + cachedRepresentationTableName + "(" + external_id + "," + mime_type + "," + tool + "," + type_ + " ) VALUES (:externalId, :mime_type,  :tool, :type)";
99        final int affectedRows = getSimpleJdbcTemplate().update(sql, params);
100        return getInternalID(externalIdentifier);
101    }
102
103    /////////////////////// DELETERS  //////////////////////////////////////////////
104    @Override
105    public int deleteCachedRepresentationInfo(Number internalID) {
106        if (cachedIsInUse(internalID)){
107           return 0;
108        }
109       
110        String sql = "DELETE FROM " + cachedRepresentationTableName + " where " + cached_representation_id + " = ?";
111        return  getSimpleJdbcTemplate().update(sql, internalID);
112    }
113   
114   
115}
Note: See TracBrowser for help on using the repository browser.