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.database.function.criteria;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.commons.beanutils.PropertyUtils;
24  
25  public class CriteriaListFunction implements CriteriaFunction {
26  
27  	private boolean isDisjunction_;
28  
29  	private List functionList_ = new ArrayList();
30  
31  	private String name;
32  
33  	public CriteriaListFunction() {
34  	}
35  
36  	public static CriteriaListFunction create(int firstResult, int maxResult) {
37  		CriteriaListFunction func = new CriteriaListFunction();
38  		if (firstResult > 0) {
39  			func.addFunction(new FirstResultCriteriaFunction(firstResult));
40  		}
41  		if (maxResult > 0) {
42  			func.addFunction(new MaxResultCriteriaFunction(maxResult));
43  		}
44  		return func;
45  	}
46  	
47  	public static CriteriaFunction addCriteria(CriteriaFunction origCriteria, CriteriaFunction addCriteria){
48  		if(origCriteria == null){
49  			return addCriteria;
50  		}else if(origCriteria instanceof CriteriaListFunction){
51  			((CriteriaListFunction)origCriteria).addFunction(addCriteria);
52  			return origCriteria;
53  		}else{
54  			return new CriteriaListFunction(origCriteria, addCriteria);
55  		}
56  	}
57  	
58  	public CriteriaListFunction(CriteriaFunction obj) {
59  		functionList_.add(obj);
60  	}
61  
62  	public CriteriaListFunction(CriteriaFunction left, CriteriaFunction right) {
63  		functionList_.add(left);
64  		functionList_.add(right);
65  	}
66  
67  	public void setName(String name) {
68  		this.name = name;
69  	}
70  
71  	public String getName() {
72  		return name;
73  	}
74  
75  	public boolean isDisjunction() {
76  		return isDisjunction_;
77  	}
78  
79  	public void setDisjunction(boolean disjunction) {
80  		this.isDisjunction_ = disjunction;
81  	}
82  
83  	public List getFunctionList() {
84  		return functionList_;
85  	}
86  
87  	public CriteriaListFunction replace(String name, CriteriaFunction newFunc) {
88  		CriteriaListFunction list = new CriteriaListFunction();
89  		for (Iterator i = functionList_.iterator(); i.hasNext();) {
90  			CriteriaFunction func = (CriteriaFunction) i.next();
91  			String aName = func.getName();
92  			if (name.equals(aName)) {
93  				list.addFunction(newFunc);
94  			} else {
95  				list.addFunction(func);
96  			}
97  		}
98  		return list;
99  	}
100 
101 	public void injectCriteriaListFunction(CriteriaFunction func) {
102 		if (func instanceof CriteriaListFunction) {
103 			CriteriaListFunction listFunction = (CriteriaListFunction) func;
104 			Iterator itr = listFunction.getFunctionList().iterator();
105 			while (itr.hasNext()) {
106 				CriteriaFunction criteriaFunction = (CriteriaFunction) itr
107 						.next();
108 				addFunction(criteriaFunction);
109 			}
110 		} else {
111 			addFunction(func);
112 		}
113 	}
114 
115 	public void addFunction(CriteriaFunction function) {
116 		if (function != null) {
117 			functionList_.add(function);
118 		}
119 	}
120 
121 	public Object evaluate(Object obj) {
122 		return evaluate((ICriteria) obj);
123 	}
124 
125 	public Object evaluate(ICriteria criteria) {
126 		Iterator itr = functionList_.iterator();
127 
128 		ICriteria listCriteria = disjunctionCheck(criteria);
129 
130 		while (itr.hasNext()) {
131 			UnaryCriteriaFunction function = (UnaryCriteriaFunction) itr.next();
132 			function.evaluate(listCriteria);
133 		}
134 		return criteria;
135 	}
136 
137 	public Object evaluate(Object left, Object right) {
138 		return evaluate((ICriteria) left, right);
139 	}
140 
141 	public Object evaluate(ICriteria criteria, Object dto) {
142 		Iterator itr = functionList_.iterator();
143 
144 		ICriteria listCriteria = disjunctionCheck(criteria);
145 
146 		while (itr.hasNext()) {
147 			BinaryCriteriaFunction function = (BinaryCriteriaFunction) itr
148 					.next();
149 			String name = function.getName();
150 
151 			try {
152 				Object value = PropertyUtils.getProperty(dto, name);
153 				function.evaluate(listCriteria, value);
154 			} catch (IllegalAccessException e) {
155 				// do nothing
156 			} catch (InvocationTargetException e) {
157 				// do nothing
158 			} catch (NoSuchMethodException e) {
159 				// do nothing
160 			}
161 		}
162 		return criteria;
163 	}
164 
165 	private ICriteria disjunctionCheck(ICriteria criteria) {
166 		if (isDisjunction_) {
167 			return criteria.disjunction();
168 		} else {
169 			return criteria;
170 		}
171 	}
172 
173 	public void setValue(Object value) {
174 		// do nothing
175 	}
176 
177 	public void setField(String field) {
178 		// do nothing
179 	}
180 
181 	public String getField() {
182 		return null;
183 	}
184 
185 	public void setOption(String option) {
186 		// do nothing
187 	}
188 
189 	public String getOption() {
190 		return null;
191 	}
192 
193 	public Object getValue() {
194 		return null;
195 	}
196 }