1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.controller;
17
18 import java.util.Iterator;
19 import java.util.List;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.apache.commons.beanutils.DynaBean;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.ActionForward;
27 import org.apache.struts.action.ActionMapping;
28 import org.apache.struts.actions.MappingDispatchAction;
29 import org.apache.struts.config.FormBeanConfig;
30 import org.seasar.tuigwaa.database.DataTable;
31 import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
32 import org.seasar.tuigwaa.database.function.criteria.OrderCriteriaFunction;
33 import org.seasar.tuigwaa.model.DataService;
34 import org.seasar.tuigwaa.model.ModelService;
35 import org.seasar.tuigwaa.model.common.EntityInfo;
36 import org.seasar.tuigwaa.model.common.EntityUtils;
37 import org.seasar.tuigwaa.model.core.TgwEntity;
38 import org.seasar.tuigwaa.plugin.TgwPluginUtils;
39 import org.seasar.tuigwaa.plugin.database.FormPlugin;
40 import org.seasar.tuigwaa.plugin.database.TablePlugin;
41 import org.seasar.tuigwaa.plugin.database.component.ResultDto;
42 import org.seasar.tuigwaa.system.Constants;
43 import org.seasar.tuigwaa.system.ServiceHelper;
44 import org.seasar.tuigwaa.util.TgwContext;
45 import org.seasar.tuigwaa.util.TgwNameUtils;
46 import org.seasar.tuigwaa.util.TgwUtils;
47 import org.seasar.tuigwaa.view.EntityFormComponent;
48 import org.seasar.tuigwaa.view.HtmlService;
49 import org.seasar.tuigwaa.view.TableFormComponent;
50
51 import com.isenshi.util.extlib.StrutsUtil;
52
53 public class DatabaseAction extends MappingDispatchAction {
54
55 private static final int DEFAULT_MAX_SIZE = 101;
56
57 protected ModelService model;
58
59 protected DataService dataService;
60
61 protected HtmlService htmlService;
62
63 protected ControllerService controller;
64
65 protected ServiceHelper helper;
66
67 public void setModel(ModelService model) {
68 this.model = model;
69 }
70
71 public void setDataService(DataService dataService) {
72 this.dataService = dataService;
73 }
74
75 public void setHtmlService(HtmlService htmlService) {
76 this.htmlService = htmlService;
77 }
78
79 public void setController(ControllerService controller) {
80 this.controller = controller;
81 }
82
83 public void setHelper(ServiceHelper helper) {
84 this.helper = helper;
85 }
86
87
88
89 public ActionForward listRecord(ActionMapping mapping, ActionForm form,
90 HttpServletRequest request, HttpServletResponse response)
91 throws Exception {
92 String schema = TgwContext.getSiteName();
93 String entityName = TgwContext.getEntityName();
94 TgwEntity entity = model.getEntity(schema, entityName);
95 return doFind(mapping, form, request, response, entity);
96 }
97
98 public ActionForward createRecord(ActionMapping mapping, ActionForm form,
99 HttpServletRequest request, HttpServletResponse response)
100 throws Exception {
101
102 return doCreateForm(mapping, form, request, response, null);
103 }
104
105 public ActionForward editRecord(ActionMapping mapping, ActionForm form,
106 HttpServletRequest request, HttpServletResponse response)
107 throws Exception {
108 Long id = Long.valueOf(request.getParameter(Constants.PARAM_ENTITY_ID));
109 TgwEntity entity = TgwUtils.getEntity(request, getServlet());
110 Object obj = dataService.load(entity, id);
111 return doCreateForm(mapping, form, request, response, obj);
112 }
113
114 public ActionForward deleteRecordAll(ActionMapping mapping,
115 ActionForm form, HttpServletRequest request,
116 HttpServletResponse response) throws Exception {
117 TgwEntity entity = TgwUtils.getEntity(request, getServlet());
118
119 dataService.deleteAll(entity);
120
121 String pageName = StrutsUtil.getURLDecodedParameter(request,
122 Constants.PARAM_PAGENAME);
123 return doForward(mapping, form, request, response, pageName, entity);
124 }
125
126 public ActionForward deleteRecord(ActionMapping mapping, ActionForm form,
127 HttpServletRequest request, HttpServletResponse response)
128 throws Exception {
129 Long id = Long.valueOf(request.getParameter(Constants.PARAM_ENTITY_ID));
130 TgwEntity entity = TgwUtils.getEntity(request, getServlet());
131
132 dataService.delete(entity, id);
133
134 String pageName = StrutsUtil.getURLDecodedParameter(request,
135 Constants.PARAM_PAGENAME);
136
137 FormPlugin.clear(entity);
138
139 return doForward(mapping, form, request, response, pageName, entity);
140 }
141
142 public ActionForward deleteRecordByFilter(ActionMapping mapping,
143 ActionForm form, HttpServletRequest request,
144 HttpServletResponse response) throws Exception {
145
146 String filter = StrutsUtil.getURLDecodedParameter(request,
147 Constants.PARAM_ENTITY_FILTER);
148 TgwEntity entity = TgwUtils.getEntity(request, getServlet());
149
150 dataService.deleteByFilter(entity, filter);
151 String pageName = StrutsUtil.getURLDecodedParameter(request,
152 Constants.PARAM_PAGENAME);
153 return doForward(mapping, form, request, response, pageName, entity);
154 }
155
156 public ActionForward saveRecord(ActionMapping mapping, ActionForm form,
157 HttpServletRequest request, HttpServletResponse response)
158 throws Exception {
159 DynaBean dynaForm = (DynaBean) form;
160 String siteName = TgwContext.getSiteName();
161 String entityName = (String) dynaForm.get(Constants.PARAM_ENTITY_NAME);
162
163
164
165 TgwEntity entity = model.getEntity(siteName, entityName);
166
167 Object obj = EntityUtils.exchange(entity, dynaForm);
168
169 if (mapping.getPath().startsWith("/_c")) {
170 String savePath = mapping.getPath();
171 String methodName = controller
172 .getCustomFormName(siteName, savePath);
173 dataService.saveOrUpdate(entity, obj, methodName);
174 } else {
175 dataService.saveOrUpdate(entity, obj);
176 }
177
178 StrutsUtil.clearSessionForm(request, mapping);
179 TgwContext.relayEntity((DynaBean) form);
180 FormPlugin.clear(entity);
181
182 String redirectUrl = request.getParameter(Constants.PARAM_REDIRECT_URL);
183 String sredirectUrl = (String) EntityUtils.getProperty(form,
184 Constants.PARAM_SEARCH_REDIREDT_URL);
185
186 if (redirectUrl != null && !"".equals(redirectUrl)) {
187 Long id = TgwPluginUtils.getEntityId(entity);
188 redirectUrl = redirectUrl + "?tgw.data." + entity.getName() + "="
189 + id;
190 response.sendRedirect(redirectUrl);
191 return null;
192 } else if (sredirectUrl != null && !"".equals(sredirectUrl)) {
193 response.sendRedirect(sredirectUrl);
194 return null;
195 } else {
196
197
198 String forward = TgwContext.getPageName();
199 return doForward(mapping, form, request, response, forward, entity);
200 }
201 }
202
203 public ActionForward searchRecord(ActionMapping mapping, ActionForm form,
204 HttpServletRequest request, HttpServletResponse response)
205 throws Exception {
206
207 String entityName = request.getParameter(Constants.PARAM_ENTITY_NAME);
208 String siteName = TgwContext.getSiteName();
209 TgwEntity entity = model.getEntity(siteName, entityName);
210
211
212 ResultDto resultDto = doSearch(mapping, form, request, siteName, entity);
213 TablePlugin.setResult(resultDto);
214
215
216 String forward = request.getParameter(Constants.PARAM_PAGENAME);
217 return doForward(mapping, form, request, response, forward, entity);
218 }
219
220
221
222
223 public ActionForward createRecordTable(ActionMapping mapping,
224 ActionForm form, HttpServletRequest request,
225 HttpServletResponse response) throws Exception {
226
227
228 TgwEntity entity = TgwUtils.getEntity(request, getServlet());
229 TableFormComponent component = htmlService.createTableFormComponent(
230 entity, null);
231 DataTable dataTable = dataService.list(entity);
232
233 RecordTableForm rform = new RecordTableForm(entity, component,
234 dataTable);
235 request.getSession().setAttribute(component.getFormName(), rform);
236
237 bindEntityInfo(request, entity);
238
239 return mapping.findForward("success");
240 }
241
242 public ActionForward saveRecordTable(ActionMapping mapping,
243 ActionForm form, HttpServletRequest request,
244 HttpServletResponse response) throws Exception {
245
246 RecordTableForm tableForm = (RecordTableForm) form;
247
248 TgwEntity entity = tableForm.getEntity();
249 String siteName = TgwContext.getSiteName();
250
251 if (entity == null) {
252 String tableName = tableForm.getTableName();
253 entity = model.getEntity(siteName, tableName);
254 }
255
256 doSaveTable(tableForm, entity);
257
258 TgwPluginUtils.removeEntityObject(siteName, entity.getName());
259 StrutsUtil.clearSessionForm(request, mapping);
260 String forward = tableForm.get_pageName_();
261
262 FormPlugin.clear(entity);
263 TgwContext.relayEntityByRequestParameter(request);
264
265 return doForward(mapping, form, request, response, forward, entity);
266 }
267
268
269
270 private void doSaveTable(RecordTableForm tableForm, TgwEntity entity)
271 throws Exception {
272
273
274 String updateMethodName = tableForm.getMethodName();
275
276
277 Iterator itr = tableForm.getChangedRowIterator();
278 while (itr.hasNext()) {
279 DynaBean dyna = (DynaBean) itr.next();
280 Object dto = EntityUtils.exchange(entity.newInstance(), dyna,
281 entity);
282 if (updateMethodName == null) {
283 dataService.saveOrUpdate(entity, dto);
284 } else {
285 dataService.saveOrUpdate(entity, dto, updateMethodName);
286 }
287 }
288
289 itr = tableForm.getDeletedRows().iterator();
290 while (itr.hasNext()) {
291 Long id = (Long) itr.next();
292 dataService.delete(entity, id);
293 }
294 }
295
296 private ActionForward doForward(ActionMapping mapping, ActionForm form,
297 HttpServletRequest request, HttpServletResponse response,
298 String pageName, TgwEntity entity) throws Exception {
299
300 if (request.getParameter(Constants.PARAM_SEARCH_REDIREDT_URL) != null
301 && TablePlugin.getResult(entity) == null) {
302 String searchRedirect = request
303 .getParameter(Constants.PARAM_SEARCH_REDIREDT_URL);
304 response.sendRedirect(searchRedirect);
305 return null;
306 } else if (pageName != null && !pageName.equals("")) {
307 return TgwUtils.forwardPage(request, pageName);
308 } else {
309 return doFind(mapping, form, request, response, entity);
310 }
311 }
312
313 private ActionForward doCreateForm(ActionMapping mapping, ActionForm form,
314 HttpServletRequest request, HttpServletResponse response, Object obj)
315 throws Exception {
316
317 TgwEntity entity = TgwUtils.getEntity(request, getServlet());
318
319 String formName = TgwNameUtils.toFormName(entity);
320 FormBeanConfig config = mapping.getModuleConfig().findFormBeanConfig(
321 TgwNameUtils.toFormName(entity));
322 Object formObj = config.createActionForm(getServlet());
323 request.getSession().setAttribute(formName, formObj);
324
325 EntityFormComponent formComponet = htmlService.createFormComponent(
326 entity, obj);
327
328
329
330 request.setAttribute(Constants.RATTR_HTML_FORM, htmlService
331 .toHtml(formComponet));
332 request.setAttribute(Constants.RATTR_ENTITY, entity);
333
334 return mapping.findForward("success");
335 }
336
337 private ActionForward doFind(ActionMapping mapping, ActionForm form,
338 HttpServletRequest request, HttpServletResponse response,
339 TgwEntity entity) throws Exception {
340
341 String filter = request.getParameter(Constants.PARAM_ENTITY_FILTER);
342 String filterType = request
343 .getParameter(Constants.PARAM_ENTITY_FILTERTYPE);
344
345 if (DataService.DAOTYPE_VALUE.equals(filterType)) {
346 doFindValue(request, entity, filter);
347 } else {
348 doFindTable(request, entity, filter, filterType);
349 }
350
351 request.setAttribute(Constants.RATTR_SELECTED_FILTER, filter);
352 bindEntityInfo(request, entity);
353 return mapping.findForward("success");
354 }
355
356 private void doFindValue(HttpServletRequest request, TgwEntity entity,
357 String methodName) throws Exception {
358 Object result = dataService.getValue(entity, methodName);
359 request.setAttribute(Constants.RATTR_VALUE, result);
360 }
361
362 private void doFindTable(HttpServletRequest request, TgwEntity entity,
363 String filter, String filterType) throws Exception {
364
365 String order = request.getParameter(Constants.PARAM_ENTITY_ORDER);
366 String orderdesc = request
367 .getParameter(Constants.PARAM_ENTITY_ORDERDESC);
368 boolean orderdescFlag = (orderdesc != null) && orderdesc.length() > 0;
369
370 int first = getFirstResult(request);
371 CriteriaListFunction criteriaList = CriteriaListFunction.create(first,
372 DEFAULT_MAX_SIZE);
373 if (order != null && order.length() > 0) {
374 criteriaList.addFunction(new OrderCriteriaFunction(order, null,
375 orderdescFlag));
376 }
377
378 DataTable result = dataService.find(entity, filter, criteriaList);
379 if (DataService.DAOTYPE_AGGREGATION.equals(filterType)) {
380 request.setAttribute(Constants.RATTR_ENTITY_AGGREGATION, "true");
381 }
382 request.setAttribute(Constants.RATTR_RESULTSET, result);
383 }
384
385 private void bindEntityInfo(HttpServletRequest request, TgwEntity entity) {
386 EntityInfo info = model.createEntityInfo(entity.getDomainName(), entity
387 .getName());
388 List infoList = model.createEntityInfoList(entity.getDomainName());
389 request.setAttribute(Constants.RATTR_ENTITYINFOLIST, infoList);
390 request.setAttribute(Constants.RATTR_ENTITYINFO, info);
391 }
392
393 private int getFirstResult(HttpServletRequest request) {
394 String firstStr = request.getParameter(Constants.PARAM_ENTITY_FIRST);
395 int first = ((firstStr != null) && (firstStr.length() > 0)) ? Integer
396 .parseInt(firstStr) : 0;
397 return first;
398 }
399
400 private int getMaxResult(HttpServletRequest request) {
401 String maxStr = request.getParameter(Constants.PARAM_ENTITY_MAX);
402 int max = ((maxStr != null) && (maxStr.length() > 0)) ? Integer
403 .parseInt(maxStr) : DEFAULT_MAX_SIZE;
404 return max;
405 }
406
407 private ResultDto doSearch(ActionMapping mapping, Object form,
408 HttpServletRequest request, String siteName, TgwEntity entity) {
409 int first = getFirstResult(request);
410 int max = getMaxResult(request);
411
412 CriteriaListFunction criteriaList = CriteriaListFunction.create(first,
413 max);
414
415
416 String searchPath = mapping.getPath();
417 String method = controller.getMethodName(siteName, searchPath);
418 String filterName = request.getParameter(Constants.PARAM_METHOD_NAME);
419 DataTable dataTable = dataService.searchByFilter(entity, method, form,
420 filterName, criteriaList);
421
422 ResultDto resultDto = new ResultDto(entity, dataTable);
423 resultDto.setSearchDto(form);
424 resultDto.setSearchKeyList(helper.getSearchForm(entity, method)
425 .getSearchKeyList(form));
426 resultDto.setSearchName(method);
427
428 int rows = dataService.rowCountByFilter(entity, filterName, method,
429 form);
430 resultDto.setRows(rows);
431 resultDto.setMax(max);
432 resultDto.setPath(request.getRequestURI() + "?"
433 + request.getQueryString());
434 resultDto.setFirst(first);
435 return resultDto;
436 }
437
438
439 }