Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1012 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableDBAdapter.java @ 12987

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