source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/integration/DaoRequestor.java @ 3407

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

refactored DAO-s: resource dao do not call each-other methods any more. Only the new Dispatcher method can do it. Not tested.

File size: 12.3 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.integration;
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.NotebookDao;
24import eu.dasish.annotation.backend.dao.PermissionsDao;
25import eu.dasish.annotation.backend.dao.SourceDao;
26import eu.dasish.annotation.backend.dao.UserDao;
27import eu.dasish.annotation.backend.dao.VersionDao;
28import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
29import eu.dasish.annotation.backend.identifiers.CachedRepresentationIdentifier;
30import eu.dasish.annotation.backend.identifiers.SourceIdentifier;
31import eu.dasish.annotation.backend.identifiers.UserIdentifier;
32import eu.dasish.annotation.backend.identifiers.VersionIdentifier;
33import eu.dasish.annotation.schema.Annotation;
34import eu.dasish.annotation.schema.CachedRepresentationInfo;
35import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
36import eu.dasish.annotation.schema.NewSourceInfo;
37import eu.dasish.annotation.schema.Permission;
38import eu.dasish.annotation.schema.Source;
39import eu.dasish.annotation.schema.SourceInfo;
40import java.sql.SQLException;
41import java.sql.Timestamp;
42import java.util.HashMap;
43import java.util.List;
44import java.util.Map;
45import org.springframework.beans.factory.annotation.Autowired;
46
47/**
48 *
49 * @author olhsha
50 */
51public class DaoRequestor {
52
53    @Autowired
54    UserDao userDao;
55    @Autowired
56    PermissionsDao permissionsDao;
57    @Autowired
58    CachedRepresentationDao cachedRepresentationDao;
59    @Autowired
60    VersionDao versionDao;
61    @Autowired
62    SourceDao sourceDao;
63    @Autowired
64    AnnotationDao annotationDao;
65    @Autowired
66    NotebookDao notebookDao;
67   
68   
69   
70    public Number getAnnotationInternalIdentifier(AnnotationIdentifier annotationIdentifier){
71        return annotationDao.getInternalID(annotationIdentifier);
72    }
73   
74     public AnnotationIdentifier getAnnotationExternalIdentifier(Number annotationID){
75        return annotationDao.getExternalID(annotationID);
76    }
77
78     public Number getUserInternalIdentifier(UserIdentifier userIdentifier){
79        return userDao.getInternalID(userIdentifier);
80    }
81   
82     public UserIdentifier getUserExternalIdentifier(Number userID){
83        return userDao.getExternalID(userID);
84    } 
85     
86    /**
87     *
88     * @param versionID
89     * @param cachedID
90     * @return result[0] # deleted rows (versionID, cachedID) in the table
91     * "versions_cached_representations" result[1] # deleted rows in the table
92     * "cached_representation"
93     */
94    public int[] deleteCachedForVersion(Number versionID, Number cachedID) {
95        int[] result = new int[2];
96        result[0] = versionDao.deleteVersionCachedRepresentation(versionID, cachedID);
97        if (result[0] > 0) {
98            result[1] = cachedRepresentationDao.deleteCachedRepresentationInfo(cachedID);
99        } else {
100            result[1] = 0;
101
102        }
103        return result;
104    }
105
106    /**
107     *
108     * @param versionID
109     * @param cached
110     * @return result[0] = the internalId of the added (if it is not yet in th
111     * DB) cached representation result[1] # added rows to
112     * "versions_cached_representations"
113     */
114    public Number[] addCachedForVersion(Number versionID, CachedRepresentationInfo cached) {
115        Number[] result = new Number[2];
116        result[0] = cachedRepresentationDao.getInternalID(new CachedRepresentationIdentifier(cached.getRef()));
117        if (result[0] == null) {
118            result[0] = cachedRepresentationDao.addCachedRepresentationInfo(cached);
119        }
120        result[1] = versionDao.addVersionCachedRepresentation(versionID, result[0]);
121        return result;
122    }
123   
124    /**
125     *
126     * @param versionID
127     * @return 
128     * result[0] # deleted rows in the joit table "versions_cached_representations"
129     * result[1] # deleted rows in "version" table
130     * result[2] # deleted cached representations (which are not referred by other versions)
131     *
132     */
133
134    public int[] deleteVersionWithCachedRepresentations(Number versionID) {
135        int[] result = new int[3];
136        List<Number> cachedRepresentations = versionDao.retrieveCachedRepresentationList(versionID);
137        int[] deleteVersion = versionDao.deleteVersion(versionID);
138        result[0] = deleteVersion[0];
139        result[1] = deleteVersion[1];
140        result[3] = 0;
141        for (Number cachedID : cachedRepresentations) {
142            result[3] = result[3] + cachedRepresentationDao.deleteCachedRepresentationInfo(cachedID);
143
144        }
145        return result;
146    }
147   
148    /**
149     *
150     * @param sourceID
151     * @return result[0] # deleted rows in "sources_versions" table name
152     * result[1] # deleted rows in "source" table
153     * result[2] # deleted rows in "version" table (not used by the other sources)
154     */
155    public int[] deleteSourceWithVersions(Number sourceID){
156        int[] result = new int[3]; 
157        List<Number> versions = sourceDao.retrieveVersionList(sourceID);
158        int[] deleteSource = sourceDao.deleteSource(sourceID);
159        result[0] = deleteSource[0];
160        result[1]=deleteSource[1];
161        result[3] =0;
162        for (Number versionID : versions) {
163            int[] deleteVersion = deleteVersionWithCachedRepresentations(versionID);
164            result[3]=result[3]+deleteVersion[1];
165        }
166      return result;
167     
168    }
169   
170    /**
171     * @param source
172     * @return internal Id of the newly added source or -1 if the source is not added because no version for it;
173     * in the last case the rest-interface  will return envelope asking to add a version (with the cached representation);
174     * after the clinet adds the version (s)he gets the version external ID which is to be set in source;
175     * the the client asks to add the source again.
176     * @throws SQLException
177     */
178    public Number addSourceAndPairSourceVersion(NewSourceInfo newSource) throws SQLException{
179        Number versionID = versionDao.getInternalID(new VersionIdentifier(newSource.getVersion()));
180        if (versionID == null) {
181            System.out.println("Cannot add source because there is no version for it, and no cached representation. Create them first and try again.");
182            return -1;
183        }
184        Source source = new Source();
185        SourceIdentifier externalIdentifier = new SourceIdentifier();
186        source.setURI(externalIdentifier.toString());
187        source.setLink(newSource.getLink());
188        source.setVersion(newSource.getVersion());       
189        Number result = sourceDao.addSource(source);
190        final int sourceVersions = sourceDao.addSourceVersion(result, versionID);
191        return result;
192    }
193   
194 
195
196    ///////////////////////////////////////////////
197   
198    /**
199     *
200     * @param annotationID
201     * @param sources
202     * @return the mapping temporarySourceID -> peristenExternalSOurceId
203     * adds a source from "sources" to the DB if it is not there, to the table "target_source"
204     * adds the wro (annotationID, sourceID) to the joint table "annotations_sources"
205     * @throws SQLException
206     */
207    public Map<String, String> addTargetSourcesToAnnotation(Number annotationID, List<NewOrExistingSourceInfo> sources) throws SQLException {
208        Map<String, String> result = new HashMap<String, String>();
209        for (NewOrExistingSourceInfo noesi : sources) {
210            SourceInfo source = noesi.getSource();
211            if (source != null) {
212                int affectedRows = annotationDao.addAnnotationSourcePair(annotationID, sourceDao.getInternalID(new SourceIdentifier(source.getRef())));
213            } else {
214                Number newSourceID = addSourceAndPairSourceVersion(noesi.getNewSource());
215                if (newSourceID.intValue() == -1) {
216                    result.put(noesi.getNewSource().getId(), null);
217                } else {
218                    result.put(noesi.getNewSource().getId(), sourceDao.getExternalID(newSourceID).toString());
219                    int affectedRows = annotationDao.addAnnotationSourcePair(annotationID, newSourceID);
220                }
221            }
222        }
223        return result;
224    } 
225   
226   
227      ////////////////////////////////////////////////////////////////////////
228   
229    public List<Number> getFilteredAnnotationIDs(String link, String text, String access, String namespace, UserIdentifier owner, Timestamp after, Timestamp before) {
230
231        List<Number> annotationIDs = null;
232       
233        if (link != null) {
234            List<Number> sourceIDs = sourceDao.getSourcesForLink(link);
235            annotationIDs = annotationDao.retrieveAnnotationList(sourceIDs);
236        }
237       
238        Number ownerID =  null; 
239        if (owner != null) {
240            ownerID = userDao.getInternalID(owner);
241        }
242
243        return annotationDao.getFilteredAnnotationIDs(annotationIDs, text, access, namespace, ownerID, after, before);
244    }
245
246     /**
247     *
248     * @param annotationId
249     * @return
250     * result[0] = # removed "annotations_principals_permissions" rows
251     * result[1] = # removed "annotations_target_sources" rows
252     * result[2] = # removed annotation rows (should be 1)
253     * result[3] = # deleted sources
254     */
255    public int[] deleteAnnotationWithSources(Number annotationID) throws SQLException {
256        int[] result = new int[4];
257        List<Number> sourceIDs = annotationDao.retrieveSourceIDs(annotationID);       
258        int[] deleteAnnotation = annotationDao.deleteAnnotation(annotationID);
259        result[0]= deleteAnnotation[0];
260        result[1]= deleteAnnotation[1];
261        result[2] = deleteAnnotation[2];
262        result[3]=0;
263        for (Number sourceID : sourceIDs) {
264            int[] deleteSource = deleteSourceWithVersions(sourceID);
265            result[3] = result[3] + deleteSource[1];
266        }
267        return result;
268    }
269     
270   
271    public Annotation getAnnotation(Number annotationID) throws SQLException{
272        Annotation result = annotationDao.getAnnotationWithoutSources(annotationID);
273        List<Number> sourceIDs = annotationDao.retrieveSourceIDs(annotationID);
274        for (Number sourceID : sourceIDs) {
275            NewOrExistingSourceInfo noesi = new  NewOrExistingSourceInfo();
276            Source source = sourceDao.getSource(sourceID);
277            SourceInfo sourceInfo = new SourceInfo();
278            sourceInfo.setLink(source.getLink());
279            sourceInfo.setRef(source.getURI());
280            sourceInfo.setVersion(source.getVersion());
281            noesi.setSource(sourceInfo);
282            result.getTargetSources().getTarget().add(noesi);
283        }
284        return result;
285    }
286   
287   
288    //need to return an envelope!
289    public Annotation addAnnotationWithTargetSources(Annotation annotation, Number userID) throws SQLException{
290       
291        Number annotationID = annotationDao.addAnnotation(annotation, userID);
292       
293        List<NewOrExistingSourceInfo> sources = annotation.getTargetSources().getTarget();
294        Map<String, String> sourceIdPairs= addTargetSourcesToAnnotation(annotationID, sources);
295       
296        if (sourceIdPairs.containsValue(null)){
297           // for one of the soirces there was no version and cached representation
298            // envelope
299           return annotation;
300        }
301        String body = Helpers.serializeBody(annotation.getBody());
302        String newBody = Helpers.replace(body, sourceIdPairs);
303        int affectedAnnotRows = annotationDao.updateBody(annotationID, newBody);
304       
305        // Add the permission (annotation_id, owner);
306        int affectedPermissions = permissionsDao.addAnnotationPrincipalPermission(annotationID, userID, Permission.OWNER);
307       
308        Annotation newAnnotation = getAnnotation(annotationID);
309        return newAnnotation;
310    }
311   
312} 
Note: See TracBrowser for help on using the repository browser.