Statistics
| Revision:

root / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / driver / jdbc / h2 / H2FeatureCollectionEditingFiltered.java @ 19744

History | View | Annotate | Download (9.19 KB)

1
package org.gvsig.data.datastores.vectorial.driver.jdbc.h2;
2

    
3

    
4
import java.sql.Connection;
5
import java.sql.ResultSet;
6
import java.sql.Statement;
7
import java.util.ArrayList;
8
import java.util.Collection;
9
import java.util.ConcurrentModificationException;
10
import java.util.Iterator;
11
import java.util.NoSuchElementException;
12

    
13
import org.gvsig.data.ComplexObservable;
14
import org.gvsig.data.datastores.vectorial.driver.jdbc.DBFeatureType;
15
import org.gvsig.data.datastores.vectorial.driver.jdbc.exception.SQLException;
16
import org.gvsig.data.exception.ReadException;
17
import org.gvsig.data.vectorial.AbstractFeatureCollection;
18
import org.gvsig.data.vectorial.IFeature;
19
import org.gvsig.data.vectorial.IFeatureType;
20
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
21
import org.gvsig.data.vectorial.filter.FeatureFilterParser;
22
import org.gvsig.exceptions.BaseException;
23

    
24
public class H2FeatureCollectionEditingFiltered extends AbstractFeatureCollection {
25
        protected ComplexObservable observable = new ComplexObservable();
26
        protected DBFeatureType featureType;
27
        protected String totalFilter;
28
        protected String filter;
29
        protected H2Store store;
30
        protected ResultSet rs;
31
        private int numReg=-1;
32
        private String sql;
33
        private String sqlCount;
34
        private String totalOrder;
35
        private Connection connection;
36
        private FeatureManager featureManager;
37
        private FeatureFilterParser parser;
38

    
39

    
40
        public H2FeatureCollectionEditingFiltered(FeatureManager fm,H2Store store,IFeatureType type,String filter) {
41
                this.featureManager=fm;
42
                this.store=store;
43
                this.featureType=(DBFeatureType)type;
44

    
45
                this.filter = filter;
46

    
47
                this.calculateWhere();
48
                this.totalOrder = store.getBaseOrder();
49

    
50
                this.sql = this.store.getSqlSelectPart();
51
                this.sqlCount = "Select count(" + type.getFieldId() +") From "+ ((H2StoreParameters)this.store.getParameters()).tableID();
52
                if (!isStringEmpty(this.totalFilter)){
53
                        this.sql= this.sql + " Where " + this.totalFilter;
54
                        this.sqlCount= this.sqlCount + " Where " + this.totalFilter;
55
                }
56
                if (!isStringEmpty(this.totalOrder)){
57
                        this.sql= this.sql + " Order by " + this.totalOrder;
58
                }
59

    
60
                if (this.filter!=null)
61
                        parser = new FeatureFilterParser(filter,this.featureType);
62

    
63
        }
64

    
65
        private void calculateWhere(){
66
                if (isStringEmpty(this.store.getBaseWhereClause())){
67
                        this.totalFilter = this.filter;
68
                } else {
69
                        this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter;
70
                }
71
        }
72

    
73
        private ResultSet getNewResulset(String aSql) throws ReadException{
74
                this.store.open();
75

    
76
                this.connection = this.store.getConnection();
77

    
78
                try {
79
                        Statement st = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
80
                        return rs = st.executeQuery(aSql);
81

    
82
                } catch (java.sql.SQLException e) {
83
                        throw new SQLException(aSql,"H2FeatureCollection",e);
84
                }
85

    
86
        }
87

    
88
        private boolean isStringEmpty(String s){
89
                return s == null || s == "";
90
        }
91

    
92

    
93
        protected void checkModified(){
94
                if (modified)
95
                        throw new ConcurrentModificationException("FeatureCollection modified");
96
        }
97

    
98
        public int size() {
99
                try {
100
                        if (this.numReg < 0){
101
                                ResultSet r=null;
102
                                r = this.getNewResulset(this.sqlCount);
103
                                try {
104
                                        r.next();
105
                                        numReg = r.getInt(1);
106
                                } catch (java.sql.SQLException e) {
107
                                        throw new ReadException("H2FeatureCollection.size",e);
108
                                } finally{
109
                                        try {
110
                                                r.close();
111
                                        } catch (java.sql.SQLException e) {
112
                                                throw new ReadException("H2FeatureCollection.size",e);
113
                                        }
114
                                }
115
                                try{
116
                                        Iterator iter = new H2IteratorMemory(this.featureType);
117
                                        while (true){
118
                                                iter.next();
119
                                                numReg++;
120
                                        }
121
                                } catch (NoSuchElementException e){
122
                                        //Normal condition exit
123
                                }
124
                        }
125
                        return numReg;
126
                } catch (BaseException e){
127
                        throw new RuntimeException(e);
128
                }
129

    
130
        }
131

    
132

    
133
        public boolean isEmpty() {
134
                return (size()==0);
135
        }
136

    
137
        public boolean contains(Object o) {
138
                //FIXME: obtimizar
139
                Iterator iterator= this.iterator();
140
                while(iterator.hasNext()){
141
                        IFeature feature=(IFeature)iterator.next();
142
                        if (feature.getID().equals(((IFeature)o).getID())){
143
                                return true;
144
                        }
145
                }
146
                return false;
147
        }
148

    
149
        public Iterator iterator() {
150
                try{
151
                        ResultSet r=null;
152
                        r = this.getNewResulset(this.sql);
153
                        H2Iterator dbfIter=new H2Iterator(this.store,r,this.featureType);
154
                        return dbfIter;
155
                } catch (BaseException e){
156
                        throw new RuntimeException(e);
157
                }
158
        }
159

    
160
        public Object[] toArray() {
161
                ArrayList features= new ArrayList();
162
                Iterator iterator= this.iterator();
163
                while(iterator.hasNext()){
164
                        IFeature feature=(IFeature)iterator.next();
165
                        features.add(feature);
166
                }
167
                return features.toArray();
168
        }
169

    
170
        public Object[] toArray(Object[] a) {
171
                ArrayList features= new ArrayList();
172
                Iterator iterator= this.iterator();
173
                while(iterator.hasNext()){
174
                        IFeature feature=(IFeature)iterator.next();
175
                        features.add(feature);
176
                }
177
                return features.toArray(a);
178
        }
179

    
180
        public boolean add(Object o) {
181
                throw new UnsupportedOperationException();
182
        }
183

    
184
        public boolean remove(Object o) {
185
                throw new UnsupportedOperationException();
186
        }
187

    
188
        public boolean containsAll(Collection c) {
189
                Iterator iter = c.iterator();
190
                while (iter.hasNext()){
191
                        if (!this.contains(iter.next())){
192
                                return false;
193
                        }
194
                }
195
                return true;
196

    
197
        }
198

    
199
        public boolean addAll(Collection c) {
200
                throw new UnsupportedOperationException();
201
        }
202

    
203
        public boolean removeAll(Collection c) {
204
                throw new UnsupportedOperationException();
205
        }
206

    
207
        public boolean retainAll(Collection c) {
208
                throw new UnsupportedOperationException();
209
        }
210

    
211
        public void clear() {
212
                throw new UnsupportedOperationException();
213
        }
214

    
215

    
216
        protected class H2Iterator implements Iterator{
217
                private Iterator dbIter;
218
                private Iterator mIter;
219
                public H2Iterator(H2Store store, ResultSet rs, DBFeatureType featureType){
220
                        this.dbIter = new H2IteratorDB(store,rs,featureType);
221
                        this.mIter = new H2IteratorMemory(featureType);
222
                }
223
                public boolean hasNext() {
224
                        return dbIter.hasNext() || mIter.hasNext();
225
                }
226
                public Object next() {
227
                        if (dbIter.hasNext()){
228
                                return dbIter.next();
229
                        }
230
                        return mIter.next();
231
                }
232
                public void remove() {
233
                        throw new UnsupportedOperationException();
234
                }
235

    
236

    
237

    
238
        }
239

    
240
        protected class H2IteratorDB implements Iterator{
241
                private boolean nextChecked=false;
242
                private IFeature feature;
243
                private ResultSet rs;
244
                private H2Store store;
245
                private DBFeatureType featureType;
246
                private boolean rsEOF=false;
247

    
248
                public H2IteratorDB(H2Store store, ResultSet rs, DBFeatureType featureType){
249
                        this.store = store;
250
                        this.rs = rs;
251
                        this.featureType = featureType;
252
                }
253

    
254
                protected void checkModified(){
255
                        if (modified)
256
                                throw new ConcurrentModificationException("FeatureCollection modified");
257
                }
258

    
259
                public boolean hasNext(){
260
                        checkModified();
261

    
262

    
263
                        if (nextChecked){
264
                                return this.feature != null;
265
                        }
266
                        IFeature feature=null;
267
                        nextChecked=true;
268
                        while (true){
269
                                if (!rsEOF){
270
                                        try {
271
                                                if (rs.isLast()){
272
                                                        rs.close();
273
                                                        rsEOF=true;
274
                                                        continue;
275
                                                } else {
276
                                                        feature=nextH2Feature();
277
                                                        if(featureManager.isDeleted(feature))
278
                                                                continue;
279
                                                        this.feature=feature;
280
                                                        return true;
281

    
282
                                                }
283
                                        } catch (java.sql.SQLException e) {
284
                                                throw new RuntimeException(
285
                                                                new ReadException("H2FeatureCollection",e)
286
                                                );
287
                                        }
288

    
289
                                }else {
290
                                        return false;
291

    
292
                                }
293

    
294

    
295
                        }
296
                }
297

    
298
                public Object next() {
299
                        checkModified();
300
                        if (!nextChecked){
301
                                hasNext();
302
                        }
303
                        if (this.feature == null)
304
                                throw new NoSuchElementException();
305
                        nextChecked=false;
306
                        IFeature feature = this.feature;
307
                        this.feature = null;
308
                        return feature;
309
                }
310

    
311
                private IFeature nextH2Feature() {
312
                        IFeature feature=null;
313
                        try {
314
                                if(rs.next()){
315
                                        feature=H2Utils.createFeature(this.store, this.rs, featureType);
316
                                }
317
                        } catch (java.sql.SQLException e) {
318
                                throw new RuntimeException(
319
                                                new ReadException("H2FeatureCollection",e)
320
                                        );
321
                        } catch (ReadException e) {
322
                                throw new RuntimeException(e);
323
                        }
324
                        return feature;
325
                }
326

    
327
                public void remove() {
328
                        throw new UnsupportedOperationException();
329
                }
330

    
331
        }
332

    
333

    
334
        protected class H2IteratorMemory implements Iterator{
335
                protected long position=0;
336
                private boolean nextChecked=false;
337
                private IFeature feature;
338

    
339
                public H2IteratorMemory(DBFeatureType featureType){
340
                        position=0;
341
                }
342

    
343
                protected void checkModified(){
344
                        if (modified)
345
                                throw new ConcurrentModificationException("FeatureCollection modified");
346
                }
347

    
348
                public boolean hasNext(){
349
                        checkModified();
350

    
351

    
352
                        if (nextChecked){
353
                                return this.feature != null;
354
                        }
355
                        IFeature feature=null;
356
                        nextChecked=true;
357
                        while (true){
358
                                if (position<featureManager.getNum()){
359
                                        feature=featureManager.getFeature((int)position);
360
                                        position++;
361

    
362
                                }else{
363
                                        this.feature = null;
364
                                        return false;
365
                                }
366

    
367
                                if(featureManager.isDeleted(feature))
368
                                        continue;
369

    
370
                                if (filter == null) {
371
                                        this.feature=feature;
372
                                        return true;
373

    
374
                                } else {
375
                                        try {
376
                                                if (parser.match(feature)){
377
                                                        this.feature=feature;
378
                                                        return true;
379
                                                }else{
380
                                                        continue;
381
                                                }
382
                                        } catch (Exception e) {
383
                                                throw new RuntimeException(e);
384
                                        }
385
                                }
386
                        }
387
                }
388

    
389
                public Object next() {
390
                        checkModified();
391
                        if (!nextChecked){
392
                                hasNext();
393
                        }
394
                        if (this.feature == null)
395
                                throw new NoSuchElementException();
396
                        nextChecked=false;
397
                        IFeature feature = this.feature;
398
                        this.feature = null;
399
                        return feature;
400
                }
401

    
402
                public void remove() {
403
                        throw new UnsupportedOperationException();
404
                }
405
        }
406

    
407
}