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