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  package org.seasar.tuigwaa.view;
17  
18  import java.text.SimpleDateFormat;
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import org.apache.struts.util.ResponseUtils;
28  import org.seasar.tuigwaa.database.CriteriaFunctionFactory;
29  import org.seasar.tuigwaa.database.function.SearchExeFunction;
30  import org.seasar.tuigwaa.database.function.criteria.CriteriaFunction;
31  import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
32  import org.seasar.tuigwaa.database.function.criteria.GeCriteriaFunction;
33  import org.seasar.tuigwaa.database.function.criteria.LeCriteriaFunction;
34  import org.seasar.tuigwaa.database.function.criteria.SubcriteriaFunction;
35  import org.seasar.tuigwaa.model.common.EntityUtils;
36  import org.seasar.tuigwaa.model.common.TgwAttributeVisitor;
37  import org.seasar.tuigwaa.model.core.TgwAttribute;
38  import org.seasar.tuigwaa.model.core.TgwEntity;
39  import org.seasar.tuigwaa.model.core.impl.DateAttribute;
40  import org.seasar.tuigwaa.model.core.impl.TimestampAttribute;
41  import org.seasar.tuigwaa.system.Constants;
42  import org.seasar.tuigwaa.util.TgwNameUtils;
43  import org.seasar.tuigwaa.util.TgwResource;
44  
45  public class SearchFormComponent extends AbstractGenericFormComponent {
46  
47  	private SearchExeFunction searchFunction;
48  
49  	private int max;
50  
51  	private String extraFilterName;
52  
53  	private Map alternateDisplayMap;
54  
55  	private Object valueObject;
56  
57  	public SearchFormComponent(TgwEntity entity,
58  			SearchExeFunction searchFunction) {
59  		super(entity, null);
60  		this.searchFunction = searchFunction;
61  	}
62  
63  	// [Start] ------ Setter Getter Method -----
64  
65  	public void setMax(int max) {
66  		this.max = max;
67  	}
68  
69  	public int getMax() {
70  		return max;
71  	}
72  
73  	public void setExtraFilterName(String extraFilterName) {
74  		this.extraFilterName = extraFilterName;
75  	}
76  
77  	public String getExtraFilterName() {
78  		return extraFilterName;
79  	}
80  
81  	public void setAlternateDisplayMap(Map alternateDisplayMap) {
82  		this.alternateDisplayMap = alternateDisplayMap;
83  	}
84  
85  	public void setValueObject(Object valueObject) {
86  		this.valueObject = valueObject;
87  	}
88  
89  	public Object getValueObject() {
90  		return valueObject;
91  	}
92  
93  	// [Start] ------ Override Method -----
94  
95  	// overide
96  	public String getName() {
97  		return searchFunction.getName();
98  	}
99  
100 	// overide
101 	protected String getOnSubmitFunction() {
102 		return null;
103 	}
104 
105 	// overide
106 	protected boolean isMultiPart() {
107 		return false;
108 	}
109 
110 	// overide
111 	protected Map getHiddenProperties() {
112 		Map hiddenProps = new HashMap();
113 		hiddenProps.put(Constants.PARAM_PAGENAME, getForwardPageName());
114 		hiddenProps.put(Constants.PARAM_ENTITY_NAME, getEntity().getName());
115 		if (getExtraFilterName() != null) {
116 			hiddenProps.put(Constants.PARAM_METHOD_NAME, getExtraFilterName());
117 		}
118 		return hiddenProps;
119 	}
120 
121 	// overide
122 	public List getFormElements() {
123 		CriteriaListFunction listFunction = searchFunction
124 				.getCriteriaListFunction();
125 
126 		Iterator itr = listFunction.getFunctionList().iterator();
127 		List list = new ArrayList();
128 
129 		TgwAttribute lastAttr = null;
130 		CriteriaFunction lastFunc = null;
131 
132 		while (itr.hasNext()) {
133 			CriteriaFunction function = (CriteriaFunction) itr.next();
134 			String nestedFieldName = createNestedFieldName("", function);
135 			TgwAttribute field = getEntity().getField(nestedFieldName);
136 
137 			String label = createLabel(function);
138 			String elemName = function.getName();
139 			String element = createElement(function, field, elemName);
140 
141 			String displayName = toDisplayName(field, function);
142 
143 			if (checkDateAttr(lastAttr, field)
144 					&& function instanceof LeCriteriaFunction
145 					&& lastFunc instanceof GeCriteriaFunction) {
146 				
147 				FormElement formelem = (FormElement) list.get(list.size() - 1);
148 				formelem.setElement(formelem.getElement() + " "
149 						+ TgwResource.getMessage("label.from") + " "
150 						+ element + " "
151 						+ TgwResource.getMessage("label.to"));
152 				formelem.setDescription("");
153 
154 				// FormElement newformelem = new FormElement(formelem
155 				// .getAttrName(), formelem.getName(), field
156 				// .getDisplayName(), newElem, false, label);
157 				// list.set(list.size() - 1, newformelem);
158 
159 			} else {
160 				list.add(new FormElement(field.getName(), elemName,
161 						displayName, element, false, label));
162 			}
163 			lastAttr = field;
164 			lastFunc = function;
165 		}
166 		return list;
167 	}
168 
169 	public Properties getSearchProperties() {
170 		Properties props = new Properties();
171 		CriteriaListFunction listFunction = searchFunction
172 				.getCriteriaListFunction();
173 		Iterator itr = listFunction.getFunctionList().iterator();
174 
175 		while (itr.hasNext()) {
176 			CriteriaFunction function = (CriteriaFunction) itr.next();
177 			TgwAttribute attr = getEntity().getField(function.getField());
178 			props.put(function.getName(), attr.getDisplayName());
179 		}
180 		return props;
181 	}
182 
183 	public List getSearchKeyList(Object valueObject) {
184 		List list = new ArrayList();
185 		CriteriaListFunction listFunction = searchFunction
186 				.getCriteriaListFunction();
187 		Iterator itr = listFunction.getFunctionList().iterator();
188 		while (itr.hasNext()) {
189 			CriteriaFunction function = (CriteriaFunction) itr.next();
190 			String name = function.getName();
191 			TgwAttribute attr = getEntity().getField(function.getField());
192 			Object value = EntityUtils.getProperty(valueObject, name);
193 			if ("".equals(value)) {
194 				value = null;
195 			}
196 			String label = createLabel(function);
197 			list.add(new SearchKey(name, attr, value, function, label));
198 		}
199 		return list;
200 	}
201 
202 	private boolean checkDateAttr(TgwAttribute lastAttr, TgwAttribute curAttr) {
203 		if(lastAttr == null){
204 			return false;
205 		}
206 		if (!lastAttr.equals(curAttr)) {
207 			return false;
208 		}
209 		if (lastAttr instanceof DateAttribute
210 				|| lastAttr instanceof TimestampAttribute) {
211 			return true;
212 		} else {
213 			return false;
214 		}
215 	}
216 
217 	public static class SearchKey {
218 
219 		private String name;
220 
221 		private TgwAttribute attr;
222 
223 		private Object value;
224 
225 		private CriteriaFunction function;
226 
227 		private String label;
228 
229 		public SearchKey(String name, TgwAttribute attr, Object value,
230 				CriteriaFunction function, String label) {
231 			this.name = name;
232 			this.attr = attr;
233 			this.value = value;
234 			this.function = function;
235 			this.label = label;
236 		}
237 
238 		public String getName() {
239 			return name;
240 		}
241 
242 		public TgwAttribute getAttr() {
243 			return attr;
244 		}
245 
246 		public Object getValue() {
247 			return value;
248 		}
249 
250 		public String getLabel() {
251 			return label;
252 		}
253 
254 		public Object getFilteredValue() {
255 			Object obj = getValue();
256 			if (obj == null) {
257 				return null;
258 			}
259 			if (attr instanceof DateAttribute) {
260 				String dateformat = TgwResource
261 						.getProperty("option.display.date");
262 				SimpleDateFormat format = new SimpleDateFormat(dateformat);
263 				return format.format((Date) getValue());
264 			}else if(obj instanceof String){
265 				return ResponseUtils.filter((String)obj);
266 			}else{
267 				return obj;
268 			}
269 		}
270 
271 		public CriteriaFunction getFunction() {
272 			return function;
273 		}
274 	}
275 
276 	// [End] ------ Private Method -----
277 
278 	private String createLabel(CriteriaFunction function) {
279 		String type = CriteriaFunctionFactory.getType(function);
280 		if ("like".equals(type)) {
281 			String option = CriteriaFunctionFactory.getFunction(function)
282 					.getOption();
283 			type += "?" + option;
284 		}
285 		return getMessage("filter.label.short." + type);
286 	}
287 
288 	private String createElement(CriteriaFunction function, TgwAttribute field,
289 			String elemName) {
290 		TgwAttributeVisitor visitor = AttributeFormGenerateVisitor
291 				.createSearchVisitor(valueObject);
292 		if (EntityUtils.needConverter(field)) {
293 			elemName = TgwNameUtils.toConverterFieldName(elemName);
294 		}
295 
296 		if (function instanceof SubcriteriaFunction) {
297 			String option = ((SubcriteriaFunction) function)
298 					.getSequentialFunction().getOption();
299 			if ("list".equals(option)) {
300 				TgwAttribute attr = getEntity().getField(function.getField());
301 				return (String) attr.accept(visitor, elemName);
302 			}
303 		}
304 		return (String) field.accept(visitor, elemName);
305 	}
306 
307 	private String createNestedFieldName(String prefix,
308 			CriteriaFunction function) {
309 		if (function instanceof SubcriteriaFunction) {
310 			return createNestedFieldName(prefix + function.getField() + ".",
311 					((SubcriteriaFunction) function).getSequentialFunction());
312 		}
313 		return prefix + function.getField();
314 	}
315 
316 	private String toDisplayName(TgwAttribute attr, CriteriaFunction function) {
317 
318 		if (function instanceof SubcriteriaFunction) {
319 			attr = getEntity().getField(function.getField());
320 		}
321 
322 		if (alternateDisplayMap != null
323 				&& alternateDisplayMap.get(attr.getName()) != null) {
324 			return (String) alternateDisplayMap.get(attr.getName());
325 		} else {
326 			return attr.getDisplayName();
327 		}
328 	}
329 }