]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
db893dcd2ccd82b85fbcc86d2ac72e99bdd73227
[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.common.vocabulary.nuxeo;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.collectionspace.services.client.PayloadInputPart;
31 import org.collectionspace.services.client.PayloadOutputPart;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.client.RelationClient;
35 //import org.collectionspace.services.common.authority.AuthorityItemRelations;
36 import org.collectionspace.services.common.context.MultipartServiceContext;
37 import org.collectionspace.services.common.context.ServiceContext;
38 import org.collectionspace.services.common.document.DocumentWrapper;
39 import org.collectionspace.services.common.relation.IRelationsManager;
40 import org.collectionspace.services.common.service.ObjectPartType;
41 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
42 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
43 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
44 import org.collectionspace.services.relation.RelationResource;
45 import org.collectionspace.services.relation.RelationsCommon;
46 import org.collectionspace.services.relation.RelationsCommonList;
47 import org.collectionspace.services.relation.RelationsDocListItem;
48 import org.collectionspace.services.relation.RelationshipType;
49 import org.nuxeo.ecm.core.api.DocumentModel;
50
51 import javax.ws.rs.core.MultivaluedMap;
52 import javax.ws.rs.core.UriInfo;
53
54 /**
55  * AuthorityItemDocumentModelHandler
56  *
57  * $LastChangedRevision: $
58  * $LastChangedDate: $
59  */
60 public abstract class AuthorityItemDocumentModelHandler<AICommon, AICommonList>
61         extends RemoteDocumentModelHandlerImpl<AICommon, AICommonList> {
62
63         private String authorityItemCommonSchemaName;
64         
65     //private final Logger logger = LoggerFactory.getLogger(AuthorityItemDocumentModelHandler.class);
66     /**
67      * item is used to stash JAXB object to use when handle is called
68      * for Action.CREATE, Action.UPDATE or Action.GET
69      */
70     protected AICommon item;
71     /**
72      * itemList is stashed when handle is called
73      * for ACTION.GET_ALL
74      */
75     protected AICommonList itemList;
76     
77     /**
78      * inVocabulary is the parent Authority for this context
79      */
80     protected String inAuthority;
81     
82     public AuthorityItemDocumentModelHandler(String authorityItemCommonSchemaName) {
83         this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
84     }
85
86     public String getInAuthority() {
87                 return inAuthority;
88         }
89
90         public void setInAuthority(String inAuthority) {
91                 this.inAuthority = inAuthority;
92         }
93
94     /* (non-Javadoc)
95      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
96      */
97     @Override
98     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
99         // first fill all the parts of the document
100         super.handleCreate(wrapDoc);            
101         handleInAuthority(wrapDoc.getWrappedObject());
102     }
103     
104     /**
105      * Check the logic around the parent pointer. Note that we only need do this on
106      * create, since we have logic to make this read-only on update. 
107      * 
108      * @param docModel
109      * 
110      * @throws Exception the exception
111      */
112     private void handleInAuthority(DocumentModel docModel) throws Exception {
113         docModel.setProperty(authorityItemCommonSchemaName, 
114                         AuthorityItemJAXBSchema.IN_AUTHORITY, inAuthority);
115     }
116
117
118     /**
119      * getCommonPart get associated item
120      * @return
121      */
122     @Override
123     public AICommon getCommonPart() {
124         return item;
125     }
126
127     @Override
128     public void setCommonPart(AICommon item) {
129         this.item = item;
130     }
131
132     /**
133      * getCommonPartList get associated item (for index/GET_ALL)
134      * @return
135      */
136     @Override
137     public AICommonList getCommonPartList() {
138         return itemList;
139     }
140
141     @Override
142     public void setCommonPartList(AICommonList itemList) {
143         this.itemList = itemList;
144     }
145
146     @Override
147     public AICommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
148             throws Exception {
149         throw new UnsupportedOperationException();
150     }
151
152     @Override
153     public void fillCommonPart(AICommon itemObject, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
154         throw new UnsupportedOperationException();
155     }
156     
157     /* (non-Javadoc)
158      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
159      */
160     @Override
161     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
162             throws Exception {
163         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
164         
165         // Add the CSID to the common part
166         if (partMeta.getLabel().equalsIgnoreCase(authorityItemCommonSchemaName)) {
167                 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
168                 unQObjectProperties.put("csid", csid);
169         }
170         
171         return unQObjectProperties;
172     }
173     
174     /**
175      * Filters out AuthorityItemJAXBSchema.IN_AUTHORITY, to ensure that
176      * the parent link remains untouched.
177      * @param objectProps the properties parsed from the update payload
178      * @param partMeta metadata for the object to fill
179      */
180     @Override
181     public void filterReadOnlyPropertiesForPart(
182                 Map<String, Object> objectProps, ObjectPartType partMeta) {
183         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
184         objectProps.remove(AuthorityItemJAXBSchema.IN_AUTHORITY);
185         objectProps.remove(AuthorityItemJAXBSchema.CSID);
186     }
187
188      @Override
189     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
190             throws Exception {
191         MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
192         super.extractAllParts(wrapDoc);
193
194         String thisCSID = NuxeoUtils.getCsid(wrapDoc.getWrappedObject());
195
196         //TODO: add broader, etc. here.
197         String predicate = RelationshipType.HAS_BROADER.value();
198         RelationsCommonList relationsCommonList = getRelations(thisCSID, null, predicate);
199         if (relationsCommonList.getTotalItems() == 0){
200             relationsCommonList = getRelations(null, thisCSID, predicate);   //for development... try switching subject and object.  This is not correct, though.
201         }
202         PayloadOutputPart relationsPart = new PayloadOutputPart(RelationClient.SERVICE_COMMON_LIST_NAME, relationsCommonList);
203         ctx.addOutputPart(relationsPart);
204     }
205
206     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
207         super.fillAllParts(wrapDoc, action);
208         ServiceContext ctx = getServiceContext();
209         PoxPayloadIn input = (PoxPayloadIn)ctx.getInput();
210         DocumentModel documentModel = (wrapDoc.getWrappedObject());
211         String itemCsid = documentModel.getName();
212
213         //TODO: create all relations....  UPDATE and CREATE will call.   Updates AuthorityItem part
214         RelationsCommonList relationsCommonList = updateRelations(itemCsid, input);
215         PayloadOutputPart payloadOutputPart = new PayloadOutputPart(RelationClient.SERVICE_COMMON_LIST_NAME, relationsCommonList);
216         ctx.setProperty(RelationClient.SERVICE_COMMON_LIST_NAME, payloadOutputPart);
217     }
218
219     public void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
220         super.completeUpdate(wrapDoc);
221         //now we add part for relations list
222         ServiceContext ctx = getServiceContext();
223         PayloadOutputPart foo = (PayloadOutputPart)ctx.getProperty(RelationClient.SERVICE_COMMON_LIST_NAME);
224         ((PoxPayloadOut)ctx.getOutput()).addPart(foo);
225     }
226
227     //===================================================================
228     public RelationsCommonList updateRelations(String itemCSID, PoxPayloadIn input) throws Exception {
229         PayloadInputPart part = input.getPart(RelationClient.SERVICE_COMMON_LIST_NAME);        //input.getPart("relations_common");
230         if (part == null) {
231             System.out.println("Nothing to do in updateRelations: " + input);
232             return null;
233         }
234         RelationsCommonList relationsCommonListBody = (RelationsCommonList) part.getBody();
235
236         ServiceContext ctx = getServiceContext();
237         MultivaluedMap queryParams = ctx.getQueryParams();
238        String predicate = RelationshipType.HAS_BROADER.value();
239         queryParams.putSingle(IRelationsManager.PREDICATE_QP, predicate);
240         queryParams.putSingle(IRelationsManager.SUBJECT_QP, null);
241         queryParams.putSingle(IRelationsManager.SUBJECT_TYPE_QP, null);
242         queryParams.putSingle(IRelationsManager.OBJECT_QP, itemCSID);
243         queryParams.putSingle(IRelationsManager.OBJECT_TYPE_QP, null);
244
245         RelationsCommonList childList = (new RelationResource()).getList(ctx.getUriInfo());    //magically knows all query params because they are in the context.
246         for (RelationsCommonList.RelationListItem childListItem : childList.getRelationListItem()) {
247            // System.out.println("    childListItem: " + childListItem);
248             //todo: if not found in update list, remove from system
249             //todo: if update list item not found in child list, add to system.
250         }
251
252         //Leave predicate, swap subject and object.
253         queryParams.putSingle(IRelationsManager.SUBJECT_QP, itemCSID);
254         queryParams.putSingle(IRelationsManager.OBJECT_QP, null);
255
256         RelationsCommonList parentList = (new RelationResource()).getList(ctx.getUriInfo());
257         for (RelationsCommonList.RelationListItem parentListItem : parentList.getRelationListItem()) {
258            // System.out.println("    parentListItem: " + parentListItem);
259             //todo: if num-parents > 1 then complain.
260             //todo: if not found in update list, remove from system
261             //todo: if update list item not found in parent list, add to system.
262         }
263         List<RelationsCommonList.RelationListItem> inboundList = relationsCommonListBody.getRelationListItem();
264         for (RelationsCommonList.RelationListItem item : inboundList) {
265             RelationsCommon rc = new RelationsCommon();
266             rc.setCsid(item.getCsid());
267             rc.setDocumentId1(item.getSubjectCsid());
268             rc.setDocumentId2(item.getObjectCsid());
269             rc.setRelationshipType(item.getPredicate());
270             //todo: is an enum:  rc.setPredicate(item.getPredicate());
271             rc.setDocumentType1(item.getSubject().getType());
272             rc.setDocumentType2(item.getObject().getType());
273
274             PoxPayloadOut payloadOut = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
275             PayloadOutputPart outputPart = new PayloadOutputPart(RelationClient.SERVICE_COMMONPART_NAME, rc);
276             payloadOut.addPart(outputPart);
277
278             RelationResource relationResource = new RelationResource();
279             Object res = relationResource.create(ctx.getUriInfo(), payloadOut.toXML());    //NOTE ui recycled from above to pass in unknown query params.
280         }
281         return relationsCommonListBody;
282     }
283     //================= TODO: move this to common, refactoring this and  CollectionObjectResource.java
284
285     public RelationsCommonList getRelations(String subjectCSID, String objectCSID, String predicate) throws Exception {
286         ServiceContext ctx = getServiceContext();
287         MultivaluedMap queryParams = ctx.getQueryParams();
288         queryParams.putSingle(IRelationsManager.PREDICATE_QP, predicate);
289         queryParams.putSingle(IRelationsManager.SUBJECT_QP, subjectCSID);
290         queryParams.putSingle(IRelationsManager.OBJECT_QP, objectCSID);
291
292         RelationResource relationResource = new RelationResource();
293         RelationsCommonList relationsCommonList = relationResource.getList(ctx.getUriInfo());
294         return relationsCommonList;
295     }
296
297     //============================= END refactor ==========================
298
299 }
300