svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / jdbc / h2 / H2FeatureCollectionEditing.java @ 20973
History | View | Annotate | Download (5.21 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.ReadException; |
11 |
import org.gvsig.data.datastores.vectorial.db.DBDataFeatureCollection; |
12 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
13 |
import org.gvsig.data.datastores.vectorial.db.jdbc.SQLException; |
14 |
import org.gvsig.data.vectorial.FeatureManager; |
15 |
import org.gvsig.data.vectorial.IFeature; |
16 |
import org.gvsig.data.vectorial.IFeatureType; |
17 |
import org.gvsig.exceptions.BaseException; |
18 |
|
19 |
public class H2FeatureCollectionEditing extends DBDataFeatureCollection { |
20 |
protected DBFeatureType featureType;
|
21 |
protected String totalFilter; |
22 |
protected H2Store store;
|
23 |
private int numReg=-1; |
24 |
private String sql; |
25 |
private String sqlCount; |
26 |
private String totalOrder; |
27 |
private FeatureManager featureManager;
|
28 |
|
29 |
|
30 |
H2FeatureCollectionEditing(FeatureManager fm,H2Store store,IFeatureType type) { |
31 |
this.featureManager=fm;
|
32 |
this.store=store;
|
33 |
this.featureType=(DBFeatureType)type;
|
34 |
|
35 |
this.totalFilter =store.getBaseWhereClause();
|
36 |
this.totalOrder = store.getBaseOrder();
|
37 |
|
38 |
this.sql = this.store.getSqlSelectPart(); |
39 |
this.sqlCount = "Select count(*) From "+ ((H2StoreParameters)this.store.getParameters()).tableID(); |
40 |
if (!isStringEmpty(this.totalFilter)){ |
41 |
this.sql= this.sql + " Where " + this.totalFilter; |
42 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
43 |
} |
44 |
if (!isStringEmpty(this.totalOrder)){ |
45 |
this.sql= this.sql + " Order by " + this.totalOrder; |
46 |
} |
47 |
|
48 |
// if (this.filter!=null)
|
49 |
// parser = new FeatureFilterParser(filter,this.featureType);
|
50 |
|
51 |
} |
52 |
|
53 |
private ResultSet getNewResulset(String aSql) throws ReadException{ |
54 |
this.store.open();
|
55 |
|
56 |
Connection conn = this.store.getConnection(); |
57 |
|
58 |
try {
|
59 |
Statement st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
60 |
return st.executeQuery(aSql);
|
61 |
|
62 |
} catch (java.sql.SQLException e) {
|
63 |
throw new SQLException(aSql,this.store.getName(),e); |
64 |
} |
65 |
|
66 |
} |
67 |
|
68 |
public int size() { |
69 |
checkModified(); |
70 |
try {
|
71 |
if (this.numReg < 0){ |
72 |
ResultSet r=null; |
73 |
r = this.getNewResulset(this.sqlCount); |
74 |
try {
|
75 |
r.next(); |
76 |
numReg = r.getInt(1);
|
77 |
} catch (java.sql.SQLException e) {
|
78 |
throw new ReadException(this.store.getName(),e); |
79 |
} finally{
|
80 |
try {
|
81 |
r.close(); |
82 |
} catch (java.sql.SQLException e) {
|
83 |
throw new ReadException(this.store.getName(),e); |
84 |
} |
85 |
} |
86 |
numReg = numReg+featureManager.getNum(); |
87 |
} |
88 |
return numReg;
|
89 |
} catch (BaseException e){
|
90 |
throw new RuntimeException(e); |
91 |
} |
92 |
|
93 |
} |
94 |
|
95 |
|
96 |
public Iterator iterator() { |
97 |
checkModified(); |
98 |
try{
|
99 |
ResultSet r=null; |
100 |
r = this.getNewResulset(this.sql); |
101 |
H2Iterator dbfIter=new H2Iterator(this.store,r,this.featureType); |
102 |
return dbfIter;
|
103 |
} catch (BaseException e){
|
104 |
throw new RuntimeException(e); |
105 |
} |
106 |
} |
107 |
|
108 |
protected class H2Iterator implements Iterator{ |
109 |
protected long position=0; |
110 |
private boolean nextChecked=false; |
111 |
private IFeature feature;
|
112 |
private ResultSet rs; |
113 |
private H2Store store;
|
114 |
private DBFeatureType featureType;
|
115 |
private boolean rsEOF=false; |
116 |
|
117 |
public H2Iterator(H2Store store, ResultSet rs, DBFeatureType featureType){ |
118 |
this.store = store;
|
119 |
this.rs = rs;
|
120 |
this.featureType = featureType;
|
121 |
position=0;
|
122 |
} |
123 |
|
124 |
protected void checkModified(){ |
125 |
if (modified)
|
126 |
throw new ConcurrentModificationException("FeatureCollection modified"); |
127 |
} |
128 |
|
129 |
public boolean hasNext(){ |
130 |
checkModified(); |
131 |
|
132 |
IFeature feature=null;
|
133 |
if (nextChecked){
|
134 |
return feature != null; |
135 |
} |
136 |
nextChecked=true;
|
137 |
while (true){ |
138 |
if (!rsEOF){
|
139 |
try {
|
140 |
if (rs.isLast()){
|
141 |
rs.close(); |
142 |
// rs.getStatement().close();
|
143 |
rsEOF=true;
|
144 |
continue;
|
145 |
} else {
|
146 |
feature=nextH2Feature(); |
147 |
} |
148 |
} catch (java.sql.SQLException e) {
|
149 |
throw new RuntimeException( |
150 |
new ReadException(this.store.getName(),e) |
151 |
); |
152 |
} |
153 |
|
154 |
}else {
|
155 |
if (position<featureManager.getNum()){
|
156 |
try {
|
157 |
feature=featureManager.getFeature((int)position,store);
|
158 |
} catch (ReadException e) {
|
159 |
throw new RuntimeException( |
160 |
new ReadException(this.store.getName(),e) |
161 |
); |
162 |
} |
163 |
position++; |
164 |
}else{
|
165 |
this.feature = null; |
166 |
return false; |
167 |
} |
168 |
} |
169 |
|
170 |
|
171 |
|
172 |
if(featureManager.isDeleted(feature))
|
173 |
continue;
|
174 |
this.feature=feature;
|
175 |
return true; |
176 |
|
177 |
} |
178 |
} |
179 |
|
180 |
public Object next() { |
181 |
checkModified(); |
182 |
if (!nextChecked){
|
183 |
hasNext(); |
184 |
} |
185 |
if (feature == null) |
186 |
throw new NoSuchElementException(); |
187 |
nextChecked=false;
|
188 |
return feature;
|
189 |
} |
190 |
|
191 |
private IFeature nextH2Feature() {
|
192 |
IFeature feature=null;
|
193 |
try {
|
194 |
if(rs.next()){
|
195 |
feature=this.store.createFeatureFromResulset(this.rs, featureType); |
196 |
} |
197 |
} catch (java.sql.SQLException e) {
|
198 |
throw new RuntimeException( |
199 |
new ReadException("H2FeatureCollection",e) |
200 |
); |
201 |
} catch (ReadException e) {
|
202 |
throw new RuntimeException(e); |
203 |
} |
204 |
return feature;
|
205 |
} |
206 |
|
207 |
public void remove() { |
208 |
throw new UnsupportedOperationException(); |
209 |
} |
210 |
|
211 |
} |
212 |
|
213 |
public void dispose() { |
214 |
this.store.deleteObserver(this); |
215 |
this.store = null; |
216 |
this.featureManager = null; |
217 |
this.featureType = null; |
218 |
} |
219 |
|
220 |
} |