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

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

replacing String with StringBuilder? for the SQL request-strings

File size: 6.9 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.schema.Version;
22import java.sql.ResultSet;
23import java.sql.SQLException;
24import java.util.HashMap;
25import java.util.List;
26import java.util.Map;
27import java.util.UUID;
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    /////////////// GETTERS /////////////////////
46    @Override
47    public Version getVersion(Number internalID) {
48
49        StringBuilder sql  = new StringBuilder("SELECT ");
50        sql.append(versionStar).append(" FROM ").append(versionTableName).append(" WHERE ").append(version_id).append("= ? LIMIT 1");
51        List<Version> result = getSimpleJdbcTemplate().query(sql.toString(), versionRowMapper, internalID);
52        return (!result.isEmpty() ? result.get(0) : null);
53    }
54    private final RowMapper<Version> versionRowMapper = new RowMapper<Version>() {
55        @Override
56        public Version mapRow(ResultSet rs, int rowNumber) throws SQLException {
57            Version result = new Version();
58            // TODO: clarify situation with the attribute cached representation
59            //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
60            // TODO: attribute URI (external-id is missing)
61            result.setVersion(externalIDtoURI(_serviceURI, rs.getString(external_id)));
62            return result;
63        }
64    };
65
66    ////////////////////////////////////////////////////////////////////////////
67    @Override
68    public List<Number> retrieveCachedRepresentationList(Number versionID) {
69        StringBuilder sql  = new StringBuilder("SELECT DISTINCT ");
70        sql.append(cached_representation_id).append(" FROM ").append(versionsCachedRepresentationsTableName).append(" WHERE ").append(version_id).append("= ?");
71        return getSimpleJdbcTemplate().query(sql.toString(), cachedIDRowMapper, versionID);
72    }
73
74 
75   
76    ///////////////////////////////////////////////////////////////
77   
78    @Override
79    public boolean versionIsInUse(Number versionsID) {
80        StringBuilder sqlSources  = new StringBuilder("SELECT ");
81        sqlSources.append(source_id).append(" FROM ").append(sourcesVersionsTableName).append(" WHERE ").append(version_id).append("= ? LIMIT 1");
82        List<Number> resultSources = getSimpleJdbcTemplate().query(sqlSources.toString(), sourceIDRowMapper, versionsID);
83        if (resultSources.size() > 0) {
84            return true;
85        }
86        StringBuilder sqlCached  = new StringBuilder("SELECT ");
87        sqlCached.append(cached_representation_id).append(" FROM ").append(versionsCachedRepresentationsTableName).append(" WHERE ").append(version_id).append("= ? LIMIT 1");
88        List<Number> resultCached = getSimpleJdbcTemplate().query(sqlCached.toString(), cachedIDRowMapper, versionsID);
89        return (resultCached.size() > 0) ;
90    }
91   
92 
93   
94    /////////////////// ADDERS ///////////////////
95   
96    @Override
97    public Number addVersion(Version freshVersion) {
98        UUID externalIdentifier = UUID.randomUUID();
99        String newExternalIdentifier = externalIdentifier.toString();
100
101        Map<String, Object> params = new HashMap<String, Object>();
102        params.put("externalId", newExternalIdentifier);
103        //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)
104        params.put("version", newExternalIdentifier);
105        StringBuilder sql = new StringBuilder("INSERT INTO ");
106        sql.append(versionTableName).append("(").append(external_id).append(",").append(version).append(" ) VALUES (:externalId, :version)");
107        final int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params);
108        return (affectedRows>0 ? getInternalID(externalIdentifier) : null);
109    }
110   
111       ////////////////////////////////////////////
112    @Override
113    public int addVersionCachedRepresentation(Number versionID, Number cachedID){       
114    Map<String, Object> params = new HashMap<String, Object>();
115        params.put("versionId", versionID);
116        params.put("cachedId", cachedID);
117        StringBuilder sql = new StringBuilder("INSERT INTO ");
118        sql.append(versionsCachedRepresentationsTableName).append("(").append(version_id).append(",").append(cached_representation_id).append(" ) VALUES (:versionId, :cachedId)");
119        return getSimpleJdbcTemplate().update(sql.toString(), params);
120    }
121   
122   
123    /////////////// DELETERS ////////////////////////
124   
125    @Override
126    public int deleteVersion(Number internalID) {
127        if (versionIsInUse(internalID)) {
128            return 0;
129        }       
130        StringBuilder sql = new StringBuilder("DELETE FROM ");
131        sql.append(versionTableName).append(" where ").append(version_id).append(" = ?");
132        return getSimpleJdbcTemplate().update(sql.toString(), internalID);
133
134    }
135
136   
137   
138    ////////////////////////////////////////////
139    @Override
140    public int deleteVersionCachedRepresentation(Number versionID, Number cachedID){
141    Map<String, Object> params = new HashMap<String, Object>();
142         params.put("versionId", versionID);
143         params.put("cachedId", cachedID);   
144    StringBuilder sql = new StringBuilder("DELETE FROM ");
145    sql.append(versionsCachedRepresentationsTableName).append(" WHERE ").append(version_id).append(" = :versionId AND ");
146    sql.append(cached_representation_id).append("= :cachedId");
147    return (getSimpleJdbcTemplate().update(sql.toString(), params));
148    }
149   
150    ////////////////////////////////////////////
151    @Override
152    public int deleteAllVersionCachedRepresentation(Number versionID){
153    StringBuilder sql = new StringBuilder("DELETE FROM ");
154    sql.append(versionsCachedRepresentationsTableName).append(" WHERE ").append(version_id).append(" = ?");
155    return (getSimpleJdbcTemplate().update(sql.toString(), versionID));
156    }
157       
158 
159 
160}
Note: See TracBrowser for help on using the repository browser.