]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
17c269575ceb382e8f96da676857a3cd05bbf401
[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 RelationsCommon getCommonPart() {
78         return relation;
79     }
80
81     @Override
82     public void setCommonPart(RelationsCommon theRelation) {
83         this.relation = theRelation;
84     }
85
86     /**get associated Relation (for index/GET_ALL)
87      */
88     @Override
89     public RelationsCommonList getCommonPartList() {
90         return relationList;
91     }
92
93     @Override
94     public void setCommonPartList(RelationsCommonList theRelationList) {
95         this.relationList = theRelationList;
96     }
97
98     @Override
99     public RelationsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
100             throws Exception {
101         throw new UnsupportedOperationException();
102     }
103
104     @Override
105     public void fillCommonPart(RelationsCommon theRelation, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
106         throw new UnsupportedOperationException();
107     }
108
109     @Override
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();
115
116         TenantBindingConfigReaderImpl tReader = ServiceMain.getInstance().getTenantBindingConfigReader();
117         String serviceName = getServiceContext().getServiceName().toLowerCase();
118         ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName);
119
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);
125         }
126         return relList;
127     }
128
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.
131      * @param ctx the ctx
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
138      */
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);
147
148         relationListItem.setSubjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_1));
149
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));
154
155         relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2));
156         
157         relationListItem.setUri(serviceContextPath + id);
158
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);
163
164         //Object o1 =  docModel.getProperty(ctx.getCommonPartLabel(), "subject");
165         //Object o2 =  docModel.getProperty(ctx.getCommonPartLabel(), "object");
166
167         String subjectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.subjectUri);
168         subject.setUri(subjectUri);
169         relationListItem.setSubject(subject);
170
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);
174
175         String objectUri = (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.objectUri);
176         object.setUri(objectUri);
177         relationListItem.setObject(object);
178
179         return relationListItem;
180     }
181
182      // DocumentModel itemDocModel = docModelFromCSID(ctx, itemCsid);
183
184     protected RelationsDocListItem createRelationsDocListItem(ServiceContext  ctx, 
185                                                                                                  ServiceBindingType sbt,
186                                                                                                  String itemCsid,
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         // CSPACE-4037 REMOVING: item.setService(documentType);//this one comes from the record, as documentType1, documentType2.   Current app seems to use servicename for this.
192         item.setCsid(itemCsid);
193
194         DocumentModel itemDocModel =  NuxeoUtils.getDocFromCsid(getRepositorySession(), ctx, itemCsid);    //null if not found.
195         if (itemDocModel!=null){
196             String itemDocType = itemDocModel.getDocumentType().getName();
197             // CSPACE-4037 REMOVING: item.setDocumentTypeFromModel(itemDocType);           //this one comes from the nuxeo documentType
198
199             //DEBUG: 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;
204                 }  else {
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 );
207                 }
208             } else {
209                 //usedDocumentTypeFromPayload = false;
210                 item.setDocumentType(itemDocType);
211             }   */
212              if (Tools.isBlank(documentType)){
213                item.setDocumentType(itemDocType);
214              }
215
216             // TODO: clean all the output statements out of here when CSPACE-4037 is done.
217             //TODO: ensure that itemDocType is really the entry point, i.e. servicename==doctype
218             //ServiceBindingType itemSbt2 = tReader.getServiceBinding(ctx.getTenantId(), itemDocType);
219             String propName = "ERROR-FINDING-PROP-VALUE";
220             ServiceBindingType itemSbt = tReader.getServiceBindingForDocType(ctx.getTenantId(), itemDocType);
221             try {
222                 propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP);
223                 String itemDocname = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP, itemDocModel);
224                 if (propName==null || itemDocname==null){
225                     System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType: "+documentType);
226                 }  else{
227                     item.setName(itemDocname);
228                     System.out.println("=== found prop : "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType: "+documentType);
229                 }
230             } catch (Throwable t){
231                  System.out.println("====Error finding objectNameProperty: "+itemDocModel+" field "+ServiceBindingUtils.OBJ_NAME_PROP+"="+propName
232                                             +" not found in itemDocType: "+itemDocType+" inner: "+t.getMessage());
233             }
234             propName = "ERROR-FINDING-PROP-VALUE";
235             try {
236                 propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP);
237                 String itemDocnumber = ServiceBindingUtils.getMappedFieldInDoc(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP, itemDocModel);
238
239                  if (propName==null || itemDocnumber==null){
240                      System.out.println("=== prop NOT found: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
241                                                 +" documentType: "+documentType);
242                  } else {
243                      item.setNumber(itemDocnumber);
244                      System.out.println("============ found prop : "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
245                                                 +" documentType: "+documentType);
246                  }
247             } catch (Throwable t){
248                 System.out.println("====Error finding objectNumberProperty: "+ServiceBindingUtils.OBJ_NUMBER_PROP+"="+propName
249                                            +" not found in itemDocType: "+itemDocType+" inner: "+t.getMessage());
250             }
251         } else {
252             item.setError("INVALID: related object is absent");
253             // Laramie20110510 CSPACE-3739  throw the exception for 3739, otherwise, don't throw it.
254             //throw new Exception("INVALID: related object is absent "+itemCsid);
255         }
256         return item;
257     }
258
259     @Override
260     public String getQProperty(String prop) {
261         return "/" + RelationConstants.NUXEO_SCHEMA_ROOT_ELEMENT + "/" + prop;
262     }
263 }
264