root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / data / feature / db / jdbc / h2 / H2FeatureCollectionEditingFiltered.java @ 24491
History | View | Annotate | Download (5.39 KB)
1 |
package org.gvsig.fmap.data.feature.db.jdbc.h2; |
---|---|
2 |
|
3 |
|
4 |
import java.util.ConcurrentModificationException; |
5 |
import java.util.Iterator; |
6 |
import java.util.NoSuchElementException; |
7 |
|
8 |
import org.gvsig.fmap.dal.DataManager; |
9 |
import org.gvsig.fmap.dal.DataStore; |
10 |
import org.gvsig.fmap.dal.ReadException; |
11 |
import org.gvsig.fmap.dal.feature.Feature; |
12 |
import org.gvsig.fmap.dal.feature.FeatureManager; |
13 |
import org.gvsig.fmap.dal.feature.FeatureType; |
14 |
import org.gvsig.fmap.dal.feature.expressionevaluator.Filter; |
15 |
import org.gvsig.fmap.data.feature.db.DBDataFeatureCollection; |
16 |
import org.gvsig.fmap.data.feature.db.DBFeatureType; |
17 |
import org.gvsig.tools.exception.BaseException; |
18 |
|
19 |
public class H2FeatureCollectionEditingFiltered extends DBDataFeatureCollection { |
20 |
protected DBFeatureType featureType;
|
21 |
protected String totalFilter; |
22 |
protected String filter; |
23 |
protected H2Store store;
|
24 |
private int numReg=-1; |
25 |
private String sql; |
26 |
private String sqlCount; |
27 |
private String totalOrder; |
28 |
private FeatureManager featureManager;
|
29 |
private Filter parser; |
30 |
private int fetchSize = 5000; |
31 |
|
32 |
|
33 |
H2FeatureCollectionEditingFiltered(FeatureManager fm, H2Store store, |
34 |
FeatureType type, String filter) throws ReadException { |
35 |
this.featureManager=fm;
|
36 |
this.store=store;
|
37 |
this.featureType=(DBFeatureType)type;
|
38 |
|
39 |
this.filter = filter;
|
40 |
|
41 |
this.calculateWhere();
|
42 |
this.totalOrder = store.getBaseOrder();
|
43 |
|
44 |
this.sql = this.store.getSqlSelectPart(); |
45 |
this.sqlCount = "Select count(*) From "+ ((H2StoreParameters)this.store.getParameters()).tableID(); |
46 |
if (!isStringEmpty(this.totalFilter)){ |
47 |
this.sql= this.sql + " Where " + this.totalFilter; |
48 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
49 |
} |
50 |
if (!isStringEmpty(this.totalOrder)){ |
51 |
this.sql= this.sql + " Order by " + this.totalOrder; |
52 |
} |
53 |
|
54 |
if (this.filter!=null) { |
55 |
|
56 |
parser = DataManager.getManager().getExpressionParser() |
57 |
.parseFilter(filter); |
58 |
} |
59 |
|
60 |
} |
61 |
|
62 |
private void calculateWhere(){ |
63 |
if (isStringEmpty(this.store.getBaseWhereClause())){ |
64 |
this.totalFilter = this.filter; |
65 |
} else {
|
66 |
this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter; |
67 |
} |
68 |
} |
69 |
|
70 |
public int size() { |
71 |
checkModified(); |
72 |
if (this.numReg < 0){ |
73 |
this.numReg=0; |
74 |
try{
|
75 |
Iterator iter =this.iterator(); |
76 |
while (true){ |
77 |
iter.next(); |
78 |
numReg++; |
79 |
} |
80 |
} catch (NoSuchElementException e){ |
81 |
//Normal condition exit
|
82 |
} |
83 |
} |
84 |
return numReg;
|
85 |
|
86 |
} |
87 |
|
88 |
protected Iterator internalIterator(int index) { |
89 |
checkModified(); |
90 |
try{
|
91 |
H2Iterator dbIter = new H2Iterator(this.store, this.featureType, |
92 |
this.sql, this.fetchSize, this.featureManager, this.parser, |
93 |
index + 1);
|
94 |
return dbIter;
|
95 |
} catch (BaseException e){
|
96 |
throw new RuntimeException(e); |
97 |
} |
98 |
} |
99 |
|
100 |
protected class H2Iterator extends AbstractH2DBIterator { |
101 |
|
102 |
public H2Iterator(H2Store store, DBFeatureType featureType,
|
103 |
String sql,
|
104 |
int fetchSize, FeatureManager featureManager,
|
105 |
Filter featureFilter, int initialPosition) |
106 |
throws ReadException {
|
107 |
super(store, featureType, sql, fetchSize, featureManager,
|
108 |
featureFilter, initialPosition); |
109 |
} |
110 |
|
111 |
protected Feature createFeatureFromTheResulset() throws ReadException { |
112 |
return ((H2Store) this.store).createFeatureFromResulset(this.rs, |
113 |
this.featureType);
|
114 |
} |
115 |
|
116 |
protected void checkModified() { |
117 |
if (modified) {
|
118 |
throw new ConcurrentModificationException( |
119 |
"FeatureCollection modified");
|
120 |
} |
121 |
} |
122 |
|
123 |
} |
124 |
|
125 |
|
126 |
protected class H2IteratorMemory implements Iterator{ |
127 |
protected long position=0; |
128 |
private boolean nextChecked=false; |
129 |
private Feature feature;
|
130 |
private FeatureManager featureManager;
|
131 |
private FeatureType featureType;
|
132 |
|
133 |
public H2IteratorMemory(DBFeatureType featureType, FeatureManager featureManager){
|
134 |
this.featureType = featureType;
|
135 |
position=0;
|
136 |
this.featureManager=featureManager;
|
137 |
} |
138 |
|
139 |
protected void checkModified(){ |
140 |
if (modified) {
|
141 |
throw new ConcurrentModificationException("FeatureCollection modified"); |
142 |
} |
143 |
} |
144 |
|
145 |
public boolean hasNext(){ |
146 |
checkModified(); |
147 |
|
148 |
|
149 |
if (nextChecked){
|
150 |
return this.feature != null; |
151 |
} |
152 |
Feature feature=null;
|
153 |
nextChecked=true;
|
154 |
while (true){ |
155 |
if (position<featureManager.getNum()){
|
156 |
try {
|
157 |
feature=featureManager.get((int)position,store,this.featureType); |
158 |
} catch (ReadException e) {
|
159 |
throw new RuntimeException( |
160 |
new ReadException(store.getName(),e)
|
161 |
); |
162 |
} |
163 |
position++; |
164 |
|
165 |
}else{
|
166 |
this.feature = null; |
167 |
return false; |
168 |
} |
169 |
|
170 |
if(featureManager.isDeleted(feature)) {
|
171 |
continue;
|
172 |
} |
173 |
|
174 |
if (filter == null) { |
175 |
this.feature=feature;
|
176 |
return true; |
177 |
|
178 |
} else {
|
179 |
try {
|
180 |
if (parser.evaluate(feature)) {
|
181 |
this.feature=feature;
|
182 |
return true; |
183 |
}else{
|
184 |
continue;
|
185 |
} |
186 |
} catch (Exception e) { |
187 |
throw new RuntimeException(e); |
188 |
} |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
public Object next() { |
194 |
checkModified(); |
195 |
if (!nextChecked){
|
196 |
hasNext(); |
197 |
} |
198 |
if (this.feature == null) { |
199 |
throw new NoSuchElementException(); |
200 |
} |
201 |
nextChecked=false;
|
202 |
Feature feature = this.feature;
|
203 |
this.feature = null; |
204 |
return feature;
|
205 |
} |
206 |
|
207 |
public void remove() { |
208 |
throw new UnsupportedOperationException(); |
209 |
} |
210 |
} |
211 |
|
212 |
|
213 |
public void dispose() { |
214 |
this.store.deleteObserver(this); |
215 |
this.store = null; |
216 |
this.featureManager = null; |
217 |
this.featureType = null; |
218 |
this.parser= null; |
219 |
|
220 |
} |
221 |
|
222 |
public boolean isFromStore(DataStore store) { |
223 |
return this.store.equals(store); |
224 |
} |
225 |
|
226 |
public FeatureType getFeatureType() {
|
227 |
return this.featureType; |
228 |
} |
229 |
} |