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.AnnotationDao; |
---|
22 | import eu.dasish.annotation.schema.Annotation; |
---|
23 | import eu.dasish.annotation.schema.AnnotationBody; |
---|
24 | import eu.dasish.annotation.schema.AnnotationBody.TextBody; |
---|
25 | import eu.dasish.annotation.schema.AnnotationBody.XmlBody; |
---|
26 | import eu.dasish.annotation.schema.AnnotationInfo; |
---|
27 | import eu.dasish.annotation.schema.Permission; |
---|
28 | import java.lang.String; |
---|
29 | import java.sql.ResultSet; |
---|
30 | import java.sql.SQLException; |
---|
31 | import java.sql.Timestamp; |
---|
32 | import java.util.ArrayList; |
---|
33 | import java.util.HashMap; |
---|
34 | import java.util.List; |
---|
35 | import java.util.Map; |
---|
36 | import java.util.UUID; |
---|
37 | import javax.sql.DataSource; |
---|
38 | import org.slf4j.Logger; |
---|
39 | import org.slf4j.LoggerFactory; |
---|
40 | import org.springframework.jdbc.core.RowMapper; |
---|
41 | |
---|
42 | /** |
---|
43 | * Created on : Jun 27, 2013, 10:30:52 AM |
---|
44 | * |
---|
45 | * @author Peter Withers <peter.withers@mpi.nl> |
---|
46 | */ |
---|
47 | public class JdbcAnnotationDao extends JdbcResourceDao implements AnnotationDao { |
---|
48 | |
---|
49 | private final Logger loggerAnnotationDao = LoggerFactory.getLogger(JdbcAnnotationDao.class); |
---|
50 | |
---|
51 | public JdbcAnnotationDao(DataSource dataSource) { |
---|
52 | setDataSource(dataSource); |
---|
53 | internalIdName = annotation_id; |
---|
54 | resourceTableName = annotationTableName; |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | public void setServiceURI(String serviceURI) { |
---|
60 | _serviceURI = serviceURI; |
---|
61 | } |
---|
62 | |
---|
63 | ///////////// GETTERS ///////////// |
---|
64 | @Override |
---|
65 | public List<Number> retrieveTargetIDs(Number annotationID) { |
---|
66 | if (annotationID != null) { |
---|
67 | StringBuilder sql = new StringBuilder("SELECT DISTINCT "); |
---|
68 | sql.append(target_id).append(" FROM ").append(annotationsTargetsTableName).append(" WHERE ").append(annotation_id).append("= ?"); |
---|
69 | return getSimpleJdbcTemplate().query(sql.toString(), TargetIDRowMapper, annotationID); |
---|
70 | } else { |
---|
71 | loggerAnnotationDao.debug(nullArgument); |
---|
72 | return null; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /////////////////////////////////////////////////////////////////// |
---|
77 | @Override |
---|
78 | public List<Map<Number, String>> getPermissions(Number annotationID) { |
---|
79 | if (annotationID == null) { |
---|
80 | loggerAnnotationDao.debug(nullArgument); |
---|
81 | return null; |
---|
82 | } |
---|
83 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
84 | sql.append(principal_id).append(",").append(permission).append(" FROM ").append(permissionsTableName).append(" WHERE ").append(annotation_id).append(" = ?"); |
---|
85 | return getSimpleJdbcTemplate().query(sql.toString(), principalsPermissionsRowMapper, annotationID); |
---|
86 | } |
---|
87 | private final RowMapper<Map<Number, String>> principalsPermissionsRowMapper = new RowMapper<Map<Number, String>>() { |
---|
88 | @Override |
---|
89 | public Map<Number, String> mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
90 | Map<Number, String> result = new HashMap<Number, String>(); |
---|
91 | result.put(rs.getInt(principal_id), rs.getString(permission)); |
---|
92 | return result; |
---|
93 | } |
---|
94 | }; |
---|
95 | |
---|
96 | @Override |
---|
97 | public Permission getPermission(Number annotationID, Number userID) { |
---|
98 | if (annotationID == null) { |
---|
99 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
100 | return null; |
---|
101 | } |
---|
102 | |
---|
103 | if (userID == null) { |
---|
104 | loggerAnnotationDao.debug("userID: "+nullArgument); |
---|
105 | return null; |
---|
106 | } |
---|
107 | |
---|
108 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
109 | sql.append(permission).append(" FROM ").append(permissionsTableName).append(" WHERE "). |
---|
110 | append(annotation_id).append(" = ").append(annotationID.toString()).append(" AND "). |
---|
111 | append(principal_id).append(" = ").append(userID.toString()).append(" LIMIT 1"); |
---|
112 | List<Permission> result = getSimpleJdbcTemplate().query(sql.toString(), permissionRowMapper); |
---|
113 | if (result == null) { |
---|
114 | return null; |
---|
115 | } |
---|
116 | if (result.isEmpty()) { |
---|
117 | return null; |
---|
118 | } |
---|
119 | return result.get(0); |
---|
120 | } |
---|
121 | private final RowMapper<Permission> permissionRowMapper = new RowMapper<Permission>() { |
---|
122 | @Override |
---|
123 | public Permission mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
124 | return Permission.fromValue(rs.getString(permission)); |
---|
125 | } |
---|
126 | }; |
---|
127 | |
---|
128 | @Override |
---|
129 | public List<Number> getAnnotationIDsForUserWithPermission(Number userID, String[] permissionStrings) { |
---|
130 | if (permissionStrings == null) { |
---|
131 | loggerAnnotationDao.debug("premissionStrings: "+nullArgument); |
---|
132 | return null; |
---|
133 | } |
---|
134 | |
---|
135 | if (userID == null) { |
---|
136 | loggerAnnotationDao.debug("userID: "+nullArgument); |
---|
137 | return null; |
---|
138 | } |
---|
139 | |
---|
140 | String values = stringsToValuesString(permissionStrings); |
---|
141 | |
---|
142 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
143 | sql.append(annotation_id).append(" FROM ").append(permissionsTableName).append(" WHERE "). |
---|
144 | append(principal_id).append(" = ").append(userID.toString()).append(" AND "). |
---|
145 | append(permission).append(" IN ").append(values); |
---|
146 | return getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper); |
---|
147 | } |
---|
148 | |
---|
149 | private String stringsToValuesString(String[] strings) { |
---|
150 | if (strings == null) { |
---|
151 | return null; |
---|
152 | } |
---|
153 | |
---|
154 | int length = strings.length; |
---|
155 | if (length == 0) { |
---|
156 | return null; |
---|
157 | } |
---|
158 | String result = "("; |
---|
159 | for (int i = 0; i < length - 1; i++) { |
---|
160 | result = result + "'" + strings[i] + "', "; |
---|
161 | } |
---|
162 | result = result + "'" + strings[length - 1] + "')"; |
---|
163 | return result; |
---|
164 | } |
---|
165 | |
---|
166 | //////////////////////////////////////////////////////////////////////// |
---|
167 | @Override |
---|
168 | public List<Number> getFilteredAnnotationIDs(List<Number> annotationIDs, String text, String namespace, Timestamp after, Timestamp before) { |
---|
169 | |
---|
170 | StringBuilder sql = new StringBuilder("SELECT DISTINCT "); |
---|
171 | sql.append(annotation_id).append(" FROM ").append(annotationTableName).append(" WHERE TRUE "); |
---|
172 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
173 | |
---|
174 | if (annotationIDs == null) { |
---|
175 | loggerAnnotationDao.debug("annotationIDs: "+nullArgument); |
---|
176 | return null; |
---|
177 | } else { |
---|
178 | if (annotationIDs.isEmpty()) { |
---|
179 | return new ArrayList<Number>(); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | String values = makeListOfValues(annotationIDs); |
---|
184 | sql.append(" AND ").append(annotation_id).append(" IN ").append(values); |
---|
185 | |
---|
186 | |
---|
187 | if (after != null) { |
---|
188 | sql.append(" AND ").append(last_modified).append(" > :after"); |
---|
189 | params.put("after", after); |
---|
190 | } |
---|
191 | |
---|
192 | if (before != null) { |
---|
193 | sql.append(" AND ").append(last_modified).append(" < :before"); |
---|
194 | params.put("before", before); |
---|
195 | } |
---|
196 | |
---|
197 | if (text != null) { |
---|
198 | sql.append(" AND ").append(body_text).append(" LIKE '%").append(text).append("%'"); |
---|
199 | } |
---|
200 | |
---|
201 | return getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper, params); |
---|
202 | } |
---|
203 | |
---|
204 | ///////////////////////////////////////// |
---|
205 | |
---|
206 | @Override |
---|
207 | public List<Number> getAllAnnotationIDs(){ |
---|
208 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
209 | sql.append(annotation_id).append(" , ").append(last_modified).append(" FROM ").append(annotationTableName).append(" ORDER BY ").append(last_modified).append(" DESC"); |
---|
210 | return getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper); |
---|
211 | } |
---|
212 | |
---|
213 | ////////////////////////////// |
---|
214 | @Override |
---|
215 | public List<Number> retrieveAnnotationList(List<Number> targetIDs) { |
---|
216 | if (targetIDs == null) { |
---|
217 | loggerAnnotationDao.debug("targetIDs: "+nullArgument); |
---|
218 | return null; |
---|
219 | } |
---|
220 | if (targetIDs.isEmpty()) { |
---|
221 | return new ArrayList<Number>(); |
---|
222 | } |
---|
223 | String values = makeListOfValues(targetIDs); |
---|
224 | StringBuilder query = new StringBuilder("SELECT DISTINCT "); |
---|
225 | query.append(annotation_id).append(" FROM ").append(annotationsTargetsTableName).append(" WHERE ").append(target_id).append(" IN "); |
---|
226 | query.append(values); |
---|
227 | return getSimpleJdbcTemplate().query(query.toString(), internalIDRowMapper); |
---|
228 | } |
---|
229 | |
---|
230 | @Override |
---|
231 | public AnnotationInfo getAnnotationInfoWithoutTargets(Number annotationID) { |
---|
232 | if (annotationID == null) { |
---|
233 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
234 | return null; |
---|
235 | } |
---|
236 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
237 | sql.append(annotationStar).append(" FROM ").append(annotationTableName).append(" WHERE ").append(annotation_id).append(" = ? "); |
---|
238 | List<AnnotationInfo> result = getSimpleJdbcTemplate().query(sql.toString(), annotationInfoRowMapper, annotationID); |
---|
239 | if (result == null) { |
---|
240 | return null; |
---|
241 | } |
---|
242 | if (result.isEmpty()) { |
---|
243 | return null; |
---|
244 | } |
---|
245 | |
---|
246 | return result.get(0); |
---|
247 | } |
---|
248 | private final RowMapper<AnnotationInfo> annotationInfoRowMapper = new RowMapper<AnnotationInfo>() { |
---|
249 | @Override |
---|
250 | public AnnotationInfo mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
251 | AnnotationInfo annotationInfo = new AnnotationInfo(); |
---|
252 | annotationInfo.setRef(externalIDtoURI(rs.getString(external_id))); |
---|
253 | annotationInfo.setHeadline(rs.getString(headline)); |
---|
254 | annotationInfo.setLastModified(timeStampToXMLGregorianCalendar(rs)); |
---|
255 | return annotationInfo; |
---|
256 | } |
---|
257 | }; |
---|
258 | |
---|
259 | ///////////////////////////////////////////////// |
---|
260 | /** |
---|
261 | * |
---|
262 | * @param annotationIDs |
---|
263 | * @return list of annotation references corresponding to the annotation-ids |
---|
264 | * from the input list if the input list is null or empty (zero elements) |
---|
265 | * returns an empty list there may be annotationIDs which are not in the DB |
---|
266 | * (so that's why we need this method). |
---|
267 | */ |
---|
268 | @Override |
---|
269 | public List<String> getAnnotationREFs(List<Number> annotationIDs) { |
---|
270 | if (annotationIDs == null) { |
---|
271 | loggerAnnotationDao.debug("annotationIDs: "+nullArgument); |
---|
272 | return null; |
---|
273 | } |
---|
274 | if (annotationIDs.isEmpty()) { |
---|
275 | return (new ArrayList<String>()); |
---|
276 | } |
---|
277 | |
---|
278 | String values = makeListOfValues(annotationIDs); |
---|
279 | StringBuilder sql = new StringBuilder("SELECT DISTINCT "); |
---|
280 | sql.append(external_id).append(" FROM ").append(annotationTableName).append(" WHERE ").append(annotationAnnotation_id).append(" IN ").append(values); |
---|
281 | return getSimpleJdbcTemplate().query(sql.toString(), annotationREFRowMapper); |
---|
282 | } |
---|
283 | private final RowMapper<String> annotationREFRowMapper = new RowMapper<String>() { |
---|
284 | @Override |
---|
285 | public String mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
286 | return externalIDtoURI(rs.getString(external_id)); |
---|
287 | } |
---|
288 | }; |
---|
289 | |
---|
290 | ////////////////////////////////////////////////////////////////////////// |
---|
291 | @Override |
---|
292 | public Annotation getAnnotationWithoutTargetsAndPermissions(Number annotationID) { |
---|
293 | if (annotationID == null) { |
---|
294 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
295 | return null; |
---|
296 | } |
---|
297 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
298 | sql.append(annotationStar).append(" FROM ").append(annotationTableName).append(" WHERE ").append(annotationAnnotation_id).append("= ? LIMIT 1"); |
---|
299 | List<Annotation> respond = getSimpleJdbcTemplate().query(sql.toString(), annotationRowMapper, annotationID); |
---|
300 | return (respond.isEmpty() ? null : respond.get(0)); |
---|
301 | } |
---|
302 | private final RowMapper<Annotation> annotationRowMapper = new RowMapper<Annotation>() { |
---|
303 | @Override |
---|
304 | public Annotation mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
305 | Annotation annotation = new Annotation(); |
---|
306 | annotation.setHeadline(rs.getString(headline)); |
---|
307 | AnnotationBody body = new AnnotationBody(); |
---|
308 | if (rs.getBoolean(is_xml)) { |
---|
309 | body.setTextBody(null); |
---|
310 | XmlBody xmlBody = new XmlBody(); |
---|
311 | xmlBody.setMimeType(rs.getString(body_mimetype)); |
---|
312 | xmlBody.setAny(Helpers.stringToElement(rs.getString(body_text))); |
---|
313 | body.setXmlBody(xmlBody); |
---|
314 | } else { |
---|
315 | body.setXmlBody(null); |
---|
316 | TextBody textBody = new TextBody(); |
---|
317 | textBody.setMimeType(rs.getString(body_mimetype)); |
---|
318 | textBody.setBody(rs.getString(body_text)); |
---|
319 | body.setTextBody(textBody); |
---|
320 | } |
---|
321 | annotation.setBody(body); |
---|
322 | |
---|
323 | annotation.setTargets(null); |
---|
324 | annotation.setURI(externalIDtoURI(rs.getString(external_id))); |
---|
325 | annotation.setLastModified(timeStampToXMLGregorianCalendar(rs)); |
---|
326 | return annotation; |
---|
327 | } |
---|
328 | }; |
---|
329 | |
---|
330 | @Override |
---|
331 | public Number getOwner(Number annotationID){ |
---|
332 | if (annotationID == null) { |
---|
333 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
334 | return null; |
---|
335 | } |
---|
336 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
337 | sql.append(principal_id).append(" FROM ").append(permissionsTableName).append(" WHERE "). |
---|
338 | append(permission).append("= 'owner' AND "). |
---|
339 | append(annotation_id).append("= ? LIMIT 1"); |
---|
340 | List<Number> respond = getSimpleJdbcTemplate().query(sql.toString(), principalIDRowMapper, annotationID); |
---|
341 | return (respond.isEmpty() ? null : respond.get(0)); |
---|
342 | } |
---|
343 | |
---|
344 | ///////////////////////////// |
---|
345 | @Override |
---|
346 | public boolean annotationIsInUse(Number annotationID) { |
---|
347 | StringBuilder sqlNotebooks = new StringBuilder("SELECT "); |
---|
348 | sqlNotebooks.append(notebook_id).append(" FROM ").append(notebooksAnnotationsTableName).append(" WHERE ").append(annotation_id).append("= ? LIMIT 1"); |
---|
349 | List<Number> resultNotebooks = getSimpleJdbcTemplate().query(sqlNotebooks.toString(), notebookIDRowMapper, annotationID); |
---|
350 | if (resultNotebooks.size() > 0) { |
---|
351 | return true; |
---|
352 | } |
---|
353 | |
---|
354 | StringBuilder sqlTargets = new StringBuilder("SELECT "); |
---|
355 | sqlTargets.append(target_id).append(" FROM ").append(annotationsTargetsTableName).append(" WHERE ").append(annotation_id).append("= ? LIMIT 1"); |
---|
356 | List<Number> resultTargets = getSimpleJdbcTemplate().query(sqlTargets.toString(), TargetIDRowMapper, annotationID); |
---|
357 | if (resultTargets.size() > 0) { |
---|
358 | return true; |
---|
359 | } |
---|
360 | |
---|
361 | StringBuilder sqlPermissions = new StringBuilder("SELECT "); |
---|
362 | sqlPermissions.append(principal_id).append(" FROM ").append(permissionsTableName).append(" WHERE ").append(annotation_id).append("= ? LIMIT 1"); |
---|
363 | List<Number> resultPermissions = getSimpleJdbcTemplate().query(sqlPermissions.toString(), principalIDRowMapper, annotationID); |
---|
364 | return (resultPermissions.size() > 0); |
---|
365 | } |
---|
366 | |
---|
367 | //////////// UPDATERS ///////////// |
---|
368 | @Override |
---|
369 | public int updateAnnotationBody(Number annotationID, String text, String mimeType, Boolean isXml) { |
---|
370 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
371 | params.put("annotationID", annotationID); |
---|
372 | params.put("bodyText", text); |
---|
373 | params.put("bodyMimeType", mimeType); |
---|
374 | params.put("isXml", isXml); |
---|
375 | |
---|
376 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
377 | sql.append(annotationTableName).append(" SET "). |
---|
378 | append(last_modified).append("= default,"). |
---|
379 | append(body_text).append("= :bodyText, "). |
---|
380 | append(body_mimetype).append("= :bodyMimeType, "). |
---|
381 | append(is_xml).append("= :isXml"). |
---|
382 | append(" WHERE ").append(annotation_id).append("= :annotationID"); |
---|
383 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
384 | return affectedRows; |
---|
385 | } |
---|
386 | |
---|
387 | // TODO Unit test |
---|
388 | @Override |
---|
389 | public int updateAnnotation(Annotation annotation) { |
---|
390 | |
---|
391 | String[] body = retrieveBodyComponents(annotation.getBody()); |
---|
392 | String externalID = stringURItoExternalID(annotation.getURI()); |
---|
393 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
394 | params.put("bodyText", body[0]); |
---|
395 | params.put("bodyMimeType", body[1]); |
---|
396 | params.put("headline", annotation.getHeadline()); |
---|
397 | params.put("isXml", annotation.getBody().getXmlBody() != null); |
---|
398 | params.put("externalID", externalID); |
---|
399 | |
---|
400 | |
---|
401 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
402 | sql.append(annotationTableName).append(" SET "). |
---|
403 | append(body_text).append("= :bodyText ,"). |
---|
404 | append(body_mimetype).append("= :bodyMimeType ,"). |
---|
405 | append(headline).append("= :headline ,"). |
---|
406 | append(last_modified).append("= default,"). |
---|
407 | append(is_xml).append("= :isXml"). |
---|
408 | append(" WHERE ").append(external_id).append("= :externalID"); |
---|
409 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
410 | return affectedRows; |
---|
411 | } |
---|
412 | |
---|
413 | @Override |
---|
414 | public int updateAnnotationPrincipalPermission(Number annotationID, Number userID, Permission permission) { |
---|
415 | |
---|
416 | if (annotationID == null) { |
---|
417 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
418 | return 0; |
---|
419 | } |
---|
420 | |
---|
421 | if (userID == null) { |
---|
422 | loggerAnnotationDao.debug("userID: "+nullArgument); |
---|
423 | return 0; |
---|
424 | } |
---|
425 | |
---|
426 | if (permission == null) { |
---|
427 | loggerAnnotationDao.debug("permission: "+nullArgument); |
---|
428 | return 0; |
---|
429 | } |
---|
430 | |
---|
431 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
432 | |
---|
433 | params.put("annotationID", annotationID); |
---|
434 | params.put("principalID", userID); |
---|
435 | |
---|
436 | if (permission != null) { |
---|
437 | params.put("permission", permission.value()); |
---|
438 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
439 | sql.append(permissionsTableName).append(" SET "). |
---|
440 | append(this.permission).append("= :permission"). |
---|
441 | append(" WHERE ").append(annotation_id).append("= :annotationID"). |
---|
442 | append(" AND ").append(principal_id).append("= :principalID"); |
---|
443 | return getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
444 | } else { |
---|
445 | StringBuilder sql = new StringBuilder("DELETE FROM "); |
---|
446 | sql.append(permissionsTableName).append(" WHERE ").append(annotation_id).append("= :annotationID"). |
---|
447 | append(" AND ").append(principal_id).append("= :principalID"); |
---|
448 | return (getSimpleJdbcTemplate().update(sql.toString(), params)); |
---|
449 | } |
---|
450 | |
---|
451 | } |
---|
452 | |
---|
453 | //////////// ADDERS //////////////////////// |
---|
454 | @Override |
---|
455 | public Number addAnnotation(Annotation annotation) { |
---|
456 | |
---|
457 | String[] body = retrieveBodyComponents(annotation.getBody()); |
---|
458 | |
---|
459 | if (annotation == null) { |
---|
460 | loggerAnnotationDao.debug("annotation: "+nullArgument); |
---|
461 | return 0; |
---|
462 | } |
---|
463 | |
---|
464 | // generate a new annotation ID |
---|
465 | UUID externalID = UUID.randomUUID(); |
---|
466 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
467 | params.put("externalId", externalID.toString()); |
---|
468 | params.put("headline", annotation.getHeadline()); |
---|
469 | params.put("bodyText", body[0]); |
---|
470 | params.put("bodyMimeType", body[1]); |
---|
471 | params.put("isXml", annotation.getBody().getXmlBody() != null); |
---|
472 | |
---|
473 | StringBuilder sql = new StringBuilder("INSERT INTO "); |
---|
474 | sql.append(annotationTableName).append("(").append(external_id); |
---|
475 | sql.append(",").append(headline).append(",").append(body_text).append(",").append(body_mimetype).append(",").append(is_xml). |
---|
476 | append(" ) VALUES (:externalId, :headline, :bodyText, :bodyMimeType, :isXml)"); |
---|
477 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
478 | return ((affectedRows > 0) ? getInternalID(externalID) : null); |
---|
479 | } |
---|
480 | |
---|
481 | ////////////////////////////////////////////////////////////////////////////////// |
---|
482 | @Override |
---|
483 | public int addAnnotationTarget(Number annotationID, Number targetID) { |
---|
484 | |
---|
485 | if (annotationID == null) { |
---|
486 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
487 | return 0; |
---|
488 | } |
---|
489 | |
---|
490 | if (targetID == null) { |
---|
491 | loggerAnnotationDao.debug("targetID: "+nullArgument); |
---|
492 | return 0; |
---|
493 | } |
---|
494 | |
---|
495 | Map<String, Object> paramsAnnotationsTargets = new HashMap<String, Object>(); |
---|
496 | paramsAnnotationsTargets.put("annotationId", annotationID); |
---|
497 | paramsAnnotationsTargets.put("targetId", targetID); |
---|
498 | StringBuilder sqlAnnotationsTargets = new StringBuilder("INSERT INTO "); |
---|
499 | sqlAnnotationsTargets.append(annotationsTargetsTableName).append("(").append(annotation_id).append(",").append(target_id).append(" ) VALUES (:annotationId, :targetId)"); |
---|
500 | return getSimpleJdbcTemplate().update(sqlAnnotationsTargets.toString(), paramsAnnotationsTargets); |
---|
501 | } |
---|
502 | |
---|
503 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
504 | @Override |
---|
505 | public int addAnnotationPrincipalPermission(Number annotationID, Number userID, Permission permission) { |
---|
506 | |
---|
507 | if (annotationID == null) { |
---|
508 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
509 | return 0; |
---|
510 | } |
---|
511 | |
---|
512 | if (userID == null) { |
---|
513 | loggerAnnotationDao.debug("userID: "+nullArgument); |
---|
514 | return 0; |
---|
515 | } |
---|
516 | |
---|
517 | if (permission == null) { |
---|
518 | loggerAnnotationDao.debug("permission: "+nullArgument); |
---|
519 | return 0; |
---|
520 | } |
---|
521 | |
---|
522 | |
---|
523 | Map<String, Object> paramsPermissions = new HashMap<String, Object>(); |
---|
524 | paramsPermissions.put("annotationId", annotationID); |
---|
525 | paramsPermissions.put("principalId", userID); |
---|
526 | paramsPermissions.put("status", permission.value()); |
---|
527 | StringBuilder sqlUpdatePermissionTable = new StringBuilder("INSERT INTO "); |
---|
528 | sqlUpdatePermissionTable.append(permissionsTableName).append(" (").append(annotation_id).append(",").append(principal_id).append(",").append(this.permission).append(") VALUES (:annotationId, :principalId, :status)"); |
---|
529 | final int affectedPermissions = getSimpleJdbcTemplate().update(sqlUpdatePermissionTable.toString(), paramsPermissions); |
---|
530 | return affectedPermissions; |
---|
531 | } |
---|
532 | |
---|
533 | ////////////////////////////////////////////////////////////////////////////////// |
---|
534 | /////////////////// DELETERS ////////////////////////// |
---|
535 | @Override |
---|
536 | public int deleteAnnotation(Number annotationID) { |
---|
537 | if (annotationID != null) { |
---|
538 | if (annotationIsInUse(annotationID)) { |
---|
539 | return 0; |
---|
540 | } |
---|
541 | StringBuilder sqlAnnotation = new StringBuilder("DELETE FROM "); |
---|
542 | sqlAnnotation.append(annotationTableName).append(" where ").append(annotation_id).append(" = ?"); |
---|
543 | return (getSimpleJdbcTemplate().update(sqlAnnotation.toString(), annotationID)); |
---|
544 | } else { |
---|
545 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
546 | return 0; |
---|
547 | } |
---|
548 | } |
---|
549 | |
---|
550 | @Override |
---|
551 | public int deleteAllAnnotationTarget(Number annotationID) { |
---|
552 | if (annotationID != null) { |
---|
553 | StringBuilder sqlTargetTargets = new StringBuilder("DELETE FROM "); |
---|
554 | sqlTargetTargets.append(annotationsTargetsTableName).append(" WHERE ").append(annotation_id).append(" = ?"); |
---|
555 | return getSimpleJdbcTemplate().update(sqlTargetTargets.toString(), annotationID); // # removed "annotations_target_Targets" rows |
---|
556 | } else { |
---|
557 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
558 | return 0; |
---|
559 | } |
---|
560 | } |
---|
561 | |
---|
562 | ////////////////////////////////////////////////////// |
---|
563 | @Override |
---|
564 | public int deleteAnnotationPrincipalPermissions(Number annotationID) { |
---|
565 | if (annotationID != null) { |
---|
566 | StringBuilder sqlPermissions = new StringBuilder("DELETE FROM "); |
---|
567 | sqlPermissions.append(permissionsTableName).append(" WHERE ").append(annotation_id).append(" = ?"); |
---|
568 | return getSimpleJdbcTemplate().update(sqlPermissions.toString(), annotationID); // removed "permission" rows |
---|
569 | } else { |
---|
570 | loggerAnnotationDao.debug("annotationID: "+nullArgument); |
---|
571 | return 0; |
---|
572 | } |
---|
573 | |
---|
574 | } |
---|
575 | |
---|
576 | /////////////// helpers ////////////////// |
---|
577 | @Override |
---|
578 | public String[] retrieveBodyComponents(AnnotationBody annotationBody) { |
---|
579 | boolean body_is_xml = annotationBody.getXmlBody() != null; |
---|
580 | String[] result = new String[2]; |
---|
581 | if (body_is_xml) { |
---|
582 | result[0] = Helpers.elementToString(annotationBody.getXmlBody().getAny()); |
---|
583 | result[1] = annotationBody.getXmlBody().getMimeType(); |
---|
584 | } else { |
---|
585 | TextBody textBody = annotationBody.getTextBody(); |
---|
586 | if (textBody != null) { |
---|
587 | result[0] = textBody.getBody(); |
---|
588 | result[1] = textBody.getMimeType(); |
---|
589 | } else { |
---|
590 | loggerAnnotationDao.error("Ill-formed body: both options, xml-body and text-body, are set to null. "); |
---|
591 | return null; |
---|
592 | } |
---|
593 | } |
---|
594 | return result; |
---|
595 | } |
---|
596 | } |
---|