View Javadoc

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.beans.Introspector;
26  import java.beans.PropertyDescriptor;
27  import java.lang.reflect.Method;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import javax.servlet.ServletRequest;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import net.smartlab.web.DataAccessObject.SearchInfo;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionMapping;
41  import org.apache.struts.action.DynaActionForm;
42  import org.apache.struts.util.RequestUtils;
43  
44  
45  /**
46   * This is the base action for generic archive management. The class provides
47   * template methods for common archive management operations and provides
48   * support methods to handle common problems like multiple selections and search
49   * criteria.
50   * 
51   * @author rlogiacco
52   * @uml.dependency supplier="net.smartlab.web.EmptyMarkerTag"
53   * @uml.dependency supplier="net.smartlab.web.Validator"
54   */
55  public abstract class AbstractArchiveAction extends DynaAction {
56  
57  	private final static Log LOGGER = LogFactory.getLog(AbstractArchiveAction.class);
58  	/**
59  	 * The standard name for a multiple selection request parameter.
60  	 */
61  	public final static String SELECTION = "selection";
62  
63  
64  	/**
65  	 * This action method should be invoked to list items matching a set of
66  	 * specified criteria.
67  	 * 
68  	 * @param form the html form submitted with this request.
69  	 * @param request the user request.
70  	 * @param response the representation of the response channel.
71  	 * @param mapping the system control mapping.
72  	 * @return the name of a defined global or local forward.
73  	 * @throws Exception if something unexpected happend during the request
74  	 *         execution.
75  	 */
76  	public abstract String search(ActionForm form, HttpServletRequest request, HttpServletResponse response,
77  			ActionMapping mapping) throws Exception;
78  
79  	/**
80  	 * This action method should be invoked to retrieve a specific item.
81  	 * 
82  	 * @param form the html form submitted with this request.
83  	 * @param request the user request.
84  	 * @param response the representation of the response channel.
85  	 * @param mapping the system control mapping.
86  	 * @return the name of a defined global or local forward.
87  	 * @throws Exception if something unexpected happend during the request
88  	 *         execution.
89  	 */
90  	public abstract String select(ActionForm form, HttpServletRequest request, HttpServletResponse response,
91  			ActionMapping mapping) throws Exception;
92  
93  	/**
94  	 * This action method should be invoked to save or update an item.
95  	 * 
96  	 * @param form the html form submitted with this request.
97  	 * @param request the user request.
98  	 * @param response the representation of the response channel.
99  	 * @param mapping the system control mapping.
100 	 * @return the name of a defined global or local forward.
101 	 * @throws Exception if something unexpected happend during the request
102 	 *         execution.
103 	 */
104 	public abstract String update(ActionForm form, HttpServletRequest request, HttpServletResponse response,
105 			ActionMapping mapping) throws Exception;
106 
107 	/**
108 	 * This action method should be invoked to permanently remove an item.
109 	 * 
110 	 * @param form the html form submitted with this request.
111 	 * @param request the user request.
112 	 * @param response the representation of the response channel.
113 	 * @param mapping the system control mapping.
114 	 * @return the name of a defined global or local forward.
115 	 * @throws Exception if something unexpected happend during the request
116 	 *         execution.
117 	 */
118 	public abstract String remove(ActionForm form, HttpServletRequest request, HttpServletResponse response,
119 			ActionMapping mapping) throws Exception;
120 
121 	/**
122 	 * This action method should be invoked to permanently remove a set of
123 	 * items.
124 	 * 
125 	 * @param form the html form submitted with this request.
126 	 * @param request the user request.
127 	 * @param response the representation of the response channel.
128 	 * @param mapping the system control mapping.
129 	 * @return the name of a defined global or local forward.
130 	 * @throws Exception if something unexpected happend during the request
131 	 *         execution.
132 	 */
133 	public abstract String removeAll(ActionForm form, HttpServletRequest request, HttpServletResponse response,
134 			ActionMapping mapping) throws Exception;
135 
136 	/**
137 	 * TODO documentation
138 	 * 
139 	 * @param request the user request.
140 	 * @return
141 	 */
142 	public static String[] getListSelection(ServletRequest request) {
143 		return request.getParameterValues("selection");
144 	}
145 
146 	/**
147 	 * TODO documentation
148 	 * 
149 	 * @param request the user request.
150 	 * @param form
151 	 * @param name
152 	 * @return
153 	 */
154 	public static String[] getSelection(ActionForm form, String name) {
155 		String[] values = null;
156 		if (form instanceof DynaActionForm) {
157 			values = (String[])((DynaActionForm)form).get(name);
158 		} else {
159 			try {
160 				PropertyDescriptor[] descriptors = Introspector.getBeanInfo(form.getClass()).getPropertyDescriptors();
161 				for (int i = 0; i < descriptors.length; i++) {
162 					if (descriptors[i].getName().equals(name)) {
163 						values = (String[])descriptors[i].getReadMethod().invoke(form, null);
164 						break;
165 					}
166 				}
167 			} catch (Exception e) {
168 				LOGGER.warn("getSelection()", e);
169 			}
170 		}
171 		if (values.length == 0) {
172 			return null;
173 		} else if (values.length == 1 && EmptyMarkerTag.EMPTY_MARKER.equals(values[0])) {
174 			return new String[0];
175 		} else {
176 			List selection = new ArrayList();
177 			for (int i = 0; i < values.length; i++) {
178 				if (!EmptyMarkerTag.EMPTY_MARKER.equals(values[i])) {
179 					selection.add(values[i]);
180 				}
181 			}
182 			return (String[])selection.toArray(new String[selection.size()]);
183 		}
184 	}
185 
186 	/**
187 	 * TODO documentation
188 	 * 
189 	 * @param form
190 	 * @param name
191 	 * @param values
192 	 */
193 	public static void setSelection(ActionForm form, String name, String[] values) {
194 		if (form instanceof DynaActionForm) {
195 			((DynaActionForm)form).set(name, values);
196 		} else {
197 			try {
198 				PropertyDescriptor[] descriptors = Introspector.getBeanInfo(form.getClass()).getPropertyDescriptors();
199 				for (int i = 0; i < descriptors.length; i++) {
200 					if (descriptors[i].getName().equals(name)) {
201 						Method setter = descriptors[i].getWriteMethod();
202 						Class[] types = setter.getParameterTypes();
203 						if (types.length == 1 && types[0].isArray() && types[0] == String.class) {
204 							setter.invoke(form, values);
205 							break;
206 						}
207 					}
208 				}
209 			} catch (Exception e) {
210 				LOGGER.warn("getSelection()", e);
211 			}
212 		}
213 	}
214 
215 	/**
216 	 * TODO documentation
217 	 * 
218 	 * @param form
219 	 * @param name
220 	 */
221 	public static void resetSelection(ActionForm form, String name) {
222 		if (form instanceof DynaActionForm) {
223 			((DynaActionForm)form).set(name, new String[0]);
224 		} else {
225 			try {
226 				PropertyDescriptor[] descriptors = Introspector.getBeanInfo(form.getClass()).getPropertyDescriptors();
227 				for (int i = 0; i < descriptors.length; i++) {
228 					if (descriptors[i].getName().equals(name)) {
229 						Method setter = descriptors[i].getWriteMethod();
230 						Class[] types = setter.getParameterTypes();
231 						if (types.length == 1 && types[0].isArray() && types[0] == String.class) {
232 							setter.invoke(form, new String[0]);
233 							break;
234 						}
235 					}
236 				}
237 			} catch (Exception e) {
238 				LOGGER.warn("getSelection()", e);
239 			}
240 		}
241 	}
242 
243 	/**
244 	 * Parses the request searching for standard filtering and ordering
245 	 * parameters.
246 	 * 
247 	 * @param request the received request.
248 	 * @return filtering and ordering parameters.
249 	 */
250 	public static SearchInfo getSearchInfo(HttpServletRequest request) {
251 		SearchInfo info = new SearchInfo();
252 		info.setLocale(RequestUtils.getUserLocale(request, null));
253 		info.setFilters(request.getParameterValues("filter"));
254 		info.setUnion(request.getParameter("union"));
255 		info.setOrder(request.getParameter("order"));
256 		return info;
257 	}
258 }