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.text.ParseException;
19  import java.util.Date;
20  
21  import org.seasar.tuigwaa.database.DataRow;
22  import org.seasar.tuigwaa.database.DataTable;
23  import org.seasar.tuigwaa.database.function.aggregation.IProjectionList;
24  import org.seasar.tuigwaa.model.core.TgwEntity;
25  import org.seasar.tuigwaa.system.Constants;
26  import org.seasar.tuigwaa.util.functor.Op;
27  
28  import com.isenshi.util.functor.BinaryPredicate;
29  
30  public class DataTableCriteria implements ICriteria {
31  
32  	private DataTable dataTable_;
33  
34  	public DataTableCriteria(DataTable dataTable) {
35  		this.dataTable_ = dataTable;
36  	}
37  
38  	public TgwEntity getEntity() {
39  		return null;
40  	}
41  
42  	private void doFilter(BinaryPredicate predicate, String field, Object value) {
43  		while (dataTable_.hasNext()) {
44  			DataRow row = (DataRow) dataTable_.next();
45  			Object data = row.getCell(field);
46  			if (!predicate.test(data, value)) {
47  				dataTable_.remove();
48  			}
49  		}
50  	}
51  
52  	public ICriteria addEq(String field, Object value) {
53  		doFilter(Op.EQ, field, value);
54  		return this;
55  	}
56  
57  	public ICriteria addLe(String field, Object value) {
58  		doFilter(Op.LE, field, value);
59  		return this;
60  	}
61  
62  	public ICriteria addGe(String field, Object value) {
63  		doFilter(Op.GE, field, value);
64  		return this;
65  	}
66  
67  	public ICriteria createCriteria(String field) {
68  		throw new RuntimeException("this method is not supported.");
69  	}
70  
71  	public ICriteria createCriteria(String field, String alias) {
72  		throw new RuntimeException("this method is not supported.");
73  	}
74  
75  	public ICriteria setProjection(IProjectionList projectionList) {
76  		throw new RuntimeException();
77  		// DataTableProjectionList dlist =
78  		// (DataTableProjectionList)projectionList;
79  		// String name = dataTable_.getName();
80  		// List headers = dlist.getAliases();
81  		// List data = new ArrayList();
82  		// DataTable newDataTable = new DataTableImpl(name, headers, data,
83  		// DataTable.PRIMITIVE_DATA);
84  		// while(dataTable_.hasNext()){
85  		// DataRow row = dataTable_.nextRow();
86  		// for(Iterator i = headers.iterator(); i.hasNext();){
87  		// String alias = (String)i.next();
88  		// }
89  		// }
90  		// return this;
91  	}
92  
93  	public DataTable list() {
94  		dataTable_.init();
95  		return dataTable_;
96  	}
97  
98  	public ICriteria addOrder(String field, boolean isDesc) {
99  		// TODO Auto-generated method stub
100 		return null;
101 	}
102 
103 	public ICriteria disjunction() {
104 		throw new UnsupportedOperationException("this method is not supported.");
105 	}
106 
107 	public ICriteria addLt(String field, Object value) {
108 		doFilter(Op.LT, field, value);
109 		return this;
110 	}
111 
112 	public ICriteria addGt(String field, Object value) {
113 		doFilter(Op.GT, field, value);
114 		return this;
115 	}
116 
117 	public ICriteria addNe(String field, Object value) {
118 		doFilter(Op.EQ, field, value);
119 		return this;
120 	}
121 
122 	private static final BinaryPredicate BETWEEN = new BinaryPredicate() {
123 		public boolean test(Object left, Object right) {
124 			Object[] array = (Object[]) right;
125 			Date date = null;
126 			try {
127 				date = Constants.LIGHT_DATEFORMAT.parse((String) left);
128 			} catch (ParseException e) {
129 				e.printStackTrace();
130 				throw new RuntimeException();
131 			}
132 			return Op.GE.test(date, array[0]) && Op.LE.test(date, array[1]);
133 		}
134 	};
135 
136 	public ICriteria addBetween(String field, Object value) {
137 		doFilter(BETWEEN, field, value);
138 		return this;
139 	}
140 
141 	public ICriteria addLike(String field, Object value) {
142 		// TODO Auto-generated method stub
143 		return this;
144 	}
145 
146 	public ICriteria setMaxResult(int maxResult) {
147 		for (int i = 0; dataTable_.hasNext(); i++) {
148 			dataTable_.next();
149 			if (i > maxResult) {
150 				dataTable_.remove();
151 			}
152 		}
153 		return this;
154 	}
155 
156 	public ICriteria setFirstResult(int firstResult) {
157 		for (int i = 0; dataTable_.hasNext(); i++) {
158 			dataTable_.next();
159 			if (i < firstResult) {
160 				dataTable_.remove();
161 			}
162 		}
163 		return this;
164 	}
165 
166 	public ICriteria addIsNull(String field) {
167 		// unsupported
168 		return this;
169 	}
170 	
171 	public ICriteria addIsNotNull(String field){
172 		return this;
173 	}
174 	
175 	public void setJoinType(int joinType){
176 		// unsupported
177 	}
178 
179 }