Statistics
| Revision:

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

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