root / trunk / libraries / libDataSourceDBBaseDrivers / src / org / gvsig / data / datastores / vectorial / driver / jdbc / h2 / H2FeatureCollection.java @ 19610
History | View | Annotate | Download (7.1 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.IFeatureAttributeDescriptor; |
19 |
import org.gvsig.data.vectorial.IFeatureType; |
20 |
import org.gvsig.exceptions.BaseException; |
21 |
|
22 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
23 |
|
24 |
public class H2FeatureCollection extends AbstractFeatureCollection { |
25 |
protected ComplexObservable observable = new ComplexObservable(); |
26 |
protected DBFeatureType featureType;
|
27 |
protected String filter; |
28 |
protected String totalFilter; |
29 |
protected H2Driver driver;
|
30 |
protected ResultSet rs; |
31 |
private WKBParser2 wkbParser = new WKBParser2(); |
32 |
private String order; |
33 |
private int numReg=-1; |
34 |
private String sql; |
35 |
private String sqlCount; |
36 |
private String totalOrder; |
37 |
private Connection connection; |
38 |
private Statement statement; |
39 |
|
40 |
public H2FeatureCollection(H2Driver driver,IFeatureType type, String filter, String order) { |
41 |
this.driver=driver;
|
42 |
this.featureType=(DBFeatureType)type;
|
43 |
this.filter=filter;
|
44 |
this.order=order;
|
45 |
this.calculateWhere();
|
46 |
this.calculateOrder();
|
47 |
|
48 |
this.sql = this.driver.getSqlSelectPart(); |
49 |
this.sqlCount = "Select count(" + type.getFieldId() +") "; |
50 |
if (!isStringEmpty(this.totalFilter)){ |
51 |
this.sql= this.sql + " Where " + this.totalFilter; |
52 |
this.sqlCount= this.sqlCount + " Where " + this.totalFilter; |
53 |
} |
54 |
if (!isStringEmpty(this.totalOrder)){ |
55 |
this.sql= this.sql + " Order by " + this.totalOrder; |
56 |
} |
57 |
} |
58 |
|
59 |
public H2FeatureCollection(H2Driver driver,IFeatureType type, String sql) { |
60 |
this.driver=driver;
|
61 |
this.featureType=(DBFeatureType)type;
|
62 |
this.filter=null; |
63 |
this.order=null; |
64 |
|
65 |
this.sql = sql;
|
66 |
|
67 |
} |
68 |
|
69 |
|
70 |
private ResultSet getNewResulset(String aSql) throws ReadException{ |
71 |
this.driver.open();
|
72 |
|
73 |
this.connection = this.driver.getConnection(); |
74 |
|
75 |
try {
|
76 |
|
77 |
if (this.statement == null){ |
78 |
this.statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); |
79 |
} |
80 |
|
81 |
return rs = this.statement.executeQuery(aSql); |
82 |
} catch (java.sql.SQLException e) {
|
83 |
throw new SQLException(aSql,"H2FeatureCollection",e); |
84 |
} |
85 |
|
86 |
} |
87 |
|
88 |
|
89 |
private boolean isStringEmpty(String s){ |
90 |
return s == null || s == ""; |
91 |
} |
92 |
|
93 |
private void calculateWhere(){ |
94 |
if (isStringEmpty(this.driver.getBaseWhereClause())){ |
95 |
this.totalFilter = this.filter; |
96 |
} else {
|
97 |
this.totalFilter = "(" + this.driver.getBaseWhereClause() + ") and " +this.filter; |
98 |
} |
99 |
} |
100 |
|
101 |
private void calculateOrder(){ |
102 |
if (isStringEmpty(this.driver.getBaseOrder())){ |
103 |
this.totalOrder = this.order; |
104 |
} else {
|
105 |
this.totalOrder = this.driver.getBaseOrder() + ", " +this.order; |
106 |
} |
107 |
|
108 |
} |
109 |
|
110 |
protected void checkModified(){ |
111 |
if (modified)
|
112 |
throw new ConcurrentModificationException("FeatureCollection modified"); |
113 |
} |
114 |
|
115 |
public int size() { |
116 |
try {
|
117 |
if (this.numReg < 0){ |
118 |
ResultSet r=null; |
119 |
r = this.getNewResulset(this.sqlCount); |
120 |
try {
|
121 |
r.next(); |
122 |
numReg = r.getInt(1);
|
123 |
} catch (java.sql.SQLException e) {
|
124 |
throw new ReadException("H2FeatureCollection.size",e); |
125 |
} finally{
|
126 |
try {
|
127 |
r.close(); |
128 |
} catch (java.sql.SQLException e) {
|
129 |
throw new ReadException("H2FeatureCollection.size",e); |
130 |
} |
131 |
|
132 |
} |
133 |
|
134 |
System.err.println("numReg = " + numReg); |
135 |
} |
136 |
return numReg;
|
137 |
} catch (BaseException e){
|
138 |
throw new RuntimeException(e); |
139 |
} |
140 |
|
141 |
} |
142 |
|
143 |
public boolean isEmpty() { |
144 |
return (size()==0); |
145 |
} |
146 |
|
147 |
public boolean contains(Object o) { |
148 |
Iterator iterator= this.iterator(); |
149 |
while(iterator.hasNext()){
|
150 |
IFeature feature=(IFeature)iterator.next(); |
151 |
if (feature.getID().equals(((IFeature)o).getID())){
|
152 |
return true; |
153 |
} |
154 |
} |
155 |
return false; |
156 |
} |
157 |
|
158 |
public Iterator iterator() { |
159 |
ResultSet r;
|
160 |
try {
|
161 |
r = this.getNewResulset(this.sqlCount); |
162 |
} catch (ReadException e) {
|
163 |
throw new RuntimeException(e); |
164 |
} |
165 |
H2Iterator dbfIter=new H2Iterator(this.driver,this.featureType,r); |
166 |
return dbfIter;
|
167 |
} |
168 |
|
169 |
public Object[] toArray() { |
170 |
ArrayList features= new ArrayList(); |
171 |
Iterator iterator= this.iterator(); |
172 |
while(iterator.hasNext()){
|
173 |
IFeature feature=(IFeature)iterator.next(); |
174 |
features.add(feature); |
175 |
} |
176 |
return features.toArray();
|
177 |
} |
178 |
|
179 |
public Object[] toArray(Object[] a) { |
180 |
ArrayList features= new ArrayList(); |
181 |
Iterator iterator= this.iterator(); |
182 |
while(iterator.hasNext()){
|
183 |
IFeature feature=(IFeature)iterator.next(); |
184 |
features.add(feature); |
185 |
} |
186 |
return features.toArray(a);
|
187 |
} |
188 |
|
189 |
public boolean add(Object o) { |
190 |
throw new UnsupportedOperationException(); |
191 |
} |
192 |
|
193 |
public boolean remove(Object o) { |
194 |
throw new UnsupportedOperationException(); |
195 |
} |
196 |
|
197 |
public boolean containsAll(Collection c) { |
198 |
Iterator iter = c.iterator();
|
199 |
while (iter.hasNext()){
|
200 |
if (!this.contains(iter.next())){ |
201 |
return false; |
202 |
} |
203 |
} |
204 |
return true; |
205 |
|
206 |
} |
207 |
|
208 |
public boolean addAll(Collection c) { |
209 |
throw new UnsupportedOperationException(); |
210 |
} |
211 |
|
212 |
public boolean removeAll(Collection c) { |
213 |
throw new UnsupportedOperationException(); |
214 |
} |
215 |
|
216 |
public boolean retainAll(Collection c) { |
217 |
throw new UnsupportedOperationException(); |
218 |
} |
219 |
|
220 |
public void clear() { |
221 |
throw new UnsupportedOperationException(); |
222 |
} |
223 |
|
224 |
|
225 |
protected class H2Iterator implements Iterator{ |
226 |
private ResultSet rs; |
227 |
private H2Driver driver;
|
228 |
private DBFeatureType featureType;
|
229 |
|
230 |
public H2Iterator(H2Driver driver,DBFeatureType featureType ,ResultSet rs){ |
231 |
this.rs = rs;
|
232 |
this.driver = driver;
|
233 |
this.featureType = featureType;
|
234 |
} |
235 |
|
236 |
public boolean hasNext(){ |
237 |
try {
|
238 |
if (rs.isLast()){
|
239 |
return false; |
240 |
} else {
|
241 |
return true; |
242 |
} |
243 |
} catch (java.sql.SQLException e) {
|
244 |
throw new RuntimeException( |
245 |
new ReadException("H2FeatureCollection",e) |
246 |
); |
247 |
} |
248 |
} |
249 |
|
250 |
public Object next() { |
251 |
if (!hasNext())
|
252 |
throw new NoSuchElementException(); |
253 |
return nextFeature();
|
254 |
} |
255 |
|
256 |
private IFeature nextFeature() {
|
257 |
try {
|
258 |
IFeature feature=null;
|
259 |
try {
|
260 |
if(rs.next()){
|
261 |
feature = H2DriverUtils.createFeature(this.driver, this.rs, this.featureType); |
262 |
} else {
|
263 |
throw new NoSuchElementException(); |
264 |
} |
265 |
if (rs.isAfterLast()){
|
266 |
rs.close(); |
267 |
} |
268 |
|
269 |
|
270 |
} catch (java.sql.SQLException e) {
|
271 |
throw new RuntimeException( |
272 |
new ReadException("H2FeatureCollection",e) |
273 |
); |
274 |
} |
275 |
return feature;
|
276 |
} catch (BaseException e){
|
277 |
throw new RuntimeException( |
278 |
new ReadException("H2FeatureCollection",e) |
279 |
); |
280 |
|
281 |
} |
282 |
} |
283 |
|
284 |
public void remove() { |
285 |
throw new UnsupportedOperationException(); |
286 |
} |
287 |
|
288 |
} |
289 |
|
290 |
} |