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