KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > mydesktoptool > actions > ViewMyDesktopToolStartPageAction


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

23
24 package org.infoglue.cms.applications.mydesktoptool.actions;
25
26 import java.io.IOException JavaDoc;
27 import java.io.UnsupportedEncodingException JavaDoc;
28 import java.net.URLEncoder JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction;
35 import org.infoglue.cms.applications.managementtool.actions.WYSIWYGPropertiesAction;
36 import org.infoglue.cms.controllers.kernel.impl.simple.ShortcutController;
37 import org.infoglue.cms.controllers.kernel.impl.simple.WorkflowController;
38 import org.infoglue.cms.entities.mydesktop.WorkflowActionVO;
39 import org.infoglue.cms.entities.mydesktop.WorkflowVO;
40 import org.infoglue.cms.exception.SystemException;
41 import org.infoglue.cms.util.CmsPropertyHandler;
42 import org.infoglue.cms.util.workflow.StepFilter;
43
44 import webwork.action.ActionContext;
45
46 import com.opensymphony.workflow.InvalidActionException;
47 import com.opensymphony.workflow.WorkflowException;
48
49 /**
50  * This class implements the action class for the startpage in the mydesktop tool.
51  * @author Mattias Bogeblad
52  * @author <a HREF="mailto:jedprentice@gmail.com">Jed Prentice</a>
53  */

54 public class ViewMyDesktopToolStartPageAction extends InfoGlueAbstractAction
55 {
56     private final static Logger logger = Logger.getLogger(ViewMyDesktopToolStartPageAction.class.getName());
57
58     private static final long serialVersionUID = 6543209932597662088L;
59
60     protected static final String JavaDoc INVALID_ACTION = "invalidAction";
61
62     private static final WorkflowController controller = WorkflowController.getController();
63     private static final ShortcutController shortcutController = ShortcutController.getController();
64
65     private List JavaDoc availableWorkflowVOList;
66     private List JavaDoc workflowVOList;
67     private List JavaDoc availableShortcutVOList;
68
69     private WorkflowVO workflow = new WorkflowVO();
70     private int actionId;
71     private String JavaDoc finalReturnAddress = "";
72
73     public List JavaDoc getWorkflowVOList()
74     {
75         return workflowVOList;
76     }
77
78     public List JavaDoc getAvailableWorkflowVOList()
79     {
80         return availableWorkflowVOList;
81     }
82
83     public List JavaDoc getAvailableShortcutVOList()
84     {
85         return availableShortcutVOList;
86     }
87
88     public List JavaDoc getWorkflowActionVOList()
89     {
90         return getAvailableActions(null);
91     }
92
93     public List JavaDoc getWorkflowActionVOList(StepFilter filter)
94     {
95         return getAvailableActions(filter);
96     }
97
98     /**
99      * Provides access to the underlying workflow
100      * @return a reference to the underlying workflow
101      */

102     WorkflowVO getWorkflow()
103     {
104         return workflow;
105     }
106
107     /**
108      * Allows the workflow name to be set via the request parameter "workflowName".
109      * @param name the name of the desired workflow
110      */

111     public void setWorkflowName(String JavaDoc name)
112     {
113         workflow.setName(name);
114     }
115
116     private String JavaDoc getWorkflowName()
117     {
118         return workflow.getName();
119     }
120
121     /**
122      * Allows the action ID to be set via the request parameter "actionId"
123      * @param actionId the ID of the action to execute
124      */

125     public void setActionId(int actionId)
126     {
127         this.actionId = actionId;
128     }
129
130     /**
131      * Allows the workflowID to be set via the request parameter "workflowId"
132      * @param workflowId the ID of the desired workflow
133      */

134     public void setWorkflowId(long workflowId)
135     {
136         workflow.setWorkflowId(new Long JavaDoc(workflowId));
137     }
138
139     private long getWorkflowId()
140     {
141         return workflow.getIdAsPrimitive();
142     }
143
144     /**
145      * Populates the lists of workflow and workflow action VOs.
146      * @return Action.SUCCESS
147      * @throws SystemException if a workflow error occurs
148      */

149     public String JavaDoc doExecute() throws SystemException
150     {
151         populateLists();
152         return SUCCESS;
153     }
154
155     /**
156      * Starts the workflow specified in the request parameter "workflowName" with the initial action identified by
157      * the request parameter "actionId" and redirects to the view page for the desired initial action. If no actionId
158      * is passed in the request, we assume that the ID of the initial action is zero.
159      * @return Action.NONE if the desired initial action has a view, otherwise the effect is the same as calling
160      * doExecute()
161      * @throws SystemException
162      * @see #doExecute
163      */

164     public String JavaDoc doStartWorkflow() throws SystemException
165     {
166         workflow = controller.initializeWorkflow(getInfoGluePrincipal(), getWorkflowName(), actionId, WorkflowController.createWorkflowParameters(ActionContext.getRequest()));
167         return redirectToView();
168     }
169
170     /**
171      * Invokes an action in a workflow based on the values of the actionId and workflowId request parameters and
172      * redirects to the action's view if one has been defined. If there is no action view, the user will be taken to
173      * ViewMyDesktopToolStartPage.action, i.e., the effect is the same as calling doExecute().
174      * <b>NOTE:</b> Assumes there will be one current action with a view; we iterate over the available actions until we
175      * find one with a view, then redirect to that view. If there are other actions with different views, they will be
176      * ignored.
177      * @return NONE if there is an available action with a view and we redirect to the action's view page, otherwise
178      * performs the same operations as doExecute().
179      * @throws SystemException if an error occurs invoking the action or URL-encoding the action view
180      * @see #doExecute
181      */

182     public String JavaDoc doInvoke() throws SystemException
183     {
184         logger.info("****************************************");
185         logger.info("workflowId:" + getWorkflowId());
186         logger.info("actionId:" + actionId);
187         logger.info("****************************************");
188
189         try
190         {
191             workflow = controller.invokeAction(getInfoGluePrincipal(), getWorkflowId(), actionId, WorkflowController.createWorkflowParameters(ActionContext.getRequest()));
192             return redirectToView();
193         }
194         catch (InvalidActionException e)
195         {
196             return INVALID_ACTION;
197         }
198         catch (WorkflowException e)
199         {
200             throw new SystemException(e);
201         }
202     }
203
204     /**
205      * Redirects to view defined for the current action(s) in the given workflow.
206      * <b>NOTE:</b> Assumes there will be one current action with a view; we iterate over the available actions until we
207      * find one with a view, then redirect to that view. If there are other actions with different views, they will be
208      * ignored. If we go through the entire list without finding a view, the user will be sent to
209      * ViewMyDesktopToolStartPage.action, i.e., the effect will be the same as calling doExecute();
210      * @return NONE if there is an available action with a view and we redirect to the action's view page, otherwise
211      * performs the same operations as doExecute().
212      * @throws SystemException if an error occurs invoking the action or URL-encoding the action view
213      * @see #doExecute
214      * <b>TODO:</b> find a better heuristic for determining which view to go to.
215      */

216     private String JavaDoc redirectToView() throws SystemException
217     {
218         for (Iterator JavaDoc i = workflow.getAvailableActions().iterator(); i.hasNext();)
219         {
220             String JavaDoc url = getViewUrl((WorkflowActionVO)i.next());
221             if (url.length() > 0)
222                 return redirect(url);
223         }
224
225         if(this.finalReturnAddress != null && !this.finalReturnAddress.equals(""))
226             return redirect(finalReturnAddress);
227             
228         logger.info("No action view, coming back to mydesktop...");
229         return doExecute();
230     }
231
232     /**
233      * Populates availableWorkflowVOList, workflowVOList, and workflowActionVOList.
234      * @throws SystemException if a workflow error occurs
235      */

236     private void populateLists() throws SystemException
237     {
238         availableWorkflowVOList = controller.getAvailableWorkflowVOList(getInfoGluePrincipal());
239         final String JavaDoc showAllWorkflows = CmsPropertyHandler.getShowAllWorkflows();
240         if(showAllWorkflows == null || showAllWorkflows.equalsIgnoreCase("true"))
241         {
242             workflowVOList = controller.getCurrentWorkflowVOList(getInfoGluePrincipal());
243         }
244         else
245         {
246             workflowVOList = controller.getMyCurrentWorkflowVOList(getInfoGluePrincipal());
247         }
248         
249         availableShortcutVOList = shortcutController.getAvailableShortcutVOList(getInfoGluePrincipal());
250     }
251
252     /**
253      * Builds a list of all available actions for all workflows using the given step filter. Assumes workflowVOList has
254      * been populated.
255      * @return a list of all available actions
256      * @throws NullPointerException if workflowVOList has not been populated
257      */

258     private List JavaDoc getAvailableActions(StepFilter filter)
259     {
260         List JavaDoc actions = new ArrayList JavaDoc();
261         for (Iterator JavaDoc workflows = workflowVOList.iterator(); workflows.hasNext();)
262             actions.addAll(((WorkflowVO)workflows.next()).getAvailableActions(filter));
263
264         return actions;
265     }
266
267     /**
268      * Creates the view URL from the given workflow action.
269      * @param action a workflow action
270      * @return the view URL
271      * @throws SystemException if an error occurs while encoding the URL
272      */

273     private String JavaDoc getViewUrl(WorkflowActionVO action) throws SystemException
274     {
275         if (!action.hasView())
276             return "";
277
278         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(action.getView());
279         if (containsQuestionMark(action.getView()))
280             buffer.append('&');
281         else
282             buffer.append('?');
283
284         return buffer.append("workflowId=").append(getWorkflowId()).append("&actionId=").append(action.getId())
285                 .append("&returnAddress=").append(getReturnAddress()).append("&finalReturnAddress=").append(getFinalReturnAddress()).append('&')
286                 .append(getRequest().getQueryString()).toString();
287     }
288
289     private static boolean containsQuestionMark(String JavaDoc s)
290     {
291         return s.indexOf("?") >= 0;
292     }
293
294     /**
295      * Returns the return address
296      * @return the return address
297      * @throws SystemException if an error occurs while encoding the URL
298      */

299     private String JavaDoc getReturnAddress() throws SystemException
300     {
301         try
302         {
303             String JavaDoc cmsFullBaseUrl = CmsPropertyHandler.getCmsFullBaseUrl();
304             logger.info("cmsFullBaseUrl:" + cmsFullBaseUrl);
305             if(cmsFullBaseUrl != null && !cmsFullBaseUrl.equals(""))
306                 return URLEncoder.encode(cmsFullBaseUrl + "/ViewMyDesktopToolStartPage!invoke.action", "UTF-8");
307             else
308                 return URLEncoder.encode(getURLBase() + "/ViewMyDesktopToolStartPage!invoke.action", "UTF-8");
309         }
310         catch (UnsupportedEncodingException JavaDoc e)
311         {
312             throw new SystemException(e);
313         }
314     }
315
316     private String JavaDoc redirect(String JavaDoc url) throws SystemException
317     {
318         try
319         {
320             logger.info("Url in doInvoke:" + url);
321             getResponse().sendRedirect(url);
322             return NONE;
323         }
324         catch (IOException JavaDoc e)
325         {
326             throw new SystemException(e);
327         }
328     }
329
330     /**
331      * Returns the final return address
332      * @return the final return address
333      * @throws SystemException if an error occurs while encoding the URL
334      */

335     private String JavaDoc getFinalReturnAddress() throws SystemException
336     {
337         try
338         {
339             return URLEncoder.encode(this.finalReturnAddress, "UTF-8");
340         }
341         catch (UnsupportedEncodingException JavaDoc e)
342         {
343             throw new SystemException(e);
344         }
345     }
346
347     public void setFinalReturnAddress(String JavaDoc finalReturnAddress)
348     {
349         if(finalReturnAddress != null && !finalReturnAddress.equals("null"))
350             this.finalReturnAddress = finalReturnAddress;
351     }
352 }
353
Popular Tags