Clover Coverage Report - SmartWeb
Coverage timestamp: Sun Jun 8 2008 21:20:12 CEST
../../../img/srcFileCovDistChart1.png 26% of files have more coverage
64   336   35   4,92
30   161   0,55   13
13     2,69  
1    
10,1% of code in this file is excluded from these metrics.
 
  Action       Line # 59 64 35 9,3% 0.093457945
 
  (2)
 
1    /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10    * This library is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this library; if not, write to the Free Software
17    * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18    *
19    * For further informations on the SmartWeb Framework please visit
20    *
21    * http://smartweb.sourceforge.net
22    */
23    package net.smartlab.web;
24   
25    import java.io.InputStream;
26    import java.util.ArrayList;
27    import java.util.Iterator;
28    import java.util.List;
29    import java.util.Locale;
30    import java.util.MissingResourceException;
31    import java.util.Properties;
32    import javax.servlet.ServletContext;
33    import javax.servlet.http.HttpServletRequest;
34    import javax.servlet.http.HttpServletResponse;
35    import net.smartlab.web.bean.Valorizer;
36    import org.apache.commons.beanutils.locale.LocaleBeanUtilsBean;
37    import org.apache.commons.beanutils.locale.LocaleConvertUtilsBean;
38    import org.apache.commons.logging.Log;
39    import org.apache.commons.logging.LogFactory;
40    import org.apache.struts.Globals;
41    import org.apache.struts.action.ActionErrors;
42    import org.apache.struts.action.ActionForm;
43    import org.apache.struts.action.ActionForward;
44    import org.apache.struts.action.ActionMapping;
45    import org.apache.struts.action.ActionMessage;
46    import org.apache.struts.action.ActionMessages;
47    import org.apache.struts.action.DynaActionForm;
48   
49    /**
50    * This class represents the <i>controller </i> part of the <b>MVC </b> pattern.
51    * Extend this class to provide a specific set of operations to be performed in
52    * response of a user selection.
53    *
54    * @author rlogiacco
55    * @see org.apache.struts.action.Action
56    * @uml.dependency supplier="net.smartlab.web.ActionException"
57    * @uml.dependency supplier="net.smartlab.web.bean.Valorizer"
58    */
 
59    public abstract class Action extends org.apache.struts.action.Action {
60   
61    /**
62    * Identifies the default resource the request will be forwarded.
63    */
64    public final static ActionForward DEFAULT_FORWARD = new ActionForward("default");
65   
66    /**
67    * Provides logging capabilities to the action.
68    */
69    protected final Log logger = LogFactory.getLog(this.getClass());
70   
71   
72    /**
73    * This method performs some common operations then redirects control to the
74    * abstract <code>execute</code>.
75    *
76    * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
77    * org.apache.struts.action.ActionForm,
78    * javax.servlet.http.HttpServletRequest,
79    * javax.servlet.http.HttpServletResponse)
80    */
 
81  0 toggle public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
82    HttpServletResponse response) throws Exception {
83  0 if (logger.isDebugEnabled()) {
84  0 logger.debug("execute(" + mapping.getPath() + ") - start");
85    }
86  0 ActionForward forward = null;
87  0 if (super.isCancelled(request)) {
88  0 if (logger.isDebugEnabled()) {
89  0 logger.debug("execute(" + mapping.getPath() + ") - cancel");
90    }
91  0 forward = this.cancel(form, request, response, mapping);
92  0 if (forward == DEFAULT_FORWARD) {
93  0 return mapping.getInputForward();
94    } else {
95  0 return forward;
96    }
97    }
98  0 forward = this.execute(form, request, response, mapping);
99  0 try {
100  0 if (request.getAttribute(Globals.ERROR_KEY) != null && forward == DEFAULT_FORWARD) {
101  0 if (logger.isDebugEnabled()) {
102  0 logger.debug("execute(" + mapping.getPath() + ") - errors");
103  0 if (logger.isTraceEnabled()) {
104  0 logger.trace(" " + request.getAttribute(Globals.ERROR_KEY));
105    }
106    }
107  0 return mapping.getInputForward();
108    }
109  0 return forward;
110    } finally {
111  0 if (logger.isTraceEnabled()) {
112  0 logger.trace(" forward = " + forward);
113    }
114    }
115    }
116   
117    /**
118    * Implement this method to provide a custom response to a user input.
119    *
120    * @param form the html form submitted with this request.
121    * @param request the user request.
122    * @param response the representation of the response channel.
123    * @param mapping the system control mapping.
124    * @return the name of a defined global or local forward.
125    * @throws Exception if something unexpected happend during the request
126    * execution.
127    */
128    protected abstract ActionForward execute(ActionForm form, HttpServletRequest request, HttpServletResponse response,
129    ActionMapping mapping) throws Exception;
130   
131    /**
132    * Describes the operations sequence to be performed upon cancellation of a
133    * form or wizard returning the mapping to redirect to. By default this
134    * method simply does nothing more than redirecting to the
135    * <code>input</code> path.
136    *
137    * @param form the html form submitted with this request.
138    * @param request the user request.
139    * @param response the representation of the response channel.
140    * @param mapping the system control mapping.
141    * @return the name of a defined global or local forward.
142    * @throws Exception if something unexpected happend during the request
143    * execution.
144    */
 
145  0 toggle protected ActionForward cancel(ActionForm form, HttpServletRequest request, HttpServletResponse response,
146    ActionMapping mapping) throws Exception {
147  0 return DEFAULT_FORWARD;
148    }
149   
150    /**
151    * Valorizes a <code>bean</code> instance with the values providen with
152    * the user submitted html form. This method uses an improved version of the
153    * introspection paradigm to discover properties bindings.
154    *
155    * @param form the user submitted html form containing the values to be
156    * read.
157    * @param bean the bean instance to be valorized.
158    * @param locale the user locale for values parsing.
159    * @throws ActionException if the valorization fails, usually due to
160    * inconsistencies between the bean and the submitted form.
161    */
 
162  1 toggle protected void valorize(ActionForm form, Object bean, Locale locale) throws ActionException {
163  1 try {
164  1 if (logger.isDebugEnabled()) {
165  0 logger.debug("valorize(" + form + ", " + bean + ") - start");
166    }
167    // FIXME
168    // this.getConverter(locale).copyProperties(bean, form);
169  1 Valorizer.copy(form, bean, locale);
170    } catch (Exception e) {
171    logger.error("valorize(" + form + ", " + bean + ") - error", e);
172    throw new ActionException("errors.valorize", e);
173    }
174    }
175   
176    /**
177    * Populates, or prevalorizes, an html form with the values of a
178    * <code>bean</code> instance . This method uses an improved version of
179    * the introspection paradigm to discover properties bindings.
180    *
181    * @param form the form to be populated.
182    * @param bean the bean instance containing the values to be written in the
183    * form.
184    * @param locale the user locale for values parsing.
185    * @throws ActionException if the valorization fails, usually due to
186    * inconsistencies between the bean and the submitted form.
187    */
 
188  1 toggle protected void populate(ActionForm form, Object bean, Locale locale) throws ActionException {
189  1 try {
190  1 if (logger.isDebugEnabled()) {
191  0 logger.debug("populate(" + form + ", " + bean + ") - start");
192    }
193    // FIXME
194    // this.getConverter(locale).copyProperties(form,bean);
195  1 Valorizer.copy(bean, form, locale);
196    } catch (Exception e) {
197    logger.error("populate(" + form + ", " + bean + ") - error", e);
198    throw new ActionException("errors.populate", e);
199    }
200    }
201   
202    /**
203    * @TODO documentation
204    * @param locale
205    * @return
206    */
 
207  0 toggle private LocaleBeanUtilsBean getConverter(Locale locale) {
208  0 LocaleConvertUtilsBean converter = new LocaleConvertUtilsBean();
209  0 converter.setDefaultLocale(locale);
210  0 return new LocaleBeanUtilsBean(converter);
211    }
212   
213    /**
214    * Ensures the specified html form has all its fields resetted to their
215    * initial value.
216    *
217    * @param form the form to be cleaned up.
218    * @param request the user request.
219    * @param mapping the system control mapping.
220    */
 
221  0 toggle public void reset(ActionForm form, HttpServletRequest request, ActionMapping mapping) {
222  0 if (form instanceof DynaActionForm) {
223  0 ((DynaActionForm)form).initialize(mapping);
224    } else {
225  0 form.reset(mapping, request);
226    }
227    }
228   
229    /**
230    * TODO documentation
231    *
232    * @param request
233    * @return
234    */
 
235  0 toggle protected boolean hasErrors(HttpServletRequest request) {
236  0 return request.getAttribute(Globals.ERROR_KEY) != null;
237    }
238   
239    /**
240    * TODO documentation
241    *
242    * @param message
243    * @param request
244    */
 
245  0 toggle protected void addError(ActionMessage message, HttpServletRequest request) {
246  0 this.addError(ActionMessages.GLOBAL_MESSAGE, message, request);
247    }
248   
249    /**
250    * TODO documentation
251    *
252    * @param property
253    * @param message
254    * @param request
255    */
 
256  0 toggle protected void addError(String property, ActionMessage message, HttpServletRequest request) {
257  0 ActionErrors errors = (ActionErrors)request.getAttribute(Globals.ERROR_KEY);
258  0 if (errors == null) {
259  0 errors = new ActionErrors();
260  0 request.setAttribute(Globals.ERROR_KEY, errors);
261    }
262  0 errors.add(property, message);
263    }
264   
265    /**
266    * TODO documentation
267    *
268    * @param name
269    * @param request
270    * @return
271    */
 
272  0 toggle public boolean isChecked(String name, HttpServletRequest request) {
273  0 if ("on".equalsIgnoreCase(request.getParameter("group"))
274    || "true".equalsIgnoreCase(request.getParameter("group"))
275    || "yes".equalsIgnoreCase(request.getParameter("group"))) {
276  0 return true;
277    } else {
278  0 return false;
279    }
280    }
281   
282    /**
283    * TODO documentation
284    *
285    * @return
286    */
 
287  0 toggle protected ServletContext getServletContext() {
288  0 return super.getServlet().getServletContext();
289    }
290   
291    /**
292    * TODO documentation
293    *
294    * @param path
295    * @return
296    */
 
297  0 toggle protected String getRealPath(String path) {
298  0 return this.getServletContext().getRealPath(path);
299    }
300   
301    /**
302    * TODO documentation
303    *
304    * @return
305    */
 
306  0 toggle protected Properties getProperties() {
307  0 Class type = this.getClass();
308  0 List inputs = new ArrayList();
309  0 Properties result = new Properties();
310  0 while (type.getSuperclass().isAssignableFrom(Action.class)) {
311  0 try {
312  0 InputStream in = this.getClass().getResourceAsStream(this.getClass().getName() + ".prop");
313  0 inputs.add(in);
314    } catch (Exception e) {
315    if (logger.isDebugEnabled()) {
316    logger.debug("getProperties() - descend", e);
317    }
318    }
319  0 type = type.getSuperclass();
320    }
321  0 Iterator iterator = inputs.iterator();
322  0 while (iterator.hasNext()) {
323  0 try {
324  0 InputStream in = (InputStream)iterator.next();
325  0 Properties properties = new Properties();
326  0 properties.load(in);
327  0 result.putAll(properties);
328    } catch (Exception e) {
329    if (logger.isDebugEnabled()) {
330    logger.debug("getProperties() - load", e);
331    }
332    }
333    }
334  0 throw new MissingResourceException("No properties defined for action", this.getClass().getName(), null);
335    }
336    }