KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > workflow > component > UITask


1 /*
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  */

5
6 package org.exoplatform.portlets.workflow.component;
7
8 import org.apache.commons.lang.StringUtils;
9 import org.apache.commons.logging.Log;
10 import org.exoplatform.faces.core.component.UISimpleForm;
11 import org.exoplatform.faces.core.component.UIStringInput;
12 import org.exoplatform.faces.core.component.model.*;
13 import org.exoplatform.faces.core.event.ExoActionEvent;
14 import org.exoplatform.faces.core.event.ExoActionListener;
15 import org.exoplatform.services.workflow.*;
16 import org.jbpm.ExecutionException;
17 import org.jbpm.model.execution.Token;
18
19 import javax.faces.context.FacesContext;
20 import java.text.Format JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.util.*;
23
24 /**
25  * Created y the eXo platform team
26  * User: Benjamin Mestrallet
27  * Date: 17 mai 2004
28  */

29 public class UITask extends UISimpleForm {
30   public static final String JavaDoc PREFIX = "prefix";
31   public static final String JavaDoc START_PROCESS = PREFIX + "startProcess";
32   public static final String JavaDoc END_OF_STATE = PREFIX + "endOfState";
33   public static final String JavaDoc CANCEL_PROCESS = PREFIX + "cancelProcess";
34   public static final String JavaDoc MANAGE_TRANSITION = "manageTransition";
35
36   protected static Log log_ = getLog("org.exoplatform.portal.portlets.workflow");
37
38   private ResourceBundle res;
39   private WorkflowFormsService formsService;
40   private Form form;
41   private UIStringInput[] inputs;
42   private boolean isStart;
43   private Long JavaDoc identification;
44   private WorkflowServiceContainer workflowServiceContainer;
45
46   public UITask(WorkflowServiceContainer workflowServiceContainer,
47                 WorkflowFormsService workflowFormsService,
48                 ResourceBundle resourceBundle) throws Exception JavaDoc {
49     super("taskForm", "post", null);
50     setRendererType("SimpleFormRenderer");
51     setClazz("UITask");
52     setId("UITask");
53
54     res = resourceBundle;
55     this.workflowServiceContainer = workflowServiceContainer;
56     this.formsService = workflowFormsService;
57
58     addActionListener(StartListener.class, START_PROCESS);
59     addActionListener(EndListener.class, END_OF_STATE);
60     addActionListener(TransitionListener.class, MANAGE_TRANSITION);
61     addActionListener(CancelListener.class, CANCEL_PROCESS);
62   }
63
64   public void updateUITree() {
65     clear();
66     Map variables = new HashMap();
67     WorkflowExecutionService executionService = workflowServiceContainer.createWorkflowExecutionService();
68     if (isStart) {
69       String JavaDoc startStateName = executionService.
70           getDefinition(identification, null).getStartState().getName();
71       form = formsService.getForm(identification, startStateName);
72     } else {
73       Token token = executionService.getToken(identification, null);
74       variables = executionService.getVariables(identification);
75       form = formsService.getForm(token.getState().getDefinition().getId(),
76           token.getState().getName());
77     }
78     executionService.close();
79
80     add(new HeaderRow().add(new LabelCell(form.getResourceBundle().getString("title")).
81         addColspan("2")));
82
83     List attributes = form.getVariableFormats();
84     int nbVar = form.getVariableFormats().size();
85
86     inputs = new UIStringInput[nbVar];
87     int i = 0;
88     for (Iterator iterator = attributes.iterator(); iterator.hasNext(); i++) {
89       Form.Attribute attribute = (Form.Attribute) iterator.next();
90       log_.debug("variable : " + attribute.getName());
91       Object JavaDoc value = variables.get(attribute.getName());
92       String JavaDoc text = "";
93       if (value != null) {
94         Format JavaDoc format = (Format JavaDoc) attribute.getValue();
95         text = format.format(value);
96       }
97       inputs[i] = new UIStringInput(StringUtils.replace(attribute.getName(), ".", "_"),
98           text);
99       add(new Row().
100           add(new LabelCell(form.getResourceBundle().
101           getString(attribute.getName() + ".label"))).
102           add(new ComponentCell(this, inputs[i])));
103     }
104     List submitButtons = form.getSubmitButtons();
105     ListComponentCell cells = new ListComponentCell();
106     if (submitButtons.isEmpty()) {
107       if (isStart) {
108         cells.add(new FormButton(form.getResourceBundle().
109             getString("submit"), START_PROCESS));
110       } else {
111         cells.add(new FormButton(form.getResourceBundle().
112             getString("submit"), END_OF_STATE));
113       }
114     } else {
115       for (Iterator iterator = submitButtons.iterator(); iterator.hasNext();) {
116         Form.Attribute attribute = (Form.Attribute) iterator.next();
117         cells.add(new FormButton(form.getResourceBundle().
118             getString(attribute.getName() + ".submit"), attribute.getName()));
119       }
120     }
121     cells.add(new FormButton("#{UITask.cancel}",
122         CANCEL_PROCESS)).addAlign("center");
123     add(new Row().add(cells.addColspan("2")));
124   }
125
126   public void setIdentification(Long JavaDoc identification) {
127     this.identification = identification;
128     if (isStart) {
129       WorkflowExecutionService executionService = workflowServiceContainer.createWorkflowExecutionService();
130       formsService.addForms(executionService.getDefinition(identification, null),
131           executionService, res.getLocale());
132       executionService.close();
133     }
134   }
135
136   public static class StartListener extends ExoActionListener {
137     public void execute(ExoActionEvent event) throws Exception JavaDoc {
138       UITask uiTask = (UITask) event.getComponent();
139       String JavaDoc remoteUser = getRemoteUser();
140       Map variables = prepareVariables(uiTask.form, uiTask.inputs);
141       WorkflowExecutionService executionService = uiTask.workflowServiceContainer.createWorkflowExecutionService();
142       executionService.startProcessInstance(remoteUser,
143           uiTask.identification, variables);
144       executionService.close();
145       ((UIBPDefinitionController) uiTask.getParent()).setRenderedComponent(UIBPDefinition.class);
146     }
147   }
148
149   public static class EndListener extends ExoActionListener {
150     public void execute(ExoActionEvent event) throws Exception JavaDoc {
151       UITask uiTask = (UITask) event.getComponent();
152       Map variables = prepareVariables(uiTask.form, uiTask.inputs);
153       String JavaDoc remoteUser = getRemoteUser();
154       try {
155         log_.debug("Actor ID : " + remoteUser);
156         WorkflowExecutionService executionService = uiTask.workflowServiceContainer.createWorkflowExecutionService();
157         executionService.endOfState(remoteUser, uiTask.identification, variables);
158         executionService.close();
159       } catch (Exception JavaDoc ex) {
160         log_.error("Cannot resolve state", ex);
161       }
162       ((UITaskListController) uiTask.getParent()).
163           setRenderedComponent(UITaskList.class);
164     }
165   }
166
167   public static class CancelListener extends ExoActionListener {
168     public void execute(ExoActionEvent event) throws Exception JavaDoc {
169       UITask uiTask = (UITask) event.getComponent();
170       if (uiTask.isStart) {
171         ((UIBPDefinitionController) uiTask.getParent()).setRenderedComponent(UIBPDefinition.class);
172       } else {
173         ((UITaskListController) uiTask.getParent()).setRenderedComponent(UITaskList.class);
174       }
175     }
176   }
177
178   public static class TransitionListener extends ExoActionListener {
179
180     public boolean canHandleAction(String JavaDoc action) {
181       return !action.startsWith(PREFIX);
182     }
183
184     public void execute(ExoActionEvent event) throws Exception JavaDoc {
185       UITask uiTask = (UITask) event.getComponent();
186       Map variables = prepareVariables(uiTask.form, uiTask.inputs);
187       String JavaDoc remoteUser = getRemoteUser();
188       List submitButtons = uiTask.form.getSubmitButtons();
189       for (Iterator iterator = submitButtons.iterator(); iterator.hasNext();) {
190         Form.Attribute attribute = (Form.Attribute) iterator.next();
191         if (event.getAction().equals(attribute.getName())) {
192           String JavaDoc transition = (String JavaDoc) extractAttribute(submitButtons,
193               attribute.getName()).getValue();
194           try {
195             WorkflowExecutionService executionService = uiTask.workflowServiceContainer.
196                 createWorkflowExecutionService();
197             executionService.endOfState(remoteUser, uiTask.identification,
198                 variables, transition);
199             executionService.close();
200           } catch (ExecutionException e) {
201             log_.error("Cannot resolve state", e);
202           }
203         }
204       }
205       ((UITaskListController) uiTask.getParent()).setRenderedComponent(UITaskList.class);
206     }
207   }
208
209   private static String JavaDoc getRemoteUser() {
210     String JavaDoc remoteUser = FacesContext.getCurrentInstance().
211         getExternalContext().getRemoteUser();
212     if (remoteUser == null)
213       remoteUser = "anonymous";
214     return remoteUser;
215   }
216
217   private static Map prepareVariables(Form form, UIStringInput[] inputs) {
218     Map variables = new HashMap();
219     Format JavaDoc format = null;
220     for (int i = 0; i < inputs.length; i++) {
221       UIStringInput input = inputs[i];
222       String JavaDoc name = StringUtils.replace(input.getName(), "_", ".");
223       log_.debug("input name : " + name);
224       format = (Format JavaDoc) extractAttribute(form.getVariableFormats(), name).getValue();
225       String JavaDoc value = input.getValue();
226       log_.debug("input value : " + value);
227       try {
228         variables.put(name, format.parseObject(value));
229       } catch (ParseException JavaDoc e) {
230         log_.error("can not format object : " + value, e);
231       }
232     }
233     return variables;
234   }
235
236
237   private static Form.Attribute extractAttribute(List list, String JavaDoc name) {
238     for (Iterator iterator = list.iterator(); iterator.hasNext();) {
239       Form.Attribute attribute = (Form.Attribute) iterator.next();
240       if (attribute.getName().equals(name))
241         return attribute;
242     }
243     return null;
244   }
245
246   public void setIsStart(boolean b) {
247     isStart = b;
248   }
249
250
251 }
252
Popular Tags