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.Iterator;
28 import org.collectionspace.services.client.PoxPayloadIn;
29 import org.collectionspace.services.client.PoxPayloadOut;
30 import org.collectionspace.services.common.ServiceMain;
31 import org.collectionspace.services.common.api.Tools;
32 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
33 import org.collectionspace.services.common.context.ServiceBindingUtils;
34 import org.collectionspace.services.common.relation.RelationJAXBSchema;
35 import org.collectionspace.services.common.relation.nuxeo.RelationConstants;
36 import org.collectionspace.services.common.context.ServiceContext;
37 import org.collectionspace.services.common.repository.RepositoryClient;
38 import org.collectionspace.services.common.repository.RepositoryClientFactory;
39 import org.collectionspace.services.common.service.ServiceBindingType;
40 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
41 import org.collectionspace.services.relation.RelationsCommon;
42 import org.collectionspace.services.relation.RelationsCommonList;
43 import org.collectionspace.services.relation.RelationsCommonList.RelationListItem;
45 import org.collectionspace.services.common.document.DocumentWrapper;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
47 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
48 import org.collectionspace.services.relation.RelationsDocListItem;
49 import org.nuxeo.ecm.core.api.DocumentModel;
50 import org.nuxeo.ecm.core.api.DocumentModelList;
51 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * RelationDocumentModelHandler
58 * $LastChangedRevision: $
61 public class RelationDocumentModelHandler
62 extends RemoteDocumentModelHandlerImpl<RelationsCommon, RelationsCommonList> {
64 private final Logger logger = LoggerFactory.getLogger(RelationDocumentModelHandler.class);
66 * relation is used to stash JAXB object to use when handle is called
67 * for Action.CREATE, Action.UPDATE or Action.GET
69 private RelationsCommon relation;
71 * relationList is stashed when handle is called
74 private RelationsCommonList relationList;
77 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
78 super.handleCreate(wrapDoc);
82 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
83 super.handleUpdate(wrapDoc);
87 public RelationsCommon getCommonPart() {
92 public void setCommonPart(RelationsCommon theRelation) {
93 this.relation = theRelation;
96 /**get associated Relation (for index/GET_ALL)
99 public RelationsCommonList getCommonPartList() {
104 public void setCommonPartList(RelationsCommonList theRelationList) {
105 this.relationList = theRelationList;
109 public RelationsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
111 throw new UnsupportedOperationException();
115 public void fillCommonPart(RelationsCommon theRelation, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
116 throw new UnsupportedOperationException();
120 public RelationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
121 RelationsCommonList relList = this.extractPagingInfo(new RelationsCommonList(), wrapDoc) ;
122 relList.setFieldsReturned("subjectCsid|relationshipType|predicateDisplayName|objectCsid|uri|csid|subject|object");
123 ServiceContext ctx = getServiceContext();
124 String serviceContextPath = getServiceContextPath();
126 TenantBindingConfigReaderImpl tReader = ServiceMain.getInstance().getTenantBindingConfigReader();
127 String serviceName = getServiceContext().getServiceName().toLowerCase();
128 ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName);
130 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
131 while(iter.hasNext()){
132 DocumentModel docModel = iter.next();
133 RelationListItem relListItem = getRelationListItem(ctx, sbt, tReader, docModel, serviceContextPath);
134 relList.getRelationListItem().add(relListItem);
139 /** Gets the relation list item, looking up the subject and object documents, and getting summary
140 * info via the objectName and objectNumber properties in tenant-bindings.
142 * @param sbt the ServiceBindingType of Relations service
143 * @param tReader the tenant-bindings reader, for looking up docnumber and docname
144 * @param docModel the doc model
145 * @param serviceContextPath the service context path
146 * @return the relation list item, with nested subject and object summary info.
147 * @throws Exception the exception
149 private RelationListItem getRelationListItem(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
150 ServiceBindingType sbt,
151 TenantBindingConfigReaderImpl tReader,
152 DocumentModel docModel,
153 String serviceContextPath) throws Exception {
154 RelationListItem relationListItem = new RelationListItem();
155 String id = getCsid(docModel);
156 relationListItem.setCsid(id);
158 relationListItem.setSubjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_1));
160 String predicate = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE);
161 relationListItem.setRelationshipType(predicate);
162 relationListItem.setPredicate(predicate); //predicate is new name for relationshipType.
163 relationListItem.setPredicateDisplayName((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE_DISPLAYNAME));
165 relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2));
167 relationListItem.setUri(serviceContextPath + id);
169 //Now fill in summary info for the related docs: subject and object.
170 String subjectCsid = relationListItem.getSubjectCsid();
171 String documentType = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_1);
172 RelationsDocListItem subject = createRelationsDocListItem(ctx, sbt, subjectCsid, tReader, documentType);
174 //Object o1 = docModel.getProperty(ctx.getCommonPartLabel(), "subject");
175 //Object o2 = docModel.getProperty(ctx.getCommonPartLabel(), "object");
177 String subjectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.subjectUri);
178 subject.setUri(subjectUri);
179 relationListItem.setSubject(subject);
181 String objectCsid = relationListItem.getObjectCsid();
182 String documentType2 = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_2);
183 RelationsDocListItem object = createRelationsDocListItem(ctx, sbt, objectCsid, tReader, documentType2);
185 String objectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.objectUri);
186 object.setUri(objectUri);
187 relationListItem.setObject(object);
189 return relationListItem;
192 // DocumentModel itemDocModel = docModelFromCSID(ctx, itemCsid);
194 protected RelationsDocListItem createRelationsDocListItem(ServiceContext ctx,
195 ServiceBindingType sbt,
197 TenantBindingConfigReaderImpl tReader,
198 String documentType) throws Exception {
199 RelationsDocListItem item = new RelationsDocListItem();
200 item.setDocumentType(documentType);//this one comes from the record, as documentType1, documentType2.
201 // CSPACE-4037 REMOVING: item.setService(documentType);//this one comes from the record, as documentType1, documentType2. Current app seems to use servicename for this.
202 item.setCsid(itemCsid);
204 DocumentModel itemDocModel = NuxeoUtils.getDocFromCsid(getRepositorySession(), ctx, itemCsid); //null if not found.
205 if (itemDocModel!=null){
206 String itemDocType = itemDocModel.getDocumentType().getName();
207 // CSPACE-4037 REMOVING: item.setDocumentTypeFromModel(itemDocType); //this one comes from the nuxeo documentType
209 //DEBUG: System.out.println("\r\n******** AuthorityItemDocumentModelHandlder documentType **************\r\n\tdocModel: "+itemDocType+"\r\n\tpayload: "+documentType);
210 //boolean usedDocumentTypeFromPayload = true;
211 /*if ( ! Tools.isBlank(documentType)){
212 if (documentType.equals(itemDocType)){
213 //usedDocumentTypeFromPayload = true;
215 // Laramie20110510 CSPACE-3739 throw the exception for 3739, otherwise, don't throw it.
216 //throw new Exception("documentType supplied was wrong. supplied: "+documentType+" required: "+itemDocType+ " itemCsid: "+itemCsid );
219 //usedDocumentTypeFromPayload = false;
220 item.setDocumentType(itemDocType);
222 if (Tools.isBlank(documentType)){
223 item.setDocumentType(itemDocType);
226 // TODO: clean all the output statements out of here when CSPACE-4037 is done.
227 //TODO: ensure that itemDocType is really the entry point, i.e. servicename==doctype
228 //ServiceBindingType itemSbt2 = tReader.getServiceBinding(ctx.getTenantId(), itemDocType);
229 String propName = "ERROR-FINDING-PROP-VALUE";
230 ServiceBindingType itemSbt = tReader.getServiceBindingForDocType(ctx.getTenantId(), itemDocType);
232 propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP);
233 String itemDocname = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP, itemDocModel);
234 if (propName==null || itemDocname==null){
235 //System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType: "+documentType);
237 item.setName(itemDocname);
238 //System.out.println("=== found prop : "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType: "+documentType);
240 } catch (Throwable t){
241 System.out.println("====Error finding objectNameProperty: "+itemDocModel+" field "+ServiceBindingUtils.OBJ_NAME_PROP+"="+propName
242 +" not found in itemDocType: "+itemDocType+" inner: "+t.getMessage());
244 propName = "ERROR-FINDING-PROP-VALUE";
246 propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP);
247 String itemDocnumber = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP, itemDocModel);
249 if (propName==null || itemDocnumber==null){
250 //System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
251 // +" documentType: "+documentType);
253 item.setNumber(itemDocnumber);
254 //System.out.println("============ found prop : "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
255 // +" documentType: "+documentType);
257 } catch (Throwable t){
258 logger.error("====Error finding objectNumberProperty: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"="+propName
259 +" not found in itemDocType: "+itemDocType+" inner: "+t.getMessage());
262 item.setError("INVALID: related object is absent");
263 // Laramie20110510 CSPACE-3739 throw the exception for 3739, otherwise, don't throw it.
264 //throw new Exception("INVALID: related object is absent "+itemCsid);
270 public String getQProperty(String prop) {
271 return "/" + RelationConstants.NUXEO_SCHEMA_ROOT_ELEMENT + "/" + prop;