Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / VectorialEditableDBAdapter.java @ 6326

History | View | Annotate | Download (12.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
import com.iver.cit.gvsig.fmap.DriverException;
13
import com.iver.cit.gvsig.fmap.core.IFeature;
14 4191 fjp
import com.iver.cit.gvsig.fmap.core.IGeometry;
15 4181 fjp
import com.iver.cit.gvsig.fmap.core.IRow;
16 4159 fjp
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
17 4171 fjp
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
18
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
19 4159 fjp
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
20
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
21 4171 fjp
import com.iver.cit.gvsig.fmap.layers.ISpatialDB;
22 4191 fjp
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
23 4171 fjp
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
                        dbDriver = (VectorialDatabaseDriver) getDriver();
61
                        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
                                                                e.printStackTrace();
117
                                                                throw new DriverException(e);
118
                                                        }
119
                                                } // else
120
                                        } // if tercer bOriginalCursorOpened
121
                                } // if segundo bOriginalCursorOpened
122
                        } // if primer bOriginalCursorOpened
123 4181 fjp
                        if (!bOriginalCursorOpened) {
124
                                // Si ya no hay m?s de las originales, todav?a tenemos
125 4191 fjp
                                // que revisar las a?adidas que hay en el fichero
126 4181 fjp
                                // de expansi?n
127 4191 fjp
                                try {
128
                                        while ((idFromExpansion < expansionFile.getSize()) && (feat == null))
129 4182 fjp
                                        {
130 4191 fjp
                                                IRowEdited rowEd = expansionFile.getRow(idFromExpansion);
131 4195 fjp
                                                IFeature aux = (IFeature) rowEd.getLinkedRow();
132 4191 fjp
                                                Integer calculated = (Integer) mapFID2index.get(aux.getID());
133
                                                calculatedIndex = calculated.intValue();
134
                                                System.out.println("El elemento idFromExpansion = " + idFromExpansion + " es " + aux.getID());
135 5115 caballero
136 4191 fjp
                                                // Revisamos los borrados
137
                                                if (delRows.get(calculatedIndex) == true)
138 4182 fjp
                                                {
139 4191 fjp
                                                        boolean bFound = false;
140 4193 fjp
                                                        while ((!bFound) && (idFromExpansion < expansionFile.getSize()-1))
141 4182 fjp
                                                        {
142 4195 fjp
                                                                // calculatedIndex++;
143 4191 fjp
                                                                idFromExpansion++;
144 4195 fjp
                                                                rowEd = expansionFile.getRow(idFromExpansion);
145
                                                                aux = (IFeature) rowEd.getLinkedRow();
146 5115 caballero
147
148
149 4195 fjp
                                                                Integer auxCalculated = (Integer) mapFID2index.get(aux.getID());
150
                                                                calculatedIndex = auxCalculated.intValue();
151 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)
152
                                                                if ((delRows.get(calculatedIndex) == false) && (relations.containsKey(auxCalculated)))
153 4191 fjp
                                                                {
154
                                                                        bFound = true;
155 4193 fjp
                                                                        calculated = auxCalculated;
156 4191 fjp
                                                                        break;
157
                                                                }
158
                                                                else
159
                                                                {
160 5115 caballero
                                                                        System.out.println("El elemento idFromExpansion = " + idFromExpansion + " est? borrado");
161 4191 fjp
                                                                }
162 4182 fjp
                                                        }
163 4191 fjp
                                                        if (bFound)
164
                                                        {
165
                                                                calculated = new Integer(calculatedIndex);
166
                                                                rowEd = expansionFile.getRow(idFromExpansion);
167 5115 caballero
                                                                aux = (IFeature) rowEd.getLinkedRow();
168 4191 fjp
                                                        }
169
                                                        else
170
                                                        {
171
                                                                return false; // El resto est?n borrados
172
                                                        }
173
                                                } // if primer borrado
174
                                                if (relations.containsKey(calculated))
175 4182 fjp
                                                {
176 4191 fjp
                                                        Integer realExpansionIndex = (Integer) relations.get(calculated);
177
                                                        if (realExpansionIndex.intValue() == idFromExpansion)
178
                                                        {
179 4195 fjp
                                                                feat = (IFeature) aux.cloneRow();
180 4181 fjp
                                                        }
181
                                                }
182 4191 fjp
                                                idFromExpansion++;
183 4181 fjp
                                        }
184 4191 fjp
                                } catch (IOException e) {
185
                                        throw new DriverException(e);
186 4181 fjp
                                }
187 4171 fjp
                        }
188 4181 fjp
189
                        if (calculatedIndex == -1)
190 4171 fjp
                                return false;
191 4181 fjp
                        else {
192 4191 fjp
                                if (feat == null)
193
                                {
194
                                        if (idFromExpansion == expansionFile.getSize())
195
                                                return false;
196
                                        else
197
                                                System.err.println("ERROR DE ENTREGA DE FEATURE EN hasNext del Iterador");
198
                                }
199
                                /* if (delRows.get(calculatedIndex))
200
                                        feat = null; */
201 4171 fjp
                                return true;
202 4181 fjp
                        }
203
204 4171 fjp
                }
205
206
                public IFeature next() throws DriverException {
207
                        return feat;
208
                }
209
210
                public void closeIterator() throws DriverException {
211
                        // TODO Auto-generated method stub
212 4181 fjp
213 4171 fjp
                }
214 4181 fjp
215
                private void getFeaturesFromExpansionFile() {
216
                        Envelope e = FConverter.convertRectangle2DtoEnvelope(extent);
217
                        listFromExpansion = index.query(e);
218 4171 fjp
                }
219
        }
220 4181 fjp
221 5115 caballero
222 4191 fjp
        private Hashtable mapFID2index = new Hashtable();
223
        private Hashtable mapIndex2FID = new Hashtable();
224 4159 fjp
        /**
225 5115 caballero
         *
226 4159 fjp
         */
227
        public VectorialEditableDBAdapter() {
228
                super();
229
        }
230
231 4181 fjp
        /*
232
         * (non-Javadoc)
233 5115 caballero
         *
234 4181 fjp
         * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#getFeatures(java.awt.geom.Rectangle2D,
235
         *      java.lang.String)
236 4159 fjp
         */
237 4181 fjp
        public IRowEdited[] getFeatures(Rectangle2D r, String strEPSG)
238
                        throws DriverException {
239 4159 fjp
                ArrayList aux = new ArrayList();
240 4181 fjp
                IFeatureIterator featIt = getFeatureIterator(r, strEPSG, null);
241 4191 fjp
                int numEntities = 0;
242 4181 fjp
                while (featIt.hasNext()) {
243
                        IFeature feat = featIt.next();
244
                        // TODO:
245 4191 fjp
                        assert(feat !=null);
246 4181 fjp
                        int index = getRowIndexByFID(feat);
247
                        IRowEdited edRow = new DefaultRowEdited(feat, IRowEdited.STATUS_ORIGINAL, index);
248
                        aux.add(edRow);
249 4191 fjp
                        numEntities++;
250 4171 fjp
                }
251
252 4181 fjp
                return (IRowEdited[]) aux.toArray(new IRowEdited[0]);
253
                // return (IFeature[]) aux.toArray(new IFeature[0]);
254
255 4159 fjp
        }
256
257 4181 fjp
        /*
258
         * (non-Javadoc)
259 5115 caballero
         *
260 4159 fjp
         * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#startEdition()
261
         */
262 5184 caballero
        public void startEdition(int sourceType) throws EditionException {
263 4171 fjp
                isEditing = true;
264 4181 fjp
                try {
265
                        expansionFile.open();
266 4171 fjp
267 4181 fjp
                        // TODO: Si la capa dispone de un ?ndice espacial, hacer
268
                        // algo aqu? para que se use ese ?ndice espacial.
269
                        index = new Quadtree();
270
                        // No metemos ninguna entidad de las originales dentro
271
                        // de la base de datos porque esa consulta ya la
272
                        // hace getFeatures sin tener en cuenta el ?ndice local.
273 4191 fjp
                         for (int i = 0; i < ova.getShapeCount(); i++)
274
                         {
275
                                 IFeature feat = ova.getFeature(i);
276
                                 Integer calculatedIndex = new Integer(i);
277
                                 mapFID2index.put(feat.getID(), calculatedIndex);
278
                                 mapIndex2FID.put(calculatedIndex, feat.getID());
279
                         }
280 4171 fjp
281 4181 fjp
                        /*
282
                         * for (int i = 0; i < ova.getShapeCount(); i++) { IGeometry g=null;
283
                         * try { g = ((DefaultFeature) ova.getFeature(i)).getGeometry(); }
284
                         * catch (DriverException e1) { // TODO Auto-generated catch block
285
                         * e1.printStackTrace(); }
286 5115 caballero
                         *
287 4181 fjp
                         * if (g == null) { continue; }
288 5115 caballero
                         *
289 4181 fjp
                         * Rectangle2D r = g.getBounds2D(); Envelope e = new
290
                         * Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(), r.getY() +
291
                         * r.getHeight()); index.insert(e, new Integer(i)); } } catch
292
                         * (DriverIOException e) { throw new EditionException(e);
293
                         */
294
                } catch (IOException e) {
295
                        throw new EditionException(e);
296 4191 fjp
                } catch (DriverIOException e) {
297
                        throw new EditionException(e);
298
                } catch (DriverException e) {
299
                        throw new EditionException(e);
300 4181 fjp
                }
301 4171 fjp
302 4181 fjp
                System.err.println("Se han metido en el ?ndice "
303
                                + index.queryAll().size() + " geometr?as");
304 4171 fjp
305 4159 fjp
        }
306
307 4181 fjp
        public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG)
308
                        throws DriverException {
309 4171 fjp
                return new MyIterator(r, strEPSG);
310
        }
311
312 4181 fjp
        public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG,
313
                        String[] alphaNumericFieldsNeeded) throws DriverException {
314 4171 fjp
                return new MyIterator(r, strEPSG);
315
        }
316
317
        public DBLayerDefinition getLyrDef() {
318
                VectorialDBAdapter orig = (VectorialDBAdapter) ova;
319
                return orig.getLyrDef();
320
        }
321
322 4181 fjp
        public int getRowIndexByFID(IFeature feat) {
323 4191 fjp
                Integer calculatedIndex = (Integer) mapFID2index.get(feat.getID());
324
                return getInversedIndex(calculatedIndex.intValue());
325 4181 fjp
        }
326
327
        /* (non-Javadoc)
328
         * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doAddRow(com.iver.cit.gvsig.fmap.core.IRow)
329
         */
330 5184 caballero
        public int doAddRow(IRow feat, int sourceType) throws DriverIOException, IOException {
331
                int calculatedIndex = super.doAddRow(feat, sourceType);
332 6313 fjp
                // Integer posInExpansionFile = (Integer) relations.get(new Integer(calculatedIndex));
333 4193 fjp
                Integer virtual = new Integer(calculatedIndex); // calculatedIndex es igual al numero de shapes originales + el numero de entidades a?adidas.
334 4191 fjp
                                        // es decir, virtual es el calculatedIndex (no tiene en cuenta los borrados)
335
                                        // calculatedIndex = indiceExterno + borrados hasta ese punto.
336
                mapFID2index.put(feat.getID(), virtual);
337
                mapIndex2FID.put(virtual, feat.getID());
338 4193 fjp
                return calculatedIndex;
339 5115 caballero
340 4181 fjp
        }
341
342
        /* (non-Javadoc)
343
         * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doModifyRow(int, com.iver.cit.gvsig.fmap.core.IRow)
344
         */
345 5184 caballero
        public int doModifyRow(int calculatedIndex, IRow feat,int sourceType) throws IOException, DriverIOException {
346
                int posAnteriorInExpansionFile = super.doModifyRow(calculatedIndex, feat, sourceType); // devolver? -1 si es original
347 4191 fjp
                // No hacemos nada con las modificaciones sobre los ?ndices.
348
                // Suponiendo que feat tenga la misma ID que la que hab?a antes.
349
                Integer virtual = new Integer(calculatedIndex);
350
                String theIDoriginal = (String) mapIndex2FID.get(virtual);
351
                if (!theIDoriginal.equals(feat.getID()))
352
                {
353
                        AssertionError err = new AssertionError("Fallo al modificar la fila. ID viejo=" + theIDoriginal + " ID nuevo = " + feat.getID());
354
                        err.printStackTrace();
355
                }
356
                // hashFIDtoExpansionFile.put(feat.getID(), new Integer(posInExpansionFile));
357 4195 fjp
                mapFID2index.put(feat.getID(), virtual);
358 5115 caballero
                mapIndex2FID.put(virtual, feat.getID());
359 4191 fjp
                return posAnteriorInExpansionFile;
360 4181 fjp
        }
361
362
        /* (non-Javadoc)
363
         * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#doRemoveRow(int)
364
         */
365 5184 caballero
        public IRow doRemoveRow(int index,int sourceType) throws DriverIOException, IOException {
366 4182 fjp
                // Le entra un calculatedIndex, as? que delRows tiene guardados
367
                // los ?ndices internos, no los externos.
368 5184 caballero
                IFeature deletedFeat = (IFeature) super.doRemoveRow(index, sourceType);
369 4191 fjp
                // Lo borramos de hashFIDtoExpansionFile
370
                // hashFIDtoExpansionFile.remove(deletedFeat.getID());
371
                return deletedFeat;
372 4181 fjp
        }
373
374 4191 fjp
        /* (non-Javadoc)
375
         * @see com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter#undoAddRow(int)
376
         */
377 5184 caballero
        public void undoAddRow(int calculatedIndex, int sourceType) throws DriverIOException, IOException {
378 4191 fjp
                // TODO Auto-generated method stub
379 5184 caballero
                super.undoAddRow(calculatedIndex,sourceType);
380 4195 fjp
                Integer calculated = new Integer(calculatedIndex);
381
                String theID = (String) mapIndex2FID.get(calculated);
382
                mapFID2index.remove(calculated);
383
                mapIndex2FID.remove(theID);
384 5115 caballero
385 4191 fjp
        }
386
387
388 4159 fjp
}