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