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

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

a fragments (of a target) is now a part of the attribute link. The Dao's and unit tests are updated and work. Fixed corrupted pom (there was ill glass-fish dependency that spoiled authentication)

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    @Override
139    public Annotation getAnnotation(Number annotationID) {
140        if (annotationID != null) {
141            Map<Annotation, Number> annotationOwner = annotationDao.getAnnotationWithoutTargetsAndPermissions(annotationID);
142
143            Annotation[] annotations = new Annotation[1];
144            annotationOwner.keySet().toArray(annotations);
145            Annotation result = annotations[0];
146            result.setOwnerRef(userDao.getURIFromInternalID(annotationOwner.get(result)));
147
148            List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
149            TargetInfoList sis = new TargetInfoList();
150            for (Number targetID : targetIDs) {
151                TargetInfo targetInfo = getTargetInfoFromTarget(targetDao.getTarget(targetID));
152                sis.getTargetInfo().add(targetInfo);
153            }
154            result.setTargets(sis);
155
156            result.setPermissions(getPermissionsForAnnotation(annotationID));
157            return result;
158        } else {
159            return null;
160        }
161    }
162
163    ///////////////////////////////////////////////////
164    // TODO UNIT tests
165    @Override
166    public UserWithPermissionList getPermissionsForAnnotation(Number annotationID) {
167        if (annotationID != null) {
168            List<Map<Number, String>> principalsPermissions = annotationDao.getPermissions(annotationID);
169            UserWithPermissionList result = new UserWithPermissionList();
170            List<UserWithPermission> list = result.getUserWithPermission();
171            for (Map<Number, String> principalPermission : principalsPermissions) {
172
173                Number[] principal = new Number[1];
174                principalPermission.keySet().toArray(principal);
175
176                UserWithPermission userWithPermission = new UserWithPermission();
177                userWithPermission.setRef(userDao.getURIFromInternalID(principal[0]));
178                userWithPermission.setPermission(Permission.fromValue(principalPermission.get(principal[0])));
179
180                list.add(userWithPermission);
181            }
182            return result;
183        } else {
184            return null;
185        }
186
187    }
188
189    ////////////////////////////////////////////////////////////////////////
190    @Override
191    public List<Number> getFilteredAnnotationIDs(String link, String text, Number inloggedUserID, String access, String namespace, UUID owner, Timestamp after, Timestamp before) {
192
193        if (access == null) {
194            return null;
195        }
196
197        List<Number> annotationIDs = annotationDao.getAnnotationIDsForUserWithPermission(inloggedUserID, access);
198
199        if (link != null) {
200            List<Number> targetIDs = targetDao.getTargetsReferringTo(link);
201            List<Number> annotationIDsForTargets = annotationDao.retrieveAnnotationList(targetIDs);
202            annotationIDs.retainAll(annotationIDsForTargets);
203        }
204
205        return annotationDao.getFilteredAnnotationIDs(annotationIDs, text, namespace, userDao.getInternalID(owner), after, before);
206    }
207
208    @Override
209    public ReferenceList getAnnotationTargets(Number annotationID) {
210        ReferenceList result = new ReferenceList();
211        List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
212        for (Number targetID : targetIDs) {
213            result.getRef().add(targetDao.getURIFromInternalID(targetID));
214        }
215        return result;
216    }
217
218    @Override
219    public List<String> getTargetsWithNoCachedRepresentation(Number annotationID) {
220        if (annotationID == null) {
221            return null;
222        }
223        List<String> result = new ArrayList<String>();
224        List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
225        for (Number targetID : targetIDs) {
226            List<Number> versions = targetDao.getCachedRepresentations(targetID);
227            if (versions == null) {
228                result.add(targetDao.getURIFromInternalID(targetID));
229            } else {
230                if (versions.isEmpty()) {
231                    result.add(targetDao.getURIFromInternalID(targetID));
232                }
233
234            }
235        }
236        return result;
237    }
238
239    @Override
240    public List<String> getUsersWithNoInfo(Number annotationID) {
241        if (annotationID == null) {
242            return null;
243        }
244        List<String> result = new ArrayList<String>();
245        List<Map<Number, String>> usersWithPermissions = annotationDao.getPermissions(annotationID);
246        for (Map<Number, String> userWithPermission : usersWithPermissions) {
247            Number[] userID = new Number[1];
248            userWithPermission.keySet().toArray(userID);
249            User user = userDao.getUser(userID[0]);
250
251            if (user.getDisplayName() == null || user.getDisplayName().trim().isEmpty() || user.getEMail() == null || user.getEMail().trim().isEmpty()) {
252                result.add(userDao.getURIFromInternalID(userID[0]));
253
254            }
255        }
256        return result;
257    }
258
259    @Override
260    public AnnotationInfoList getFilteredAnnotationInfos(String word, String text, Number inloggedUserID, String access, String namespace, UUID owner, Timestamp after, Timestamp before) {
261        List<Number> annotationIDs = getFilteredAnnotationIDs(word, text, inloggedUserID, access, namespace, owner, after, before);
262        if (annotationIDs != null) {
263            AnnotationInfoList result = new AnnotationInfoList();
264            for (Number annotationID : annotationIDs) {
265                Map<AnnotationInfo, Number> annotationInfoOwnerId = annotationDao.getAnnotationInfoWithoutTargets(annotationID);
266                ReferenceList targets = getAnnotationTargets(annotationID);
267                AnnotationInfo[] annotationInfos = new AnnotationInfo[1];
268                annotationInfoOwnerId.keySet().toArray(annotationInfos);
269                AnnotationInfo annotationInfo = annotationInfos[0];
270                annotationInfo.setTargets(targets);
271                annotationInfo.setOwnerRef(userDao.getURIFromInternalID(annotationInfoOwnerId.get(annotationInfo)));
272                result.getAnnotationInfo().add(annotationInfo);
273            }
274
275            return result;
276        } else {
277            return null;
278        }
279
280    }
281
282    // TODO unit test
283    @Override
284    public Target getTarget(Number internalID) {
285        Target result = targetDao.getTarget(internalID);
286        result.setSiblingTargets(getTargetsForTheSameLinkAs(internalID));
287        Map<Number, String> cachedIDsFragments = targetDao.getCachedRepresentationFragmentPairs(internalID);
288        CachedRepresentationFragmentList cachedRepresentationFragmentList = new CachedRepresentationFragmentList();
289        for (Number key : cachedIDsFragments.keySet()) {
290            CachedRepresentationFragment cachedRepresentationFragment = new CachedRepresentationFragment();
291            cachedRepresentationFragment.setRef(cachedRepresentationDao.getURIFromInternalID(key));
292            cachedRepresentationFragment.setFragmentString(cachedIDsFragments.get(key));
293            cachedRepresentationFragmentList.getCached().add(cachedRepresentationFragment);
294        }
295        result.setCachedRepresentatinons(cachedRepresentationFragmentList);
296        return result;
297    }
298
299    // TODO unit test
300    @Override
301    public CachedRepresentationInfo getCachedRepresentationInfo(Number internalID) {
302        return cachedRepresentationDao.getCachedRepresentationInfo(internalID);
303    }
304
305    //TODO unit test
306    @Override
307    public InputStream getCachedRepresentationBlob(Number cachedID) {
308        return cachedRepresentationDao.getCachedRepresentationBlob(cachedID);
309    }
310
311    @Override
312    public ReferenceList getTargetsForTheSameLinkAs(Number targetID) {
313        List<Number> targetIDs = targetDao.getTargetsForLink(targetDao.getLink(targetID));
314        ReferenceList referenceList = new ReferenceList();
315        for (Number siblingID : targetIDs) {
316            referenceList.getRef().add(targetDao.externalIDtoURI(targetDao.getExternalID(siblingID).toString()));
317        }
318        return referenceList;
319    }
320
321    @Override
322    public User getUser(Number userID) {
323        return userDao.getUser(userID);
324    }
325
326    @Override
327    public User getUserByInfo(String eMail) {
328        return userDao.getUserByInfo(eMail);
329    }
330
331    @Override
332    public String getUserRemoteID(Number internalID) {
333        return userDao.getRemoteID(internalID);
334    }
335
336    @Override
337    public Permission getPermission(Number annotationID, Number userID) {
338        return annotationDao.getPermission(annotationID, userID);
339    }
340
341    @Override
342    public Number getUserInternalIDFromRemoteID(String remoteID) {
343        return userDao.getUserInternalIDFromRemoteID(remoteID);
344    }
345
346    ///// UPDATERS /////////////////
347    @Override
348    public int updateAnnotationPrincipalPermission(Number annotationID, Number userID, Permission permission) {
349        return annotationDao.updateAnnotationPrincipalPermission(annotationID, userID, permission);
350    }
351
352    @Override
353    public int updatePermissions(Number annotationID, UserWithPermissionList permissionList) {
354
355        List<UserWithPermission> usersWithPermissions = permissionList.getUserWithPermission();
356        int result = 0;
357        for (UserWithPermission userWithPermission : usersWithPermissions) {
358            Number userID = userDao.getInternalID(UUID.fromString(userDao.stringURItoExternalID(userWithPermission.getRef())));
359            Permission permission = userWithPermission.getPermission();
360            Permission currentPermission = annotationDao.getPermission(annotationID, userID);
361            if (currentPermission != null) {
362                if (!permission.value().equals(currentPermission.value())) {
363                    result = result + annotationDao.updateAnnotationPrincipalPermission(annotationID, userID, permission);
364                }
365            } else {
366                result = result + annotationDao.addAnnotationPrincipalPermission(annotationID, userID, permission);
367            }
368        }
369
370        return result;
371    }
372
373    // TODO: optimize (not chnaged targets should not be deleted)
374    // TODO: unit test
375    @Override
376    public int updateUsersAnnotation(Number userID, Annotation annotation) {
377        int updatedAnnotations = annotationDao.updateAnnotation(annotation, userID);
378        Number annotationID = annotationDao.getInternalIDFromURI(annotation.getURI());
379        int deletedTargets = annotationDao.deleteAllAnnotationTarget(annotationID);
380        int deletedPrinsipalsPermissions = annotationDao.deleteAnnotationPrincipalPermissions(annotationID);
381        int addedTargets = addTargets(annotation, annotationID);
382        int addedPrincipalsPermissions = addPrincipalsPermissions(annotation, annotationID);
383        return updatedAnnotations;
384    }
385   
386     
387    // TODO: unit test
388    @Override
389    public int updateAnnotationBody(Number internalID, AnnotationBody annotationBody) {
390        return annotationDao.updateAnnotationBody(internalID, annotationBody);
391    }
392   
393   
394   
395    /////////////// ADDERS  /////////////////////////////////
396    @Override
397    public Number[] addCachedForTarget(Number targetID, String fragmentDescriptor, CachedRepresentationInfo cachedInfo, InputStream cachedBlob) {
398        Number[] result = new Number[2];
399        result[1] = cachedRepresentationDao.getInternalIDFromURI(cachedInfo.getURI());
400        if (result[1] == null) {
401            result[1] = cachedRepresentationDao.addCachedRepresentation(cachedInfo, cachedBlob);
402        }
403        result[0] = targetDao.addTargetCachedRepresentation(targetID, result[1], fragmentDescriptor);
404        return result;
405
406    }
407
408    // TODo: mapping uri to external ID
409    @Override
410    public Map<String, String> addTargetsForAnnotation(Number annotationID, List<TargetInfo> targets) {
411        Map<String, String> result = new HashMap<String, String>();
412        Number targetIDRunner;
413        for (TargetInfo targetInfo : targets) {
414            targetIDRunner = targetDao.getInternalIDFromURI(targetInfo.getRef());
415            if (targetIDRunner != null) {
416                int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetIDRunner);
417            } else {
418                Target newTarget = createFreshTarget(targetInfo);
419                Number targetID = targetDao.addTarget(newTarget);
420                String targetTemporaryID = targetDao.stringURItoExternalID(targetInfo.getRef());
421                result.put(targetTemporaryID, targetDao.getExternalID(targetID).toString());
422                int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetID);
423            }
424        }
425        return result;
426    }
427
428    @Override
429    public Number addUsersAnnotation(Number userID, Annotation annotation) {
430        Number annotationID = annotationDao.addAnnotation(annotation, userID);
431        int affectedAnnotRows = addTargets(annotation, annotationID);
432        int affectedPermissions = annotationDao.addAnnotationPrincipalPermission(annotationID, userID, Permission.OWNER);
433        return annotationID;
434    }
435
436    @Override
437    public Number updateUser(User user) {
438        return userDao.updateUser(user);
439    }
440
441    @Override
442    public Number addUser(User user, String remoteID) {
443        if (userDao.userExists(user)) {
444            return null;
445        } else {
446            return userDao.addUser(user, remoteID);
447        }
448    }
449
450    @Override
451    public int addAnnotationPrincipalPermission(Number annotationID, Number userID, Permission permission) {
452        return annotationDao.addAnnotationPrincipalPermission(annotationID, userID, permission);
453    }
454
455    ////////////// DELETERS //////////////////
456    @Override
457    public int deleteUser(Number userID) {
458        return userDao.deleteUser(userID);
459    }
460
461    @Override
462    public int[] deleteCachedRepresentationOfTarget(Number versionID, Number cachedID) {
463        int[] result = new int[2];
464        result[0] = targetDao.deleteTargetCachedRepresentation(versionID, cachedID);
465        if (result[0] > 0) {
466            result[1] = cachedRepresentationDao.deleteCachedRepresentation(cachedID);
467        } else {
468            result[1] = 0;
469
470        }
471        return result;
472    }
473
474    @Override
475    public int[] deleteAllCachedRepresentationsOfTarget(Number TargetID) {
476        int[] result = new int[2];
477        result[0] = 0;
478        result[1] = 0;
479        List<Number> cachedIDs = targetDao.getCachedRepresentations(TargetID);
480        for (Number cachedID : cachedIDs) {
481            int[] currentResult = deleteCachedRepresentationOfTarget(TargetID, cachedID);
482            result[0] = result[0] + currentResult[0];
483            result[1] = result[1] + currentResult[1];
484        }
485        return result;
486    }
487
488    @Override
489    public int[] deleteAnnotation(Number annotationID) {
490        int[] result = new int[4];
491        result[1] = annotationDao.deleteAnnotationPrincipalPermissions(annotationID);
492        List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
493        result[2] = annotationDao.deleteAllAnnotationTarget(annotationID);
494        result[0] = annotationDao.deleteAnnotation(annotationID);
495        result[3] = 0;
496        if (targetIDs != null) {
497            for (Number targetID : targetIDs) {
498                deleteAllCachedRepresentationsOfTarget(targetID);
499                result[3] = result[3] + targetDao.deleteTarget(targetID);
500
501            }
502        }
503        return result;
504    }
505
506    ////////////// HELPERS ////////////////////
507    private Target createFreshTarget(TargetInfo targetInfo) {
508        Target target = new Target();
509        target.setLink(targetInfo.getLink());
510        target.setVersion(targetInfo.getVersion());
511        return target;
512    }
513
514    private int addTargets(Annotation annotation, Number annotationID) {
515        List<TargetInfo> targets = annotation.getTargets().getTargetInfo();
516        Map<String, String> targetIdPairs = addTargetsForAnnotation(annotationID, targets);
517        AnnotationBody annotationBody = annotation.getBody();
518        String bodyText;
519        String newBodyText;
520        if (annotationBody.getXmlBody() != null) {
521            bodyText = Helpers.elementToString(annotation.getBody().getXmlBody().getAny());
522        } else {
523            if (annotation.getBody().getTextBody() != null) {
524                bodyText = annotation.getBody().getTextBody().getValue();
525
526            } else {
527                logger.error("The client has sent ill-formed annotation body.");
528                return -1;
529            }
530        }
531        newBodyText = Helpers.replace(bodyText, targetIdPairs);
532        return annotationDao.updateAnnotationBodyText(annotationID, newBodyText);
533    }
534
535    private int addPrincipalsPermissions(Annotation annotation, Number annotationID) {
536        List<UserWithPermission> permissions = annotation.getPermissions().getUserWithPermission();
537        int addedPermissions = 0;
538        for (UserWithPermission permission : permissions) {
539            addedPermissions = addedPermissions + annotationDao.addAnnotationPrincipalPermission(annotationID, userDao.getInternalIDFromURI(permission.getRef()), permission.getPermission());
540
541        }
542        return addedPermissions;
543    }
544   
545     private TargetInfo getTargetInfoFromTarget(Target target) {
546        TargetInfo targetInfo = new TargetInfo();
547        targetInfo.setRef(target.getURI());
548        targetInfo.setLink(target.getLink());
549        targetInfo.setVersion(target.getVersion());
550        return targetInfo;
551    }
552}
Note: See TracBrowser for help on using the repository browser.