svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2FeatureCollectionEditingFiltered.java @ 20058
History | View | Annotate | Download (7.43 KB)
1 |
package org.gvsig.data.datastores.vectorial.db.jdbc.h2; |
---|---|
2 |
|
3 |
|
4 |
import java.sql.Connection; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.Statement; |
7 |
import java.util.ConcurrentModificationException; |
8 |
import java.util.Iterator; |
9 |
import java.util.NoSuchElementException; |
10 |
|
11 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
12 |
import org.gvsig.data.datastores.vectorial.db.jdbc.AbstractJDBCDataFeatureCollection; |
13 |
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore; |
14 |
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException; |
15 |
import org.gvsig.data.exception.ReadException; |
16 |
import org.gvsig.data.vectorial.AbstractFeatureCollection; |
17 |
import org.gvsig.data.vectorial.IFeature; |
18 |
import org.gvsig.data.vectorial.IFeatureType; |
19 |
import org.gvsig.data.vectorial.expansionadapter.FeatureManager; |
20 |
import org.gvsig.data.vectorial.filter.FeatureFilterParser; |
21 |
import org.gvsig.exceptions.BaseException; |
22 |
|
23 |
public class H2FeatureCollectionEditingFiltered extends AbstractJDBCDataFeatureCollection { |
24 |
protected DBFeatureType featureType;
|
25 |
protected String totalFilter; |
26 |
protected String filter; |
27 |
protected H2Store store;
|
28 |
private int numReg=-1; |
29 |
private String sql; |
30 |
private String sqlCount; |
31 |
private String totalOrder; |
32 |
private FeatureManager featureManager;
|
33 |
private FeatureFilterParser parser;
|
34 |
|
35 |
|
36 |
H2FeatureCollectionEditingFiltered(FeatureManager fm,H2Store store,IFeatureType type,String filter) {
|
37 |
this.featureManager=fm;
|
38 |
this.store=store;
|
39 |
this.featureType=(DBFeatureType)type;
|
40 |
|
41 |
this.filter = filter;
|
42 |
|
43 |
this.calculateWhere();
|
44 |
this.totalOrder = store.getBaseOrder();
|
45 |
|
46 |
this.sql = this.store.getSqlSelectPart(); |
47 |
this.sqlCount = "Select count(*) From "+ ((H2StoreParameters)this.store.getParameters()).tableID(); |
48 |
if (!isStringEmpty(this.totalFilter)){ |
49 |
this.sql= this.sql + " Where " + this.totalFilter; |
50 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
51 |
} |
52 |
if (!isStringEmpty(this.totalOrder)){ |
53 |
this.sql= this.sql + " Order by " + this.totalOrder; |
54 |
} |
55 |
|
56 |
if (this.filter!=null) |
57 |
parser = new FeatureFilterParser(filter,this.featureType); |
58 |
|
59 |
} |
60 |
|
61 |
private void calculateWhere(){ |
62 |
if (isStringEmpty(this.store.getBaseWhereClause())){ |
63 |
this.totalFilter = this.filter; |
64 |
} else {
|
65 |
this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter; |
66 |
} |
67 |
} |
68 |
|
69 |
private ResultSet getNewResulset(String aSql) throws ReadException{ |
70 |
this.store.open();
|
71 |
|
72 |
Connection con = this.store.getCurrentConnection(); |
73 |
|
74 |
try {
|
75 |
Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
76 |
return st.executeQuery(aSql);
|
77 |
|
78 |
} catch (java.sql.SQLException e) {
|
79 |
throw new SQLException(aSql,this.store.getName(),e); |
80 |
} |
81 |
|
82 |
} |
83 |
|
84 |
public int size() { |
85 |
checkModified(); |
86 |
if (this.numReg < 0){ |
87 |
this.numReg=0; |
88 |
try{
|
89 |
Iterator iter =this.iterator(); |
90 |
while (true){ |
91 |
iter.next(); |
92 |
numReg++; |
93 |
} |
94 |
} catch (NoSuchElementException e){ |
95 |
//Normal condition exit
|
96 |
} |
97 |
} |
98 |
return numReg;
|
99 |
|
100 |
} |
101 |
|
102 |
|
103 |
public Iterator iterator() { |
104 |
checkModified(); |
105 |
try{
|
106 |
ResultSet r=null; |
107 |
r = this.getNewResulset(this.sql); |
108 |
H2Iterator dbIter=new H2Iterator(this.store,r,this.featureType,this.featureManager); |
109 |
return dbIter;
|
110 |
} catch (BaseException e){
|
111 |
throw new RuntimeException(e); |
112 |
} |
113 |
} |
114 |
|
115 |
protected class H2Iterator implements Iterator{ |
116 |
private Iterator dbIter; |
117 |
private Iterator mIter; |
118 |
public H2Iterator(H2Store store, ResultSet rs, DBFeatureType featureType,FeatureManager fm){ |
119 |
this.dbIter = new H2IteratorDB(store,rs,featureType,fm); |
120 |
this.mIter = new H2IteratorMemory(featureType,fm); |
121 |
} |
122 |
public boolean hasNext() { |
123 |
checkModified(); |
124 |
return dbIter.hasNext() || mIter.hasNext();
|
125 |
} |
126 |
public Object next() { |
127 |
checkModified(); |
128 |
if (dbIter.hasNext()){
|
129 |
return dbIter.next();
|
130 |
} |
131 |
return mIter.next();
|
132 |
} |
133 |
public void remove() { |
134 |
throw new UnsupportedOperationException(); |
135 |
} |
136 |
|
137 |
|
138 |
|
139 |
} |
140 |
|
141 |
protected class H2IteratorDB implements Iterator{ |
142 |
private boolean nextChecked=false; |
143 |
private IFeature feature;
|
144 |
private ResultSet rs; |
145 |
private H2Store store;
|
146 |
private DBFeatureType featureType;
|
147 |
private boolean rsEOF=false; |
148 |
private FeatureManager featureManager;
|
149 |
|
150 |
public H2IteratorDB(H2Store store, ResultSet rs, DBFeatureType featureType, FeatureManager featureManager){ |
151 |
this.store = store;
|
152 |
this.rs = rs;
|
153 |
this.featureType = featureType;
|
154 |
this.featureManager = featureManager;
|
155 |
} |
156 |
|
157 |
protected void checkModified(){ |
158 |
if (modified)
|
159 |
throw new ConcurrentModificationException("FeatureCollection modified"); |
160 |
} |
161 |
|
162 |
|
163 |
public boolean hasNext(){ |
164 |
checkModified(); |
165 |
|
166 |
|
167 |
if (nextChecked){
|
168 |
return this.feature != null; |
169 |
} |
170 |
IFeature feature=null;
|
171 |
nextChecked=true;
|
172 |
while (true){ |
173 |
if (!rsEOF){
|
174 |
try {
|
175 |
if (rs.isLast()){
|
176 |
rs.close(); |
177 |
rsEOF=true;
|
178 |
continue;
|
179 |
} else {
|
180 |
feature=nextH2Feature(); |
181 |
if(this.featureManager.isDeleted(feature)) |
182 |
continue;
|
183 |
this.feature=feature;
|
184 |
return true; |
185 |
|
186 |
} |
187 |
} catch (java.sql.SQLException e) {
|
188 |
throw new RuntimeException( |
189 |
new ReadException(this.store.getName(),e) |
190 |
); |
191 |
} |
192 |
|
193 |
}else {
|
194 |
return false; |
195 |
|
196 |
} |
197 |
|
198 |
|
199 |
} |
200 |
} |
201 |
|
202 |
public Object next() { |
203 |
checkModified(); |
204 |
if (!nextChecked){
|
205 |
hasNext(); |
206 |
} |
207 |
if (this.feature == null) |
208 |
throw new NoSuchElementException(); |
209 |
nextChecked=false;
|
210 |
IFeature feature = this.feature;
|
211 |
this.feature = null; |
212 |
return feature;
|
213 |
} |
214 |
|
215 |
private IFeature nextH2Feature() {
|
216 |
IFeature feature=null;
|
217 |
try {
|
218 |
if(rs.next()){
|
219 |
feature=this.store.createFeatureFromResulset(this.rs, featureType); |
220 |
} |
221 |
} catch (java.sql.SQLException e) {
|
222 |
throw new RuntimeException( |
223 |
new ReadException(this.store.getName(),e) |
224 |
); |
225 |
} catch (ReadException e) {
|
226 |
throw new RuntimeException(e); |
227 |
} |
228 |
return feature;
|
229 |
} |
230 |
|
231 |
public void remove() { |
232 |
throw new UnsupportedOperationException(); |
233 |
} |
234 |
|
235 |
} |
236 |
|
237 |
|
238 |
protected class H2IteratorMemory implements Iterator{ |
239 |
protected long position=0; |
240 |
private boolean nextChecked=false; |
241 |
private IFeature feature;
|
242 |
private FeatureManager featureManager;
|
243 |
|
244 |
public H2IteratorMemory(DBFeatureType featureType, FeatureManager featureManager){
|
245 |
position=0;
|
246 |
this.featureManager=featureManager;
|
247 |
} |
248 |
|
249 |
protected void checkModified(){ |
250 |
if (modified)
|
251 |
throw new ConcurrentModificationException("FeatureCollection modified"); |
252 |
} |
253 |
|
254 |
public boolean hasNext(){ |
255 |
checkModified(); |
256 |
|
257 |
|
258 |
if (nextChecked){
|
259 |
return this.feature != null; |
260 |
} |
261 |
IFeature feature=null;
|
262 |
nextChecked=true;
|
263 |
while (true){ |
264 |
if (position<featureManager.getNum()){
|
265 |
feature=featureManager.getFeature((int)position);
|
266 |
position++; |
267 |
|
268 |
}else{
|
269 |
this.feature = null; |
270 |
return false; |
271 |
} |
272 |
|
273 |
if(featureManager.isDeleted(feature))
|
274 |
continue;
|
275 |
|
276 |
if (filter == null) { |
277 |
this.feature=feature;
|
278 |
return true; |
279 |
|
280 |
} else {
|
281 |
try {
|
282 |
if (parser.match(feature)){
|
283 |
this.feature=feature;
|
284 |
return true; |
285 |
}else{
|
286 |
continue;
|
287 |
} |
288 |
} catch (Exception e) { |
289 |
throw new RuntimeException(e); |
290 |
} |
291 |
} |
292 |
} |
293 |
} |
294 |
|
295 |
public Object next() { |
296 |
checkModified(); |
297 |
if (!nextChecked){
|
298 |
hasNext(); |
299 |
} |
300 |
if (this.feature == null) |
301 |
throw new NoSuchElementException(); |
302 |
nextChecked=false;
|
303 |
IFeature feature = this.feature;
|
304 |
this.feature = null; |
305 |
return feature;
|
306 |
} |
307 |
|
308 |
public void remove() { |
309 |
throw new UnsupportedOperationException(); |
310 |
} |
311 |
} |
312 |
|
313 |
|
314 |
public void dispose() { |
315 |
this.store.deleteObserver(this); |
316 |
this.store = null; |
317 |
this.featureManager = null; |
318 |
this.featureType = null; |
319 |
this.parser= null; |
320 |
|
321 |
} |
322 |
|
323 |
} |