KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > WorkflowController


1 /* ===============================================================================
2 /* ===============================================================================
3 *
4 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
5 *
6 * ===============================================================================
7 *
8 * Copyright (C)
9 *
10 * This program is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License version 2, as published by the
12 * Free Software Foundation. See the file LICENSE.html for more information.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
20 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
21 *
22 * ===============================================================================
23 */

24
25 package org.infoglue.cms.controllers.kernel.impl.simple;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.exolab.castor.jdo.Database;
37 import org.infoglue.cms.entities.kernel.BaseEntityVO;
38 import org.infoglue.cms.entities.mydesktop.WorkflowActionVO;
39 import org.infoglue.cms.entities.mydesktop.WorkflowVO;
40 import org.infoglue.cms.exception.Bug;
41 import org.infoglue.cms.exception.SystemException;
42 import org.infoglue.cms.security.InfoGluePrincipal;
43 import org.infoglue.cms.util.CmsPropertyHandler;
44 import org.infoglue.cms.util.workflow.WorkflowFacade;
45
46 import com.opensymphony.module.propertyset.PropertySet;
47 import com.opensymphony.workflow.WorkflowException;
48
49 /**
50  * This controller acts as the api towards the OSWorkflow Workflow-engine.
51  * @author Mattias Bogeblad
52  * @author <a HREF="mailto:jedprentice@gmail.com">Jed Prentice</a>
53  */

54 public class WorkflowController extends BaseController
55 {
56     private final static Logger logger = Logger.getLogger(UserPropertiesController.class.getName());
57
58     private static final WorkflowController controller = new WorkflowController();
59
60     /**
61      * Returns the WorkflowController singleton
62      * @return a reference to a WorkflowController
63      */

64     public static WorkflowController getController()
65     {
66         return controller;
67     }
68
69     private WorkflowController() {}
70
71     /**
72      * TODO: move; used by tests + CreateWorkflowInstanceAction
73      */

74     public static Map JavaDoc createWorkflowParameters(final HttpServletRequest JavaDoc request)
75     {
76         final Map JavaDoc parameters = new HashMap JavaDoc();
77         parameters.putAll(request.getParameterMap());
78         parameters.put("request", request);
79         return parameters;
80     }
81
82     /**
83      * @param principal the user principal representing the desired user
84      * @param name the name of the workflow to create.
85      * @param actionId the ID of the initial action
86      * @param inputs the inputs to the workflow
87      * @return a WorkflowVO representing the newly created workflow instance
88      * @throws SystemException if an error occurs while initiaizing the workflow
89      */

90     public WorkflowVO initializeWorkflow(InfoGluePrincipal principal, String JavaDoc name, int actionId, Map JavaDoc inputs) throws SystemException
91     {
92         try
93         {
94             if(getIsAccessApproved(name, principal))
95             {
96                 return new WorkflowFacade(principal, name, actionId, inputs).createWorkflowVO();
97             }
98             throw new Bug("You are not allowed to create " + name + " workflows.");
99         }
100         catch (Exception JavaDoc e)
101         {
102             throw new SystemException(e);
103         }
104     }
105
106     /**
107      * Returns a list of all available workflows, i.e., workflows defined in workflows.xml
108      * @param userPrincipal a user principal
109      * @return a list WorkflowVOs representing available workflows
110      */

111     public List JavaDoc getAvailableWorkflowVOList(InfoGluePrincipal userPrincipal) throws SystemException
112     {
113         final List JavaDoc allWorkflows = new WorkflowFacade(userPrincipal).getDeclaredWorkflows();
114         final List JavaDoc accessibleWorkflows = new ArrayList JavaDoc();
115         for(final Iterator JavaDoc i = allWorkflows.iterator(); i.hasNext(); )
116         {
117             final WorkflowVO workflowVO = (WorkflowVO) i.next();
118             if(getIsAccessApproved(workflowVO.getName(), userPrincipal))
119             {
120                 accessibleWorkflows.add(workflowVO);
121             }
122         }
123         
124         return accessibleWorkflows;
125     }
126
127     /**
128      * This method returns true if the user should have access to the contentTypeDefinition sent in.
129      */

130     
131     public boolean getIsAccessApproved(String JavaDoc workflowName, InfoGluePrincipal infoGluePrincipal) throws SystemException
132     {
133         final String JavaDoc protectWorkflows = CmsPropertyHandler.getProtectWorkflows();
134         if(protectWorkflows == null || !protectWorkflows.equalsIgnoreCase("true"))
135         {
136             return true;
137         }
138             
139         logger.info("getIsAccessApproved for " + workflowName + " AND " + infoGluePrincipal);
140         boolean hasAccess = false;
141         
142         Database db = CastorDatabaseService.getDatabase();
143         beginTransaction(db);
144
145         try
146         {
147             hasAccess = AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Workflow.Create", workflowName);
148             commitTransaction(db);
149         }
150         catch(Exception JavaDoc e)
151         {
152             logger.error("An error occurred so we should not complete the transaction:" + e, e);
153             rollbackTransaction(db);
154             throw new SystemException(e.getMessage());
155         }
156     
157         return hasAccess;
158     }
159
160     /**
161      * Returns current workflows, i.e., workflows that are active.
162      * @param userPrincipal a user principal
163      * @return a list of WorkflowVOs representing all active workflows
164      * @throws SystemException if an error occurs while finding the current workflows
165      */

166     public List JavaDoc getCurrentWorkflowVOList(InfoGluePrincipal userPrincipal) throws SystemException
167     {
168         return new WorkflowFacade(userPrincipal).getActiveWorkflows();
169     }
170     
171     /**
172      * Returns the workflows owned by the specified principal.
173      *
174      * @param userPrincipal a user principal.
175      * @return a list of WorkflowVOs owned by the principal.
176      * @throws SystemException if an error occurs while finding the workflows
177      */

178     public List JavaDoc getMyCurrentWorkflowVOList(InfoGluePrincipal userPrincipal) throws SystemException
179     {
180         return new WorkflowFacade(userPrincipal).getMyActiveWorkflows(userPrincipal);
181     }
182     
183
184     /**
185      * Invokes an action on a workflow for a given user and request
186      * @param principal the user principal
187      * @param workflowId the ID of the desired workflow
188      * @param actionId the ID of the desired action
189      * @param inputs the inputs to the workflow
190      * @return a WorkflowVO representing the current state of the workflow identified by workflowId
191      * @throws WorkflowException if a workflow error occurs
192      */

193     public WorkflowVO invokeAction(InfoGluePrincipal principal, long workflowId, int actionId, Map JavaDoc inputs) throws WorkflowException
194     {
195         WorkflowFacade workflow = new WorkflowFacade(principal, workflowId);
196         workflow.doAction(actionId, inputs);
197         return workflow.createWorkflowVO();
198     }
199
200     /**
201      * Returns the workflow property set for a particular user and workflow
202      * @return the workflow property set for the workflow with workflowId and the user represented by userPrincipal
203      */

204     public PropertySet getPropertySet(InfoGluePrincipal userPrincipal, long workflowId)
205     {
206         return new WorkflowFacade(userPrincipal, workflowId).getPropertySet();
207     }
208
209     /**
210      * Returns the contents of the PropertySet for a particular workflow
211      * @param userPrincipal a user principal
212      * @param workflowId the ID of the desired workflow
213      * @return a map containing the contents of the workflow property set
214      */

215     public Map JavaDoc getProperties(InfoGluePrincipal userPrincipal, long workflowId)
216     {
217         logger.info("userPrincipal:" + userPrincipal);
218         logger.info("workflowId:" + workflowId);
219
220         PropertySet propertySet = getPropertySet(userPrincipal, workflowId);
221         Map JavaDoc parameters = new HashMap JavaDoc();
222         for (Iterator JavaDoc keys = getPropertySet(userPrincipal, workflowId).getKeys().iterator(); keys.hasNext();)
223         {
224             String JavaDoc key = (String JavaDoc)keys.next();
225             parameters.put(key, propertySet.getString(key));
226         }
227
228         return parameters;
229     }
230
231     /**
232      * Returns all history steps for a workflow, i.e., all the steps that have already been performed.
233      * @param userPrincipal a user principal
234      * @param workflowId the ID of the desired workflow
235      * @return a list of WorkflowStepVOs representing all history steps for the workflow with workflowId
236      */

237     public List JavaDoc getHistorySteps(InfoGluePrincipal userPrincipal, long workflowId)
238     {
239         return new WorkflowFacade(userPrincipal, workflowId).getHistorySteps();
240     }
241
242     /**
243      * Returns all current steps for a workflow, i.e., steps that could be performed in the workflow's current state
244      * @param userPrincipal a user principal
245      * @param workflowId the Id of the desired workflow
246      * @return a list of WorkflowStepVOs representing the current steps of the workflow with workflowId
247      */

248     public List JavaDoc getCurrentSteps(InfoGluePrincipal userPrincipal, long workflowId)
249     {
250         return new WorkflowFacade(userPrincipal, workflowId).getCurrentSteps();
251     }
252
253     /**
254      * Returns all steps for a workflow definition. These are the steps declared in the workfow descriptor; there is
255      * no knowledge of current or history steps at this point.
256      * @param userPrincipal an InfoGluePrincipal representing a system user
257      * @param workflowId a workflowId
258      * @return a list of WorkflowStepVOs representing all steps in the workflow.
259      */

260     public List JavaDoc getAllSteps(InfoGluePrincipal userPrincipal, long workflowId)
261     {
262         return new WorkflowFacade(userPrincipal, workflowId).getDeclaredSteps();
263     }
264
265     /**
266      * Returns a new WorkflowActionVO. This method is apparently unused, but is required by BaseController. We don't
267      * use it internally because it requires a cast; it is simpler to just use <code>new</code> to create an instance.
268      * @return a new WorkflowActionVO.
269      */

270     public BaseEntityVO getNewVO()
271     {
272         return new WorkflowActionVO();
273     }
274 }
275
Popular Tags