1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.seasar.tuigwaa.controller;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.apache.commons.collections.Factory;
29 import org.apache.commons.collections.ListUtils;
30 import org.apache.struts.action.ActionErrors;
31 import org.apache.struts.action.ActionMapping;
32 import org.seasar.tuigwaa.database.CriteriaFunctionFactory;
33 import org.seasar.tuigwaa.database.function.CriteriaExeFunction;
34 import org.seasar.tuigwaa.database.function.criteria.CriteriaFunction;
35 import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
36 import org.seasar.tuigwaa.database.function.criteria.FirstResultCriteriaFunction;
37 import org.seasar.tuigwaa.database.function.criteria.MaxResultCriteriaFunction;
38 import org.seasar.tuigwaa.database.function.criteria.OrderCriteriaFunction;
39 import org.seasar.tuigwaa.database.function.criteria.SubcriteriaFunction;
40 import org.seasar.tuigwaa.database.function.criteria.UnaryCriteriaFunction;
41 import org.seasar.tuigwaa.model.common.EntityUtils;
42 import org.seasar.tuigwaa.model.core.RelationAttribute;
43 import org.seasar.tuigwaa.model.core.TgwAttribute;
44 import org.seasar.tuigwaa.model.core.TgwElement;
45 import org.seasar.tuigwaa.model.core.TgwEntity;
46 import org.seasar.tuigwaa.model.core.impl.FkAttribute;
47 import org.seasar.tuigwaa.view.AttributeFormGenerateVisitor;
48
49 /***
50 * フィルタを作成するフォームに対応
51 *
52 * @author nishioka
53 */
54 public class DataFilterForm extends AbstractFieldFilterForm {
55
56 private static final long serialVersionUID = -6374715710236762455L;
57
58 private String aggregationName_;
59
60 private boolean columnSelected = false;
61
62 private String[] selectedColumns;
63
64 private List dataFilterFormOrderRows = ListUtils.lazyList(new ArrayList(),
65 new DataFilterFormOrderRowFactory());
66
67 private List orderedFormNameList = new ArrayList();
68
69 private Integer maxResult;
70
71 private Integer firstResult;
72
73 private void init(TgwEntity entity) {
74 setEntity(entity);
75 initSelectedColumns();
76 }
77
78 public DataFilterForm(TgwEntity entity) {
79 init(entity);
80 }
81
82 public DataFilterForm(TgwEntity entity, String aggregationName) {
83 init(entity);
84 this.aggregationName_ = aggregationName;
85
86 }
87
88 public DataFilterForm(TgwEntity entity, CriteriaExeFunction function) {
89 init(entity);
90 this.dataFilterFormOrderRows = ListUtils.lazyList(new ArrayList(),
91 new DataFilterFormOrderRowFactory());
92 setName(function.getName());
93 setSelectedHeaders(function.getSelectedHeaders());
94 setFormDatasFromFunction((CriteriaListFunction) function
95 .getUnaryCriteriaFunction());
96 }
97
98 private void setFormDatasFromFunction(CriteriaListFunction listFunction) {
99
100 setDisjunction(listFunction.isDisjunction());
101
102 Iterator itr = listFunction.getFunctionList().iterator();
103 List orderCriteriaFunctions = ListUtils.lazyList(new ArrayList(),
104 new DataFilterFormOrderRowFactory());
105 while (itr.hasNext()) {
106 CriteriaFunction criteriaFunction = (CriteriaFunction) itr.next();
107 if (criteriaFunction instanceof OrderCriteriaFunction) {
108 orderCriteriaFunctions.add(criteriaFunction);
109 } else if (criteriaFunction instanceof FirstResultCriteriaFunction) {
110 chargeFirstResultCriteriaFunction((FirstResultCriteriaFunction) criteriaFunction);
111 } else if (criteriaFunction instanceof MaxResultCriteriaFunction) {
112 chargeMaxResultCriteriaFunction((MaxResultCriteriaFunction) criteriaFunction);
113 } else {
114 chargeCriteriaFunction(criteriaFunction);
115 }
116
117 }
118 chargeOrderCriteriaFunction(orderCriteriaFunctions);
119 }
120
121 private void chargeCriteriaFunction(CriteriaFunction function) {
122 String fieldName = null;
123
124 if (function instanceof SubcriteriaFunction) {
125 fieldName = ((SubcriteriaFunction) function).getNestedAttrName();
126 } else {
127 fieldName = function.getField();
128 }
129
130 TgwAttribute attribute = getEntity().getField(fieldName);
131 FieldForm form = createFieldForm(attribute, fieldName);
132
133 CriteriaFunction nestedFunc = CriteriaFunctionFactory
134 .getFunction(function);
135
136
137 form.setType(CriteriaFunctionFactory.getType(nestedFunc));
138 form.setValue(nestedFunc.getValue());
139
140 setEntityProperty(form);
141 addRow(form);
142 }
143
144 private void setEntityProperty(FieldForm form) {
145 if (form instanceof AbstractFieldFilterForm.BooleanFieldForm
146 || form instanceof AbstractFieldFilterForm.DateFieldForm) {
147 return;
148 }
149
150 Object obj = null;
151 String propName = form.getFieldName();
152 int lastIndex = propName.lastIndexOf(".");
153
154 if (lastIndex >= 0) {
155 String parentAttrName = propName.substring(0, lastIndex);
156 propName = propName.substring(lastIndex + 1);
157 RelationAttribute fk = (RelationAttribute) getEntity().getField(
158 parentAttrName);
159 obj = fk.getRefEntity().newInstance();
160 } else {
161 obj = getEntity().newInstance();
162 }
163 EntityUtils.setProperty(obj, propName, form.getValue());
164 form.setVisitor(AttributeFormGenerateVisitor.createTableForm(obj));
165 }
166
167 private void chargeOrderCriteriaFunction(List orderCriteriaFunctions) {
168 Iterator itr = orderCriteriaFunctions.iterator();
169 while (itr.hasNext()) {
170 OrderCriteriaFunction function = (OrderCriteriaFunction) itr.next();
171 DataFilterFormOrderRow row = new DataFilterFormOrderRow();
172 row.setOrderSelected(true);
173 row.setSelectedOrder(function.getField());
174 row.setAscendant(!function.isDesc());
175 row.setDisplayName(getEntity().getField(function.getField())
176 .getDisplayName());
177
178 this.dataFilterFormOrderRows.add(row);
179 this.orderedFormNameList.add(row.getSelectedOrder());
180
181 }
182 }
183
184 private void chargeFirstResultCriteriaFunction(
185 FirstResultCriteriaFunction function) {
186 setFirstResult((Integer) ((FirstResultCriteriaFunction) function)
187 .getValue());
188 }
189
190 private void chargeMaxResultCriteriaFunction(
191 MaxResultCriteriaFunction function) {
192 setMaxResult((Integer) ((MaxResultCriteriaFunction) function)
193 .getValue());
194 }
195
196 private void setSelectedHeaders(List selectedHeaders) {
197 if (selectedHeaders != null && selectedHeaders.size() > 0) {
198 setColumnSelected(true);
199
200 Iterator fieldIterator = getEntity().getFieldIterator();
201 int n = 0;
202 while (fieldIterator.hasNext()) {
203 String fieldName = ((TgwElement) fieldIterator.next())
204 .getName();
205 if (selectedHeaders.contains(fieldName)) {
206 setSelectedColumn(n, fieldName);
207 }
208 n++;
209 }
210 }
211 }
212
213 public Integer getFirstResult() {
214 return firstResult;
215 }
216
217 public void setFirstResult(Integer firstResult) {
218 this.firstResult = firstResult;
219 }
220
221 public Integer getMaxResult() {
222 return maxResult;
223 }
224
225 public void setMaxResult(Integer maxResult) {
226 this.maxResult = maxResult;
227 }
228
229 public boolean isColumnSelected() {
230 return columnSelected;
231 }
232
233 public void setColumnSelected(boolean columnSelected) {
234 this.columnSelected = columnSelected;
235 }
236
237 public String[] getSelectedColumns() {
238 return selectedColumns;
239 }
240
241 private void initSelectedColumns() {
242 Collection fields = getEntity().getFieldList();
243 if (fields != null) {
244 selectedColumns = new String[fields.size()];
245 }
246 }
247
248 public void setSelectedColumns(String[] selectedColumns) {
249 this.selectedColumns = selectedColumns;
250 }
251
252 private void setSelectedColumn(int index, String str) {
253 this.selectedColumns[index] = str;
254 }
255
256 public String getAggregationName() {
257 return aggregationName_;
258 }
259
260 public Collection getFieldList() {
261 return getEntity().getFieldList();
262 }
263
264 protected void removeRow(int index, Object row) {
265 }
266
267 protected String[] getHtmlData(int index, Object row) {
268 FieldForm fieldForm = (FieldForm) row;
269 return fieldForm.getHtmlData(index);
270 }
271
272 protected Object createRow(int index, String value) {
273 TgwAttribute field = getEntity().getField(value);
274 return createFieldForm(field, value);
275 }
276
277 protected Object createNewRow() {
278 TgwAttribute field = getEntity().getFirstNotFkField();
279 String fieldName = getEntity().getFirstNotFkFieldName();
280 return createFieldForm(field, fieldName);
281 }
282
283
284
285 /***
286 * This method is called by DatgaFilterAction. User input data is converted
287 * to DataFilterFunction Object.
288 */
289 public CriteriaExeFunction getDataFilterFunction() {
290 UnaryCriteriaFunction function = getUnaryCriteriaFunction();
291 CriteriaExeFunction exe = CriteriaFunctionFactory
292 .createCriteriaExeFunction(getEntity(), getName(), function);
293 if (selectedColumns != null && selectedColumns.length > 0) {
294 for (int i = 0; i < selectedColumns.length; i++) {
295 exe.addSelectedHeader(selectedColumns[i]);
296 }
297 }
298 return exe;
299 }
300
301 public UnaryCriteriaFunction getUnaryCriteriaFunction() {
302 CriteriaListFunction function = new CriteriaListFunction();
303 function.setDisjunction(isDisjunction());
304 for (Iterator itr = getRowSet().iterator(); itr.hasNext();) {
305 FieldForm form = (FieldForm) itr.next();
306 CriteriaFunction critariFunction = getFunction(form.getType(),
307 getEntity(), form.getFieldName(), form.getValue());
308 function.addFunction(critariFunction);
309 }
310
311 if (getMaxResult() != null && getMaxResult().intValue() > 0) {
312 function.addFunction(new MaxResultCriteriaFunction(getMaxResult()));
313 }
314
315 if (getFirstResult() != null && getFirstResult().intValue() > 0) {
316 function.addFunction(new FirstResultCriteriaFunction(
317 getFirstResult()));
318 }
319
320 Iterator itr = this.dataFilterFormOrderRows.iterator();
321 while (itr.hasNext()) {
322 DataFilterFormOrderRow row = (DataFilterFormOrderRow) itr.next();
323 OrderCriteriaFunction func = row.getUnaryCriteriaFunction();
324 if (func != null) {
325 function.addFunction(func);
326 }
327 }
328
329 return function;
330 }
331
332 public void reset(ActionMapping arg0, HttpServletRequest arg1) {
333
334 if (getSelectedColumns() != null) {
335 String str[] = new String[getSelectedColumns().length];
336 setSelectedColumns(str);
337 }
338 Iterator itr = this.dataFilterFormOrderRows.iterator();
339 while (itr.hasNext()) {
340 ((DataFilterFormOrderRow) itr.next()).reset();
341 }
342
343 super.reset(arg0, arg1);
344 }
345
346 public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
347 if (!isColumnSelected()) {
348 initSelectedColumns();
349 }
350 return super.validate(arg0, arg1);
351 }
352
353 public DataFilterFormOrderRow getDataFilterFormOrderRow(int index) {
354 return (DataFilterFormOrderRow) dataFilterFormOrderRows.get(index);
355 }
356
357 public void setDataFilterFormOrderRow(DataFilterFormOrderRow orderRow) {
358 this.dataFilterFormOrderRows.add(orderRow);
359 }
360
361 public void setDataFilterFormOrderRows(List dataFilterFormOrderRows) {
362 this.dataFilterFormOrderRows = dataFilterFormOrderRows;
363 }
364
365 public Collection getDataFilterFormOrderRows() {
366 return dataFilterFormOrderRows;
367 }
368
369 public Collection getNotOrderFields() {
370 List list = new ArrayList();
371 Collection fields = getFieldList();
372 Iterator itr = fields.iterator();
373 while (itr.hasNext()) {
374 TgwAttribute field = (TgwAttribute) itr.next();
375 if (!orderedFormNameList.contains(field.getName())) {
376 list.add(field);
377 }
378 }
379 return list;
380 }
381
382 public class DataFilterFormOrderRow {
383 private boolean ascendant = false;
384
385 private String selectedOrder = null;
386
387 private boolean orderSelected = false;
388
389 private String displayName = null;
390
391 public boolean isAscendant() {
392 return ascendant;
393 }
394
395 public void setAscendant(boolean ascendant) {
396 this.ascendant = ascendant;
397 }
398
399 public boolean isOrderSelected() {
400 return orderSelected;
401 }
402
403 public void setOrderSelected(boolean orderSelected) {
404 this.orderSelected = orderSelected;
405 }
406
407 public String getSelectedOrder() {
408 return selectedOrder;
409 }
410
411 public void setSelectedOrder(String selectedOrder) {
412 this.selectedOrder = selectedOrder;
413 }
414
415 private OrderCriteriaFunction getUnaryCriteriaFunction() {
416 if (this.orderSelected && this.selectedOrder != null) {
417 boolean descFlag = !isAscendant();
418 return new OrderCriteriaFunction(getSelectedOrder(), null,
419 descFlag);
420 }
421 return null;
422 }
423
424 public void reset() {
425 this.setOrderSelected(false);
426 }
427
428 public String getDisplayName() {
429 return displayName;
430 }
431
432 public void setDisplayName(String displayName) {
433 this.displayName = displayName;
434 }
435
436 public String getAscendantChecked() {
437 if (ascendant) {
438 return "checked";
439 }
440 return "";
441 }
442
443 public String getDescendantChecked() {
444 if (!ascendant) {
445 return "checked";
446 }
447 return "";
448 }
449
450 }
451
452 public class DataFilterFormOrderRowFactory implements Factory {
453 public Object create() {
454 return new DataFilterFormOrderRow();
455 }
456 }
457
458 }