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.relation.nuxeo;
26 import java.util.HashMap;
27 import java.util.Iterator;
29 import org.collectionspace.services.client.PoxPayloadIn;
30 import org.collectionspace.services.client.PoxPayloadOut;
31 import org.collectionspace.services.common.ResourceBase;
32 import org.collectionspace.services.common.ResourceMap;
33 import org.collectionspace.services.common.ServiceMain;
34 import org.collectionspace.services.common.api.RefName;
35 import org.collectionspace.services.common.api.Tools;
36 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
37 import org.collectionspace.services.common.context.ServiceBindingUtils;
38 import org.collectionspace.services.common.document.DocumentNotFoundException;
39 import org.collectionspace.services.common.document.InvalidDocumentException;
40 import org.collectionspace.services.common.relation.RelationJAXBSchema;
41 import org.collectionspace.services.common.relation.nuxeo.RelationConstants;
42 import org.collectionspace.services.common.context.ServiceContext;
43 import org.collectionspace.services.common.repository.RepositoryClient;
44 import org.collectionspace.services.common.repository.RepositoryClientFactory;
45 import org.collectionspace.services.common.service.ServiceBindingType;
46 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
47 import org.collectionspace.services.relation.RelationsCommon;
48 import org.collectionspace.services.relation.RelationsCommonList;
49 import org.collectionspace.services.relation.RelationsCommonList.RelationListItem;
52 import org.collectionspace.services.client.PersonAuthorityClient;
53 import org.collectionspace.services.client.OrgAuthorityClient;
54 import org.collectionspace.services.client.LocationAuthorityClient;
55 import org.collectionspace.services.client.TaxonomyAuthorityClient;
57 import org.collectionspace.services.common.document.DocumentWrapper;
58 import org.collectionspace.services.jaxb.AbstractCommonList;
59 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
60 import org.collectionspace.services.relation.RelationsDocListItem;
61 import org.nuxeo.ecm.core.api.ClientException;
62 import org.nuxeo.ecm.core.api.DocumentModel;
63 import org.nuxeo.ecm.core.api.DocumentModelList;
64 import org.nuxeo.ecm.core.api.model.PropertyException;
65 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
70 * RelationDocumentModelHandler
72 * $LastChangedRevision: $
75 public class RelationDocumentModelHandler
76 extends RemoteDocumentModelHandlerImpl<RelationsCommon, RelationsCommonList> {
78 private final Logger logger = LoggerFactory.getLogger(RelationDocumentModelHandler.class);
80 * relation is used to stash JAXB object to use when handle is called
81 * for Action.CREATE, Action.UPDATE or Action.GET
83 private RelationsCommon relation;
85 * relationList is stashed when handle is called
88 private RelationsCommonList relationList;
91 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
92 // Merge in the data from the payload
93 super.handleCreate(wrapDoc);
95 // Obtain document models for the subject and object of the relation, so that
96 // we ensure we have value docType, URI info. If the docModels support refNames,
97 // we will also set those.
98 // Note that this introduces another caching problem...
99 DocumentModel relationDocModel = wrapDoc.getWrappedObject();
100 ServiceContext ctx = getServiceContext();
101 DocumentModel subjectDocModel = getSubjectOrObjectDocModel(relationDocModel, ctx, SUBJ_DOC_MODEL);
102 DocumentModel objectDocModel = getSubjectOrObjectDocModel(relationDocModel, ctx, OBJ_DOC_MODEL);
104 // Use values from the subject and object document models to populate the
105 // relevant fields of the relation's own document model.
106 if (subjectDocModel != null) {
107 populateSubjectOrObjectValues(relationDocModel, subjectDocModel, ctx, SUBJ_DOC_MODEL);
109 if (objectDocModel != null) {
110 populateSubjectOrObjectValues(relationDocModel, objectDocModel, ctx, OBJ_DOC_MODEL);
115 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
116 super.handleUpdate(wrapDoc);
120 public RelationsCommon getCommonPart() {
125 public void setCommonPart(RelationsCommon theRelation) {
126 this.relation = theRelation;
129 /**get associated Relation (for index/GET_ALL)
132 public RelationsCommonList getCommonPartList() {
137 public void setCommonPartList(RelationsCommonList theRelationList) {
138 this.relationList = theRelationList;
142 public RelationsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
144 throw new UnsupportedOperationException();
148 public void fillCommonPart(RelationsCommon theRelation, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
149 throw new UnsupportedOperationException();
153 public RelationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
154 RelationsCommonList relList = this.extractPagingInfo(new RelationsCommonList(), wrapDoc);
155 relList.setFieldsReturned("subjectCsid|relationshipType|predicateDisplayName|objectCsid|uri|csid|subject|object");
156 ServiceContext ctx = getServiceContext();
157 String serviceContextPath = getServiceContextPath();
159 TenantBindingConfigReaderImpl tReader = ServiceMain.getInstance().getTenantBindingConfigReader();
160 String serviceName = getServiceContext().getServiceName().toLowerCase();
161 ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName);
163 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
164 while (iter.hasNext()) {
165 DocumentModel docModel = iter.next();
166 RelationListItem relListItem = getRelationListItem(ctx, sbt, tReader, docModel, serviceContextPath);
167 relList.getRelationListItem().add(relListItem);
172 /** Gets the relation list item, looking up the subject and object documents, and getting summary
173 * info via the objectName and objectNumber properties in tenant-bindings.
175 * @param sbt the ServiceBindingType of Relations service
176 * @param tReader the tenant-bindings reader, for looking up docnumber and docname
177 * @param docModel the doc model
178 * @param serviceContextPath the service context path
179 * @return the relation list item, with nested subject and object summary info.
180 * @throws Exception the exception
182 private RelationListItem getRelationListItem(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
183 ServiceBindingType sbt,
184 TenantBindingConfigReaderImpl tReader,
185 DocumentModel docModel,
186 String serviceContextPath) throws Exception {
187 RelationListItem relationListItem = new RelationListItem();
188 String id = getCsid(docModel);
189 relationListItem.setCsid(id);
191 relationListItem.setSubjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_1));
193 String predicate = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE);
194 relationListItem.setRelationshipType(predicate);
195 relationListItem.setPredicate(predicate); //predicate is new name for relationshipType.
196 relationListItem.setPredicateDisplayName((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE_DISPLAYNAME));
198 relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2));
200 relationListItem.setUri(serviceContextPath + id);
202 //Now fill in summary info for the related docs: subject and object.
203 String subjectCsid = relationListItem.getSubjectCsid();
204 String documentType = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_1);
205 RelationsDocListItem subject = createRelationsDocListItem(ctx, sbt, subjectCsid, tReader, documentType);
207 //Object o1 = docModel.getProperty(ctx.getCommonPartLabel(), "subject");
208 //Object o2 = docModel.getProperty(ctx.getCommonPartLabel(), "object");
210 String subjectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.SUBJECT_URI);
211 subject.setUri(subjectUri);
212 String subjectRefName = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.SUBJECT_REFNAME);
213 subject.setRefName(subjectRefName);
214 relationListItem.setSubject(subject);
216 String objectCsid = relationListItem.getObjectCsid();
217 String documentType2 = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_2);
218 RelationsDocListItem object = createRelationsDocListItem(ctx, sbt, objectCsid, tReader, documentType2);
220 String objectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.OBJECT_URI);
221 object.setUri(objectUri);
222 String objectRefName = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.OBJECT_REFNAME);
223 object.setRefName(objectRefName);
224 relationListItem.setObject(object);
226 return relationListItem;
229 // DocumentModel itemDocModel = docModelFromCSID(ctx, itemCsid);
230 protected RelationsDocListItem createRelationsDocListItem(ServiceContext ctx,
231 ServiceBindingType sbt,
233 TenantBindingConfigReaderImpl tReader,
234 String documentType) throws Exception {
235 RelationsDocListItem item = new RelationsDocListItem();
236 item.setDocumentType(documentType);//this one comes from the record, as documentType1, documentType2.
237 // CSPACE-4037 REMOVING: item.setService(documentType);//this one comes from the record, as documentType1, documentType2. Current app seems to use servicename for this.
238 item.setCsid(itemCsid);
240 DocumentModel itemDocModel = NuxeoUtils.getDocFromCsid(getRepositorySession(), ctx, itemCsid); //null if not found.
241 if (itemDocModel != null) {
242 String itemDocType = itemDocModel.getDocumentType().getName();
243 // CSPACE-4037 REMOVING: item.setDocumentTypeFromModel(itemDocType); //this one comes from the nuxeo documentType
245 //DEBUG: System.out.println("\r\n******** AuthorityItemDocumentModelHandlder documentType **************\r\n\tdocModel: "+itemDocType+"\r\n\tpayload: "+documentType);
246 //boolean usedDocumentTypeFromPayload = true;
247 /*if ( ! Tools.isBlank(documentType)){
248 if (documentType.equals(itemDocType)){
249 //usedDocumentTypeFromPayload = true;
251 // Laramie20110510 CSPACE-3739 throw the exception for 3739, otherwise, don't throw it.
252 //throw new Exception("documentType supplied was wrong. supplied: "+documentType+" required: "+itemDocType+ " itemCsid: "+itemCsid );
255 //usedDocumentTypeFromPayload = false;
256 item.setDocumentType(itemDocType);
258 if (Tools.isBlank(documentType)) {
259 item.setDocumentType(itemDocType);
262 // TODO: clean all the output statements out of here when CSPACE-4037 is done.
263 //TODO: ensure that itemDocType is really the entry point, i.e. servicename==doctype
264 //ServiceBindingType itemSbt2 = tReader.getServiceBinding(ctx.getTenantId(), itemDocType);
265 String propName = "ERROR-FINDING-PROP-VALUE";
266 ServiceBindingType itemSbt = tReader.getServiceBindingForDocType(ctx.getTenantId(), itemDocType);
268 propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP);
269 String itemDocname = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP, itemDocModel);
270 if (propName == null || itemDocname == null) {
271 //System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType: "+documentType);
273 item.setName(itemDocname);
274 //System.out.println("=== found prop : "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType: "+documentType);
276 } catch (Throwable t) {
277 System.out.println("====Error finding objectNameProperty: " + itemDocModel + " field " + ServiceBindingUtils.OBJ_NAME_PROP + "=" + propName
278 + " not found in itemDocType: " + itemDocType + " inner: " + t.getMessage());
280 propName = "ERROR-FINDING-PROP-VALUE";
282 propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP);
283 String itemDocnumber = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP, itemDocModel);
285 if (propName == null || itemDocnumber == null) {
286 //System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
287 // +" documentType: "+documentType);
289 item.setNumber(itemDocnumber);
290 //System.out.println("============ found prop : "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
291 // +" documentType: "+documentType);
293 } catch (Throwable t) {
294 logger.error("====Error finding objectNumberProperty: " + ServiceBindingUtils.OBJ_NUMBER_PROP + "=" + propName
295 + " not found in itemDocType: " + itemDocType + " inner: " + t.getMessage());
298 item.setError("INVALID: related object is absent");
299 // Laramie20110510 CSPACE-3739 throw the exception for 3739, otherwise, don't throw it.
300 //throw new Exception("INVALID: related object is absent "+itemCsid);
306 public String getQProperty(String prop) {
307 return "/" + RelationConstants.NUXEO_SCHEMA_ROOT_ELEMENT + "/" + prop;
310 private final boolean SUBJ_DOC_MODEL = true;
311 private final boolean OBJ_DOC_MODEL = false;
313 private DocumentModel getSubjectOrObjectDocModel(
314 DocumentModel relationDocModel, ServiceContext ctx, boolean fSubject) throws Exception {
315 // Get the document model for the object of the relation.
316 String commonPartLabel = ctx.getCommonPartLabel();
319 DocumentModel docModel = null;
320 // FIXME: Currently assumes that the object CSID is valid if present
321 // in the incoming payload.
323 csid = (String) relationDocModel.getProperty(commonPartLabel,
324 (fSubject?RelationJAXBSchema.SUBJECT_CSID:RelationJAXBSchema.OBJECT_CSID));
325 // FIXME: Remove this entire 'if' statement when legacy fields are removed from the Relation record:
326 if (Tools.isBlank(csid)) {
327 csid = (String) relationDocModel.getProperty(commonPartLabel,
328 (fSubject?RelationJAXBSchema.DOCUMENT_ID_1:RelationJAXBSchema.DOCUMENT_ID_2));
330 } catch (PropertyException pe) {
331 // Per CSPACE-4468, ignore any property exception here.
332 // The objectCsid and/or subjectCsid field in a relation record
333 // can now be null (missing), because a refName value can be
334 // provided as an alternate identifier.
336 if (Tools.notBlank(csid)) {
337 DocumentWrapper<DocumentModel> docWrapper = getRepositoryClient(ctx).getDocFromCsid(ctx, csid);
338 docModel = docWrapper.getWrappedObject();
339 } else { // if (Tools.isBlank(objectCsid)) {
341 refName = (String) relationDocModel.getProperty(commonPartLabel,
342 (fSubject?RelationJAXBSchema.SUBJECT_REFNAME:RelationJAXBSchema.OBJECT_REFNAME));
343 docModel = ResourceBase.getDocModelForRefName(refName, ctx.getResourceMap());
344 } catch (Exception e) {
345 throw new InvalidDocumentException(
346 "Relation record must have a CSID or refName to identify the object of the relation.", e);
350 throw new DocumentNotFoundException("Relation.getSubjectOrObjectDocModel could not find doc with CSID: "
351 +csid+" and/or refName: "+refName );
356 private void populateSubjectOrObjectValues(
357 DocumentModel relationDocModel,
358 DocumentModel subjectOrObjectDocModel,
361 HashMap<String,Object> properties = new HashMap<String,Object>();
364 String doctype = (String) subjectOrObjectDocModel.getType();
365 properties.put((fSubject?RelationJAXBSchema.SUBJECT_DOCTYPE:RelationJAXBSchema.OBJECT_DOCTYPE),
367 // FIXME: Remove the line below when legacy fields are removed from the Relation record:
368 properties.put((fSubject?RelationJAXBSchema.DOCUMENT_TYPE_1:RelationJAXBSchema.DOCUMENT_TYPE_2),
371 String csid = (String) subjectOrObjectDocModel.getName();
372 properties.put((fSubject?RelationJAXBSchema.SUBJECT_CSID:RelationJAXBSchema.OBJECT_CSID),
374 // FIXME: Remove the two lines immediately below when legacy fields are removed from the Relation record:
375 properties.put((fSubject?RelationJAXBSchema.DOCUMENT_ID_1:RelationJAXBSchema.DOCUMENT_ID_2),
378 String uri = (String) subjectOrObjectDocModel.getProperty(COLLECTIONSPACE_CORE_SCHEMA,
379 COLLECTIONSPACE_CORE_URI);
380 properties.put((fSubject?RelationJAXBSchema.SUBJECT_URI:RelationJAXBSchema.OBJECT_URI),
383 String common_schema = getCommonSchemaNameForDocType(doctype);
385 if(common_schema!=null) {
386 String refname = (String)subjectOrObjectDocModel.getProperty(common_schema,
388 properties.put((fSubject?RelationJAXBSchema.SUBJECT_REFNAME:RelationJAXBSchema.OBJECT_REFNAME),
391 } catch (ClientException ce) {
392 throw new RuntimeException(
393 "populateSubjectOrObjectValues: Problem fetching field " + ce.getLocalizedMessage());
396 // FIXME: Call below is based solely on Nuxeo API docs; have not yet verified that it correctly updates existing
397 // property values in the target document model.
399 relationDocModel.setProperties(ctx.getCommonPartLabel(), properties);
400 } catch (ClientException ce) {
401 throw new RuntimeException(
402 "populateSubjectValues: Problem setting fields " + ce.getLocalizedMessage());
406 private String getCommonSchemaNameForDocType(String docType) {
407 String common_schema = null;
408 if("Person".equals(docType))
409 common_schema = PersonAuthorityClient.SERVICE_ITEM_COMMON_PART_NAME;
410 else if("Organization".equals(docType))
411 common_schema = OrgAuthorityClient.SERVICE_ITEM_COMMON_PART_NAME;
412 else if("Locationitem".equals(docType))
413 common_schema = LocationAuthorityClient.SERVICE_ITEM_COMMON_PART_NAME;
414 else if("Taxon".equals(docType))
415 common_schema = TaxonomyAuthorityClient.SERVICE_ITEM_COMMON_PART_NAME;
416 //else leave it null.
417 return common_schema;