KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > function > InfoglueFunction


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 package org.infoglue.cms.applications.workflowtool.function;
24
25 import java.util.Locale JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.infoglue.cms.applications.common.Session;
32 import org.infoglue.cms.applications.workflowtool.util.InfoglueWorkflowBase;
33 import org.infoglue.cms.security.InfoGluePrincipal;
34
35 import com.opensymphony.module.propertyset.PropertySet;
36 import com.opensymphony.workflow.FunctionProvider;
37 import com.opensymphony.workflow.WorkflowException;
38
39 /**
40  * Base class for all infoglue workflow functions.
41  */

42 public abstract class InfoglueFunction extends InfoglueWorkflowBase implements FunctionProvider
43 {
44     private final static Logger logger = Logger.getLogger(InfoglueFunction.class.getName());
45
46     /**
47      * The key used by the <code>request</code> in the <code>parameters</code>.
48      */

49     private static final String JavaDoc REQUEST_PARAMETER = "request";
50     
51     /**
52      * The key used by the <code>principal</code> in the <code>parameters</code>.
53      */

54     public static final String JavaDoc PRINCIPAL_PARAMETER = "principal";
55     
56     /**
57      * The key used by the <code>locale</code> in the <code>parameters</code>.
58      */

59     public static final String JavaDoc LOCALE_PARAMETER = "locale";
60     
61     /**
62      * The locale associated with the current session.
63      */

64     private Locale JavaDoc locale;
65     
66     /**
67      * The caller of the workflow.
68      */

69     private InfoGluePrincipal principal;
70     
71     /**
72      * Default constructor.
73      */

74     public InfoglueFunction()
75     {
76         super();
77     }
78
79     /**
80      * Executes this function.
81      *
82      * @param transientVars the transient variables of the current execution context.
83      * @param args the arguments of the function.
84      * @param ps the propertyset associated with the current workflow.
85      * @throws WorkflowException if an error occurs during the execution.
86      */

87     public final void execute(final Map JavaDoc transientVars, final Map JavaDoc args, final PropertySet ps) throws WorkflowException
88     {
89         try
90         {
91             storeContext(transientVars, args, ps);
92             logger.debug(getClass().getName() + ".execute()--------- START");
93             initialize();
94             execute();
95             logger.debug(getClass().getName() + ".execute()--------- STOP");
96         }
97         catch(Exception JavaDoc e)
98         {
99             throwException(e);
100         }
101     }
102     
103     /**
104      * Method used for initializing the function; will be called before <code>execute</code> is called.
105      * <p><strong>Note</strong>! You must call <code>super.initialize()</code> first.</p>
106      *
107      * @throws WorkflowException if an error occurs during the initialization.
108      */

109     protected void initialize() throws WorkflowException
110     {
111         super.initialize();
112         initializeLocale();
113         initializePrincipal();
114     }
115     
116     /**
117      * Determine the locale associated with the current session. If a locale is found in the
118      * <code>parameters</code>, that locale will be used. Otherwise the locale will be taken
119      * from the <code>session</code>.
120      *
121      * @throws WorkflowException if an error occurs during the initialization.
122      */

123     private void initializeLocale() throws WorkflowException
124     {
125         if(parameterExists(LOCALE_PARAMETER))
126         {
127             locale = (Locale JavaDoc) getParameter(LOCALE_PARAMETER);
128         }
129         else
130         {
131             locale = getSession().getLocale();
132         }
133     }
134     
135     /**
136      * Determine the caller of the workflow. If a principal is found in the
137      * <code>parameters</code>, that principal will be used. Otherwise the principal will be taken
138      * from the <code>session</code>.
139      *
140      * @throws WorkflowException if an error occurs during the initialization.
141      */

142     private void initializePrincipal() throws WorkflowException
143     {
144         if(parameterExists(PRINCIPAL_PARAMETER))
145         {
146             principal = (InfoGluePrincipal) getParameter(PRINCIPAL_PARAMETER);
147         }
148         else
149         {
150             principal = getSession().getInfoGluePrincipal();
151         }
152     }
153     
154     /**
155      * Returns the session associated with the current execution.
156      */

157     private Session getSession() throws WorkflowException
158     {
159         return new Session(((HttpServletRequest JavaDoc) getParameter(REQUEST_PARAMETER)).getSession());
160     }
161     
162     /**
163      * Executes this function.
164      *
165      * @throws WorkflowException if an error occurs during the execution.
166      */

167     protected abstract void execute() throws WorkflowException;
168     
169     /**
170      * @todo : is this really needed?
171      */

172     protected final String JavaDoc getRequestParameter(final String JavaDoc key)
173     {
174         Object JavaDoc value = getParameters().get(key);
175         if(value != null && value.getClass().isArray())
176         {
177             final String JavaDoc[] values = (String JavaDoc[]) value;
178             value = (values.length == 1) ? values[0] : null;
179         }
180         return (value == null) ? null : value.toString();
181     }
182     
183
184     /**
185      * Returns the locale associated with the current session.
186      *
187      * @return the locale associated with the current session.
188      */

189     protected final Locale JavaDoc getLocale() throws WorkflowException
190     {
191         return locale;
192     }
193     
194     /**
195      * Returns the caller of the workflow.
196      *
197      * @return the caller of the workflow.
198      */

199     protected final InfoGluePrincipal getPrincipal()
200     {
201         return principal;
202     }
203     
204     /**
205      *
206      *
207      * @param status
208      */

209     protected final void setFunctionStatus(final String JavaDoc status)
210     {
211         logger.debug("setFunctionStatus(" + status + ")");
212         getPropertySet().setString(FUNCTION_STATUS_PROPERTYSET_KEY, status);
213     }
214 }
215
Popular Tags