View Javadoc

1   package org.apache.fulcrum.osworkflow.example.modules.actions;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import java.util.Collections;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.fulcrum.osworkflow.WorkflowInstance;
29  import org.apache.fulcrum.osworkflow.WorkflowService;
30  import org.apache.fulcrum.osworkflow.WorkflowServiceFacade;
31  import org.apache.turbine.modules.actions.VelocityAction;
32  import org.apache.turbine.util.RunData;
33  import org.apache.velocity.context.Context;
34  
35  //import com.opensymphony.module.user.Group;
36  //import com.opensymphony.module.user.User;
37  //import com.opensymphony.module.user.UserManager;
38  import com.opensymphony.workflow.Workflow;
39  /***
40   * This action contains all the manipulations of a workflow.  Look at the various
41   * doXXX methods to see what actions can be performed on workflows via this Action
42   * class.
43   *
44   * @author     <a href="mailto:epugh@upstate.com">Eric Pugh</a>
45   */
46  public class WorkflowAction extends VelocityAction
47  {
48    /*** Logger to use */
49      private static Log log = LogFactory.getLog(WorkflowAction.class);
50  
51      /*** The workflowService that will be lazy loaded.  */
52      private WorkflowService workflowService;
53  
54      /***
55       *  This guy deals with actions related to workflows.
56       *
57       * @param  data           Current RunData information
58       * @param  context        Context to populate
59       * @exception  Exception  Thrown on error
60       */
61      public void doPerform(RunData data, Context context) throws Exception
62      {
63          log.debug("doPerform action event called");
64          data.setScreenTemplate("Index.vm");
65      }
66  
67      /***
68       *  This logs you in as user "test".  This is required because
69       * the example workflow can only be executed by user "test"
70       *
71       * @param  data           Current RunData information
72       * @param  context        Context to populate
73       * @exception  Exception  Thrown on error
74       */
75      public void doLogin(RunData data, Context context) throws Exception
76      {
77          log.debug("doLogin action event called");
78          data.getUser().setName("test");
79  
80          data.getMessages().setMessage(
81              "",
82              "INFO",
83              "You are logged in as user test!");
84      }
85  
86      /***
87       *  This sets up the user "test/test" in OSWorkflow.  This is
88       * required because the example workflow only allows user test
89       * to create it!  Also, it makes decisions based on the user
90       * test being in groups "foo","bars", and "bazs".
91       *
92       * @param  data           Current RunData information
93       * @param  context        Context to populate
94       * @exception  Exception  Thrown on error
95       */
96      public void doSetupuser(RunData data, Context context) throws Exception
97      {
98          log.debug("doSetupuser action event called");
99          data.setScreenTemplate("Index.vm");
100        /* UserManager um = UserManager.getInstance();
101         User test = um.createUser("test");
102         test.setPassword("test");
103         Group foos = um.createGroup("foos");
104         Group bars = um.createGroup("bars");
105         Group bazs = um.createGroup("bazs");
106         test.addToGroup(foos);
107         test.addToGroup(bars);
108         test.addToGroup(bazs);
109 
110         data.getMessages().setMessage(
111             "",
112             "INFO",
113             "User test/test is setup in system.  Don't forget to login!");
114             */
115         data.getMessages().setMessage(
116                 "",
117                 "INFO",
118                 "Please uncomment the code in WorkflowAction.doSetupuser and add OSUser!");
119     }
120 
121     /***
122      *  Create a new Workflow instance.
123      *
124      * @param  data           Current RunData information
125      * @param  context        Context to populate
126      * @exception  Exception  Thrown on error
127      */
128     public void doNew(RunData data, Context context) throws Exception
129     {
130         log.debug("doNew action event called");
131         data.setScreenTemplate("Index.vm");
132         try
133         {
134             Workflow wf =
135 			WorkflowServiceFacade.retrieveWorkflow(data.getUser().getName());
136             long id = wf.initialize("example", 1, null);
137             data.getMessages().setMessage(
138                 "",
139                 "INFO",
140                 "New Workflow id " + id + " created and initialized!");
141         }
142         catch (Exception e)
143         {
144             log.error(e);
145             data.getMessages().setMessage("", "ERROR", e.getMessage());
146         }
147     }
148     /***
149     *  View the details of a specific workflow instance.
150     *
151     * @param  data           Current RunData information
152     * @param  context        Context to populate
153     * @exception  Exception  Thrown on error
154     */
155     public void doViewdetail(RunData data, Context context) throws Exception
156     {
157         log.debug("doViewdetail action event called");
158         data.setScreenTemplate("WorkflowDetail.vm");
159         try
160         {
161             context.put("wf", getWorkflowInstance(data, context));
162         }
163         catch (Exception e)
164         {
165             log.error(e);
166             data.getMessages().setMessage("", "ERROR", e.getMessage());
167         }
168     }
169 
170     /***
171     *  Perform an action for a workflow that moves it from one state
172     * to the next.
173     *
174     * @param  data           Current RunData information
175     * @param  context        Context to populate
176     * @exception  Exception  Thrown on error
177     */
178     public void doAction(RunData data, Context context) throws Exception
179     {
180         log.debug("doAction action event called");
181         try
182         {
183             WorkflowInstance wf = getWorkflowInstance(data, context);
184             int action = data.getParameters().getInt("actionId");
185             wf.doAction(action, Collections.EMPTY_MAP);
186 
187         }
188         catch (Exception e)
189         {
190             log.error(e);
191             data.getMessages().setMessage("", "ERROR", e.getMessage());
192         }
193         doViewdetail(data, context);
194     }
195 
196     /***
197      * Look up a workflow by the parameter "id".  Creates a workflow
198      * instance object to facilitate looking things up.
199      *
200      * @param  data           Current RunData information
201      * @param  context        Context to populate
202      * @return a populated workflow instance
203      */
204     protected WorkflowInstance getWorkflowInstance(
205         RunData data,
206         Context context)
207     {
208         long workflowId = data.getParameters().getLong("id");
209         Workflow workflow =
210             WorkflowServiceFacade.retrieveWorkflow(data.getUser().getName());
211         WorkflowInstance wf = new WorkflowInstance(workflow, workflowId);
212         return wf;
213     }
214 }