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