]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c17d4379da473ac6b302b47783581a7fecd9902a
[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.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;
43
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;
53
54 /**
55  * RelationDocumentModelHandler
56  *
57  * $LastChangedRevision: $
58  * $LastChangedDate: $
59  */
60 public class RelationDocumentModelHandler
61         extends RemoteDocumentModelHandlerImpl<RelationsCommon, RelationsCommonList> {
62
63     private final Logger logger = LoggerFactory.getLogger(RelationDocumentModelHandler.class);
64     /**
65      * relation is used to stash JAXB object to use when handle is called
66      * for Action.CREATE, Action.UPDATE or Action.GET
67      */
68     private RelationsCommon relation;
69     /**
70      * relationList is stashed when handle is called
71      * for ACTION.GET_ALL
72      */
73     private RelationsCommonList relationList;
74
75     @Override
76     public RelationsCommon getCommonPart() {
77         return relation;
78     }
79
80     @Override
81     public void setCommonPart(RelationsCommon theRelation) {
82         this.relation = theRelation;
83     }
84
85     /**get associated Relation (for index/GET_ALL)
86      */
87     @Override
88     public RelationsCommonList getCommonPartList() {
89         return relationList;
90     }
91
92     @Override
93     public void setCommonPartList(RelationsCommonList theRelationList) {
94         this.relationList = theRelationList;
95     }
96
97     @Override
98     public RelationsCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
99             throws Exception {
100         throw new UnsupportedOperationException();
101     }
102
103     @Override
104     public void fillCommonPart(RelationsCommon theRelation, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
105         throw new UnsupportedOperationException();
106     }
107
108     @Override
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();
114
115         TenantBindingConfigReaderImpl tReader = ServiceMain.getInstance().getTenantBindingConfigReader();
116         String serviceName = getServiceContext().getServiceName().toLowerCase();
117         ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName);
118
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);
124         }
125         return relList;
126     }
127
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.
130      * @param ctx the ctx
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
137      */
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);
146
147         relationListItem.setSubjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_1));
148
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));
153
154         relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2));
155         
156         relationListItem.setUri(serviceContextPath + id);
157
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);
163
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);
168
169         return relationListItem;
170     }
171
172      // DocumentModel itemDocModel = docModelFromCSID(ctx, itemCsid);
173
174     protected RelationsDocListItem createRelationsDocListItem(ServiceContext  ctx, 
175                                                                                                  ServiceBindingType sbt,
176                                                                                                  String itemCsid,
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);
183
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
188
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);
192             try {
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());
198             }
199             try {
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());
205             }
206         } else {
207             item.setError("INVALID: related object is absent");
208         }
209         return item;
210     }
211
212     @Override
213     public String getQProperty(String prop) {
214         return "/" + RelationConstants.NUXEO_SCHEMA_ROOT_ELEMENT + "/" + prop;
215     }
216 }
217