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

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

refactored DAO-s: resource dao do not call each-other methods any more. Only the new Dispatcher method can do it. Not tested.

File size: 7.5 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.VersionDao;
21import eu.dasish.annotation.backend.identifiers.VersionIdentifier;
22import eu.dasish.annotation.schema.Version;
23import java.sql.ResultSet;
24import java.sql.SQLException;
25import java.util.HashMap;
26import java.util.List;
27import java.util.Map;
28import javax.sql.DataSource;
29import org.springframework.jdbc.core.RowMapper;
30
31/**
32 *
33 * @author olhsha
34 */
35public class JdbcVersionDao extends JdbcResourceDao implements VersionDao {
36
37   
38    public JdbcVersionDao(DataSource dataSource) {
39        setDataSource(dataSource);
40        internalIdName = version_id;
41        resourceTableName = versionTableName;
42    }
43
44    //////////////////////////////////////////////////////////////////////////////////////////////////////   
45    @Override
46    public VersionIdentifier getExternalID(Number internalID) {
47        return new VersionIdentifier(super.getExternalIdentifier(internalID));
48    }
49
50    //////////////////////////////////////////////////////////////////////////////////////////////////////   
51    @Override
52    public Number getInternalID(VersionIdentifier externalID) {
53        return (super.getInternalID(externalID));
54    }
55
56    ///////////////////////////////////////////////////////////////
57    @Override
58    public Version getVersion(Number internalID) {
59
60        String sql = "SELECT " + versionStar + " FROM " + versionTableName + " WHERE " + version_id + "= ? LIMIT 1";
61        List<Version> result = getSimpleJdbcTemplate().query(sql, versionRowMapper, internalID);
62
63        if (result == null) {
64            return null;
65        }
66        if (result.isEmpty()) {
67            return null;
68        }
69        return result.get(0);
70    }
71    private final RowMapper<Version> versionRowMapper = new RowMapper<Version>() {
72        @Override
73        public Version mapRow(ResultSet rs, int rowNumber) throws SQLException {
74            Version result = new Version();
75            // TODO: clarify situation with the attribute cached representation
76            //result.setCachedRepresentations!!! The same situation as with permissions lists: we cannot refer from a filed to a list of smth, we have a separate joint table
77            // TODO: attribute URI (external-id is missing)
78            result.setVersion(rs.getString("external_id"));
79            return result;
80        }
81    };
82
83    ////////////////////////////////////////////////////////////////////////////
84    @Override
85    public List<Number> retrieveCachedRepresentationList(Number versionID) {
86        String sql = "SELECT " + cached_representation_id + " FROM " + versionsCachedRepresentationsTableName + " WHERE " + version_id + "= ?";
87        List<Number> result = getSimpleJdbcTemplate().query(sql, cachedIDRowMapper, versionID);
88
89        if (result == null) {
90            return null;
91        }
92
93        return result;
94    }
95
96    private final RowMapper<Number> cachedIDRowMapper = new RowMapper<Number>() {
97        @Override
98        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
99            return rs.getInt(cached_representation_id);
100        }
101    };
102   
103    /////////////////////////////////////////
104    @Override
105    public int[] deleteVersion(Number internalID) {
106       
107        int[] result = new int[2];       
108        if (versionIsInUse(internalID)) {
109            // firs delete sources that refer to it!
110            result[0]=0;
111            result[1]=0;
112            return result;
113        }
114       
115        // remove all the pairs (internalID, cached_representation) from the joint table "versions_cahched_representations"   
116        String sqlVersionsCachedRepresentations = "DELETE FROM " + versionsCachedRepresentationsTableName + " where " + version_id + " = ?";
117        result[0] = getSimpleJdbcTemplate().update(sqlVersionsCachedRepresentations, internalID);
118       
119        // the main action: remove the version with internalID from "version" table
120        String sql = "DELETE FROM " + versionTableName + " where " + version_id + " = ?";
121        result[1] = getSimpleJdbcTemplate().update(sql, internalID);     
122        return result;
123
124    }
125
126    /////////////////////////////////////////////////
127    @Override
128    public Number addVersion(Version freshVersion) {
129        VersionIdentifier externalIdentifier = new VersionIdentifier();
130        String newExternalIdentifier = externalIdentifier.toString();
131
132        Map<String, Object> params = new HashMap<String, Object>();
133        params.put("externalId", newExternalIdentifier);
134        //TODO: till the schema is fixed, version-text and version's external Id are the same (now version do not have URI's/ext id's)
135        params.put("version", newExternalIdentifier);
136        String sql = "INSERT INTO " + versionTableName + "(" + external_id + "," + version + " ) VALUES (:externalId, :version)";
137        final int affectedRows = getSimpleJdbcTemplate().update(sql, params);
138        return getInternalID(externalIdentifier);
139    }
140   
141    ////////////////////////////////////////////
142    @Override
143    public int deleteVersionCachedRepresentation(Number versionID, Number cachedID){
144    Map<String, Object> params = new HashMap<String, Object>();
145         params.put("versionId", versionID);
146         params.put("cachedId", cachedID);   
147    StringBuilder sql = new StringBuilder("DELETE FROM ");
148    sql.append(versionsCachedRepresentationsTableName).append(" WHERE ").append(version_id).append(" = :versionId AND ");
149    sql.append(cached_representation_id).append("= :cachedId");
150    return (getSimpleJdbcTemplate().update(sql.toString(), params));
151    }
152       
153    ////////////////////////////////////////////
154    @Override
155    public int addVersionCachedRepresentation(Number versionID, Number cachedID){       
156    Map<String, Object> params = new HashMap<String, Object>();
157        params.put("versionId", versionID);
158        params.put("cachedId", cachedID);
159        String sql = "INSERT INTO " + versionsCachedRepresentationsTableName + "(" + version_id + "," + cached_representation_id + " ) VALUES (:versionId, :cachedId)";
160        return getSimpleJdbcTemplate().update(sql, params);
161    }
162   
163   
164   
165   
166   
167   
168    ///////////////////// PRIVATE //////////////////////////////
169   
170    private boolean versionIsInUse(Number versionsID) {
171        String sql = "SELECT " + source_id + " FROM " + sourcesVersionsTableName + " WHERE " + version_id + "= ? LIMIT 1";
172        List<Number> result = getSimpleJdbcTemplate().query(sql, sourceIDRowMapper, versionsID);
173        if (result.size() > 0) {
174            return true;
175        }
176        return false;
177    }
178   
179     private final RowMapper<Number> sourceIDRowMapper = new RowMapper<Number>() {
180        @Override
181        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
182            return rs.getInt(source_id);
183        }
184    };
185}
Note: See TracBrowser for help on using the repository browser.