1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
52
53
54
55 public abstract class AbstractArchiveAction extends DynaAction {
56
57 private final static Log LOGGER = LogFactory.getLog(AbstractArchiveAction.class);
58
59
60
61 public final static String SELECTION = "selection";
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public abstract String search(ActionForm form, HttpServletRequest request, HttpServletResponse response,
77 ActionMapping mapping) throws Exception;
78
79
80
81
82
83
84
85
86
87
88
89
90 public abstract String select(ActionForm form, HttpServletRequest request, HttpServletResponse response,
91 ActionMapping mapping) throws Exception;
92
93
94
95
96
97
98
99
100
101
102
103
104 public abstract String update(ActionForm form, HttpServletRequest request, HttpServletResponse response,
105 ActionMapping mapping) throws Exception;
106
107
108
109
110
111
112
113
114
115
116
117
118 public abstract String remove(ActionForm form, HttpServletRequest request, HttpServletResponse response,
119 ActionMapping mapping) throws Exception;
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public abstract String removeAll(ActionForm form, HttpServletRequest request, HttpServletResponse response,
134 ActionMapping mapping) throws Exception;
135
136
137
138
139
140
141
142 public static String[] getListSelection(ServletRequest request) {
143 return request.getParameterValues("selection");
144 }
145
146
147
148
149
150
151
152
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
188
189
190
191
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
217
218
219
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
245
246
247
248
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 }