KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > CMLUtil


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.webservice;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.alfresco.repo.webservice.repository.UpdateResult;
26 import org.alfresco.repo.webservice.types.CML;
27 import org.alfresco.repo.webservice.types.CMLAddAspect;
28 import org.alfresco.repo.webservice.types.CMLAddChild;
29 import org.alfresco.repo.webservice.types.CMLCopy;
30 import org.alfresco.repo.webservice.types.CMLCreate;
31 import org.alfresco.repo.webservice.types.CMLCreateAssociation;
32 import org.alfresco.repo.webservice.types.CMLDelete;
33 import org.alfresco.repo.webservice.types.CMLMove;
34 import org.alfresco.repo.webservice.types.CMLRemoveAspect;
35 import org.alfresco.repo.webservice.types.CMLRemoveAssociation;
36 import org.alfresco.repo.webservice.types.CMLRemoveChild;
37 import org.alfresco.repo.webservice.types.CMLUpdate;
38 import org.alfresco.repo.webservice.types.NamedValue;
39 import org.alfresco.repo.webservice.types.ParentReference;
40 import org.alfresco.repo.webservice.types.Predicate;
41 import org.alfresco.repo.webservice.types.Reference;
42 import org.alfresco.service.cmr.repository.CopyService;
43 import org.alfresco.service.cmr.repository.NodeRef;
44 import org.alfresco.service.cmr.repository.NodeService;
45 import org.alfresco.service.cmr.search.SearchService;
46 import org.alfresco.service.namespace.NamespaceService;
47 import org.alfresco.service.namespace.QName;
48 import org.alfresco.util.PropertyMap;
49
50 /**
51  * @author Roy Wetherall
52  */

53 public class CMLUtil
54 {
55     private static final String JavaDoc CREATE = "create";
56     private static final String JavaDoc ADD_ASPECT = "addAspect";
57     private static final String JavaDoc REMOVE_ASPECT = "removeAspect";
58     private static final String JavaDoc UPDATE = "update";
59     private static final String JavaDoc DELETE = "delete";
60     private static final String JavaDoc MOVE = "move";
61     private static final String JavaDoc COPY = "copy";
62     private static final String JavaDoc ADD_CHILD = "addChild";
63     private static final String JavaDoc REMOVE_CHILD = "removeChild";
64     private static final String JavaDoc CREATE_ASSOCIATION = "createAssociation";
65     private static final String JavaDoc REMOVE_ASSOCIATION = "removeAssociation";
66     
67     private NodeService nodeService;
68     private SearchService searchService;
69     private NamespaceService namespaceService;
70     private CopyService copyService;
71     
72     public void setNodeService(NodeService nodeService)
73     {
74         this.nodeService = nodeService;
75     }
76     
77     public void setSearchService(SearchService searchService)
78     {
79         this.searchService = searchService;
80     }
81     
82     public void setNamespaceService(NamespaceService namespaceService)
83     {
84         this.namespaceService = namespaceService;
85     }
86     
87     public void setCopyService(CopyService copyService)
88     {
89         this.copyService = copyService;
90     }
91     
92     /**
93      * Execute a cml update query.
94      *
95      * @param cml the cml objects
96      * @return the update result
97      */

98     public UpdateResult[] executeCML(CML cml)
99     {
100         ExecutionContext context = new ExecutionContext();
101         List JavaDoc<UpdateResult> results = new ArrayList JavaDoc<UpdateResult>();
102         
103         // Execute creates
104
CMLCreate[] creates = cml.getCreate();
105         if (creates != null)
106         {
107             for (CMLCreate create : creates)
108             {
109                 executeCMLCreate(create, context, results);
110             }
111         }
112         
113         // Exceute add aspect
114
CMLAddAspect[] addAspects = cml.getAddAspect();
115         if (addAspects != null)
116         {
117             for (CMLAddAspect addAspect : addAspects)
118             {
119                 executeCMLAddAspect(addAspect, context, results);
120             }
121         }
122         
123         // Execeute remove aspect
124
CMLRemoveAspect[] removeAspects = cml.getRemoveAspect();
125         if (removeAspects != null)
126         {
127             for (CMLRemoveAspect removeAspect : removeAspects)
128             {
129                 executeCMLRemoveAspect(removeAspect, context, results);
130             }
131         }
132         
133         // Execute update
134
CMLUpdate[] updates = cml.getUpdate();
135         if (updates != null)
136         {
137             for (CMLUpdate update : updates)
138             {
139                 executeCMLUpdate(update, context, results);
140             }
141         }
142         
143         // Execute delete
144
CMLDelete[] deletes = cml.getDelete();
145         if (deletes != null)
146         {
147             for (CMLDelete delete : deletes)
148             {
149                 executeCMLDelete(delete, context, results);
150             }
151         }
152         
153         // Execute move
154
CMLMove[] moves = cml.getMove();
155         if (moves != null)
156         {
157             for (CMLMove move : moves)
158             {
159                 executeCMLMove(move, context, results);
160             }
161         }
162         
163         // Execute copy
164
CMLCopy[] copies = cml.getCopy();
165         if (copies != null)
166         {
167             for (CMLCopy copy : copies)
168             {
169                 executeCMLCopy(copy, context, results);
170             }
171         }
172         
173         // Execute addChild
174
CMLAddChild[] addChildren = cml.getAddChild();
175         if (addChildren != null)
176         {
177             for (CMLAddChild addChild : addChildren)
178             {
179                 executeCMLAddChild(addChild, context, results);
180             }
181         }
182         
183         // Execute removeChild
184
CMLRemoveChild[] removeChildren = cml.getRemoveChild();
185         if (removeChildren != null)
186         {
187             for (CMLRemoveChild removeChild : removeChildren)
188             {
189                 executeCMLRemoveChild(removeChild, context, results);
190             }
191         }
192         
193         // Execute createAssociation
194
CMLCreateAssociation[] createAssocs = cml.getCreateAssociation();
195         if (createAssocs != null)
196         {
197             for (CMLCreateAssociation createAssoc : createAssocs)
198             {
199                 executeCMLCreateAssociation(createAssoc, context, results);
200             }
201         }
202         
203         // Execute removeAssociation
204
CMLRemoveAssociation[] removeAssocs = cml.getRemoveAssociation();
205         if (removeAssocs != null)
206         {
207             for (CMLRemoveAssociation removeAssoc : removeAssocs)
208             {
209                 executeCMLRemoveAssociation(removeAssoc, context, results);
210             }
211         }
212         
213         return results.toArray(new UpdateResult[results.size()]);
214     }
215
216     /**
217      *
218      * @param create
219      * @param result
220      */

221     private void executeCMLCreate(CMLCreate create, ExecutionContext context, List JavaDoc<UpdateResult> results)
222     {
223         // Get the detail of the parent
224
ParentReference parentReference = create.getParent();
225         NodeRef parentNodeRef = Utils.convertToNodeRef(
226                                             parentReference,
227                                             this.nodeService,
228                                             this.searchService,
229                                             this.namespaceService);
230         QName assocTypeQName = QName.createQName(parentReference.getAssociationType());
231         QName assocQName = QName.createQName(parentReference.getChildName());
232         
233         // Get the type of the node to create
234
QName nodeTypeQName = QName.createQName(create.getType());
235         
236         // Get the properties
237
PropertyMap properties = getPropertyMap(create.getProperty());
238         
239         // Create the new node
240
NodeRef nodeRef = this.nodeService.createNode(parentNodeRef, assocTypeQName, assocQName, nodeTypeQName, properties).getChildRef();
241         
242         // Store the node ref in the execution context (if appropraite)
243
String JavaDoc id = create.getId();
244         if (id != null && id.length() != 0)
245         {
246             context.addId(id, nodeRef);
247         }
248
249         results.add(createResult(CREATE, null, nodeRef));
250     }
251     
252     private PropertyMap getPropertyMap(NamedValue[] namedValues)
253     {
254         PropertyMap properties = new PropertyMap();
255         if (namedValues != null)
256         {
257             for (NamedValue value : namedValues)
258             {
259                 QName qname = QName.createQName(value.getName());
260                 properties.put(qname, value.getValue());
261             }
262         }
263         return properties;
264     }
265     
266     private UpdateResult createResult(String JavaDoc cmd, NodeRef sourceNodeRef, NodeRef destinationNodeRef)
267     {
268         UpdateResult result = new UpdateResult();
269         result.setStatement(cmd);
270         if (sourceNodeRef != null)
271         {
272             result.setSource(Utils.convertToReference(sourceNodeRef));
273         }
274         if (destinationNodeRef != null)
275         {
276             result.setDestination(Utils.convertToReference(destinationNodeRef));
277         }
278         // Sort out the count ???
279
return result;
280     }
281     
282     /**
283      *
284      * @param addAspect
285      * @param result
286      */

287     private void executeCMLAddAspect(CMLAddAspect addAspect, ExecutionContext context, List JavaDoc<UpdateResult> results)
288     {
289         // Get the node refs
290
List JavaDoc<NodeRef> nodeRefs = getNodeRefList(addAspect.getWhere_id(), addAspect.getWhere(), context);
291         
292         // Get the aspect name and the properties
293
QName aspectQName = QName.createQName(addAspect.getAspect());
294         PropertyMap properties = getPropertyMap(addAspect.getProperty());
295                 
296         for (NodeRef nodeRef : nodeRefs)
297         {
298             // Add the aspect
299
this.nodeService.addAspect(nodeRef, aspectQName, properties);
300             
301             // Create the result
302
results.add(createResult(ADD_ASPECT, nodeRef, nodeRef));
303         }
304     }
305     
306     private void executeCMLRemoveAspect(CMLRemoveAspect removeAspect, ExecutionContext context, List JavaDoc<UpdateResult> results)
307     {
308         // Get the node refs
309
List JavaDoc<NodeRef> nodeRefs = getNodeRefList(removeAspect.getWhere_id(), removeAspect.getWhere(), context);
310         
311         // Get the aspect name
312
QName aspectQName = QName.createQName(removeAspect.getAspect());
313         
314         for (NodeRef nodeRef : nodeRefs)
315         {
316             // Add the aspect
317
this.nodeService.removeAspect(nodeRef, aspectQName);
318             
319             // Create the result
320
results.add(createResult(REMOVE_ASPECT, nodeRef, nodeRef));
321         }
322     }
323     
324     private List JavaDoc<NodeRef> getNodeRefList(String JavaDoc id, Predicate predicate, ExecutionContext context)
325     {
326         List JavaDoc<NodeRef> nodeRefs = new ArrayList JavaDoc<NodeRef>();
327         if (id != null && id.length() != 0)
328         {
329             NodeRef localNodeRef = context.getNodeRef(id);
330             if (localNodeRef != null)
331             {
332                 nodeRefs.add(localNodeRef);
333             }
334         }
335         else
336         {
337             nodeRefs = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
338         }
339         return nodeRefs;
340     }
341     
342     private void executeCMLUpdate(CMLUpdate update, ExecutionContext context, List JavaDoc<UpdateResult> results)
343     {
344         // Get the nodes and properties
345
List JavaDoc<NodeRef> nodeRefs = getNodeRefList(update.getWhere_id(), update.getWhere(), context);
346         PropertyMap props = getPropertyMap(update.getProperty());
347         
348         for (NodeRef nodeRef : nodeRefs)
349         {
350             // Update the property values
351
Map JavaDoc<QName, Serializable JavaDoc> currentProps = this.nodeService.getProperties(nodeRef);
352             currentProps.putAll(props);
353             this.nodeService.setProperties(nodeRef, currentProps);
354             
355             // Get the result
356
results.add(createResult(UPDATE, nodeRef, nodeRef));
357         }
358     }
359     
360     private void executeCMLDelete(CMLDelete delete, ExecutionContext context, List JavaDoc<UpdateResult> results)
361     {
362         List JavaDoc<NodeRef> nodeRefs = Utils.resolvePredicate(delete.getWhere(), this.nodeService, this.searchService, this.namespaceService);
363         for (NodeRef nodeRef : nodeRefs)
364         {
365             // Delete the node
366
this.nodeService.deleteNode(nodeRef);
367             
368             // Create the result
369
results.add(createResult(DELETE, nodeRef, null));
370         }
371     }
372     
373     private void executeCMLMove(CMLMove move, ExecutionContext context, List JavaDoc<UpdateResult> results)
374     {
375         NodeRef destinationNodeRef = getNodeRef(move.getTo_id(), move.getTo(), context);
376         if (destinationNodeRef != null)
377         {
378             QName assocType = null;
379             QName assocName = null;
380             if (move.getTo_id() != null)
381             {
382                 assocType = QName.createQName(move.getAssociationType());
383                 assocName = QName.createQName(move.getChildName());
384             }
385             else
386             {
387                 assocType = QName.createQName(move.getTo().getAssociationType());
388                 assocName = QName.createQName(move.getTo().getChildName());
389             }
390             
391             List JavaDoc<NodeRef> nodesToMove = getNodeRefList(move.getWhere_id(), move.getWhere(), context);
392             for (NodeRef nodeToMove : nodesToMove)
393             {
394                 NodeRef newNodeRef = this.nodeService.moveNode(nodeToMove, destinationNodeRef, assocType, assocName).getChildRef();
395                 
396                 // Create the result
397
results.add(createResult(MOVE, nodeToMove, newNodeRef));
398             }
399         }
400     }
401     
402     private NodeRef getNodeRef(String JavaDoc id, ParentReference parentReference, ExecutionContext context)
403     {
404         NodeRef nodeRef = null;
405         if (id != null && id.length() != 0)
406         {
407             nodeRef = context.getNodeRef(id);
408         }
409         else
410         {
411             nodeRef = Utils.convertToNodeRef(parentReference, this.nodeService, this.searchService, this.namespaceService);
412         }
413         
414         return nodeRef;
415     }
416     
417     private NodeRef getNodeRef(String JavaDoc id, Reference reference, ExecutionContext context)
418     {
419         NodeRef nodeRef = null;
420         if (id != null && id.length() != 0)
421         {
422             nodeRef = context.getNodeRef(id);
423         }
424         else
425         {
426             nodeRef = Utils.convertToNodeRef(reference, this.nodeService, this.searchService, this.namespaceService);
427         }
428         
429         return nodeRef;
430     }
431     
432     private void executeCMLCopy(CMLCopy copy, ExecutionContext context, List JavaDoc<UpdateResult> results)
433     {
434         NodeRef destinationNodeRef = getNodeRef(copy.getTo_id(), copy.getTo(), context);
435         if (destinationNodeRef != null)
436         {
437             QName assocType = null;
438             QName assocName = null;
439             if (copy.getTo_id() != null)
440             {
441                 assocType = QName.createQName(copy.getAssociationType());
442                 assocName = QName.createQName(copy.getChildName());
443             }
444             else
445             {
446                 assocType = QName.createQName(copy.getTo().getAssociationType());
447                 assocName = QName.createQName(copy.getTo().getChildName());
448             }
449             
450             boolean copyChildren = false;
451             Boolean JavaDoc value = copy.getChildren();
452             if (value != null)
453             {
454                 copyChildren = value.booleanValue();
455             }
456             
457             List JavaDoc<NodeRef> nodesToCopy = getNodeRefList(copy.getWhere_id(), copy.getWhere(), context);
458             for (NodeRef nodeToCopy : nodesToCopy)
459             {
460                 NodeRef newNodeRef = this.copyService.copy(nodeToCopy, destinationNodeRef, assocType, assocName, copyChildren);
461                 
462                 // Create the result
463
results.add(createResult(COPY, nodeToCopy, newNodeRef));
464             }
465         }
466         
467     }
468
469     private void executeCMLAddChild(CMLAddChild addChild, ExecutionContext context, List JavaDoc<UpdateResult> results)
470     {
471         NodeRef nodeRef = getNodeRef(addChild.getTo_id(), addChild.getTo(), context);
472         if (nodeRef != null)
473         {
474             QName assocType = null;
475             QName assocName = null;
476             if (addChild.getTo_id() != null)
477             {
478                 assocType = QName.createQName(addChild.getAssociationType());
479                 assocName = QName.createQName(addChild.getChildName());
480             }
481             else
482             {
483                 assocType = QName.createQName(addChild.getTo().getAssociationType());
484                 assocName = QName.createQName(addChild.getTo().getChildName());
485             }
486             
487             List JavaDoc<NodeRef> whereNodeRefs = getNodeRefList(addChild.getWhere_id(), addChild.getWhere(), context);
488             for (NodeRef whereNodeRef : whereNodeRefs)
489             {
490                 this.nodeService.addChild(nodeRef, whereNodeRef, assocType, assocName);
491                 
492                 // Create the result
493
results.add(createResult(ADD_CHILD, nodeRef, whereNodeRef));
494             }
495         }
496     }
497     
498     private void executeCMLRemoveChild(CMLRemoveChild removeChild, ExecutionContext context, List JavaDoc<UpdateResult> results)
499     {
500         NodeRef parentNodeRef = getNodeRef(removeChild.getFrom_id(), removeChild.getFrom(), context);
501         if (parentNodeRef != null)
502         {
503             List JavaDoc<NodeRef> childNodeRefs = getNodeRefList(removeChild.getWhere_id(), removeChild.getWhere(), context);
504             for (NodeRef childNodeRef : childNodeRefs)
505             {
506                 this.nodeService.removeChild(parentNodeRef, childNodeRef);
507                 
508                 // Create the result
509
results.add(createResult(REMOVE_CHILD, parentNodeRef, null));
510             }
511         }
512         
513     }
514
515     private void executeCMLCreateAssociation(CMLCreateAssociation createAssoc, ExecutionContext context, List JavaDoc<UpdateResult> results)
516     {
517         QName assocType = QName.createQName(createAssoc.getAssociation());
518         if (assocType != null)
519         {
520             List JavaDoc<NodeRef> fromNodeRefs = getNodeRefList(createAssoc.getFrom_id(), createAssoc.getFrom(), context);
521             List JavaDoc<NodeRef> toNodeRefs = getNodeRefList(createAssoc.getTo_id(), createAssoc.getTo(), context);
522             for (NodeRef fromNodeRef : fromNodeRefs)
523             {
524                 for (NodeRef toNodeRef : toNodeRefs)
525                 {
526                     this.nodeService.createAssociation(fromNodeRef, toNodeRef, assocType);
527                     
528                     // Create the result
529
results.add(createResult(CREATE_ASSOCIATION, fromNodeRef, toNodeRef));
530                 }
531             }
532         }
533     }
534
535     private void executeCMLRemoveAssociation(CMLRemoveAssociation removeAssoc, ExecutionContext context, List JavaDoc<UpdateResult> results)
536     {
537         QName assocType = QName.createQName(removeAssoc.getAssociation());
538         if (assocType != null)
539         {
540             List JavaDoc<NodeRef> fromNodeRefs = getNodeRefList(removeAssoc.getFrom_id(), removeAssoc.getFrom(), context);
541             List JavaDoc<NodeRef> toNodeRefs = getNodeRefList(removeAssoc.getTo_id(), removeAssoc.getTo(), context);
542             for (NodeRef fromNodeRef : fromNodeRefs)
543             {
544                 for (NodeRef toNodeRef : toNodeRefs)
545                 {
546                     this.nodeService.removeAssociation(fromNodeRef, toNodeRef, assocType);
547                     
548                     // Create the result
549
results.add(createResult(REMOVE_ASSOCIATION, fromNodeRef, toNodeRef));
550                 }
551             }
552         }
553     }
554     
555     private class ExecutionContext
556     {
557         private Map JavaDoc<String JavaDoc, NodeRef> idMap = new HashMap JavaDoc<String JavaDoc, NodeRef>();
558         
559         public void addId(String JavaDoc id, NodeRef nodeRef)
560         {
561             this.idMap.put(id, nodeRef);
562         }
563         
564         public NodeRef getNodeRef(String JavaDoc id)
565         {
566             return this.idMap.get(id);
567         }
568     }
569 }
570
Popular Tags