]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c1524552cd605b40815c9f41f53227a8f3487dad
[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
31 import org.collectionspace.services.common.relation.IRelationsManager;
32 import org.collectionspace.services.common.relation.nuxeo.RelationConstants;
33 import org.collectionspace.services.common.relation.nuxeo.RelationsUtils;
34 import org.collectionspace.services.common.document.DocumentHandler.Action;
35 import org.collectionspace.services.relation.RelationsCommon;
36 import org.collectionspace.services.relation.RelationsCommonList;
37 import org.collectionspace.services.relation.RelationsCommonList.RelationListItem;
38
39 import org.collectionspace.services.common.document.DocumentWrapper;
40 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
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 RemoteDocumentModelHandlerImpl<RelationsCommon, RelationsCommonList> {
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 RelationsCommon relation;
61     /**
62      * relationList is stashed when handle is called
63      * for ACTION.GET_ALL
64      */
65     private RelationsCommonList relationList;
66
67
68     /**
69      * getCommonObject get associated Relation
70      * @return
71      */
72     @Override
73     public RelationsCommon getCommonPart() {
74         return relation;
75     }
76
77     /**
78      * setCommonObject set associated relation
79      * @param relation
80      */
81     @Override
82     public void setCommonPart(RelationsCommon relation) {
83         this.relation = relation;
84     }
85
86     /**
87      * getRelationList get associated Relation (for index/GET_ALL)
88      * @return
89      */
90     @Override
91     public RelationsCommonList getCommonPartList() {
92         return relationList;
93     }
94
95     @Override
96     public void setCommonPartList(RelationsCommonList relationList) {
97         this.relationList = relationList;
98     }
99
100     @Override
101     public RelationsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
102             throws Exception {
103         throw new UnsupportedOperationException();
104     }
105
106     @Override
107     public void fillCommonPart(RelationsCommon relation, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
108         throw new UnsupportedOperationException();
109     }
110
111     @Override
112     public RelationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
113         DocumentModelList docList = wrapDoc.getWrappedObject();
114
115         Map propsFromResource = this.getProperties();
116         String subjectCsid = (String) propsFromResource.get(IRelationsManager.SUBJECT);
117         String predicate = (String) propsFromResource.get(IRelationsManager.PREDICATE);
118         String objectCsid = (String) propsFromResource.get(IRelationsManager.OBJECT);
119
120         RelationsCommonList relList = new RelationsCommonList();
121         List<RelationsCommonList.RelationListItem> itemList = relList.getRelationListItem();
122
123         //FIXME: iterating over a long itemList of documents is not a long term
124         //strategy...need to change to more efficient iterating in future
125         Iterator<DocumentModel> iter = docList.iterator();
126         while(iter.hasNext()){
127             DocumentModel docModel = iter.next();
128             if(RelationsUtils.isQueryMatch(docModel, subjectCsid,
129                     predicate, objectCsid) == true){
130                 RelationListItem relListItem = RelationsUtils.getRelationListItem(getServiceContext(),
131                         docModel, getServiceContextPath());
132                 itemList.add(relListItem);
133             }
134         }
135         return relList;
136     }
137
138   
139
140     @Override
141     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
142         super.fillAllParts(wrapDoc);
143         fillDublinCoreObject(wrapDoc); //dublincore might not be needed in future
144     }
145
146     private void fillDublinCoreObject(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
147         DocumentModel docModel = wrapDoc.getWrappedObject();
148         //FIXME property setter should be dynamically set using schema inspection
149         //so it does not require hard coding
150         // a default title for the Dublin Core schema
151         docModel.setPropertyValue("dublincore:title", RelationConstants.NUXEO_DC_TITLE);
152     }
153
154
155     @Override
156     public String getQProperty(String prop) {
157         return "/" + RelationConstants.NUXEO_SCHEMA_ROOT_ELEMENT + "/" + prop;
158     }
159 }
160