Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2FeatureCollectionEditingFiltered.java @ 20973

History | View | Annotate | Download (7.38 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.ReadException;
12
import org.gvsig.data.datastores.vectorial.db.DBDataFeatureCollection;
13
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
14
import org.gvsig.data.datastores.vectorial.db.jdbc.SQLException;
15
import org.gvsig.data.vectorial.FeatureManager;
16
import org.gvsig.data.vectorial.IFeature;
17
import org.gvsig.data.vectorial.IFeatureType;
18
import org.gvsig.data.vectorial.expressionevaluator.FeatureFilter;
19
import org.gvsig.exceptions.BaseException;
20

    
21
public class H2FeatureCollectionEditingFiltered extends DBDataFeatureCollection {
22
        protected DBFeatureType featureType;
23
        protected String totalFilter;
24
        protected String filter;
25
        protected H2Store store;
26
        private int numReg=-1;
27
        private String sql;
28
        private String sqlCount;
29
        private String totalOrder;
30
        private FeatureManager featureManager;
31
        private FeatureFilter parser;
32

    
33

    
34
        H2FeatureCollectionEditingFiltered(FeatureManager fm,H2Store store,IFeatureType type,String filter) {
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
                        parser = new FeatureFilter(filter,this.featureType);
56

    
57
        }
58

    
59
        private void calculateWhere(){
60
                if (isStringEmpty(this.store.getBaseWhereClause())){
61
                        this.totalFilter = this.filter;
62
                } else {
63
                        this.totalFilter = "(" + this.store.getBaseWhereClause() + ") and " +this.filter;
64
                }
65
        }
66

    
67
        private ResultSet getNewResulset(String aSql) throws ReadException{
68
                this.store.open();
69

    
70
                Connection con = this.store.getConnection();
71

    
72
                try {
73
                        Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
74
                        return st.executeQuery(aSql);
75

    
76
                } catch (java.sql.SQLException e) {
77
                        throw new SQLException(aSql,this.store.getName(),e);
78
                }
79

    
80
        }
81

    
82
        public int size() {
83
                checkModified();
84
                if (this.numReg < 0){
85
                        this.numReg=0;
86
                        try{
87
                                Iterator iter =this.iterator();
88
                                while (true){
89
                                        iter.next();
90
                                        numReg++;
91
                                }
92
                        } catch (NoSuchElementException e){
93
                                //Normal condition exit
94
                        }
95
                }
96
                return numReg;
97

    
98
        }
99

    
100

    
101
        public Iterator iterator() {
102
                checkModified();
103
                try{
104
                        ResultSet r=null;
105
                        r = this.getNewResulset(this.sql);
106
                        H2Iterator dbIter=new H2Iterator(this.store,r,this.featureType,this.featureManager);
107
                        return dbIter;
108
                } catch (BaseException e){
109
                        throw new RuntimeException(e);
110
                }
111
        }
112

    
113
        protected class H2Iterator implements Iterator{
114
                private Iterator dbIter;
115
                private Iterator mIter;
116
                public H2Iterator(H2Store store, ResultSet rs, DBFeatureType featureType,FeatureManager fm){
117
                        this.dbIter = new H2IteratorDB(store,rs,featureType,fm);
118
                        this.mIter = new H2IteratorMemory(featureType,fm);
119
                }
120
                public boolean hasNext() {
121
                        checkModified();
122
                        return dbIter.hasNext() || mIter.hasNext();
123
                }
124
                public Object next() {
125
                        checkModified();
126
                        if (dbIter.hasNext()){
127
                                return dbIter.next();
128
                        }
129
                        return mIter.next();
130
                }
131
                public void remove() {
132
                        throw new UnsupportedOperationException();
133
                }
134

    
135

    
136

    
137
        }
138

    
139
        protected class H2IteratorDB implements Iterator{
140
                private boolean nextChecked=false;
141
                private IFeature feature;
142
                private ResultSet rs;
143
                private H2Store store;
144
                private DBFeatureType featureType;
145
                private boolean rsEOF=false;
146
                private FeatureManager featureManager;
147

    
148
                public H2IteratorDB(H2Store store, ResultSet rs, DBFeatureType featureType, FeatureManager featureManager){
149
                        this.store = store;
150
                        this.rs = rs;
151
                        this.featureType = featureType;
152
                        this.featureManager = featureManager;
153
                }
154

    
155
                protected void checkModified(){
156
                        if (modified)
157
                                throw new ConcurrentModificationException("FeatureCollection modified");
158
                }
159

    
160

    
161
                public boolean hasNext(){
162
                        checkModified();
163

    
164

    
165
                        if (nextChecked){
166
                                return this.feature != null;
167
                        }
168
                        IFeature feature=null;
169
                        nextChecked=true;
170
                        while (true){
171
                                if (!rsEOF){
172
                                        try {
173
                                                if (rs.isLast()){
174
                                                        rs.close();
175
                                                        rsEOF=true;
176
                                                        continue;
177
                                                } else {
178
                                                        feature=nextH2Feature();
179
                                                        if(this.featureManager.isDeleted(feature))
180
                                                                continue;
181
                                                        this.feature=feature;
182
                                                        return true;
183

    
184
                                                }
185
                                        } catch (java.sql.SQLException e) {
186
                                                throw new RuntimeException(
187
                                                                new ReadException(this.store.getName(),e)
188
                                                );
189
                                        }
190

    
191
                                }else {
192
                                        return false;
193

    
194
                                }
195

    
196

    
197
                        }
198
                }
199

    
200
                public Object next() {
201
                        checkModified();
202
                        if (!nextChecked){
203
                                hasNext();
204
                        }
205
                        if (this.feature == null)
206
                                throw new NoSuchElementException();
207
                        nextChecked=false;
208
                        IFeature feature = this.feature;
209
                        this.feature = null;
210
                        return feature;
211
                }
212

    
213
                private IFeature nextH2Feature() {
214
                        IFeature feature=null;
215
                        try {
216
                                if(rs.next()){
217
                                        feature=this.store.createFeatureFromResulset(this.rs, featureType);
218
                                }
219
                        } catch (java.sql.SQLException e) {
220
                                throw new RuntimeException(
221
                                                new ReadException(this.store.getName(),e)
222
                                        );
223
                        } catch (ReadException e) {
224
                                throw new RuntimeException(e);
225
                        }
226
                        return feature;
227
                }
228

    
229
                public void remove() {
230
                        throw new UnsupportedOperationException();
231
                }
232

    
233
        }
234

    
235

    
236
        protected class H2IteratorMemory implements Iterator{
237
                protected long position=0;
238
                private boolean nextChecked=false;
239
                private IFeature feature;
240
                private FeatureManager featureManager;
241

    
242
                public H2IteratorMemory(DBFeatureType featureType, FeatureManager featureManager){
243
                        position=0;
244
                        this.featureManager=featureManager;
245
                }
246

    
247
                protected void checkModified(){
248
                        if (modified)
249
                                throw new ConcurrentModificationException("FeatureCollection modified");
250
                }
251

    
252
                public boolean hasNext(){
253
                        checkModified();
254

    
255

    
256
                        if (nextChecked){
257
                                return this.feature != null;
258
                        }
259
                        IFeature feature=null;
260
                        nextChecked=true;
261
                        while (true){
262
                                if (position<featureManager.getNum()){
263
                                        try {
264
                                                feature=featureManager.getFeature((int)position,store);
265
                                        } catch (ReadException e) {
266
                                                throw new RuntimeException(
267
                                                                new ReadException(store.getName(),e)
268
                                                );
269
                                        }
270
                                        position++;
271

    
272
                                }else{
273
                                        this.feature = null;
274
                                        return false;
275
                                }
276

    
277
                                if(featureManager.isDeleted(feature))
278
                                        continue;
279

    
280
                                if (filter == null) {
281
                                        this.feature=feature;
282
                                        return true;
283

    
284
                                } else {
285
                                        try {
286
                                                if (parser.match(feature)){
287
                                                        this.feature=feature;
288
                                                        return true;
289
                                                }else{
290
                                                        continue;
291
                                                }
292
                                        } catch (Exception e) {
293
                                                throw new RuntimeException(e);
294
                                        }
295
                                }
296
                        }
297
                }
298

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

    
312
                public void remove() {
313
                        throw new UnsupportedOperationException();
314
                }
315
        }
316

    
317

    
318
        public void dispose() {
319
                this.store.deleteObserver(this);
320
                this.store = null;
321
                this.featureManager = null;
322
                this.featureType = null;
323
                this.parser= null;
324

    
325
        }
326

    
327
}