svn-gvsig-desktop / tags / v1_1_Build_1004 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableDBAdapter.java @ 12319
History | View | Annotate | Download (13.5 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.driverManager.Driver; |
13 |
import com.iver.cit.gvsig.fmap.DriverException; |
14 |
import com.iver.cit.gvsig.fmap.DriverIOExceptionType; |
15 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
16 |
import com.iver.cit.gvsig.fmap.core.IRow; |
17 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
18 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
19 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
20 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
21 |
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver; |
22 |
import com.iver.cit.gvsig.fmap.layers.ISpatialDB; |
23 |
import com.iver.cit.gvsig.fmap.layers.VectorialDBAdapter; |
24 |
import com.vividsolutions.jts.geom.Envelope; |
25 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
26 |
|
27 |
/**
|
28 |
* @author fjp
|
29 |
*
|
30 |
*/
|
31 |
public class VectorialEditableDBAdapter extends VectorialEditableAdapter |
32 |
implements ISpatialDB {
|
33 |
private class MyIterator implements IFeatureIterator { |
34 |
private Rectangle2D extent = null; |
35 |
|
36 |
private VectorialDBAdapter orig;
|
37 |
|
38 |
private IFeature feat;
|
39 |
|
40 |
private IFeatureIterator featIt;
|
41 |
|
42 |
private String epsg; |
43 |
|
44 |
private IVectorialDatabaseDriver dbDriver;
|
45 |
|
46 |
Hashtable alreadyDone = new Hashtable(); |
47 |
|
48 |
private int idFromExpansion = 0; |
49 |
|
50 |
private List listFromExpansion; |
51 |
|
52 |
private boolean bOriginalCursorOpened = true; |
53 |
|
54 |
public MyIterator(Rectangle2D r, String strEPSG) throws DriverException { |
55 |
extent = r; |
56 |
epsg = strEPSG; |
57 |
orig = (VectorialDBAdapter) ova; |
58 |
featIt = orig.getFeatureIterator(extent, epsg); |
59 |
dbDriver = (IVectorialDatabaseDriver) getOriginalDriver(); |
60 |
getFeaturesFromExpansionFile(); |
61 |
} |
62 |
|
63 |
public boolean hasNext() throws DriverException { |
64 |
feat = null;
|
65 |
int calculatedIndex = -1; |
66 |
if (bOriginalCursorOpened) // Si hay originales (Es porque si se ha |
67 |
// llegado al final, se cierra el
|
68 |
// iterador y salta un fallo
|
69 |
{ |
70 |
bOriginalCursorOpened = featIt.hasNext(); |
71 |
if (bOriginalCursorOpened) {
|
72 |
feat = featIt.next(); |
73 |
int originalIndex = dbDriver.getRowIndexByFID(feat);
|
74 |
// Iteramos hasta que encontremos alguno no borrado.
|
75 |
// Aqu? suponemos que el orden es el original. Si no, no funcionar?.
|
76 |
if (delRows.get(originalIndex)) // Si est? borrado |
77 |
{ |
78 |
feat = null;
|
79 |
boolean bFound = false; |
80 |
while (featIt.hasNext())
|
81 |
{ |
82 |
feat = featIt.next(); |
83 |
originalIndex = dbDriver.getRowIndexByFID(feat); |
84 |
// calculatedIndex = getCalculatedIndex(originalIndex);
|
85 |
if (delRows.get(originalIndex) == false) // Si NO est? borrado |
86 |
{ |
87 |
bFound = true;
|
88 |
break;
|
89 |
} |
90 |
} |
91 |
if (bFound == false) // Todos los ?ltimos est?n borrados. |
92 |
{ |
93 |
bOriginalCursorOpened = false; // para que busque en el fichero de expansi?n |
94 |
feat = null;
|
95 |
} |
96 |
} // if delRows
|
97 |
if (bOriginalCursorOpened) // Si todav?a quedan features por leer, y no est?n borradas |
98 |
{ |
99 |
calculatedIndex = originalIndex; //getCalculatedIndex(originalIndex);
|
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 |
DriverIOExceptionType type = |
115 |
new DriverIOExceptionType();
|
116 |
type.setDriverName(getDriver().getName()); |
117 |
throw new DriverException(e, type); |
118 |
} |
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
} // else
|
125 |
} // if tercer bOriginalCursorOpened
|
126 |
} // if segundo bOriginalCursorOpened
|
127 |
} // if primer bOriginalCursorOpened
|
128 |
if (!bOriginalCursorOpened) {
|
129 |
// Si ya no hay m?s de las originales, todav?a tenemos
|
130 |
// que revisar las a?adidas que hay en el fichero
|
131 |
// de expansi?n
|
132 |
try {
|
133 |
while ((idFromExpansion < expansionFile.getSize()) && (feat == null)) |
134 |
{ |
135 |
IRowEdited rowEd = expansionFile.getRow(idFromExpansion); |
136 |
IFeature aux = (IFeature) rowEd.getLinkedRow(); |
137 |
Integer calculated = (Integer) mapFID2index.get(aux.getID()); |
138 |
calculatedIndex = calculated.intValue(); |
139 |
System.out.println("El elemento idFromExpansion = " + idFromExpansion + " es " + aux.getID()); |
140 |
|
141 |
// Revisamos los borrados
|
142 |
if (delRows.get(calculatedIndex) == true) |
143 |
{ |
144 |
boolean bFound = false; |
145 |
while ((!bFound) && (idFromExpansion < expansionFile.getSize()-1)) |
146 |
{ |
147 |
// calculatedIndex++;
|
148 |
idFromExpansion++; |
149 |
rowEd = expansionFile.getRow(idFromExpansion); |
150 |
aux = (IFeature) rowEd.getLinkedRow(); |
151 |
|
152 |
|
153 |
|
154 |
Integer auxCalculated = (Integer) mapFID2index.get(aux.getID()); |
155 |
calculatedIndex = auxCalculated.intValue(); |
156 |
// Si no est? borrado y es una entidad v?lida, que est? siendo usada (no es algo que est? en el expansionFile sin usarse)
|
157 |
if ((delRows.get(calculatedIndex) == false) && (relations.containsKey(auxCalculated))) |
158 |
{ |
159 |
bFound = true;
|
160 |
calculated = auxCalculated; |
161 |
break;
|
162 |
} |
163 |
else
|
164 |
{ |
165 |
System.out.println("El elemento idFromExpansion = " + idFromExpansion + " est? borrado"); |
166 |
} |
167 |
} |
168 |
if (bFound)
|
169 |
{ |
170 |
calculated = new Integer(calculatedIndex); |
171 |
rowEd = expansionFile.getRow(idFromExpansion); |
172 |
aux = (IFeature) rowEd.getLinkedRow(); |
173 |
} |
174 |
else
|
175 |
{ |
176 |
return false; // El resto est?n borrados |
177 |
} |
178 |
} // if primer borrado
|
179 |
if (relations.containsKey(calculated))
|
180 |
{ |
181 |
Integer realExpansionIndex = (Integer) relations.get(calculated); |
182 |
if (realExpansionIndex.intValue() == idFromExpansion)
|
183 |
{ |
184 |
feat = (IFeature) aux.cloneRow(); |
185 |
} |
186 |
} |
187 |
idFromExpansion++; |
188 |
} |
189 |
} catch (IOException e) { |
190 |
throw new DriverException(e); |
191 |
} |
192 |
} |
193 |
|
194 |
if (calculatedIndex == -1) |
195 |
return false; |
196 |
else {
|
197 |
if (feat == null) |
198 |
{ |
199 |
if (idFromExpansion == expansionFile.getSize())
|
200 |
return false; |
201 |
else
|
202 |
System.err.println("ERROR DE ENTREGA DE FEATURE EN hasNext del Iterador"); |
203 |
} |
204 |
/* if (delRows.get(calculatedIndex))
|
205 |
feat = null; */
|
206 |
return true; |
207 |
} |
208 |
|
209 |
} |
210 |
|
211 |
public IFeature next() throws DriverException { |
212 |
return feat;
|
213 |
} |
214 |
|
215 |
public void closeIterator() throws DriverException { |
216 |
// TODO Auto-generated method stub
|
217 |
|
218 |
} |
219 |
|
220 |
private void getFeaturesFromExpansionFile() { |
221 |
Envelope e = FConverter.convertRectangle2DtoEnvelope(extent); |
222 |
listFromExpansion = index.query(e); |
223 |
} |
224 |
} |
225 |
|
226 |
|
227 |
private Hashtable mapFID2index = new Hashtable(); |
228 |
// private Hashtable mapIndex2FID = new Hashtable();
|
229 |
/**
|
230 |
*
|
231 |
*/
|
232 |
public VectorialEditableDBAdapter() {
|
233 |
super();
|
234 |
} |
235 |
|
236 |
/*
|
237 |
* (non-Javadoc)
|
238 |
*
|
239 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#getFeatures(java.awt.geom.Rectangle2D,
|
240 |
* java.lang.String)
|
241 |
*/
|
242 |
public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG) |
243 |
throws DriverException {
|
244 |
ArrayList aux = new ArrayList(); |
245 |
IFeatureIterator featIt = getFeatureIterator(r, strEPSG, null);
|
246 |
int numEntities = 0; |
247 |
while (featIt.hasNext()) {
|
248 |
IFeature feat = featIt.next(); |
249 |
// TODO:
|
250 |
assert(feat !=null); |
251 |
int index = getRowIndexByFID(feat);
|
252 |
IRowEdited edRow = new DefaultRowEdited(feat, IRowEdited.STATUS_ORIGINAL, index);
|
253 |
aux.add(edRow); |
254 |
numEntities++; |
255 |
} |
256 |
|
257 |
return (IRowEdited[]) aux.toArray(new IRowEdited[0]); |
258 |
// return (IFeature[]) aux.toArray(new IFeature[0]);
|
259 |
|
260 |
} |
261 |
|
262 |
/*
|
263 |
* (non-Javadoc)
|
264 |
*
|
265 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#startEdition()
|
266 |
*/
|
267 |
public void startEdition(int sourceType) throws EditionException { |
268 |
isEditing = true;
|
269 |
Driver drv = ova.getDriver();
|
270 |
if (drv instanceof IWriteable) |
271 |
{ |
272 |
setWriter(((IWriteable) drv).getWriter()); |
273 |
} |
274 |
|
275 |
try {
|
276 |
expansionFile.open(); |
277 |
|
278 |
// TODO: Si la capa dispone de un ?ndice espacial, hacer
|
279 |
// algo aqu? para que se use ese ?ndice espacial.
|
280 |
index = new Quadtree();
|
281 |
// No metemos ninguna entidad de las originales dentro
|
282 |
// de la base de datos porque esa consulta ya la
|
283 |
// hace getFeatures sin tener en cuenta el ?ndice local.
|
284 |
|
285 |
// TODO: URGENTE !!!!!
|
286 |
// PONER ALGO EN LAS DRIVERS DE BASE DE DATOS PARA QUE
|
287 |
// ESTO SEA R?PIDO!!! No podemos pedir TOOOOOODAS las features
|
288 |
// para hacer un ?ndice que YA EXISTE dentro del VectorialDriver!!.
|
289 |
|
290 |
for (int i = 0; i < ova.getShapeCount(); i++) |
291 |
{ |
292 |
IFeature feat = ova.getFeature(i); |
293 |
Integer calculatedIndex = new Integer(i); |
294 |
mapFID2index.put(feat.getID(), calculatedIndex); |
295 |
// mapIndex2FID.put(calculatedIndex, feat.getID());
|
296 |
} |
297 |
|
298 |
/*
|
299 |
* for (int i = 0; i < ova.getShapeCount(); i++) { IGeometry g=null;
|
300 |
* try { g = ((DefaultFeature) ova.getFeature(i)).getGeometry(); }
|
301 |
* catch (DriverException e1) { // TODO Auto-generated catch block
|
302 |
* e1.printStackTrace(); }
|
303 |
*
|
304 |
* if (g == null) { continue; }
|
305 |
*
|
306 |
* Rectangle2D r = g.getBounds2D(); Envelope e = new
|
307 |
* Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(), r.getY() +
|
308 |
* r.getHeight()); index.insert(e, new Integer(i)); } } catch
|
309 |
* (DriverIOException e) { throw new EditionException(e);
|
310 |
*/
|
311 |
} catch (IOException e) { |
312 |
throw new EditionException(e); |
313 |
} catch (DriverIOException e) {
|
314 |
throw new EditionException(e); |
315 |
} catch (DriverException e) {
|
316 |
throw new EditionException(e); |
317 |
} |
318 |
|
319 |
System.err.println("Se han metido en el ?ndice " |
320 |
+ index.queryAll().size() + " geometr?as");
|
321 |
|
322 |
} |
323 |
|
324 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
325 |
throws DriverException {
|
326 |
return new MyIterator(r, strEPSG); |
327 |
} |
328 |
|
329 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, |
330 |
String[] alphaNumericFieldsNeeded) throws DriverException { |
331 |
return new MyIterator(r, strEPSG); |
332 |
} |
333 |
|
334 |
public DBLayerDefinition getLyrDef() {
|
335 |
VectorialDBAdapter orig = (VectorialDBAdapter) ova; |
336 |
return orig.getLyrDef();
|
337 |
} |
338 |
|
339 |
public int getRowIndexByFID(IFeature feat) { |
340 |
Integer calculatedIndex = (Integer) mapFID2index.get(feat.getID()); |
341 |
return getInversedIndex(calculatedIndex.intValue());
|
342 |
} |
343 |
|
344 |
/* (non-Javadoc)
|
345 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doAddRow(com.iver.cit.gvsig.fmap.core.IRow)
|
346 |
*/
|
347 |
public int doAddRow(IRow feat, int sourceType) throws DriverIOException, IOException { |
348 |
int calculatedIndex = super.doAddRow(feat, sourceType); |
349 |
// Integer posInExpansionFile = (Integer) relations.get(new Integer(calculatedIndex));
|
350 |
Integer virtual = new Integer(calculatedIndex); // calculatedIndex es igual al numero de shapes originales + el numero de entidades a?adidas. |
351 |
// es decir, virtual es el calculatedIndex (no tiene en cuenta los borrados)
|
352 |
// calculatedIndex = indiceExterno + borrados hasta ese punto.
|
353 |
mapFID2index.put(feat.getID(), virtual); |
354 |
// mapIndex2FID.put(virtual, feat.getID());
|
355 |
return calculatedIndex;
|
356 |
|
357 |
} |
358 |
|
359 |
/* (non-Javadoc)
|
360 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doModifyRow(int, com.iver.cit.gvsig.fmap.core.IRow)
|
361 |
*/
|
362 |
public int doModifyRow(int calculatedIndex, IRow feat,int sourceType) throws IOException, DriverIOException { |
363 |
int posAnteriorInExpansionFile = super.doModifyRow(calculatedIndex, feat, sourceType); // devolver? -1 si es original |
364 |
// No hacemos nada con las modificaciones sobre los ?ndices.
|
365 |
// Suponiendo que feat tenga la misma ID que la que hab?a antes.
|
366 |
Integer virtual = new Integer(calculatedIndex); |
367 |
// String theIDoriginal = (String) mapIndex2FID.get(virtual);
|
368 |
// if (!theIDoriginal.equals(feat.getID()))
|
369 |
// {
|
370 |
// AssertionError err = new AssertionError("Fallo al modificar la fila. ID viejo=" + theIDoriginal + " ID nuevo = " + feat.getID());
|
371 |
// err.printStackTrace();
|
372 |
// }
|
373 |
// hashFIDtoExpansionFile.put(feat.getID(), new Integer(posInExpansionFile));
|
374 |
mapFID2index.put(feat.getID(), virtual); |
375 |
// mapIndex2FID.put(virtual, feat.getID());
|
376 |
return posAnteriorInExpansionFile;
|
377 |
} |
378 |
|
379 |
/* (non-Javadoc)
|
380 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doRemoveRow(int)
|
381 |
*/
|
382 |
public IRow doRemoveRow(int index,int sourceType) throws DriverIOException, IOException { |
383 |
// Le entra un calculatedIndex, as? que delRows tiene guardados
|
384 |
// los ?ndices internos, no los externos.
|
385 |
IFeature deletedFeat = (IFeature) super.doRemoveRow(index, sourceType);
|
386 |
// Lo borramos de hashFIDtoExpansionFile
|
387 |
// hashFIDtoExpansionFile.remove(deletedFeat.getID());
|
388 |
return deletedFeat;
|
389 |
} |
390 |
|
391 |
/* (non-Javadoc)
|
392 |
* @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#undoAddRow(int)
|
393 |
*/
|
394 |
public void undoAddRow(int calculatedIndex, int sourceType) throws DriverIOException, IOException { |
395 |
// TODO Auto-generated method stub
|
396 |
super.undoAddRow(calculatedIndex,sourceType);
|
397 |
Integer calculated = new Integer(calculatedIndex); |
398 |
// String theID = (String) mapIndex2FID.get(calculated);
|
399 |
mapFID2index.remove(calculated); |
400 |
// mapIndex2FID.remove(theID);
|
401 |
|
402 |
} |
403 |
|
404 |
public void cancelEdition(int sourceType) throws IOException { |
405 |
super.cancelEdition(sourceType);
|
406 |
mapFID2index.clear(); |
407 |
// mapIndex2FID.clear();
|
408 |
} |
409 |
|
410 |
public void stopEdition(IWriter writer, int sourceType) throws EditionException { |
411 |
super.stopEdition(writer, sourceType);
|
412 |
mapFID2index.clear(); |
413 |
// mapIndex2FID.clear();
|
414 |
} |
415 |
|
416 |
// public int getNewIndex() throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
417 |
// int index = maxIndex;
|
418 |
// while(mapFID2index.containsKey(String.valueOf(index))){
|
419 |
// index++;
|
420 |
// }
|
421 |
// return index;
|
422 |
// }
|
423 |
|
424 |
|
425 |
} |