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 RelationsCommon getCommonPart() {
82 public void setCommonPart(RelationsCommon theRelation) {
83 this.relation = theRelation;
86 /**get associated Relation (for index/GET_ALL)
89 public RelationsCommonList getCommonPartList() {
94 public void setCommonPartList(RelationsCommonList theRelationList) {
95 this.relationList = theRelationList;
99 public RelationsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
101 throw new UnsupportedOperationException();
105 public void fillCommonPart(RelationsCommon theRelation, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
106 throw new UnsupportedOperationException();
110 public RelationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
111 RelationsCommonList relList = this.extractPagingInfo(new RelationsCommonList(), wrapDoc) ;
112 relList.setFieldsReturned("subjectCsid|relationshipType|predicateDisplayName|objectCsid|uri|csid|subject|object");
113 ServiceContext ctx = getServiceContext();
114 String serviceContextPath = getServiceContextPath();
116 TenantBindingConfigReaderImpl tReader = ServiceMain.getInstance().getTenantBindingConfigReader();
117 String serviceName = getServiceContext().getServiceName().toLowerCase();
118 ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName);
120 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
121 while(iter.hasNext()){
122 DocumentModel docModel = iter.next();
123 RelationListItem relListItem = getRelationListItem(ctx, sbt, tReader, docModel, serviceContextPath);
124 relList.getRelationListItem().add(relListItem);
129 /** Gets the relation list item, looking up the subject and object documents, and getting summary
130 * info via the objectName and objectNumber properties in tenant-bindings.
132 * @param sbt the ServiceBindingType of Relations service
133 * @param tReader the tenant-bindings reader, for looking up docnumber and docname
134 * @param docModel the doc model
135 * @param serviceContextPath the service context path
136 * @return the relation list item, with nested subject and object summary info.
137 * @throws Exception the exception
139 private RelationListItem getRelationListItem(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
140 ServiceBindingType sbt,
141 TenantBindingConfigReaderImpl tReader,
142 DocumentModel docModel,
143 String serviceContextPath) throws Exception {
144 RelationListItem relationListItem = new RelationListItem();
145 String id = getCsid(docModel);
146 relationListItem.setCsid(id);
148 relationListItem.setSubjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_1));
150 String predicate = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE);
151 relationListItem.setRelationshipType(predicate);
152 relationListItem.setPredicate(predicate); //predicate is new name for relationshipType.
153 relationListItem.setPredicateDisplayName((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE_DISPLAYNAME));
155 relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2));
157 relationListItem.setUri(serviceContextPath + id);
159 //Now fill in summary info for the related docs: subject and object.
160 String subjectCsid = relationListItem.getSubjectCsid();
161 String documentType = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_1);
162 RelationsDocListItem subject = createRelationsDocListItem(ctx, sbt, subjectCsid, tReader, documentType);
164 //Object o1 = docModel.getProperty(ctx.getCommonPartLabel(), "subject");
165 //Object o2 = docModel.getProperty(ctx.getCommonPartLabel(), "object");
167 String subjectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.subjectUri);
168 subject.setUri(subjectUri);
169 relationListItem.setSubject(subject);
171 String objectCsid = relationListItem.getObjectCsid();
172 String documentType2 = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_2);
173 RelationsDocListItem object = createRelationsDocListItem(ctx, sbt, objectCsid, tReader, documentType2);
175 String objectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.objectUri);
176 object.setUri(objectUri);
177 relationListItem.setObject(object);
179 return relationListItem;
182 // DocumentModel itemDocModel = docModelFromCSID(ctx, itemCsid);
184 protected RelationsDocListItem createRelationsDocListItem(ServiceContext ctx,
185 ServiceBindingType sbt,
187 TenantBindingConfigReaderImpl tReader,
188 String documentType) throws Exception {
189 RelationsDocListItem item = new RelationsDocListItem();
190 item.setDocumentType(documentType);//this one comes from the record, as documentType1, documentType2.
191 item.setService(documentType);//this one comes from the record, as documentType1, documentType2. Current app seems to use servicename for this.
192 item.setCsid(itemCsid);
194 DocumentModel itemDocModel = NuxeoUtils.getDocFromCsid(getRepositorySession(), ctx, itemCsid); //null if not found.
195 if (itemDocModel!=null){
196 String itemDocType = itemDocModel.getDocumentType().getName();
197 item.setDocumentTypeFromModel(itemDocType); //this one comes from the nuxeo documentType
199 System.out.println("\r\n******** AuthorityItemDocumentModelHandlder documentType **************\r\n\tdocModel: "+itemDocType+"\r\n\tpayload: "+documentType);
200 boolean usedDocumentTypeFromPayload = true;
201 if ( ! Tools.isBlank(documentType)){
202 if (documentType.equals(itemDocType)){
203 usedDocumentTypeFromPayload = true;
205 // Laramie20110510 CSPACE-3739 throw the exception for 3739, otherwise, don't throw it.
206 //throw new Exception("documentType supplied was wrong. supplied: "+documentType+" required: "+itemDocType+ " itemCsid: "+itemCsid );
209 usedDocumentTypeFromPayload = false;
210 item.setDocumentType(itemDocType);
215 //TODO: ensure that itemDocType is really the entry point, i.e. servicename==doctype
216 //ServiceBindingType itemSbt2 = tReader.getServiceBinding(ctx.getTenantId(), itemDocType);
217 ServiceBindingType itemSbt = tReader.getServiceBindingForDocType(ctx.getTenantId(), itemDocType);
219 String itemDocname = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP, itemDocModel);
220 item.setName(itemDocname);
221 //System.out.println("\r\n\r\n\r\n=================\r\n~~found prop : "+ServiceBindingUtils.OBJ_NAME_PROP+" in :"+itemDocname);
222 } catch (Throwable t){
223 System.out.println("\r\n\r\n\r\n=================\r\n NOTE: "+itemDocModel+" field "+ServiceBindingUtils.OBJ_NAME_PROP+" not found in DocModel: "+itemDocModel.getName()+" inner: "+t.getMessage());
226 String itemDocnumber = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP, itemDocModel);
227 item.setNumber(itemDocnumber);
228 //System.out.println("\r\n\r\n\r\n=================\r\n~~found prop : "+ServiceBindingUtils.OBJ_NUMBER_PROP+" in :"+itemDocnumber);
229 } catch (Throwable t){
230 System.out.println("\r\n\r\n\r\n=================\r\n NOTE: field "+ServiceBindingUtils.OBJ_NUMBER_PROP+" not found in DocModel: "+itemDocModel.getName()+" inner: "+t.getMessage());
233 item.setError("INVALID: related object is absent");
234 // Laramie20110510 CSPACE-3739 throw the exception for 3739, otherwise, don't throw it.
235 //throw new Exception("INVALID: related object is absent "+itemCsid);
241 public String getQProperty(String prop) {
242 return "/" + RelationConstants.NUXEO_SCHEMA_ROOT_ELEMENT + "/" + prop;