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

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

Adjusting logging. Server responds are redirected to a separate server logging files as well.

File size: 23.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.IOException;
43import java.io.InputStream;
44import java.lang.Number;
45import java.sql.SQLException;
46import java.sql.Timestamp;
47import java.util.ArrayList;
48import java.util.HashMap;
49import java.util.List;
50import java.util.Map;
51import java.util.UUID;
52import javax.xml.parsers.ParserConfigurationException;
53import org.springframework.beans.factory.annotation.Autowired;
54import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56import org.springframework.transaction.annotation.Transactional;
57
58/**
59 *
60 * @author olhsha
61 */
62public class DBIntegrityServiceImlp implements DBIntegrityService {
63
64    @Autowired
65    UserDao userDao;
66    @Autowired
67    CachedRepresentationDao cachedRepresentationDao;
68    @Autowired
69    TargetDao targetDao;
70    @Autowired
71    AnnotationDao annotationDao;
72    private static final Logger logger = LoggerFactory.getLogger(AnnotationResource.class);
73    //////////////////////////////////
74
75    @Override
76    public void setServiceURI(String serviceURI) {
77        userDao.setServiceURI(serviceURI + "users/");
78        cachedRepresentationDao.setServiceURI(serviceURI + "cached/");
79        targetDao.setServiceURI(serviceURI + "targets/");
80        annotationDao.setServiceURI(serviceURI + "annotations/");
81        //notebookDao.setServiceURI(serviceURI+"notebooks/");
82    }
83
84    ///////////// GETTERS //////////////////////////
85    @Override
86    public Number getAnnotationInternalIdentifier(UUID externalID) {
87        return annotationDao.getInternalID(externalID);
88    }
89
90    @Override
91    public Number getAnnotationInternalIdentifierFromURI(String uri) {
92        return annotationDao.getInternalIDFromURI(uri);
93    }
94
95    @Override
96    public UUID getAnnotationExternalIdentifier(Number annotationID) {
97        return annotationDao.getExternalID(annotationID);
98    }
99
100    @Override
101    public Number getTargetInternalIdentifier(UUID externalID) {
102        return targetDao.getInternalID(externalID);
103    }
104
105    @Override
106    public UUID getTargetExternalIdentifier(Number targetID) {
107        return targetDao.getExternalID(targetID);
108    }
109
110    @Override
111    public String getTargetURI(Number targetID) {
112        return targetDao.getURIFromInternalID(targetID);
113    }
114
115    @Override
116    public String getUserURI(Number userID) {
117        return userDao.getURIFromInternalID(userID);
118    }
119
120    @Override
121    public Number getUserInternalIdentifier(UUID externalID) {
122        return userDao.getInternalID(externalID);
123    }
124
125    @Override
126    public UUID getUserExternalIdentifier(Number userID) {
127        return userDao.getExternalID(userID);
128    }
129
130    @Override
131    public Number getCachedRepresentationInternalIdentifier(UUID externalID) {
132        return cachedRepresentationDao.getInternalID(externalID);
133    }
134
135    @Override
136    public UUID getCachedRepresentationExternalIdentifier(Number cachedID) {
137        return cachedRepresentationDao.getExternalID(cachedID);
138    }
139
140    @Override
141    public Annotation getAnnotation(Number annotationID) {
142        if (annotationID != null) {
143            Annotation result = annotationDao.getAnnotationWithoutTargetsAndPermissions(annotationID);
144            result.setOwnerRef(userDao.getURIFromInternalID(annotationDao.getOwner(annotationID)));
145            List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
146            TargetInfoList sis = new TargetInfoList();
147            for (Number targetID : targetIDs) {
148                TargetInfo targetInfo = getTargetInfoFromTarget(targetDao.getTarget(targetID));
149                sis.getTargetInfo().add(targetInfo);
150            }
151            result.setTargets(sis);
152
153            result.setPermissions(getPermissionsForAnnotation(annotationID));
154            return result;
155        } else {
156            return null;
157        }
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(String link, String text, Number inloggedUserID, String[] accessModes, String namespace, Timestamp after, Timestamp 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        return annotationDao.getFilteredAnnotationIDs(annotationIDs, text, namespace, after, before);
203    }
204
205    @Override
206    public ReferenceList getAnnotationTargets(Number annotationID) {
207        ReferenceList result = new ReferenceList();
208        List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
209        for (Number targetID : targetIDs) {
210            result.getRef().add(targetDao.getURIFromInternalID(targetID));
211        }
212        return result;
213    }
214
215    @Override
216    public List<String> getTargetsWithNoCachedRepresentation(Number annotationID) {
217        if (annotationID == null) {
218            return null;
219        }
220        List<String> result = new ArrayList<String>();
221        List<Number> targetIDs = annotationDao.retrieveTargetIDs(annotationID);
222        for (Number targetID : targetIDs) {
223            List<Number> versions = targetDao.getCachedRepresentations(targetID);
224            if (versions == null) {
225                result.add(targetDao.getURIFromInternalID(targetID));
226            } else {
227                if (versions.isEmpty()) {
228                    result.add(targetDao.getURIFromInternalID(targetID));
229                }
230
231            }
232        }
233        return result;
234    }
235
236    @Override
237    public List<String> getUsersWithNoInfo(Number annotationID) {
238        if (annotationID == null) {
239            return null;
240        }
241        List<String> result = new ArrayList<String>();
242        List<Map<Number, String>> usersWithPermissions = annotationDao.getPermissions(annotationID);
243        for (Map<Number, String> userWithPermission : usersWithPermissions) {
244            Number[] userID = new Number[1];
245            userWithPermission.keySet().toArray(userID);
246            User user = userDao.getUser(userID[0]);
247
248            if (user.getDisplayName() == null || user.getDisplayName().trim().isEmpty() || user.getEMail() == null || user.getEMail().trim().isEmpty()) {
249                result.add(userDao.getURIFromInternalID(userID[0]));
250
251            }
252        }
253        return result;
254    }
255
256    @Override
257    public AnnotationInfoList getFilteredAnnotationInfos(String word, String text, Number inloggedUserID, String[] accessModes, String namespace, UUID owner, Timestamp after, Timestamp before) {
258        List<Number> annotationIDs = getFilteredAnnotationIDs(word, text, inloggedUserID, accessModes, namespace, after, before);
259        Number givenOwnerID = userDao.getInternalID(owner);
260        if (annotationIDs != null) {
261            AnnotationInfoList result = new AnnotationInfoList();
262            for (Number annotationID : annotationIDs) {
263                Number ownerID = annotationDao.getOwner(annotationID);
264                if ((owner == null) || ownerID.equals(givenOwnerID)) {
265                    ReferenceList targets = getAnnotationTargets(annotationID);
266                    AnnotationInfo annotationInfo = annotationDao.getAnnotationInfoWithoutTargets(annotationID);
267                    annotationInfo.setTargets(targets);
268                    annotationInfo.setOwnerRef(userDao.getURIFromInternalID(ownerID));
269                    result.getAnnotationInfo().add(annotationInfo);
270                }
271            }
272
273            return result;
274        } else {
275            return null;
276        }
277    }
278
279    @Override
280    public AnnotationInfoList getAllAnnotationInfos() {
281        List<Number> annotationIDs = annotationDao.getAllAnnotationIDs();
282        if (annotationIDs != null) {
283            AnnotationInfoList result = new AnnotationInfoList();
284            for (Number annotationID : annotationIDs) {
285                Number ownerID = annotationDao.getOwner(annotationID);
286                ReferenceList targets = getAnnotationTargets(annotationID);
287                AnnotationInfo annotationInfo = annotationDao.getAnnotationInfoWithoutTargets(annotationID);
288                annotationInfo.setTargets(targets);
289                if (ownerID != null) {
290                    annotationInfo.setOwnerRef(userDao.getURIFromInternalID(ownerID));
291                } else {
292                    annotationInfo.setOwnerRef("ACHTUNG: This annotation does not have an owner in the DB!!!!");
293                }
294                result.getAnnotationInfo().add(annotationInfo);
295            }
296
297            return result;
298        } else {
299            return null;
300        }
301    }
302
303    // TODO unit test
304    @Override
305    public Target getTarget(Number internalID) {
306        Target result = targetDao.getTarget(internalID);
307        result.setSiblingTargets(getTargetsForTheSameLinkAs(internalID));
308        Map<Number, String> cachedIDsFragments = targetDao.getCachedRepresentationFragmentPairs(internalID);
309        CachedRepresentationFragmentList cachedRepresentationFragmentList = new CachedRepresentationFragmentList();
310        for (Number key : cachedIDsFragments.keySet()) {
311            CachedRepresentationFragment cachedRepresentationFragment = new CachedRepresentationFragment();
312            cachedRepresentationFragment.setRef(cachedRepresentationDao.getURIFromInternalID(key));
313            cachedRepresentationFragment.setFragmentString(cachedIDsFragments.get(key));
314            cachedRepresentationFragmentList.getCached().add(cachedRepresentationFragment);
315        }
316        result.setCachedRepresentatinons(cachedRepresentationFragmentList);
317        return result;
318    }
319
320    // TODO unit test
321    @Override
322    public CachedRepresentationInfo getCachedRepresentationInfo(Number internalID) {
323        return cachedRepresentationDao.getCachedRepresentationInfo(internalID);
324    }
325
326    //TODO unit test
327    @Override
328    public InputStream getCachedRepresentationBlob(Number cachedID) {
329        return cachedRepresentationDao.getCachedRepresentationBlob(cachedID);
330    }
331
332    @Override
333    public ReferenceList getTargetsForTheSameLinkAs(Number targetID) {
334        List<Number> targetIDs = targetDao.getTargetsForLink(targetDao.getLink(targetID));
335        ReferenceList referenceList = new ReferenceList();
336        for (Number siblingID : targetIDs) {
337            referenceList.getRef().add(targetDao.externalIDtoURI(targetDao.getExternalID(siblingID).toString()));
338        }
339        return referenceList;
340    }
341
342    @Override
343    public User getUser(Number userID) {
344        return userDao.getUser(userID);
345    }
346
347    @Override
348    public User getUserByInfo(String eMail) {
349        return userDao.getUserByInfo(eMail);
350    }
351
352    @Override
353    public String getUserRemoteID(Number internalID) {
354        return userDao.getRemoteID(internalID);
355    }
356
357    @Override
358    public Permission getPermission(Number annotationID, Number userID) {
359        return annotationDao.getPermission(annotationID, userID);
360    }
361
362    @Override
363    public Number getUserInternalIDFromRemoteID(String remoteID) {
364        return userDao.getUserInternalIDFromRemoteID(remoteID);
365    }
366
367    @Override
368    public String getTypeOfUserAccount(Number userID) {
369        return userDao.getTypeOfUserAccount(userID);
370    }
371
372    ///// UPDATERS /////////////////
373    @Override
374    public boolean updateAccount(UUID userExternalID, String account) {
375        return userDao.updateAccount(userExternalID, account);
376    }
377
378    @Override
379    public int updateAnnotationPrincipalPermission(Number annotationID, Number userID, Permission permission) {
380        return annotationDao.updateAnnotationPrincipalPermission(annotationID, userID, permission);
381    }
382
383    @Override
384    public int updatePermissions(Number annotationID, UserWithPermissionList permissionList) {
385
386        List<UserWithPermission> usersWithPermissions = permissionList.getUserWithPermission();
387        int result = 0;
388        for (UserWithPermission userWithPermission : usersWithPermissions) {
389            Number userID = userDao.getInternalID(UUID.fromString(userDao.stringURItoExternalID(userWithPermission.getRef())));
390            if (userID != null) {
391                Permission permission = userWithPermission.getPermission();
392                Permission currentPermission = annotationDao.getPermission(annotationID, userID);
393                if (currentPermission != null) {
394                    if (!permission.value().equals(currentPermission.value())) {
395                        result = result + annotationDao.updateAnnotationPrincipalPermission(annotationID, userID, permission);
396                    }
397                } else {
398                    result = result + annotationDao.addAnnotationPrincipalPermission(annotationID, userID, permission);
399                }
400            }
401        }
402
403        return result;
404    }
405
406    // TODO: optimize (not chnaged targets should not be deleted)
407    // TODO: unit test
408    @Override
409    public int updateUsersAnnotation(Number userID, Annotation annotation) {
410        int updatedAnnotations = annotationDao.updateAnnotation(annotation);
411        Number annotationID = annotationDao.getInternalIDFromURI(annotation.getURI());
412        int deletedTargets = annotationDao.deleteAllAnnotationTarget(annotationID);
413        int deletedPrinsipalsPermissions = annotationDao.deleteAnnotationPrincipalPermissions(annotationID);
414        int addedTargets = addTargets(annotation, annotationID);
415        int addedPrincipalsPermissions = addPrincipalsPermissions(annotation.getPermissions().getUserWithPermission(), annotationID);
416        return updatedAnnotations;
417    }
418
419    // TODO: unit test
420    @Override
421    public int updateAnnotationBody(Number internalID, AnnotationBody annotationBody) {
422        String[] body = annotationDao.retrieveBodyComponents(annotationBody);
423        return annotationDao.updateAnnotationBody(internalID, body[0], body[1], annotationBody.getXmlBody() != null);
424    }
425
426    /////////////// ADDERS  /////////////////////////////////
427    @Override
428    public Number[] addCachedForTarget(Number targetID, String fragmentDescriptor, CachedRepresentationInfo cachedInfo, InputStream cachedBlob) {
429        Number[] result = new Number[2];
430        result[1] = cachedRepresentationDao.getInternalIDFromURI(cachedInfo.getURI());
431        if (result[1] == null) {
432            result[1] = cachedRepresentationDao.addCachedRepresentation(cachedInfo, cachedBlob);
433        }
434        result[0] = targetDao.addTargetCachedRepresentation(targetID, result[1], fragmentDescriptor);
435        return result;
436
437    }
438
439    // TODo: mapping uri to external ID
440    @Override
441    public Map<String, String> addTargetsForAnnotation(Number annotationID, List<TargetInfo> targets) {
442        Map<String, String> result = new HashMap<String, String>();
443        Number targetIDRunner;
444        for (TargetInfo targetInfo : targets) {
445            targetIDRunner = targetDao.getInternalIDFromURI(targetInfo.getRef());
446            if (targetIDRunner != null) {
447                int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetIDRunner);
448            } else {
449                Target newTarget = createFreshTarget(targetInfo);
450                Number targetID = targetDao.addTarget(newTarget);
451                String targetTemporaryID = targetDao.stringURItoExternalID(targetInfo.getRef());
452                result.put(targetTemporaryID, targetDao.getExternalID(targetID).toString());
453                int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetID);
454            }
455        }
456        return result;
457    }
458
459    @Override
460    public Number addUsersAnnotation(Number userID, Annotation annotation) {
461        Number annotationID = annotationDao.addAnnotation(annotation);
462        int affectedAnnotRows = addTargets(annotation, annotationID);
463        if (annotation.getPermissions() != null) {
464            if (annotation.getPermissions().getUserWithPermission() != null) {
465                int addedPrincipalsPermissions = addPrincipalsPermissions(annotation.getPermissions().getUserWithPermission(), annotationID);
466            }
467        }
468        int affectedPermissions = annotationDao.addAnnotationPrincipalPermission(annotationID, userID, Permission.OWNER);
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 = 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                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.