Statistics
| Revision:

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

History | View | Annotate | Download (47.9 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 11008 jmvivo
                                        /*
872 4792 fjp
                                        ds = new SelectableDataSource(LayerFactory
873
                                                        .getDataSourceFactory().createRandomDataSource(
874 6313 fjp
                                                                        name, DataSourceFactory.MANUAL_OPENING));
875 11008 jmvivo
                                        */
876
                                        // CHEMA: AUTOMATIC DATA SOURCE
877
                                        ds = new SelectableDataSource(LayerFactory
878
                                                        .getDataSourceFactory().createRandomDataSource(
879
                                                                        name, DataSourceFactory.AUTOMATIC_OPENING));
880 6313 fjp
                                        ds.start();
881 4792 fjp
                                        ds.setSelectionSupport(ods.getSelectionSupport());
882 3940 caballero
883 4792 fjp
                                } catch (NoSuchTableException e) {
884
                                        throw new RuntimeException(e);
885
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
886
                                        throw new RuntimeException(e);
887
                                }
888
                        }
889 3940 caballero
890 4792 fjp
                        return ds;
891
                } else {
892
                        return ods;
893
                }
894
        }
895 10333 caballero
896 9887 fjp
        /**
897
         * Return always the original recordset (even when is editing,
898
         * nor the getRecorset() method)
899 10333 caballero
         *
900 9887 fjp
         * */
901
        public SelectableDataSource getOriginalRecordset(){
902
                return ods;
903
        }
904 3940 caballero
905
906 4792 fjp
        /**
907
         * DOCUMENT ME!
908 7272 jaume
         *
909 4792 fjp
         * @return
910
         */
911
        public FBitSet getSelection() {
912
                /*
913
                 * try { return getRecordset().getSelection(); } catch
914
                 * (DriverLoadException e) { // TODO Auto-generated catch block
915
                 * e.printStackTrace(); } return null;
916
                 */
917
                return getRecordset().getSelection();
918
        }
919 3940 caballero
920 4792 fjp
        public void setSelection(FBitSet selection) {
921
                /*
922
                 * try { getRecordset().setSelection(selection); } catch
923
                 * (DriverLoadException e) { // TODO Auto-generated catch block
924
                 * e.printStackTrace(); }
925
                 */
926
                getRecordset().setSelection(selection);
927
        }
928 3940 caballero
929 4792 fjp
        /**
930
         * DOCUMENT ME!
931 7272 jaume
         *
932 4792 fjp
         * @return DOCUMENT ME!
933
         */
934
        public boolean isEditing() {
935
                return isEditing;
936
        }
937 3996 caballero
938 5986 caballero
        public int getInversedIndex(long rowIndex) {
939 4792 fjp
                int intervalNotDeleted = 0;
940
                int antDeleted = -1;
941
                int idPedido = (int) rowIndex;
942
                int numNotDeleted = 0;
943
                int numBorradosAnt = 0;
944 3996 caballero
945 4792 fjp
                for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
946
                                .nextSetBit(i + 1)) {
947
                        intervalNotDeleted = i - antDeleted - 1;
948
                        numNotDeleted += intervalNotDeleted;
949
                        if (i > idPedido) {
950
                                numNotDeleted = numNotDeleted + (i - idPedido);
951
                                break;
952
                        }
953
                        numBorradosAnt++;
954
                        antDeleted = i;
955
                }
956
                numNotDeleted = idPedido - numBorradosAnt;
957
                // System.out.println("Piden Viejo : "+ rowIndex + " y devuelvo como
958
                // nuevo " + (numNotDeleted));
959
                return numNotDeleted;
960
        }
961 3940 caballero
962 4792 fjp
        /**
963
         * DOCUMENT ME!
964 7272 jaume
         *
965 4792 fjp
         * @param rowIndex
966
         *            DOCUMENT ME!
967 7272 jaume
         *
968 4792 fjp
         * @return DOCUMENT ME!
969
         */
970 4954 caballero
        public int getCalculatedIndex(long rowIndex) {
971 4792 fjp
                int numNotDeleted = 0;
972
                int intervalNotDeleted = 0;
973
                int antDeleted = -1;
974
                int calculatedIndex;
975
                int idPedido = (int) rowIndex;
976
                int numBorradosAnt = 0;
977 3940 caballero
978 4792 fjp
                for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
979
                                .nextSetBit(i + 1)) {
980
                        intervalNotDeleted = i - antDeleted - 1;
981
                        numNotDeleted += intervalNotDeleted;
982
                        if (numNotDeleted > idPedido) {
983
                                break;
984
                        }
985
                        numBorradosAnt++;
986
                        antDeleted = i;
987
                }
988
                calculatedIndex = numBorradosAnt + idPedido;
989
                // System.out.println("Piden Registro : "+ rowIndex + " y devuelvo el "
990
                // + (calculatedIndex));
991
                return calculatedIndex;
992
        }
993 3940 caballero
994 4792 fjp
        /**
995
         * DOCUMENT ME!
996 7272 jaume
         *
997 4792 fjp
         * @author Vicente Caballero Navarro
998
         */
999
        private class myObjectDriver implements ObjectDriver {
1000
                /*
1001
                 * (non-Javadoc)
1002 7272 jaume
                 *
1003 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
1004
                 */
1005
                public int[] getPrimaryKeys() throws DriverException {
1006
                        return ods.getPrimaryKeys();
1007
                        // int[] pk=new int[1];
1008
                        /*
1009
                         * for (int i=0;i<getRowCount();i++){ pk[i]=i; }
1010
                         */
1011
                        // pk[0]=1;
1012
                        // return pk;
1013
                }
1014 3996 caballero
1015 4792 fjp
                /*
1016
                 * (non-Javadoc)
1017 7272 jaume
                 *
1018 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
1019
                 */
1020
                public void write(DataWare dataWare) throws DriverException {
1021
                        DataWare dataWareOrig = ods
1022
                                        .getDataWare(DataSourceFactory.DATA_WARE_DIRECT_MODE);
1023
                        dataWareOrig.commitTrans();
1024
                }
1025 3940 caballero
1026 4792 fjp
                /*
1027
                 * (non-Javadoc)
1028 7272 jaume
                 *
1029 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
1030
                 */
1031
                public void setDataSourceFactory(DataSourceFactory dsf) {
1032
                        ods.setDataSourceFactory(dsf);
1033
                }
1034
1035
                /*
1036
                 * (non-Javadoc)
1037 7272 jaume
                 *
1038 4792 fjp
                 * @see com.hardcode.driverManager.Driver#getName()
1039
                 */
1040
                public String getName() {
1041
                        return ods.getName();
1042
                }
1043
1044
                /*
1045
                 * (non-Javadoc)
1046 7272 jaume
                 *
1047 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
1048
                 *      int)
1049
                 */
1050
                public Value getFieldValue(long rowIndex, int fieldId)
1051
                                throws DriverException {
1052
                        // Si no est? en el fichero de expansi?n
1053 6259 fjp
                        // Integer integer = new Integer(getCalculatedIndex(rowIndex));
1054 4792 fjp
1055 6259 fjp
1056 4792 fjp
                        try {
1057 6259 fjp
                                IRow row = getRow((int)rowIndex);
1058
                                return row.getAttribute(fieldId);
1059
//                                if (!relations.containsKey(integer)) {
1060
//                                        return ods.getFieldValue(rowIndex, fieldId);
1061
//                                } else {
1062
//                                        int num = ((Integer) relations.get(integer)).intValue();
1063
//                                        DefaultRowEdited feat = (DefaultRowEdited) expansionFile
1064
//                                                        .getRow(num);
1065
//
1066
//                                        if (feat == null) {
1067
//                                                return null;
1068
//                                        }
1069
//
1070
//                                        return feat.getAttribute(fieldId);
1071
//                                }
1072
//                        } catch (DriverException e) {
1073
//                                e.printStackTrace();
1074
//                                throw new DriverException(e);
1075
                        } catch (IOException e) {
1076 4792 fjp
                                e.printStackTrace();
1077
                                throw new DriverException(e);
1078 6259 fjp
                        } catch (DriverIOException e) {
1079 4792 fjp
                                e.printStackTrace();
1080
                                throw new DriverException(e);
1081
                        }
1082
1083
                        /**
1084
                         * try { if (!relations.containsKey(integer)) { // Si ha sido
1085
                         * eliminada if (delRows.get(integer.intValue())) { return null; }
1086
                         * else { return ods.getFieldValue(rowIndex, fieldId); }} else { int
1087
                         * num = ((Integer) relations.get(integer)).intValue();
1088
                         * DefaultRowEdited feat = (DefaultRowEdited)
1089
                         * expansionFile.getRow(num); if (feat==null)return null; return
1090
                         * feat.getAttribute(fieldId); }} catch (DriverException e) {
1091
                         * e.printStackTrace(); throw new DriverException(e); } catch
1092
                         * (IOException e) { e.printStackTrace(); throw new
1093
                         * DriverException(e); }
1094
                         */
1095
                }
1096
1097
                /*
1098
                 * (non-Javadoc)
1099 7272 jaume
                 *
1100 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
1101
                 */
1102
                public int getFieldCount() throws DriverException {
1103 6483 fjp
                        return fastAccessFields.size();
1104 4792 fjp
                }
1105
1106
                /*
1107
                 * (non-Javadoc)
1108 7272 jaume
                 *
1109 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
1110
                 */
1111
                public String getFieldName(int fieldId) throws DriverException {
1112 6483 fjp
//                        int i=0;
1113
//                        for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
1114
//                                InternalField fld = (InternalField) iter.next();
1115
//                                if (i == fieldId)
1116 6384 fjp
//                                        return fld.getFieldDesc().getFieldAlias();
1117 6483 fjp
//                                i++;
1118 7272 jaume
//
1119 6483 fjp
//                        }
1120
//                        throw new DriverException("FieldId " + fieldId + " not found ");
1121
                        FieldDescription aux = (FieldDescription) fastAccessFields.get(fieldId);
1122
                        return aux.getFieldAlias();
1123 6259 fjp
                        // return null;
1124
                        // return ods.getFieldName(fieldId);
1125 4792 fjp
                }
1126
1127
                /*
1128
                 * (non-Javadoc)
1129 7272 jaume
                 *
1130 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
1131
                 */
1132
                public long getRowCount() {
1133
                        try {
1134
                                return (int) (ods.getRowCount() + numAdd)
1135
                                                - delRows.cardinality();// -
1136 6212 fjp
                                // expansionFile.getInvalidRows().cardinality();
1137 4792 fjp
                        } catch (DriverException e) {
1138
                                // TODO Auto-generated catch block
1139
                                e.printStackTrace();
1140
                        }
1141
1142
                        return 0;
1143
                }
1144
1145
                /*
1146
                 * (non-Javadoc)
1147 7272 jaume
                 *
1148 4792 fjp
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
1149
                 */
1150 6384 fjp
                public int getFieldType(int fieldId) throws DriverException {
1151 6483 fjp
//                        int i=0;
1152
//                        for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
1153
//                                InternalField fld = (InternalField) iter.next();
1154
//                                if (i == fieldId)
1155 6384 fjp
//                                        return fld.getFieldDesc().getFieldType();
1156 6483 fjp
//                                i++;
1157 7272 jaume
//
1158 6483 fjp
//                        }
1159
                        FieldDescription aux = (FieldDescription) fastAccessFields.get(fieldId);
1160
                        return aux.getFieldType();
1161 7272 jaume
1162 6483 fjp
//                        return ods.getFieldType(i);
1163 4792 fjp
                }
1164 4863 fjp
1165 6384 fjp
                public int getFieldWidth(int fieldId) throws DriverException {
1166 6483 fjp
//                        int i=0;
1167 7272 jaume
//                        for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
1168 6483 fjp
//                                InternalField fld = (InternalField) iter.next();
1169
////                                if (fld.getFieldIndex() == i)
1170
////                                        return fld.getFieldDesc().getFieldLength();
1171
//                                if (i == fieldId)
1172 6384 fjp
//                                        return fld.getFieldDesc().getFieldLength();
1173 6483 fjp
//                                i++;
1174 7272 jaume
//
1175 6483 fjp
//                        }
1176
//
1177
//                        return ods.getFieldWidth(i);
1178
                        FieldDescription aux = (FieldDescription) fastAccessFields.get(fieldId);
1179
                        return aux.getFieldLength();
1180 6259 fjp
1181 4863 fjp
                }
1182 6323 fjp
1183
                public void reload() throws IOException, DriverException {
1184
                        ods.reload();
1185 7272 jaume
1186 6323 fjp
                }
1187 4792 fjp
        }
1188
1189
        public CommandRecord getCommandRecord() {
1190
                return cr;
1191
        }
1192 4832 fjp
1193 6212 fjp
        protected void fireAfterRemoveRow(int index, int sourceType) {
1194
                AfterRowEditEvent event = new AfterRowEditEvent(this, index,
1195
                                EditionEvent.CHANGE_TYPE_DELETE, sourceType);
1196 10333 caballero
                if (complex){
1197
                        rowEvents.add(event);
1198 10234 caballero
                        return;
1199
                }
1200 6212 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1201
                        IEditionListener listener = (IEditionListener) editionListeners
1202
                                        .get(i);
1203 6689 caballero
                        listener.afterRowEditEvent(null, event);
1204 4832 fjp
                }
1205 4954 caballero
1206 4832 fjp
        }
1207
1208 6212 fjp
        protected boolean fireBeforeRemoveRow(int index, int sourceType) {
1209 4832 fjp
                Cancel cancel = new Cancel();
1210 6212 fjp
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, index,
1211
                                EditionEvent.CHANGE_TYPE_DELETE, cancel, sourceType);
1212
                for (int i = 0; i < editionListeners.size(); i++) {
1213
                        IEditionListener listener = (IEditionListener) editionListeners
1214
                                        .get(i);
1215
                        listener.beforeRowEditEvent(null, event);
1216 4832 fjp
                        if (cancel.isCanceled())
1217
                                return true;
1218
                }
1219
                return false;
1220
        }
1221
1222 8765 jjdelcerro
        protected void fireAfterRowAdded(IRow feat,int calculatedIndex, int sourceType) {
1223 6212 fjp
                AfterRowEditEvent event = new AfterRowEditEvent(this, calculatedIndex,
1224
                                EditionEvent.CHANGE_TYPE_ADD, sourceType);
1225 10333 caballero
                if (complex){
1226
                        rowEvents.add(event);
1227 10234 caballero
                        return;
1228
                }
1229 6212 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1230
                        IEditionListener listener = (IEditionListener) editionListeners
1231
                                        .get(i);
1232 8765 jjdelcerro
                        listener.afterRowEditEvent(feat, event);
1233 4954 caballero
                }
1234 4832 fjp
        }
1235
1236 6335 fjp
        protected void fireAfterFieldAdded(FieldDescription field) {
1237
                AfterFieldEditEvent event = new AfterFieldEditEvent(this,field,
1238
                                EditionEvent.CHANGE_TYPE_ADD);
1239 10234 caballero
                if (complex) {
1240 10333 caballero
                        fieldEvents.add(event);
1241 10234 caballero
                        return;
1242
                }
1243 6335 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1244
                        IEditionListener listener = (IEditionListener) editionListeners
1245
                                        .get(i);
1246
                        listener.afterFieldEditEvent(event);
1247 10234 caballero
1248 6335 fjp
                }
1249
        }
1250
1251
        protected void fireAfterFieldRemoved(FieldDescription field) {
1252
                AfterFieldEditEvent event = new AfterFieldEditEvent(this,field,
1253
                                EditionEvent.CHANGE_TYPE_DELETE);
1254 10234 caballero
                if (complex) {
1255 10333 caballero
                        fieldEvents.add(event);
1256 10234 caballero
                        return;
1257
                }
1258 6335 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1259
                        IEditionListener listener = (IEditionListener) editionListeners
1260
                                        .get(i);
1261
                        listener.afterFieldEditEvent(event);
1262
                }
1263
        }
1264
1265 6399 fjp
        protected void fireAfterFieldModified(FieldDescription field) {
1266
                AfterFieldEditEvent event = new AfterFieldEditEvent(this,field,
1267
                                EditionEvent.CHANGE_TYPE_MODIFY);
1268 10234 caballero
                if (complex) {
1269 10333 caballero
                        fieldEvents.add(event);
1270 10234 caballero
                        return;
1271
                }
1272 6399 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1273
                        IEditionListener listener = (IEditionListener) editionListeners
1274
                                        .get(i);
1275
                        listener.afterFieldEditEvent(event);
1276
                }
1277
        }
1278 7272 jaume
1279
1280 6212 fjp
        protected boolean fireBeforeRowAdded(int sourceType)
1281
                        throws DriverIOException, IOException {
1282 4832 fjp
                Cancel cancel = new Cancel();
1283 6212 fjp
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, getRowCount(),
1284
                                EditionEvent.CHANGE_TYPE_ADD, cancel, sourceType);
1285
                for (int i = 0; i < editionListeners.size(); i++) {
1286
                        IEditionListener listener = (IEditionListener) editionListeners
1287
                                        .get(i);
1288
                        listener.beforeRowEditEvent(null, event);
1289 4832 fjp
                        if (cancel.isCanceled())
1290
                                return true;
1291
                }
1292
                return false;
1293
        }
1294
1295 6335 fjp
        protected boolean fireBeforeFieldAdded(FieldDescription field)
1296
        throws EditionException {
1297
                Cancel cancel = new Cancel();
1298
                BeforeFieldEditEvent event = new BeforeFieldEditEvent(this, field,
1299
                EditionEvent.CHANGE_TYPE_ADD, cancel);
1300
                for (int i = 0; i < editionListeners.size(); i++) {
1301
                        IEditionListener listener = (IEditionListener) editionListeners
1302
                        .get(i);
1303
                        listener.beforeFieldEditEvent(event);
1304
                        if (cancel.isCanceled())
1305
                                return true;
1306
                }
1307
                return false;
1308
        }
1309
1310
        protected boolean fireBeforeRemoveField(FieldDescription field)
1311
        throws EditionException {
1312
                Cancel cancel = new Cancel();
1313
                BeforeFieldEditEvent event = new BeforeFieldEditEvent(this, field,
1314
                EditionEvent.CHANGE_TYPE_DELETE, cancel);
1315
                for (int i = 0; i < editionListeners.size(); i++) {
1316
                        IEditionListener listener = (IEditionListener) editionListeners
1317
                        .get(i);
1318
                        listener.beforeFieldEditEvent(event);
1319
                        if (cancel.isCanceled())
1320
                                return true;
1321
                }
1322
                return false;
1323
        }
1324
1325 7272 jaume
1326 6212 fjp
        protected boolean fireBeforeModifyRow(IRow feat, int index, int sourceType) {
1327 4832 fjp
                Cancel cancel = new Cancel();
1328 6212 fjp
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, index,
1329
                                EditionEvent.CHANGE_TYPE_MODIFY, cancel, sourceType);
1330
                for (int i = 0; i < editionListeners.size(); i++) {
1331
                        IEditionListener listener = (IEditionListener) editionListeners
1332
                                        .get(i);
1333 5197 caballero
                        listener.beforeRowEditEvent(feat, event);
1334 4832 fjp
                        if (cancel.isCanceled())
1335
                                return true;
1336
                }
1337
                return false;
1338
        }
1339
1340 6212 fjp
        protected void fireAfterModifyRow(int index, int sourceType) {
1341
                AfterRowEditEvent event = new AfterRowEditEvent(this, index,
1342
                                EditionEvent.CHANGE_TYPE_MODIFY, sourceType);
1343 10333 caballero
                if (complex){
1344
                        rowEvents.add(event);
1345
                        return;
1346
                }
1347 6212 fjp
                for (int i = 0; i < editionListeners.size(); i++) {
1348
                        IEditionListener listener = (IEditionListener) editionListeners
1349
                                        .get(i);
1350 6689 caballero
                        listener.afterRowEditEvent(null, event);
1351 4954 caballero
                }
1352 4832 fjp
1353
        }
1354
1355 5184 caballero
        protected void fireStartEditionEvent(int sourceType) {
1356 6212 fjp
                EditionEvent ev = new EditionEvent(this, EditionEvent.START_EDITION,
1357
                                sourceType);
1358
                for (int i = 0; i < editionListeners.size(); i++) {
1359
                        IEditionListener listener = (IEditionListener) editionListeners
1360
                                        .get(i);
1361 4832 fjp
                        listener.processEvent(ev);
1362 4954 caballero
                }
1363
1364 4832 fjp
        }
1365
1366 5184 caballero
        protected void fireStopEditionEvent(int sourceType) {
1367 6212 fjp
                EditionEvent ev = new EditionEvent(this, EditionEvent.STOP_EDITION,
1368
                                sourceType);
1369
                for (int i = 0; i < editionListeners.size(); i++) {
1370
                        IEditionListener listener = (IEditionListener) editionListeners
1371
                                        .get(i);
1372 4832 fjp
                        listener.processEvent(ev);
1373 4954 caballero
                }
1374
1375 4832 fjp
        }
1376
1377 5184 caballero
        protected void fireCancelEditionEvent(int sourceType) {
1378 6212 fjp
                EditionEvent ev = new EditionEvent(this, EditionEvent.CANCEL_EDITION,
1379
                                sourceType);
1380
                for (int i = 0; i < editionListeners.size(); i++) {
1381
                        IEditionListener listener = (IEditionListener) editionListeners
1382
                                        .get(i);
1383 4832 fjp
                        listener.processEvent(ev);
1384 4954 caballero
                }
1385
1386 4832 fjp
        }
1387 4954 caballero
1388 6212 fjp
        public void addEditionListener(IEditionListener listener) {
1389 5111 caballero
                if (!editionListeners.contains(listener))
1390
                        editionListeners.add(listener);
1391 4832 fjp
        }
1392
1393 6212 fjp
        public void removeEditionListener(IEditionListener listener) {
1394 4832 fjp
                editionListeners.remove(listener);
1395
        }
1396 4954 caballero
1397 5549 fjp
        public IWriter getWriter() {
1398
                return writer;
1399
        }
1400
1401 6212 fjp
        protected void setWriter(IWriter writer) {
1402 5549 fjp
                this.writer = writer;
1403 5986 caballero
1404 5549 fjp
        }
1405 9887 fjp
        /*
1406
         * azabala: esto funciona para todos los drivers gdbms
1407
         * salvo para MySQL, que necesita que el ITableDefinition
1408
         * contenga el nombre de la tabla (y por tanto requiere
1409
         * DBLayerDefinition-en realidad hace falta DBTableDefinition)
1410
         * TODO REVISAR LA ARQUITECTURA DE ESTO
1411 10333 caballero
         *
1412 9887 fjp
         * */
1413 6212 fjp
        public ITableDefinition getTableDefinition() throws DriverLoadException,
1414
                        DriverException {
1415 9887 fjp
                Driver originalDriver = getOriginalDriver();
1416 10052 fjp
//                if(! (originalDriver instanceof AlphanumericDBDriver)){
1417 9887 fjp
                        TableDefinition tableDef = new TableDefinition();
1418
                        tableDef.setFieldsDesc(getRecordset().getFieldsDescription());
1419
                        tableDef.setName(getRecordset().getSourceInfo().name);
1420
                        return tableDef;
1421 10052 fjp
//                }else{
1422
//                        AlphanumericDBDriver dbDriver = (AlphanumericDBDriver)originalDriver;
1423
//                        return dbDriver.getTableDefinition();
1424 10333 caballero
//
1425 10052 fjp
//                }
1426 10333 caballero
1427
1428 5558 fjp
        }
1429
1430 10234 caballero
        public void validateRow(IRow row,int sourceType) throws EditionException {
1431 6212 fjp
                for (int i = 0; i < rules.size(); i++) {
1432 5886 fjp
                        IRule rule = (IRule) rules.get(i);
1433 10234 caballero
                        boolean bAux = rule.validate(row,sourceType);
1434 6212 fjp
                        if (bAux == false) {
1435
                                EditionException ex = new EditionException(
1436
                                                "NOT follow the rule: " + rule.getDescription());
1437 6002 fjp
                                // TODO: Lanzar una RuleException con datos como el registro
1438
                                // que no cumple, la regla que no lo ha cumplido, etc.
1439 6071 caballero
                                throw ex;
1440 6002 fjp
                        }
1441 5886 fjp
                }
1442
        }
1443
1444
        public ArrayList getRules() {
1445
                return rules;
1446
        }
1447
1448
        public void setRules(ArrayList rules) {
1449
                this.rules = rules;
1450
        }
1451 6212 fjp
1452 6313 fjp
        private void clean() throws IOException, DriverException {
1453 6127 fjp
                expansionFile.close();
1454
                relations.clear();
1455
                numAdd = 0;
1456
                delRows.clear();
1457 6313 fjp
                // TODO: Es muy probable que necesitemos un reload de los datasources, al
1458
                // igual que lo tenemos en las capas. Por ahora, basta con retocar
1459
                // listInternalFields, pero casi seguro que lo correcto ser?a hacer un
1460
                // reload completo.
1461
                initalizeFields(ods);
1462 7272 jaume
1463 6313 fjp
//                listInternalFields.clear();
1464
//                listInternalFields.add(actualFields);
1465 6127 fjp
        }
1466 5886 fjp
1467 6212 fjp
        /*
1468
         * (non-Javadoc)
1469 7272 jaume
         *
1470 6212 fjp
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getFieldManager()
1471
         */
1472
        public IFieldManager getFieldManager() {
1473 6356 fjp
                if (ods.getDriver() instanceof IWriteable)
1474
                {
1475
                        IWriter writer = ((IWriteable)ods.getDriver()).getWriter();
1476
                        if ((writer != null) && (writer instanceof IFieldManager))
1477
                        {
1478 6628 fjp
                                IFieldManager fldManager = (IFieldManager) writer;
1479
                                return fldManager;
1480 6356 fjp
                        }
1481
                }
1482 6212 fjp
                return null;
1483
        }
1484
1485
        /**
1486
         * Tiene en cuenta los campos actuales para formatear una row con ellos. Le
1487
         * pasamos los campos que hab?a en el momento en que se cre? esa row.
1488 7272 jaume
         *
1489 6212 fjp
         * @param edRow
1490
         * @param indexInternalFields
1491
         * @return
1492
         */
1493
        public IRowEdited createExternalRow(IRowEdited edRow,
1494
                        int indexInternalFields) {
1495 7272 jaume
1496 6483 fjp
                // para acelerar
1497
                if (bFieldsHasBeenChanged == false)
1498
                        return edRow;
1499 7272 jaume
1500 6212 fjp
                Value[] att = edRow.getAttributes();
1501
                TreeMap ancientFields = (TreeMap) listInternalFields
1502
                                .get(indexInternalFields);
1503
                Value[] newAtt = new Value[actualFields.size()];
1504
                Collection aux = actualFields.values();
1505
                Iterator it = aux.iterator();
1506
                int i = 0;
1507
                Value val = null;
1508
                while (it.hasNext()) {
1509 6384 fjp
                        // Para cada campo de los actuales, miramos si ya estaba cuando
1510 7272 jaume
                        // el registro estaba guardado.
1511 6384 fjp
                        // Si estaba, cogemos el valor de ese campo en el registro
1512
                        // guardado. Si no estaba, ha sido a?adido despu?s y ponemos
1513
                        // su valor por defecto.
1514
                        // Nota importante: fieldIndex es el ?ndice del campo cuando
1515
                        // se guard?. NO es el ?ndice actual dentro de actualFields.
1516
                        // Se usa SOLO para recuperar el valor de los atributos
1517
                        // antiguos. Por eso no nos preocupamos de mantener actuallizados
1518
                        // el resto de campos cuando se borra o a?ade un nuevo campo.
1519 6212 fjp
                        InternalField fld = (InternalField) it.next();
1520 6483 fjp
                        // System.err.println("fld = " + fld.getFieldDesc().getFieldAlias() +  " id=" + fld.getFieldId());
1521 6212 fjp
                        if (ancientFields.containsKey(fld.getFieldId())) {
1522
                                InternalField ancientField = (InternalField) ancientFields
1523
                                                .get(fld.getFieldId());
1524 6839 fjp
                                val = att[ancientField.getFieldIndex()];
1525
                                // val = att[ancientField.getFieldId().intValue()];
1526 6483 fjp
                                // System.out.println("fld: " + fld.getFieldDesc().getFieldAlias() + " ancient:" + " val" + val);
1527 6212 fjp
                        } else
1528
                                val = fld.getFieldDesc().getDefaultValue();
1529 6227 fjp
                        newAtt[i++] = val;
1530 6212 fjp
                }
1531
                IRowEdited newRow = (IRowEdited) edRow.cloneRow();
1532
                newRow.setAttributes(newAtt);
1533
                return newRow;
1534
        }
1535
1536
        public void removeField(String fieldName) throws EditionException {
1537
1538
                InternalField fld = findFieldByName(fieldName);
1539 6259 fjp
                if (fld == null)
1540
                        throw new EditionException("Field " + fieldName + " not found when removing field");
1541 6212 fjp
                Command command = new RemoveFieldCommand(this, fld);
1542
                if (complex) {
1543
                        commands.add(command);
1544
                } else {
1545
                        cr.pushCommand(command);
1546
                }
1547
                doRemoveField(fld);
1548
1549
        }
1550
1551
        private InternalField findFieldByName(String fieldName) {
1552
                Collection aux = actualFields.values();
1553
                Iterator it = aux.iterator();
1554
                while (it.hasNext()) {
1555
                        InternalField fld = (InternalField) it.next();
1556 6399 fjp
                        if (fld.getFieldDesc().getFieldAlias().compareToIgnoreCase(fieldName) == 0)
1557 6212 fjp
                                return fld;
1558
                }
1559 7272 jaume
1560 6212 fjp
                return null;
1561
        }
1562
1563 6259 fjp
        public void undoRemoveField(InternalField field) throws EditionException {
1564 6384 fjp
                // field.setDeleted(false);
1565
//                field.setFieldIndex(actualFields.size());
1566 6212 fjp
                actualFields.put(field.getFieldId(), field);
1567
                fieldsChanged();
1568 6356 fjp
                fireAfterFieldAdded(field.getFieldDesc());
1569 6212 fjp
        }
1570
1571 6259 fjp
        public void doRemoveField(InternalField field) throws EditionException {
1572 6335 fjp
                boolean cancel = fireBeforeRemoveField(field.getFieldDesc());
1573
                if (cancel) return;
1574 6212 fjp
                actualFields.remove(field.getFieldId());
1575
                fieldsChanged();
1576 6335 fjp
                fireAfterFieldRemoved(field.getFieldDesc());
1577 6212 fjp
        }
1578
1579
        public void renameField(String antName, String newName) throws EditionException {
1580
1581
                InternalField fld = findFieldByName(antName);
1582
                Command command = new RenameFieldCommand(this, fld, newName);
1583
                if (complex) {
1584
                        commands.add(command);
1585
                } else {
1586
                        cr.pushCommand(command);
1587
                }
1588
                doRenameField(fld, newName);
1589
1590
        }
1591 7272 jaume
1592 6399 fjp
        public void undoRenameField(InternalField field, String antName) throws EditionException  {
1593 6621 fjp
                field.getFieldDesc().setFieldAlias(antName);
1594 6399 fjp
                fieldsChanged();
1595
                fireAfterFieldModified(field.getFieldDesc());
1596
1597 6212 fjp
        }
1598
1599 6399 fjp
        public void doRenameField(InternalField field, String newName) throws EditionException  {
1600 6621 fjp
                field.getFieldDesc().setFieldAlias(newName);
1601 6399 fjp
                fieldsChanged();
1602
                fireAfterFieldModified(field.getFieldDesc());
1603
1604 6212 fjp
        }
1605
1606
1607
        public void addField(FieldDescription field) throws EditionException {
1608
1609
                InternalField fld = new InternalField(field, InternalField.ADDED, new Integer(listFields.size()));
1610
                Command command = new AddFieldCommand(this, fld);
1611
                if (complex) {
1612
                        commands.add(command);
1613
                } else {
1614
                        cr.pushCommand(command);
1615
                }
1616
                listFields.add(fld);
1617
                doAddField(fld);
1618
1619
        }
1620
1621 6259 fjp
        public void undoAddField(InternalField field) throws EditionException {
1622 6335 fjp
                boolean cancel = fireBeforeRemoveField(field.getFieldDesc());
1623
                if (cancel)
1624
                        return;
1625
1626 6384 fjp
                // field.setDeleted(true);
1627 6227 fjp
                actualFields.remove(field.getFieldId());
1628 6212 fjp
                fieldsChanged();
1629 6335 fjp
                fireAfterFieldRemoved(field.getFieldDesc());
1630 7272 jaume
1631 6212 fjp
        }
1632
1633 6335 fjp
        public int doAddField(InternalField field) throws EditionException {
1634
                boolean cancel;
1635
                cancel = fireBeforeFieldAdded(field.getFieldDesc());
1636
                if (cancel)
1637
                        return -1;
1638
1639 6384 fjp
                // field.setDeleted(false);
1640
//                field.setFieldIndex(actualFields.size());
1641 6227 fjp
                actualFields.put(field.getFieldId(), field);
1642 6212 fjp
                fieldsChanged();
1643 6335 fjp
                fireAfterFieldAdded(field.getFieldDesc());
1644 6384 fjp
//                return field.getFieldIndex();
1645
                return field.getFieldId().intValue();
1646 6212 fjp
        }
1647 7272 jaume
1648 6365 fjp
        public Driver getOriginalDriver()
1649 6356 fjp
        {
1650
                return ods.getDriver();
1651
        }
1652 7272 jaume
1653 6478 fjp
        /**
1654
         * Use it to be sure the recordset will have the right fields. It forces a new SelectableDataSource
1655
         * to be created next time it is needed
1656
         */
1657
        public void cleanSelectableDatasource() {
1658
                ds = null;
1659
        }
1660
1661 6628 fjp
        public FieldDescription[] getFieldsDescription() {
1662
                return (FieldDescription[]) fastAccessFields.toArray(new FieldDescription[0]);
1663
        }
1664
1665 7272 jaume
1666 6384 fjp
//        private InternalField getInternalFieldByIndex(int fieldId)
1667
//        {
1668
//                for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
1669
//                        InternalField fld = (InternalField) iter.next();
1670
//                        if (fld.getFieldIndex() == fieldId)
1671
//                                return fld;
1672
//                }
1673
//                return null;
1674
//        }
1675 7272 jaume
1676 3940 caballero
}