KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > webservices > RemoteWorkflowServiceImpl


1 package org.infoglue.cms.webservices;
2
3 import java.util.Map JavaDoc;
4
5 import org.apache.log4j.Logger;
6 import org.infoglue.cms.applications.workflowtool.function.InfoglueFunction;
7 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
8 import org.infoglue.cms.controllers.kernel.impl.simple.UserControllerProxy;
9 import org.infoglue.cms.controllers.kernel.impl.simple.WorkflowController;
10 import org.infoglue.cms.entities.mydesktop.WorkflowVO;
11 import org.infoglue.cms.exception.SystemException;
12 import org.infoglue.cms.security.InfoGluePrincipal;
13 import org.infoglue.cms.util.workflow.WorkflowFacade;
14 import org.infoglue.deliver.util.webservices.DynamicWebserviceSerializer;
15
16 /**
17  * This service is used for creating workflows from an external application.
18  */

19
20 public class RemoteWorkflowServiceImpl extends RemoteInfoGlueService
21 {
22     /**
23      * The class logger.
24      */

25     private final static Logger logger = Logger.getLogger(RemoteWorkflowServiceImpl.class.getName());
26     
27     /**
28      * The principal executing the workflow.
29      */

30     private InfoGluePrincipal principal;
31     
32     /**
33      * The inputs to the workflow.
34      */

35     private Map JavaDoc inputs;
36     
37     /**
38      * Default constructor.
39      */

40     public RemoteWorkflowServiceImpl()
41     {
42         super();
43     }
44
45     /**
46      * Creates the specified workflow running as the specified principal.
47      * To determine if the workflow executed successfully, the state of the workflow is checked.
48      * A terminated workflow is interpreted as a failure, meaning that all workflows that could
49      * be started from an external application, should terminate directly if an error occurs.
50      *
51      * @param principalName the name of the principal that should execute the workflow. Must have permission to create the workflow.
52      * @param languageId the language to use when executing the workflow.
53      * @param workflowName the name of the workflow.
54      * @param inputsArray the inputs to the workflow.
55      * @return true if the workflow executed sucessfully; false otherwise.
56      */

57     public Boolean JavaDoc start(final String JavaDoc principalName, final Integer JavaDoc languageId, final String JavaDoc workflowName, final Object JavaDoc[] inputsArray, final Object JavaDoc[] ppp)
58     {
59         try
60         {
61             final DynamicWebserviceSerializer serializer = new DynamicWebserviceSerializer();
62             initializePrincipal(principalName, workflowName);
63             initializeInputs((Map JavaDoc) serializer.deserialize(inputsArray), languageId);
64             
65             logger.debug("start(" + principalName + "," + workflowName + "," + languageId + "," + inputs + ")");
66             
67             final WorkflowVO workflowVO = WorkflowController.getController().initializeWorkflow(principal, workflowName, 0, inputs);
68             if(hasTerminated(workflowVO))
69             {
70                 logger.debug("The workflow has terminated.");
71                 return Boolean.FALSE;
72             }
73         }
74         catch(Throwable JavaDoc t)
75         {
76             System.out.println(t);
77             return Boolean.FALSE;
78         }
79         
80         updateCaches();
81
82         return Boolean.TRUE;
83     }
84
85     /**
86      * Returns true if the workflow has terminated; false otherwise.
87      *
88      * @param workflowVO the workflow.
89      * @return true if the workflow has terminated; false otherwise.
90      */

91     private boolean hasTerminated(final WorkflowVO workflowVO)
92     {
93         return new WorkflowFacade(principal, workflowVO.getIdAsPrimitive()).isFinished();
94     }
95
96     /**
97      * Initializes the inputs to the workflow.
98      *
99      * @param callerInputs the inputs sent in from the caller of this service.
100      * @param languageId the locale to use when running the workflow.
101      */

102     private void initializeInputs(final Map JavaDoc callerInputs, final Integer JavaDoc languageId) throws SystemException
103     {
104         inputs = callerInputs;
105         inputs.put(InfoglueFunction.PRINCIPAL_PARAMETER, principal);
106         inputs.put(InfoglueFunction.LOCALE_PARAMETER, LanguageController.getController().getLocaleWithId(languageId));
107     }
108     
109     /**
110      * Checks if the principal exists and if the principal is allowed to create the workflow.
111      *
112      * @param userName the name of the user.
113      * @param workflowName the name of the workflow to create.
114      * @throws SystemException if the principal doesn't exists or doesn't have permission to create the workflow.
115      */

116     private void initializePrincipal(final String JavaDoc userName, final String JavaDoc workflowName) throws SystemException
117     {
118         try
119         {
120             principal = UserControllerProxy.getController().getUser(userName);
121         }
122         catch(SystemException e)
123         {
124             throw e;
125         }
126         catch(Exception JavaDoc e)
127         {
128             throw new SystemException(e);
129         }
130         if(principal == null)
131         {
132             throw new SystemException("No such principal [" + userName + "].");
133         }
134         if(!WorkflowController.getController().getIsAccessApproved(workflowName, principal))
135         {
136             throw new SystemException("The principal [" + userName + "] is not allowed to create the [" + workflowName + "] workflow.");
137         }
138     }
139 }
140
Popular Tags