source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/DBIntegrityServiceImlp.java @ 4028

Last change on this file since 4028 was 4028, checked in by olhsha, 11 years ago

updating body of the annotation. Testing Get methods and verifying their produced xml-s. Ok.

File size: 21.7 KB
Line 
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 */
18package eu.dasish.annotation.backend.dao.impl;
19
20import eu.dasish.annotation.backend.Helpers;
21import eu.dasish.annotation.backend.dao.AnnotationDao;
22import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
23import eu.dasish.annotation.backend.dao.DBIntegrityService;
24import eu.dasish.annotation.backend.dao.TargetDao;
25import eu.dasish.annotation.backend.dao.UserDao;
26import eu.dasish.annotation.backend.rest.AnnotationResource;
27import eu.dasish.annotation.schema.Annotation;
28import eu.dasish.annotation.schema.AnnotationBody;
29import eu.dasish.annotation.schema.AnnotationInfo;
30import eu.dasish.annotation.schema.AnnotationInfoList;
31import eu.dasish.annotation.schema.CachedRepresentationFragment;
32import eu.dasish.annotation.schema.CachedRepresentationFragmentList;
33import eu.dasish.annotation.schema.CachedRepresentationInfo;
34import eu.dasish.annotation.schema.TargetInfoList;
35import eu.dasish.annotation.schema.Permission;
36import eu.dasish.annotation.schema.UserWithPermissionList;
37import eu.dasish.annotation.schema.ReferenceList;
38import eu.dasish.annotation.schema.Target;
39import eu.dasish.annotation.schema.TargetInfo;
40import eu.dasish.annotation.schema.User;
41import eu.dasish.annotation.schema.UserWithPermission;
42import java.io.InputStream;
43import java.lang.Number;
44import java.sql.Timestamp;
45import java.util.ArrayList;
46import java.util.HashMap;
47import java.util.List;
48import java.util.Map;
49import java.util.UUID;
50import org.springframework.beans.factory.annotation.Autowired;
51import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
54/**
55 *
56 * @author olhsha
57 */
58public class DBIntegrityServiceImlp implements DBIntegrityService {
59
60    @Autowired
61    UserDao userDao;
62    @Autowired
63    CachedRepresentationDao cachedRepresentationDao;
64    @Autowired
65    TargetDao targetDao;
66    @Autowired
67    AnnotationDao annotationDao;
68    private static final Logger logger = LoggerFactory.getLogger(AnnotationResource.class);
69    //////////////////////////////////
70
71    @Override
72    public void setServiceURI(String serviceURI) {
73        userDao.setServiceURI(serviceURI + "users/");
74        cachedRepresentationDao.setServiceURI(serviceURI + "cached/");
75        targetDao.setServiceURI(serviceURI + "targets/");
76        annotationDao.setServiceURI(serviceURI + "annotations/");
77        //notebookDao.setServiceURI(serviceURI+"notebooks/");
78    }
79
80    ///////////// GETTERS //////////////////////////
81    @Override
82    public Number getAnnotationInternalIdentifier(UUID externalID) {
83        return annotationDao.getInternalID(externalID);
84    }
85
86    @Override
87    public Number getAnnotationInternalIdentifierFromURI(String uri) {
88        return annotationDao.getInternalIDFromURI(uri);
89    }
90
91    @Override
92    public UUID getAnnotationExternalIdentifier(Number annotationID) {
93        return annotationDao.getExternalID(annotationID);
94    }
95
96    ///////////// GETTERS //////////////////////////
97    @Override
98    public Number getTargetInternalIdentifier(UUID externalID) {
99        return targetDao.getInternalID(externalID);
100    }
101
102    @Override
103    public UUID getTargetExternalIdentifier(Number targetID) {
104        return targetDao.getExternalID(targetID);
105    }
106
107    @Override
108    public String getTargetURI(Number targetID) {
109        return targetDao.getURIFromInternalID(targetID);
110    }
111
112    @Override
113    public String getUserURI(Number userID) {
114        return userDao.getURIFromInternalID(userID);
115    }
116
117    @Override
118    public Number getUserInternalIdentifier(UUID externalID) {
119        return userDao.getInternalID(externalID);
120    }
121
122    @Override
123    public UUID getUserExternalIdentifier(Number userID) {
124        return userDao.getExternalID(userID);
125    }
126
127    @Override
128    public Number getCachedRepresentationInternalIdentifier(UUID externalID) {
129        return cachedRepresentationDao.getInternalID(externalID);
130    }
131
132    @Override
133    public UUID getCachedRepresentationExternalIdentifier(Number cachedID) {
134        return cachedRepresentationDao.getExternalID(cachedID);
135    }
136
137    ////////////////////////////////////////////////////////
138    // TODO: refactor, Target grabbing should be made a separate private method
139    @Override
140    public Annotation getAnnotation(Number annotationID) {
141        if (annotationID != null) {
142            Map<Annotation, Number> annotationOwner = annotationDao.getAnnotationWithoutTargetsAndPermissions(annotationID);
143
144            Annotation[] annotations = new Annotation[1];
145            annotationOwner.keySet().toArray(annotations);
146            Annotation result = annotations[0];
147            result.setOwnerRef(userDao.getURIFromInternalID(annotationOwner.get(result)));
148
149            List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
150            TargetInfoList sis = new TargetInfoList();
151            for (Number targetID : targetIDs) {
152                Target target = targetDao.getTarget(targetID);
153                TargetInfo targetInfo = new TargetInfo();
154                targetInfo.setLink(target.getLink());
155                targetInfo.setRef(target.getURI());
156                targetInfo.setVersion(target.getVersion());
157                sis.getTargetInfo().add(targetInfo);
158            }
159            result.setTargets(sis);
160
161            result.setPermissions(getPermissionsForAnnotation(annotationID));
162            return result;
163        } else {
164            return null;
165        }
166    }
167
168    ///////////////////////////////////////////////////
169    // TODO UNIT tests
170    @Override
171    public UserWithPermissionList getPermissionsForAnnotation(Number annotationID) {
172        if (annotationID != null) {
173            List<Map<Number, String>> principalsPermissions = annotationDao.getPermissions(annotationID);
174            UserWithPermissionList result = new UserWithPermissionList();
175            List<UserWithPermission> list = result.getUserWithPermission();
176            for (Map<Number, String> principalPermission : principalsPermissions) {
177
178                Number[] principal = new Number[1];
179                principalPermission.keySet().toArray(principal);
180
181                UserWithPermission userWithPermission = new UserWithPermission();
182                userWithPermission.setRef(userDao.getURIFromInternalID(principal[0]));
183                userWithPermission.setPermission(Permission.fromValue(principalPermission.get(principal[0])));
184
185                list.add(userWithPermission);
186            }
187            return result;
188        } else {
189            return null;
190        }
191
192    }
193
194    ////////////////////////////////////////////////////////////////////////
195    @Override
196    public List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String access, String namespace, UUID owner, Timestamp after, Timestamp before) {
197
198        if (access == null) {
199            return null;
200        }
201
202        List<Number> annotationIDs = annotationDao.getAnnotationIDsForUserWithPermission(inloggedUserID, access);
203
204        if (link != null) {
205            List<Number> targetIDs = targetDao.getTargetsReferringTo(link);
206            List<Number> annotationIDsForTargets = annotationDao.retrieveAnnotationList(targetIDs);
207            annotationIDs.retainAll(annotationIDsForTargets);
208        }
209
210        return annotationDao.getFilteredAnnotationIDs(annotationIDs, text, namespace, userDao.getInternalID(owner), after, before);
211    }
212
213    @Override
214    public ReferenceList getAnnotationTargets(Number annotationID) {
215        ReferenceList result = new ReferenceList();
216        List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
217        for (Number targetID : targetIDs) {
218            result.getRef().add(targetDao.getURIFromInternalID(targetID));
219        }
220        return result;
221    }
222
223    @Override
224    public List<String> getTargetsWithNoCachedRepresentation(Number annotationID) {
225        if (annotationID == null) {
226            return null;
227        }
228        List<String> result = new ArrayList<String>();
229        List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
230        for (Number targetID : targetIDs) {
231            List<Number> versions = targetDao.getCachedRepresentations(targetID);
232            if (versions == null) {
233                result.add(targetDao.getURIFromInternalID(targetID));
234            } else {
235                if (versions.isEmpty()) {
236                    result.add(targetDao.getURIFromInternalID(targetID));
237                }
238
239            }
240        }
241        return result;
242    }
243
244    @Override
245    public List<String> getUsersWithNoInfo(Number annotationID) {
246        if (annotationID == null) {
247            return null;
248        }
249        List<String> result = new ArrayList<String>();
250        List<Map<Number, String>> usersWithPermissions = annotationDao.getPermissions(annotationID);
251        for (Map<Number, String> userWithPermission : usersWithPermissions) {
252            Number[] userID = new Number[1];
253            userWithPermission.keySet().toArray(userID);
254            User user = userDao.getUser(userID[0]);
255
256            if (user.getDisplayName() == null || user.getDisplayName().trim().isEmpty() || user.getEMail() == null || user.getEMail().trim().isEmpty()) {
257                result.add(userDao.getURIFromInternalID(userID[0]));
258
259            }
260        }
261        return result;
262    }
263
264    @Override
265    public AnnotationInfoList getFilteredAnnotationInfos(String word, String text, Number inloggedUserID, String access, String namespace, UUID owner, Timestamp after, Timestamp before) {
266        List<Number> annotationIDs = getFilteredAnnotationIDs(word, text, inloggedUserID, access, namespace, owner, after, before);
267        if (annotationIDs != null) {
268            AnnotationInfoList result = new AnnotationInfoList();
269            for (Number annotationID : annotationIDs) {
270                Map<AnnotationInfo, Number> annotationInfoOwnerId = annotationDao.getAnnotationInfoWithoutTargets(annotationID);
271                ReferenceList targets = getAnnotationTargets(annotationID);
272                AnnotationInfo[] annotationInfos = new AnnotationInfo[1];
273                annotationInfoOwnerId.keySet().toArray(annotationInfos);
274                AnnotationInfo annotationInfo = annotationInfos[0];
275                annotationInfo.setTargets(targets);
276                annotationInfo.setOwnerRef(userDao.getURIFromInternalID(annotationInfoOwnerId.get(annotationInfo)));
277                result.getAnnotationInfo().add(annotationInfo);
278            }
279
280            return result;
281        } else {
282            return null;
283        }
284
285    }
286
287    // TODO unit test
288    @Override
289    public Target getTarget(Number internalID) {
290        Target result = targetDao.getTarget(internalID);
291        result.setSiblingTargets(getTargetsForTheSameLinkAs(internalID));
292        Map<Number, String> cachedIDsFragments = targetDao.getCachedRepresentationFragmentPairs(internalID);
293        CachedRepresentationFragmentList cachedRepresentationFragmentList = new CachedRepresentationFragmentList();
294        for (Number key : cachedIDsFragments.keySet()) {
295            CachedRepresentationFragment cachedRepresentationFragment = new CachedRepresentationFragment();
296            cachedRepresentationFragment.setRef(cachedRepresentationDao.getURIFromInternalID(key));
297            cachedRepresentationFragment.setFragmentString(cachedIDsFragments.get(key));
298            cachedRepresentationFragmentList.getCached().add(cachedRepresentationFragment);
299        }
300        result.setCachedRepresentatinons(cachedRepresentationFragmentList);
301        return result;
302    }
303
304    // TODO unit test
305    @Override
306    public CachedRepresentationInfo getCachedRepresentationInfo(Number internalID) {
307        return cachedRepresentationDao.getCachedRepresentationInfo(internalID);
308    }
309
310    //TODO unit test
311    @Override
312    public InputStream getCachedRepresentationBlob(Number cachedID) {
313        return cachedRepresentationDao.getCachedRepresentationBlob(cachedID);
314    }
315
316    @Override
317    public ReferenceList getTargetsForTheSameLinkAs(Number targetID) {
318        List<Number> targetIDs = targetDao.getTargetsForLink(targetDao.getLink(targetID));
319        ReferenceList referenceList = new ReferenceList();
320        for (Number siblingID : targetIDs) {
321            referenceList.getRef().add(targetDao.externalIDtoURI(targetDao.getExternalID(siblingID).toString()));
322        }
323        return referenceList;
324    }
325
326    @Override
327    public User getUser(Number userID) {
328        return userDao.getUser(userID);
329    }
330
331    @Override
332    public User getUserByInfo(String eMail) {
333        return userDao.getUserByInfo(eMail);
334    }
335
336    @Override
337    public String getUserRemoteID(Number internalID) {
338        return userDao.getRemoteID(internalID);
339    }
340
341    @Override
342    public Permission getPermission(Number annotationID, Number userID) {
343        return annotationDao.getPermission(annotationID, userID);
344    }
345
346    @Override
347    public Number getUserInternalIDFromRemoteID(String remoteID) {
348        return userDao.getUserInternalIDFromRemoteID(remoteID);
349    }
350
351    ///// UPDATERS /////////////////
352    @Override
353    public int updateAnnotationPrincipalPermission(Number annotationID, Number userID, Permission permission) {
354        return annotationDao.updateAnnotationPrincipalPermission(annotationID, userID, permission);
355    }
356
357    @Override
358    public int updatePermissions(Number annotationID, UserWithPermissionList permissionList) {
359
360        List<UserWithPermission> usersWithPermissions = permissionList.getUserWithPermission();
361        int result = 0;
362        for (UserWithPermission userWithPermission : usersWithPermissions) {
363            Number userID = userDao.getInternalID(UUID.fromString(userDao.stringURItoExternalID(userWithPermission.getRef())));
364            Permission permission = userWithPermission.getPermission();
365            Permission currentPermission = annotationDao.getPermission(annotationID, userID);
366            if (currentPermission != null) {
367                if (!permission.value().equals(currentPermission.value())) {
368                    result = result + annotationDao.updateAnnotationPrincipalPermission(annotationID, userID, permission);
369                }
370            } else {
371                result = result + annotationDao.addAnnotationPrincipalPermission(annotationID, userID, permission);
372            }
373        }
374
375        return result;
376    }
377
378    // TODO: optimize (not chnaged targets should not be deleted)
379    // TODO: unit test
380    @Override
381    public int updateUsersAnnotation(Number userID, Annotation annotation) {
382        int updatedAnnotations = annotationDao.updateAnnotation(annotation, userID);
383        Number annotationID = annotationDao.getInternalIDFromURI(annotation.getURI());
384        int deletedTargets = annotationDao.deleteAllAnnotationTarget(annotationID);
385        int deletedPrinsipalsPermissions = annotationDao.deleteAnnotationPrincipalPermissions(annotationID);
386        int addedTargets = addTargets(annotation, annotationID);
387        int addedPrincipalsPermissions = addPrincipalsPermissions(annotation, annotationID);
388        return updatedAnnotations;
389    }
390   
391     
392    // TODO: unit test
393    @Override
394    public int updateAnnotationBody(Number internalID, AnnotationBody annotationBody) {
395        return annotationDao.updateAnnotationBody(internalID, annotationBody);
396    }
397   
398   
399   
400    /////////////// ADDERS  /////////////////////////////////
401    @Override
402    public Number[] addCachedForTarget(Number targetID, String fragmentDescriptor, CachedRepresentationInfo cachedInfo, InputStream cachedBlob) {
403        Number[] result = new Number[2];
404        result[1] = cachedRepresentationDao.getInternalIDFromURI(cachedInfo.getURI());
405        if (result[1] == null) {
406            result[1] = cachedRepresentationDao.addCachedRepresentation(cachedInfo, cachedBlob);
407        }
408        result[0] = targetDao.addTargetCachedRepresentation(targetID, result[1], fragmentDescriptor);
409        return result;
410
411    }
412
413    // TODo: mapping uri to external ID
414    @Override
415    public Map<String, String> addTargetsForAnnotation(Number annotationID, List<TargetInfo> targets) {
416        Map<String, String> result = new HashMap<String, String>();
417        Number targetIDRunner;
418        for (TargetInfo targetInfo : targets) {
419            targetIDRunner = targetDao.getInternalIDFromURI(targetInfo.getRef());
420            if (targetIDRunner != null) {
421                int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetIDRunner);
422            } else {
423                Target newTarget = createFreshTarget(targetInfo);
424                Number targetID = targetDao.addTarget(newTarget);
425                String targetTemporaryID = targetDao.stringURItoExternalID(targetInfo.getRef());
426                result.put(targetTemporaryID, targetDao.getExternalID(targetID).toString());
427                int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetID);
428            }
429        }
430        return result;
431    }
432
433    @Override
434    public Number addUsersAnnotation(Number userID, Annotation annotation) {
435        Number annotationID = annotationDao.addAnnotation(annotation, userID);
436        int affectedAnnotRows = addTargets(annotation, annotationID);
437        int affectedPermissions = annotationDao.addAnnotationPrincipalPermission(annotationID, userID, Permission.OWNER);
438        return annotationID;
439    }
440
441    @Override
442    public Number updateUser(User user) {
443        return userDao.updateUser(user);
444    }
445
446    @Override
447    public Number addUser(User user, String remoteID) {
448        if (userDao.userExists(user)) {
449            return null;
450        } else {
451            return userDao.addUser(user, remoteID);
452        }
453    }
454
455    @Override
456    public int addAnnotationPrincipalPermission(Number annotationID, Number userID, Permission permission) {
457        return annotationDao.addAnnotationPrincipalPermission(annotationID, userID, permission);
458    }
459
460    ////////////// DELETERS //////////////////
461    @Override
462    public int deleteUser(Number userID) {
463        return userDao.deleteUser(userID);
464    }
465
466    @Override
467    public int[] deleteCachedRepresentationOfTarget(Number versionID, Number cachedID) {
468        int[] result = new int[2];
469        result[0] = targetDao.deleteTargetCachedRepresentation(versionID, cachedID);
470        if (result[0] > 0) {
471            result[1] = cachedRepresentationDao.deleteCachedRepresentation(cachedID);
472        } else {
473            result[1] = 0;
474
475        }
476        return result;
477    }
478
479    @Override
480    public int[] deleteAllCachedRepresentationsOfTarget(Number TargetID) {
481        int[] result = new int[2];
482        result[0] = 0;
483        result[1] = 0;
484        List<Number> cachedIDs = targetDao.getCachedRepresentations(TargetID);
485        for (Number cachedID : cachedIDs) {
486            int[] currentResult = deleteCachedRepresentationOfTarget(TargetID, cachedID);
487            result[0] = result[0] + currentResult[0];
488            result[1] = result[1] + currentResult[1];
489        }
490        return result;
491    }
492
493    @Override
494    public int[] deleteAnnotation(Number annotationID) {
495        int[] result = new int[4];
496        result[1] = annotationDao.deleteAnnotationPrincipalPermissions(annotationID);
497        List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
498        result[2] = annotationDao.deleteAllAnnotationTarget(annotationID);
499        result[0] = annotationDao.deleteAnnotation(annotationID);
500        result[3] = 0;
501        if (targetIDs != null) {
502            for (Number targetID : targetIDs) {
503                deleteAllCachedRepresentationsOfTarget(targetID);
504                result[3] = result[3] + targetDao.deleteTarget(targetID);
505
506            }
507        }
508        return result;
509    }
510
511    ////////////// HELPERS ////////////////////
512    private Target createFreshTarget(TargetInfo targetInfo) {
513        Target target = new Target();
514        target.setLink(targetInfo.getLink());
515        target.setVersion(targetInfo.getVersion());
516        return target;
517    }
518
519    private int addTargets(Annotation annotation, Number annotationID) {
520        List<TargetInfo> targets = annotation.getTargets().getTargetInfo();
521        Map<String, String> targetIdPairs = addTargetsForAnnotation(annotationID, targets);
522        AnnotationBody annotationBody = annotation.getBody();
523        String bodyText;
524        String newBodyText;
525        if (annotationBody.getXmlBody() != null) {
526            bodyText = Helpers.elementToString(annotation.getBody().getXmlBody().getAny());
527        } else {
528            if (annotation.getBody().getTextBody() != null) {
529                bodyText = annotation.getBody().getTextBody().getValue();
530
531            } else {
532                logger.error("The client has sent ill-formed annotation body.");
533                return -1;
534            }
535        }
536        newBodyText = Helpers.replace(bodyText, targetIdPairs);
537        return annotationDao.updateAnnotationBodyText(annotationID, newBodyText);
538    }
539
540    private int addPrincipalsPermissions(Annotation annotation, Number annotationID) {
541        List<UserWithPermission> permissions = annotation.getPermissions().getUserWithPermission();
542        int addedPermissions = 0;
543        for (UserWithPermission permission : permissions) {
544            addedPermissions = addedPermissions + annotationDao.addAnnotationPrincipalPermission(annotationID, userDao.getInternalIDFromURI(permission.getRef()), permission.getPermission());
545
546        }
547        return addedPermissions;
548    }
549}
Note: See TracBrowser for help on using the repository browser.