Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_905 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / EditableAdapter.java @ 10767

History | View | Annotate | Download (47.7 KB)

1 3940 caballero
package com.iver.cit.gvsig.fmap.edition;
2
3 4061 caballero
import java.io.IOException;
4 4832 fjp
import java.util.ArrayList;
5 6212 fjp
import java.util.Collection;
6 4061 caballero
import java.util.HashMap;
7 6212 fjp
import java.util.Iterator;
8
import java.util.TreeMap;
9 3940 caballero
10 5569 jmvivo
import com.hardcode.driverManager.Driver;
11 4061 caballero
import com.hardcode.driverManager.DriverLoadException;
12 3940 caballero
import com.hardcode.gdbms.engine.data.DataSourceFactory;
13
import com.hardcode.gdbms.engine.data.NoSuchTableException;
14 9887 fjp
import com.hardcode.gdbms.engine.data.driver.AlphanumericDBDriver;
15 3940 caballero
import com.hardcode.gdbms.engine.data.driver.DriverException;
16
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
17
import com.hardcode.gdbms.engine.data.edition.DataWare;
18
import com.hardcode.gdbms.engine.values.Value;
19 4026 caballero
import com.iver.cit.gvsig.fmap.core.DefaultRow;
20 3940 caballero
import com.iver.cit.gvsig.fmap.core.IRow;
21
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
22 6212 fjp
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
23 5558 fjp
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
24
import com.iver.cit.gvsig.fmap.drivers.TableDefinition;
25 6212 fjp
import com.iver.cit.gvsig.fmap.edition.commands.AddFieldCommand;
26 3940 caballero
import com.iver.cit.gvsig.fmap.edition.commands.AddRowCommand;
27 4120 caballero
import com.iver.cit.gvsig.fmap.edition.commands.Command;
28 3940 caballero
import com.iver.cit.gvsig.fmap.edition.commands.CommandCollection;
29
import com.iver.cit.gvsig.fmap.edition.commands.CommandRecord;
30
import com.iver.cit.gvsig.fmap.edition.commands.MemoryCommandRecord;
31
import com.iver.cit.gvsig.fmap.edition.commands.ModifyRowCommand;
32 6212 fjp
import com.iver.cit.gvsig.fmap.edition.commands.RemoveFieldCommand;
33 3940 caballero
import com.iver.cit.gvsig.fmap.edition.commands.RemoveRowCommand;
34 6212 fjp
import com.iver.cit.gvsig.fmap.edition.commands.RenameFieldCommand;
35
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.AbstractFieldManager;
36 5886 fjp
import com.iver.cit.gvsig.fmap.edition.rules.IRule;
37 3940 caballero
import com.iver.cit.gvsig.fmap.layers.FBitSet;
38
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
39
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
40 4832 fjp
import com.iver.cit.gvsig.fmap.operations.Cancel;
41 3940 caballero
42
/**
43
 * DOCUMENT ME!
44 7272 jaume
 *
45 3940 caballero
 * @author Vicente Caballero Navarro
46
 */
47 5595 fjp
public class EditableAdapter implements IEditableSource, IWriteable {
48 4792 fjp
        protected boolean isEditing = false;
49 3996 caballero
50 4792 fjp
        private SelectableDataSource ds = null;
51 3940 caballero
52 4792 fjp
        protected FBitSet delRows = new FBitSet();
53 3940 caballero
54 4792 fjp
        private CommandRecord cr;
55 6212 fjp
56 5549 fjp
        protected IWriter writer;
57 3940 caballero
58 4792 fjp
        /**
59
         * Flag que indica que hay que tomar las siguientes operaciones como una
60
         * operaci?n at?mica
61
         */
62
        private boolean complex = false;
63 3940 caballero
64 4792 fjp
        private CommandCollection commands = null;
65 3940 caballero
66 6212 fjp
        protected ArrayList listFields = new ArrayList();
67
68
        protected ArrayList listInternalFields = new ArrayList();
69 7272 jaume
70 6483 fjp
        protected boolean bFieldsHasBeenChanged = false;
71 6212 fjp
72
        /**
73 6483 fjp
         * La clave ser? el fieldId. Para buscar si un value de una row ha de ser
74 6212 fjp
         * rellenado con defaultValue o con lo que venga del expansion file,
75
         * miraremos si existe en este hash. Si existe, usamos el value del
76
         * expansion file. Si no existe, usamos el defaultValue del campo busc?ndolo
77
         * en la lista internalFields. Por cierto, en listInternalFields NO se
78 6483 fjp
         * borran campos. Solo se van a?adiendo nuevos actualFields.
79 6212 fjp
         */
80 6483 fjp
        protected TreeMap actualFields; // la clave ser? el fieldId.
81 7272 jaume
82 6483 fjp
        protected ArrayList fastAccessFields = new ArrayList();
83 6212 fjp
84
        protected class MyFieldManager extends AbstractFieldManager {
85
86
                public boolean alterTable() throws EditionException {
87
                        return getFieldManager().alterTable();
88
                }
89
90
                public void addField(FieldDescription fieldDesc) {
91
                        super.addField(fieldDesc);
92
                }
93
94
                public FieldDescription removeField(String fieldName) {
95
                        // TODO Auto-generated method stub
96
                        return super.removeField(fieldName);
97
                }
98
99
                public void renameField(String antName, String newName) {
100
                        // TODO Auto-generated method stub
101
                        super.renameField(antName, newName);
102
                }
103
104
        }
105
106 4792 fjp
        /*
107
         * Establece una relaci?n entre los ?ndices de las geometr?as en el
108
         * EditableFeatureSource y los ?ndices en el fichero de expansi?n FJP:
109
         * CAMBIO: NECESITAMOS TRABAJAR CON FEATURE Y FEATUREITERATOR PARA IR
110
         * PREPARANDO EL CAMINO, GUARDAMOS EL FEATUREID (STRING) COMO CLAVE, Y COMO
111
         * VALOR, EL INDICE DENTRO DEL FICHERO DE EXPANSION (Integer). Lo de que
112
         * FeatureId sea un String es por compatibilidad con OGC. Seg?n OGC, una
113
         * Feature tiene que tener un Id string En el caso de los randomaccess,
114
         * ser?n el id de registro En los casos de base de datos espaciales, supongo
115
         * que siempre ser? num?rico tambi?n, pero lo tendremos que convertir a
116
         * string. Lo que est? claro es que NO se puede confiar nunca en que sea
117
         * algo correlativo (1, 2, 3, 4, 5, ... => FALSO!!)
118
         */
119
        protected HashMap relations = new HashMap();
120 3940 caballero
121 4792 fjp
        /*
122
         * Fichero en el que se guardan las nuevas geometr?as, producto de adiciones
123
         * o de modificaciones
124
         */
125
        protected ExpansionFile expansionFile;
126 3940 caballero
127 4792 fjp
        protected int numAdd = 0;
128 3940 caballero
129 4792 fjp
        private ObjectDriver editingDriver = new myObjectDriver();
130 3996 caballero
131 4792 fjp
        private SelectableDataSource ods;
132 4954 caballero
133 4832 fjp
        private ArrayList editionListeners = new ArrayList();
134 3940 caballero
135 5886 fjp
        private ArrayList rules = new ArrayList();
136
137 6212 fjp
        protected int actualIndexFields;
138
139 7272 jaume
        protected boolean isFullExtentDirty = false;
140
141 10333 caballero
        private ArrayList fieldEvents=new ArrayList();
142
        private ArrayList rowEvents=new ArrayList();
143 10234 caballero
144 4792 fjp
        /**
145
         * Crea un nuevo EditableAdapter.
146
         */
147
        public EditableAdapter() {
148 6212 fjp
                expansionFile = new MemoryExpansionFile(this);
149 4792 fjp
                cr = new MemoryCommandRecord();
150
        }
151 3940 caballero
152 4792 fjp
        /**
153
         * DOCUMENT ME!
154 7272 jaume
         *
155 4792 fjp
         * @param ds
156
         *            DOCUMENT ME!
157 7272 jaume
         * @throws DriverException
158 4792 fjp
         */
159 6212 fjp
        public void setOriginalDataSource(SelectableDataSource ds) throws DriverException {
160 4792 fjp
                this.ods = ds;
161 6313 fjp
                initalizeFields(ds);
162 6856 fjp
                Driver drv = ods.getDriver();
163
                if (drv instanceof IWriteable) {
164
                        setWriter(((IWriteable) drv).getWriter());
165
                }
166
167 7272 jaume
168 6313 fjp
        }
169
170
        /**
171
         * @param ds
172
         * @throws DriverException
173
         */
174
        private void initalizeFields(SelectableDataSource ds) throws DriverException {
175 6212 fjp
                FieldDescription[] fields = ds.getFieldsDescription();
176 6313 fjp
                listInternalFields.clear();
177 6326 fjp
                actualIndexFields = 0;
178 6212 fjp
                actualFields = new TreeMap();
179 6483 fjp
//                fastAccessFields = new ArrayList();
180 6212 fjp
                for (int i=0; i < fields.length; i++)
181
                {
182
                        InternalField field = new InternalField(fields[i], InternalField.ORIGINAL, new Integer(i));
183
                        listFields.add(field);
184 6384 fjp
                        // field.setFieldIndex(i);
185 6212 fjp
                        actualFields.put(field.getFieldId(), field);
186 6483 fjp
//                        fastAccessFields.add(fields[i]);
187 6326 fjp
                        System.out.println("INITIALIZEFIELDS: FIELD " + field.getFieldDesc().getFieldAlias());
188 6212 fjp
                }
189 6259 fjp
                try {
190
                        fieldsChanged();
191 6483 fjp
                        bFieldsHasBeenChanged = false;
192 6259 fjp
                } catch (EditionException e) {
193
                        e.printStackTrace();
194
                        throw new DriverException(e);
195
                }
196 4792 fjp
        }
197 3940 caballero
198 6839 fjp
        private TreeMap deepCloneInternalFields(TreeMap col)
199
        {
200
                TreeMap clonedFields = new TreeMap();
201
                for (Iterator iter = col.values().iterator(); iter.hasNext();) {
202
                        InternalField fld = (InternalField) iter.next();
203
                        InternalField clonedField = fld.cloneInternalField();
204
                        clonedFields.put(clonedField.getFieldId(), clonedField);
205
                }
206 7272 jaume
207 6839 fjp
                return clonedFields;
208
        }
209 6259 fjp
        private void fieldsChanged() throws EditionException {
210 6483 fjp
                fastAccessFields= new ArrayList();
211 6839 fjp
                int index = 0;
212 6483 fjp
                for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
213
                        InternalField fld = (InternalField) iter.next();
214
                        fastAccessFields.add(fld.getFieldDesc());
215 6839 fjp
                        fld.setFieldIndex(index++);
216 6483 fjp
                }
217 6384 fjp
218 6839 fjp
                listInternalFields.add(deepCloneInternalFields(actualFields));
219 6212 fjp
                actualIndexFields = listInternalFields.size()-1;
220 6259 fjp
                try {
221 6365 fjp
                        ds = null;
222 6259 fjp
                        getRecordset().mapExternalFields();
223 6483 fjp
                        bFieldsHasBeenChanged = true;
224 6259 fjp
                } catch (DriverLoadException e) {
225
                        e.printStackTrace();
226
                        throw new EditionException(e);
227
                } catch (DriverException e) {
228
                        e.printStackTrace();
229
                        throw new EditionException(e);
230
                }
231 6212 fjp
        }
232
233 4792 fjp
        /**
234
         * DOCUMENT ME!
235 7272 jaume
         *
236 4792 fjp
         * @throws EditionException
237
         *             DOCUMENT ME!
238
         */
239 5184 caballero
        public void startEdition(int sourceType) throws EditionException {
240 4792 fjp
                isEditing = true;
241 5986 caballero
242 5184 caballero
                fireStartEditionEvent(sourceType);
243 4792 fjp
        }
244 3996 caballero
245 4792 fjp
        /**
246
         * Se ejecuta preProcess() del IWriter, luego se itera por los registros
247
         * borrados por si el IWriter los quiere borrar (solo ser? necesario cuando
248
         * escribimos sobre la misma tabla) y luego se itera por los nuevos
249
         * registros llamando a process con el registro correcto. (A?adidos,
250
         * modificados). Para finalizar, se ejecuta PostProcess
251 7272 jaume
         *
252 4792 fjp
         * @param writer
253
         *            IWriter que recibir? las llamadas.
254 7272 jaume
         *
255 4792 fjp
         * @throws EditionException
256
         *             DOCUMENT ME!
257 7272 jaume
         *
258 4792 fjp
         */
259 6212 fjp
        public void stopEdition(IWriter writer, int sourceType)
260
                        throws EditionException {
261
                saveEdits(writer, sourceType);
262 6227 fjp
                isEditing = false;
263 6356 fjp
                cr.clearAll();
264 6212 fjp
                fireStopEditionEvent(sourceType);
265
        }
266
267
        public void saveEdits(IWriter writer, int sourceType)
268
                        throws EditionException {
269 7272 jaume
270 6579 fjp
                // TODO: ARREGLAR ESTO PARA QUE CUANDO HA HABIDO CAMBIOS
271
                // EN LOS CAMPOS, PODAMOS CAMBIAR LO QUE TOQUE (A SER POSIBLE
272
                // SIN TENER QUE REESCRIBIR TODA LA TABLA CON POSTGIS)
273
                if (bFieldsHasBeenChanged)
274
                {
275 7272 jaume
                        // Para cada campo de los originales, miramos si no est? en
276 6579 fjp
                        // los actuales. Si no est?, le decimos al fieldManager
277
                        // que lo borre. Si est?, pero le hemos cambiado el nombre
278
                        // le pedimos al fieldManager que le cambie el nombre.
279
                        // Luego recorremos los campos actuales para ver cuales
280
                        // son nuevos, y los a?adimos.
281 7272 jaume
282 6579 fjp
                        TreeMap ancientFields = (TreeMap) listInternalFields
283
                                        .get(0);
284
                        Collection aux = ancientFields.values();
285
                        Iterator it = aux.iterator();
286
                        while (it.hasNext()) {
287
                                InternalField fld = (InternalField) it.next();
288
                                // System.err.println("fld = " + fld.getFieldDesc().getFieldAlias() +  " id=" + fld.getFieldId());
289
                                if (actualFields.containsKey(fld.getFieldId())) {
290
                                        // Es un original
291
                                        String f1 = fld.getFieldDesc().getFieldName();
292
                                        String f2 = fld.getFieldDesc().getFieldAlias();
293
                                        if (f1.compareTo(f2) != 0)
294
                                        {
295
                                                getFieldManager().renameField(f1, f2);
296 7272 jaume
                                        }
297 6579 fjp
                                }
298
                                else
299
                                {        // No est?, hay que borrarlo
300
                                        getFieldManager().removeField(fld.getFieldDesc().getFieldAlias());
301
                                }
302
                        }
303
                        Collection aux2= actualFields.values();
304
                        Iterator it2 = aux2.iterator();
305
                        while (it2.hasNext()) {
306
                                InternalField fld = (InternalField) it2.next();
307
                                // System.err.println("fld = " + fld.getFieldDesc().getFieldAlias() +  " id=" + fld.getFieldId());
308
                                if (!ancientFields.containsKey(fld.getFieldId())) {
309
                                        // Es uno a?adido
310
                                        getFieldManager().addField(fld.getFieldDesc());
311
                                }
312
                        }
313
                        // getFieldManager().alterTable(); // Se llama dentro del preprocess()
314
                }
315 7272 jaume
316 4792 fjp
                writer.preProcess();
317 4368 caballero
318 4792 fjp
                try {
319 3940 caballero
320 4792 fjp
                        // Procesamos primero los borrados.
321
                        // Cuando se genere un tema nuevo, no se les debe hacer caso
322
                        // a estos registros
323 3996 caballero
324 6212 fjp
                        for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
325
                                        .nextSetBit(i + 1)) {
326 4792 fjp
                                int calculatedIndex = i;
327
                                Integer integer = new Integer(calculatedIndex);
328
                                // Si no est? en el fichero de expansi?n, es de los originales
329
                                // y hay que borrarlo
330
                                DefaultRowEdited edRow = null;
331
                                if (!relations.containsKey(integer)) {
332
                                        edRow = new DefaultRowEdited(new DefaultRow(ods
333
                                                        .getRow(calculatedIndex)),
334
                                                        DefaultRowEdited.STATUS_DELETED, calculatedIndex);
335
                                        writer.process(edRow);
336
                                } else {
337
                                        int num = ((Integer) relations.get(integer)).intValue();
338 4061 caballero
339 4792 fjp
                                        // return expansionFile.getRow(num);
340
                                        IRowEdited rowFromExpansion = expansionFile.getRow(num);
341
                                        // ?Habr?a que hacer aqu? setID(index + "")?
342
                                        edRow = new DefaultRowEdited(rowFromExpansion
343 6212 fjp
                                                        .getLinkedRow().cloneRow(),
344
                                                        DefaultRowEdited.STATUS_DELETED, calculatedIndex);
345 4792 fjp
                                        writer.process(edRow);
346
                                }
347 3940 caballero
348 4792 fjp
                        }
349 3940 caballero
350 4792 fjp
                        int rowCount = getRowCount();
351 8913 fjp
                        if (writer.isWriteAll())
352
                        {
353
                                for (int i = 0; i < rowCount; i++) {
354
                                        IRowEdited rowEdited = getRow(i);
355 10333 caballero
356 8913 fjp
                                        if (rowEdited != null) {
357
                                                writer.process(rowEdited);
358
                                        }
359
                                }
360
                        }
361
                        else
362
                        {
363
                                // Escribimos solo aquellos registros que han cambiado
364
                                for (int i = 0; i < rowCount; i++) {
365
                                        int calculatedIndex = getCalculatedIndex(i);
366
                                        Integer integer = new Integer(calculatedIndex);
367
                                        DefaultRowEdited edRow = null;
368
                                        // Si est? en el fichero de expansi?n hay que modificar
369
                                        if (relations.containsKey(integer)) {
370
                                                int num = ((Integer) relations.get(integer)).intValue();
371 3940 caballero
372 8913 fjp
                                                // return expansionFile.getRow(num);
373
                                                // ExpansionFile ya entrega el registro formateado como debe
374
                                                IRowEdited rowFromExpansion = expansionFile.getRow(num);
375
                                                // ?Habr?a que hacer aqu? setID(index + "")?
376
                                                edRow = new DefaultRowEdited(rowFromExpansion.getLinkedRow()
377
                                                                .cloneRow(), rowFromExpansion.getStatus(), i);
378
                                                writer.process(edRow);
379
                                        }
380 4792 fjp
                                }
381
                        }
382 5668 fjp
                        writer.postProcess();
383 5986 caballero
384 6580 fjp
                        // Hacemos que el escritor se entere de los nuevos campos
385
                        // que tiene. El escritor debe guardar una referencia
386
                        // a los campos con los que trabaja el drier, de forma
387
                        // que al recargar la capa (por ejemplo en el caso de
388
                        // PostGIS, se haga un setData con los campos que se hayan
389
                        // a?adido, borrado o renombrado).
390
                        writer.getTableDefinition().setFieldsDesc(getRecordset().getFieldsDescription());
391
392 6323 fjp
                        ods.reload();
393 6326 fjp
                        ds = null;
394
                        clean();
395 7272 jaume
396 4792 fjp
                } catch (DriverIOException e) {
397
                        e.printStackTrace();
398
                        throw new EditionException(e);
399
                } catch (IOException e) {
400
                        e.printStackTrace();
401
                        throw new EditionException(e);
402
                } catch (DriverException e) {
403
                        e.printStackTrace();
404
                        throw new EditionException(e);
405
                }
406 3940 caballero
407 4792 fjp
        }
408 3940 caballero
409 4792 fjp
        /**
410
         * DOCUMENT ME!
411 7272 jaume
         *
412 4792 fjp
         * @throws IOException
413
         *             DOCUMENT ME!
414
         */
415 5184 caballero
        public void cancelEdition(int sourceType) throws IOException {
416 4792 fjp
                isEditing = false;
417 6313 fjp
                try {
418 6341 fjp
                        ds= null;
419 6313 fjp
                        clean();
420 6356 fjp
                        cr.clearAll();
421 6313 fjp
                } catch (DriverException e) {
422
                        e.printStackTrace();
423
                        throw new IOException("Error: " + e.getMessage());
424
                }
425 5184 caballero
                fireCancelEditionEvent(sourceType);
426 4792 fjp
        }
427 3940 caballero
428 4792 fjp
        /*
429
         * (non-Javadoc)
430 7272 jaume
         *
431 4792 fjp
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
432
         */
433
        public IRowEdited getRow(int index) throws DriverIOException, IOException {
434
                int calculatedIndex = getCalculatedIndex(index);
435
                Integer integer = new Integer(calculatedIndex);
436
                DefaultRowEdited edRow = null;
437
                // Si no est? en el fichero de expansi?n
438
                if (!relations.containsKey(integer)) {
439
                        try {
440
                                /*
441
                                 * edRow = new DefaultRowEdited(new
442
                                 * DefaultRow(ods.getRow(calculatedIndex), "" + index),
443
                                 * DefaultRowEdited.STATUS_ORIGINAL, index);
444
                                 */
445 6313 fjp
                                DefaultRow auxR = new DefaultRow(ods.getRow(calculatedIndex));
446
                                edRow = new DefaultRowEdited(auxR,
447 4792 fjp
                                                DefaultRowEdited.STATUS_ORIGINAL, index);
448 6313 fjp
                                return createExternalRow(edRow, 0);
449
//                                edRow = new DefaultRowEdited(new DefaultRow(ods
450
//                                                .getRow(calculatedIndex)),
451
//                                                DefaultRowEdited.STATUS_ORIGINAL, index);
452 4792 fjp
                        } catch (DriverException e) {
453
                                throw new DriverIOException(e);
454
                        }
455
                } else {
456
                        int num = ((Integer) relations.get(integer)).intValue();
457 3940 caballero
458 4792 fjp
                        // return expansionFile.getRow(num);
459 6313 fjp
                        // ExpansionFile ya entrega el registro formateado como debe
460 4792 fjp
                        IRowEdited rowFromExpansion = expansionFile.getRow(num);
461
                        // ?Habr?a que hacer aqu? setID(index + "")?
462
                        edRow = new DefaultRowEdited(rowFromExpansion.getLinkedRow()
463
                                        .cloneRow(), rowFromExpansion.getStatus(), index);
464
                        return edRow;
465
                }
466 6313 fjp
467 7272 jaume
468
469 4792 fjp
        }
470 3940 caballero
471 4792 fjp
        /**
472
         * DOCUMENT ME!
473 7272 jaume
         *
474 4792 fjp
         * @return DOCUMENT ME!
475 7272 jaume
         *
476 4792 fjp
         * @throws DriverIOException
477
         *             DOCUMENT ME!
478
         * @throws IOException
479
         *             DOCUMENT ME!
480
         */
481
        public int getRowCount() throws DriverIOException, IOException {
482
                try {
483
                        return (int) (ods.getRowCount() + numAdd) - delRows.cardinality();// -
484 6212 fjp
                        // expansionFile.getInvalidRows().cardinality();
485 4792 fjp
                } catch (DriverException e) {
486
                        throw new DriverIOException(e);
487
                }
488
489 4637 caballero
        }
490 3940 caballero
491 6212 fjp
        /*
492
         * (non-Javadoc)
493 7272 jaume
         *
494 6212 fjp
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#addRow(com.iver.cit.gvsig.fmap.core.IRow,
495
         *      java.lang.String)
496 4792 fjp
         */
497 6212 fjp
        public int addRow(IRow row, String descrip, int sourceType)
498
                        throws DriverIOException, IOException {
499 6071 caballero
500 6002 fjp
                try {
501 10234 caballero
                        validateRow(row,sourceType);
502 6002 fjp
                } catch (EditionException e) {
503
                        e.printStackTrace();
504
                        throw new IOException(e.getMessage());
505 5886 fjp
                }
506 5077 caballero
507 5184 caballero
                int calculatedIndex = doAddRow(row, sourceType);
508 6212 fjp
                Command command = new AddRowCommand(this, row, calculatedIndex,
509
                                sourceType);
510 4792 fjp
                command.setDescription(descrip);
511
                if (complex) {
512
                        commands.add(command);
513
                } else {
514
                        cr.pushCommand(command);
515
                }
516 5077 caballero
517 4792 fjp
                return calculatedIndex;
518
        }
519 3940 caballero
520 4792 fjp
        /**
521
         * DOCUMENT ME!
522 7272 jaume
         *
523 4792 fjp
         * @throws DriverIOException
524
         *             DOCUMENT ME!
525
         * @throws IOException
526
         *             DOCUMENT ME!
527
         */
528
        public void undo() throws DriverIOException, IOException {
529
                // seleccion.clear();
530 6127 fjp
                if (cr.moreUndoCommands()) {
531 4792 fjp
                        cr.undoCommand();
532
                }
533
        }
534 3940 caballero
535 4792 fjp
        /**
536
         * DOCUMENT ME!
537 7272 jaume
         *
538 4792 fjp
         * @throws DriverIOException
539
         *             DOCUMENT ME!
540
         * @throws IOException
541
         *             DOCUMENT ME!
542
         */
543
        public void redo() throws DriverIOException, IOException {
544
                // seleccion.clear();
545 6127 fjp
                if (cr.moreRedoCommands()) {
546 4792 fjp
                        cr.redoCommand();
547
                }
548
        }
549 3940 caballero
550 6212 fjp
        /*
551 4792 fjp
         * (non-Javadoc)
552 7272 jaume
         *
553 4792 fjp
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#removeRow(int)
554
         */
555 6212 fjp
        public void removeRow(int index, String descrip, int sourceType)
556
                        throws IOException, DriverIOException {
557 5077 caballero
558 4792 fjp
                int calculatedIndex = getCalculatedIndex(index);
559 6212 fjp
                Command command = new RemoveRowCommand(this, calculatedIndex,
560
                                sourceType);
561 4792 fjp
                command.setDescription(descrip);
562
                if (complex) {
563
                        commands.add(command);
564
                } else {
565
                        cr.pushCommand(command);
566
                }
567 5184 caballero
                doRemoveRow(calculatedIndex, sourceType);
568 5077 caballero
569 4792 fjp
        }
570 3940 caballero
571 4792 fjp
        /*
572
         * (non-Javadoc)
573 7272 jaume
         *
574 4792 fjp
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#modifyRow(int,
575
         *      com.iver.cit.gvsig.fmap.core.IRow)
576
         */
577 5184 caballero
        public int modifyRow(int index, IRow row, String descrip, int sourceType)
578 4792 fjp
                        throws IOException, DriverIOException {
579 4954 caballero
580 6002 fjp
                try {
581 10234 caballero
                        validateRow(row,sourceType);
582 6002 fjp
                } catch (EditionException e) {
583
                        e.printStackTrace();
584
                        throw new IOException(e.getMessage());
585 5886 fjp
                }
586
587 4792 fjp
                int calculatedIndex = getCalculatedIndex(index);
588 5184 caballero
                int pos = doModifyRow(calculatedIndex, row, sourceType);
589 6212 fjp
                Command command = new ModifyRowCommand(this, calculatedIndex, pos, row,
590
                                sourceType);
591 4792 fjp
                command.setDescription(descrip);
592
                if (complex) {
593
                        commands.add(command);
594
                } else {
595
                        cr.pushCommand(command);
596
                }
597 5077 caballero
598 4792 fjp
                return pos;
599
        }
600 3940 caballero
601 4792 fjp
        /**
602
         * DOCUMENT ME!
603
         */
604
        public void compact() {
605
                expansionFile.compact(relations);
606
        }
607 3996 caballero
608 4792 fjp
        /**
609
         * DOCUMENT ME!
610
         */
611
        public void startComplexRow() {
612
                complex = true;
613
                commands = new CommandCollection();
614
        }
615 3996 caballero
616 4792 fjp
        /**
617
         * DOCUMENT ME!
618 7272 jaume
         *
619 4792 fjp
         * @throws IOException
620
         *             DOCUMENT ME!
621
         * @throws DriverIOException
622
         *             DOCUMENT ME!
623
         */
624 6212 fjp
        public void endComplexRow(String description) throws IOException,
625
                        DriverIOException {
626 6071 caballero
                commands.setDescription(description);
627 4792 fjp
                cr.pushCommand(commands);
628
                complex = false;
629 10377 caballero
                for (int j = 0; j < editionListeners.size(); j++) {
630
                        for (int i = 0; i < fieldEvents.size(); i++) {
631
                                IEditionListener listener = (IEditionListener) editionListeners
632
                                        .get(j);
633
                                listener.afterFieldEditEvent((AfterFieldEditEvent)fieldEvents.get(i));
634
                        }
635
                        for (int i = 0; i < rowEvents.size(); i++) {
636
                                IEditionListener listener = (IEditionListener) editionListeners
637
                                                .get(j);
638
                                listener.afterRowEditEvent(null,(AfterRowEditEvent)rowEvents.get(i));
639
                        }
640 10234 caballero
                }
641 10333 caballero
                fieldEvents.clear();
642
                rowEvents.clear();
643 4792 fjp
        }
644 3940 caballero
645 4792 fjp
        /**
646
         * Actualiza en el mapa de ?ndices, la posici?n en la que estaba la
647
         * geometr?a antes de ser modificada. Se marca como v?lida, en caso de que
648
         * fuera una modificaci?n de una geometr?a que estuviese en el fichero de
649
         * expansi?n antes de ser modificada y se pone el puntero de escritura del
650
         * expansion file a justo despues de la penultima geometr?a
651 7272 jaume
         *
652 4792 fjp
         * @param geometryIndex
653
         *            ?ndice de la geometr?a que se quiere deshacer su modificaci?n
654
         * @param previousExpansionFileIndex
655
         *            ?ndice que ten?a antes la geometr?a en el expansionFile. Si
656
         *            vale -1 quiere decir que es una modificaci?n de una geometr?a
657
         *            original y por tanto no hay que actualizar el mapa de indices
658
         *            sino eliminar su entrada.
659 7272 jaume
         *
660 4792 fjp
         * @throws IOException
661
         * @throws DriverIOException
662
         */
663 6212 fjp
        public void undoModifyRow(int geometryIndex,
664
                        int previousExpansionFileIndex, int sourceType) throws IOException,
665
                        DriverIOException {
666 5197 caballero
667 4792 fjp
                if (previousExpansionFileIndex == -1) {
668 6212 fjp
                        DefaultRowEdited edRow = null;
669 5197 caballero
                        try {
670
                                edRow = new DefaultRowEdited(new DefaultRow(ods
671
                                                .getRow(geometryIndex)),
672 6212 fjp
                                                DefaultRowEdited.STATUS_ORIGINAL, geometryIndex);
673 5197 caballero
                        } catch (DriverException e) {
674
                                e.printStackTrace();
675
                        }
676 6212 fjp
                        boolean cancel = fireBeforeModifyRow(edRow, geometryIndex,
677
                                        sourceType);
678 5197 caballero
                        if (cancel)
679
                                return;
680 4792 fjp
                        // Se elimina de las relaciones y del fichero de expansi?n
681
                        relations.remove(new Integer(geometryIndex));
682
                        expansionFile.deleteLastRow();
683
                } else {
684 6212 fjp
                        boolean cancel = fireBeforeModifyRow(expansionFile
685
                                        .getRow(previousExpansionFileIndex), geometryIndex,
686
                                        sourceType);
687 5197 caballero
                        if (cancel)
688
                                return;
689 4792 fjp
                        // Se actualiza la relaci?n de ?ndices
690
                        relations.put(new Integer(geometryIndex), new Integer(
691
                                        previousExpansionFileIndex));
692
                }
693 10292 caballero
                //fireAfterModifyRow(geometryIndex, sourceType);
694 4792 fjp
        }
695 3940 caballero
696 4792 fjp
        /**
697
         * Elimina una geometria. Si es una geometr?a original de la capa en edici?n
698
         * se marca como eliminada (haya sido modificada o no). Si es una geometr?a
699
         * a?adida posteriormente se invalida en el fichero de expansi?n, para que
700
         * una futura compactaci?n termine con ella.
701 7272 jaume
         *
702 4792 fjp
         * @param index
703
         *            ?ndice de la geometr?a.
704 7272 jaume
         *
705 4792 fjp
         * @throws DriverIOException
706
         * @throws IOException
707
         */
708 6212 fjp
        public IRow doRemoveRow(int index, int sourceType)
709
                        throws DriverIOException, IOException {
710 5184 caballero
                boolean cancel = fireBeforeRemoveRow(index, sourceType);
711 5077 caballero
                if (cancel)
712
                        return null;
713 4792 fjp
                // Llega un calculatedIndex
714
                delRows.set(index, true);
715
                System.err.println("Elimina una Row en la posici?n: " + index);
716
                // TODO: Con tablas no es necesario devolver la anterior feature. Por
717
                // ahora.
718 7272 jaume
                isFullExtentDirty = true;
719 5184 caballero
                fireAfterRemoveRow(index, sourceType);
720 4792 fjp
                return null;
721
        }
722 3940 caballero
723 4792 fjp
        /**
724 9887 fjp
         ** Si se intenta modificar una geometr?a original de la capa en edici?n se
725 4792 fjp
         * a?ade al fichero de expansi?n y se registra la posici?n en la que se
726
         * a?adi?. Si se intenta modificar una geometria que se encuentra en el
727 9887 fjp
         * fichero de expansi?n, ?sta puede estar ah? (en el ExpansionFile
728
         * por haber sido a?adida o modificada. Si ha sido a?adida, entonces hay
729
         * que respetar su estatus para que los writers puedan saber que es
730
         * un registro NUEVO).
731 7272 jaume
         *
732 4792 fjp
         * @param index
733
         *            DOCUMENT ME!
734
         * @param feat
735
         *            DOCUMENT ME!
736 7272 jaume
         *
737 4792 fjp
         * @return DOCUMENT ME!
738 7272 jaume
         *
739 4792 fjp
         * @throws IOException
740
         * @throws DriverIOException
741
         */
742 6212 fjp
        public int doModifyRow(int index, IRow feat, int sourceType)
743
                        throws IOException, DriverIOException {
744
                boolean cancel = fireBeforeModifyRow(feat, index, sourceType);
745 5077 caballero
                if (cancel)
746
                        return -1;
747
748 4792 fjp
                int pos = -1;
749
                Integer integer = new Integer(index);
750 10234 caballero
//                System.err.println("Modifica una Row en la posici?n: " + index);
751 4792 fjp
                // Si la geometr?a no ha sido modificada
752
                if (!relations.containsKey(integer)) {
753 6212 fjp
                        int expansionIndex = expansionFile.addRow(feat,
754
                                        IRowEdited.STATUS_MODIFIED, actualIndexFields);
755 4792 fjp
                        relations.put(integer, new Integer(expansionIndex));
756
                } else {
757
                        // Obtenemos el ?ndice en el fichero de expansi?n
758
                        int num = ((Integer) relations.get(integer)).intValue();
759
                        pos = num;
760 3940 caballero
761 4792 fjp
                        /*
762
                         * Se modifica la geometr?a y nos guardamos el ?ndice dentro del
763
                         * fichero de expansi?n en el que se encuentra la geometr?a
764
                         * modificada
765
                         */
766 6212 fjp
                        num = expansionFile.modifyRow(num, feat, actualIndexFields);
767 3940 caballero
768 4792 fjp
                        /*
769
                         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
770
                         * fichero de expansi?n.
771
                         */
772
                        relations.put(integer, new Integer(num));
773
                }
774 7272 jaume
                isFullExtentDirty = true;
775 10292 caballero
                //fireAfterModifyRow(index, sourceType);
776 4792 fjp
                return pos;
777
        }
778 3940 caballero
779 4792 fjp
        /**
780
         * A?ade una geometria al fichero de expansi?n y guarda la correspondencia
781
         * en la tabla relations.
782 7272 jaume
         *
783 4792 fjp
         * @param feat
784
         *            geometr?a a guardar.
785 7272 jaume
         *
786 4792 fjp
         * @return calculatedIndex
787 7272 jaume
         *
788 4792 fjp
         * @throws DriverIOException
789
         * @throws IOException
790
         */
791 6212 fjp
        public int doAddRow(IRow feat, int sourceType) throws DriverIOException,
792
                        IOException {
793 5184 caballero
                boolean cancel = fireBeforeRowAdded(sourceType);
794 5077 caballero
                if (cancel)
795
                        return -1;
796 4792 fjp
                // A?ade la geometr?a
797
                // int virtualIndex = 0;
798
                int calculatedIndex = -1;
799
800
                try {
801
                        calculatedIndex = (int) ods.getRowCount() + numAdd;
802
                        // int externalIndex = getRowCount();
803
                        // calculatedIndex = getCalculatedIndex(externalIndex);
804
                } catch (DriverException e) {
805
                        throw new DriverIOException(e);
806 4368 caballero
                }
807 4792 fjp
808 6212 fjp
                int pos = expansionFile.addRow(feat, IRowEdited.STATUS_ADDED, actualIndexFields);
809 4792 fjp
                relations.put(new Integer(calculatedIndex), new Integer(pos));
810
                numAdd++;
811
                System.err.println("A?ade una Row en la posici?n: " + calculatedIndex);
812 8765 jjdelcerro
                isFullExtentDirty = true;
813
                fireAfterRowAdded(feat,calculatedIndex, sourceType);
814 4792 fjp
                return calculatedIndex;
815 4061 caballero
        }
816 3996 caballero
817 4792 fjp
        /**
818
         * Se desmarca como invalidada en el fichero de expansion o como eliminada
819
         * en el fichero original
820 7272 jaume
         *
821 4792 fjp
         * @param index
822
         *            DOCUMENT ME!
823 7272 jaume
         *
824 4792 fjp
         * @throws IOException
825
         * @throws DriverIOException
826
         */
827 6212 fjp
        public void undoRemoveRow(int index, int sourceType) throws IOException,
828
                        DriverIOException {
829 5184 caballero
                boolean cancel = fireBeforeRowAdded(sourceType);
830 5077 caballero
                if (cancel)
831
                        return;
832 4792 fjp
                delRows.set(index, false);
833 8765 jjdelcerro
                fireAfterRowAdded(null,index, sourceType);
834 4792 fjp
        }
835 3996 caballero
836 4792 fjp
        /**
837
         * Se elimina del final del fichero de expansi?n poniendo el puntero de
838
         * escritura apuntando al final de la pen?ltima geometr?a. Deber? quitar la
839
         * relaci?n del mapa de relaciones
840 7272 jaume
         *
841 4792 fjp
         * @param index
842
         *            ?ndice de la geometr?a que se a?adi?
843 7272 jaume
         *
844 4792 fjp
         * @throws DriverIOException
845
         * @throws IOException
846
         */
847 6212 fjp
        public void undoAddRow(int calculatedIndex, int sourceType)
848
                        throws DriverIOException, IOException {
849
                boolean cancel = fireBeforeRemoveRow(calculatedIndex, sourceType);
850 5077 caballero
                if (cancel)
851
                        return;
852 4792 fjp
                expansionFile.deleteLastRow();
853
                relations.remove(new Integer(calculatedIndex));
854
                numAdd--;
855 6212 fjp
                fireAfterRemoveRow(calculatedIndex, sourceType);
856 4792 fjp
        }
857 3996 caballero
858 4792 fjp
        /*
859
         * (non-Javadoc)
860 7272 jaume
         *
861 4792 fjp
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
862
         */
863
        public SelectableDataSource getRecordset() throws DriverLoadException {
864
                if (isEditing) {
865
                        if (ds == null) {
866
                                String name = LayerFactory.getDataSourceFactory()
867
                                                .addDataSource((ObjectDriver) editingDriver);
868 3996 caballero
869 4792 fjp
                                try {
870 7272 jaume
871 4792 fjp
                                        ds = new SelectableDataSource(LayerFactory
872
                                                        .getDataSourceFactory().createRandomDataSource(
873 6313 fjp
                                                                        name, DataSourceFactory.MANUAL_OPENING));
874
                                        ds.start();
875 4792 fjp
                                        ds.setSelectionSupport(ods.getSelectionSupport());
876 3940 caballero
877 4792 fjp
                                } catch (NoSuchTableException e) {
878
                                        throw new RuntimeException(e);
879
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
880
                                        throw new RuntimeException(e);
881
                                }
882
                        }
883 3940 caballero
884 4792 fjp
                        return ds;
885
                } else {
886
                        return ods;
887
                }
888
        }
889 10333 caballero
890 9887 fjp
        /**
891
         * Return always the original recordset (even when is editing,
892
         * nor the getRecorset() method)
893 10333 caballero
         *
894 9887 fjp
         * */
895
        public SelectableDataSource getOriginalRecordset(){
896
                return ods;
897
        }
898 3940 caballero
899
900 4792 fjp
        /**
901
         * DOCUMENT ME!
902 7272 jaume
         *
903 4792 fjp
         * @return
904
         */
905
        public FBitSet getSelection() {
906
                /*
907
                 * try { return getRecordset().getSelection(); } catch
908
                 * (DriverLoadException e) { // TODO Auto-generated catch block
909
                 * e.printStackTrace(); } return null;
910
                 */
911
                return getRecordset().getSelection();
912
        }
913 3940 caballero
914 4792 fjp
        public void setSelection(FBitSet selection) {
915
                /*
916
                 * try { getRecordset().setSelection(selection); } catch
917
                 * (DriverLoadException e) { // TODO Auto-generated catch block
918
                 * e.printStackTrace(); }
919
                 */
920
                getRecordset().setSelection(selection);
921
        }
922 3940 caballero
923 4792 fjp
        /**
924
         * DOCUMENT ME!
925 7272 jaume
         *
926 4792 fjp
         * @return DOCUMENT ME!
927
         */
928
        public boolean isEditing() {
929
                return isEditing;
930
        }
931 3996 caballero
932 5986 caballero
        public int getInversedIndex(long rowIndex) {
933 4792 fjp
                int intervalNotDeleted = 0;
934
                int antDeleted = -1;
935
                int idPedido = (int) rowIndex;
936
                int numNotDeleted = 0;
937
                int numBorradosAnt = 0;
938 3996 caballero
939 4792 fjp
                for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
940
                                .nextSetBit(i + 1)) {
941
                        intervalNotDeleted = i - antDeleted - 1;
942
                        numNotDeleted += intervalNotDeleted;
943
                        if (i > idPedido) {
944
                                numNotDeleted = numNotDeleted + (i - idPedido);
945
                                break;
946
                        }
947
                        numBorradosAnt++;
948
                        antDeleted = i;
949
                }
950
                numNotDeleted = idPedido - numBorradosAnt;
951
                // System.out.println("Piden Viejo : "+ rowIndex + " y devuelvo como
952
                // nuevo " + (numNotDeleted));
953
                return numNotDeleted;
954
        }
955 3940 caballero
956 4792 fjp
        /**
957
         * DOCUMENT ME!
958 7272 jaume
         *
959 4792 fjp
         * @param rowIndex
960
         *            DOCUMENT ME!
961 7272 jaume
         *
962 4792 fjp
         * @return DOCUMENT ME!
963
         */
964 4954 caballero
        public int getCalculatedIndex(long rowIndex) {
965 4792 fjp
                int numNotDeleted = 0;
966
                int intervalNotDeleted = 0;
967
                int antDeleted = -1;
968
                int calculatedIndex;
969
                int idPedido = (int) rowIndex;
970
                int numBorradosAnt = 0;
971 3940 caballero
972 4792 fjp
                for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
973
                                .nextSetBit(i + 1)) {
974
                        intervalNotDeleted = i - antDeleted - 1;
975
                        numNotDeleted += intervalNotDeleted;
976
                        if (numNotDeleted > idPedido) {
977
                                break;
978
                        }
979
                        numBorradosAnt++;
980
                        antDeleted = i;
981
                }
982
                calculatedIndex = numBorradosAnt + idPedido;
983
                // System.out.println("Piden Registro : "+ rowIndex + " y devuelvo el "
984
                // + (calculatedIndex));
985
                return calculatedIndex;
986
        }
987 3940 caballero
988 4792 fjp
        /**
989
         * DOCUMENT ME!
990 7272 jaume
         *
991 4792 fjp
         * @author Vicente Caballero Navarro
992
         */
993
        private class myObjectDriver implements ObjectDriver {
994
                /*
995
                 * (non-Javadoc)
996 7272 jaume
                 *
997 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
998
                 */
999
                public int[] getPrimaryKeys() throws DriverException {
1000
                        return ods.getPrimaryKeys();
1001
                        // int[] pk=new int[1];
1002
                        /*
1003
                         * for (int i=0;i<getRowCount();i++){ pk[i]=i; }
1004
                         */
1005
                        // pk[0]=1;
1006
                        // return pk;
1007
                }
1008 3996 caballero
1009 4792 fjp
                /*
1010
                 * (non-Javadoc)
1011 7272 jaume
                 *
1012 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
1013
                 */
1014
                public void write(DataWare dataWare) throws DriverException {
1015
                        DataWare dataWareOrig = ods
1016
                                        .getDataWare(DataSourceFactory.DATA_WARE_DIRECT_MODE);
1017
                        dataWareOrig.commitTrans();
1018
                }
1019 3940 caballero
1020 4792 fjp
                /*
1021
                 * (non-Javadoc)
1022 7272 jaume
                 *
1023 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
1024
                 */
1025
                public void setDataSourceFactory(DataSourceFactory dsf) {
1026
                        ods.setDataSourceFactory(dsf);
1027
                }
1028
1029
                /*
1030
                 * (non-Javadoc)
1031 7272 jaume
                 *
1032 4792 fjp
                 * @see com.hardcode.driverManager.Driver#getName()
1033
                 */
1034
                public String getName() {
1035
                        return ods.getName();
1036
                }
1037
1038
                /*
1039
                 * (non-Javadoc)
1040 7272 jaume
                 *
1041 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
1042
                 *      int)
1043
                 */
1044
                public Value getFieldValue(long rowIndex, int fieldId)
1045
                                throws DriverException {
1046
                        // Si no est? en el fichero de expansi?n
1047 6259 fjp
                        // Integer integer = new Integer(getCalculatedIndex(rowIndex));
1048 4792 fjp
1049 6259 fjp
1050 4792 fjp
                        try {
1051 6259 fjp
                                IRow row = getRow((int)rowIndex);
1052
                                return row.getAttribute(fieldId);
1053
//                                if (!relations.containsKey(integer)) {
1054
//                                        return ods.getFieldValue(rowIndex, fieldId);
1055
//                                } else {
1056
//                                        int num = ((Integer) relations.get(integer)).intValue();
1057
//                                        DefaultRowEdited feat = (DefaultRowEdited) expansionFile
1058
//                                                        .getRow(num);
1059
//
1060
//                                        if (feat == null) {
1061
//                                                return null;
1062
//                                        }
1063
//
1064
//                                        return feat.getAttribute(fieldId);
1065
//                                }
1066
//                        } catch (DriverException e) {
1067
//                                e.printStackTrace();
1068
//                                throw new DriverException(e);
1069
                        } catch (IOException e) {
1070 4792 fjp
                                e.printStackTrace();
1071
                                throw new DriverException(e);
1072 6259 fjp
                        } catch (DriverIOException e) {
1073 4792 fjp
                                e.printStackTrace();
1074
                                throw new DriverException(e);
1075
                        }
1076
1077
                        /**
1078
                         * try { if (!relations.containsKey(integer)) { // Si ha sido
1079
                         * eliminada if (delRows.get(integer.intValue())) { return null; }
1080
                         * else { return ods.getFieldValue(rowIndex, fieldId); }} else { int
1081
                         * num = ((Integer) relations.get(integer)).intValue();
1082
                         * DefaultRowEdited feat = (DefaultRowEdited)
1083
                         * expansionFile.getRow(num); if (feat==null)return null; return
1084
                         * feat.getAttribute(fieldId); }} catch (DriverException e) {
1085
                         * e.printStackTrace(); throw new DriverException(e); } catch
1086
                         * (IOException e) { e.printStackTrace(); throw new
1087
                         * DriverException(e); }
1088
                         */
1089
                }
1090
1091
                /*
1092
                 * (non-Javadoc)
1093 7272 jaume
                 *
1094 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
1095
                 */
1096
                public int getFieldCount() throws DriverException {
1097 6483 fjp
                        return fastAccessFields.size();
1098 4792 fjp
                }
1099
1100
                /*
1101
                 * (non-Javadoc)
1102 7272 jaume
                 *
1103 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
1104
                 */
1105
                public String getFieldName(int fieldId) throws DriverException {
1106 6483 fjp
//                        int i=0;
1107
//                        for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
1108
//                                InternalField fld = (InternalField) iter.next();
1109
//                                if (i == fieldId)
1110 6384 fjp
//                                        return fld.getFieldDesc().getFieldAlias();
1111 6483 fjp
//                                i++;
1112 7272 jaume
//
1113 6483 fjp
//                        }
1114
//                        throw new DriverException("FieldId " + fieldId + " not found ");
1115
                        FieldDescription aux = (FieldDescription) fastAccessFields.get(fieldId);
1116
                        return aux.getFieldAlias();
1117 6259 fjp
                        // return null;
1118
                        // return ods.getFieldName(fieldId);
1119 4792 fjp
                }
1120
1121
                /*
1122
                 * (non-Javadoc)
1123 7272 jaume
                 *
1124 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
1125
                 */
1126
                public long getRowCount() {
1127
                        try {
1128
                                return (int) (ods.getRowCount() + numAdd)
1129
                                                - delRows.cardinality();// -
1130 6212 fjp
                                // expansionFile.getInvalidRows().cardinality();
1131 4792 fjp
                        } catch (DriverException e) {
1132
                                // TODO Auto-generated catch block
1133
                                e.printStackTrace();
1134
                        }
1135
1136
                        return 0;
1137
                }
1138
1139
                /*
1140
                 * (non-Javadoc)
1141 7272 jaume
                 *
1142 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
1143
                 */
1144 6384 fjp
                public int getFieldType(int fieldId) throws DriverException {
1145 6483 fjp
//                        int i=0;
1146
//                        for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
1147
//                                InternalField fld = (InternalField) iter.next();
1148
//                                if (i == fieldId)
1149 6384 fjp
//                                        return fld.getFieldDesc().getFieldType();
1150 6483 fjp
//                                i++;
1151 7272 jaume
//
1152 6483 fjp
//                        }
1153
                        FieldDescription aux = (FieldDescription) fastAccessFields.get(fieldId);
1154
                        return aux.getFieldType();
1155 7272 jaume
1156 6483 fjp
//                        return ods.getFieldType(i);
1157 4792 fjp
                }
1158 4863 fjp
1159 6384 fjp
                public int getFieldWidth(int fieldId) throws DriverException {
1160 6483 fjp
//                        int i=0;
1161 7272 jaume
//                        for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
1162 6483 fjp
//                                InternalField fld = (InternalField) iter.next();
1163
////                                if (fld.getFieldIndex() == i)
1164
////                                        return fld.getFieldDesc().getFieldLength();
1165
//                                if (i == fieldId)
1166 6384 fjp
//                                        return fld.getFieldDesc().getFieldLength();
1167 6483 fjp
//                                i++;
1168 7272 jaume
//
1169 6483 fjp
//                        }
1170
//
1171
//                        return ods.getFieldWidth(i);
1172
                        FieldDescription aux = (FieldDescription) fastAccessFields.get(fieldId);
1173
                        return aux.getFieldLength();
1174 6259 fjp
1175 4863 fjp
                }
1176 6323 fjp
1177
                public void reload() throws IOException, DriverException {
1178
                        ods.reload();
1179 7272 jaume
1180 6323 fjp
                }
1181 4792 fjp
        }
1182
1183
        public CommandRecord getCommandRecord() {
1184
                return cr;
1185
        }
1186 4832 fjp
1187 6212 fjp
        protected void fireAfterRemoveRow(int index, int sourceType) {
1188
                AfterRowEditEvent event = new AfterRowEditEvent(this, index,
1189
                                EditionEvent.CHANGE_TYPE_DELETE, sourceType);
1190 10333 caballero
                if (complex){
1191
                        rowEvents.add(event);
1192 10234 caballero
                        return;
1193
                }
1194 6212 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1195
                        IEditionListener listener = (IEditionListener) editionListeners
1196
                                        .get(i);
1197 6689 caballero
                        listener.afterRowEditEvent(null, event);
1198 4832 fjp
                }
1199 4954 caballero
1200 4832 fjp
        }
1201
1202 6212 fjp
        protected boolean fireBeforeRemoveRow(int index, int sourceType) {
1203 4832 fjp
                Cancel cancel = new Cancel();
1204 6212 fjp
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, index,
1205
                                EditionEvent.CHANGE_TYPE_DELETE, cancel, sourceType);
1206
                for (int i = 0; i < editionListeners.size(); i++) {
1207
                        IEditionListener listener = (IEditionListener) editionListeners
1208
                                        .get(i);
1209
                        listener.beforeRowEditEvent(null, event);
1210 4832 fjp
                        if (cancel.isCanceled())
1211
                                return true;
1212
                }
1213
                return false;
1214
        }
1215
1216 8765 jjdelcerro
        protected void fireAfterRowAdded(IRow feat,int calculatedIndex, int sourceType) {
1217 6212 fjp
                AfterRowEditEvent event = new AfterRowEditEvent(this, calculatedIndex,
1218
                                EditionEvent.CHANGE_TYPE_ADD, sourceType);
1219 10333 caballero
                if (complex){
1220
                        rowEvents.add(event);
1221 10234 caballero
                        return;
1222
                }
1223 6212 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1224
                        IEditionListener listener = (IEditionListener) editionListeners
1225
                                        .get(i);
1226 8765 jjdelcerro
                        listener.afterRowEditEvent(feat, event);
1227 4954 caballero
                }
1228 4832 fjp
        }
1229
1230 6335 fjp
        protected void fireAfterFieldAdded(FieldDescription field) {
1231
                AfterFieldEditEvent event = new AfterFieldEditEvent(this,field,
1232
                                EditionEvent.CHANGE_TYPE_ADD);
1233 10234 caballero
                if (complex) {
1234 10333 caballero
                        fieldEvents.add(event);
1235 10234 caballero
                        return;
1236
                }
1237 6335 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1238
                        IEditionListener listener = (IEditionListener) editionListeners
1239
                                        .get(i);
1240
                        listener.afterFieldEditEvent(event);
1241 10234 caballero
1242 6335 fjp
                }
1243
        }
1244
1245
        protected void fireAfterFieldRemoved(FieldDescription field) {
1246
                AfterFieldEditEvent event = new AfterFieldEditEvent(this,field,
1247
                                EditionEvent.CHANGE_TYPE_DELETE);
1248 10234 caballero
                if (complex) {
1249 10333 caballero
                        fieldEvents.add(event);
1250 10234 caballero
                        return;
1251
                }
1252 6335 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1253
                        IEditionListener listener = (IEditionListener) editionListeners
1254
                                        .get(i);
1255
                        listener.afterFieldEditEvent(event);
1256
                }
1257
        }
1258
1259 6399 fjp
        protected void fireAfterFieldModified(FieldDescription field) {
1260
                AfterFieldEditEvent event = new AfterFieldEditEvent(this,field,
1261
                                EditionEvent.CHANGE_TYPE_MODIFY);
1262 10234 caballero
                if (complex) {
1263 10333 caballero
                        fieldEvents.add(event);
1264 10234 caballero
                        return;
1265
                }
1266 6399 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1267
                        IEditionListener listener = (IEditionListener) editionListeners
1268
                                        .get(i);
1269
                        listener.afterFieldEditEvent(event);
1270
                }
1271
        }
1272 7272 jaume
1273
1274 6212 fjp
        protected boolean fireBeforeRowAdded(int sourceType)
1275
                        throws DriverIOException, IOException {
1276 4832 fjp
                Cancel cancel = new Cancel();
1277 6212 fjp
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, getRowCount(),
1278
                                EditionEvent.CHANGE_TYPE_ADD, cancel, sourceType);
1279
                for (int i = 0; i < editionListeners.size(); i++) {
1280
                        IEditionListener listener = (IEditionListener) editionListeners
1281
                                        .get(i);
1282
                        listener.beforeRowEditEvent(null, event);
1283 4832 fjp
                        if (cancel.isCanceled())
1284
                                return true;
1285
                }
1286
                return false;
1287
        }
1288
1289 6335 fjp
        protected boolean fireBeforeFieldAdded(FieldDescription field)
1290
        throws EditionException {
1291
                Cancel cancel = new Cancel();
1292
                BeforeFieldEditEvent event = new BeforeFieldEditEvent(this, field,
1293
                EditionEvent.CHANGE_TYPE_ADD, cancel);
1294
                for (int i = 0; i < editionListeners.size(); i++) {
1295
                        IEditionListener listener = (IEditionListener) editionListeners
1296
                        .get(i);
1297
                        listener.beforeFieldEditEvent(event);
1298
                        if (cancel.isCanceled())
1299
                                return true;
1300
                }
1301
                return false;
1302
        }
1303
1304
        protected boolean fireBeforeRemoveField(FieldDescription field)
1305
        throws EditionException {
1306
                Cancel cancel = new Cancel();
1307
                BeforeFieldEditEvent event = new BeforeFieldEditEvent(this, field,
1308
                EditionEvent.CHANGE_TYPE_DELETE, cancel);
1309
                for (int i = 0; i < editionListeners.size(); i++) {
1310
                        IEditionListener listener = (IEditionListener) editionListeners
1311
                        .get(i);
1312
                        listener.beforeFieldEditEvent(event);
1313
                        if (cancel.isCanceled())
1314
                                return true;
1315
                }
1316
                return false;
1317
        }
1318
1319 7272 jaume
1320 6212 fjp
        protected boolean fireBeforeModifyRow(IRow feat, int index, int sourceType) {
1321 4832 fjp
                Cancel cancel = new Cancel();
1322 6212 fjp
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, index,
1323
                                EditionEvent.CHANGE_TYPE_MODIFY, cancel, sourceType);
1324
                for (int i = 0; i < editionListeners.size(); i++) {
1325
                        IEditionListener listener = (IEditionListener) editionListeners
1326
                                        .get(i);
1327 5197 caballero
                        listener.beforeRowEditEvent(feat, event);
1328 4832 fjp
                        if (cancel.isCanceled())
1329
                                return true;
1330
                }
1331
                return false;
1332
        }
1333
1334 6212 fjp
        protected void fireAfterModifyRow(int index, int sourceType) {
1335
                AfterRowEditEvent event = new AfterRowEditEvent(this, index,
1336
                                EditionEvent.CHANGE_TYPE_MODIFY, sourceType);
1337 10333 caballero
                if (complex){
1338
                        rowEvents.add(event);
1339
                        return;
1340
                }
1341 6212 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1342
                        IEditionListener listener = (IEditionListener) editionListeners
1343
                                        .get(i);
1344 6689 caballero
                        listener.afterRowEditEvent(null, event);
1345 4954 caballero
                }
1346 4832 fjp
1347
        }
1348
1349 5184 caballero
        protected void fireStartEditionEvent(int sourceType) {
1350 6212 fjp
                EditionEvent ev = new EditionEvent(this, EditionEvent.START_EDITION,
1351
                                sourceType);
1352
                for (int i = 0; i < editionListeners.size(); i++) {
1353
                        IEditionListener listener = (IEditionListener) editionListeners
1354
                                        .get(i);
1355 4832 fjp
                        listener.processEvent(ev);
1356 4954 caballero
                }
1357
1358 4832 fjp
        }
1359
1360 5184 caballero
        protected void fireStopEditionEvent(int sourceType) {
1361 6212 fjp
                EditionEvent ev = new EditionEvent(this, EditionEvent.STOP_EDITION,
1362
                                sourceType);
1363
                for (int i = 0; i < editionListeners.size(); i++) {
1364
                        IEditionListener listener = (IEditionListener) editionListeners
1365
                                        .get(i);
1366 4832 fjp
                        listener.processEvent(ev);
1367 4954 caballero
                }
1368
1369 4832 fjp
        }
1370
1371 5184 caballero
        protected void fireCancelEditionEvent(int sourceType) {
1372 6212 fjp
                EditionEvent ev = new EditionEvent(this, EditionEvent.CANCEL_EDITION,
1373
                                sourceType);
1374
                for (int i = 0; i < editionListeners.size(); i++) {
1375
                        IEditionListener listener = (IEditionListener) editionListeners
1376
                                        .get(i);
1377 4832 fjp
                        listener.processEvent(ev);
1378 4954 caballero
                }
1379
1380 4832 fjp
        }
1381 4954 caballero
1382 6212 fjp
        public void addEditionListener(IEditionListener listener) {
1383 5111 caballero
                if (!editionListeners.contains(listener))
1384
                        editionListeners.add(listener);
1385 4832 fjp
        }
1386
1387 6212 fjp
        public void removeEditionListener(IEditionListener listener) {
1388 4832 fjp
                editionListeners.remove(listener);
1389
        }
1390 4954 caballero
1391 5549 fjp
        public IWriter getWriter() {
1392
                return writer;
1393
        }
1394
1395 6212 fjp
        protected void setWriter(IWriter writer) {
1396 5549 fjp
                this.writer = writer;
1397 5986 caballero
1398 5549 fjp
        }
1399 9887 fjp
        /*
1400
         * azabala: esto funciona para todos los drivers gdbms
1401
         * salvo para MySQL, que necesita que el ITableDefinition
1402
         * contenga el nombre de la tabla (y por tanto requiere
1403
         * DBLayerDefinition-en realidad hace falta DBTableDefinition)
1404
         * TODO REVISAR LA ARQUITECTURA DE ESTO
1405 10333 caballero
         *
1406 9887 fjp
         * */
1407 6212 fjp
        public ITableDefinition getTableDefinition() throws DriverLoadException,
1408
                        DriverException {
1409 9887 fjp
                Driver originalDriver = getOriginalDriver();
1410 10052 fjp
//                if(! (originalDriver instanceof AlphanumericDBDriver)){
1411 9887 fjp
                        TableDefinition tableDef = new TableDefinition();
1412
                        tableDef.setFieldsDesc(getRecordset().getFieldsDescription());
1413
                        tableDef.setName(getRecordset().getSourceInfo().name);
1414
                        return tableDef;
1415 10052 fjp
//                }else{
1416
//                        AlphanumericDBDriver dbDriver = (AlphanumericDBDriver)originalDriver;
1417
//                        return dbDriver.getTableDefinition();
1418 10333 caballero
//
1419 10052 fjp
//                }
1420 10333 caballero
1421
1422 5558 fjp
        }
1423
1424 10234 caballero
        public void validateRow(IRow row,int sourceType) throws EditionException {
1425 6212 fjp
                for (int i = 0; i < rules.size(); i++) {
1426 5886 fjp
                        IRule rule = (IRule) rules.get(i);
1427 10234 caballero
                        boolean bAux = rule.validate(row,sourceType);
1428 6212 fjp
                        if (bAux == false) {
1429
                                EditionException ex = new EditionException(
1430
                                                "NOT follow the rule: " + rule.getDescription());
1431 6002 fjp
                                // TODO: Lanzar una RuleException con datos como el registro
1432
                                // que no cumple, la regla que no lo ha cumplido, etc.
1433 6071 caballero
                                throw ex;
1434 6002 fjp
                        }
1435 5886 fjp
                }
1436
        }
1437
1438
        public ArrayList getRules() {
1439
                return rules;
1440
        }
1441
1442
        public void setRules(ArrayList rules) {
1443
                this.rules = rules;
1444
        }
1445 6212 fjp
1446 6313 fjp
        private void clean() throws IOException, DriverException {
1447 6127 fjp
                expansionFile.close();
1448
                relations.clear();
1449
                numAdd = 0;
1450
                delRows.clear();
1451 6313 fjp
                // TODO: Es muy probable que necesitemos un reload de los datasources, al
1452
                // igual que lo tenemos en las capas. Por ahora, basta con retocar
1453
                // listInternalFields, pero casi seguro que lo correcto ser?a hacer un
1454
                // reload completo.
1455
                initalizeFields(ods);
1456 7272 jaume
1457 6313 fjp
//                listInternalFields.clear();
1458
//                listInternalFields.add(actualFields);
1459 6127 fjp
        }
1460 5886 fjp
1461 6212 fjp
        /*
1462
         * (non-Javadoc)
1463 7272 jaume
         *
1464 6212 fjp
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getFieldManager()
1465
         */
1466
        public IFieldManager getFieldManager() {
1467 6356 fjp
                if (ods.getDriver() instanceof IWriteable)
1468
                {
1469
                        IWriter writer = ((IWriteable)ods.getDriver()).getWriter();
1470
                        if ((writer != null) && (writer instanceof IFieldManager))
1471
                        {
1472 6628 fjp
                                IFieldManager fldManager = (IFieldManager) writer;
1473
                                return fldManager;
1474 6356 fjp
                        }
1475
                }
1476 6212 fjp
                return null;
1477
        }
1478
1479
        /**
1480
         * Tiene en cuenta los campos actuales para formatear una row con ellos. Le
1481
         * pasamos los campos que hab?a en el momento en que se cre? esa row.
1482 7272 jaume
         *
1483 6212 fjp
         * @param edRow
1484
         * @param indexInternalFields
1485
         * @return
1486
         */
1487
        public IRowEdited createExternalRow(IRowEdited edRow,
1488
                        int indexInternalFields) {
1489 7272 jaume
1490 6483 fjp
                // para acelerar
1491
                if (bFieldsHasBeenChanged == false)
1492
                        return edRow;
1493 7272 jaume
1494 6212 fjp
                Value[] att = edRow.getAttributes();
1495
                TreeMap ancientFields = (TreeMap) listInternalFields
1496
                                .get(indexInternalFields);
1497
                Value[] newAtt = new Value[actualFields.size()];
1498
                Collection aux = actualFields.values();
1499
                Iterator it = aux.iterator();
1500
                int i = 0;
1501
                Value val = null;
1502
                while (it.hasNext()) {
1503 6384 fjp
                        // Para cada campo de los actuales, miramos si ya estaba cuando
1504 7272 jaume
                        // el registro estaba guardado.
1505 6384 fjp
                        // Si estaba, cogemos el valor de ese campo en el registro
1506
                        // guardado. Si no estaba, ha sido a?adido despu?s y ponemos
1507
                        // su valor por defecto.
1508
                        // Nota importante: fieldIndex es el ?ndice del campo cuando
1509
                        // se guard?. NO es el ?ndice actual dentro de actualFields.
1510
                        // Se usa SOLO para recuperar el valor de los atributos
1511
                        // antiguos. Por eso no nos preocupamos de mantener actuallizados
1512
                        // el resto de campos cuando se borra o a?ade un nuevo campo.
1513 6212 fjp
                        InternalField fld = (InternalField) it.next();
1514 6483 fjp
                        // System.err.println("fld = " + fld.getFieldDesc().getFieldAlias() +  " id=" + fld.getFieldId());
1515 6212 fjp
                        if (ancientFields.containsKey(fld.getFieldId())) {
1516
                                InternalField ancientField = (InternalField) ancientFields
1517
                                                .get(fld.getFieldId());
1518 6839 fjp
                                val = att[ancientField.getFieldIndex()];
1519
                                // val = att[ancientField.getFieldId().intValue()];
1520 6483 fjp
                                // System.out.println("fld: " + fld.getFieldDesc().getFieldAlias() + " ancient:" + " val" + val);
1521 6212 fjp
                        } else
1522
                                val = fld.getFieldDesc().getDefaultValue();
1523 6227 fjp
                        newAtt[i++] = val;
1524 6212 fjp
                }
1525
                IRowEdited newRow = (IRowEdited) edRow.cloneRow();
1526
                newRow.setAttributes(newAtt);
1527
                return newRow;
1528
        }
1529
1530
        public void removeField(String fieldName) throws EditionException {
1531
1532
                InternalField fld = findFieldByName(fieldName);
1533 6259 fjp
                if (fld == null)
1534
                        throw new EditionException("Field " + fieldName + " not found when removing field");
1535 6212 fjp
                Command command = new RemoveFieldCommand(this, fld);
1536
                if (complex) {
1537
                        commands.add(command);
1538
                } else {
1539
                        cr.pushCommand(command);
1540
                }
1541
                doRemoveField(fld);
1542
1543
        }
1544
1545
        private InternalField findFieldByName(String fieldName) {
1546
                Collection aux = actualFields.values();
1547
                Iterator it = aux.iterator();
1548
                while (it.hasNext()) {
1549
                        InternalField fld = (InternalField) it.next();
1550 6399 fjp
                        if (fld.getFieldDesc().getFieldAlias().compareToIgnoreCase(fieldName) == 0)
1551 6212 fjp
                                return fld;
1552
                }
1553 7272 jaume
1554 6212 fjp
                return null;
1555
        }
1556
1557 6259 fjp
        public void undoRemoveField(InternalField field) throws EditionException {
1558 6384 fjp
                // field.setDeleted(false);
1559
//                field.setFieldIndex(actualFields.size());
1560 6212 fjp
                actualFields.put(field.getFieldId(), field);
1561
                fieldsChanged();
1562 6356 fjp
                fireAfterFieldAdded(field.getFieldDesc());
1563 6212 fjp
        }
1564
1565 6259 fjp
        public void doRemoveField(InternalField field) throws EditionException {
1566 6335 fjp
                boolean cancel = fireBeforeRemoveField(field.getFieldDesc());
1567
                if (cancel) return;
1568 6212 fjp
                actualFields.remove(field.getFieldId());
1569
                fieldsChanged();
1570 6335 fjp
                fireAfterFieldRemoved(field.getFieldDesc());
1571 6212 fjp
        }
1572
1573
        public void renameField(String antName, String newName) throws EditionException {
1574
1575
                InternalField fld = findFieldByName(antName);
1576
                Command command = new RenameFieldCommand(this, fld, newName);
1577
                if (complex) {
1578
                        commands.add(command);
1579
                } else {
1580
                        cr.pushCommand(command);
1581
                }
1582
                doRenameField(fld, newName);
1583
1584
        }
1585 7272 jaume
1586 6399 fjp
        public void undoRenameField(InternalField field, String antName) throws EditionException  {
1587 6621 fjp
                field.getFieldDesc().setFieldAlias(antName);
1588 6399 fjp
                fieldsChanged();
1589
                fireAfterFieldModified(field.getFieldDesc());
1590
1591 6212 fjp
        }
1592
1593 6399 fjp
        public void doRenameField(InternalField field, String newName) throws EditionException  {
1594 6621 fjp
                field.getFieldDesc().setFieldAlias(newName);
1595 6399 fjp
                fieldsChanged();
1596
                fireAfterFieldModified(field.getFieldDesc());
1597
1598 6212 fjp
        }
1599
1600
1601
        public void addField(FieldDescription field) throws EditionException {
1602
1603
                InternalField fld = new InternalField(field, InternalField.ADDED, new Integer(listFields.size()));
1604
                Command command = new AddFieldCommand(this, fld);
1605
                if (complex) {
1606
                        commands.add(command);
1607
                } else {
1608
                        cr.pushCommand(command);
1609
                }
1610
                listFields.add(fld);
1611
                doAddField(fld);
1612
1613
        }
1614
1615 6259 fjp
        public void undoAddField(InternalField field) throws EditionException {
1616 6335 fjp
                boolean cancel = fireBeforeRemoveField(field.getFieldDesc());
1617
                if (cancel)
1618
                        return;
1619
1620 6384 fjp
                // field.setDeleted(true);
1621 6227 fjp
                actualFields.remove(field.getFieldId());
1622 6212 fjp
                fieldsChanged();
1623 6335 fjp
                fireAfterFieldRemoved(field.getFieldDesc());
1624 7272 jaume
1625 6212 fjp
        }
1626
1627 6335 fjp
        public int doAddField(InternalField field) throws EditionException {
1628
                boolean cancel;
1629
                cancel = fireBeforeFieldAdded(field.getFieldDesc());
1630
                if (cancel)
1631
                        return -1;
1632
1633 6384 fjp
                // field.setDeleted(false);
1634
//                field.setFieldIndex(actualFields.size());
1635 6227 fjp
                actualFields.put(field.getFieldId(), field);
1636 6212 fjp
                fieldsChanged();
1637 6335 fjp
                fireAfterFieldAdded(field.getFieldDesc());
1638 6384 fjp
//                return field.getFieldIndex();
1639
                return field.getFieldId().intValue();
1640 6212 fjp
        }
1641 7272 jaume
1642 6365 fjp
        public Driver getOriginalDriver()
1643 6356 fjp
        {
1644
                return ods.getDriver();
1645
        }
1646 7272 jaume
1647 6478 fjp
        /**
1648
         * Use it to be sure the recordset will have the right fields. It forces a new SelectableDataSource
1649
         * to be created next time it is needed
1650
         */
1651
        public void cleanSelectableDatasource() {
1652
                ds = null;
1653
        }
1654
1655 6628 fjp
        public FieldDescription[] getFieldsDescription() {
1656
                return (FieldDescription[]) fastAccessFields.toArray(new FieldDescription[0]);
1657
        }
1658
1659 7272 jaume
1660 6384 fjp
//        private InternalField getInternalFieldByIndex(int fieldId)
1661
//        {
1662
//                for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
1663
//                        InternalField fld = (InternalField) iter.next();
1664
//                        if (fld.getFieldIndex() == fieldId)
1665
//                                return fld;
1666
//                }
1667
//                return null;
1668
//        }
1669 7272 jaume
1670 3940 caballero
}