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.aggregation;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.seasar.tuigwaa.database.function.criteria.AbstractCriteriaFunction;
23  import org.seasar.tuigwaa.database.function.criteria.ICriteria;
24  
25  import com.isenshi.util.functor.UnaryFunction;
26  
27  public class AggregationFunction extends AbstractCriteriaFunction {
28  
29  	private List functionList = new ArrayList();
30  
31  	public List getFunctionList() {
32  		return functionList;
33  	}
34  
35  	public void addFunction(UnaryFunction function) {
36  		if(function!=null){
37  			functionList.add(function);
38  		}
39  	}
40  
41  	public Object evaluate(ICriteria criteria) {
42  		return evaluate(criteria, null);
43  	}
44  
45  	public Object evaluate(ICriteria criteria, Object value) {
46  		IProjectionList projectionList = new HibernateProjectionList();
47  		//List groupPropertyList = new ArrayList();
48  
49  		for (Iterator i = functionList.iterator(); i.hasNext();) {
50  			ProjectionFunction function = (ProjectionFunction) i.next();
51  			String field = function.getField();
52  
53  			//if (function instanceof GroupProjectionFunction) {
54  //				groupPropertyList.add(field);
55  	//		}
56  
57  			if (field!=null&&field.indexOf(".") > 0) {
58  				int index = field.indexOf(".");
59  				String subcriteria = field.substring(0, index);
60  				criteria.createCriteria(subcriteria, subcriteria);
61  			}
62  			projectionList = (IProjectionList) function.evaluate(projectionList);
63  		}
64  		return criteria.setProjection(projectionList);
65  	}
66  }