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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.common.vocabulary.nuxeo;
26 import java.util.ArrayList;
27 import java.util.List;
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;
51 import javax.ws.rs.core.MultivaluedMap;
52 import javax.ws.rs.core.UriInfo;
55 * AuthorityItemDocumentModelHandler
57 * $LastChangedRevision: $
60 public abstract class AuthorityItemDocumentModelHandler<AICommon, AICommonList>
61 extends RemoteDocumentModelHandlerImpl<AICommon, AICommonList> {
63 private String authorityItemCommonSchemaName;
65 //private final Logger logger = LoggerFactory.getLogger(AuthorityItemDocumentModelHandler.class);
67 * item is used to stash JAXB object to use when handle is called
68 * for Action.CREATE, Action.UPDATE or Action.GET
70 protected AICommon item;
72 * itemList is stashed when handle is called
75 protected AICommonList itemList;
78 * inVocabulary is the parent Authority for this context
80 protected String inAuthority;
82 public AuthorityItemDocumentModelHandler(String authorityItemCommonSchemaName) {
83 this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
86 public String getInAuthority() {
90 public void setInAuthority(String inAuthority) {
91 this.inAuthority = inAuthority;
95 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
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());
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.
110 * @throws Exception the exception
112 private void handleInAuthority(DocumentModel docModel) throws Exception {
113 docModel.setProperty(authorityItemCommonSchemaName,
114 AuthorityItemJAXBSchema.IN_AUTHORITY, inAuthority);
119 * getCommonPart get associated item
123 public AICommon getCommonPart() {
128 public void setCommonPart(AICommon item) {
133 * getCommonPartList get associated item (for index/GET_ALL)
137 public AICommonList getCommonPartList() {
142 public void setCommonPartList(AICommonList itemList) {
143 this.itemList = itemList;
147 public AICommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
149 throw new UnsupportedOperationException();
153 public void fillCommonPart(AICommon itemObject, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
154 throw new UnsupportedOperationException();
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)
161 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
163 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
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);
171 return unQObjectProperties;
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
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);
189 public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
191 MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
192 super.extractAllParts(wrapDoc);
194 String thisCSID = NuxeoUtils.getCsid(wrapDoc.getWrappedObject());
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.
202 PayloadOutputPart relationsPart = new PayloadOutputPart(RelationClient.SERVICE_COMMON_LIST_NAME, relationsCommonList);
203 ctx.addOutputPart(relationsPart);
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();
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);
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);
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");
231 System.out.println("Nothing to do in updateRelations: " + input);
234 RelationsCommonList relationsCommonListBody = (RelationsCommonList) part.getBody();
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);
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.
252 //Leave predicate, swap subject and object.
253 queryParams.putSingle(IRelationsManager.SUBJECT_QP, itemCSID);
254 queryParams.putSingle(IRelationsManager.OBJECT_QP, null);
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.
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());
274 PoxPayloadOut payloadOut = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
275 PayloadOutputPart outputPart = new PayloadOutputPart(RelationClient.SERVICE_COMMONPART_NAME, rc);
276 payloadOut.addPart(outputPart);
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.
281 return relationsCommonListBody;
283 //================= TODO: move this to common, refactoring this and CollectionObjectResource.java
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);
292 RelationResource relationResource = new RelationResource();
293 RelationsCommonList relationsCommonList = relationResource.getList(ctx.getUriInfo());
294 return relationsCommonList;
297 //============================= END refactor ==========================