svn-gvsig-desktop / tags / v1_0_2_Build_908 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableDBAdapter.java @ 11054
History | View | Annotate | Download (13 KB)
1 | 4159 | fjp | /**
|
---|---|---|---|
2 | 5115 | caballero | *
|
3 | 4159 | fjp | */
|
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 | 6856 | fjp | import com.hardcode.driverManager.Driver; |
13 | 4159 | fjp | import com.iver.cit.gvsig.fmap.DriverException; |
14 | 8765 | jjdelcerro | import com.iver.cit.gvsig.fmap.DriverIOExceptionType; |
15 | 4159 | fjp | import com.iver.cit.gvsig.fmap.core.IFeature; |
16 | 4181 | fjp | import com.iver.cit.gvsig.fmap.core.IRow; |
17 | 4159 | fjp | import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
18 | 4171 | fjp | import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
19 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
20 | 4159 | fjp | import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
21 | import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver; |
||
22 | 4171 | fjp | import com.iver.cit.gvsig.fmap.layers.ISpatialDB; |
23 | import com.iver.cit.gvsig.fmap.layers.VectorialDBAdapter; |
||
24 | 4159 | fjp | import com.vividsolutions.jts.geom.Envelope; |
25 | 4171 | fjp | import com.vividsolutions.jts.index.quadtree.Quadtree; |
26 | 4159 | fjp | |
27 | /**
|
||
28 | * @author fjp
|
||
29 | 5115 | caballero | *
|
30 | 4159 | fjp | */
|
31 | 4181 | fjp | public class VectorialEditableDBAdapter extends VectorialEditableAdapter |
32 | implements ISpatialDB {
|
||
33 | 4159 | fjp | |
34 | 4181 | fjp | private class MyIterator implements IFeatureIterator { |
35 | 4171 | fjp | private Rectangle2D extent = null; |
36 | 4181 | fjp | |
37 | 4171 | fjp | private VectorialDBAdapter orig;
|
38 | 4181 | fjp | |
39 | 4171 | fjp | private IFeature feat;
|
40 | 4181 | fjp | |
41 | 4171 | fjp | private IFeatureIterator featIt;
|
42 | 4181 | fjp | |
43 | 4171 | fjp | private String epsg; |
44 | 4181 | fjp | |
45 | 4171 | fjp | private VectorialDatabaseDriver dbDriver;
|
46 | 4181 | fjp | |
47 | 4171 | fjp | Hashtable alreadyDone = new Hashtable(); |
48 | 4181 | fjp | |
49 | 4171 | fjp | private int idFromExpansion = 0; |
50 | 4181 | fjp | |
51 | 4171 | fjp | private List listFromExpansion; |
52 | 4181 | fjp | |
53 | private boolean bOriginalCursorOpened = true; |
||
54 | |||
55 | public MyIterator(Rectangle2D r, String strEPSG) throws DriverException { |
||
56 | 4171 | fjp | extent = r; |
57 | epsg = strEPSG; |
||
58 | orig = (VectorialDBAdapter) ova; |
||
59 | featIt = orig.getFeatureIterator(extent, epsg); |
||
60 | 6365 | fjp | dbDriver = (VectorialDatabaseDriver) getOriginalDriver(); |
61 | 4171 | fjp | getFeaturesFromExpansionFile(); |
62 | } |
||
63 | 4181 | fjp | |
64 | 4171 | fjp | public boolean hasNext() throws DriverException { |
65 | feat = null;
|
||
66 | 4181 | fjp | 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 | 4171 | fjp | { |
71 | 4181 | fjp | bOriginalCursorOpened = featIt.hasNext(); |
72 | if (bOriginalCursorOpened) {
|
||
73 | feat = featIt.next(); |
||
74 | int originalIndex = dbDriver.getRowIndexByFID(feat);
|
||
75 | 5115 | caballero | |
76 | 4182 | fjp | // Iteramos hasta que encontremos alguno no borrado.
|
77 | 4191 | fjp | // Aqu? suponemos que el orden es el original. Si no, no funcionar?.
|
78 | if (delRows.get(originalIndex)) // Si est? borrado |
||
79 | 4182 | fjp | { |
80 | feat = null;
|
||
81 | boolean bFound = false; |
||
82 | while (featIt.hasNext())
|
||
83 | { |
||
84 | feat = featIt.next(); |
||
85 | originalIndex = dbDriver.getRowIndexByFID(feat); |
||
86 | 4191 | fjp | // calculatedIndex = getCalculatedIndex(originalIndex);
|
87 | if (delRows.get(originalIndex) == false) // Si NO est? borrado |
||
88 | 4182 | fjp | { |
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 | 6025 | fjp | feat = null;
|
97 | 4182 | fjp | } |
98 | } // if delRows
|
||
99 | if (bOriginalCursorOpened) // Si todav?a quedan features por leer, y no est?n borradas |
||
100 | { |
||
101 | 4191 | fjp | calculatedIndex = originalIndex; //getCalculatedIndex(originalIndex);
|
102 | 4182 | fjp | Integer integer = new Integer(calculatedIndex); |
103 | if (!relations.containsKey(integer)) { // Si no est? en el |
||
104 | // fichero de
|
||
105 | // expansi?n
|
||
106 | 4181 | fjp | alreadyDone.put(integer, feat); |
107 | 4182 | fjp | } 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 | 8765 | jjdelcerro | DriverIOExceptionType type = |
117 | new DriverIOExceptionType();
|
||
118 | type.setDriverName(getDriver().getName()); |
||
119 | throw new DriverException(e, type); |
||
120 | 4182 | fjp | } |
121 | 8765 | jjdelcerro | |
122 | |||
123 | |||
124 | |||
125 | |||
126 | 4182 | fjp | } // else
|
127 | } // if tercer bOriginalCursorOpened
|
||
128 | } // if segundo bOriginalCursorOpened
|
||
129 | } // if primer bOriginalCursorOpened
|
||
130 | 4181 | fjp | if (!bOriginalCursorOpened) {
|
131 | // Si ya no hay m?s de las originales, todav?a tenemos
|
||
132 | 4191 | fjp | // que revisar las a?adidas que hay en el fichero
|
133 | 4181 | fjp | // de expansi?n
|
134 | 4191 | fjp | try {
|
135 | while ((idFromExpansion < expansionFile.getSize()) && (feat == null)) |
||
136 | 4182 | fjp | { |
137 | 4191 | fjp | IRowEdited rowEd = expansionFile.getRow(idFromExpansion); |
138 | 4195 | fjp | IFeature aux = (IFeature) rowEd.getLinkedRow(); |
139 | 4191 | fjp | Integer calculated = (Integer) mapFID2index.get(aux.getID()); |
140 | calculatedIndex = calculated.intValue(); |
||
141 | System.out.println("El elemento idFromExpansion = " + idFromExpansion + " es " + aux.getID()); |
||
142 | 5115 | caballero | |
143 | 4191 | fjp | // Revisamos los borrados
|
144 | if (delRows.get(calculatedIndex) == true) |
||
145 | 4182 | fjp | { |
146 | 4191 | fjp | boolean bFound = false; |
147 | 4193 | fjp | while ((!bFound) && (idFromExpansion < expansionFile.getSize()-1)) |
148 | 4182 | fjp | { |
149 | 4195 | fjp | // calculatedIndex++;
|
150 | 4191 | fjp | idFromExpansion++; |
151 | 4195 | fjp | rowEd = expansionFile.getRow(idFromExpansion); |
152 | aux = (IFeature) rowEd.getLinkedRow(); |
||
153 | 5115 | caballero | |
154 | |||
155 | |||
156 | 4195 | fjp | Integer auxCalculated = (Integer) mapFID2index.get(aux.getID()); |
157 | calculatedIndex = auxCalculated.intValue(); |
||
158 | 4193 | fjp | // 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 | 4191 | fjp | { |
161 | bFound = true;
|
||
162 | 4193 | fjp | calculated = auxCalculated; |
163 | 4191 | fjp | break;
|
164 | } |
||
165 | else
|
||
166 | { |
||
167 | 5115 | caballero | System.out.println("El elemento idFromExpansion = " + idFromExpansion + " est? borrado"); |
168 | 4191 | fjp | } |
169 | 4182 | fjp | } |
170 | 4191 | fjp | if (bFound)
|
171 | { |
||
172 | calculated = new Integer(calculatedIndex); |
||
173 | rowEd = expansionFile.getRow(idFromExpansion); |
||
174 | 5115 | caballero | aux = (IFeature) rowEd.getLinkedRow(); |
175 | 4191 | fjp | } |
176 | else
|
||
177 | { |
||
178 | return false; // El resto est?n borrados |
||
179 | } |
||
180 | } // if primer borrado
|
||
181 | if (relations.containsKey(calculated))
|
||
182 | 4182 | fjp | { |
183 | 4191 | fjp | Integer realExpansionIndex = (Integer) relations.get(calculated); |
184 | if (realExpansionIndex.intValue() == idFromExpansion)
|
||
185 | { |
||
186 | 4195 | fjp | feat = (IFeature) aux.cloneRow(); |
187 | 4181 | fjp | } |
188 | } |
||
189 | 4191 | fjp | idFromExpansion++; |
190 | 4181 | fjp | } |
191 | 4191 | fjp | } catch (IOException e) { |
192 | throw new DriverException(e); |
||
193 | 4181 | fjp | } |
194 | 4171 | fjp | } |
195 | 4181 | fjp | |
196 | if (calculatedIndex == -1) |
||
197 | 4171 | fjp | return false; |
198 | 4181 | fjp | else {
|
199 | 4191 | fjp | 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 | 4171 | fjp | return true; |
209 | 4181 | fjp | } |
210 | |||
211 | 4171 | fjp | } |
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 | 4181 | fjp | |
220 | 4171 | fjp | } |
221 | 4181 | fjp | |
222 | private void getFeaturesFromExpansionFile() { |
||
223 | Envelope e = FConverter.convertRectangle2DtoEnvelope(extent); |
||
224 | listFromExpansion = index.query(e); |
||
225 | 4171 | fjp | } |
226 | } |
||
227 | 4181 | fjp | |
228 | 5115 | caballero | |
229 | 4191 | fjp | private Hashtable mapFID2index = new Hashtable(); |
230 | private Hashtable mapIndex2FID = new Hashtable(); |
||
231 | 4159 | fjp | /**
|
232 | 5115 | caballero | *
|
233 | 4159 | fjp | */
|
234 | public VectorialEditableDBAdapter() {
|
||
235 | super();
|
||
236 | } |
||
237 | |||
238 | 4181 | fjp | /*
|
239 | * (non-Javadoc)
|
||
240 | 5115 | caballero | *
|
241 | 4181 | fjp | * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#getFeatures(java.awt.geom.Rectangle2D,
|
242 | * java.lang.String)
|
||
243 | 4159 | fjp | */
|
244 | 4181 | fjp | public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG) |
245 | throws DriverException {
|
||
246 | 4159 | fjp | ArrayList aux = new ArrayList(); |
247 | 4181 | fjp | IFeatureIterator featIt = getFeatureIterator(r, strEPSG, null);
|
248 | 4191 | fjp | int numEntities = 0; |
249 | 4181 | fjp | while (featIt.hasNext()) {
|
250 | IFeature feat = featIt.next(); |
||
251 | // TODO:
|
||
252 | 4191 | fjp | assert(feat !=null); |
253 | 4181 | fjp | int index = getRowIndexByFID(feat);
|
254 | IRowEdited edRow = new DefaultRowEdited(feat, IRowEdited.STATUS_ORIGINAL, index);
|
||
255 | aux.add(edRow); |
||
256 | 4191 | fjp | numEntities++; |
257 | 4171 | fjp | } |
258 | |||
259 | 4181 | fjp | return (IRowEdited[]) aux.toArray(new IRowEdited[0]); |
260 | // return (IFeature[]) aux.toArray(new IFeature[0]);
|
||
261 | |||
262 | 4159 | fjp | } |
263 | |||
264 | 4181 | fjp | /*
|
265 | * (non-Javadoc)
|
||
266 | 5115 | caballero | *
|
267 | 4159 | fjp | * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#startEdition()
|
268 | */
|
||
269 | 5184 | caballero | public void startEdition(int sourceType) throws EditionException { |
270 | 4171 | fjp | isEditing = true;
|
271 | 6856 | fjp | Driver drv = ova.getDriver();
|
272 | if (drv instanceof IWriteable) |
||
273 | { |
||
274 | setWriter(((IWriteable) drv).getWriter()); |
||
275 | } |
||
276 | |||
277 | 4181 | fjp | try {
|
278 | expansionFile.open(); |
||
279 | 4171 | fjp | |
280 | 4181 | fjp | // 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 | 10401 | fjp | |
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 | 4191 | fjp | 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 | 4171 | fjp | |
300 | 4181 | fjp | /*
|
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 | 5115 | caballero | *
|
306 | 4181 | fjp | * if (g == null) { continue; }
|
307 | 5115 | caballero | *
|
308 | 4181 | fjp | * 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 | 4191 | fjp | } catch (DriverIOException e) {
|
316 | throw new EditionException(e); |
||
317 | } catch (DriverException e) {
|
||
318 | throw new EditionException(e); |
||
319 | 4181 | fjp | } |
320 | 4171 | fjp | |
321 | 4181 | fjp | System.err.println("Se han metido en el ?ndice " |
322 | + index.queryAll().size() + " geometr?as");
|
||
323 | 4171 | fjp | |
324 | 4159 | fjp | } |
325 | |||
326 | 4181 | fjp | public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
327 | throws DriverException {
|
||
328 | 4171 | fjp | return new MyIterator(r, strEPSG); |
329 | } |
||
330 | |||
331 | 4181 | fjp | public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, |
332 | String[] alphaNumericFieldsNeeded) throws DriverException { |
||
333 | 4171 | fjp | return new MyIterator(r, strEPSG); |
334 | } |
||
335 | |||
336 | public DBLayerDefinition getLyrDef() {
|
||
337 | VectorialDBAdapter orig = (VectorialDBAdapter) ova; |
||
338 | return orig.getLyrDef();
|
||
339 | } |
||
340 | |||
341 | 4181 | fjp | public int getRowIndexByFID(IFeature feat) { |
342 | 4191 | fjp | Integer calculatedIndex = (Integer) mapFID2index.get(feat.getID()); |
343 | return getInversedIndex(calculatedIndex.intValue());
|
||
344 | 4181 | fjp | } |
345 | |||
346 | /* (non-Javadoc)
|
||
347 | * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doAddRow(com.iver.cit.gvsig.fmap.core.IRow)
|
||
348 | */
|
||
349 | 5184 | caballero | public int doAddRow(IRow feat, int sourceType) throws DriverIOException, IOException { |
350 | int calculatedIndex = super.doAddRow(feat, sourceType); |
||
351 | 6313 | fjp | // Integer posInExpansionFile = (Integer) relations.get(new Integer(calculatedIndex));
|
352 | 4193 | fjp | Integer virtual = new Integer(calculatedIndex); // calculatedIndex es igual al numero de shapes originales + el numero de entidades a?adidas. |
353 | 4191 | fjp | // 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 | 4193 | fjp | return calculatedIndex;
|
358 | 5115 | caballero | |
359 | 4181 | fjp | } |
360 | |||
361 | /* (non-Javadoc)
|
||
362 | * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doModifyRow(int, com.iver.cit.gvsig.fmap.core.IRow)
|
||
363 | */
|
||
364 | 5184 | caballero | 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 | 4191 | fjp | // 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 | 4195 | fjp | mapFID2index.put(feat.getID(), virtual); |
377 | 5115 | caballero | mapIndex2FID.put(virtual, feat.getID()); |
378 | 4191 | fjp | return posAnteriorInExpansionFile;
|
379 | 4181 | fjp | } |
380 | |||
381 | /* (non-Javadoc)
|
||
382 | * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doRemoveRow(int)
|
||
383 | */
|
||
384 | 5184 | caballero | public IRow doRemoveRow(int index,int sourceType) throws DriverIOException, IOException { |
385 | 4182 | fjp | // Le entra un calculatedIndex, as? que delRows tiene guardados
|
386 | // los ?ndices internos, no los externos.
|
||
387 | 5184 | caballero | IFeature deletedFeat = (IFeature) super.doRemoveRow(index, sourceType);
|
388 | 4191 | fjp | // Lo borramos de hashFIDtoExpansionFile
|
389 | // hashFIDtoExpansionFile.remove(deletedFeat.getID());
|
||
390 | return deletedFeat;
|
||
391 | 4181 | fjp | } |
392 | |||
393 | 4191 | fjp | /* (non-Javadoc)
|
394 | * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#undoAddRow(int)
|
||
395 | */
|
||
396 | 5184 | caballero | public void undoAddRow(int calculatedIndex, int sourceType) throws DriverIOException, IOException { |
397 | 4191 | fjp | // TODO Auto-generated method stub
|
398 | 5184 | caballero | super.undoAddRow(calculatedIndex,sourceType);
|
399 | 4195 | fjp | Integer calculated = new Integer(calculatedIndex); |
400 | String theID = (String) mapIndex2FID.get(calculated); |
||
401 | mapFID2index.remove(calculated); |
||
402 | mapIndex2FID.remove(theID); |
||
403 | 5115 | caballero | |
404 | 4191 | fjp | } |
405 | |||
406 | |||
407 | 4159 | fjp | } |