Statistics
| Revision:

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

History | View | Annotate | Download (28 KB)

1
package com.iver.cit.gvsig.fmap.edition;
2

    
3
import java.io.IOException;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6

    
7
import com.hardcode.driverManager.Driver;
8
import com.hardcode.driverManager.DriverLoadException;
9
import com.hardcode.gdbms.engine.data.DataSourceFactory;
10
import com.hardcode.gdbms.engine.data.NoSuchTableException;
11
import com.hardcode.gdbms.engine.data.driver.DriverException;
12
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
13
import com.hardcode.gdbms.engine.data.edition.DataWare;
14
import com.hardcode.gdbms.engine.values.Value;
15
import com.iver.cit.gvsig.fmap.MapControl.CancelDraw;
16
import com.iver.cit.gvsig.fmap.core.DefaultRow;
17
import com.iver.cit.gvsig.fmap.core.IRow;
18
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
19
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
20
import com.iver.cit.gvsig.fmap.drivers.TableDefinition;
21
import com.iver.cit.gvsig.fmap.edition.commands.AddRowCommand;
22
import com.iver.cit.gvsig.fmap.edition.commands.Command;
23
import com.iver.cit.gvsig.fmap.edition.commands.CommandCollection;
24
import com.iver.cit.gvsig.fmap.edition.commands.CommandRecord;
25
import com.iver.cit.gvsig.fmap.edition.commands.MemoryCommandRecord;
26
import com.iver.cit.gvsig.fmap.edition.commands.ModifyRowCommand;
27
import com.iver.cit.gvsig.fmap.edition.commands.RemoveRowCommand;
28
import com.iver.cit.gvsig.fmap.layers.FBitSet;
29
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
30
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
31
import com.iver.cit.gvsig.fmap.operations.Cancel;
32

    
33
/**
34
 * DOCUMENT ME!
35
 *
36
 * @author Vicente Caballero Navarro
37
 */
38
public class EditableAdapter implements IEditableSource, IWriteable {
39
        protected boolean isEditing = false;
40

    
41
        private SelectableDataSource ds = null;
42

    
43
        protected FBitSet delRows = new FBitSet();
44

    
45
        private CommandRecord cr;
46
        protected IWriter writer;
47

    
48
        /**
49
         * Flag que indica que hay que tomar las siguientes operaciones como una
50
         * operaci?n at?mica
51
         */
52
        private boolean complex = false;
53

    
54
        private CommandCollection commands = null;
55

    
56
        /*
57
         * Establece una relaci?n entre los ?ndices de las geometr?as en el
58
         * EditableFeatureSource y los ?ndices en el fichero de expansi?n FJP:
59
         * CAMBIO: NECESITAMOS TRABAJAR CON FEATURE Y FEATUREITERATOR PARA IR
60
         * PREPARANDO EL CAMINO, GUARDAMOS EL FEATUREID (STRING) COMO CLAVE, Y COMO
61
         * VALOR, EL INDICE DENTRO DEL FICHERO DE EXPANSION (Integer). Lo de que
62
         * FeatureId sea un String es por compatibilidad con OGC. Seg?n OGC, una
63
         * Feature tiene que tener un Id string En el caso de los randomaccess,
64
         * ser?n el id de registro En los casos de base de datos espaciales, supongo
65
         * que siempre ser? num?rico tambi?n, pero lo tendremos que convertir a
66
         * string. Lo que est? claro es que NO se puede confiar nunca en que sea
67
         * algo correlativo (1, 2, 3, 4, 5, ... => FALSO!!)
68
         */
69
        protected HashMap relations = new HashMap();
70

    
71
        /*
72
         * Fichero en el que se guardan las nuevas geometr?as, producto de adiciones
73
         * o de modificaciones
74
         */
75
        protected ExpansionFile expansionFile;
76

    
77
        protected int numAdd = 0;
78

    
79
        private ObjectDriver editingDriver = new myObjectDriver();
80

    
81
        private SelectableDataSource ods;
82

    
83
        private ArrayList editionListeners = new ArrayList();
84

    
85
        /**
86
         * Crea un nuevo EditableAdapter.
87
         */
88
        public EditableAdapter() {
89
                expansionFile = new MemoryExpansionFile();
90
                cr = new MemoryCommandRecord();
91
        }
92

    
93
        /**
94
         * DOCUMENT ME!
95
         *
96
         * @param ds
97
         *            DOCUMENT ME!
98
         */
99
        public void setOriginalDataSource(SelectableDataSource ds) {
100
                this.ods = ds;
101
        }
102

    
103
        /**
104
         * DOCUMENT ME!
105
         *
106
         * @throws EditionException
107
         *             DOCUMENT ME!
108
         */
109
        public void startEdition(int sourceType) throws EditionException {
110
                isEditing = true;
111
                // Obtenemos el driver y vemos si implementa IWriter.
112
                /* DataWare dataWare = ods.getDataWare(DataSourceFactory.DATA_WARE_DIRECT_MODE);
113
                dataWare.get */
114
                Driver drv = ods.getDriver();
115
                if (drv instanceof IWriteable)
116
                {
117
                        setWriter(((IWriteable) drv).getWriter());
118
                }
119
                
120
                fireStartEditionEvent(sourceType);
121
        }
122

    
123

    
124
        /**
125
         * Se ejecuta preProcess() del IWriter, luego se itera por los registros
126
         * borrados por si el IWriter los quiere borrar (solo ser? necesario cuando
127
         * escribimos sobre la misma tabla) y luego se itera por los nuevos
128
         * registros llamando a process con el registro correcto. (A?adidos,
129
         * modificados). Para finalizar, se ejecuta PostProcess
130
         *
131
         * @param writer
132
         *            IWriter que recibir? las llamadas.
133
         *
134
         * @throws EditionException
135
         *             DOCUMENT ME!
136
         */
137
        public void stopEdition(IWriter writer, int sourceType) throws EditionException {
138

    
139
                writer.preProcess();
140

    
141
                try {
142

    
143
                        // Procesamos primero los borrados.
144
                        // Cuando se genere un tema nuevo, no se les debe hacer caso
145
                        // a estos registros
146

    
147
                        for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
148
                                        .nextSetBit(i + 1)) {
149
                                int calculatedIndex = i;
150
                                Integer integer = new Integer(calculatedIndex);
151
                                // Si no est? en el fichero de expansi?n, es de los originales
152
                                // y hay que borrarlo
153
                                DefaultRowEdited edRow = null;
154
                                if (!relations.containsKey(integer)) {
155
                                        edRow = new DefaultRowEdited(new DefaultRow(ods
156
                                                        .getRow(calculatedIndex)),
157
                                                        DefaultRowEdited.STATUS_DELETED, calculatedIndex);
158
                                        writer.process(edRow);
159
                                } else {
160
                                        int num = ((Integer) relations.get(integer)).intValue();
161

    
162
                                        // return expansionFile.getRow(num);
163
                                        IRowEdited rowFromExpansion = expansionFile.getRow(num);
164
                                        // ?Habr?a que hacer aqu? setID(index + "")?
165
                                        edRow = new DefaultRowEdited(rowFromExpansion
166
                                                        .getLinkedRow().cloneRow(), DefaultRowEdited.STATUS_DELETED
167
                                                        , calculatedIndex);
168
                                        writer.process(edRow);
169
                                }
170

    
171
                        }
172

    
173
                        int rowCount = getRowCount();
174
                        for (int i = 0; i < rowCount; i++) {
175
                                IRowEdited rowEdited = getRow(i);
176

    
177
                                if (rowEdited != null) {
178
                                        writer.process(rowEdited);
179
                                }
180
                        }
181
                } catch (DriverIOException e) {
182
                        e.printStackTrace();
183
                        throw new EditionException(e);
184
                } catch (IOException e) {
185
                        e.printStackTrace();
186
                        throw new EditionException(e);
187
                } catch (DriverException e) {
188
                        e.printStackTrace();
189
                        throw new EditionException(e);
190
                }
191

    
192
                writer.postProcess();
193
                isEditing = false;
194
                fireStopEditionEvent(sourceType);
195
        }
196

    
197
        /**
198
         * DOCUMENT ME!
199
         *
200
         * @throws IOException
201
         *             DOCUMENT ME!
202
         */
203
        public void cancelEdition(int sourceType) throws IOException {
204
                isEditing = false;
205
                expansionFile.close();
206
                fireCancelEditionEvent(sourceType);
207
        }
208

    
209
        /*
210
         * (non-Javadoc)
211
         *
212
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
213
         */
214
        public IRowEdited getRow(int index) throws DriverIOException, IOException {
215
                int calculatedIndex = getCalculatedIndex(index);
216
                Integer integer = new Integer(calculatedIndex);
217
                DefaultRowEdited edRow = null;
218
                // Si no est? en el fichero de expansi?n
219
                if (!relations.containsKey(integer)) {
220
                        try {
221
                                /*
222
                                 * edRow = new DefaultRowEdited(new
223
                                 * DefaultRow(ods.getRow(calculatedIndex), "" + index),
224
                                 * DefaultRowEdited.STATUS_ORIGINAL, index);
225
                                 */
226
                                edRow = new DefaultRowEdited(new DefaultRow(ods
227
                                                .getRow(calculatedIndex)),
228
                                                DefaultRowEdited.STATUS_ORIGINAL, index);
229
                        } catch (DriverException e) {
230
                                throw new DriverIOException(e);
231
                        }
232

    
233
                        return edRow;
234
                } else {
235
                        int num = ((Integer) relations.get(integer)).intValue();
236

    
237
                        // return expansionFile.getRow(num);
238
                        IRowEdited rowFromExpansion = expansionFile.getRow(num);
239
                        // ?Habr?a que hacer aqu? setID(index + "")?
240
                        edRow = new DefaultRowEdited(rowFromExpansion.getLinkedRow()
241
                                        .cloneRow(), rowFromExpansion.getStatus(), index);
242
                        return edRow;
243
                }
244
        }
245

    
246
        /**
247
         * DOCUMENT ME!
248
         *
249
         * @return DOCUMENT ME!
250
         *
251
         * @throws DriverIOException
252
         *             DOCUMENT ME!
253
         * @throws IOException
254
         *             DOCUMENT ME!
255
         */
256
        public int getRowCount() throws DriverIOException, IOException {
257
                try {
258
                        return (int) (ods.getRowCount() + numAdd) - delRows.cardinality();// -
259
                                                                                                                                                                // expansionFile.getInvalidRows().cardinality();
260
                } catch (DriverException e) {
261
                        throw new DriverIOException(e);
262
                }
263

    
264
        }
265

    
266
        /* (non-Javadoc)
267
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#addRow(com.iver.cit.gvsig.fmap.core.IRow, java.lang.String)
268
         */
269
        public int addRow(IRow row, String descrip, int sourceType) throws DriverIOException,
270
                        IOException {
271

    
272
                int calculatedIndex = doAddRow(row, sourceType);
273
                Command command = new AddRowCommand(this, row, calculatedIndex, sourceType);
274
                command.setDescription(descrip);
275
                if (complex) {
276
                        commands.add(command);
277
                } else {
278
                        cr.pushCommand(command);
279
                }
280

    
281
                return calculatedIndex;
282
        }
283

    
284

    
285
        /**
286
         * DOCUMENT ME!
287
         *
288
         * @throws DriverIOException
289
         *             DOCUMENT ME!
290
         * @throws IOException
291
         *             DOCUMENT ME!
292
         */
293
        public void undo() throws DriverIOException, IOException {
294
                // seleccion.clear();
295
                if (moreUndoCommands()) {
296
                        cr.undoCommand();
297
                }
298
        }
299

    
300
        /**
301
         * DOCUMENT ME!
302
         *
303
         * @throws DriverIOException
304
         *             DOCUMENT ME!
305
         * @throws IOException
306
         *             DOCUMENT ME!
307
         */
308
        public void redo() throws DriverIOException, IOException {
309
                // seleccion.clear();
310
                if (moreRedoCommands()) {
311
                        cr.redoCommand();
312
                }
313
        }
314

    
315
        /**
316
         * DOCUMENT ME!
317
         *
318
         * @return DOCUMENT ME!
319
         */
320
        public boolean moreUndoCommands() {
321
                return cr.moreUndoCommands();
322
        }
323

    
324
        /**
325
         * DOCUMENT ME!
326
         *
327
         * @return DOCUMENT ME!
328
         */
329
        public boolean moreRedoCommands() {
330
                return cr.moreRedoCommands();
331
        }
332

    
333
        /*
334
         * (non-Javadoc)
335
         *
336
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#removeRow(int)
337
         */
338
        public void removeRow(int index, String descrip, int sourceType) throws IOException,
339
                        DriverIOException {
340

    
341
                int calculatedIndex = getCalculatedIndex(index);
342
                Command command = new RemoveRowCommand(this, calculatedIndex, sourceType);
343
                command.setDescription(descrip);
344
                if (complex) {
345
                        commands.add(command);
346
                } else {
347
                        cr.pushCommand(command);
348
                }
349
                doRemoveRow(calculatedIndex, sourceType);
350

    
351
        }
352

    
353
        /*
354
         * (non-Javadoc)
355
         *
356
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#modifyRow(int,
357
         *      com.iver.cit.gvsig.fmap.core.IRow)
358
         */
359
        public int modifyRow(int index, IRow row, String descrip, int sourceType)
360
                        throws IOException, DriverIOException {
361

    
362
                int calculatedIndex = getCalculatedIndex(index);
363
                int pos = doModifyRow(calculatedIndex, row, sourceType);
364
                Command command = new ModifyRowCommand(this, calculatedIndex, pos, row,sourceType);
365
                command.setDescription(descrip);
366
                if (complex) {
367
                        commands.add(command);
368
                } else {
369
                        cr.pushCommand(command);
370
                }
371

    
372
                return pos;
373
        }
374

    
375
        /**
376
         * DOCUMENT ME!
377
         */
378
        public void compact() {
379
                expansionFile.compact(relations);
380
        }
381

    
382
        /**
383
         * DOCUMENT ME!
384
         */
385
        public void startComplexRow() {
386
                complex = true;
387
                commands = new CommandCollection();
388
        }
389

    
390
        /**
391
         * DOCUMENT ME!
392
         *
393
         * @throws IOException
394
         *             DOCUMENT ME!
395
         * @throws DriverIOException
396
         *             DOCUMENT ME!
397
         */
398
        public void endComplexRow() throws IOException, DriverIOException {
399
                cr.pushCommand(commands);
400
                complex = false;
401
        }
402

    
403
        /**
404
         * Actualiza en el mapa de ?ndices, la posici?n en la que estaba la
405
         * geometr?a antes de ser modificada. Se marca como v?lida, en caso de que
406
         * fuera una modificaci?n de una geometr?a que estuviese en el fichero de
407
         * expansi?n antes de ser modificada y se pone el puntero de escritura del
408
         * expansion file a justo despues de la penultima geometr?a
409
         *
410
         * @param geometryIndex
411
         *            ?ndice de la geometr?a que se quiere deshacer su modificaci?n
412
         * @param previousExpansionFileIndex
413
         *            ?ndice que ten?a antes la geometr?a en el expansionFile. Si
414
         *            vale -1 quiere decir que es una modificaci?n de una geometr?a
415
         *            original y por tanto no hay que actualizar el mapa de indices
416
         *            sino eliminar su entrada.
417
         *
418
         * @throws IOException
419
         * @throws DriverIOException
420
         */
421
        public void undoModifyRow(int geometryIndex, int previousExpansionFileIndex, int sourceType)
422
                        throws IOException, DriverIOException {
423

    
424
                if (previousExpansionFileIndex == -1) {
425
                        DefaultRowEdited edRow=null;
426
                        try {
427
                                edRow = new DefaultRowEdited(new DefaultRow(ods
428
                                                .getRow(geometryIndex)),
429
                                                DefaultRowEdited.STATUS_ORIGINAL,geometryIndex);
430
                        } catch (DriverException e) {
431
                                e.printStackTrace();
432
                        }
433
                        boolean cancel = fireBeforeModifyRow(edRow,geometryIndex,sourceType);
434
                        if (cancel)
435
                                return;
436
                        // Se elimina de las relaciones y del fichero de expansi?n
437
                        relations.remove(new Integer(geometryIndex));
438
                        expansionFile.deleteLastRow();
439
                } else {
440
                        boolean cancel = fireBeforeModifyRow(expansionFile.getRow(previousExpansionFileIndex),geometryIndex,sourceType);
441
                        if (cancel)
442
                                return;
443
                        // Se actualiza la relaci?n de ?ndices
444
                        relations.put(new Integer(geometryIndex), new Integer(
445
                                        previousExpansionFileIndex));
446
                }
447
                fireAfterModifyRow(geometryIndex,sourceType);
448
        }
449

    
450
        /**
451
         * Elimina una geometria. Si es una geometr?a original de la capa en edici?n
452
         * se marca como eliminada (haya sido modificada o no). Si es una geometr?a
453
         * a?adida posteriormente se invalida en el fichero de expansi?n, para que
454
         * una futura compactaci?n termine con ella.
455
         *
456
         * @param index
457
         *            ?ndice de la geometr?a.
458
         *
459
         * @throws DriverIOException
460
         * @throws IOException
461
         */
462
        public IRow doRemoveRow(int index, int sourceType) throws DriverIOException, IOException {
463
                boolean cancel = fireBeforeRemoveRow(index, sourceType);
464
                if (cancel)
465
                        return null;
466
                // Llega un calculatedIndex
467
                delRows.set(index, true);
468
                System.err.println("Elimina una Row en la posici?n: " + index);
469
                // TODO: Con tablas no es necesario devolver la anterior feature. Por
470
                // ahora.
471
                fireAfterRemoveRow(index, sourceType);
472
                return null;
473
        }
474

    
475
        /**
476
         * Si se intenta modificar una geometr?a original de la capa en edici?n se
477
         * a?ade al fichero de expansi?n y se registra la posici?n en la que se
478
         * a?adi?. Si se intenta modificar una geometria que se encuentra en el
479
         * fichero de expansi?n (por ser nueva o original pero modificada) se invoca
480
         * el m?todo modifyGeometry y se actualiza el ?ndice de la geometria en el
481
         * fichero.
482
         *
483
         * @param index
484
         *            DOCUMENT ME!
485
         * @param feat
486
         *            DOCUMENT ME!
487
         *
488
         * @return DOCUMENT ME!
489
         *
490
         * @throws IOException
491
         * @throws DriverIOException
492
         */
493
        public int doModifyRow(int index, IRow feat, int sourceType) throws IOException,
494
                        DriverIOException {
495
                boolean cancel = fireBeforeModifyRow(feat,index, sourceType);
496
                if (cancel)
497
                        return -1;
498

    
499
                int pos = -1;
500
                Integer integer = new Integer(index);
501
                System.err.println("Modifica una Row en la posici?n: " + index);
502
                // Si la geometr?a no ha sido modificada
503
                if (!relations.containsKey(integer)) {
504
                        int expansionIndex = expansionFile.addRow(feat, IRowEdited.STATUS_MODIFIED);
505
                        relations.put(integer, new Integer(expansionIndex));
506
                } else {
507
                        // Obtenemos el ?ndice en el fichero de expansi?n
508
                        int num = ((Integer) relations.get(integer)).intValue();
509
                        pos = num;
510

    
511
                        /*
512
                         * Se modifica la geometr?a y nos guardamos el ?ndice dentro del
513
                         * fichero de expansi?n en el que se encuentra la geometr?a
514
                         * modificada
515
                         */
516
                        num = expansionFile.modifyRow(num, feat);
517

    
518
                        /*
519
                         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
520
                         * fichero de expansi?n.
521
                         */
522
                        relations.put(integer, new Integer(num));
523
                }
524
                fireAfterModifyRow(index, sourceType);
525
                return pos;
526
        }
527

    
528
        /**
529
         * A?ade una geometria al fichero de expansi?n y guarda la correspondencia
530
         * en la tabla relations.
531
         *
532
         * @param feat
533
         *            geometr?a a guardar.
534
         *
535
         * @return calculatedIndex
536
         *
537
         * @throws DriverIOException
538
         * @throws IOException
539
         */
540
        public int doAddRow(IRow feat,int sourceType) throws DriverIOException, IOException {
541
                boolean cancel = fireBeforeRowAdded(sourceType);
542
                if (cancel)
543
                        return -1;
544
                // A?ade la geometr?a
545
                // int virtualIndex = 0;
546
                int calculatedIndex = -1;
547

    
548
                try {
549
                        calculatedIndex = (int) ods.getRowCount() + numAdd;
550
                        // int externalIndex = getRowCount();
551
                        // calculatedIndex = getCalculatedIndex(externalIndex);
552
                } catch (DriverException e) {
553
                        throw new DriverIOException(e);
554
                }
555

    
556
                int pos = expansionFile.addRow(feat, IRowEdited.STATUS_ADDED);
557
                relations.put(new Integer(calculatedIndex), new Integer(pos));
558
                numAdd++;
559
                System.err.println("A?ade una Row en la posici?n: " + calculatedIndex);
560
                fireAfterRowAdded(calculatedIndex, sourceType);
561
                return calculatedIndex;
562
        }
563

    
564
        /**
565
         * Se desmarca como invalidada en el fichero de expansion o como eliminada
566
         * en el fichero original
567
         *
568
         * @param index
569
         *            DOCUMENT ME!
570
         *
571
         * @throws IOException
572
         * @throws DriverIOException
573
         */
574
        public void undoRemoveRow(int index, int sourceType) throws IOException, DriverIOException {
575
                boolean cancel = fireBeforeRowAdded(sourceType);
576
                if (cancel)
577
                        return;
578
                delRows.set(index, false);
579
                fireAfterRowAdded(index, sourceType);
580
        }
581

    
582
        /**
583
         * Se elimina del final del fichero de expansi?n poniendo el puntero de
584
         * escritura apuntando al final de la pen?ltima geometr?a. Deber? quitar la
585
         * relaci?n del mapa de relaciones
586
         *
587
         * @param index
588
         *            ?ndice de la geometr?a que se a?adi?
589
         *
590
         * @throws DriverIOException
591
         * @throws IOException
592
         */
593
        public void undoAddRow(int calculatedIndex,int sourceType) throws DriverIOException,
594
                        IOException {
595
                boolean cancel = fireBeforeRemoveRow(calculatedIndex,sourceType);
596
                if (cancel)
597
                        return;
598
                expansionFile.deleteLastRow();
599
                relations.remove(new Integer(calculatedIndex));
600
                numAdd--;
601
                fireAfterRemoveRow(calculatedIndex,sourceType);
602
        }
603

    
604
        /*
605
         * (non-Javadoc)
606
         *
607
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
608
         */
609
        public SelectableDataSource getRecordset() throws DriverLoadException {
610
                if (isEditing) {
611
                        if (ds == null) {
612
                                String name = LayerFactory.getDataSourceFactory()
613
                                                .addDataSource((ObjectDriver) editingDriver);
614

    
615
                                try {
616
                                        ds = new SelectableDataSource(LayerFactory
617
                                                        .getDataSourceFactory().createRandomDataSource(
618
                                                                        name, DataSourceFactory.AUTOMATIC_OPENING));
619
                                        ds.setSelectionSupport(ods.getSelectionSupport());
620

    
621
                                } catch (NoSuchTableException e) {
622
                                        throw new RuntimeException(e);
623
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
624
                                        throw new RuntimeException(e);
625
                                }
626
                        }
627

    
628
                        return ds;
629
                } else {
630
                        return ods;
631
                }
632
        }
633

    
634
        /**
635
         * DOCUMENT ME!
636
         *
637
         * @param sds
638
         *            DOCUMENT ME!
639
         */
640
        public void setRecordSet(SelectableDataSource sds) {
641
                this.ods = sds;
642
        }
643

    
644
        /**
645
         * DOCUMENT ME!
646
         *
647
         * @return
648
         */
649
        public FBitSet getSelection() {
650
                /*
651
                 * try { return getRecordset().getSelection(); } catch
652
                 * (DriverLoadException e) { // TODO Auto-generated catch block
653
                 * e.printStackTrace(); } return null;
654
                 */
655
                return getRecordset().getSelection();
656
        }
657

    
658
        public void setSelection(FBitSet selection) {
659
                /*
660
                 * try { getRecordset().setSelection(selection); } catch
661
                 * (DriverLoadException e) { // TODO Auto-generated catch block
662
                 * e.printStackTrace(); }
663
                 */
664
                getRecordset().setSelection(selection);
665
        }
666

    
667
        /**
668
         * DOCUMENT ME!
669
         *
670
         * @return DOCUMENT ME!
671
         */
672
        public boolean isEditing() {
673
                return isEditing;
674
        }
675

    
676
        protected int getInversedIndex(long rowIndex) {
677
                int intervalNotDeleted = 0;
678
                int antDeleted = -1;
679
                int idPedido = (int) rowIndex;
680
                int numNotDeleted = 0;
681
                int numBorradosAnt = 0;
682

    
683
                for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
684
                                .nextSetBit(i + 1)) {
685
                        intervalNotDeleted = i - antDeleted - 1;
686
                        numNotDeleted += intervalNotDeleted;
687
                        if (i > idPedido) {
688
                                numNotDeleted = numNotDeleted + (i - idPedido);
689
                                break;
690
                        }
691
                        numBorradosAnt++;
692
                        antDeleted = i;
693
                }
694
                numNotDeleted = idPedido - numBorradosAnt;
695
                // System.out.println("Piden Viejo : "+ rowIndex + " y devuelvo como
696
                // nuevo " + (numNotDeleted));
697
                return numNotDeleted;
698
        }
699

    
700
        /**
701
         * DOCUMENT ME!
702
         *
703
         * @param rowIndex
704
         *            DOCUMENT ME!
705
         *
706
         * @return DOCUMENT ME!
707
         */
708
        public int getCalculatedIndex(long rowIndex) {
709
                int numNotDeleted = 0;
710
                int intervalNotDeleted = 0;
711
                int antDeleted = -1;
712
                int calculatedIndex;
713
                int idPedido = (int) rowIndex;
714
                int numBorradosAnt = 0;
715

    
716
                for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
717
                                .nextSetBit(i + 1)) {
718
                        intervalNotDeleted = i - antDeleted - 1;
719
                        numNotDeleted += intervalNotDeleted;
720
                        if (numNotDeleted > idPedido) {
721
                                break;
722
                        }
723
                        numBorradosAnt++;
724
                        antDeleted = i;
725
                }
726
                calculatedIndex = numBorradosAnt + idPedido;
727
                // System.out.println("Piden Registro : "+ rowIndex + " y devuelvo el "
728
                // + (calculatedIndex));
729
                return calculatedIndex;
730
        }
731

    
732
        /**
733
         * DOCUMENT ME!
734
         *
735
         * @author Vicente Caballero Navarro
736
         */
737
        private class myObjectDriver implements ObjectDriver {
738
                /*
739
                 * (non-Javadoc)
740
                 *
741
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
742
                 */
743
                public int[] getPrimaryKeys() throws DriverException {
744
                        return ods.getPrimaryKeys();
745
                        // int[] pk=new int[1];
746
                        /*
747
                         * for (int i=0;i<getRowCount();i++){ pk[i]=i; }
748
                         */
749
                        // pk[0]=1;
750
                        // return pk;
751
                }
752

    
753
                /*
754
                 * (non-Javadoc)
755
                 *
756
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
757
                 */
758
                public void write(DataWare dataWare) throws DriverException {
759
                        DataWare dataWareOrig = ods
760
                                        .getDataWare(DataSourceFactory.DATA_WARE_DIRECT_MODE);
761
                        dataWareOrig.commitTrans();
762
                }
763

    
764
                /*
765
                 * (non-Javadoc)
766
                 *
767
                 * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
768
                 */
769
                public void setDataSourceFactory(DataSourceFactory dsf) {
770
                        ods.setDataSourceFactory(dsf);
771
                }
772

    
773
                /*
774
                 * (non-Javadoc)
775
                 *
776
                 * @see com.hardcode.driverManager.Driver#getName()
777
                 */
778
                public String getName() {
779
                        return ods.getName();
780
                }
781

    
782
                /*
783
                 * (non-Javadoc)
784
                 *
785
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
786
                 *      int)
787
                 */
788
                public Value getFieldValue(long rowIndex, int fieldId)
789
                                throws DriverException {
790
                        // Si no est? en el fichero de expansi?n
791
                        Integer integer = new Integer(getCalculatedIndex(rowIndex));
792

    
793
                        try {
794
                                if (!relations.containsKey(integer)) {
795
                                        return ods.getFieldValue(rowIndex, fieldId);
796
                                } else {
797
                                        int num = ((Integer) relations.get(integer)).intValue();
798
                                        DefaultRowEdited feat = (DefaultRowEdited) expansionFile
799
                                                        .getRow(num);
800

    
801
                                        if (feat == null) {
802
                                                return null;
803
                                        }
804

    
805
                                        return feat.getAttribute(fieldId);
806
                                }
807
                        } catch (DriverException e) {
808
                                e.printStackTrace();
809
                                throw new DriverException(e);
810
                        } catch (IOException e) {
811
                                e.printStackTrace();
812
                                throw new DriverException(e);
813
                        }
814

    
815
                        /**
816
                         * try { if (!relations.containsKey(integer)) { // Si ha sido
817
                         * eliminada if (delRows.get(integer.intValue())) { return null; }
818
                         * else { return ods.getFieldValue(rowIndex, fieldId); }} else { int
819
                         * num = ((Integer) relations.get(integer)).intValue();
820
                         * DefaultRowEdited feat = (DefaultRowEdited)
821
                         * expansionFile.getRow(num); if (feat==null)return null; return
822
                         * feat.getAttribute(fieldId); }} catch (DriverException e) {
823
                         * e.printStackTrace(); throw new DriverException(e); } catch
824
                         * (IOException e) { e.printStackTrace(); throw new
825
                         * DriverException(e); }
826
                         */
827
                }
828

    
829
                /*
830
                 * (non-Javadoc)
831
                 *
832
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
833
                 */
834
                public int getFieldCount() throws DriverException {
835
                        // TODO Por ahora, no dejamos que se a?adan campos
836
                        return ods.getFieldCount();
837
                }
838

    
839
                /*
840
                 * (non-Javadoc)
841
                 *
842
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
843
                 */
844
                public String getFieldName(int fieldId) throws DriverException {
845
                        return ods.getFieldName(fieldId);
846
                }
847

    
848
                /*
849
                 * (non-Javadoc)
850
                 *
851
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
852
                 */
853
                public long getRowCount() {
854
                        try {
855
                                return (int) (ods.getRowCount() + numAdd)
856
                                                - delRows.cardinality();// -
857
                                                                                                // expansionFile.getInvalidRows().cardinality();
858
                        } catch (DriverException e) {
859
                                // TODO Auto-generated catch block
860
                                e.printStackTrace();
861
                        }
862

    
863
                        return 0;
864
                }
865

    
866
                /*
867
                 * (non-Javadoc)
868
                 *
869
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
870
                 */
871
                public int getFieldType(int i) throws DriverException {
872
                        return ods.getFieldType(i);
873
                }
874

    
875
                public int getFieldWidth(int i) throws DriverException {
876
                        return ods.getFieldWidth(i);
877
                }
878
        }
879

    
880
        public CommandRecord getCommandRecord() {
881
                return cr;
882
        }
883

    
884
        protected void fireAfterRemoveRow(int index,int sourceType) {
885
                AfterRowEditEvent event = new AfterRowEditEvent(this, index, EditionEvent.CHANGE_TYPE_DELETE, sourceType);
886
                for (int i=0; i < editionListeners.size(); i++)
887
                {
888
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
889
                        listener.afterRowEditEvent(event);
890
                }
891

    
892
        }
893

    
894
        protected boolean fireBeforeRemoveRow(int index,int sourceType) {
895
                Cancel cancel = new Cancel();
896
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, index, EditionEvent.CHANGE_TYPE_DELETE, cancel, sourceType);
897
                for (int i=0; i < editionListeners.size(); i++)
898
                {
899
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
900
                        listener.beforeRowEditEvent(null,event);
901
                        if (cancel.isCanceled())
902
                                return true;
903
                }
904
                return false;
905
        }
906

    
907
        protected void fireAfterRowAdded(int calculatedIndex,int sourceType) {
908
                AfterRowEditEvent event = new AfterRowEditEvent(this, calculatedIndex, EditionEvent.CHANGE_TYPE_ADD, sourceType);
909
                for (int i=0; i < editionListeners.size(); i++)
910
                {
911
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
912
                        listener.afterRowEditEvent(event);
913
                }
914
        }
915

    
916
        protected boolean fireBeforeRowAdded(int sourceType) throws DriverIOException, IOException {
917
                Cancel cancel = new Cancel();
918
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, getRowCount(), EditionEvent.CHANGE_TYPE_ADD, cancel, sourceType);
919
                for (int i=0; i < editionListeners.size(); i++)
920
                {
921
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
922
                        listener.beforeRowEditEvent(null,event);
923
                        if (cancel.isCanceled())
924
                                return true;
925
                }
926
                return false;
927
        }
928

    
929
        protected boolean fireBeforeModifyRow(IRow feat,int index, int sourceType) {
930
                Cancel cancel = new Cancel();
931
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, index, EditionEvent.CHANGE_TYPE_MODIFY, cancel,sourceType);
932
                for (int i=0; i < editionListeners.size(); i++)
933
                {
934
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
935
                        listener.beforeRowEditEvent(feat, event);
936
                        if (cancel.isCanceled())
937
                                return true;
938
                }
939
                return false;
940
        }
941

    
942
        protected void fireAfterModifyRow(int index,int sourceType) {
943
                AfterRowEditEvent event = new AfterRowEditEvent(this, index, EditionEvent.CHANGE_TYPE_MODIFY,sourceType);
944
                for (int i=0; i < editionListeners.size(); i++)
945
                {
946
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
947
                        listener.afterRowEditEvent(event);
948
                }
949

    
950

    
951
        }
952

    
953
        protected void fireStartEditionEvent(int sourceType) {
954
                EditionEvent ev = new EditionEvent(this, EditionEvent.START_EDITION, sourceType);
955
                for (int i=0; i < editionListeners.size(); i++)
956
                {
957
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
958
                        listener.processEvent(ev);
959
                }
960

    
961
        }
962

    
963
        protected void fireStopEditionEvent(int sourceType) {
964
                EditionEvent ev = new EditionEvent(this, EditionEvent.STOP_EDITION, sourceType);
965
                for (int i=0; i < editionListeners.size(); i++)
966
                {
967
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
968
                        listener.processEvent(ev);
969
                }
970

    
971
        }
972

    
973
        protected void fireCancelEditionEvent(int sourceType) {
974
                EditionEvent ev = new EditionEvent(this, EditionEvent.CANCEL_EDITION, sourceType);
975
                for (int i=0; i < editionListeners.size(); i++)
976
                {
977
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
978
                        listener.processEvent(ev);
979
                }
980

    
981
        }
982

    
983

    
984
        public void addEditionListener(IEditionListener listener){
985
                if (!editionListeners.contains(listener))
986
                        editionListeners.add(listener);
987
        }
988

    
989
        public void removeEditionListener(IEditionListener listener)
990
        {
991
                editionListeners.remove(listener);
992
        }
993

    
994
        public IWriter getWriter() {
995
                return writer;
996
        }
997

    
998
        public void setWriter(IWriter writer) {
999
                this.writer = writer;
1000
                
1001
        }
1002

    
1003
        public ITableDefinition getTableDefinition() throws DriverLoadException, DriverException {
1004
                TableDefinition tableDef = new TableDefinition();
1005
                tableDef.setFieldsDesc(getRecordset().getFieldsDescription());
1006
                tableDef.setName(getRecordset().getSourceInfo().name);
1007
                return tableDef;
1008
        }
1009

    
1010
}