View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  /*
17   * Created on 2005/08/23
18   */
19  package org.seasar.tuigwaa.controller;
20  
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import org.apache.commons.lang.ArrayUtils;
26  import org.seasar.tuigwaa.database.CriteriaFunctionFactory;
27  import org.seasar.tuigwaa.database.function.CriteriaExeFunction;
28  import org.seasar.tuigwaa.database.function.aggregation.AggregationFunction;
29  import org.seasar.tuigwaa.database.function.aggregation.GroupProjectionFunction;
30  import org.seasar.tuigwaa.database.function.aggregation.ProjectionFunction;
31  import org.seasar.tuigwaa.database.function.criteria.CriteriaFunction;
32  import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
33  import org.seasar.tuigwaa.model.core.TgwAttribute;
34  import org.seasar.tuigwaa.model.core.TgwEntity;
35  import org.seasar.tuigwaa.model.core.impl.FloatAttribute;
36  import org.seasar.tuigwaa.model.core.impl.IntegerAttribute;
37  import org.seasar.tuigwaa.model.core.impl.NestedAttribute;
38  import org.seasar.tuigwaa.util.ajax.AbstractRowSetForm;
39  
40  import com.isenshi.util.HtmlBuffer;
41  
42  /***
43   * This class is registerd into JSON Ajax.
44   * 集計値を作成するフォームに対応。
45   * 
46   * @author nishioka
47   */
48  public class AggregationForm extends AbstractRowSetForm {
49  
50  	private static final long serialVersionUID = -6793435086829923995L;
51  
52  	private static final String[] AGGREGATION_TYPES = { "rowcount", "avg",
53  			"min", "max", "sum", "countDistinct" };
54  
55  	private static final String[] NUMFIELD_TYPE = { AGGREGATION_TYPES[1],
56  			AGGREGATION_TYPES[2], AGGREGATION_TYPES[3], AGGREGATION_TYPES[4] };
57  
58  	private String name_;
59  
60  	private String filterName_;
61  
62  	private TgwEntity entity_;
63  
64  	// private String[] groupFields_;
65  
66  	private String groupField1;
67  
68  	private String groupField2;
69  
70  	private String[] dataFilters_;
71  
72  	private Map dataFilterMap_;
73  
74  	private boolean valueFlag;
75  
76  	private boolean extraRowFlag;
77  
78  	public AggregationForm(TgwEntity entity, Map dataFilterMap) {
79  		this.entity_ = entity;
80  		this.dataFilterMap_ = dataFilterMap;
81  		addRow(createNewRow());
82  	}
83  
84  	// [Start] ----- Getter Setter Methods -----
85  
86  	public String[] getDataFilters() {
87  		return dataFilters_;
88  	}
89  
90  	public void setDataFilters(String[] dataFilters) {
91  		this.dataFilters_ = dataFilters;
92  	}
93  
94  	public Collection getDataFilterList() {
95  		return dataFilterMap_.values();
96  	}
97  
98  	public void setGroupField1(String groupField1) {
99  		this.groupField1 = groupField1;
100 	}
101 
102 	public void setGroupField2(String groupField2) {
103 		this.groupField2 = groupField2;
104 	}
105 
106 	public String getGroupField1() {
107 		return groupField1;
108 	}
109 
110 	public String getGroupField2() {
111 		return groupField2;
112 	}
113 
114 	/*
115 	 * public void setGroupFields(String[] groupFields) { this.groupFields_ =
116 	 * groupFields; }
117 	 * 
118 	 * public String[] getGroupFields() { return groupFields_; }
119 	 */
120 	public TgwEntity getEntity() {
121 		return entity_;
122 	}
123 
124 	public String getName() {
125 		return name_;
126 	}
127 
128 	public void setName(String name) {
129 		this.name_ = name;
130 	}
131 
132 	public void setFilterName(String filterName) {
133 		this.filterName_ = filterName;
134 	}
135 
136 	public String getFilterName() {
137 		return filterName_;
138 	}
139 
140 	public void setValueFlag(boolean valueFlag) {
141 		this.valueFlag = valueFlag;
142 	}
143 
144 	public boolean isValueFlag() {
145 		return valueFlag;
146 	}
147 
148 	public void setExtraRowFlag(boolean extraRowFlag) {
149 		this.extraRowFlag = extraRowFlag;
150 	}
151 
152 	public boolean isExtraRowFlag() {
153 		return extraRowFlag;
154 	}
155 
156 	// [End] ----- Getter Setter Methods -----
157 
158 	// [Start] ----- Ajax Methods -----
159 
160 	protected void removeRow(int index, Object row) {
161 	}
162 
163 	protected String[] getHtmlData(int index, Object row) {
164 		String[] data = new String[4];
165 		FunctionForm form = (FunctionForm) row;
166 
167 		data[0] = createTypeSelector(index, form);
168 		data[1] = createTextField(getPrefix(index) + "displayName");
169 		data[2] = createFieldSelector(index, form);
170 
171 		if (valueFlag) {
172 			data[3] = "";
173 		} else {
174 			data[3] = createRemoveButtonHtml(index, getMessage("button.remove"));
175 		}
176 		return data;
177 	}
178 
179 	protected Object createRow(int index, String value) {
180 		return new FunctionForm(value);
181 	}
182 
183 	private String createTypeSelector(int index, FunctionForm form) {
184 		HtmlBuffer buf = new HtmlBuffer();
185 		buf.appendStartTag("select");
186 		buf.appendAttribute("name", getPrefix(index) + "type");
187 		bindTypeEvent(buf);
188 		String[] pickTypes = form.getPickTypes();
189 
190 		for (int i = 0; i < pickTypes.length; i++) {
191 			String value = pickTypes[i];
192 			String label = getMessage("aggregation.label." + value);
193 			buf.appendOption(label, value, form.getType());
194 		}
195 		buf.endTag();
196 
197 		return buf.toString();
198 	}
199 
200 	public String createFieldSelector(int index, FunctionForm form) {
201 		if (form.getType().equals(AGGREGATION_TYPES[0])) {
202 			return "";
203 		}
204 		boolean onlyNumAttr = false;
205 		if (ArrayUtils.contains(NUMFIELD_TYPE, form.getType())) {
206 			onlyNumAttr = true;
207 		}
208 
209 		HtmlBuffer buf = new HtmlBuffer();
210 		buf.appendStartTag("select");
211 		buf.appendAttribute("name", getPrefix(index) + "field");
212 
213 		// Iterator itr = entity_.getFieldIterator();
214 		Iterator itr = entity_.getFirstDepthAllFieldList().iterator();
215 		while (itr.hasNext()) {
216 			TgwAttribute field = (TgwAttribute) itr.next();
217 			if (!onlyNumAttr || isNumField(field)) {
218 				buf.appendOption(field.getDisplayName(), field.getName());
219 			}
220 		}
221 		buf.endTag();
222 		return buf.toString();
223 	} /*
224 		 * public void bindNumFieldOption(HtmlBuffer buf, TgwAttribute field,
225 		 * String displayPrefix, String prefix) { } }
226 		 */
227 
228 	private boolean isNumField(TgwAttribute field) {
229 		if (field instanceof IntegerAttribute
230 				|| field instanceof FloatAttribute) {
231 			return true;
232 		}
233 		if (field instanceof NestedAttribute) {
234 			return isNumField(((NestedAttribute) field).getRealField());
235 		}
236 		return false;
237 	}
238 
239 	protected Object createNewRow() {
240 		return new FunctionForm();
241 	}
242 
243 	// [End] ----- Ajax Methods -----
244 
245 	public CriteriaExeFunction getAggregatoinExeFunction() {
246 		CriteriaListFunction criteriaList = new CriteriaListFunction();
247 		CriteriaExeFunction exe = CriteriaFunctionFactory
248 				.createCriteriaExeFunction(getEntity(), getName(), criteriaList);
249 
250 		if (dataFilters_ != null) {
251 			for (int i = 0; i < dataFilters_.length; i++) {
252 				CriteriaExeFunction dataFilter = (CriteriaExeFunction) dataFilterMap_
253 						.get(dataFilters_[i]);
254 				criteriaList
255 						.injectCriteriaListFunction((CriteriaFunction) dataFilter
256 								.getUnaryCriteriaFunction());
257 			}
258 		}
259 
260 		AggregationFunction aggFunction = getAggregaionFunction(exe);
261 		criteriaList.addFunction(aggFunction);
262 
263 		if (isValueFlag()) {
264 			exe.setValueFlag(true);
265 		}
266 
267 		if (extraRowFlag) {
268 			exe.setExtraRowFlag(true);
269 		}
270 		if (groupField2 != null && groupField2.length() > 0) {
271 			exe.setSpreadSheetFlag(true);
272 		}
273 		return exe;
274 	}
275 
276 	public AggregationFunction getAggregaionFunction(CriteriaExeFunction exe) {
277 		AggregationFunction aggFunction = new AggregationFunction();
278 
279 		addGroupField(exe, aggFunction, groupField1);
280 		addGroupField(exe, aggFunction, groupField2);
281 
282 		for (Iterator i = getRowSet().iterator(); i.hasNext();) {
283 			FunctionForm form = (FunctionForm) i.next();
284 			String displayName = form.getDisplayName();
285 			String type = form.getType();
286 
287 			ProjectionFunction function = CriteriaFunctionFactory
288 					.getProjectionFunction(type);
289 			function.setField(form.getField());
290 			function.setAlias(displayName);
291 
292 			aggFunction.addFunction(function);
293 			exe.addProjection(displayName);
294 
295 		}
296 		return aggFunction;
297 	}
298 
299 	private void addGroupField(CriteriaExeFunction exe,
300 			AggregationFunction aggFunction, String fieldName) {
301 		if (fieldName == null) {
302 			return;
303 		}
304 
305 		String[] options = fieldName.split("//?");
306 		String option = null;
307 		if (options.length == 2) {
308 			fieldName = options[0];
309 			option = options[1];
310 		}
311 		TgwAttribute attr = entity_.getField(fieldName);
312 		String displayName = attr.getDisplayName();
313 		GroupProjectionFunction groupFunction = new GroupProjectionFunction();
314 		groupFunction.setField(fieldName);
315 		groupFunction.setAlias(fieldName);
316 		groupFunction.setOption(option);
317 		
318 		aggFunction.addFunction(groupFunction);
319 		exe.addProjection(displayName);
320 	}
321 
322 	public class FunctionForm {
323 		private String type_;
324 
325 		private String displayName_;
326 
327 		private String field_;
328 
329 		public FunctionForm() {
330 			this.type_ = AGGREGATION_TYPES[0];
331 			// this(type);
332 			// this(AGGREGATION_TYPES[0]);
333 		}
334 
335 		public FunctionForm(String type) {
336 			this.type_ = type;
337 		}
338 
339 		public String[] getPickTypes() {
340 			// Iterator itr = entity_.getFieldIterator();
341 			Iterator itr = entity_.getFirstDepthAllFieldList().iterator();
342 			boolean existNumField = false;
343 			while (itr.hasNext()) {
344 				TgwAttribute field = (TgwAttribute) itr.next();
345 				if (field instanceof NestedAttribute) {
346 					field = ((NestedAttribute) field).getRealField();
347 				}
348 
349 				if (field instanceof IntegerAttribute
350 						|| field instanceof FloatAttribute) {
351 					existNumField = true;
352 					break;
353 				}
354 			}
355 			if (existNumField) {
356 				return AGGREGATION_TYPES;
357 			} else {
358 				return new String[] { "rowcount" };
359 			}
360 		}
361 
362 		public void setType(String type) {
363 			// this.type_ = type;
364 			// Do noting
365 		}
366 
367 		public String getType() {
368 			return type_;
369 		}
370 
371 		public String getDisplayName() {
372 			return displayName_;
373 		}
374 
375 		public void setDisplayName(String displayName) {
376 			this.displayName_ = displayName;
377 		}
378 
379 		public String getField() {
380 			return field_;
381 		}
382 
383 		public void setField(String field) {
384 			this.field_ = field;
385 		}
386 	}
387 }