]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1af842ea5b85ab81693c7398cec4872437f63943
[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.common.vocabulary.nuxeo;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.ListIterator;
32 import java.util.Map;
33
34 import org.collectionspace.services.client.AuthorityClient;
35 import org.collectionspace.services.client.PayloadInputPart;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PoxPayloadIn;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.client.RelationClient;
40 //import org.collectionspace.services.common.authority.AuthorityItemRelations;
41 import org.collectionspace.services.common.api.CommonAPI;
42 import org.collectionspace.services.common.api.RefName;
43 import org.collectionspace.services.common.api.Tools;
44 import org.collectionspace.services.common.context.MultipartServiceContext;
45 import org.collectionspace.services.common.context.ServiceContext;
46 import org.collectionspace.services.common.document.DocumentWrapper;
47 import org.collectionspace.services.common.relation.IRelationsManager;
48 import org.collectionspace.services.common.service.ObjectPartType;
49 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
50 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
51 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
52 import org.collectionspace.services.relation.RelationResource;
53 import org.collectionspace.services.relation.RelationsCommon;
54 import org.collectionspace.services.relation.RelationsCommonList;
55 import org.collectionspace.services.relation.RelationsDocListItem;
56 import org.collectionspace.services.relation.RelationshipType;
57 import org.nuxeo.ecm.core.api.DocumentModel;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 import javax.management.relation.Relation;
62 import javax.ws.rs.core.MultivaluedMap;
63 import javax.ws.rs.core.UriInfo;
64
65 /**
66  * AuthorityItemDocumentModelHandler
67  *
68  * $LastChangedRevision: $
69  * $LastChangedDate: $
70  */
71 public abstract class AuthorityItemDocumentModelHandler<AICommon, AICommonList>
72         extends RemoteDocumentModelHandlerImpl<AICommon, AICommonList> {
73
74     private final Logger logger = LoggerFactory.getLogger(AuthorityItemDocumentModelHandler.class);
75
76         private String authorityItemCommonSchemaName;
77         
78     //private final Logger logger = LoggerFactory.getLogger(AuthorityItemDocumentModelHandler.class);
79     /**
80      * item is used to stash JAXB object to use when handle is called
81      * for Action.CREATE, Action.UPDATE or Action.GET
82      */
83     protected AICommon item;
84     /**
85      * itemList is stashed when handle is called
86      * for ACTION.GET_ALL
87      */
88     protected AICommonList itemList;
89     
90     /**
91      * inVocabulary is the parent Authority for this context
92      */
93     protected String inAuthority;
94     protected String authorityRefNameBase;
95     
96     public AuthorityItemDocumentModelHandler(String authorityItemCommonSchemaName) {
97         this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
98     }
99
100     public String getInAuthority() {
101                 return inAuthority;
102         }
103
104         public void setInAuthority(String inAuthority) {
105                 this.inAuthority = inAuthority;
106         }
107
108     /** Subclasses may override this to customize the URI segment. */
109     public String getAuthorityServicePath(){
110         return getServiceContext().getServiceName().toLowerCase();    // Laramie20110510 CSPACE-3932
111     }
112
113     @Override
114     public String getUri(DocumentModel docModel) {
115         // Laramie20110510 CSPACE-3932
116         String authorityServicePath = getAuthorityServicePath();
117         return "/"+authorityServicePath+'/'+inAuthority+'/'+ AuthorityClient.ITEMS+'/'+getCsid(docModel);
118     }
119
120     public String getAuthorityRefNameBase(){
121         return this.authorityRefNameBase;
122     }
123
124     public void setAuthorityRefNameBase(String value){
125         this.authorityRefNameBase = value;
126     }
127
128     /* (non-Javadoc)
129      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
130      */
131     @Override
132     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
133         // first fill all the parts of the document
134         super.handleCreate(wrapDoc);            
135         handleInAuthority(wrapDoc.getWrappedObject());
136         // Uncomment once debugged and App layer is read to integrate
137         //handleDisplayNameAsShortIdentifier(wrapDoc.getWrappedObject(), authorityItemCommonSchemaName);
138         //updateRefnameForAuthorityItem(wrapDoc, authorityItemCommonSchemaName, getAuthorityRefNameBase());  //CSPACE-3178
139     }
140     
141     private void handleDisplayNameAsShortIdentifier(DocumentModel docModel, String schemaName) throws Exception {
142         String shortIdentifier = (String)docModel.getProperty(schemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
143         String displayName =     (String)docModel.getProperty(schemaName, AuthorityItemJAXBSchema.DISPLAY_NAME);
144         if (Tools.isEmpty(shortIdentifier) && Tools.notEmpty(displayName)){
145             String cookedShortIdentifier = Tools.squeeze(displayName)+'-'+Tools.now().toString();
146             docModel.setProperty(schemaName , AuthorityItemJAXBSchema.SHORT_IDENTIFIER, cookedShortIdentifier);
147         }
148     }
149
150     protected void updateRefnameForAuthorityItem(DocumentWrapper<DocumentModel> wrapDoc,
151             String schemaName,
152             String authorityRefBaseName) throws Exception {
153         DocumentModel docModel = wrapDoc.getWrappedObject();
154         String shortIdentifier = (String)docModel.getProperty(schemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
155         String displayName =     (String)docModel.getProperty(schemaName, AuthorityItemJAXBSchema.DISPLAY_NAME);
156         if (Tools.isEmpty(authorityRefBaseName)){
157                 throw new Exception("updateRefnameForAuthorityItem requires an authorityRefBaseName, but none was supplied.");
158         }
159         RefName.Authority authority = RefName.Authority.parse(authorityRefBaseName);
160         String refName = RefName.buildAuthorityItem(authority, shortIdentifier, displayName).toString();
161         docModel.setProperty(schemaName , AuthorityItemJAXBSchema.REF_NAME, refName);
162     }
163
164     /**
165      * Check the logic around the parent pointer. Note that we only need do this on
166      * create, since we have logic to make this read-only on update. 
167      * 
168      * @param docModel
169      * 
170      * @throws Exception the exception
171      */
172     private void handleInAuthority(DocumentModel docModel) throws Exception {
173         docModel.setProperty(authorityItemCommonSchemaName, 
174                         AuthorityItemJAXBSchema.IN_AUTHORITY, inAuthority);
175     }
176
177
178     /**
179      * getCommonPart get associated item
180      * @return
181      */
182     @Override
183     public AICommon getCommonPart() {
184         return item;
185     }
186
187     @Override
188     public void setCommonPart(AICommon item) {
189         this.item = item;
190     }
191
192     /**
193      * getCommonPartList get associated item (for index/GET_ALL)
194      * @return
195      */
196     @Override
197     public AICommonList getCommonPartList() {
198         return itemList;
199     }
200
201     @Override
202     public void setCommonPartList(AICommonList itemList) {
203         this.itemList = itemList;
204     }
205
206     @Override
207     public AICommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
208             throws Exception {
209         throw new UnsupportedOperationException();
210     }
211
212     @Override
213     public void fillCommonPart(AICommon itemObject, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
214         throw new UnsupportedOperationException();
215     }
216     
217     /* (non-Javadoc)
218      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
219      */
220     @Override
221     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
222             throws Exception {
223         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
224         
225         // Add the CSID to the common part
226         if (partMeta.getLabel().equalsIgnoreCase(authorityItemCommonSchemaName)) {
227                 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
228                 unQObjectProperties.put("csid", csid);
229         }
230         
231         return unQObjectProperties;
232     }
233     
234     /**
235      * Filters out AuthorityItemJAXBSchema.IN_AUTHORITY, to ensure that
236      * the parent link remains untouched.
237      * @param objectProps the properties parsed from the update payload
238      * @param partMeta metadata for the object to fill
239      */
240     @Override
241     public void filterReadOnlyPropertiesForPart(
242                 Map<String, Object> objectProps, ObjectPartType partMeta) {
243         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
244         objectProps.remove(AuthorityItemJAXBSchema.IN_AUTHORITY);
245         objectProps.remove(AuthorityItemJAXBSchema.CSID);
246     }
247
248     @Override
249     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
250         MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
251         super.extractAllParts(wrapDoc);
252
253         String showSiblings = ctx.getQueryParams().getFirst(CommonAPI.showSiblings_QP);
254         if (Tools.isTrue(showSiblings)) {
255             showSiblings(wrapDoc, ctx);
256             return;   // actual result is returned on ctx.addOutputPart();
257         }
258
259         String showRelations = ctx.getQueryParams().getFirst(CommonAPI.showRelations_QP);
260         if (Tools.isTrue(showRelations)) {
261             showRelations(wrapDoc, ctx);
262             return;   // actual result is returned on ctx.addOutputPart();
263         }
264
265         String showAllRelations = ctx.getQueryParams().getFirst(CommonAPI.showAllRelations_QP);
266         if (Tools.isTrue(showAllRelations)) {
267             showAllRelations(wrapDoc, ctx);
268             return;   // actual result is returned on ctx.addOutputPart();
269         }
270     }
271
272     /** @return null on parent not found
273      */
274     protected String getParentCSID(String thisCSID) throws Exception {
275         String parentCSID = null;
276         try {
277             String predicate = RelationshipType.HAS_BROADER.value();
278             RelationsCommonList parentListOuter = getRelations(thisCSID, null, predicate);
279             List<RelationsCommonList.RelationListItem> parentList = parentListOuter.getRelationListItem();
280             if (parentList != null) {
281                 RelationsCommonList.RelationListItem relationListItem = parentList.get(0);
282                 parentCSID = relationListItem.getObjectCsid();
283             }
284             return parentCSID;
285         } catch (Exception e) {
286             logger.error("Could not find parent for this: "+thisCSID, e);
287             return null;
288         }
289     }
290
291     public void showRelations(DocumentWrapper<DocumentModel> wrapDoc,
292                                               MultipartServiceContext ctx)   throws Exception {
293         String thisCSID = NuxeoUtils.getCsid(wrapDoc.getWrappedObject());
294
295          String predicate = RelationshipType.HAS_BROADER.value();
296          RelationsCommonList parentListOuter = getRelations(thisCSID, null, predicate);
297          List<RelationsCommonList.RelationListItem> parentList = parentListOuter.getRelationListItem();
298
299          RelationsCommonList childrenListOuter = getRelations(null, thisCSID, predicate);
300          List<RelationsCommonList.RelationListItem> childrenList = childrenListOuter.getRelationListItem();
301
302          //Assume that there are more children than parents.  Will be true for parent/child, but maybe not for other relations.
303          //Now add all parents to our childrenList, to be able to return just one list of consolidated results.
304          //Not optimal, but that's the current design spec.
305         long added = 0;
306         for (RelationsCommonList.RelationListItem parent : parentList) {
307              childrenList.add(parent);
308              added++;
309         }
310         long childrenSize = childrenList.size();
311         childrenListOuter.setTotalItems(childrenSize);
312         childrenListOuter.setItemsInPage(childrenListOuter.getItemsInPage()+added);
313
314         PayloadOutputPart relationsPart = new PayloadOutputPart(RelationClient.SERVICE_COMMON_LIST_NAME, childrenListOuter);
315         ctx.addOutputPart(relationsPart);
316     }
317
318     public void showSiblings(DocumentWrapper<DocumentModel> wrapDoc,
319                                               MultipartServiceContext ctx)   throws Exception {
320         String thisCSID = NuxeoUtils.getCsid(wrapDoc.getWrappedObject());
321          String parentCSID = getParentCSID(thisCSID);
322         if (parentCSID == null){
323             logger.warn("~~~~~\r\n~~~~ Could not find parent for this: "+thisCSID);
324             return;
325         }
326
327          String predicate = RelationshipType.HAS_BROADER.value();
328          RelationsCommonList siblingListOuter = getRelations(null, parentCSID, predicate);
329          List<RelationsCommonList.RelationListItem> siblingList = siblingListOuter.getRelationListItem();
330
331         List<RelationsCommonList.RelationListItem> toRemoveList = newList();
332
333
334         RelationsCommonList.RelationListItem item = null;
335         for (RelationsCommonList.RelationListItem sibling : siblingList) {
336             if (thisCSID.equals(sibling.getSubjectCsid())){
337                 toRemoveList.add(sibling);   //IS_A copy of the main item, i.e. I have a parent that is my parent, so I'm in the list from the above query.
338             }
339         }
340         //rather than create an immutable iterator, I'm just putting the items to remove on a separate list, then looping over that list and removing.
341         for (RelationsCommonList.RelationListItem self : toRemoveList) {
342             removeFromList(siblingList, self);
343         }
344
345         long siblingSize = siblingList.size();
346         siblingListOuter.setTotalItems(siblingSize);
347         siblingListOuter.setItemsInPage(siblingSize);
348
349         PayloadOutputPart relationsPart = new PayloadOutputPart(RelationClient.SERVICE_COMMON_LIST_NAME,siblingListOuter);
350         ctx.addOutputPart(relationsPart);
351     }
352
353     public void showAllRelations(DocumentWrapper<DocumentModel> wrapDoc, MultipartServiceContext ctx)   throws Exception {
354         String thisCSID = NuxeoUtils.getCsid(wrapDoc.getWrappedObject());
355
356         RelationsCommonList subjectListOuter = getRelations(thisCSID, null, null);   //  nulls are wildcards:  predicate=*, and object=*
357         List<RelationsCommonList.RelationListItem> subjectList = subjectListOuter.getRelationListItem();
358
359         RelationsCommonList objectListOuter = getRelations(null, thisCSID, null);   //  nulls are wildcards:  subject=*, and predicate=*
360         List<RelationsCommonList.RelationListItem> objectList = objectListOuter.getRelationListItem();
361
362         //  MERGE LISTS:
363         subjectList.addAll(objectList);
364
365         //now subjectList actually has records BOTH where thisCSID is subject and object.
366         long relatedSize = subjectList.size();
367         subjectListOuter.setTotalItems(relatedSize);
368         subjectListOuter.setItemsInPage(relatedSize);
369
370         PayloadOutputPart relationsPart = new PayloadOutputPart(RelationClient.SERVICE_COMMON_LIST_NAME,subjectListOuter);
371         ctx.addOutputPart(relationsPart);
372     }
373
374     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
375         super.fillAllParts(wrapDoc, action);
376         ServiceContext ctx = getServiceContext();
377         PoxPayloadIn input = (PoxPayloadIn)ctx.getInput();
378         DocumentModel documentModel = (wrapDoc.getWrappedObject());
379         String itemCsid = documentModel.getName();
380
381         //UPDATE and CREATE will call.   Updates relations part
382         RelationsCommonList relationsCommonList = updateRelations(itemCsid, input, wrapDoc);
383
384         PayloadOutputPart payloadOutputPart = new PayloadOutputPart(RelationClient.SERVICE_COMMON_LIST_NAME, relationsCommonList);
385         ctx.setProperty(RelationClient.SERVICE_COMMON_LIST_NAME, payloadOutputPart);
386     }
387
388     public void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
389         super.completeUpdate(wrapDoc);
390         //now we add part for relations list
391         ServiceContext ctx = getServiceContext();
392         PayloadOutputPart foo = (PayloadOutputPart)ctx.getProperty(RelationClient.SERVICE_COMMON_LIST_NAME);
393         ((PoxPayloadOut)ctx.getOutput()).addPart(foo);
394     }
395
396     public RelationsCommonList updateRelations(String itemCSID, PoxPayloadIn input, DocumentWrapper<DocumentModel> wrapDoc)
397      throws Exception {
398         PayloadInputPart part = input.getPart(RelationClient.SERVICE_COMMON_LIST_NAME);        //input.getPart("relations_common");
399         if (part == null) {
400             return null;  //nothing to do--they didn't send a list of relations.
401         }
402         RelationsCommonList relationsCommonListBody = (RelationsCommonList) part.getBody();
403
404         ServiceContext ctx = getServiceContext();
405         UriInfo uriInfo = ctx.getUriInfo();
406         MultivaluedMap queryParams = uriInfo.getQueryParameters();
407
408         String predicate = RelationshipType.HAS_BROADER.value();
409         queryParams.putSingle(IRelationsManager.PREDICATE_QP, predicate);
410         queryParams.putSingle(IRelationsManager.SUBJECT_QP, null);
411         queryParams.putSingle(IRelationsManager.SUBJECT_TYPE_QP, null);
412         queryParams.putSingle(IRelationsManager.OBJECT_QP, itemCSID);
413         queryParams.putSingle(IRelationsManager.OBJECT_TYPE_QP, null);
414
415         RelationsCommonList childListOuter = (new RelationResource()).getList(ctx.getUriInfo());    //magically knows all query params because they are in the context.
416
417         //Leave predicate, swap subject and object.
418         queryParams.putSingle(IRelationsManager.PREDICATE_QP, predicate);
419         queryParams.putSingle(IRelationsManager.SUBJECT_QP, itemCSID);
420         queryParams.putSingle(IRelationsManager.OBJECT_QP, null);
421
422         RelationsCommonList parentListOuter = (new RelationResource()).getList(ctx.getUriInfo());
423         /*
424             go through inboundList, remove anything from childList that matches  from childList
425             go through inboundList, remove anything from parentList that matches  from parentList
426             go through parentList, delete all remaining
427             go through childList, delete all remaining
428             go through actionList, add all remaining.
429             check for duplicate children
430             check for more than one parent.
431
432         inboundList                           parentList                      childList          actionList
433         ----------------                          ---------------                  ----------------       ----------------
434         child-a                                   parent-c                        child-a             child-b
435         child-b                                   parent-d                        child-c
436          parent-a
437            */
438         String HAS_BROADER = RelationshipType.HAS_BROADER.value();
439
440         List<RelationsCommonList.RelationListItem> inboundList = relationsCommonListBody.getRelationListItem();
441         List<RelationsCommonList.RelationListItem> actionList = newList();
442         List<RelationsCommonList.RelationListItem> childList = childListOuter.getRelationListItem();
443         List<RelationsCommonList.RelationListItem> parentList = parentListOuter.getRelationListItem();
444
445         DocumentModel docModel = wrapDoc.getWrappedObject();
446
447         for (RelationsCommonList.RelationListItem inboundItem : inboundList) {
448             if (inboundItem.getObject().getCsid().equalsIgnoreCase(CommonAPI.AuthorityItemCSID_REPLACE)){
449                 inboundItem.setObjectCsid(itemCSID);
450                 inboundItem.getObject().setCsid(itemCSID);
451                 inboundItem.getObject().setUri(getUri(docModel));
452             }
453             if (inboundItem.getSubject().getCsid().equalsIgnoreCase(CommonAPI.AuthorityItemCSID_REPLACE)){
454                 inboundItem.setSubjectCsid(itemCSID);
455                 inboundItem.getSubject().setCsid(itemCSID);
456                  inboundItem.getSubject().setUri(getUri(docModel));
457             }
458             if (inboundItem.getObject().getCsid().equals(itemCSID) && inboundItem.getPredicate().equals(HAS_BROADER)) {
459                 //then this is an item that says we have a child.
460                 RelationsCommonList.RelationListItem childItem = findInList(childList, inboundItem);
461                 if (childItem != null){
462                     removeFromList(childList,  childItem);    //exists, just take it off delete list
463                 } else {
464                     actionList.add(inboundItem);   //doesn't exist as a child, but is a child.  Add to additions list
465                 }
466             } else if  (inboundItem.getSubject().getCsid().equals(itemCSID) && inboundItem.getPredicate().equals(HAS_BROADER)) {
467                 //then this is an item that says we have a parent
468                 RelationsCommonList.RelationListItem parentItem = findInList(parentList, inboundItem);
469                 if (parentItem != null){
470                     removeFromList(parentList,  parentItem);    //exists, just take it off delete list
471                 } else {
472                     actionList.add(inboundItem);   //doesn't exist as a parent, but is a parent. Add to additions list
473                 }
474             }  else {
475
476                 System.out.println("\r\n\r\n================\r\n    Element didn't match parent or child, but may have partial fields that match. inboundItem: "+inboundItem);
477                 //not dealing with: hasNarrower or any other predicate.
478             }
479         }
480         deleteRelations(parentList, ctx);               //todo: there are items appearing on both lists....april 20.
481         deleteRelations(childList, ctx);
482         createRelations(actionList, ctx);
483         //We return all elements on the inbound list, since we have just worked to make them exist in the system
484         // and be non-redundant, etc.  That list came from relationsCommonListBody, so it is still attached to it, just pass that back.
485         return relationsCommonListBody;
486     }
487
488     // this method calls the RelationResource to have it create the relations and persist them.
489     private void createRelations(List<RelationsCommonList.RelationListItem> inboundList, ServiceContext ctx){
490          for (RelationsCommonList.RelationListItem item : inboundList) {
491              RelationsCommon rc = new RelationsCommon();
492              //rc.setCsid(item.getCsid());
493              //todo: assignTo(item, rc);
494              RelationsDocListItem itemSubject = item.getSubject();
495              RelationsDocListItem itemObject = item.getObject();
496
497              String subjectCsid =  itemSubject.getCsid();
498              rc.setDocumentId1(subjectCsid);
499              rc.setSubjectCsid(subjectCsid);
500
501              String objCsid = item.getObject().getCsid();
502              rc.setDocumentId2(objCsid);
503              rc.setObjectCsid(objCsid);
504
505              rc.setRelationshipType(item.getPredicate());
506              //RelationshipType  foo = (RelationshipType.valueOf(item.getPredicate())) ;
507              //rc.setPredicate(foo);     //this must be one of the type found in the enum in  services/jaxb/src/main/resources/relations_common.xsd
508
509              rc.setDocumentType1(itemSubject.getDocumentType());
510              rc.setDocumentType2(itemObject.getDocumentType());
511
512              rc.setSubjectUri(itemSubject.getUri());
513              rc.setObjectUri(itemObject.getUri());
514
515
516             PoxPayloadOut payloadOut = new PoxPayloadOut(RelationClient.SERVICE_PAYLOAD_NAME);
517             PayloadOutputPart outputPart = new PayloadOutputPart(RelationClient.SERVICE_COMMONPART_NAME, rc);
518             payloadOut.addPart(outputPart);
519             //System.out.println("\r\n==== TO CREATE: "+rc.getDocumentId1()+"==>"+rc.getPredicate()+"==>"+rc.getDocumentId2());
520             RelationResource relationResource = new RelationResource();
521             Object res = relationResource.create(ctx.getUriInfo(), payloadOut.toXML());    //NOTE ui recycled from above to pass in unknown query params.
522         }
523     }
524      private void deleteRelations(List<RelationsCommonList.RelationListItem> list,ServiceContext ctx){
525           try {
526               for (RelationsCommonList.RelationListItem inboundItem : list) {
527                   RelationResource relationResource = new RelationResource();
528                   //System.out.println("\r\n==== TO DELETE: "+inboundItem.getCsid());
529                   Object res = relationResource.delete(inboundItem.getCsid());
530               }
531           } catch (Throwable t){
532               String msg = "Unable to deleteRelations: "+ Tools.errorToString(t, true);
533               logger.error(msg);
534           }
535      }
536
537     private  List<RelationsCommonList.RelationListItem> newList(){
538         List<RelationsCommonList.RelationListItem> result = new ArrayList<RelationsCommonList.RelationListItem>();
539         return result;
540     }
541      protected List<RelationsCommonList.RelationListItem> cloneList(List<RelationsCommonList.RelationListItem> inboundList){
542         List<RelationsCommonList.RelationListItem> result = newList();
543         for (RelationsCommonList.RelationListItem item: inboundList){
544             result.add(item);
545         }
546         return result;
547     }
548      private RelationsCommonList.RelationListItem findInList(List<RelationsCommonList.RelationListItem> list, RelationsCommonList.RelationListItem item){
549          for (RelationsCommonList.RelationListItem listItem : list) {
550              if (itemsEqual(listItem, item)){   //equals must be defined, else
551                 return listItem;
552              }
553          }
554          return null;
555      }
556
557     private boolean itemsEqual(RelationsCommonList.RelationListItem item, RelationsCommonList.RelationListItem item2){
558         if (item==null || item2==null){
559             return false;
560         }
561         RelationsDocListItem subj1 = item.getSubject();
562         RelationsDocListItem subj2 = item2.getSubject();
563         RelationsDocListItem obj1 = item.getObject();
564         RelationsDocListItem obj2 = item2.getObject();
565
566         return     (subj1.getCsid().equals(subj2.getCsid()))
567                 && (obj1.getCsid().equals(obj1.getCsid()))
568                 && ( (item.getPredicate().equals(item2.getPredicate()))
569                 && (item.getRelationshipType().equals(item2.getRelationshipType()))   )
570                 && (obj1.getDocumentType().equals(obj2.getDocumentType()))
571                 && (subj1.getDocumentType().equals(subj2.getDocumentType())) ;
572     }
573
574      private void removeFromList(List<RelationsCommonList.RelationListItem> list, RelationsCommonList.RelationListItem item){
575         list.remove(item);
576     }
577     //================= TODO: move this to common, refactoring this and  CollectionObjectResource.java
578
579     public RelationsCommonList getRelations(String subjectCSID, String objectCSID, String predicate) throws Exception {
580         ServiceContext ctx = getServiceContext();
581         MultivaluedMap queryParams = ctx.getQueryParams();
582         queryParams.putSingle(IRelationsManager.PREDICATE_QP, predicate);
583         queryParams.putSingle(IRelationsManager.SUBJECT_QP, subjectCSID);
584         queryParams.putSingle(IRelationsManager.OBJECT_QP, objectCSID);
585
586         RelationResource relationResource = new RelationResource();
587         RelationsCommonList relationsCommonList = relationResource.getList(ctx.getUriInfo());
588         return relationsCommonList;
589     }
590
591     //============================= END refactor ==========================
592
593 }
594