Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2FeatureCollectionEditing.java @ 20058

History | View | Annotate | Download (5.25 KB)

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

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

    
10
import org.gvsig.data.datastores.vectorial.db.DBFeatureType;
11
import org.gvsig.data.datastores.vectorial.db.jdbc.AbstractJDBCDataFeatureCollection;
12
import org.gvsig.data.datastores.vectorial.db.jdbc.JDBCStore;
13
import org.gvsig.data.datastores.vectorial.db.jdbc.exception.SQLException;
14
import org.gvsig.data.exception.ReadException;
15
import org.gvsig.data.vectorial.AbstractFeatureCollection;
16
import org.gvsig.data.vectorial.IFeature;
17
import org.gvsig.data.vectorial.IFeatureType;
18
import org.gvsig.data.vectorial.expansionadapter.FeatureManager;
19
import org.gvsig.exceptions.BaseException;
20

    
21
public class H2FeatureCollectionEditing extends AbstractJDBCDataFeatureCollection {
22
        protected DBFeatureType featureType;
23
        protected String totalFilter;
24
        protected H2Store store;
25
        private int numReg=-1;
26
        private String sql;
27
        private String sqlCount;
28
        private String totalOrder;
29
        private FeatureManager featureManager;
30

    
31

    
32
        H2FeatureCollectionEditing(FeatureManager fm,H2Store store,IFeatureType type) {
33
                this.featureManager=fm;
34
                this.store=store;
35
                this.featureType=(DBFeatureType)type;
36

    
37
                this.totalFilter =store.getBaseWhereClause();
38
                this.totalOrder = store.getBaseOrder();
39

    
40
                this.sql = this.store.getSqlSelectPart();
41
                this.sqlCount = "Select count(*) From "+ ((H2StoreParameters)this.store.getParameters()).tableID();
42
                if (!isStringEmpty(this.totalFilter)){
43
                        this.sql= this.sql + " Where " + this.totalFilter;
44
                        this.sqlCount= this.sqlCount + " Where " + this.totalFilter;
45
                }
46
                if (!isStringEmpty(this.totalOrder)){
47
                        this.sql= this.sql + " Order by " + this.totalOrder;
48
                }
49

    
50
//                if (this.filter!=null)
51
//                        parser = new FeatureFilterParser(filter,this.featureType);
52

    
53
        }
54

    
55
        private ResultSet getNewResulset(String aSql) throws ReadException{
56
                this.store.open();
57

    
58
                Connection conn = this.store.getCurrentConnection();
59

    
60
                try {
61
                        Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
62
                        return st.executeQuery(aSql);
63

    
64
                } catch (java.sql.SQLException e) {
65
                        throw new SQLException(aSql,this.store.getName(),e);
66
                }
67

    
68
        }
69

    
70
        public int size() {
71
                checkModified();
72
                try {
73
                        if (this.numReg < 0){
74
                                ResultSet r=null;
75
                                r = this.getNewResulset(this.sqlCount);
76
                                try {
77
                                        r.next();
78
                                        numReg = r.getInt(1);
79
                                } catch (java.sql.SQLException e) {
80
                                        throw new ReadException(this.store.getName(),e);
81
                                } finally{
82
                                        try {
83
                                                r.close();
84
                                        } catch (java.sql.SQLException e) {
85
                                                throw new ReadException(this.store.getName(),e);
86
                                        }
87
                                }
88
                                numReg = numReg+featureManager.getNum();
89
                        }
90
                        return numReg;
91
                } catch (BaseException e){
92
                        throw new RuntimeException(e);
93
                }
94

    
95
        }
96

    
97

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

    
110
        protected class H2Iterator implements Iterator{
111
                protected long position=0;
112
                private boolean nextChecked=false;
113
                private IFeature feature;
114
                private ResultSet rs;
115
                private H2Store store;
116
                private DBFeatureType featureType;
117
                private boolean rsEOF=false;
118

    
119
                public H2Iterator(H2Store store, ResultSet rs, DBFeatureType featureType){
120
                        this.store = store;
121
                        this.rs = rs;
122
                        this.featureType = featureType;
123
                        position=0;
124
                }
125

    
126
                protected void checkModified(){
127
                        if (modified)
128
                                throw new ConcurrentModificationException("FeatureCollection modified");
129
                }
130

    
131
                public boolean hasNext(){
132
                        checkModified();
133

    
134
                        IFeature feature=null;
135
                        if (nextChecked){
136
                                return feature != null;
137
                        }
138
                        nextChecked=true;
139
                        while (true){
140
                                if (!rsEOF){
141
                                        try {
142
                                                if (rs.isLast()){
143
                                                        rs.close();
144
//                                                        rs.getStatement().close();
145
                                                        rsEOF=true;
146
                                                        continue;
147
                                                } else {
148
                                                        feature=nextH2Feature();
149
                                                }
150
                                        } catch (java.sql.SQLException e) {
151
                                                throw new RuntimeException(
152
                                                                new ReadException(this.store.getName(),e)
153
                                                );
154
                                        }
155

    
156
                                }else {
157
                                        if (position<featureManager.getNum()){
158
                                                feature=featureManager.getFeature((int)position);
159
                                                position++;
160
                                        }else{
161
                                                this.feature = null;
162
                                                return false;
163
                                        }
164
                                }
165

    
166

    
167

    
168
                                if(featureManager.isDeleted(feature))
169
                                        continue;
170
                                this.feature=feature;
171
                                return true;
172

    
173
                        }
174
                }
175

    
176
                public Object next() {
177
                        checkModified();
178
                        if (!nextChecked){
179
                                hasNext();
180
                        }
181
                        if (feature == null)
182
                                throw new NoSuchElementException();
183
                        nextChecked=false;
184
                        return feature;
185
                }
186

    
187
                private IFeature nextH2Feature() {
188
                        IFeature feature=null;
189
                        try {
190
                                if(rs.next()){
191
                                        feature=this.store.createFeatureFromResulset(this.rs, featureType);
192
                                }
193
                        } catch (java.sql.SQLException e) {
194
                                throw new RuntimeException(
195
                                                new ReadException("H2FeatureCollection",e)
196
                                        );
197
                        } catch (ReadException e) {
198
                                throw new RuntimeException(e);
199
                        }
200
                        return feature;
201
                }
202

    
203
                public void remove() {
204
                        throw new UnsupportedOperationException();
205
                }
206

    
207
        }
208

    
209
        public void dispose() {
210
                this.store.deleteObserver(this);
211
                this.store = null;
212
                this.featureManager = null;
213
                this.featureType = null;
214
        }
215

    
216
}