Statistics
| Revision:

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

History | View | Annotate | Download (7.28 KB)

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

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

    
12
import org.gvsig.data.ComplexObservable;
13
import org.gvsig.data.datastores.vectorial.driver.jdbc.DBFeatureType;
14
import org.gvsig.data.datastores.vectorial.driver.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.exceptions.BaseException;
21

    
22
public class H2FeatureCollectionEditing extends AbstractFeatureCollection {
23
        protected ComplexObservable observable = new ComplexObservable();
24
        protected DBFeatureType featureType;
25
        protected String totalFilter;
26
        protected H2Driver driver;
27
        protected ResultSet rs;
28
        private int numReg=-1;
29
        private String sql;
30
        private String sqlCount;
31
        private String totalOrder;
32
        private Connection connection;
33
        private FeatureManager featureManager;
34
//        private FeatureFilterParser parser;
35

    
36

    
37
        public H2FeatureCollectionEditing(FeatureManager fm,H2Driver driver,IFeatureType type) {
38
                this.featureManager=fm;
39
                this.driver=driver;
40
                this.featureType=(DBFeatureType)type;
41

    
42
                this.totalFilter =driver.getBaseWhereClause();
43
                this.totalOrder = driver.getBaseOrder();
44

    
45
                this.sql = this.driver.getSqlSelectPart();
46
                this.sqlCount = "Select count(" + type.getFieldId() +") From "+ this.driver.getParametersH2().tableID();
47
                if (!isStringEmpty(this.totalFilter)){
48
                        this.sql= this.sql + " Where " + this.totalFilter;
49
                        this.sqlCount= this.sqlCount + " Where " + this.totalFilter;
50
                }
51
                if (!isStringEmpty(this.totalOrder)){
52
                        this.sql= this.sql + " Order by " + this.totalOrder;
53
                }
54

    
55
//                if (this.filter!=null)
56
//                        parser = new FeatureFilterParser(filter,this.featureType);
57

    
58
        }
59

    
60
        private ResultSet getNewResulset(String aSql) throws ReadException{
61
                this.driver.open();
62

    
63
                this.connection = this.driver.getConnection();
64

    
65
                try {
66
                        Statement st = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
67
                        return rs = st.executeQuery(aSql);
68

    
69
                } catch (java.sql.SQLException e) {
70
                        throw new SQLException(aSql,"H2FeatureCollection",e);
71
                }
72

    
73
        }
74

    
75
        private boolean isStringEmpty(String s){
76
                return s == null || s == "";
77
        }
78

    
79

    
80
        protected void checkModified(){
81
                if (modified)
82
                        throw new ConcurrentModificationException("FeatureCollection modified");
83
        }
84

    
85
        public int size() {
86
                try {
87
                        if (this.numReg < 0){
88
                                ResultSet r=null;
89
                                r = this.getNewResulset(this.sqlCount);
90
                                try {
91
                                        r.next();
92
                                        numReg = r.getInt(1);
93
                                } catch (java.sql.SQLException e) {
94
                                        throw new ReadException("H2FeatureCollection.size",e);
95
                                } finally{
96
                                        try {
97
                                                r.close();
98
                                        } catch (java.sql.SQLException e) {
99
                                                throw new ReadException("H2FeatureCollection.size",e);
100
                                        }
101
                                }
102
                                numReg = numReg+featureManager.getNum();
103
                        }
104
                        return numReg;
105
                } catch (BaseException e){
106
                        throw new RuntimeException(e);
107
                }
108

    
109
        }
110

    
111

    
112
        public boolean isEmpty() {
113
                return (size()==0);
114
        }
115

    
116
        public boolean contains(Object o) {
117
                //FIXME: obtimizar
118
                Iterator iterator= this.iterator();
119
                while(iterator.hasNext()){
120
                        IFeature feature=(IFeature)iterator.next();
121
                        if (feature.getID().equals(((IFeature)o).getID())){
122
                                return true;
123
                        }
124
                }
125
                return false;
126
        }
127

    
128
        public Iterator iterator() {
129
                try{
130
                        ResultSet r=null;
131
                        r = this.getNewResulset(this.sql);
132
                        H2Iterator dbfIter=new H2Iterator(this.driver,r,this.featureType);
133
                        return dbfIter;
134
                } catch (BaseException e){
135
                        throw new RuntimeException(e);
136
                }
137
        }
138

    
139
        public Object[] toArray() {
140
                ArrayList features= new ArrayList();
141
                Iterator iterator= this.iterator();
142
                while(iterator.hasNext()){
143
                        IFeature feature=(IFeature)iterator.next();
144
                        features.add(feature);
145
                }
146
                return features.toArray();
147
        }
148

    
149
        public Object[] toArray(Object[] a) {
150
                ArrayList features= new ArrayList();
151
                Iterator iterator= this.iterator();
152
                while(iterator.hasNext()){
153
                        IFeature feature=(IFeature)iterator.next();
154
                        features.add(feature);
155
                }
156
                return features.toArray(a);
157
        }
158

    
159
        public boolean add(Object o) {
160
                throw new UnsupportedOperationException();
161
        }
162

    
163
        public boolean remove(Object o) {
164
                throw new UnsupportedOperationException();
165
        }
166

    
167
        public boolean containsAll(Collection c) {
168
                Iterator iter = c.iterator();
169
                while (iter.hasNext()){
170
                        if (!this.contains(iter.next())){
171
                                return false;
172
                        }
173
                }
174
                return true;
175

    
176
        }
177

    
178
        public boolean addAll(Collection c) {
179
                throw new UnsupportedOperationException();
180
        }
181

    
182
        public boolean removeAll(Collection c) {
183
                throw new UnsupportedOperationException();
184
        }
185

    
186
        public boolean retainAll(Collection c) {
187
                throw new UnsupportedOperationException();
188
        }
189

    
190
        public void clear() {
191
                throw new UnsupportedOperationException();
192
        }
193

    
194

    
195
        protected class H2Iterator implements Iterator{
196
                protected long position=0;
197
                private boolean nextChecked=false;
198
                private IFeature feature;
199
                private ResultSet rs;
200
                private H2Driver driver;
201
                private DBFeatureType featureType;
202
                private boolean rsEOF=false;
203

    
204
                public H2Iterator(H2Driver driver, ResultSet rs, DBFeatureType featureType){
205
                        this.driver = driver;
206
                        this.rs = rs;
207
                        this.featureType = featureType;
208
                        position=0;
209
                }
210

    
211
                protected void checkModified(){
212
                        if (modified)
213
                                throw new ConcurrentModificationException("FeatureCollection modified");
214
                }
215

    
216
                public boolean hasNext(){
217
                        checkModified();
218

    
219
                        IFeature feature=null;
220
                        if (nextChecked){
221
                                return feature != null;
222
                        }
223
                        nextChecked=true;
224
                        while (true){
225
                                if (!rsEOF){
226
                                        try {
227
                                                if (rs.isLast()){
228
                                                        rs.close();
229
//                                                        rs.getStatement().close();
230
                                                        rsEOF=true;
231
                                                        continue;
232
                                                } else {
233
                                                        feature=nextH2Feature();
234
                                                }
235
                                        } catch (java.sql.SQLException e) {
236
                                                throw new RuntimeException(
237
                                                                new ReadException("H2FeatureCollection",e)
238
                                                );
239
                                        }
240

    
241
                                }else {
242
                                        if (position<featureManager.getNum()){
243
                                                feature=featureManager.getFeature((int)position);
244
                                                position++;
245
                                        }else{
246
                                                this.feature = null;
247
                                                return false;
248
                                        }
249
                                }
250

    
251

    
252

    
253
                                if(featureManager.isDeleted(feature))
254
                                        continue;
255
                                this.feature=feature;
256
                                return true;
257

    
258
//                                if (filter == null) {
259
//                                        this.feature=feature;
260
//                                        return true;
261
//
262
//                                } else {
263
//                                        try {
264
//                                                if (parser.match(feature)){
265
//                                                        this.feature=feature;
266
//                                                        return true;
267
//                                                }else{
268
//                                                        continue;
269
//                                                }
270
//                                        } catch (Exception e) {
271
//                                                // TODO Auto-generated catch block
272
//                                                e.printStackTrace();
273
//                                        }
274
//                                }
275
                        }
276
                }
277

    
278
                public Object next() {
279
//                        checkModified();
280
                        if (!nextChecked){
281
                                hasNext();
282
                        }
283
                        if (feature == null)
284
                                throw new NoSuchElementException();
285
                        nextChecked=false;
286
                        return feature;
287
                }
288

    
289
                private IFeature nextH2Feature() {
290
                        IFeature feature=null;
291
                        try {
292
                                if(rs.next()){
293
                                        feature=H2DriverUtils.createFeature(this.driver, this.rs, featureType);
294
                                }
295
                        } catch (java.sql.SQLException e) {
296
                                throw new RuntimeException(
297
                                                new ReadException("H2FeatureCollection",e)
298
                                        );
299
                        } catch (ReadException e) {
300
                                throw new RuntimeException(e);
301
                        }
302
                        return feature;
303
                }
304

    
305
                public void remove() {
306
                        throw new UnsupportedOperationException();
307
                }
308

    
309
        }
310

    
311
}