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.backend.dao.CachedRepresentationDao; |
---|
23 | import eu.dasish.annotation.backend.dao.DBIntegrityService; |
---|
24 | import eu.dasish.annotation.backend.dao.NotebookDao; |
---|
25 | import eu.dasish.annotation.backend.dao.TargetDao; |
---|
26 | import eu.dasish.annotation.backend.dao.UserDao; |
---|
27 | import eu.dasish.annotation.backend.rest.AnnotationResource; |
---|
28 | import eu.dasish.annotation.schema.Annotation; |
---|
29 | import eu.dasish.annotation.schema.AnnotationBody; |
---|
30 | import eu.dasish.annotation.schema.AnnotationInfo; |
---|
31 | import eu.dasish.annotation.schema.AnnotationInfoList; |
---|
32 | import eu.dasish.annotation.schema.CachedRepresentationFragment; |
---|
33 | import eu.dasish.annotation.schema.CachedRepresentationFragmentList; |
---|
34 | import eu.dasish.annotation.schema.CachedRepresentationInfo; |
---|
35 | import eu.dasish.annotation.schema.Notebook; |
---|
36 | import eu.dasish.annotation.schema.NotebookInfo; |
---|
37 | import eu.dasish.annotation.schema.NotebookInfoList; |
---|
38 | import eu.dasish.annotation.schema.TargetInfoList; |
---|
39 | import eu.dasish.annotation.schema.Permission; |
---|
40 | import eu.dasish.annotation.schema.UserWithPermissionList; |
---|
41 | import eu.dasish.annotation.schema.ReferenceList; |
---|
42 | import eu.dasish.annotation.schema.Target; |
---|
43 | import eu.dasish.annotation.schema.TargetInfo; |
---|
44 | import eu.dasish.annotation.schema.User; |
---|
45 | import eu.dasish.annotation.schema.UserWithPermission; |
---|
46 | import java.io.InputStream; |
---|
47 | import java.lang.Number; |
---|
48 | import java.util.ArrayList; |
---|
49 | import java.util.HashMap; |
---|
50 | import java.util.List; |
---|
51 | import java.util.Map; |
---|
52 | import java.util.UUID; |
---|
53 | import org.springframework.beans.factory.annotation.Autowired; |
---|
54 | import org.slf4j.Logger; |
---|
55 | import org.slf4j.LoggerFactory; |
---|
56 | |
---|
57 | /** |
---|
58 | * |
---|
59 | * @author olhsha |
---|
60 | */ |
---|
61 | public class DBIntegrityServiceImlp implements DBIntegrityService { |
---|
62 | |
---|
63 | @Autowired |
---|
64 | UserDao userDao; |
---|
65 | @Autowired |
---|
66 | CachedRepresentationDao cachedRepresentationDao; |
---|
67 | @Autowired |
---|
68 | TargetDao targetDao; |
---|
69 | @Autowired |
---|
70 | AnnotationDao annotationDao; |
---|
71 | @Autowired |
---|
72 | NotebookDao notebookDao; |
---|
73 | final static protected String admin = "admin"; |
---|
74 | private static final Logger logger = LoggerFactory.getLogger(AnnotationResource.class); |
---|
75 | ////////////////////////////////// |
---|
76 | |
---|
77 | @Override |
---|
78 | public void setServiceURI(String serviceURI) { |
---|
79 | userDao.setServiceURI(serviceURI + "users/"); |
---|
80 | cachedRepresentationDao.setServiceURI(serviceURI + "cached/"); |
---|
81 | targetDao.setServiceURI(serviceURI + "targets/"); |
---|
82 | annotationDao.setServiceURI(serviceURI + "annotations/"); |
---|
83 | //notebookDao.setServiceURI(serviceURI+"notebooks/"); |
---|
84 | } |
---|
85 | |
---|
86 | ///////////// GETTERS ////////////////////////// |
---|
87 | @Override |
---|
88 | public Number getAnnotationInternalIdentifier(UUID externalID) { |
---|
89 | return annotationDao.getInternalID(externalID); |
---|
90 | } |
---|
91 | |
---|
92 | @Override |
---|
93 | public Number getAnnotationInternalIdentifierFromURI(String uri) { |
---|
94 | return annotationDao.getInternalIDFromURI(uri); |
---|
95 | } |
---|
96 | |
---|
97 | @Override |
---|
98 | public UUID getAnnotationExternalIdentifier(Number annotationID) { |
---|
99 | return annotationDao.getExternalID(annotationID); |
---|
100 | } |
---|
101 | |
---|
102 | @Override |
---|
103 | public Number getTargetInternalIdentifier(UUID externalID) { |
---|
104 | return targetDao.getInternalID(externalID); |
---|
105 | } |
---|
106 | |
---|
107 | @Override |
---|
108 | public UUID getTargetExternalIdentifier(Number targetID) { |
---|
109 | return targetDao.getExternalID(targetID); |
---|
110 | } |
---|
111 | |
---|
112 | @Override |
---|
113 | public String getTargetURI(Number targetID) { |
---|
114 | return targetDao.getURIFromInternalID(targetID); |
---|
115 | } |
---|
116 | |
---|
117 | @Override |
---|
118 | public String getUserURI(Number userID) { |
---|
119 | return userDao.getURIFromInternalID(userID); |
---|
120 | } |
---|
121 | |
---|
122 | @Override |
---|
123 | public Number getUserInternalIdentifier(UUID externalID) { |
---|
124 | return userDao.getInternalID(externalID); |
---|
125 | } |
---|
126 | |
---|
127 | @Override |
---|
128 | public UUID getUserExternalIdentifier(Number userID) { |
---|
129 | return userDao.getExternalID(userID); |
---|
130 | } |
---|
131 | |
---|
132 | @Override |
---|
133 | public Number getCachedRepresentationInternalIdentifier(UUID externalID) { |
---|
134 | return cachedRepresentationDao.getInternalID(externalID); |
---|
135 | } |
---|
136 | |
---|
137 | @Override |
---|
138 | public UUID getCachedRepresentationExternalIdentifier(Number cachedID) { |
---|
139 | return cachedRepresentationDao.getExternalID(cachedID); |
---|
140 | } |
---|
141 | |
---|
142 | @Override |
---|
143 | public Annotation getAnnotation(Number annotationID) { |
---|
144 | if (annotationID != null) { |
---|
145 | Annotation result = annotationDao.getAnnotationWithoutTargetsAndPermissions(annotationID); |
---|
146 | result.setOwnerRef(userDao.getURIFromInternalID(annotationDao.getOwner(annotationID))); |
---|
147 | List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID); |
---|
148 | TargetInfoList sis = new TargetInfoList(); |
---|
149 | for (Number targetID : targetIDs) { |
---|
150 | TargetInfo targetInfo = getTargetInfoFromTarget(targetDao.getTarget(targetID)); |
---|
151 | sis.getTargetInfo().add(targetInfo); |
---|
152 | } |
---|
153 | result.setTargets(sis); |
---|
154 | |
---|
155 | result.setPermissions(this.getPermissionsForAnnotation(annotationID)); |
---|
156 | return result; |
---|
157 | } else { |
---|
158 | return null; |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | @Override |
---|
163 | public Number getAnnotationOwner(Number annotationID) { |
---|
164 | return annotationDao.getOwner(annotationID); |
---|
165 | } |
---|
166 | |
---|
167 | /////////////////////////////////////////////////// |
---|
168 | // TODO UNIT tests |
---|
169 | @Override |
---|
170 | public UserWithPermissionList getPermissionsForAnnotation(Number annotationID) { |
---|
171 | if (annotationID != null) { |
---|
172 | List<Map<Number, String>> principalsPermissions = annotationDao.getPermissions(annotationID); |
---|
173 | UserWithPermissionList result = new UserWithPermissionList(); |
---|
174 | List<UserWithPermission> list = result.getUserWithPermission(); |
---|
175 | for (Map<Number, String> principalPermission : principalsPermissions) { |
---|
176 | |
---|
177 | Number[] principal = new Number[1]; |
---|
178 | principalPermission.keySet().toArray(principal); |
---|
179 | |
---|
180 | UserWithPermission userWithPermission = new UserWithPermission(); |
---|
181 | userWithPermission.setRef(userDao.getURIFromInternalID(principal[0])); |
---|
182 | userWithPermission.setPermission(Permission.fromValue(principalPermission.get(principal[0]))); |
---|
183 | |
---|
184 | list.add(userWithPermission); |
---|
185 | } |
---|
186 | return result; |
---|
187 | } else { |
---|
188 | return null; |
---|
189 | } |
---|
190 | |
---|
191 | } |
---|
192 | |
---|
193 | //////////////////////////////////////////////////////////////////////// |
---|
194 | @Override |
---|
195 | public List<Number> getFilteredAnnotationIDs(UUID ownerId, String link, String text, Number inloggedUserID, String access, String namespace, String after, String before) { |
---|
196 | |
---|
197 | Number ownerID = (ownerId != null) ? userDao.getInternalID(ownerId) : null; |
---|
198 | if (ownerID != null) { |
---|
199 | if ("owner".equals(access) && !inloggedUserID.equals(ownerID)) { |
---|
200 | logger.info("The inlogged user cannot be the owner of the annotations owned by " + ownerId.toString()); |
---|
201 | return null; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | //filtering on tables "target" and "annotations_targets" |
---|
206 | List<Number> annotationIDsForTargets = null; |
---|
207 | if (link != null) { |
---|
208 | List<Number> targetIDs = targetDao.getTargetsReferringTo(link); |
---|
209 | annotationIDsForTargets = annotationDao.getAnnotationIDsForTargets(targetIDs); |
---|
210 | if (annotationIDsForTargets == null) { |
---|
211 | logger.info("There are no annotations for the targets referring to " + link + "."); |
---|
212 | return null; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | // filtering in the table "annotation" |
---|
217 | if (ownerID == null && "owner".equals(access)) { |
---|
218 | ownerID = inloggedUserID; |
---|
219 | } |
---|
220 | List<Number> annotationIDs = annotationDao.getFilteredAnnotationIDs(ownerID, text, namespace, after, before); |
---|
221 | if (annotationIDs != null) { |
---|
222 | if (annotationIDsForTargets != null) { |
---|
223 | annotationIDs.retainAll(annotationIDsForTargets); |
---|
224 | } else { |
---|
225 | // nothing to filter on link == null |
---|
226 | } |
---|
227 | } else { |
---|
228 | logger.info("There are no annotations for the given filters on the annotation table."); |
---|
229 | return null; |
---|
230 | } |
---|
231 | |
---|
232 | // filtering on table "annotations_principals_permissions" |
---|
233 | if ("reader".equals(access) || "writer".equals(access)) { |
---|
234 | // owner != inloggedUser |
---|
235 | List<Number> annotationIDsPermission = annotationDao.getAnnotationIDsForUserWithPermission(inloggedUserID, access); |
---|
236 | if (annotationIDsPermission != null) { |
---|
237 | annotationIDs.retainAll(annotationIDsPermission); |
---|
238 | } else { |
---|
239 | logger.info("There are no annotations for which the inlogged user has access " + access); |
---|
240 | return null; |
---|
241 | } |
---|
242 | } else { |
---|
243 | // inloggedUser == owner |
---|
244 | } |
---|
245 | return annotationIDs; |
---|
246 | } |
---|
247 | |
---|
248 | @Override |
---|
249 | public ReferenceList getAnnotationTargets(Number annotationID) { |
---|
250 | ReferenceList result = new ReferenceList(); |
---|
251 | List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID); |
---|
252 | for (Number targetID : targetIDs) { |
---|
253 | result.getRef().add(targetDao.getURIFromInternalID(targetID)); |
---|
254 | } |
---|
255 | return result; |
---|
256 | } |
---|
257 | |
---|
258 | @Override |
---|
259 | public List<String> getTargetsWithNoCachedRepresentation(Number annotationID) { |
---|
260 | if (annotationID == null) { |
---|
261 | return null; |
---|
262 | } |
---|
263 | List<String> result = new ArrayList<String>(); |
---|
264 | List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID); |
---|
265 | for (Number targetID : targetIDs) { |
---|
266 | List<Number> versions = targetDao.getCachedRepresentations(targetID); |
---|
267 | if (versions == null) { |
---|
268 | result.add(targetDao.getURIFromInternalID(targetID)); |
---|
269 | } else { |
---|
270 | if (versions.isEmpty()) { |
---|
271 | result.add(targetDao.getURIFromInternalID(targetID)); |
---|
272 | } |
---|
273 | |
---|
274 | } |
---|
275 | } |
---|
276 | return result; |
---|
277 | } |
---|
278 | |
---|
279 | @Override |
---|
280 | public List<String> getUsersWithNoInfo(Number annotationID) { |
---|
281 | if (annotationID == null) { |
---|
282 | return null; |
---|
283 | } |
---|
284 | List<String> result = new ArrayList<String>(); |
---|
285 | List<Map<Number, String>> usersWithPermissions = annotationDao.getPermissions(annotationID); |
---|
286 | for (Map<Number, String> userWithPermission : usersWithPermissions) { |
---|
287 | Number[] userID = new Number[1]; |
---|
288 | userWithPermission.keySet().toArray(userID); |
---|
289 | User user = userDao.getUser(userID[0]); |
---|
290 | |
---|
291 | if (user.getDisplayName() == null || user.getDisplayName().trim().isEmpty() || user.getEMail() == null || user.getEMail().trim().isEmpty()) { |
---|
292 | result.add(userDao.getURIFromInternalID(userID[0])); |
---|
293 | |
---|
294 | } |
---|
295 | } |
---|
296 | return result; |
---|
297 | } |
---|
298 | |
---|
299 | @Override |
---|
300 | public AnnotationInfoList getFilteredAnnotationInfos(UUID ownerId, String word, String text, Number inloggedUserID, String access, String namespace, String after, String before) { |
---|
301 | List<Number> annotationIDs = this.getFilteredAnnotationIDs(ownerId, word, text, inloggedUserID, access, namespace, after, before); |
---|
302 | if (annotationIDs != null) { |
---|
303 | AnnotationInfoList result = new AnnotationInfoList(); |
---|
304 | for (Number annotationID : annotationIDs) { |
---|
305 | AnnotationInfo annotationInfo = annotationDao.getAnnotationInfoWithoutTargets(annotationID); |
---|
306 | annotationInfo.setTargets(this.getAnnotationTargets(annotationID)); |
---|
307 | annotationInfo.setOwnerRef(userDao.getURIFromInternalID(annotationDao.getOwner(annotationID))); |
---|
308 | result.getAnnotationInfo().add(annotationInfo); |
---|
309 | |
---|
310 | } |
---|
311 | |
---|
312 | return result; |
---|
313 | } else { |
---|
314 | return null; |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | @Override |
---|
319 | public AnnotationInfoList getAllAnnotationInfos() { |
---|
320 | List<Number> annotationIDs = annotationDao.getAllAnnotationIDs(); |
---|
321 | if (annotationIDs != null) { |
---|
322 | AnnotationInfoList result = new AnnotationInfoList(); |
---|
323 | for (Number annotationID : annotationIDs) { |
---|
324 | Number ownerID = annotationDao.getOwner(annotationID); |
---|
325 | ReferenceList targets = getAnnotationTargets(annotationID); |
---|
326 | AnnotationInfo annotationInfo = annotationDao.getAnnotationInfoWithoutTargets(annotationID); |
---|
327 | annotationInfo.setTargets(targets); |
---|
328 | if (ownerID != null) { |
---|
329 | annotationInfo.setOwnerRef(userDao.getURIFromInternalID(ownerID)); |
---|
330 | } else { |
---|
331 | annotationInfo.setOwnerRef("ACHTUNG: This annotation does not have an owner in the DB!!!!"); |
---|
332 | } |
---|
333 | result.getAnnotationInfo().add(annotationInfo); |
---|
334 | } |
---|
335 | |
---|
336 | return result; |
---|
337 | } else { |
---|
338 | return null; |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | // TODO unit test |
---|
343 | @Override |
---|
344 | public Target getTarget(Number internalID) { |
---|
345 | Target result = targetDao.getTarget(internalID); |
---|
346 | result.setSiblingTargets(getTargetsForTheSameLinkAs(internalID)); |
---|
347 | Map<Number, String> cachedIDsFragments = targetDao.getCachedRepresentationFragmentPairs(internalID); |
---|
348 | CachedRepresentationFragmentList cachedRepresentationFragmentList = new CachedRepresentationFragmentList(); |
---|
349 | for (Number key : cachedIDsFragments.keySet()) { |
---|
350 | CachedRepresentationFragment cachedRepresentationFragment = new CachedRepresentationFragment(); |
---|
351 | cachedRepresentationFragment.setRef(cachedRepresentationDao.getURIFromInternalID(key)); |
---|
352 | cachedRepresentationFragment.setFragmentString(cachedIDsFragments.get(key)); |
---|
353 | cachedRepresentationFragmentList.getCached().add(cachedRepresentationFragment); |
---|
354 | } |
---|
355 | result.setCachedRepresentatinons(cachedRepresentationFragmentList); |
---|
356 | return result; |
---|
357 | } |
---|
358 | |
---|
359 | // TODO unit test |
---|
360 | @Override |
---|
361 | public CachedRepresentationInfo getCachedRepresentationInfo(Number internalID) { |
---|
362 | return cachedRepresentationDao.getCachedRepresentationInfo(internalID); |
---|
363 | } |
---|
364 | |
---|
365 | //TODO unit test |
---|
366 | @Override |
---|
367 | public InputStream getCachedRepresentationBlob(Number cachedID) { |
---|
368 | return cachedRepresentationDao.getCachedRepresentationBlob(cachedID); |
---|
369 | } |
---|
370 | |
---|
371 | @Override |
---|
372 | public ReferenceList getTargetsForTheSameLinkAs(Number targetID) { |
---|
373 | List<Number> targetIDs = targetDao.getTargetsForLink(targetDao.getLink(targetID)); |
---|
374 | ReferenceList referenceList = new ReferenceList(); |
---|
375 | for (Number siblingID : targetIDs) { |
---|
376 | referenceList.getRef().add(targetDao.externalIDtoURI(targetDao.getExternalID(siblingID).toString())); |
---|
377 | } |
---|
378 | return referenceList; |
---|
379 | } |
---|
380 | |
---|
381 | @Override |
---|
382 | public User getUser(Number userID) { |
---|
383 | return userDao.getUser(userID); |
---|
384 | } |
---|
385 | |
---|
386 | @Override |
---|
387 | public User getUserByInfo(String eMail) { |
---|
388 | return userDao.getUserByInfo(eMail); |
---|
389 | } |
---|
390 | |
---|
391 | @Override |
---|
392 | public String getUserRemoteID(Number internalID) { |
---|
393 | return userDao.getRemoteID(internalID); |
---|
394 | } |
---|
395 | |
---|
396 | @Override |
---|
397 | public Permission getPermission(Number annotationID, Number userID) { |
---|
398 | return annotationDao.getPermission(annotationID, userID); |
---|
399 | } |
---|
400 | |
---|
401 | @Override |
---|
402 | public Number getUserInternalIDFromRemoteID(String remoteID) { |
---|
403 | return userDao.getUserInternalIDFromRemoteID(remoteID); |
---|
404 | } |
---|
405 | |
---|
406 | @Override |
---|
407 | public String getTypeOfUserAccount(Number userID) { |
---|
408 | return userDao.getTypeOfUserAccount(userID); |
---|
409 | } |
---|
410 | |
---|
411 | @Override |
---|
412 | public boolean canRead(Number userID, Number annotationID) { |
---|
413 | if (userID.equals(annotationDao.getOwner(annotationID)) || userDao.getTypeOfUserAccount(userID).equals(admin)) { |
---|
414 | return true; |
---|
415 | } |
---|
416 | |
---|
417 | final Permission permission = annotationDao.getPermission(annotationID, userID); |
---|
418 | if (permission != null) { |
---|
419 | return (permission.value().equals(Permission.WRITER.value()) || permission.value().equals(Permission.READER.value())); |
---|
420 | } else { |
---|
421 | return false; |
---|
422 | } |
---|
423 | } |
---|
424 | |
---|
425 | @Override |
---|
426 | public boolean canWrite(Number userID, Number annotationID) { |
---|
427 | if (userID.equals(annotationDao.getOwner(annotationID)) || userDao.getTypeOfUserAccount(userID).equals(admin)) { |
---|
428 | return true; |
---|
429 | } |
---|
430 | final Permission permission = annotationDao.getPermission(annotationID, userID); |
---|
431 | if (permission != null) { |
---|
432 | return (permission.value().equals(Permission.WRITER.value())); |
---|
433 | } else { |
---|
434 | return false; |
---|
435 | } |
---|
436 | } |
---|
437 | |
---|
438 | /// notebooks /// |
---|
439 | @Override |
---|
440 | public NotebookInfoList getNotebooks(Number prinipalID, Permission permission) { |
---|
441 | NotebookInfoList result = new NotebookInfoList(); |
---|
442 | List<Number> notebookIDs = notebookDao.getNotebookIDs(prinipalID, permission); |
---|
443 | for (Number notebookID : notebookIDs) { |
---|
444 | NotebookInfo notebookInfo = notebookDao.getNotebookInfoWithoutOwner(notebookID); |
---|
445 | Number ownerID = notebookDao.getOwner(notebookID); |
---|
446 | notebookInfo.setOwnerRef(userDao.getURIFromInternalID(ownerID)); |
---|
447 | result.getNotebookInfo().add(notebookInfo); |
---|
448 | } |
---|
449 | |
---|
450 | return result; |
---|
451 | } |
---|
452 | |
---|
453 | @Override |
---|
454 | public NotebookInfoList getNotebooksOwnedBy(Number principalID) { |
---|
455 | NotebookInfoList result = new NotebookInfoList(); |
---|
456 | List<Number> notebookIDs = notebookDao.getNotebookIDsOwnedBy(principalID); |
---|
457 | String ownerRef = userDao.getURIFromInternalID(principalID); |
---|
458 | for (Number notebookID : notebookIDs) { |
---|
459 | NotebookInfo notebookInfo = notebookDao.getNotebookInfoWithoutOwner(notebookID); |
---|
460 | notebookInfo.setOwnerRef(ownerRef); |
---|
461 | result.getNotebookInfo().add(notebookInfo); |
---|
462 | } |
---|
463 | |
---|
464 | return result; |
---|
465 | } |
---|
466 | |
---|
467 | @Override |
---|
468 | public List<UUID> getPrincipals(Number notebookID, Permission permission) { |
---|
469 | List<UUID> result = new ArrayList<UUID>(); |
---|
470 | List<Number> principalIDs = notebookDao.getPrincipalIDsWithPermission(notebookID, permission); |
---|
471 | for (Number principalID : principalIDs) { |
---|
472 | UUID uuid = userDao.getExternalID(principalID); |
---|
473 | result.add(uuid); |
---|
474 | } |
---|
475 | return result; |
---|
476 | } |
---|
477 | |
---|
478 | @Override |
---|
479 | public NotebookInfo getNotebookInfo(Number notebookID) { |
---|
480 | NotebookInfo result = notebookDao.getNotebookInfoWithoutOwner(notebookID); |
---|
481 | result.setOwnerRef(userDao.getURIFromInternalID(notebookDao.getOwner(notebookID))); |
---|
482 | return result; |
---|
483 | } |
---|
484 | |
---|
485 | ///////////////////////////////////////////////////////////// |
---|
486 | @Override |
---|
487 | public List<UUID> getAnnotationsForNotebook(Number notebookID, int startAnnotation, int maximumAnnotations, String orderedBy, boolean desc) { |
---|
488 | List<Number> annotationIDs = notebookDao.getAnnotations(notebookID); |
---|
489 | |
---|
490 | if (startAnnotation < -1) { |
---|
491 | logger.info("Variable's startAnnotation value " + startAnnotation + " is invalid. I will return null."); |
---|
492 | return null; |
---|
493 | } |
---|
494 | |
---|
495 | if (maximumAnnotations < -1) { |
---|
496 | logger.info("Variable's maximumAnnotations value " + maximumAnnotations + " is invalid. I will return null."); |
---|
497 | return null; |
---|
498 | } |
---|
499 | |
---|
500 | int offset = (startAnnotation > 0) ? startAnnotation - 1 : 0; |
---|
501 | String direction = desc ? " DESC " : " ASC "; |
---|
502 | List<Number> selectedAnnotIDs = annotationDao.sublistOrderedAnnotationIDs(annotationIDs, offset, maximumAnnotations, orderedBy, direction); |
---|
503 | List<UUID> annotationUUIDs = new ArrayList<UUID>(); |
---|
504 | for (Number annotationID : selectedAnnotIDs) { |
---|
505 | annotationUUIDs.add(annotationDao.getExternalID(annotationID)); |
---|
506 | } |
---|
507 | return annotationUUIDs; |
---|
508 | } |
---|
509 | |
---|
510 | ///// UPDATERS ///////////////// |
---|
511 | @Override |
---|
512 | public boolean updateAccount(UUID userExternalID, String account) { |
---|
513 | return userDao.updateAccount(userExternalID, account); |
---|
514 | } |
---|
515 | |
---|
516 | @Override |
---|
517 | public int updateAnnotationPrincipalPermission(Number annotationID, Number userID, Permission permission) { |
---|
518 | if (permission != null) { |
---|
519 | return annotationDao.updateAnnotationPrincipalPermission(annotationID, userID, permission); |
---|
520 | } else { |
---|
521 | return annotationDao.deleteAnnotationPrincipalPermission(annotationID, userID); |
---|
522 | } |
---|
523 | } |
---|
524 | |
---|
525 | @Override |
---|
526 | public int updatePermissions(Number annotationID, UserWithPermissionList permissionList) { |
---|
527 | |
---|
528 | List<UserWithPermission> usersWithPermissions = permissionList.getUserWithPermission(); |
---|
529 | int result = 0; |
---|
530 | for (UserWithPermission userWithPermission : usersWithPermissions) { |
---|
531 | Number userID = userDao.getInternalID(UUID.fromString(userDao.stringURItoExternalID(userWithPermission.getRef()))); |
---|
532 | if (userID != null) { |
---|
533 | Permission permission = userWithPermission.getPermission(); |
---|
534 | Permission currentPermission = annotationDao.getPermission(annotationID, userID); |
---|
535 | if (currentPermission != null) { |
---|
536 | if (!permission.value().equals(currentPermission.value())) { |
---|
537 | result = result + annotationDao.updateAnnotationPrincipalPermission(annotationID, userID, permission); |
---|
538 | } |
---|
539 | } else { |
---|
540 | result = result + annotationDao.addAnnotationPrincipalPermission(annotationID, userID, permission); |
---|
541 | } |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | return result; |
---|
546 | } |
---|
547 | |
---|
548 | // TODO: optimize (not chnaged targets should not be deleted) |
---|
549 | // TODO: unit test |
---|
550 | @Override |
---|
551 | public int updateAnnotation(Annotation annotation) { |
---|
552 | int updatedAnnotations = annotationDao.updateAnnotation(annotation, userDao.getInternalIDFromURI(annotation.getOwnerRef())); |
---|
553 | Number annotationID = annotationDao.getInternalIDFromURI(annotation.getURI()); |
---|
554 | int deletedTargets = annotationDao.deleteAllAnnotationTarget(annotationID); |
---|
555 | int deletedPrinsipalsPermissions = annotationDao.deleteAnnotationPrincipalPermissions(annotationID); |
---|
556 | int addedTargets = addTargets(annotation, annotationID); |
---|
557 | int addedPrincipalsPermissions = addPrincipalsPermissions(annotation.getPermissions().getUserWithPermission(), annotationID); |
---|
558 | return updatedAnnotations; |
---|
559 | } |
---|
560 | |
---|
561 | // TODO: unit test |
---|
562 | @Override |
---|
563 | public int updateAnnotationBody(Number internalID, AnnotationBody annotationBody) { |
---|
564 | String[] body = annotationDao.retrieveBodyComponents(annotationBody); |
---|
565 | return annotationDao.updateAnnotationBody(internalID, body[0], body[1], annotationBody.getXmlBody() != null); |
---|
566 | } |
---|
567 | |
---|
568 | @Override |
---|
569 | public Number updateUser(User user) { |
---|
570 | return userDao.updateUser(user); |
---|
571 | } |
---|
572 | /// notebooks /// |
---|
573 | |
---|
574 | @Override |
---|
575 | public boolean updateNotebookMetadata(Number notebookID, NotebookInfo upToDateNotebookInfo) { |
---|
576 | Number ownerID = userDao.getInternalIDFromURI(upToDateNotebookInfo.getOwnerRef()); |
---|
577 | return notebookDao.updateNotebookMetadata(notebookID, upToDateNotebookInfo.getTitle(), ownerID); |
---|
578 | } |
---|
579 | |
---|
580 | @Override |
---|
581 | public boolean addAnnotationToNotebook(Number notebookID, Number annotationID) { |
---|
582 | return notebookDao.addAnnotationToNotebook(notebookID, annotationID); |
---|
583 | } |
---|
584 | |
---|
585 | /////////////// ADDERS ///////////////////////////////// |
---|
586 | @Override |
---|
587 | public Number[] addCachedForTarget(Number targetID, String fragmentDescriptor, CachedRepresentationInfo cachedInfo, InputStream cachedBlob) { |
---|
588 | Number[] result = new Number[2]; |
---|
589 | result[1] = cachedRepresentationDao.getInternalIDFromURI(cachedInfo.getURI()); |
---|
590 | if (result[1] == null) { |
---|
591 | result[1] = cachedRepresentationDao.addCachedRepresentation(cachedInfo, cachedBlob); |
---|
592 | } |
---|
593 | result[0] = targetDao.addTargetCachedRepresentation(targetID, result[1], fragmentDescriptor); |
---|
594 | return result; |
---|
595 | |
---|
596 | } |
---|
597 | |
---|
598 | // TODo: mapping uri to external ID |
---|
599 | @Override |
---|
600 | public Map<String, String> addTargetsForAnnotation(Number annotationID, List<TargetInfo> targets) { |
---|
601 | Map<String, String> result = new HashMap<String, String>(); |
---|
602 | Number targetIDRunner; |
---|
603 | for (TargetInfo targetInfo : targets) { |
---|
604 | targetIDRunner = targetDao.getInternalIDFromURI(targetInfo.getRef()); |
---|
605 | if (targetIDRunner != null) { |
---|
606 | int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetIDRunner); |
---|
607 | } else { |
---|
608 | Target newTarget = createFreshTarget(targetInfo); |
---|
609 | Number targetID = targetDao.addTarget(newTarget); |
---|
610 | String targetTemporaryID = targetDao.stringURItoExternalID(targetInfo.getRef()); |
---|
611 | result.put(targetTemporaryID, targetDao.getExternalID(targetID).toString()); |
---|
612 | int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetID); |
---|
613 | } |
---|
614 | } |
---|
615 | return result; |
---|
616 | } |
---|
617 | |
---|
618 | @Override |
---|
619 | public Number addUsersAnnotation(Number ownerID, Annotation annotation) { |
---|
620 | Number annotationID = annotationDao.addAnnotation(annotation, ownerID); |
---|
621 | int affectedAnnotRows = addTargets(annotation, annotationID); |
---|
622 | if (annotation.getPermissions() != null) { |
---|
623 | if (annotation.getPermissions().getUserWithPermission() != null) { |
---|
624 | int addedPrincipalsPermissions = this.addPrincipalsPermissions(annotation.getPermissions().getUserWithPermission(), annotationID); |
---|
625 | } |
---|
626 | } |
---|
627 | return annotationID; |
---|
628 | } |
---|
629 | |
---|
630 | @Override |
---|
631 | public Number addUser(User user, String remoteID) { |
---|
632 | if (userDao.userExists(user)) { |
---|
633 | return null; |
---|
634 | } else { |
---|
635 | return userDao.addUser(user, remoteID); |
---|
636 | } |
---|
637 | } |
---|
638 | |
---|
639 | @Override |
---|
640 | public int addAnnotationPrincipalPermission(Number annotationID, Number userID, Permission permission) { |
---|
641 | return annotationDao.addAnnotationPrincipalPermission(annotationID, userID, permission); |
---|
642 | } |
---|
643 | |
---|
644 | //////////// notebooks ////// |
---|
645 | |
---|
646 | @Override |
---|
647 | public Number createNotebook(Notebook notebook, Number ownerID) { |
---|
648 | Number notebookID = notebookDao.createNotebookWithoutPermissionsAndAnnotations(notebook, ownerID); |
---|
649 | boolean updateOwner = notebookDao.setOwner(notebookID, ownerID); |
---|
650 | List<UserWithPermission> permissions = notebook.getPermissions().getUserWithPermission(); |
---|
651 | for (UserWithPermission principalPermission : permissions) { |
---|
652 | Number principalID = userDao.getInternalIDFromURI(principalPermission.getRef()); |
---|
653 | Permission permission = principalPermission.getPermission(); |
---|
654 | boolean updatePermissions = notebookDao.addPermissionToNotebook(notebookID, principalID, permission); |
---|
655 | } |
---|
656 | return notebookID; |
---|
657 | } |
---|
658 | |
---|
659 | @Override |
---|
660 | public boolean createAnnotationInNotebook(Number notebookID, Annotation annotation, Number ownerID) { |
---|
661 | Number newAnnotationID = this.addUsersAnnotation(ownerID, annotation); |
---|
662 | return notebookDao.addAnnotationToNotebook(notebookID, newAnnotationID); |
---|
663 | } |
---|
664 | |
---|
665 | ////////////// DELETERS ////////////////// |
---|
666 | @Override |
---|
667 | public int deleteUser(Number userID) { |
---|
668 | return userDao.deleteUser(userID); |
---|
669 | } |
---|
670 | |
---|
671 | ////////////// DELETERS ////////////////// |
---|
672 | @Override |
---|
673 | public int deleteUserSafe(Number userID) { |
---|
674 | return userDao.deleteUserSafe(userID); |
---|
675 | } |
---|
676 | |
---|
677 | @Override |
---|
678 | public int[] deleteCachedRepresentationOfTarget(Number versionID, Number cachedID) { |
---|
679 | int[] result = new int[2]; |
---|
680 | result[0] = targetDao.deleteTargetCachedRepresentation(versionID, cachedID); |
---|
681 | if (result[0] > 0) { |
---|
682 | result[1] = cachedRepresentationDao.deleteCachedRepresentation(cachedID); |
---|
683 | } else { |
---|
684 | result[1] = 0; |
---|
685 | |
---|
686 | } |
---|
687 | return result; |
---|
688 | } |
---|
689 | |
---|
690 | @Override |
---|
691 | public int[] deleteAllCachedRepresentationsOfTarget(Number TargetID) { |
---|
692 | int[] result = new int[2]; |
---|
693 | result[0] = 0; |
---|
694 | result[1] = 0; |
---|
695 | List<Number> cachedIDs = targetDao.getCachedRepresentations(TargetID); |
---|
696 | for (Number cachedID : cachedIDs) { |
---|
697 | int[] currentResult = this.deleteCachedRepresentationOfTarget(TargetID, cachedID); |
---|
698 | result[0] = result[0] + currentResult[0]; |
---|
699 | result[1] = result[1] + currentResult[1]; |
---|
700 | } |
---|
701 | return result; |
---|
702 | } |
---|
703 | |
---|
704 | @Override |
---|
705 | public int[] deleteAnnotation(Number annotationID) { |
---|
706 | int[] result = new int[4]; |
---|
707 | result[1] = annotationDao.deleteAnnotationPrincipalPermissions(annotationID); |
---|
708 | List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID); |
---|
709 | result[2] = annotationDao.deleteAllAnnotationTarget(annotationID); |
---|
710 | result[0] = annotationDao.deleteAnnotation(annotationID); |
---|
711 | result[3] = 0; |
---|
712 | if (targetIDs != null) { |
---|
713 | for (Number targetID : targetIDs) { |
---|
714 | this.deleteAllCachedRepresentationsOfTarget(targetID); |
---|
715 | result[3] = result[3] + targetDao.deleteTarget(targetID); |
---|
716 | |
---|
717 | } |
---|
718 | } |
---|
719 | return result; |
---|
720 | } |
---|
721 | |
---|
722 | @Override |
---|
723 | public boolean deleteNotebook(Number notebookID) { |
---|
724 | boolean deletePermissions = notebookDao.deleteAllPermissionsForNotebook(notebookID); |
---|
725 | boolean deleteAnnotations = notebookDao.deleteAllAnnotationsFromNotebook(notebookID); |
---|
726 | return notebookDao.deleteNotebook(notebookID); |
---|
727 | } |
---|
728 | |
---|
729 | ////////////// HELPERS //////////////////// |
---|
730 | private Target createFreshTarget(TargetInfo targetInfo) { |
---|
731 | Target target = new Target(); |
---|
732 | target.setLink(targetInfo.getLink()); |
---|
733 | target.setVersion(targetInfo.getVersion()); |
---|
734 | return target; |
---|
735 | } |
---|
736 | |
---|
737 | private int addTargets(Annotation annotation, Number annotationID) { |
---|
738 | List<TargetInfo> targets = annotation.getTargets().getTargetInfo(); |
---|
739 | Map<String, String> targetIdPairs = addTargetsForAnnotation(annotationID, targets); |
---|
740 | AnnotationBody annotationBody = annotation.getBody(); |
---|
741 | String bodyText; |
---|
742 | String newBodyText; |
---|
743 | String mimeType; |
---|
744 | if (annotationBody.getXmlBody() != null) { |
---|
745 | bodyText = Helpers.elementToString(annotation.getBody().getXmlBody().getAny()); |
---|
746 | mimeType = annotationBody.getXmlBody().getMimeType(); |
---|
747 | } else { |
---|
748 | if (annotation.getBody().getTextBody() != null) { |
---|
749 | bodyText = annotation.getBody().getTextBody().getBody(); |
---|
750 | mimeType = annotationBody.getTextBody().getMimeType(); |
---|
751 | } else { |
---|
752 | logger.error("The client has sent ill-formed annotation body."); |
---|
753 | return -1; |
---|
754 | } |
---|
755 | } |
---|
756 | newBodyText = Helpers.replace(bodyText, targetIdPairs); |
---|
757 | return annotationDao.updateAnnotationBody(annotationID, newBodyText, mimeType, annotationBody.getXmlBody() != null); |
---|
758 | } |
---|
759 | |
---|
760 | private int addPrincipalsPermissions(List<UserWithPermission> permissions, Number annotationID) { |
---|
761 | int addedPermissions = 0; |
---|
762 | for (UserWithPermission permission : permissions) { |
---|
763 | addedPermissions = addedPermissions + annotationDao.addAnnotationPrincipalPermission(annotationID, userDao.getInternalIDFromURI(permission.getRef()), permission.getPermission()); |
---|
764 | |
---|
765 | } |
---|
766 | return addedPermissions; |
---|
767 | } |
---|
768 | |
---|
769 | private TargetInfo getTargetInfoFromTarget(Target target) { |
---|
770 | TargetInfo targetInfo = new TargetInfo(); |
---|
771 | targetInfo.setRef(target.getURI()); |
---|
772 | targetInfo.setLink(target.getLink()); |
---|
773 | targetInfo.setVersion(target.getVersion()); |
---|
774 | return targetInfo; |
---|
775 | } |
---|
776 | } |
---|