source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.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: 8.1 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.Helpers;
21import eu.dasish.annotation.backend.dao.SourceDao;
22import eu.dasish.annotation.schema.Source;
23import eu.dasish.annotation.schema.SourceInfo;
24import java.sql.ResultSet;
25import java.sql.SQLException;
26import java.util.ArrayList;
27import java.util.HashMap;
28import java.util.List;
29import java.util.Map;
30import java.util.UUID;
31import javax.sql.DataSource;
32import javax.xml.datatype.DatatypeConfigurationException;
33import javax.xml.datatype.XMLGregorianCalendar;
34import org.springframework.jdbc.core.RowMapper;
35
36/**
37 *
38 * @author olhsha
39 */
40public class JdbcSourceDao extends JdbcResourceDao implements SourceDao {
41
42   
43    public JdbcSourceDao(DataSource dataSource) {
44        setDataSource(dataSource);
45        internalIdName = source_id;
46        resourceTableName = sourceTableName;
47    }
48
49
50    //////////////////////// GETTERS ///////////////////////////////////
51    @Override
52    public Source getSource(Number internalID) {
53        String sql = "SELECT " + sourceStar + "FROM " + sourceTableName + " WHERE " + source_id + " = ?";
54        List<Source> result = getSimpleJdbcTemplate().query(sql, sourceRowMapper, internalID);
55        return result.get(0);
56    }
57    private final RowMapper<Source> sourceRowMapper = new RowMapper<Source>() {
58        @Override
59        public Source mapRow(ResultSet rs, int rowNumber) throws SQLException {
60            try {
61                XMLGregorianCalendar xmlDate = Helpers.setXMLGregorianCalendar(rs.getTimestamp(time_stamp));
62                Source result = 
63                        constructSource(UUID.fromString(rs.getString(external_id)), rs.getString(link_uri), rs.getString(version), xmlDate);
64                return result;
65            } catch (DatatypeConfigurationException e) {
66                // TODO: which logger are we going to use?
67                System.out.println("Cannot construct time stamp: probably worng date/time format");
68                return null;
69            }
70        }
71    };
72   
73    /////////////////////////////////////////
74    @Override
75    public List<Number> retrieveVersionList(Number sourceID) {
76        String sql = "SELECT " + version_id + " FROM " + sourcesVersionsTableName + " WHERE " + source_id + " = ?";
77        List<Number> result = getSimpleJdbcTemplate().query(sql, versionIDRowMapper, sourceID);
78        return result;
79    }
80
81   
82     ///////////////////////////////////////////////////////////////////
83    @Override
84    public List<SourceInfo> getSourceInfos(List<Number> sources) {
85        if (sources == null) {
86            return null;
87        }
88        if (sources.isEmpty()) {
89            return new ArrayList<SourceInfo>();
90        }
91
92        String sourceIDs = makeListOfValues(sources);
93        String sql = "SELECT " + external_id + "," + link_uri + "," + version + " FROM " + sourceTableName + " WHERE " + source_id + " IN " + sourceIDs;
94        List<SourceInfo> result = getSimpleJdbcTemplate().query(sql, SourceInfoRowMapper);
95        return result;
96    }
97    private final RowMapper<SourceInfo> SourceInfoRowMapper = new RowMapper<SourceInfo>() {
98        @Override
99        public SourceInfo mapRow(ResultSet rs, int rowNumber) throws SQLException {
100            return constructSourceInfo(UUID.fromString(rs.getString(external_id)), rs.getString(link_uri), rs.getString(version));
101        }
102    };
103
104 
105    /////////////////////////////////////////////////////
106    @Override
107    public List<Number> getSourcesForLink(String link) {
108        StringBuilder sql = new StringBuilder("SELECT ");
109        sql.append(source_id).append(" FROM ").append(sourceTableName).append(" WHERE ").append(link_uri).append(" LIKE '%").append(link).append("%'");
110        List<Number> result = getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper);
111        return result;
112    }
113
114   
115    /////////////////////////////////////////////////
116    @Override
117    public boolean sourceIsInUse(Number sourceID) {
118        String sqlAnnotations = "SELECT " + annotation_id + " FROM " + annotationsSourcesTableName + " WHERE " + source_id + "= ? LIMIT 1";
119        List<Number> resultAnnotations = getSimpleJdbcTemplate().query(sqlAnnotations, annotationIDRowMapper, sourceID);
120        if (resultAnnotations.size() > 0) {
121            return true;
122        }
123        String sqlVersions = "SELECT " + version_id + " FROM " + sourcesVersionsTableName + " WHERE " + source_id + "= ? LIMIT 1";
124        List<Number> resultVersions = getSimpleJdbcTemplate().query(sqlVersions, versionIDRowMapper, sourceID);
125        if (resultVersions.size() > 0) {
126            return true;
127        }
128        return false;
129    }
130   
131 
132   
133
134    ///////////////////////// ADDERS /////////////////////////////////
135    @Override
136    public Number addSource(Source source) throws SQLException {       
137        String externalID = source.getURI();       
138        Map<String, Object> params = new HashMap<String, Object>();
139        params.put("externalId", externalID);
140        params.put("linkUri", source.getLink());
141        params.put("version", source.getVersion());
142        String sql = "INSERT INTO " + sourceTableName + "(" + external_id + "," + link_uri + "," + version + " ) VALUES (:externalId, :linkUri,  :version)";
143        final int affectedRows = getSimpleJdbcTemplate().update(sql, params);       
144        Number internalID = getInternalID(UUID.fromString(externalID));
145        return internalID;
146    }
147   
148   
149    ///////////////////////////////////////////////////////////////////
150    @Override
151    public int addSourceVersion(Number sourceID, Number versionID) throws SQLException {
152        Map<String, Object> paramsJoint = new HashMap<String, Object>();
153        paramsJoint.put("sourceId", sourceID);
154        paramsJoint.put("versionId", versionID);
155        String sqlJoint = "INSERT INTO " + sourcesVersionsTableName + "(" + source_id + "," + version_id + " ) VALUES (:sourceId, :versionId)";
156        return getSimpleJdbcTemplate().update(sqlJoint, paramsJoint);
157    }
158   
159////////////////////// DELETERS ////////////////////////
160    @Override
161    public int deleteSource(Number internalID) {
162        if (sourceIsInUse(internalID)){
163            return 0;
164        }
165        String sqlSourcesVersions = "DELETE FROM " + sourceTableName + " WHERE " + source_id + " = ? ";
166        int result = getSimpleJdbcTemplate().update(sqlSourcesVersions, internalID);
167        return result;
168
169    }
170
171   
172    ///////////////////////////////////////////////////////////////////
173    @Override
174    public int deleteAllSourceVersion(Number internalID) {
175        String sqlSourcesVersions = "DELETE FROM " + sourcesVersionsTableName + " WHERE " + source_id + " = ?";
176        int result = getSimpleJdbcTemplate().update(sqlSourcesVersions, internalID);
177        return result;
178
179    }
180 
181   
182  /////////// HELPERS  ////////////////
183   
184
185    private SourceInfo constructSourceInfo(UUID UUID, String link, String version) {
186        SourceInfo sourceInfo = new SourceInfo();
187        sourceInfo.setRef(UUID.toString());
188        sourceInfo.setLink(link);
189        sourceInfo.setVersion(version);
190        return sourceInfo;
191    }
192
193    private Source constructSource(UUID UUID, String link, String version, XMLGregorianCalendar xmlTimeStamp) {
194        Source source = new Source();
195        source.setURI(UUID.toString());
196        source.setTimeSatmp(xmlTimeStamp);
197        source.setLink(link);
198        source.setVersion(version);
199
200        return source;
201    }
202}
Note: See TracBrowser for help on using the repository browser.