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.config.TenantBindingConfigReaderImpl;
32 import org.collectionspace.services.common.context.ServiceBindingUtils;
33 import org.collectionspace.services.common.relation.RelationJAXBSchema;
34 import org.collectionspace.services.common.relation.nuxeo.RelationConstants;
35 import org.collectionspace.services.common.context.ServiceContext;
36 import org.collectionspace.services.common.repository.RepositoryClient;
37 import org.collectionspace.services.common.repository.RepositoryClientFactory;
38 import org.collectionspace.services.common.service.ServiceBindingType;
39 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
40 import org.collectionspace.services.relation.RelationsCommon;
41 import org.collectionspace.services.relation.RelationsCommonList;
42 import org.collectionspace.services.relation.RelationsCommonList.RelationListItem;
44 import org.collectionspace.services.common.document.DocumentWrapper;
45 import org.collectionspace.services.jaxb.AbstractCommonList;
46 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
47 import org.collectionspace.services.relation.RelationsDocListItem;
48 import org.nuxeo.ecm.core.api.DocumentModel;
49 import org.nuxeo.ecm.core.api.DocumentModelList;
50 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * RelationDocumentModelHandler
57 * $LastChangedRevision: $
60 public class RelationDocumentModelHandler
61 extends RemoteDocumentModelHandlerImpl<RelationsCommon, RelationsCommonList> {
63 private final Logger logger = LoggerFactory.getLogger(RelationDocumentModelHandler.class);
65 * relation is used to stash JAXB object to use when handle is called
66 * for Action.CREATE, Action.UPDATE or Action.GET
68 private RelationsCommon relation;
70 * relationList is stashed when handle is called
73 private RelationsCommonList relationList;
76 public RelationsCommon getCommonPart() {
81 public void setCommonPart(RelationsCommon theRelation) {
82 this.relation = theRelation;
85 /**get associated Relation (for index/GET_ALL)
88 public RelationsCommonList getCommonPartList() {
93 public void setCommonPartList(RelationsCommonList theRelationList) {
94 this.relationList = theRelationList;
98 public RelationsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
100 throw new UnsupportedOperationException();
104 public void fillCommonPart(RelationsCommon theRelation, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
105 throw new UnsupportedOperationException();
109 public RelationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
110 RelationsCommonList relList = this.extractPagingInfo(new RelationsCommonList(), wrapDoc) ;
111 relList.setFieldsReturned("subjectCsid|relationshipType|predicateDisplayName|objectCsid|uri|csid|subject|object");
112 ServiceContext ctx = getServiceContext();
113 String serviceContextPath = getServiceContextPath();
115 TenantBindingConfigReaderImpl tReader = ServiceMain.getInstance().getTenantBindingConfigReader();
116 String serviceName = getServiceContext().getServiceName().toLowerCase();
117 ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName);
119 Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
120 while(iter.hasNext()){
121 DocumentModel docModel = iter.next();
122 RelationListItem relListItem = getRelationListItem(ctx, sbt, tReader, docModel, serviceContextPath);
123 relList.getRelationListItem().add(relListItem);
128 /** Gets the relation list item, looking up the subject and object documents, and getting summary
129 * info via the objectName and objectNumber properties in tenant-bindings.
131 * @param sbt the ServiceBindingType of Relations service
132 * @param tReader the tenant-bindings reader, for looking up docnumber and docname
133 * @param docModel the doc model
134 * @param serviceContextPath the service context path
135 * @return the relation list item, with nested subject and object summary info.
136 * @throws Exception the exception
138 private RelationListItem getRelationListItem(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
139 ServiceBindingType sbt,
140 TenantBindingConfigReaderImpl tReader,
141 DocumentModel docModel,
142 String serviceContextPath) throws Exception {
143 RelationListItem relationListItem = new RelationListItem();
144 String id = getCsid(docModel);
145 relationListItem.setCsid(id);
147 relationListItem.setSubjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_1));
149 String predicate = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE);
150 relationListItem.setRelationshipType(predicate);
151 relationListItem.setPredicate(predicate); //predicate is new name for relationshipType.
152 relationListItem.setPredicateDisplayName((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE_DISPLAYNAME));
154 relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2));
156 relationListItem.setUri(serviceContextPath + id);
158 //Now fill in summary info for the related docs: subject and object.
159 String subjectCsid = relationListItem.getSubjectCsid();
160 String documentType = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_1);
161 RelationsDocListItem subject = createRelationsDocListItem(ctx, sbt, subjectCsid, tReader, documentType);
162 relationListItem.setSubject(subject);
164 String objectCsid = relationListItem.getObjectCsid();
165 documentType = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_2);
166 RelationsDocListItem object = createRelationsDocListItem(ctx, sbt, objectCsid, tReader, documentType);
167 relationListItem.setObject(object);
169 return relationListItem;
172 // DocumentModel itemDocModel = docModelFromCSID(ctx, itemCsid);
174 protected RelationsDocListItem createRelationsDocListItem(ServiceContext ctx,
175 ServiceBindingType sbt,
177 TenantBindingConfigReaderImpl tReader,
178 String documentType) throws Exception {
179 RelationsDocListItem item = new RelationsDocListItem();
180 item.setDocumentType(documentType);//this one comes from the record, as documentType1, documentType2.
181 item.setService(documentType);//this one comes from the record, as documentType1, documentType2. Current app seems to use servicename for this.
182 item.setCsid(itemCsid);
184 DocumentModel itemDocModel = NuxeoUtils.getDocFromCsid(getRepositorySession(), ctx, itemCsid); //null if not found.
185 if (itemDocModel!=null){
186 String itemDocType = itemDocModel.getDocumentType().getName();
187 item.setDocumentTypeFromModel(itemDocType); //this one comes from the nuxeo documentType
189 //TODO: ensure that itemDocType is really the entry point, i.e. servicename==doctype
190 //ServiceBindingType itemSbt2 = tReader.getServiceBinding(ctx.getTenantId(), itemDocType);
191 ServiceBindingType itemSbt = tReader.getServiceBindingForDocType(ctx.getTenantId(), itemDocType);
193 String itemDocname = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP, itemDocModel);
194 item.setName(itemDocname);
195 //System.out.println("\r\n\r\n\r\n=================\r\n~~found prop : "+ServiceBindingUtils.OBJ_NAME_PROP+" in :"+itemDocname);
196 } catch (Throwable t){
197 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());
200 String itemDocnumber = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP, itemDocModel);
201 item.setNumber(itemDocnumber);
202 //System.out.println("\r\n\r\n\r\n=================\r\n~~found prop : "+ServiceBindingUtils.OBJ_NUMBER_PROP+" in :"+itemDocnumber);
203 } catch (Throwable t){
204 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());
207 item.setError("INVALID: related object is absent");
213 public String getQProperty(String prop) {
214 return "/" + RelationConstants.NUXEO_SCHEMA_ROOT_ELEMENT + "/" + prop;