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

Last change on this file since 4429 was 4429, checked in by olhsha, 10 years ago

ownership is unique for the annotations and notebooks. Owner is removed from the permissions tables and added to the annotations and notebooks tables. The code is modified and unit-tested.

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