svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableDBAdapter.java @ 11468
History | View | Annotate | Download (16.1 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.iver.cit.gvsig.fmap.edition; |
5 |
|
6 |
import java.awt.geom.Rectangle2D; |
7 |
import java.util.ArrayList; |
8 |
import java.util.Hashtable; |
9 |
import java.util.List; |
10 |
|
11 |
import org.cresques.cts.IProjection; |
12 |
|
13 |
import com.hardcode.driverManager.Driver; |
14 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
15 |
import com.iver.cit.gvsig.exceptions.commands.EditionCommandException; |
16 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
17 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException; |
18 |
import com.iver.cit.gvsig.exceptions.expansionfile.OpenExpansionFileException; |
19 |
import com.iver.cit.gvsig.exceptions.layers.CancelEditingLayerException; |
20 |
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException; |
21 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
22 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
23 |
import com.iver.cit.gvsig.fmap.core.IRow; |
24 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
25 |
import com.iver.cit.gvsig.fmap.drivers.AttrQueryFeatureIterator; |
26 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
27 |
import com.iver.cit.gvsig.fmap.drivers.DefaultFeatureIterator; |
28 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
29 |
import com.iver.cit.gvsig.fmap.drivers.IndexedSptQueryFeatureIterator; |
30 |
import com.iver.cit.gvsig.fmap.drivers.SpatialQueryFeatureIterator; |
31 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
32 |
import com.iver.cit.gvsig.fmap.layers.ISpatialDB; |
33 |
import com.iver.cit.gvsig.fmap.layers.VectorialDBAdapter; |
34 |
import com.vividsolutions.jts.geom.Envelope; |
35 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
36 |
|
37 |
/**
|
38 |
* @author fjp
|
39 |
*
|
40 |
*/
|
41 |
public class VectorialEditableDBAdapter extends VectorialEditableAdapter |
42 |
implements ISpatialDB {
|
43 |
|
44 |
private class MyIterator implements IFeatureIterator { |
45 |
private Rectangle2D extent = null; |
46 |
|
47 |
private VectorialDBAdapter orig;
|
48 |
|
49 |
private IFeature feat;
|
50 |
|
51 |
private IFeatureIterator featIt;
|
52 |
|
53 |
private String epsg; |
54 |
|
55 |
private VectorialDatabaseDriver dbDriver;
|
56 |
|
57 |
Hashtable alreadyDone = new Hashtable(); |
58 |
|
59 |
private int idFromExpansion = 0; |
60 |
|
61 |
private List listFromExpansion; |
62 |
|
63 |
private boolean bOriginalCursorOpened = true; |
64 |
|
65 |
|
66 |
public MyIterator(Rectangle2D r, String strEPSG) throws ReadDriverException { |
67 |
extent = r; |
68 |
epsg = strEPSG; |
69 |
orig = (VectorialDBAdapter) ova; |
70 |
featIt = orig.getFeatureIterator(extent, epsg); |
71 |
dbDriver = (VectorialDatabaseDriver) getOriginalDriver(); |
72 |
getFeaturesFromExpansionFile(); |
73 |
} |
74 |
|
75 |
|
76 |
/*
|
77 |
* azo: these new constructors must be tested
|
78 |
* */
|
79 |
|
80 |
public MyIterator(String[] fields, IProjection newProjection) throws ReadDriverException{ |
81 |
epsg = newProjection.getAbrev(); |
82 |
orig = (VectorialDBAdapter) ova; |
83 |
featIt = orig.getFeatureIterator(fields, newProjection); |
84 |
dbDriver = (VectorialDatabaseDriver) getOriginalDriver(); |
85 |
getFeaturesFromExpansionFile(); |
86 |
} |
87 |
|
88 |
public MyIterator(String sql, IProjection newProjection) throws ReadDriverException{ |
89 |
epsg = newProjection.getAbrev(); |
90 |
orig = (VectorialDBAdapter) ova; |
91 |
featIt = orig.getFeatureIterator(sql, newProjection); |
92 |
dbDriver = (VectorialDatabaseDriver) getOriginalDriver(); |
93 |
getFeaturesFromExpansionFile(); |
94 |
} |
95 |
|
96 |
public MyIterator(Rectangle2D rect, String[] fields, IProjection newProjection) throws ReadDriverException{ |
97 |
extent = rect; |
98 |
epsg = newProjection.getAbrev(); |
99 |
orig = (VectorialDBAdapter) ova; |
100 |
featIt = orig.getFeatureIterator(extent, fields, newProjection); |
101 |
dbDriver = (VectorialDatabaseDriver) getOriginalDriver(); |
102 |
getFeaturesFromExpansionFile(); |
103 |
} |
104 |
|
105 |
|
106 |
|
107 |
public boolean hasNext() throws ReadDriverException, ExpansionFileReadException { |
108 |
feat = null;
|
109 |
int calculatedIndex = -1; |
110 |
if (bOriginalCursorOpened) // Si hay originales (Es porque si se ha |
111 |
// llegado al final, se cierra el
|
112 |
// iterador y salta un fallo
|
113 |
{ |
114 |
bOriginalCursorOpened = featIt.hasNext(); |
115 |
if (bOriginalCursorOpened) {
|
116 |
feat = featIt.next(); |
117 |
int originalIndex = dbDriver.getRowIndexByFID(feat);
|
118 |
|
119 |
// Iteramos hasta que encontremos alguno no borrado.
|
120 |
// Aqu? suponemos que el orden es el original. Si no, no funcionar?.
|
121 |
if (delRows.get(originalIndex)) // Si est? borrado |
122 |
{ |
123 |
feat = null;
|
124 |
boolean bFound = false; |
125 |
while (featIt.hasNext())
|
126 |
{ |
127 |
feat = featIt.next(); |
128 |
originalIndex = dbDriver.getRowIndexByFID(feat); |
129 |
// calculatedIndex = getCalculatedIndex(originalIndex);
|
130 |
if (delRows.get(originalIndex) == false) // Si NO est? borrado |
131 |
{ |
132 |
bFound = true;
|
133 |
break;
|
134 |
} |
135 |
} |
136 |
if (bFound == false) // Todos los ?ltimos est?n borrados. |
137 |
{ |
138 |
bOriginalCursorOpened = false; // para que busque en el fichero de expansi?n |
139 |
feat = null;
|
140 |
} |
141 |
} // if delRows
|
142 |
if (bOriginalCursorOpened) // Si todav?a quedan features por leer, y no est?n borradas |
143 |
{ |
144 |
calculatedIndex = originalIndex; //getCalculatedIndex(originalIndex);
|
145 |
Integer integer = new Integer(calculatedIndex); |
146 |
if (!relations.containsKey(integer)) { // Si no est? en el |
147 |
// fichero de
|
148 |
// expansi?n
|
149 |
alreadyDone.put(integer, feat); |
150 |
} else { // Si est? en el fichero de expansi?n |
151 |
int num = ((Integer) relations.get(integer)).intValue(); |
152 |
IRowEdited auxR; |
153 |
auxR = expansionFile.getRow(num); |
154 |
feat = (IFeature) auxR.getLinkedRow().cloneRow(); |
155 |
// feat = (IFeature) auxR.getLinkedRow();
|
156 |
alreadyDone.put(integer, feat); |
157 |
} // else
|
158 |
} // if tercer bOriginalCursorOpened
|
159 |
} // if segundo bOriginalCursorOpened
|
160 |
} // if primer bOriginalCursorOpened
|
161 |
if (!bOriginalCursorOpened) {
|
162 |
// Si ya no hay m?s de las originales, todav?a tenemos
|
163 |
// que revisar las a?adidas que hay en el fichero
|
164 |
// de expansi?n
|
165 |
while ((idFromExpansion < expansionFile.getSize()) && (feat == null)) |
166 |
{ |
167 |
IRowEdited rowEd = expansionFile.getRow(idFromExpansion); |
168 |
IFeature aux = (IFeature) rowEd.getLinkedRow(); |
169 |
Integer calculated = (Integer) mapFID2index.get(aux.getID()); |
170 |
calculatedIndex = calculated.intValue(); |
171 |
System.out.println("El elemento idFromExpansion = " + idFromExpansion + " es " + aux.getID()); |
172 |
|
173 |
// Revisamos los borrados
|
174 |
if (delRows.get(calculatedIndex) == true) |
175 |
{ |
176 |
boolean bFound = false; |
177 |
while ((!bFound) && (idFromExpansion < expansionFile.getSize()-1)) |
178 |
{ |
179 |
// calculatedIndex++;
|
180 |
idFromExpansion++; |
181 |
rowEd = expansionFile.getRow(idFromExpansion); |
182 |
aux = (IFeature) rowEd.getLinkedRow(); |
183 |
|
184 |
|
185 |
|
186 |
Integer auxCalculated = (Integer) mapFID2index.get(aux.getID()); |
187 |
calculatedIndex = auxCalculated.intValue(); |
188 |
// Si no est? borrado y es una entidad v?lida, que est? siendo usada (no es algo que est? en el expansionFile sin usarse)
|
189 |
if ((delRows.get(calculatedIndex) == false) && (relations.containsKey(auxCalculated))) |
190 |
{ |
191 |
bFound = true;
|
192 |
calculated = auxCalculated; |
193 |
break;
|
194 |
} |
195 |
else
|
196 |
{ |
197 |
System.out.println("El elemento idFromExpansion = " + idFromExpansion + " est? borrado"); |
198 |
} |
199 |
} |
200 |
if (bFound)
|
201 |
{ |
202 |
calculated = new Integer(calculatedIndex); |
203 |
rowEd = expansionFile.getRow(idFromExpansion); |
204 |
aux = (IFeature) rowEd.getLinkedRow(); |
205 |
} |
206 |
else
|
207 |
{ |
208 |
return false; // El resto est?n borrados |
209 |
} |
210 |
} // if primer borrado
|
211 |
if (relations.containsKey(calculated))
|
212 |
{ |
213 |
Integer realExpansionIndex = (Integer) relations.get(calculated); |
214 |
if (realExpansionIndex.intValue() == idFromExpansion)
|
215 |
{ |
216 |
feat = (IFeature) aux.cloneRow(); |
217 |
} |
218 |
} |
219 |
idFromExpansion++; |
220 |
} |
221 |
} |
222 |
|
223 |
if (calculatedIndex == -1) |
224 |
return false; |
225 |
else {
|
226 |
if (feat == null) |
227 |
{ |
228 |
if (idFromExpansion == expansionFile.getSize())
|
229 |
return false; |
230 |
else
|
231 |
System.err.println("ERROR DE ENTREGA DE FEATURE EN hasNext del Iterador"); |
232 |
} |
233 |
/* if (delRows.get(calculatedIndex))
|
234 |
feat = null; */
|
235 |
return true; |
236 |
} |
237 |
|
238 |
} |
239 |
|
240 |
public IFeature next() {
|
241 |
return feat;
|
242 |
} |
243 |
|
244 |
public void closeIterator() throws ReadDriverException { |
245 |
// TODO Auto-generated method stub
|
246 |
|
247 |
} |
248 |
|
249 |
private void getFeaturesFromExpansionFile() { |
250 |
Envelope e = FConverter.convertRectangle2DtoEnvelope(extent); |
251 |
listFromExpansion = index.query(e); |
252 |
} |
253 |
} |
254 |
|
255 |
|
256 |
private Hashtable mapFID2index = new Hashtable(); |
257 |
private Hashtable mapIndex2FID = new Hashtable(); |
258 |
/**
|
259 |
*
|
260 |
*/
|
261 |
public VectorialEditableDBAdapter() {
|
262 |
super();
|
263 |
} |
264 |
|
265 |
/*
|
266 |
* (non-Javadoc)
|
267 |
*
|
268 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#getFeatures(java.awt.geom.Rectangle2D,
|
269 |
* java.lang.String)
|
270 |
*/
|
271 |
public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG) throws ReadDriverException, ExpansionFileReadException { |
272 |
ArrayList aux = new ArrayList(); |
273 |
IFeatureIterator featIt = getFeatureIterator(r, strEPSG, null);
|
274 |
int numEntities = 0; |
275 |
while (featIt.hasNext()) {
|
276 |
IFeature feat = featIt.next(); |
277 |
// TODO:
|
278 |
assert(feat !=null); |
279 |
int index = getRowIndexByFID(feat);
|
280 |
IRowEdited edRow = new DefaultRowEdited(feat, IRowEdited.STATUS_ORIGINAL, index);
|
281 |
aux.add(edRow); |
282 |
numEntities++; |
283 |
} |
284 |
|
285 |
return (IRowEdited[]) aux.toArray(new IRowEdited[0]); |
286 |
// return (IFeature[]) aux.toArray(new IFeature[0]);
|
287 |
|
288 |
} |
289 |
|
290 |
/*
|
291 |
* (non-Javadoc)
|
292 |
*
|
293 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#startEdition()
|
294 |
*/
|
295 |
public void startEdition(int sourceType) throws StartWriterVisitorException { |
296 |
isEditing = true;
|
297 |
Driver drv = ova.getDriver();
|
298 |
if (drv instanceof IWriteable) |
299 |
{ |
300 |
setWriter(((IWriteable) drv).getWriter()); |
301 |
} |
302 |
|
303 |
try {
|
304 |
expansionFile.open(); |
305 |
|
306 |
// TODO: Si la capa dispone de un ?ndice espacial, hacer
|
307 |
// algo aqu? para que se use ese ?ndice espacial.
|
308 |
index = new Quadtree();
|
309 |
// No metemos ninguna entidad de las originales dentro
|
310 |
// de la base de datos porque esa consulta ya la
|
311 |
// hace getFeatures sin tener en cuenta el ?ndice local.
|
312 |
for (int i = 0; i < ova.getShapeCount(); i++) |
313 |
{ |
314 |
IFeature feat = ova.getFeature(i); |
315 |
Integer calculatedIndex = new Integer(i); |
316 |
mapFID2index.put(feat.getID(), calculatedIndex); |
317 |
mapIndex2FID.put(calculatedIndex, feat.getID()); |
318 |
} |
319 |
|
320 |
/*
|
321 |
* for (int i = 0; i < ova.getShapeCount(); i++) { IGeometry g=null;
|
322 |
* try { g = ((DefaultFeature) ova.getFeature(i)).getGeometry(); }
|
323 |
* catch (DriverException e1) { // TODO Auto-generated catch block
|
324 |
* e1.printStackTrace(); }
|
325 |
*
|
326 |
* if (g == null) { continue; }
|
327 |
*
|
328 |
* Rectangle2D r = g.getBounds2D(); Envelope e = new
|
329 |
* Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(), r.getY() +
|
330 |
* r.getHeight()); index.insert(e, new Integer(i)); } } catch
|
331 |
* (DriverIOException e) { throw new EditionException(e);
|
332 |
*/
|
333 |
} catch (ReadDriverException e) {
|
334 |
throw new StartWriterVisitorException(writer.getName(),e); |
335 |
} catch (ExpansionFileReadException e) {
|
336 |
throw new StartWriterVisitorException(writer.getName(),e); |
337 |
} catch (OpenExpansionFileException e) {
|
338 |
throw new StartWriterVisitorException(writer.getName(),e); |
339 |
} |
340 |
|
341 |
System.err.println("Se han metido en el ?ndice " |
342 |
+ index.queryAll().size() + " geometr?as");
|
343 |
|
344 |
} |
345 |
|
346 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
347 |
throws ReadDriverException {
|
348 |
return new MyIterator(r, strEPSG); |
349 |
} |
350 |
|
351 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, |
352 |
String[] alphaNumericFieldsNeeded) throws ReadDriverException { |
353 |
return new MyIterator(r, strEPSG); |
354 |
} |
355 |
|
356 |
public IFeatureIterator getFeatureIterator(String[] fields, IProjection newProjection) |
357 |
throws ReadDriverException{
|
358 |
return new DefaultFeatureIterator(this, projection, newProjection, fields); |
359 |
} |
360 |
|
361 |
/**
|
362 |
* Return a feature iterator from a given sql statement.
|
363 |
* <br>
|
364 |
* In this case, the statement will have the "projection" operator
|
365 |
* (select campo1, campo2, ...etc) and the "selection" operator (where ....)
|
366 |
* @param sql statement which define a filter
|
367 |
* @return feature iterator
|
368 |
* */
|
369 |
public IFeatureIterator getFeatureIterator(String sql, |
370 |
IProjection newProjection) throws ReadDriverException{
|
371 |
|
372 |
return new AttrQueryFeatureIterator(this, projection, newProjection, sql); |
373 |
} |
374 |
|
375 |
|
376 |
/**
|
377 |
* Makes an spatial query returning a feature iterator over the features which intersects
|
378 |
* or are contained in the rectangle query. Applies a restriction to the alphanumeric fields
|
379 |
* returned by the iterator.
|
380 |
* @param rect
|
381 |
* @param fields
|
382 |
* @return
|
383 |
* @throws ReadDriverException
|
384 |
*/
|
385 |
public IFeatureIterator getFeatureIterator(Rectangle2D rect, String[] fields, IProjection newProjection) throws ReadDriverException{ |
386 |
if(fmapSpatialIndex == null){ |
387 |
return new SpatialQueryFeatureIterator(this, projection, newProjection, fields, rect); |
388 |
}else{
|
389 |
return new IndexedSptQueryFeatureIterator(this, projection, newProjection, fields, rect, fmapSpatialIndex); |
390 |
} |
391 |
} |
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
public DBLayerDefinition getLyrDef() {
|
400 |
VectorialDBAdapter orig = (VectorialDBAdapter) ova; |
401 |
return orig.getLyrDef();
|
402 |
} |
403 |
|
404 |
public int getRowIndexByFID(IFeature feat) { |
405 |
Integer calculatedIndex = (Integer) mapFID2index.get(feat.getID()); |
406 |
return getInversedIndex(calculatedIndex.intValue());
|
407 |
} |
408 |
|
409 |
/* (non-Javadoc)
|
410 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doAddRow(com.iver.cit.gvsig.fmap.core.IRow)
|
411 |
*/
|
412 |
public int doAddRow(IRow feat, int sourceType) throws ReadDriverException, ExpansionFileWriteException { |
413 |
int calculatedIndex = super.doAddRow(feat, sourceType); |
414 |
// Integer posInExpansionFile = (Integer) relations.get(new Integer(calculatedIndex));
|
415 |
Integer virtual = new Integer(calculatedIndex); // calculatedIndex es igual al numero de shapes originales + el numero de entidades a?adidas. |
416 |
// es decir, virtual es el calculatedIndex (no tiene en cuenta los borrados)
|
417 |
// calculatedIndex = indiceExterno + borrados hasta ese punto.
|
418 |
mapFID2index.put(feat.getID(), virtual); |
419 |
mapIndex2FID.put(virtual, feat.getID()); |
420 |
return calculatedIndex;
|
421 |
|
422 |
} |
423 |
|
424 |
/* (non-Javadoc)
|
425 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doModifyRow(int, com.iver.cit.gvsig.fmap.core.IRow)
|
426 |
*/
|
427 |
public int doModifyRow(int calculatedIndex, IRow feat,int sourceType) throws ReadDriverException, ExpansionFileWriteException, ExpansionFileReadException{ |
428 |
int posAnteriorInExpansionFile = super.doModifyRow(calculatedIndex, feat, sourceType); // devolver? -1 si es original |
429 |
// No hacemos nada con las modificaciones sobre los ?ndices.
|
430 |
// Suponiendo que feat tenga la misma ID que la que hab?a antes.
|
431 |
Integer virtual = new Integer(calculatedIndex); |
432 |
String theIDoriginal = (String) mapIndex2FID.get(virtual); |
433 |
if (!theIDoriginal.equals(feat.getID()))
|
434 |
{ |
435 |
AssertionError err = new AssertionError("Fallo al modificar la fila. ID viejo=" + theIDoriginal + " ID nuevo = " + feat.getID()); |
436 |
err.printStackTrace(); |
437 |
} |
438 |
// hashFIDtoExpansionFile.put(feat.getID(), new Integer(posInExpansionFile));
|
439 |
mapFID2index.put(feat.getID(), virtual); |
440 |
mapIndex2FID.put(virtual, feat.getID()); |
441 |
return posAnteriorInExpansionFile;
|
442 |
} |
443 |
|
444 |
/* (non-Javadoc)
|
445 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doRemoveRow(int)
|
446 |
*/
|
447 |
public IRow doRemoveRow(int index,int sourceType) throws ReadDriverException, ExpansionFileReadException { |
448 |
// Le entra un calculatedIndex, as? que delRows tiene guardados
|
449 |
// los ?ndices internos, no los externos.
|
450 |
IFeature deletedFeat = (IFeature) super.doRemoveRow(index, sourceType);
|
451 |
// Lo borramos de hashFIDtoExpansionFile
|
452 |
// hashFIDtoExpansionFile.remove(deletedFeat.getID());
|
453 |
return deletedFeat;
|
454 |
} |
455 |
|
456 |
/* (non-Javadoc)
|
457 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#undoAddRow(int)
|
458 |
*/
|
459 |
public void undoAddRow(int calculatedIndex, int sourceType) throws EditionCommandException { |
460 |
// TODO Auto-generated method stub
|
461 |
super.undoAddRow(calculatedIndex,sourceType);
|
462 |
Integer calculated = new Integer(calculatedIndex); |
463 |
String theID = (String) mapIndex2FID.get(calculated); |
464 |
mapFID2index.remove(calculated); |
465 |
mapIndex2FID.remove(theID); |
466 |
|
467 |
} |
468 |
public void cancelEdition(int sourceType) throws CancelEditingLayerException { |
469 |
super.cancelEdition(sourceType);
|
470 |
mapFID2index.clear(); |
471 |
mapIndex2FID.clear(); |
472 |
} |
473 |
|
474 |
public void stopEdition(IWriter writer, int sourceType) throws StopWriterVisitorException{ |
475 |
super.stopEdition(writer, sourceType);
|
476 |
mapFID2index.clear(); |
477 |
mapIndex2FID.clear(); |
478 |
} |
479 |
|
480 |
|
481 |
} |