]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c869640190f6a099dec5c1dc811b7cf88efbec29
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.relation.nuxeo;
25
26 import java.util.Iterator;
27
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;
44
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;
54
55 /**
56  * RelationDocumentModelHandler
57  *
58  * $LastChangedRevision: $
59  * $LastChangedDate: $
60  */
61 public class RelationDocumentModelHandler
62         extends RemoteDocumentModelHandlerImpl<RelationsCommon, RelationsCommonList> {
63
64     private final Logger logger = LoggerFactory.getLogger(RelationDocumentModelHandler.class);
65     /**
66      * relation is used to stash JAXB object to use when handle is called
67      * for Action.CREATE, Action.UPDATE or Action.GET
68      */
69     private RelationsCommon relation;
70     /**
71      * relationList is stashed when handle is called
72      * for ACTION.GET_ALL
73      */
74     private RelationsCommonList relationList;
75     
76     @Override
77     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
78         super.handleCreate(wrapDoc);
79     }
80     
81     @Override
82     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
83         super.handleUpdate(wrapDoc);
84     }
85
86     @Override
87     public RelationsCommon getCommonPart() {
88         return relation;
89     }
90
91     @Override
92     public void setCommonPart(RelationsCommon theRelation) {
93         this.relation = theRelation;
94     }
95
96     /**get associated Relation (for index/GET_ALL)
97      */
98     @Override
99     public RelationsCommonList getCommonPartList() {
100         return relationList;
101     }
102
103     @Override
104     public void setCommonPartList(RelationsCommonList theRelationList) {
105         this.relationList = theRelationList;
106     }
107
108     @Override
109     public RelationsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
110             throws Exception {
111         throw new UnsupportedOperationException();
112     }
113
114     @Override
115     public void fillCommonPart(RelationsCommon theRelation, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
116         throw new UnsupportedOperationException();
117     }
118
119     @Override
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();
125
126         TenantBindingConfigReaderImpl tReader = ServiceMain.getInstance().getTenantBindingConfigReader();
127         String serviceName = getServiceContext().getServiceName().toLowerCase();
128         ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName);
129
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);
135         }
136         return relList;
137     }
138
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.
141      * @param ctx the ctx
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
148      */
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);
157
158         relationListItem.setSubjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_1));
159
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));
164
165         relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2));
166         
167         relationListItem.setUri(serviceContextPath + id);
168
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);
173
174         //Object o1 =  docModel.getProperty(ctx.getCommonPartLabel(), "subject");
175         //Object o2 =  docModel.getProperty(ctx.getCommonPartLabel(), "object");
176
177         String subjectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.subjectUri);
178         subject.setUri(subjectUri);
179         relationListItem.setSubject(subject);
180
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);
184
185         String objectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.objectUri);
186         object.setUri(objectUri);
187         relationListItem.setObject(object);
188
189         return relationListItem;
190     }
191
192      // DocumentModel itemDocModel = docModelFromCSID(ctx, itemCsid);
193
194     protected RelationsDocListItem createRelationsDocListItem(ServiceContext  ctx, 
195                                                                                                  ServiceBindingType sbt,
196                                                                                                  String itemCsid,
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);
203
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
208
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;
214                 }  else {
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 );
217                 }
218             } else {
219                 //usedDocumentTypeFromPayload = false;
220                 item.setDocumentType(itemDocType);
221             }   */
222              if (Tools.isBlank(documentType)){
223                item.setDocumentType(itemDocType);
224              }
225
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);
231             try {
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);
236                 }  else{
237                     item.setName(itemDocname);
238                     //System.out.println("=== found prop : "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType: "+documentType);
239                 }
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());
243             }
244             propName = "ERROR-FINDING-PROP-VALUE";
245             try {
246                 propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP);
247                 String itemDocnumber = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP, itemDocModel);
248
249                  if (propName==null || itemDocnumber==null){
250                      //System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
251                       //                          +" documentType: "+documentType);
252                  } else {
253                      item.setNumber(itemDocnumber);
254                      //System.out.println("============ found prop : "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
255                       //                          +" documentType: "+documentType);
256                  }
257             } catch (Throwable t){
258                 logger.error("====Error finding objectNumberProperty: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"="+propName
259                                            +" not found in itemDocType: "+itemDocType+" inner: "+t.getMessage());
260             }
261         } else {
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);
265         }
266         return item;
267     }
268
269     @Override
270     public String getQProperty(String prop) {
271         return "/" + RelationConstants.NUXEO_SCHEMA_ROOT_ELEMENT + "/" + prop;
272     }
273 }
274