KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > commands > AbstractCmd


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.commands;
20
21 import java.util.*;
22
23 import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
24 import org.openharmonise.rm.PopulateException;
25 import org.openharmonise.rm.logging.*;
26 import org.openharmonise.rm.publishing.State;
27 import org.openharmonise.rm.resources.AbstractEditableObject;
28 import org.openharmonise.rm.resources.users.User;
29 import org.openharmonise.rm.security.authorization.AuthorizationValidator;
30 import org.openharmonise.rm.workflow.WorkflowLogEvent;
31
32
33 /**
34  * An abstract class which provides the base functionality and sets the main
35  * interface for a class which can execute an operation with in the Harmonise
36  * framework, possibly on a specified 'command object'. Role based security
37  * and event logging are implemented in Harmonise through the command objects.
38  *
39  * @author Michael Bell
40  * @version $Revision: 1.4 $
41  *
42  */

43 public abstract class AbstractCmd {
44     
45     private static final String JavaDoc PARAM_INPUT = "input";
46     private static final String JavaDoc PARAM_RESULT = "result";
47     /**
48      * User executing the command
49      */

50     
51     private User m_commandUser = null;
52     
53     /**
54      * The <code>State</code> to be used for the context of the execution of the command
55      */

56     private State m_state = null;
57     /**
58      * Tag name for the element which represents a command
59      */

60     public static final String JavaDoc TAG_COMMAND = "Event";
61     
62     /**
63      * Tag name for the 'Parameter' element, used to specify parameters to the command
64      */

65     public static final String JavaDoc TAG_COMMAND_PARAM = "Parameter";
66
67     /**
68      * Template id parameter name
69      */

70     public final static String JavaDoc PARAM_TEMPLATE_ID = "form_id";
71     
72     /**
73      * Output text parameter name
74      */

75     public final static String JavaDoc PARAM_OUT_TEXT = "out_text";
76     
77     /**
78      * Object this command shall execute upon
79      */

80     protected Object JavaDoc m_commandObj = null;
81     
82     /**
83      * Map of paramter names to values for this command
84      */

85     protected Map m_cmd_params;
86     
87     /**
88      * Data store interface for this command
89      */

90     private AbstractDataStoreInterface m_dsi = null;
91
92     /**
93      * Creates new instance of a command object
94      *
95      */

96     public AbstractCmd() {
97     }
98
99     /**
100      * Excutes command, on command object given if appropriate, and will return the
101      * result of the execution. If there is no result from the execution of the
102      * command it will return a <code>null</code>.
103      * @param context
104      *
105      * @return the result of the execution of the command
106      * @throws CommandException if any errors occur
107      */

108     public abstract Object JavaDoc execute(Context context) throws CommandException;
109
110     /**
111      * Returns the name of the command
112      *
113      * @return Name of the command
114      */

115     public abstract String JavaDoc getName();
116
117     /**
118      * Sets the object on which the command will be executed
119      *
120      * @param obj object on which the command will be executed
121      */

122     public void setCommandObject(Object JavaDoc obj) {
123         m_commandObj = obj;
124     }
125
126     /**
127      * Returns the object which this command will be executed on, if appropriate.
128      * @param context
129      *
130      * @return the object which this command will be executed on, if appropriate.
131      */

132     public Object JavaDoc getCommandObject(Context context) {
133         
134         Object JavaDoc cmdObj = m_commandObj;
135         
136         String JavaDoc sInput = (String JavaDoc) getParameter(PARAM_INPUT);
137         
138         if(sInput != null && context != null) {
139             cmdObj = context.getContextParameter(sInput);
140         }
141         
142         return cmdObj;
143     }
144
145     /**
146      * Returns the user executing the commands from the <code>State</code>,
147      * if a <code>State</code> has been given to this command.
148      *
149      * @return the user executing the commands from the <code>State</code>
150      */

151     public User getExecutingUser() {
152         User usr = null;
153
154         if (m_state != null) {
155             usr = m_state.getLoggedInUser();
156         }
157
158         return usr;
159     }
160
161     /**
162      * Adds a <code>State</code> object to this command, providing a context for the command
163      * to execute in.
164      *
165      * @param state the state/context for the execution of the command
166      */

167     public void setState(State state) {
168         m_state = state;
169     }
170
171     /**
172      * Returns the <code>State</code> associated to this command.
173      *
174      * @return the <code>State</code> associated to this command.
175      */

176     public State getState() {
177         return m_state;
178     }
179
180     /**
181      * Sets up an interface to the DB for this command.
182      *
183      * @param dsi the data store interface
184      */

185     public void setDataStoreInteface(AbstractDataStoreInterface dsi) {
186         m_dsi = dsi;
187     }
188
189     /**
190      * Returns the data store interface associated to this command.
191      *
192      * @return the data store interface associated to this command.
193      */

194     public AbstractDataStoreInterface getDataStoreInteface() {
195         return m_dsi;
196     }
197
198     /**
199      * Sets the parameters for this command.
200      *
201      * @param cmd_params the parameters for this command
202      */

203     public void setParameters(Map cmd_params) {
204         m_cmd_params = cmd_params;
205     }
206
207     /**
208      * Returns the list of values for the parameter of the specified parameter
209      * name .
210      *
211      * @param parameter_name the parameter name
212      * @return the list of values for the parameter
213      */

214     public List getParameters(String JavaDoc parameter_name) {
215         List param = null;
216         
217         if(m_cmd_params != null) {
218             param = (List) m_cmd_params.get(parameter_name);
219         }
220         
221         return param;
222     }
223
224     /**
225      * Returns the first value of the parameter of the specified parameter
226      * name
227      *
228      * @param parameter_name the parameter name
229      * @return the first value of the parameter
230      */

231     public String JavaDoc getParameter(String JavaDoc parameter_name) {
232         String JavaDoc sParam = null;
233         
234         if(m_cmd_params != null) {
235             List parameter_values = (List) m_cmd_params.get(parameter_name);
236
237             if ((parameter_values != null) && (parameter_values.size() > 1)) {
238                 throw new RuntimeException JavaDoc(
239                     "Call getParameters if parameter has multiple values"
240                         + " parameter_name:"
241                         + parameter_name
242                         + ", returns :"
243                         + parameter_values);
244             }
245
246             sParam = (parameter_values == null) ? null : (String JavaDoc) parameter_values.get(0);
247         }
248         
249         return sParam;
250     }
251
252     /**
253      * Returns <code>true</code> if this command is valid for the given object.
254      *
255      * @param obj
256      * @return
257      */

258     public abstract boolean isValidCommandObject(Object JavaDoc obj);
259
260     /**
261      * Logs the command with the Harmonise logger.
262      * @param context TODO
263      *
264      * @throws CommandException if any errors occur logging the command
265      */

266     protected void logCommand(Context context) throws CommandException {
267         try {
268             EventLogController logger = EventLogController.getInstance();
269
270             WorkflowLogEvent event = new WorkflowLogEvent();
271
272             event.setEventObject(getCommandObject(context));
273             event.setState(getState());
274             event.setLabel(getName());
275
276             logger.logEvent(event);
277         } catch (LogException e) {
278             throw new CommandException("Error occured logging event", e);
279         } catch (PopulateException e) {
280             throw new CommandException(
281                 "Error occured setting state for logging event",
282                 e);
283         }
284     }
285
286     /**
287      * Returns <code>true</code> if this command is available to the user
288      * executing the command.
289      * @param context TODO
290      *
291      * @return <code>true</code> if this command is available to the user
292      * executing the command
293      * @throws InvalidCommandException if an error occurs
294      */

295   public boolean isAvailable(Context context) throws InvalidCommandException {
296     User usr = getExecutingUser();
297     
298     return isAvailable(usr,(AbstractEditableObject) getCommandObject(context));
299   }
300   
301     /**
302      * Returns <code>true</code> if this command is available to the
303      * specified user for the specified object
304      *
305      * @param usr the User
306      * @return <code>true</code> if this command is available to the
307      * specified user
308      * @throws InvalidCommandException if an error occurs
309      */

310     public boolean isAvailable(User usr,AbstractEditableObject obj) throws InvalidCommandException {
311         boolean bIsAvailable = false;
312         
313         if(usr == null) {
314           throw new InvalidCommandException("Command is not valid with out specifying a user");
315         }
316
317         try {
318           bIsAvailable = AuthorizationValidator.isCommandAvailable(usr, (AbstractEditableObject) obj, getName());
319         } catch (org.openharmonise.rm.security.authorization.AuthorizationException e1) {
320           throw new InvalidCommandException("Command is not available for this object:" + m_commandObj.getClass(), e1);
321         }
322         
323         return bIsAvailable;
324     }
325     
326     /**
327      * Returns <code>true</code> if this command is available to the
328      * specified user for the specified <code>Class</code>.
329      *
330      * @param usr the User
331      * @param objClass class to be tested
332      * @return <code>true</code> if this command is available to the
333      * specified user
334      * @throws InvalidCommandException if an error occurs
335      */

336     public boolean isAvailable(User usr,Class JavaDoc objClass) throws InvalidCommandException {
337         boolean bIsAvailable = false;
338         
339         if(usr == null) {
340           throw new InvalidCommandException("Command is not valid with out specifying a user");
341         }
342
343         try {
344           bIsAvailable = AuthorizationValidator.isCommandAvailable(usr, objClass, getName());
345         } catch (org.openharmonise.rm.security.authorization.AuthorizationException e1) {
346           throw new InvalidCommandException("Command is not available for this object:" + m_commandObj.getClass(), e1);
347         }
348         
349         return bIsAvailable;
350     }
351     
352     /**
353      * Adds the result to the context if the 'result' parameter
354      * exists to give the result a name in the context
355      *
356      * @param result
357      * @param context
358      */

359     protected void addResultContext(Object JavaDoc result,Context context) {
360         String JavaDoc sResult = getParameter(PARAM_RESULT);
361         
362         if(sResult != null) {
363             context.addContextParameter(sResult, result);
364         }
365     }
366 }
Popular Tags