root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableDBAdapter.java @ 4182
History | View | Annotate | Download (10.7 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.iver.cit.gvsig.fmap.edition; |
5 |
|
6 |
import java.awt.geom.Rectangle2D; |
7 |
import java.io.IOException; |
8 |
import java.util.ArrayList; |
9 |
import java.util.Hashtable; |
10 |
import java.util.List; |
11 |
|
12 |
import com.hardcode.gdbms.engine.values.Value; |
13 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
14 |
import com.iver.cit.gvsig.fmap.DriverException; |
15 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
16 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
17 |
import com.iver.cit.gvsig.fmap.core.IRow; |
18 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
19 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
20 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
21 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
22 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
23 |
import com.iver.cit.gvsig.fmap.layers.ISpatialDB; |
24 |
import com.iver.cit.gvsig.fmap.layers.VectorialDBAdapter; |
25 |
import com.vividsolutions.jts.geom.Envelope; |
26 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
27 |
|
28 |
/**
|
29 |
* @author fjp
|
30 |
*
|
31 |
*/
|
32 |
public class VectorialEditableDBAdapter extends VectorialEditableAdapter |
33 |
implements ISpatialDB {
|
34 |
|
35 |
private class MyIterator implements IFeatureIterator { |
36 |
private Rectangle2D extent = null; |
37 |
|
38 |
private VectorialDBAdapter orig;
|
39 |
|
40 |
private IFeature feat;
|
41 |
|
42 |
private IFeatureIterator featIt;
|
43 |
|
44 |
private String epsg; |
45 |
|
46 |
private VectorialDatabaseDriver dbDriver;
|
47 |
|
48 |
Hashtable alreadyDone = new Hashtable(); |
49 |
|
50 |
private int idFromExpansion = 0; |
51 |
|
52 |
private List listFromExpansion; |
53 |
|
54 |
private boolean bOriginalCursorOpened = true; |
55 |
|
56 |
public MyIterator(Rectangle2D r, String strEPSG) throws DriverException { |
57 |
extent = r; |
58 |
epsg = strEPSG; |
59 |
orig = (VectorialDBAdapter) ova; |
60 |
featIt = orig.getFeatureIterator(extent, epsg); |
61 |
dbDriver = (VectorialDatabaseDriver) getDriver(); |
62 |
getFeaturesFromExpansionFile(); |
63 |
} |
64 |
|
65 |
public boolean hasNext() throws DriverException { |
66 |
feat = null;
|
67 |
int calculatedIndex = -1; |
68 |
if (bOriginalCursorOpened) // Si hay originales (Es porque si se ha |
69 |
// llegado al final, se cierra el
|
70 |
// iterador y salta un fallo
|
71 |
{ |
72 |
bOriginalCursorOpened = featIt.hasNext(); |
73 |
if (bOriginalCursorOpened) {
|
74 |
feat = featIt.next(); |
75 |
int originalIndex = dbDriver.getRowIndexByFID(feat);
|
76 |
calculatedIndex = getCalculatedIndex(originalIndex); |
77 |
// Iteramos hasta que encontremos alguno no borrado.
|
78 |
if (delRows.get(calculatedIndex)) // Si est? borrado |
79 |
{ |
80 |
feat = null;
|
81 |
boolean bFound = false; |
82 |
while (featIt.hasNext())
|
83 |
{ |
84 |
feat = featIt.next(); |
85 |
originalIndex = dbDriver.getRowIndexByFID(feat); |
86 |
calculatedIndex = getCalculatedIndex(originalIndex); |
87 |
if (delRows.get(calculatedIndex) == false) // Si NO est? borrado |
88 |
{ |
89 |
bFound = true;
|
90 |
break;
|
91 |
} |
92 |
} |
93 |
if (bFound == false) // Todos los ?ltimos est?n borrados. |
94 |
{ |
95 |
bOriginalCursorOpened = false; // para que busque en el fichero de expansi?n |
96 |
} |
97 |
} // if delRows
|
98 |
if (bOriginalCursorOpened) // Si todav?a quedan features por leer, y no est?n borradas |
99 |
{ |
100 |
Integer integer = new Integer(calculatedIndex); |
101 |
if (!relations.containsKey(integer)) { // Si no est? en el |
102 |
// fichero de
|
103 |
// expansi?n
|
104 |
alreadyDone.put(integer, feat); |
105 |
} else { // Si est? en el fichero de expansi?n |
106 |
int num = ((Integer) relations.get(integer)).intValue(); |
107 |
IRowEdited auxR; |
108 |
try {
|
109 |
auxR = expansionFile.getRow(num); |
110 |
feat = (IFeature) auxR.getLinkedRow().cloneRow(); |
111 |
// feat = (IFeature) auxR.getLinkedRow();
|
112 |
alreadyDone.put(integer, feat); |
113 |
} catch (IOException e) { |
114 |
e.printStackTrace(); |
115 |
throw new DriverException(e); |
116 |
} |
117 |
} // else
|
118 |
} // if tercer bOriginalCursorOpened
|
119 |
} // if segundo bOriginalCursorOpened
|
120 |
} // if primer bOriginalCursorOpened
|
121 |
if (!bOriginalCursorOpened) {
|
122 |
// Si ya no hay m?s de las originales, todav?a tenemos
|
123 |
// que revisar las nuevas y modificadas que hay en el fichero
|
124 |
// de expansi?n
|
125 |
if (idFromExpansion < listFromExpansion.size()) {
|
126 |
Integer i = (Integer) listFromExpansion |
127 |
.get(idFromExpansion); |
128 |
// int inverse = getInversedIndex(i.intValue());
|
129 |
// Integer integer = new Integer(inverse);
|
130 |
calculatedIndex = i.intValue(); |
131 |
// TODO: REVISAR AQUI LOS BORRADOS
|
132 |
if (delRows.get(calculatedIndex)) // Si est? borrado |
133 |
{ |
134 |
feat = null;
|
135 |
boolean bFound = false; |
136 |
while (idFromExpansion < listFromExpansion.size())
|
137 |
{ |
138 |
i = (Integer) listFromExpansion.get(idFromExpansion);
|
139 |
calculatedIndex = i.intValue(); |
140 |
if (delRows.get(calculatedIndex) == false) // Si NO est? borrado |
141 |
{ |
142 |
bFound = true;
|
143 |
break;
|
144 |
} |
145 |
} |
146 |
if (bFound == false) // Todos los ?ltimos est?n borrados. |
147 |
{ |
148 |
return false; // Todos borrados. |
149 |
// TODO: REVISAR. CREO QUE EN EL INDICE ESPACIAL
|
150 |
// NO VIENEN LOS BORRADOS PORQUE LOS HEMOS QUITADO DE AH?.
|
151 |
} |
152 |
} // if delRows
|
153 |
|
154 |
Integer integer = new Integer(calculatedIndex); |
155 |
// if (!alreadyDone.containsKey(integer))
|
156 |
{ |
157 |
if (relations.containsKey(integer)) {
|
158 |
int num = ((Integer) relations.get(integer)) |
159 |
.intValue(); |
160 |
IRowEdited auxR; |
161 |
try {
|
162 |
auxR = expansionFile.getRow(num); |
163 |
feat = (IFeature) auxR.cloneRow(); |
164 |
// feat = (IFeature) auxR.getLinkedRow();
|
165 |
} catch (IOException e1) { |
166 |
e1.printStackTrace(); |
167 |
throw new DriverException(e1); |
168 |
} |
169 |
} |
170 |
} |
171 |
idFromExpansion++; |
172 |
} |
173 |
} |
174 |
|
175 |
if (calculatedIndex == -1) |
176 |
return false; |
177 |
else {
|
178 |
if (delRows.get(calculatedIndex))
|
179 |
feat = null;
|
180 |
return true; |
181 |
} |
182 |
|
183 |
} |
184 |
|
185 |
public IFeature next() throws DriverException { |
186 |
return feat;
|
187 |
} |
188 |
|
189 |
public void closeIterator() throws DriverException { |
190 |
// TODO Auto-generated method stub
|
191 |
|
192 |
} |
193 |
|
194 |
private void getFeaturesFromExpansionFile() { |
195 |
Envelope e = FConverter.convertRectangle2DtoEnvelope(extent); |
196 |
listFromExpansion = index.query(e); |
197 |
} |
198 |
} |
199 |
|
200 |
|
201 |
private Hashtable hashFIDtoExpansionFile = new Hashtable(); |
202 |
/**
|
203 |
*
|
204 |
*/
|
205 |
public VectorialEditableDBAdapter() {
|
206 |
super();
|
207 |
// TODO Auto-generated constructor stub
|
208 |
} |
209 |
|
210 |
/*
|
211 |
* (non-Javadoc)
|
212 |
*
|
213 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#getFeatures(java.awt.geom.Rectangle2D,
|
214 |
* java.lang.String)
|
215 |
*/
|
216 |
public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG) |
217 |
throws DriverException {
|
218 |
ArrayList aux = new ArrayList(); |
219 |
IFeatureIterator featIt = getFeatureIterator(r, strEPSG, null);
|
220 |
while (featIt.hasNext()) {
|
221 |
IFeature feat = featIt.next(); |
222 |
// TODO:
|
223 |
int index = getRowIndexByFID(feat);
|
224 |
IRowEdited edRow = new DefaultRowEdited(feat, IRowEdited.STATUS_ORIGINAL, index);
|
225 |
aux.add(edRow); |
226 |
} |
227 |
|
228 |
return (IRowEdited[]) aux.toArray(new IRowEdited[0]); |
229 |
// return (IFeature[]) aux.toArray(new IFeature[0]);
|
230 |
|
231 |
} |
232 |
|
233 |
/*
|
234 |
* (non-Javadoc)
|
235 |
*
|
236 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#startEdition()
|
237 |
*/
|
238 |
public void startEdition() throws EditionException { |
239 |
isEditing = true;
|
240 |
try {
|
241 |
expansionFile.open(); |
242 |
|
243 |
// TODO: Si la capa dispone de un ?ndice espacial, hacer
|
244 |
// algo aqu? para que se use ese ?ndice espacial.
|
245 |
index = new Quadtree();
|
246 |
// No metemos ninguna entidad de las originales dentro
|
247 |
// de la base de datos porque esa consulta ya la
|
248 |
// hace getFeatures sin tener en cuenta el ?ndice local.
|
249 |
|
250 |
/*
|
251 |
* for (int i = 0; i < ova.getShapeCount(); i++) { IGeometry g=null;
|
252 |
* try { g = ((DefaultFeature) ova.getFeature(i)).getGeometry(); }
|
253 |
* catch (DriverException e1) { // TODO Auto-generated catch block
|
254 |
* e1.printStackTrace(); }
|
255 |
*
|
256 |
* if (g == null) { continue; }
|
257 |
*
|
258 |
* Rectangle2D r = g.getBounds2D(); Envelope e = new
|
259 |
* Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(), r.getY() +
|
260 |
* r.getHeight()); index.insert(e, new Integer(i)); } } catch
|
261 |
* (DriverIOException e) { throw new EditionException(e);
|
262 |
*/
|
263 |
} catch (IOException e) { |
264 |
throw new EditionException(e); |
265 |
} |
266 |
|
267 |
System.err.println("Se han metido en el ?ndice " |
268 |
+ index.queryAll().size() + " geometr?as");
|
269 |
|
270 |
} |
271 |
|
272 |
/*
|
273 |
* (non-Javadoc)
|
274 |
*
|
275 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#stopEdition(com.iver.cit.gvsig.fmap.edition.IWriter)
|
276 |
*/
|
277 |
public void stopEdition(IWriter writer) throws EditionException { |
278 |
// TODO Auto-generated method stub
|
279 |
super.stopEdition(writer);
|
280 |
} |
281 |
|
282 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
283 |
throws DriverException {
|
284 |
return new MyIterator(r, strEPSG); |
285 |
} |
286 |
|
287 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, |
288 |
String[] alphaNumericFieldsNeeded) throws DriverException { |
289 |
return new MyIterator(r, strEPSG); |
290 |
} |
291 |
|
292 |
public String[] getFields() { |
293 |
VectorialDBAdapter orig = (VectorialDBAdapter) ova; |
294 |
return orig.getFields();
|
295 |
} |
296 |
|
297 |
public String getWhereClause() { |
298 |
VectorialDBAdapter orig = (VectorialDBAdapter) ova; |
299 |
return orig.getWhereClause();
|
300 |
} |
301 |
|
302 |
public String getTableName() { |
303 |
VectorialDBAdapter orig = (VectorialDBAdapter) ova; |
304 |
return orig.getTableName();
|
305 |
} |
306 |
|
307 |
public DBLayerDefinition getLyrDef() {
|
308 |
VectorialDBAdapter orig = (VectorialDBAdapter) ova; |
309 |
return orig.getLyrDef();
|
310 |
} |
311 |
|
312 |
public int getRowIndexByFID(IFeature feat) { |
313 |
int resul;
|
314 |
VectorialDBAdapter orig = (VectorialDBAdapter) ova; |
315 |
resul = orig.getRowIndexByFID(feat); |
316 |
if (resul == -1) |
317 |
{ |
318 |
// No est? en los originales. Si no est? borrado,
|
319 |
// estar? en el fichero de expansi?n.
|
320 |
Integer integer = (Integer) hashFIDtoExpansionFile.get(feat.getID()); |
321 |
resul = integer.intValue(); |
322 |
// resul = getInversedIndex(resul);
|
323 |
|
324 |
} |
325 |
return resul;
|
326 |
} |
327 |
|
328 |
/* (non-Javadoc)
|
329 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doAddRow(com.iver.cit.gvsig.fmap.core.IRow)
|
330 |
*/
|
331 |
public int doAddRow(IRow feat) throws DriverIOException, IOException { |
332 |
int virtualIndex = super.doAddRow(feat); |
333 |
int externalIndex = getInversedIndex(virtualIndex);
|
334 |
hashFIDtoExpansionFile.put(feat.getID(), new Integer(externalIndex)); |
335 |
return virtualIndex;
|
336 |
|
337 |
} |
338 |
|
339 |
/* (non-Javadoc)
|
340 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doModifyRow(int, com.iver.cit.gvsig.fmap.core.IRow)
|
341 |
*/
|
342 |
public int doModifyRow(int index, IRow feat) throws IOException, DriverIOException { |
343 |
int virtualIndex = super.doModifyRow(index, feat); |
344 |
// int externalIndex = getInversedIndex(virtualIndex);
|
345 |
hashFIDtoExpansionFile.put(feat.getID(), new Integer(index)); |
346 |
return virtualIndex;
|
347 |
} |
348 |
|
349 |
/* (non-Javadoc)
|
350 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doRemoveRow(int)
|
351 |
*/
|
352 |
public void doRemoveRow(int index) throws DriverIOException, IOException { |
353 |
// Le entra un calculatedIndex, as? que delRows tiene guardados
|
354 |
// los ?ndices internos, no los externos.
|
355 |
super.doRemoveRow(index);
|
356 |
} |
357 |
|
358 |
} |