]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c6b74c1a65f97cb44268c177385eee8549482390
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.relation.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.collectionspace.services.common.relation.RelationJAXBSchema;
31 import org.collectionspace.services.common.relation.nuxeo.RelationUtilsNuxeoImpl;
32 import org.collectionspace.services.common.relation.RelationsManager;
33
34 import org.collectionspace.services.relation.Relation;
35 import org.collectionspace.services.relation.RelationList;
36 import org.collectionspace.services.relation.RelationList.RelationListItem;
37
38 import org.collectionspace.services.relation.nuxeo.RelationNuxeoConstants;
39 import org.collectionspace.services.common.repository.DocumentWrapper;
40 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
41 import org.nuxeo.ecm.core.api.DocumentModel;
42 import org.nuxeo.ecm.core.api.DocumentModelList;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * RelationDocumentModelHandler
48  *
49  * $LastChangedRevision: $
50  * $LastChangedDate: $
51  */
52 public class RelationDocumentModelHandler
53         extends DocumentModelHandler<Relation, RelationList> {
54
55     private final Logger logger = LoggerFactory.getLogger(RelationDocumentModelHandler.class);
56     /**
57      * relation is used to stash JAXB object to use when handle is called
58      * for Action.CREATE, Action.UPDATE or Action.GET
59      */
60     private Relation relation;
61     /**
62      * relationList is stashed when handle is called
63      * for ACTION.GET_ALL
64      */
65     private RelationList relationList;
66
67     @Override
68     public void prepare(Action action) throws Exception {
69         //no specific action needed
70     }
71
72
73     /**
74      * getCommonObject get associated Relation
75      * @return
76      */
77     @Override
78     public Relation getCommonObject() {
79         return relation;
80     }
81
82     /**
83      * setCommonObject set associated relation
84      * @param relation
85      */
86     @Override
87     public void setCommonObject(Relation relation) {
88         this.relation = relation;
89     }
90
91     /**
92      * getRelationList get associated Relation (for index/GET_ALL)
93      * @return
94      */
95     @Override
96     public RelationList getCommonObjectList() {
97         return relationList;
98     }
99
100     @Override
101     public void setCommonObjectList(RelationList relationList) {
102         this.relationList = relationList;
103     }
104
105     @Override
106     public Relation extractCommonObject(DocumentWrapper wrapDoc)
107             throws Exception {
108         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
109         Relation theRelation = new Relation();
110         
111         RelationUtilsNuxeoImpl.fillRelationFromDocModel(theRelation, docModel);
112
113         return theRelation;
114     }
115
116     @Override
117     public void fillCommonObject(Relation relation, DocumentWrapper wrapDoc) throws Exception {
118         DocumentModel docModel = (DocumentModel) wrapDoc.getWrappedObject();
119
120         RelationUtilsNuxeoImpl.fillDocModelFromRelation(relation, docModel);
121     }
122
123     @Override
124     public RelationList extractCommonObjectList(DocumentWrapper wrapDoc) throws Exception {
125         DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
126
127         Map propsFromResource = this.getProperties();
128         String subjectCsid = (String)propsFromResource.get(RelationsManager.SUBJECT);
129         String predicate = (String)propsFromResource.get(RelationsManager.PREDICATE);
130         String objectCsid = (String)propsFromResource.get(RelationsManager.OBJECT);
131
132         RelationList coList = new RelationList();
133         List<RelationList.RelationListItem> list = coList.getRelationListItem();
134
135         //FIXME: iterating over a long list of documents is not a long term
136         //strategy...need to change to more efficient iterating in future
137         Iterator<DocumentModel> iter = docList.iterator();
138         while(iter.hasNext()){
139             DocumentModel docModel = iter.next();
140             if (RelationsManager.isQueryMatch(docModel, subjectCsid,
141                                         predicate, objectCsid) == true) {
142                                 RelationListItem coListItem = new RelationListItem();
143                                 RelationUtilsNuxeoImpl.fillRelationListItemFromDocModel(
144                                                 coListItem, docModel);
145                                 list.add(coListItem);
146                         }
147         }
148
149         return coList;
150     }
151     
152     public String getDocumentType() {
153         return RelationNuxeoConstants.NUXEO_DOCTYPE;
154     }
155
156     @Override
157     public void fillCommonObjectList(RelationList obj, DocumentWrapper wrapDoc) throws Exception {
158         throw new UnsupportedOperationException();
159     }
160
161     /**
162      * getQProperty converts the given property to qualified schema property
163      * @param prop
164      * @return
165      */
166     private String getQProperty(String property) {
167         return RelationsManager.getQPropertyName(property);
168     }
169 }
170