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;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.seasar.tuigwaa.database.DataTable;
26  import org.seasar.tuigwaa.database.DataTableImpl;
27  import org.seasar.tuigwaa.database.ExtraRowDataTable;
28  import org.seasar.tuigwaa.database.SelectedColumnDataTableImpl;
29  import org.seasar.tuigwaa.database.function.criteria.CriteriaFunction;
30  import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
31  import org.seasar.tuigwaa.database.function.criteria.DataTableCriteria;
32  import org.seasar.tuigwaa.database.function.criteria.ICriteria;
33  import org.seasar.tuigwaa.database.function.criteria.UnaryCriteriaFunction;
34  import org.seasar.tuigwaa.util.functor.Op;
35  
36  import com.isenshi.util.functor.Function;
37  import com.isenshi.util.functor.UnaryFunction;
38  
39  public class CriteriaExeFunction extends AbstractCriteriaExeFunction implements
40  		Function, UnaryFunction {
41  
42  	private boolean valueFlag = false;
43  
44  	private boolean spreadSheetFlag = false;
45  
46  	private UnaryCriteriaFunction unaryFunction_;
47  
48  	private UnaryCriteriaFunction dataTableFilter_;
49  
50  	private List selectedHeaders;
51  
52  	private boolean extraRowFlag;
53  
54  	private Map extraProjectionMap = new HashMap();
55  
56  	public void setDataTableFilter(UnaryCriteriaFunction function) {
57  		this.dataTableFilter_ = function;
58  	}
59  
60  	// [Start] ----- Setter Getter Method ------
61  
62  	public UnaryCriteriaFunction getDataTableFilter() {
63  		return dataTableFilter_;
64  	}
65  
66  	public UnaryCriteriaFunction getUnaryCriteriaFunction() {
67  		return unaryFunction_;
68  	}
69  
70  	public void setUnaryCriteriaFunction(UnaryCriteriaFunction unaryFunction) {
71  		this.unaryFunction_ = unaryFunction;
72  	}
73  
74  	public List getSelectedHeaders() {
75  		if (selectedHeaders != null && selectedHeaders.size() > 0) {
76  			List list = new ArrayList();
77  			Iterator itr = selectedHeaders.iterator();
78  			while (itr.hasNext()) {
79  				String str = (String) itr.next();
80  				if (str != null && !"".equals(str)) {
81  					list.add(str);
82  				}
83  			}
84  			if (list.size() > 0) {
85  				return list;
86  			}
87  		}
88  		return null;
89  	}
90  
91  	public void addSelectedHeader(String header) {
92  		if (selectedHeaders == null) {
93  			selectedHeaders = new ArrayList();
94  		}
95  		if (StringUtils.isNotEmpty(header)) {
96  			selectedHeaders.add(header);
97  		}
98  
99  	}
100 
101 	public boolean isValueFlag() {
102 		return valueFlag;
103 	}
104 
105 	public void setValueFlag(boolean valueFlag) {
106 		this.valueFlag = valueFlag;
107 	}
108 
109 	public boolean isSpreadSheetFlag() {
110 		return spreadSheetFlag;
111 	}
112 
113 	public void setSpreadSheetFlag(boolean spreadSheetFlag) {
114 		this.spreadSheetFlag = spreadSheetFlag;
115 	}
116 
117 	public void setExtraRowFlag(boolean extraRowFlag) {
118 		this.extraRowFlag = extraRowFlag;
119 	}
120 
121 	// [End] ----- Setter Getter Method ------
122 
123 	public Object evaluate() {
124 		return evaluate(null);
125 	}
126 
127 	public Object evaluate(Object injectFunction) {
128 		ICriteria criteria = createHibernateCriteria();
129 		criteria = (ICriteria) createCriteriaFunction(injectFunction).evaluate(
130 				criteria);
131 
132 		DataTable dataTable = criteria.list();
133 		if (getDataTableFilter() != null) {
134 			ICriteria dataCriteria = new DataTableCriteria(dataTable);
135 			dataCriteria = (ICriteria) getDataTableFilter().evaluate(
136 					dataCriteria);
137 			dataTable = dataCriteria.list();
138 		}
139 		if (extraRowFlag) {
140 			dataTable = new ExtraRowDataTable(dataTable, Op.SUM);
141 		}
142 		return convert(dataTable);
143 	}
144 
145 	// [Start] ------ Private Method -------
146 
147 	private UnaryCriteriaFunction createCriteriaFunction(Object injectFunction) {
148 		if (injectFunction == null) {
149 			return getUnaryCriteriaFunction();
150 		}
151 		CriteriaListFunction listFunction = new CriteriaListFunction();
152 		listFunction.addFunction((CriteriaFunction) injectFunction);   //後から付け加えたCriteria を先に実行
153 		listFunction.addFunction((CriteriaFunction) getUnaryCriteriaFunction());
154 		return listFunction;
155 	}
156 
157 	private Object convert(DataTable dataTable) {
158 		if (getSelectedHeaders() != null) {
159 			return new SelectedColumnDataTableImpl(dataTable,
160 					getSelectedHeaders());
161 		} else if (isSpreadSheetFlag()) {
162 			return ((DataTableImpl) dataTable).createSpreadSheet();
163 		} else if (isValueFlag()) {
164 			return dataTable.getData(0, 0);
165 		} else {
166 			return dataTable;
167 		}
168 	}
169 }