Statistics
| Revision:

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 19845 vcaballero
package org.gvsig.data.datastores.vectorial.db.jdbc.h2;
2 19663 jmvivo
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 19893 jmvivo
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 19845 vcaballero
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException;
15 19663 jmvivo
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 19893 jmvivo
public class H2FeatureCollectionEditingFiltered extends AbstractJDBCDataFeatureCollection {
24 19663 jmvivo
        protected DBFeatureType featureType;
25
        protected String totalFilter;
26
        protected String filter;
27 19738 vcaballero
        protected H2Store store;
28 19663 jmvivo
        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 19965 jmvivo
        H2FeatureCollectionEditingFiltered(FeatureManager fm,H2Store store,IFeatureType type,String filter) {
37 19663 jmvivo
                this.featureManager=fm;
38 19738 vcaballero
                this.store=store;
39 19663 jmvivo
                this.featureType=(DBFeatureType)type;
40
41
                this.filter = filter;
42
43
                this.calculateWhere();
44 19738 vcaballero
                this.totalOrder = store.getBaseOrder();
45 19663 jmvivo
46 19738 vcaballero
                this.sql = this.store.getSqlSelectPart();
47 19781 jmvivo
                this.sqlCount = "Select count(*) From "+ ((H2StoreParameters)this.store.getParameters()).tableID();
48 19663 jmvivo
                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 19738 vcaballero
                if (isStringEmpty(this.store.getBaseWhereClause())){
63 19663 jmvivo
                        this.totalFilter = this.filter;
64
                } else {
65 19738 vcaballero
                        this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter;
66 19663 jmvivo
                }
67
        }
68
69
        private ResultSet getNewResulset(String aSql) throws ReadException{
70 19738 vcaballero
                this.store.open();
71 19663 jmvivo
72 19893 jmvivo
                Connection con = this.store.getCurrentConnection();
73 19663 jmvivo
74
                try {
75 19893 jmvivo
                        Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
76 19975 jmvivo
                        return st.executeQuery(aSql);
77 19663 jmvivo
78
                } catch (java.sql.SQLException e) {
79 19906 jmvivo
                        throw new SQLException(aSql,this.store.getName(),e);
80 19663 jmvivo
                }
81
82
        }
83
84
        public int size() {
85 19906 jmvivo
                checkModified();
86 20014 jmvivo
                if (this.numReg < 0){
87
                        this.numReg=0;
88
                        try{
89
                                Iterator iter =this.iterator();
90
                                while (true){
91
                                        iter.next();
92
                                        numReg++;
93 19663 jmvivo
                                }
94 20014 jmvivo
                        } catch (NoSuchElementException e){
95
                                //Normal condition exit
96 19663 jmvivo
                        }
97
                }
98 20014 jmvivo
                return numReg;
99 19663 jmvivo
100
        }
101
102
103
        public Iterator iterator() {
104 20014 jmvivo
                checkModified();
105 19663 jmvivo
                try{
106
                        ResultSet r=null;
107
                        r = this.getNewResulset(this.sql);
108 20014 jmvivo
                        H2Iterator dbIter=new H2Iterator(this.store,r,this.featureType,this.featureManager);
109
                        return dbIter;
110 19663 jmvivo
                } 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 20058 jmvivo
                public H2Iterator(H2Store store, ResultSet rs, DBFeatureType featureType,FeatureManager fm){
119 20014 jmvivo
                        this.dbIter = new H2IteratorDB(store,rs,featureType,fm);
120
                        this.mIter = new H2IteratorMemory(featureType,fm);
121 19663 jmvivo
                }
122
                public boolean hasNext() {
123 20014 jmvivo
                        checkModified();
124 19663 jmvivo
                        return dbIter.hasNext() || mIter.hasNext();
125
                }
126
                public Object next() {
127 20014 jmvivo
                        checkModified();
128 19663 jmvivo
                        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 20058 jmvivo
                private H2Store store;
146 19663 jmvivo
                private DBFeatureType featureType;
147
                private boolean rsEOF=false;
148 20014 jmvivo
                private FeatureManager featureManager;
149 19663 jmvivo
150 20058 jmvivo
                public H2IteratorDB(H2Store store, ResultSet rs, DBFeatureType featureType, FeatureManager featureManager){
151 19738 vcaballero
                        this.store = store;
152 19663 jmvivo
                        this.rs = rs;
153
                        this.featureType = featureType;
154 20014 jmvivo
                        this.featureManager = featureManager;
155 19663 jmvivo
                }
156
157
                protected void checkModified(){
158
                        if (modified)
159
                                throw new ConcurrentModificationException("FeatureCollection modified");
160
                }
161
162 19906 jmvivo
163 19663 jmvivo
                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 20014 jmvivo
                                                        if(this.featureManager.isDeleted(feature))
182 19663 jmvivo
                                                                continue;
183
                                                        this.feature=feature;
184
                                                        return true;
185
186
                                                }
187
                                        } catch (java.sql.SQLException e) {
188
                                                throw new RuntimeException(
189 19906 jmvivo
                                                                new ReadException(this.store.getName(),e)
190 19663 jmvivo
                                                );
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 20058 jmvivo
                                        feature=this.store.createFeatureFromResulset(this.rs, featureType);
220 19663 jmvivo
                                }
221
                        } catch (java.sql.SQLException e) {
222
                                throw new RuntimeException(
223 19906 jmvivo
                                                new ReadException(this.store.getName(),e)
224 19663 jmvivo
                                        );
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 20014 jmvivo
                private FeatureManager featureManager;
243 19663 jmvivo
244 20014 jmvivo
                public H2IteratorMemory(DBFeatureType featureType, FeatureManager featureManager){
245 19663 jmvivo
                        position=0;
246 20014 jmvivo
                        this.featureManager=featureManager;
247 19663 jmvivo
                }
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 19975 jmvivo
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 19663 jmvivo
}