KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > action > ActionWebService


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.action;
18
19 import java.io.Serializable JavaDoc;
20 import java.rmi.RemoteException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.alfresco.repo.action.ActionConditionImpl;
26 import org.alfresco.repo.action.ActionImpl;
27 import org.alfresco.repo.action.CompositeActionImpl;
28 import org.alfresco.repo.action.executer.CompositeActionExecuter;
29 import org.alfresco.repo.rule.RuleImpl;
30 import org.alfresco.repo.transaction.TransactionComponent;
31 import org.alfresco.repo.transaction.TransactionUtil;
32 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
33 import org.alfresco.repo.webservice.AbstractWebService;
34 import org.alfresco.repo.webservice.Utils;
35 import org.alfresco.repo.webservice.types.NamedValue;
36 import org.alfresco.repo.webservice.types.Predicate;
37 import org.alfresco.repo.webservice.types.Reference;
38 import org.alfresco.service.cmr.action.Action;
39 import org.alfresco.service.cmr.action.ActionCondition;
40 import org.alfresco.service.cmr.action.ActionConditionDefinition;
41 import org.alfresco.service.cmr.action.ActionDefinition;
42 import org.alfresco.service.cmr.action.ActionService;
43 import org.alfresco.service.cmr.action.CompositeAction;
44 import org.alfresco.service.cmr.action.ParameterDefinition;
45 import org.alfresco.service.cmr.action.ParameterizedItem;
46 import org.alfresco.service.cmr.action.ParameterizedItemDefinition;
47 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
48 import org.alfresco.service.cmr.dictionary.DictionaryService;
49 import org.alfresco.service.cmr.repository.NodeRef;
50 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
51 import org.alfresco.service.cmr.rule.Rule;
52 import org.alfresco.service.cmr.rule.RuleService;
53 import org.alfresco.service.cmr.rule.RuleType;
54 import org.alfresco.util.GUID;
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
57
58 /**
59  * Action web service implementation
60  *
61  * @author Roy Wetherall
62  */

63 public class ActionWebService extends AbstractWebService implements ActionServiceSoapPort
64 {
65     /** Log */
66     private static Log logger = LogFactory.getLog(ActionWebService.class);
67     
68     /** The action service */
69     private ActionService actionService;
70     
71     /** The rule service */
72     private RuleService ruleService;
73     
74     /** The dictionary service */
75     private DictionaryService dictionaryService;
76     
77     /** The transaction service */
78     private TransactionComponent transactionService;
79     
80     /**
81      * Set the action service
82      *
83      * @param actionService the action service
84      */

85     public void setActionService(ActionService actionService)
86     {
87         this.actionService = actionService;
88     }
89     
90     /**
91      * Set the rule service
92      *
93      * @param ruleService the rule service
94      */

95     public void setRuleService(RuleService ruleService)
96     {
97         this.ruleService = ruleService;
98     }
99     
100     /**
101      * Set the dictionary service
102      *
103      * @param dictionaryService the dictionary service
104      */

105     public void setDictionaryService(DictionaryService dictionaryService)
106     {
107         this.dictionaryService = dictionaryService;
108     }
109     
110     /**
111      * Sets the transaction service
112      *
113      * @param transactionService the transaction service
114      */

115     public void setTransactionService(TransactionComponent transactionService)
116     {
117         this.transactionService = transactionService;
118     }
119     
120     /**
121      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#getConditionDefinitions()
122      */

123     public ActionItemDefinition[] getConditionDefinitions() throws RemoteException JavaDoc,
124             ActionFault
125     {
126         try
127         {
128             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<ActionItemDefinition[]>()
129             {
130                 public ActionItemDefinition[] doWork() throws Exception JavaDoc
131                 {
132                     return getConditionDefintionsImpl();
133                 }
134             });
135         }
136         catch (Throwable JavaDoc exception)
137         {
138             if (logger.isDebugEnabled())
139             {
140                 logger.error("Unexpected error occurred", exception);
141             }
142             
143             throw new ActionFault(0, exception.getMessage());
144         }
145     }
146     
147     /**
148      *
149      * @return
150      * @throws RemoteException
151      */

152     private ActionItemDefinition[] getConditionDefintionsImpl() throws RemoteException JavaDoc
153     {
154         // Get the action condition defintions from the action service
155
List JavaDoc<ActionConditionDefinition> definitions = this.actionService.getActionConditionDefinitions();
156         
157         // Marshal the results into an array of action item types
158
ActionItemDefinition[] result = new ActionItemDefinition[definitions.size()];
159         int index = 0;
160         for (ActionConditionDefinition definition : definitions)
161         {
162             result[index] = convertToActionItemDefintion(definition, ActionItemDefinitionType.condition);
163             index++;
164         }
165         
166         return result;
167     }
168
169     /**
170      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#getActionDefinitions()
171      */

172     public ActionItemDefinition[] getActionDefinitions() throws RemoteException JavaDoc,
173             ActionFault
174     {
175         try
176         {
177             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<ActionItemDefinition[]>()
178             {
179                 public ActionItemDefinition[] doWork() throws Exception JavaDoc
180                 {
181                     return getActionDefinitionsImpl();
182                 }
183             });
184         }
185         catch (Throwable JavaDoc exception)
186         {
187             if (logger.isDebugEnabled())
188             {
189                 logger.error("Unexpected error occurred", exception);
190             }
191             
192             throw new ActionFault(0, exception.getMessage());
193         }
194     }
195
196     /**
197      *
198      * @return
199      * @throws RemoteException
200      */

201     private ActionItemDefinition[] getActionDefinitionsImpl() throws RemoteException JavaDoc
202     {
203         // Get the action defintions from the action service
204
List JavaDoc<ActionDefinition> definitions = this.actionService.getActionDefinitions();
205         
206         // Marshal the results into an array of action item types
207
ActionItemDefinition[] result = new ActionItemDefinition[definitions.size()];
208         int index = 0;
209         for (ActionDefinition definition : definitions)
210         {
211             result[index] = convertToActionItemDefintion(definition, ActionItemDefinitionType.action);
212             index++;
213         }
214         
215         return result;
216     }
217
218     /**
219      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#getActionItemDefinition(java.lang.String, org.alfresco.repo.webservice.action.ActionItemDefinitionType)
220      */

221     public ActionItemDefinition getActionItemDefinition(final String JavaDoc name, final ActionItemDefinitionType definitionType) throws RemoteException JavaDoc, ActionFault
222     {
223         try
224         {
225             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<ActionItemDefinition>()
226             {
227                 public ActionItemDefinition doWork() throws Exception JavaDoc
228                 {
229                     return getActionItemDefinitionImpl(name, definitionType);
230                 }
231             });
232         }
233         catch (Throwable JavaDoc exception)
234         {
235             if (logger.isDebugEnabled())
236             {
237                 logger.error("Unexpected error occurred", exception);
238             }
239             
240             throw new ActionFault(0, exception.getMessage());
241         }
242     }
243     
244     /**
245      *
246      * @param name
247      * @param definitionType
248      * @return
249      * @throws RemoteException
250      * @throws ActionFault
251      */

252     public ActionItemDefinition getActionItemDefinitionImpl(String JavaDoc name, ActionItemDefinitionType definitionType) throws RemoteException JavaDoc, ActionFault
253     {
254         ActionItemDefinition result = null;
255         
256         if (definitionType.equals(ActionItemDefinitionType.action) == true)
257         {
258             ActionDefinition actionDefinition = this.actionService.getActionDefinition(name);
259             if (actionDefinition != null)
260             {
261                 result = convertToActionItemDefintion(actionDefinition, definitionType);
262             }
263         }
264         else
265         {
266             ActionConditionDefinition conditionDefinition = this.actionService.getActionConditionDefinition(name);
267             if (conditionDefinition != null)
268             {
269                 result = convertToActionItemDefintion(conditionDefinition, definitionType);
270             }
271         }
272         
273         return result;
274     }
275
276     /**
277      * Marshal the parameterized item defintion into a action item defition object.
278      *
279      * @param definition
280      * @param type
281      * @return
282      */

283     private ActionItemDefinition convertToActionItemDefintion(ParameterizedItemDefinition definition, ActionItemDefinitionType type)
284     {
285         // Create action item defintion
286
ActionItemDefinition actionItemType = new ActionItemDefinition();
287         actionItemType.setName(definition.getName());
288         actionItemType.setType(type);
289         actionItemType.setTitle(definition.getTitle());
290         actionItemType.setDescription(definition.getDescription());
291         actionItemType.setAdHocPropertiesAllowed(definition.getAdhocPropertiesAllowed());
292         
293         // Marshal the paremeter definitions
294
List JavaDoc<ParameterDefinition> params = definition.getParameterDefinitions();
295         org.alfresco.repo.webservice.action.ParameterDefinition[] parameterDefinitions = new org.alfresco.repo.webservice.action.ParameterDefinition[params.size()];
296         int index = 0;
297         for (ParameterDefinition paramDef : params)
298         {
299             org.alfresco.repo.webservice.action.ParameterDefinition parameterDefinition = new org.alfresco.repo.webservice.action.ParameterDefinition(
300                     paramDef.getName(),
301                     paramDef.getType().toString(),
302                     paramDef.isMandatory(),
303                     paramDef.getDisplayLabel());
304             parameterDefinitions[index] = parameterDefinition;
305             index ++;
306         }
307         actionItemType.setParameterDefinition(parameterDefinitions);
308         
309         return actionItemType;
310     }
311
312     /**
313      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#getRuleTypes()
314      */

315     public org.alfresco.repo.webservice.action.RuleType[] getRuleTypes() throws RemoteException JavaDoc, ActionFault
316     {
317         try
318         {
319             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<org.alfresco.repo.webservice.action.RuleType[]>()
320             {
321                 public org.alfresco.repo.webservice.action.RuleType[] doWork() throws Exception JavaDoc
322                 {
323                     return getRuleTypesImpl();
324                 }
325             });
326         }
327         catch (Throwable JavaDoc exception)
328         {
329             if (logger.isDebugEnabled())
330             {
331                 logger.error("Unexpected error occurred", exception);
332             }
333             
334             throw new ActionFault(0, exception.getMessage());
335     }
336     }
337     
338     public org.alfresco.repo.webservice.action.RuleType[] getRuleTypesImpl() throws RemoteException JavaDoc, ActionFault
339     {
340         // Get the list of rule types
341
List JavaDoc<RuleType> ruleTypes = this.ruleService.getRuleTypes();
342         
343         // Marshal the rule types into an array
344
org.alfresco.repo.webservice.action.RuleType[] results = new org.alfresco.repo.webservice.action.RuleType[ruleTypes.size()];
345         int index = 0;
346         for (RuleType ruleType : ruleTypes)
347         {
348             org.alfresco.repo.webservice.action.RuleType webServiceRuleType = new org.alfresco.repo.webservice.action.RuleType(
349                     ruleType.getName(),
350                     ruleType.getDisplayLabel());
351             results[index] = webServiceRuleType;
352             index ++;
353         }
354         
355         return results;
356     }
357
358     /**
359      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#getRuleType(java.lang.String)
360      */

361     public org.alfresco.repo.webservice.action.RuleType getRuleType(final String JavaDoc name) throws RemoteException JavaDoc, ActionFault
362     {
363         try
364         {
365             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<org.alfresco.repo.webservice.action.RuleType>()
366             {
367                 public org.alfresco.repo.webservice.action.RuleType doWork() throws Exception JavaDoc
368                 {
369                     return getRuleTypeImpl(name);
370                 }
371             });
372         }
373         catch (Throwable JavaDoc exception)
374         {
375             if (logger.isDebugEnabled())
376             {
377                 logger.error("Unexpected error occurred", exception);
378             }
379             
380             throw new ActionFault(0, exception.getMessage());
381         }
382     }
383     
384     public org.alfresco.repo.webservice.action.RuleType getRuleTypeImpl(String JavaDoc name) throws RemoteException JavaDoc, ActionFault
385     {
386         org.alfresco.repo.webservice.action.RuleType result = null;
387         
388         RuleType ruleType = this.ruleService.getRuleType(name);
389         if (ruleType != null)
390         {
391             result = new org.alfresco.repo.webservice.action.RuleType(ruleType.getName(), ruleType.getDisplayLabel());
392         }
393         
394         return result;
395     }
396
397     /**
398      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#getActions(org.alfresco.repo.webservice.types.Reference, java.lang.String[])
399      */

400     public org.alfresco.repo.webservice.action.Action[] getActions(final Reference reference, final ActionFilter filter) throws RemoteException JavaDoc, ActionFault
401     {
402         try
403         {
404             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<org.alfresco.repo.webservice.action.Action[]>()
405             {
406                 public org.alfresco.repo.webservice.action.Action[] doWork() throws Exception JavaDoc
407                 {
408                     return getActionsImpl(reference, filter);
409                 }
410             });
411         }
412         catch (Throwable JavaDoc exception)
413         {
414             if (logger.isDebugEnabled())
415             {
416                 logger.error("Unexpected error occurred", exception);
417             }
418             
419             throw new ActionFault(0, exception.getMessage());
420         }
421     }
422     
423     private org.alfresco.repo.webservice.action.Action[] getActionsImpl(Reference reference, ActionFilter filter) throws RemoteException JavaDoc, ActionFault
424     {
425         // Get the actions
426
NodeRef nodeRef = Utils.convertToNodeRef(reference, this.nodeService, this.searchService, this.namespaceService);
427         List JavaDoc<Action> actions = this.actionService.getActions(nodeRef);
428         
429         org.alfresco.repo.webservice.action.Action[] webServiceActions = new org.alfresco.repo.webservice.action.Action[actions.size()];
430         
431         // Filter the results
432
if (filter != null)
433         {
434             // TODO implement the filters
435
}
436         
437         // Marshal the results
438
int index = 0;
439         for (Action action : actions)
440         {
441             webServiceActions[index] = convertToWebServiceAction(action);
442             index++;
443         }
444         
445         return webServiceActions;
446     }
447     
448     private org.alfresco.repo.webservice.action.Action convertToWebServiceAction(Action action)
449     {
450         // Get the parameters into a named value array
451
NamedValue[] namedValues = convertParametersToNamedValues(action);
452         
453         // Get the conditions
454
List JavaDoc<ActionCondition> conditions = action.getActionConditions();
455         Condition[] webServiceConditions = new Condition[conditions.size()];
456         int index2 = 0;
457         for (ActionCondition condition : conditions)
458         {
459             webServiceConditions[index2] = convertToWebServiceCondition(condition);
460             index2++;
461         }
462         
463         // Get the compenstaing action
464
Action compensatingAction = action.getCompensatingAction();
465         org.alfresco.repo.webservice.action.Action webServiceCompensatingAction = null;
466         if (compensatingAction != null)
467         {
468             webServiceCompensatingAction = convertToWebServiceAction(compensatingAction);
469         }
470         
471         // Sort out any sub-actions
472
org.alfresco.repo.webservice.action.Action[] childWebServiceActions = null;
473         if (action instanceof CompositeAction)
474         {
475             List JavaDoc<Action> childActions = ((CompositeAction)action).getActions();
476             childWebServiceActions = new org.alfresco.repo.webservice.action.Action[childActions.size()];
477             int index3 = 0;
478             for (Action childAction : childActions)
479             {
480                 childWebServiceActions[index3] = convertToWebServiceAction(childAction);
481                 index3 ++;
482             }
483         }
484         
485         // Get the reference to the 'owning' node
486
NodeRef owningNodeRef = action.getOwningNodeRef();
487         Reference reference = null;
488         if (owningNodeRef != null)
489         {
490             reference = Utils.convertToReference(owningNodeRef);
491         }
492         
493         // Create the web service action object
494
org.alfresco.repo.webservice.action.Action webServiceAction = new org.alfresco.repo.webservice.action.Action(
495                 action.getId(),
496                 action.getActionDefinitionName(),
497                 action.getTitle(),
498                 action.getDescription(),
499                 action.getExecuteAsychronously(),
500                 namedValues,
501                 webServiceConditions,
502                 webServiceCompensatingAction,
503                 childWebServiceActions,
504                 reference);
505         
506         return webServiceAction;
507     }
508
509     /**
510      *
511      * @param item
512      * @return
513      */

514     private NamedValue[] convertParametersToNamedValues(ParameterizedItem item)
515     {
516         NamedValue[] namedValues = null;
517         if (item != null)
518         {
519             Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = item.getParameterValues();
520             namedValues = new NamedValue[params.size()];
521             int index = 0;
522             for (Map.Entry JavaDoc<String JavaDoc, Serializable JavaDoc> entry : params.entrySet())
523             {
524                 String JavaDoc value = null;
525                 try
526                 {
527                     value = DefaultTypeConverter.INSTANCE.convert(String JavaDoc.class, entry.getValue());
528                 }
529                 catch (Throwable JavaDoc exception)
530                 {
531                     value = entry.getValue().toString();
532                 }
533                 namedValues[index] = new NamedValue(entry.getKey(), value);
534                 index++;
535             }
536         }
537         return namedValues;
538     }
539     
540     /**
541      *
542      * @param condition
543      * @return
544      */

545     private Condition convertToWebServiceCondition(ActionCondition condition)
546     {
547         // Get the parameter values as an array of names values
548
NamedValue[] namedValues = convertParametersToNamedValues(condition);
549         
550         Condition webServiceCondition = new Condition(
551                 condition.getId(),
552                 condition.getActionConditionDefinitionName(),
553                 condition.getInvertCondition(),
554                 namedValues);
555         
556         return webServiceCondition;
557     }
558
559     /**
560      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#saveActions(org.alfresco.repo.webservice.types.Reference, org.alfresco.repo.webservice.action.Action[])
561      */

562     public org.alfresco.repo.webservice.action.Action[] saveActions(
563             final Reference reference,
564             final org.alfresco.repo.webservice.action.Action[] webServiceActions) throws RemoteException JavaDoc, ActionFault
565     {
566         try
567         {
568             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<org.alfresco.repo.webservice.action.Action[]>()
569             {
570                 public org.alfresco.repo.webservice.action.Action[] doWork() throws Exception JavaDoc
571                 {
572                     return saveActionsImpl(reference, webServiceActions);
573                 }
574             });
575         }
576         catch (Throwable JavaDoc exception)
577         {
578             if (logger.isDebugEnabled())
579             {
580                 logger.error("Unexpected error occurred", exception);
581             }
582             
583             throw new ActionFault(0, exception.getMessage());
584         }
585     }
586     
587     /**
588      *
589      * @param reference
590      * @param webServiceActions
591      * @return
592      * @throws RemoteException
593      * @throws ActionFault
594      */

595     private org.alfresco.repo.webservice.action.Action[] saveActionsImpl(
596             Reference reference,
597             org.alfresco.repo.webservice.action.Action[] webServiceActions) throws RemoteException JavaDoc, ActionFault
598     {
599         // Get the node reference
600
NodeRef nodeRef = Utils.convertToNodeRef(reference, this.nodeService, this.searchService, this.namespaceService);
601         
602         // Create the result array
603
org.alfresco.repo.webservice.action.Action[] results = new org.alfresco.repo.webservice.action.Action[webServiceActions.length];
604         
605         int index = 0;
606         for (org.alfresco.repo.webservice.action.Action webServiceAction : webServiceActions)
607         {
608             // Convert to a server action object
609
Action action = convertToAction(webServiceAction);
610             
611             // Save the action
612
this.actionService.saveAction(nodeRef, action);
613             
614             // Add the updated action to the results
615
results[index] = convertToWebServiceAction(action);
616             index++;
617         }
618         
619         return results;
620     }
621     
622     /**
623      * Convert a web service action object into a repository action object.
624      *
625      * @param webServiceAction the web service action object
626      * @return the repository action object
627      */

628     private Action convertToAction(org.alfresco.repo.webservice.action.Action webServiceAction)
629     {
630         // If the id is null then generate one
631
String JavaDoc id = webServiceAction.getId();
632         if (id == null || id.length() == 0)
633         {
634             id = GUID.generate();
635         }
636         
637         // Get the owning node ref
638
NodeRef owningNodeRef = null;
639         if (webServiceAction.getReference() != null)
640         {
641             owningNodeRef = Utils.convertToNodeRef(
642                 webServiceAction.getReference(),
643                 this.nodeService,
644                 this.searchService,
645                 this.namespaceService);
646         }
647         
648         // Create the action (or composite action)
649
ActionImpl action = null;
650         String JavaDoc actionDefinitionName = webServiceAction.getActionName();
651         if (CompositeActionExecuter.NAME.equals(actionDefinitionName) == true)
652         {
653             action = new CompositeActionImpl(id, owningNodeRef);
654         }
655         else
656         {
657             action = new ActionImpl(id, actionDefinitionName, owningNodeRef);
658         }
659         
660         // Set some of the action's details
661
action.setTitle(webServiceAction.getTitle());
662         action.setDescription(webServiceAction.getDescription());
663         if (webServiceAction.isExecuteAsynchronously() == true)
664         {
665             action.setExecuteAsynchronously(true);
666         }
667         
668         // Set the parameters
669
NamedValue[] namedValues = webServiceAction.getParameters();
670         for (NamedValue namedValue : namedValues)
671         {
672             // Get the type of the property
673
DataTypeDefinition propertyType = null;
674             ActionDefinition actionDefintion = this.actionService.getActionDefinition(action.getActionDefinitionName());
675             ParameterDefinition propertyDefintion = actionDefintion.getParameterDefintion(namedValue.getName());
676             if (propertyDefintion != null)
677             {
678                 propertyType = this.dictionaryService.getDataType(propertyDefintion.getType());
679             }
680             
681             // Convert the value into the correct type
682
Serializable JavaDoc value = null;
683             if (propertyType != null)
684             {
685                 value = (Serializable JavaDoc)DefaultTypeConverter.INSTANCE.convert(propertyType, namedValue.getValue());
686             }
687             else
688             {
689                 value = namedValue.getValue();
690             }
691             
692             // Set the parameter
693
action.setParameterValue(namedValue.getName(), value);
694         }
695         
696         // Set the conditions
697
Condition[] webServiceConditions = webServiceAction.getConditions();
698         if (webServiceConditions != null)
699         {
700             for (Condition webServiceCondition : webServiceConditions)
701             {
702                 action.addActionCondition(convertToActionCondition(webServiceCondition));
703             }
704         }
705         
706         // Set the compensating action
707
org.alfresco.repo.webservice.action.Action webServiceCompensatingAction = webServiceAction.getCompensatingAction();
708         if (webServiceCompensatingAction != null)
709         {
710             Action compensatingAction = convertToAction(webServiceCompensatingAction);
711             action.setCompensatingAction(compensatingAction);
712         }
713         
714         // Set the child actions (if we are dealing with a composite action)
715
if (CompositeActionExecuter.NAME.equals(actionDefinitionName) == true)
716         {
717             org.alfresco.repo.webservice.action.Action[] webServiceChildActions = webServiceAction.getActions();
718             if (webServiceChildActions != null)
719             {
720                 for (org.alfresco.repo.webservice.action.Action webServiceChildAction : webServiceChildActions)
721                 {
722                     Action childAction = convertToAction(webServiceChildAction);
723                     ((CompositeAction)action).addAction(childAction);
724                 }
725             }
726         }
727         
728         return action;
729     }
730     
731     /**
732      *
733      * @param webServiceCondition
734      * @return
735      */

736     private ActionCondition convertToActionCondition(Condition webServiceCondition)
737     {
738         // If the id is null then generate one
739
String JavaDoc id = webServiceCondition.getId();
740         if (id == null || id.length() == 0)
741         {
742             id = GUID.generate();
743         }
744         
745         // Create the action condition
746
ActionCondition actionCondition = new ActionConditionImpl(id, webServiceCondition.getConditionName());
747         
748         // Set the details of the condition
749
actionCondition.setInvertCondition(webServiceCondition.isInvertCondition());
750         
751         // Set the condition parameters
752
NamedValue[] namedValues = webServiceCondition.getParameters();
753         for (NamedValue namedValue : namedValues)
754         {
755             // Get the type of the property
756
DataTypeDefinition propertyType = null;
757             ActionConditionDefinition actionConditionDefintion = this.actionService.getActionConditionDefinition(actionCondition.getActionConditionDefinitionName());
758             ParameterDefinition propertyDefintion = actionConditionDefintion.getParameterDefintion(namedValue.getName());
759             if (propertyDefintion != null)
760             {
761                 propertyType = this.dictionaryService.getDataType(propertyDefintion.getType());
762             }
763             
764             // Convert the value into the correct type
765
Serializable JavaDoc value = null;
766             if (propertyType != null)
767             {
768                 value = (Serializable JavaDoc)DefaultTypeConverter.INSTANCE.convert(propertyType, namedValue.getValue());
769             }
770             else
771             {
772                 value = namedValue.getValue();
773             }
774             
775             // Set the parameter
776
actionCondition.setParameterValue(namedValue.getName(), value);
777         }
778         
779         return actionCondition;
780     }
781
782     /**
783      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#removeActions(org.alfresco.repo.webservice.types.Reference, org.alfresco.repo.webservice.action.Action[])
784      */

785     public void removeActions(final Reference reference, final org.alfresco.repo.webservice.action.Action[] webServiceActions)
786             throws RemoteException JavaDoc, ActionFault
787     {
788         try
789         {
790             TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<Object JavaDoc>()
791             {
792                 public Object JavaDoc doWork() throws Exception JavaDoc
793                 {
794                     removeActionsImpl(reference, webServiceActions);
795                     return null;
796                 }
797             });
798         }
799         catch (Throwable JavaDoc exception)
800         {
801             if (logger.isDebugEnabled())
802             {
803                 logger.error("Unexpected error occurred", exception);
804             }
805             
806             throw new ActionFault(0, exception.getMessage());
807         }
808     }
809     
810     private void removeActionsImpl(Reference reference, org.alfresco.repo.webservice.action.Action[] webServiceActions)
811         throws RemoteException JavaDoc, ActionFault
812     {
813         // Get the node reference
814
NodeRef nodeRef = Utils.convertToNodeRef(reference, this.nodeService, this.searchService, this.namespaceService);
815         
816         if (webServiceActions == null)
817         {
818             // Remove all the actions
819
this.actionService.removeAllActions(nodeRef);
820         }
821         else
822         {
823             for (org.alfresco.repo.webservice.action.Action webServiceAction : webServiceActions)
824             {
825                 Action action = convertToAction(webServiceAction);
826                 this.actionService.removeAction(nodeRef, action);
827             }
828         }
829     }
830
831     /**
832      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#executeActions(org.alfresco.repo.webservice.types.Predicate, org.alfresco.repo.webservice.action.Action[])
833      */

834     public ActionExecutionResult[] executeActions(final Predicate predicate, final org.alfresco.repo.webservice.action.Action[] webServiceActions) throws RemoteException JavaDoc, ActionFault
835     {
836         try
837         {
838             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<ActionExecutionResult[]>()
839             {
840                 public ActionExecutionResult[] doWork() throws Exception JavaDoc
841                 {
842                     return executeActionsImpl(predicate, webServiceActions);
843                 }
844             });
845         }
846         catch (Throwable JavaDoc exception)
847         {
848             if (logger.isDebugEnabled())
849             {
850                 logger.error("Unexpected error occurred", exception);
851             }
852             
853             throw new ActionFault(0, exception.getMessage());
854         }
855     }
856     
857     /**
858      * Execute actions implementation
859      *
860      * @param predicate
861      * @param webServiceActions
862      * @return
863      * @throws RemoteException
864      * @throws ActionFault
865      */

866     public ActionExecutionResult[] executeActionsImpl(Predicate predicate, org.alfresco.repo.webservice.action.Action[] webServiceActions) throws RemoteException JavaDoc, ActionFault
867     {
868         List JavaDoc<ActionExecutionResult> results = new ArrayList JavaDoc<ActionExecutionResult>(10);
869         
870         // Resolve the predicate to a list of nodes
871
List JavaDoc<NodeRef> nodeRefs = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService);
872         for (NodeRef nodeRef : nodeRefs)
873         {
874             // Create the execution result object and set the action reference
875
ActionExecutionResult executionResult = new ActionExecutionResult();
876             executionResult.setReference(Utils.convertToReference(nodeRef));
877             
878             // Tyr and execute the actions
879
List JavaDoc<org.alfresco.repo.webservice.action.Action> executedActions = new ArrayList JavaDoc<org.alfresco.repo.webservice.action.Action>(10);
880             for (org.alfresco.repo.webservice.action.Action webServiceAction : webServiceActions)
881             {
882                 // Get the repository action object
883
Action action = convertToAction(webServiceAction);
884                 
885                 // TODO what about condition inversion
886
if (this.actionService.evaluateAction(action, nodeRef) == true)
887                 {
888                     // Execute the action (now that we know the conditions have been met)
889
this.actionService.executeAction(action, nodeRef, false);
890                     executedActions.add(webServiceAction);
891                 }
892             }
893             
894             // Set the executed actions on the execution result object
895
org.alfresco.repo.webservice.action.Action[] executedWebServiceActions = (org.alfresco.repo.webservice.action.Action[])executedActions.toArray(new org.alfresco.repo.webservice.action.Action[executedActions.size()]);
896             executionResult.setActions(executedWebServiceActions);
897             
898             // Add the execution object to the result list
899
results.add(executionResult);
900         }
901         return (ActionExecutionResult[])results.toArray(new ActionExecutionResult[results.size()]);
902     }
903
904     /**
905      * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#getRules(org.alfresco.repo.webservice.types.Reference, org.alfresco.repo.webservice.action.RuleFilter)
906      */

907     public org.alfresco.repo.webservice.action.Rule[] getRules(final Reference reference, final RuleFilter ruleFilter)
908             throws RemoteException JavaDoc, ActionFault
909     {
910         try
911         {
912             return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<org.alfresco.repo.webservice.action.Rule[]>()
913             {
914                 public org.alfresco.repo.webservice.action.Rule[] doWork() throws Exception JavaDoc
915                 {
916                     return getRulesImpl(reference, ruleFilter);
917                 }
918             });
919         }
920         catch (Throwable JavaDoc exception)
921         {
922             if (logger.isDebugEnabled())
923             {
924                 logger.error("Unexpected error occurred", exception);
925             }
926             
927             throw new ActionFault(0, exception.getMessage());
928         }
929     }
930     
931     private org.alfresco.repo.webservice.action.Rule[] getRulesImpl(Reference reference, RuleFilter ruleFilter)
932         throws RemoteException JavaDoc, ActionFault
933     {
934         // Get the node reference
935
NodeRef nodeRef = Utils.convertToNodeRef(reference, this.nodeService, this.searchService, this.namespaceService);
936         
937         // Get the rules associtated with the node reference
938
List JavaDoc<Rule> rules = this.ruleService.getRules(nodeRef);
939         
940         // Filter the results based on the rule filter passed
941
// TODO
942

943         // Marshal the results
944
org.alfresco.repo.webservice.action.Rule[] webServiceRules = new org.alfresco.repo.webservice.action.Rule[rules.size()];
945         int index = 0;
946         for (Rule rule : rules)
947         {
948             webServiceRules[index] = convertToWebServiceRule(rule);
949             index ++;
950         }
951         
952         
953         return webServiceRules;
954     }
955     
956     private org.alfresco.repo.webservice.action.Rule convertToWebServiceRule(Rule rule)
957     {
958         // Get the run as user
959
// TODO for now set to null since this has no effect yet
960
String JavaDoc runAsUserName = null;
961         
962         // Get the conditions
963
List JavaDoc<ActionCondition> conditions = rule.getActionConditions();
964         Condition[] webServiceConditions = new Condition[conditions.size()];
965         int index2 = 0;
966         for (ActionCondition condition : conditions)
967         {
968             webServiceConditions[index2] = convertToWebServiceCondition(condition);
969             index2++;
970         }
971                 
972         // Sort out any sub-actions
973
org.alfresco.repo.webservice.action.Action[] childWebServiceActions = null;
974         List JavaDoc<Action> childActions = rule.getActions();
975         childWebServiceActions = new org.alfresco.repo.webservice.action.Action[childActions.size()];
976         int index3 = 0;
977         for (Action childAction : childActions)
978         {
979             childWebServiceActions[index3] = convertToWebServiceAction(childAction);
980             index3 ++;
981         }
982         
983         // Get the reference to the 'owning' node
984
NodeRef owningNodeRef = rule.getOwningNodeRef();
985         Reference reference = null;
986         if (owningNodeRef != null)
987         {
988             reference = Utils.convertToReference(owningNodeRef);
989         }
990         
991         // Create the web service rule object
992
org.alfresco.repo.webservice.action.Rule webServiceRule = new org.alfresco.repo.webservice.action.Rule(
993                 rule.getId(),
994                 rule.getRuleTypeName(),
995                 rule.getTitle(),
996                 rule.getDescription(),
997                 rule.getExecuteAsychronously(),
998                 webServiceConditions,
999                 childWebServiceActions,
1000                runAsUserName,
1001                reference);
1002        
1003        return webServiceRule;
1004    }
1005
1006    /**
1007     * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#saveRules(org.alfresco.repo.webservice.types.Reference, org.alfresco.repo.webservice.action.Rule[])
1008     */

1009    public org.alfresco.repo.webservice.action.Rule[] saveRules(final Reference reference, final org.alfresco.repo.webservice.action.Rule[] webServiceRules)
1010            throws RemoteException JavaDoc, ActionFault
1011    {
1012        try
1013        {
1014            return TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<org.alfresco.repo.webservice.action.Rule[]>()
1015            {
1016                public org.alfresco.repo.webservice.action.Rule[] doWork() throws Exception JavaDoc
1017                {
1018                    return saveRulesImpl(reference, webServiceRules);
1019                }
1020            });
1021        }
1022        catch (Throwable JavaDoc exception)
1023        {
1024            if (logger.isDebugEnabled())
1025            {
1026                logger.error("Unexpected error occurred", exception);
1027            }
1028            
1029            throw new ActionFault(0, exception.getMessage());
1030        }
1031    }
1032    
1033    private org.alfresco.repo.webservice.action.Rule[] saveRulesImpl(Reference reference, org.alfresco.repo.webservice.action.Rule[] webServiceRules)
1034        throws RemoteException JavaDoc, ActionFault
1035    {
1036        // Get the node reference
1037
NodeRef nodeRef = Utils.convertToNodeRef(reference, this.nodeService, this.searchService, this.namespaceService);
1038        
1039        // Create the result array
1040
org.alfresco.repo.webservice.action.Rule[] results = new org.alfresco.repo.webservice.action.Rule[webServiceRules.length];
1041        
1042        int index = 0;
1043        for (org.alfresco.repo.webservice.action.Rule webServiceRule : webServiceRules)
1044        {
1045            // Convert to a server rule object
1046
Rule rule = convertToRule(webServiceRule);
1047            
1048            // Save the rule
1049
this.ruleService.saveRule(nodeRef, rule);
1050            
1051            // Add the updated rule to the results
1052
results[index] = convertToWebServiceRule(rule);
1053            index++;
1054        }
1055        
1056        return results;
1057    }
1058
1059    /**
1060     * @see org.alfresco.repo.webservice.action.ActionServiceSoapPort#removeRules(org.alfresco.repo.webservice.types.Reference, org.alfresco.repo.webservice.action.Rule[])
1061     */

1062    public void removeRules(final Reference reference, final org.alfresco.repo.webservice.action.Rule[] webServiceRules)
1063            throws RemoteException JavaDoc, ActionFault
1064    {
1065        try
1066        {
1067            TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionWork<Object JavaDoc>()
1068            {
1069                public Object JavaDoc doWork() throws Exception JavaDoc
1070                {
1071                    removeRulesImpl(reference, webServiceRules);
1072                    return null;
1073                }
1074            });
1075        }
1076        catch (Throwable JavaDoc exception)
1077        {
1078            if (logger.isDebugEnabled())
1079            {
1080                logger.error("Unexpected error occurred", exception);
1081            }
1082            
1083            throw new ActionFault(0, exception.getMessage());
1084        }
1085    }
1086    
1087    /**
1088     *
1089     * @param reference
1090     * @param webServiceRules
1091     * @throws RemoteException
1092     * @throws ActionFault
1093     */

1094    public void removeRulesImpl(Reference reference, org.alfresco.repo.webservice.action.Rule[] webServiceRules)
1095        throws RemoteException JavaDoc, ActionFault
1096    {
1097        // Get the node reference
1098
NodeRef nodeRef = Utils.convertToNodeRef(reference, this.nodeService, this.searchService, this.namespaceService);
1099        
1100        if (webServiceRules == null)
1101        {
1102            // Remove all the actions
1103
this.ruleService.removeAllRules(nodeRef);
1104        }
1105        else
1106        {
1107            for (org.alfresco.repo.webservice.action.Rule webServiceRule : webServiceRules)
1108            {
1109                Rule rule = convertToRule(webServiceRule);
1110                this.ruleService.removeRule(nodeRef, rule);
1111            }
1112        }
1113
1114    }
1115    
1116    /**
1117     *
1118     * @param webServiceRule
1119     * @return
1120     */

1121    private Rule convertToRule(org.alfresco.repo.webservice.action.Rule webServiceRule)
1122    {
1123        // If the id is null then generate one
1124
String JavaDoc id = webServiceRule.getId();
1125        if (id == null || id.length() == 0)
1126        {
1127            id = GUID.generate();
1128        }
1129        
1130        // Get the owning node ref
1131
NodeRef owningNodeRef = null;
1132        if (webServiceRule.getReference() != null)
1133        {
1134            owningNodeRef = Utils.convertToNodeRef(
1135                webServiceRule.getReference(),
1136                this.nodeService,
1137                this.searchService,
1138                this.namespaceService);
1139        }
1140        
1141        // Get the rule type name
1142
String JavaDoc ruleTypeName = webServiceRule.getRuleType();
1143        
1144        // Create the rule
1145
RuleImpl rule = new RuleImpl(id, ruleTypeName, owningNodeRef);
1146        
1147        // Set some of the rules details
1148
rule.setTitle(webServiceRule.getTitle());
1149        rule.setDescription(webServiceRule.getDescription());
1150        rule.setExecuteAsynchronously(webServiceRule.isExecuteAsynchronously());
1151        
1152        // Set the conditions
1153
Condition[] webServiceConditions = webServiceRule.getConditions();
1154        if (webServiceConditions != null)
1155        {
1156            for (Condition webServiceCondition : webServiceConditions)
1157            {
1158                rule.addActionCondition(convertToActionCondition(webServiceCondition));
1159            }
1160        }
1161        
1162        // Set the child actions
1163
org.alfresco.repo.webservice.action.Action[] webServiceChildActions = webServiceRule.getActions();
1164        if (webServiceChildActions != null)
1165        {
1166            for (org.alfresco.repo.webservice.action.Action webServiceChildAction : webServiceChildActions)
1167            {
1168                Action childAction = convertToAction(webServiceChildAction);
1169                rule.addAction(childAction);
1170            }
1171        }
1172        
1173        return rule;
1174    }
1175}
1176
Popular Tags