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