Statistics
| Revision:

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

History | View | Annotate | Download (25.7 KB)

1 3940 caballero
package com.iver.cit.gvsig.fmap.edition;
2
3 4061 caballero
import java.io.IOException;
4 4832 fjp
import java.util.ArrayList;
5 4061 caballero
import java.util.HashMap;
6 3940 caballero
7 4061 caballero
import com.hardcode.driverManager.DriverLoadException;
8 3940 caballero
import com.hardcode.gdbms.engine.data.DataSourceFactory;
9
import com.hardcode.gdbms.engine.data.NoSuchTableException;
10
import com.hardcode.gdbms.engine.data.driver.DriverException;
11
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
12
import com.hardcode.gdbms.engine.data.edition.DataWare;
13
import com.hardcode.gdbms.engine.values.Value;
14 4832 fjp
import com.iver.cit.gvsig.fmap.MapControl.CancelDraw;
15 4026 caballero
import com.iver.cit.gvsig.fmap.core.DefaultRow;
16 3940 caballero
import com.iver.cit.gvsig.fmap.core.IRow;
17
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
18
import com.iver.cit.gvsig.fmap.edition.commands.AddRowCommand;
19 4120 caballero
import com.iver.cit.gvsig.fmap.edition.commands.Command;
20 3940 caballero
import com.iver.cit.gvsig.fmap.edition.commands.CommandCollection;
21
import com.iver.cit.gvsig.fmap.edition.commands.CommandRecord;
22
import com.iver.cit.gvsig.fmap.edition.commands.MemoryCommandRecord;
23
import com.iver.cit.gvsig.fmap.edition.commands.ModifyRowCommand;
24
import com.iver.cit.gvsig.fmap.edition.commands.RemoveRowCommand;
25
import com.iver.cit.gvsig.fmap.layers.FBitSet;
26
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
27
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
28 4832 fjp
import com.iver.cit.gvsig.fmap.operations.Cancel;
29 3940 caballero
30
/**
31
 * DOCUMENT ME!
32 4792 fjp
 *
33 3940 caballero
 * @author Vicente Caballero Navarro
34
 */
35
public class EditableAdapter implements IEditableSource {
36 4792 fjp
        protected boolean isEditing = false;
37 3996 caballero
38 4792 fjp
        private SelectableDataSource ds = null;
39 3940 caballero
40 4792 fjp
        protected FBitSet delRows = new FBitSet();
41 3940 caballero
42 4792 fjp
        private CommandRecord cr;
43 3940 caballero
44 4792 fjp
        /**
45
         * Flag que indica que hay que tomar las siguientes operaciones como una
46
         * operaci?n at?mica
47
         */
48
        private boolean complex = false;
49 3940 caballero
50 4792 fjp
        private CommandCollection commands = null;
51 3940 caballero
52 4792 fjp
        /*
53
         * Establece una relaci?n entre los ?ndices de las geometr?as en el
54
         * EditableFeatureSource y los ?ndices en el fichero de expansi?n FJP:
55
         * CAMBIO: NECESITAMOS TRABAJAR CON FEATURE Y FEATUREITERATOR PARA IR
56
         * PREPARANDO EL CAMINO, GUARDAMOS EL FEATUREID (STRING) COMO CLAVE, Y COMO
57
         * VALOR, EL INDICE DENTRO DEL FICHERO DE EXPANSION (Integer). Lo de que
58
         * FeatureId sea un String es por compatibilidad con OGC. Seg?n OGC, una
59
         * Feature tiene que tener un Id string En el caso de los randomaccess,
60
         * ser?n el id de registro En los casos de base de datos espaciales, supongo
61
         * que siempre ser? num?rico tambi?n, pero lo tendremos que convertir a
62
         * string. Lo que est? claro es que NO se puede confiar nunca en que sea
63
         * algo correlativo (1, 2, 3, 4, 5, ... => FALSO!!)
64
         */
65
        protected HashMap relations = new HashMap();
66 3940 caballero
67 4792 fjp
        /*
68
         * Fichero en el que se guardan las nuevas geometr?as, producto de adiciones
69
         * o de modificaciones
70
         */
71
        protected ExpansionFile expansionFile;
72 3940 caballero
73 4792 fjp
        protected int numAdd = 0;
74 3940 caballero
75 4792 fjp
        private ObjectDriver editingDriver = new myObjectDriver();
76 3996 caballero
77 4792 fjp
        private SelectableDataSource ods;
78 4832 fjp
79
        private ArrayList editionListeners = new ArrayList();
80 3940 caballero
81 4792 fjp
        /**
82
         * Crea un nuevo EditableAdapter.
83
         */
84
        public EditableAdapter() {
85
                expansionFile = new MemoryExpansionFile();
86
                cr = new MemoryCommandRecord();
87
        }
88 3940 caballero
89 4792 fjp
        /**
90
         * DOCUMENT ME!
91
         *
92
         * @param ds
93
         *            DOCUMENT ME!
94
         */
95
        public void setOriginalDataSource(SelectableDataSource ds) {
96
                this.ods = ds;
97
        }
98 3940 caballero
99 4792 fjp
        /**
100
         * DOCUMENT ME!
101
         *
102
         * @throws EditionException
103
         *             DOCUMENT ME!
104
         */
105
        public void startEdition() throws EditionException {
106
                isEditing = true;
107 4832 fjp
                fireStartEditionEvent();
108 4792 fjp
        }
109 3996 caballero
110 4832 fjp
111 4792 fjp
        /**
112
         * Se ejecuta preProcess() del IWriter, luego se itera por los registros
113
         * borrados por si el IWriter los quiere borrar (solo ser? necesario cuando
114
         * escribimos sobre la misma tabla) y luego se itera por los nuevos
115
         * registros llamando a process con el registro correcto. (A?adidos,
116
         * modificados). Para finalizar, se ejecuta PostProcess
117
         *
118
         * @param writer
119
         *            IWriter que recibir? las llamadas.
120
         *
121
         * @throws EditionException
122
         *             DOCUMENT ME!
123
         */
124
        public void stopEdition(IWriter writer) throws EditionException {
125 4832 fjp
126 4792 fjp
                writer.preProcess();
127 4368 caballero
128 4792 fjp
                try {
129 3940 caballero
130 4792 fjp
                        // Procesamos primero los borrados.
131
                        // Cuando se genere un tema nuevo, no se les debe hacer caso
132
                        // a estos registros
133 3996 caballero
134 4792 fjp
                        for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
135
                                        .nextSetBit(i + 1)) {
136
                                int calculatedIndex = i;
137
                                Integer integer = new Integer(calculatedIndex);
138
                                // Si no est? en el fichero de expansi?n, es de los originales
139
                                // y hay que borrarlo
140
                                DefaultRowEdited edRow = null;
141
                                if (!relations.containsKey(integer)) {
142
                                        edRow = new DefaultRowEdited(new DefaultRow(ods
143
                                                        .getRow(calculatedIndex)),
144
                                                        DefaultRowEdited.STATUS_DELETED, calculatedIndex);
145
                                        writer.process(edRow);
146
                                } else {
147
                                        int num = ((Integer) relations.get(integer)).intValue();
148 4061 caballero
149 4792 fjp
                                        // return expansionFile.getRow(num);
150
                                        IRowEdited rowFromExpansion = expansionFile.getRow(num);
151
                                        // ?Habr?a que hacer aqu? setID(index + "")?
152
                                        edRow = new DefaultRowEdited(rowFromExpansion
153
                                                        .getLinkedRow().cloneRow(), DefaultRowEdited.STATUS_DELETED
154
                                                        , calculatedIndex);
155
                                        writer.process(edRow);
156
                                }
157 3940 caballero
158 4792 fjp
                        }
159 3940 caballero
160 4792 fjp
                        int rowCount = getRowCount();
161
                        for (int i = 0; i < rowCount; i++) {
162
                                IRowEdited rowEdited = getRow(i);
163 3940 caballero
164 4792 fjp
                                if (rowEdited != null) {
165
                                        writer.process(rowEdited);
166
                                }
167
                        }
168
                } catch (DriverIOException e) {
169
                        e.printStackTrace();
170
                        throw new EditionException(e);
171
                } catch (IOException e) {
172
                        e.printStackTrace();
173
                        throw new EditionException(e);
174
                } catch (DriverException e) {
175
                        e.printStackTrace();
176
                        throw new EditionException(e);
177
                }
178 3940 caballero
179 4792 fjp
                writer.postProcess();
180
                isEditing = false;
181 4832 fjp
                fireStopEditionEvent();
182 4792 fjp
        }
183 3940 caballero
184 4792 fjp
        /**
185
         * DOCUMENT ME!
186
         *
187
         * @throws IOException
188
         *             DOCUMENT ME!
189
         */
190
        public void cancelEdition() throws IOException {
191
                isEditing = false;
192
                expansionFile.close();
193 4832 fjp
                fireCancelEditionEvent();
194 4792 fjp
        }
195 3940 caballero
196 4792 fjp
        /*
197
         * (non-Javadoc)
198
         *
199
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
200
         */
201
        public IRowEdited getRow(int index) throws DriverIOException, IOException {
202
                int calculatedIndex = getCalculatedIndex(index);
203
                Integer integer = new Integer(calculatedIndex);
204
                DefaultRowEdited edRow = null;
205
                // Si no est? en el fichero de expansi?n
206
                if (!relations.containsKey(integer)) {
207
                        try {
208
                                /*
209
                                 * edRow = new DefaultRowEdited(new
210
                                 * DefaultRow(ods.getRow(calculatedIndex), "" + index),
211
                                 * DefaultRowEdited.STATUS_ORIGINAL, index);
212
                                 */
213
                                edRow = new DefaultRowEdited(new DefaultRow(ods
214
                                                .getRow(calculatedIndex)),
215
                                                DefaultRowEdited.STATUS_ORIGINAL, index);
216
                        } catch (DriverException e) {
217
                                throw new DriverIOException(e);
218
                        }
219 3940 caballero
220 4792 fjp
                        return edRow;
221
                } else {
222
                        int num = ((Integer) relations.get(integer)).intValue();
223 3940 caballero
224 4792 fjp
                        // return expansionFile.getRow(num);
225
                        IRowEdited rowFromExpansion = expansionFile.getRow(num);
226
                        // ?Habr?a que hacer aqu? setID(index + "")?
227
                        edRow = new DefaultRowEdited(rowFromExpansion.getLinkedRow()
228
                                        .cloneRow(), rowFromExpansion.getStatus(), index);
229
                        return edRow;
230
                }
231
        }
232 3940 caballero
233 4792 fjp
        /**
234
         * DOCUMENT ME!
235
         *
236
         * @return DOCUMENT ME!
237
         *
238
         * @throws DriverIOException
239
         *             DOCUMENT ME!
240
         * @throws IOException
241
         *             DOCUMENT ME!
242
         */
243
        public int getRowCount() throws DriverIOException, IOException {
244
                try {
245
                        return (int) (ods.getRowCount() + numAdd) - delRows.cardinality();// -
246
                                                                                                                                                                // expansionFile.getInvalidRows().cardinality();
247
                } catch (DriverException e) {
248
                        throw new DriverIOException(e);
249
                }
250
251 4637 caballero
        }
252 3940 caballero
253 4832 fjp
        /* (non-Javadoc)
254
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#addRow(com.iver.cit.gvsig.fmap.core.IRow, java.lang.String)
255 4792 fjp
         */
256
        public int addRow(IRow row, String descrip) throws DriverIOException,
257
                        IOException {
258 4832 fjp
                boolean cancel = fireBeforeRowAdded();
259
                if (cancel)
260
                        return -1;
261 4792 fjp
                int calculatedIndex = doAddRow(row);
262
                Command command = new AddRowCommand(this, row, calculatedIndex);
263
                command.setDescription(descrip);
264
                if (complex) {
265
                        commands.add(command);
266
                } else {
267
                        cr.pushCommand(command);
268
                }
269 4832 fjp
                fireAfterRowAdded(calculatedIndex);
270 4792 fjp
                return calculatedIndex;
271
        }
272 3940 caballero
273 4832 fjp
274 4792 fjp
        /**
275
         * DOCUMENT ME!
276
         *
277
         * @throws DriverIOException
278
         *             DOCUMENT ME!
279
         * @throws IOException
280
         *             DOCUMENT ME!
281
         */
282
        public void undo() throws DriverIOException, IOException {
283
                // seleccion.clear();
284
                if (moreUndoCommands()) {
285
                        cr.undoCommand();
286
                }
287
        }
288 3940 caballero
289 4792 fjp
        /**
290
         * DOCUMENT ME!
291
         *
292
         * @throws DriverIOException
293
         *             DOCUMENT ME!
294
         * @throws IOException
295
         *             DOCUMENT ME!
296
         */
297
        public void redo() throws DriverIOException, IOException {
298
                // seleccion.clear();
299
                if (moreRedoCommands()) {
300
                        cr.redoCommand();
301
                }
302
        }
303 3940 caballero
304 4792 fjp
        /**
305
         * DOCUMENT ME!
306
         *
307
         * @return DOCUMENT ME!
308
         */
309
        public boolean moreUndoCommands() {
310
                return cr.moreUndoCommands();
311
        }
312 3996 caballero
313 4792 fjp
        /**
314
         * DOCUMENT ME!
315
         *
316
         * @return DOCUMENT ME!
317
         */
318
        public boolean moreRedoCommands() {
319
                return cr.moreRedoCommands();
320
        }
321 3940 caballero
322 4792 fjp
        /*
323
         * (non-Javadoc)
324
         *
325
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#removeRow(int)
326
         */
327
        public void removeRow(int index, String descrip) throws IOException,
328
                        DriverIOException {
329 4832 fjp
                boolean cancel = fireBeforeRemoveRow(index);
330
                if (cancel)
331
                        return;
332 4792 fjp
                int calculatedIndex = getCalculatedIndex(index);
333
                Command command = new RemoveRowCommand(this, calculatedIndex);
334
                command.setDescription(descrip);
335
                if (complex) {
336
                        commands.add(command);
337
                } else {
338
                        cr.pushCommand(command);
339
                }
340
                doRemoveRow(calculatedIndex);
341 4832 fjp
                fireAfterRemoveRow(index);
342 4792 fjp
        }
343 3940 caballero
344 4792 fjp
        /*
345
         * (non-Javadoc)
346
         *
347
         * @see com.iver.cit.gvsig.fmap.edition.IEditableSource#modifyRow(int,
348
         *      com.iver.cit.gvsig.fmap.core.IRow)
349
         */
350
        public int modifyRow(int index, IRow row, String descrip)
351
                        throws IOException, DriverIOException {
352 4832 fjp
                boolean cancel = fireBeforeModifyRow(index);
353
                if (cancel)
354
                        return -1;
355
356 4792 fjp
                int calculatedIndex = getCalculatedIndex(index);
357
                int pos = doModifyRow(calculatedIndex, row);
358
                Command command = new ModifyRowCommand(this, calculatedIndex, pos, row);
359
                command.setDescription(descrip);
360
                if (complex) {
361
                        commands.add(command);
362
                } else {
363
                        cr.pushCommand(command);
364
                }
365 4832 fjp
                fireAfterModifyRow(index);
366 4792 fjp
                return pos;
367
        }
368 3940 caballero
369 4792 fjp
        /**
370
         * DOCUMENT ME!
371
         */
372
        public void compact() {
373
                expansionFile.compact(relations);
374
        }
375 3996 caballero
376 4792 fjp
        /**
377
         * DOCUMENT ME!
378
         */
379
        public void startComplexRow() {
380
                complex = true;
381
                commands = new CommandCollection();
382
        }
383 3996 caballero
384 4792 fjp
        /**
385
         * DOCUMENT ME!
386
         *
387
         * @throws IOException
388
         *             DOCUMENT ME!
389
         * @throws DriverIOException
390
         *             DOCUMENT ME!
391
         */
392
        public void endComplexRow() throws IOException, DriverIOException {
393
                cr.pushCommand(commands);
394
                complex = false;
395
        }
396 3940 caballero
397 4792 fjp
        /**
398
         * Actualiza en el mapa de ?ndices, la posici?n en la que estaba la
399
         * geometr?a antes de ser modificada. Se marca como v?lida, en caso de que
400
         * fuera una modificaci?n de una geometr?a que estuviese en el fichero de
401
         * expansi?n antes de ser modificada y se pone el puntero de escritura del
402
         * expansion file a justo despues de la penultima geometr?a
403
         *
404
         * @param geometryIndex
405
         *            ?ndice de la geometr?a que se quiere deshacer su modificaci?n
406
         * @param previousExpansionFileIndex
407
         *            ?ndice que ten?a antes la geometr?a en el expansionFile. Si
408
         *            vale -1 quiere decir que es una modificaci?n de una geometr?a
409
         *            original y por tanto no hay que actualizar el mapa de indices
410
         *            sino eliminar su entrada.
411
         *
412
         * @throws IOException
413
         * @throws DriverIOException
414
         */
415
        public void undoModifyRow(int geometryIndex, int previousExpansionFileIndex)
416
                        throws IOException, DriverIOException {
417
                if (previousExpansionFileIndex == -1) {
418
                        // Se elimina de las relaciones y del fichero de expansi?n
419
                        relations.remove(new Integer(geometryIndex));
420
                        expansionFile.deleteLastRow();
421
                } else {
422
                        // Se actualiza la relaci?n de ?ndices
423
                        relations.put(new Integer(geometryIndex), new Integer(
424
                                        previousExpansionFileIndex));
425
                }
426
        }
427 3940 caballero
428 4792 fjp
        /**
429
         * Elimina una geometria. Si es una geometr?a original de la capa en edici?n
430
         * se marca como eliminada (haya sido modificada o no). Si es una geometr?a
431
         * a?adida posteriormente se invalida en el fichero de expansi?n, para que
432
         * una futura compactaci?n termine con ella.
433
         *
434
         * @param index
435
         *            ?ndice de la geometr?a.
436
         *
437
         * @throws DriverIOException
438
         * @throws IOException
439
         */
440
        public IRow doRemoveRow(int index) throws DriverIOException, IOException {
441
                // Llega un calculatedIndex
442
                delRows.set(index, true);
443
                System.err.println("Elimina una Row en la posici?n: " + index);
444
                // TODO: Con tablas no es necesario devolver la anterior feature. Por
445
                // ahora.
446
                return null;
447
        }
448 3940 caballero
449 4792 fjp
        /**
450
         * Si se intenta modificar una geometr?a original de la capa en edici?n se
451
         * a?ade al fichero de expansi?n y se registra la posici?n en la que se
452
         * a?adi?. Si se intenta modificar una geometria que se encuentra en el
453
         * fichero de expansi?n (por ser nueva o original pero modificada) se invoca
454
         * el m?todo modifyGeometry y se actualiza el ?ndice de la geometria en el
455
         * fichero.
456
         *
457
         * @param index
458
         *            DOCUMENT ME!
459
         * @param feat
460
         *            DOCUMENT ME!
461
         *
462
         * @return DOCUMENT ME!
463
         *
464
         * @throws IOException
465
         * @throws DriverIOException
466
         */
467
        public int doModifyRow(int index, IRow feat) throws IOException,
468
                        DriverIOException {
469
                int pos = -1;
470
                Integer integer = new Integer(index);
471
                System.err.println("Modifica una Row en la posici?n: " + index);
472
                // Si la geometr?a no ha sido modificada
473
                if (!relations.containsKey(integer)) {
474 4799 fjp
                        int expansionIndex = expansionFile.addRow(feat, IRowEdited.STATUS_MODIFIED);
475 4792 fjp
                        relations.put(integer, new Integer(expansionIndex));
476
                } else {
477
                        // Obtenemos el ?ndice en el fichero de expansi?n
478
                        int num = ((Integer) relations.get(integer)).intValue();
479
                        pos = num;
480 3940 caballero
481 4792 fjp
                        /*
482
                         * Se modifica la geometr?a y nos guardamos el ?ndice dentro del
483
                         * fichero de expansi?n en el que se encuentra la geometr?a
484
                         * modificada
485
                         */
486
                        num = expansionFile.modifyRow(num, feat);
487 3940 caballero
488 4792 fjp
                        /*
489
                         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
490
                         * fichero de expansi?n.
491
                         */
492
                        relations.put(integer, new Integer(num));
493
                }
494 3996 caballero
495 4792 fjp
                return pos;
496
        }
497 3940 caballero
498 4792 fjp
        /**
499
         * A?ade una geometria al fichero de expansi?n y guarda la correspondencia
500
         * en la tabla relations.
501
         *
502
         * @param feat
503
         *            geometr?a a guardar.
504
         *
505
         * @return calculatedIndex
506
         *
507
         * @throws DriverIOException
508
         * @throws IOException
509
         */
510
        public int doAddRow(IRow feat) throws DriverIOException, IOException {
511
                // A?ade la geometr?a
512
                // int virtualIndex = 0;
513
                int calculatedIndex = -1;
514
515
                try {
516
                        calculatedIndex = (int) ods.getRowCount() + numAdd;
517
                        // int externalIndex = getRowCount();
518
                        // calculatedIndex = getCalculatedIndex(externalIndex);
519
                } catch (DriverException e) {
520
                        throw new DriverIOException(e);
521 4368 caballero
                }
522 4792 fjp
523 4799 fjp
                int pos = expansionFile.addRow(feat, IRowEdited.STATUS_ADDED);
524 4792 fjp
                relations.put(new Integer(calculatedIndex), new Integer(pos));
525
                numAdd++;
526
                System.err.println("A?ade una Row en la posici?n: " + calculatedIndex);
527
                return calculatedIndex;
528 4061 caballero
        }
529 3996 caballero
530 4792 fjp
        /**
531
         * Se desmarca como invalidada en el fichero de expansion o como eliminada
532
         * en el fichero original
533
         *
534
         * @param index
535
         *            DOCUMENT ME!
536
         *
537
         * @throws IOException
538
         * @throws DriverIOException
539
         */
540
        public void undoRemoveRow(int index) throws IOException, DriverIOException {
541
                delRows.set(index, false);
542
        }
543 3996 caballero
544 4792 fjp
        /**
545
         * Se elimina del final del fichero de expansi?n poniendo el puntero de
546
         * escritura apuntando al final de la pen?ltima geometr?a. Deber? quitar la
547
         * relaci?n del mapa de relaciones
548
         *
549
         * @param index
550
         *            ?ndice de la geometr?a que se a?adi?
551
         *
552
         * @throws DriverIOException
553
         * @throws IOException
554
         */
555
        public void undoAddRow(int calculatedIndex) throws DriverIOException,
556
                        IOException {
557
                expansionFile.deleteLastRow();
558
                relations.remove(new Integer(calculatedIndex));
559
                numAdd--;
560
        }
561 3996 caballero
562 4792 fjp
        /*
563
         * (non-Javadoc)
564
         *
565
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
566
         */
567
        public SelectableDataSource getRecordset() throws DriverLoadException {
568
                if (isEditing) {
569
                        if (ds == null) {
570
                                String name = LayerFactory.getDataSourceFactory()
571
                                                .addDataSource((ObjectDriver) editingDriver);
572 3996 caballero
573 4792 fjp
                                try {
574
                                        ds = new SelectableDataSource(LayerFactory
575
                                                        .getDataSourceFactory().createRandomDataSource(
576
                                                                        name, DataSourceFactory.AUTOMATIC_OPENING));
577
                                        ds.setSelectionSupport(ods.getSelectionSupport());
578 3940 caballero
579 4792 fjp
                                } catch (NoSuchTableException e) {
580
                                        throw new RuntimeException(e);
581
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
582
                                        throw new RuntimeException(e);
583
                                }
584
                        }
585 3940 caballero
586 4792 fjp
                        return ds;
587
                } else {
588
                        return ods;
589
                }
590
        }
591 3940 caballero
592 4792 fjp
        /**
593
         * DOCUMENT ME!
594
         *
595
         * @param sds
596
         *            DOCUMENT ME!
597
         */
598
        public void setRecordSet(SelectableDataSource sds) {
599
                this.ods = sds;
600
        }
601 3940 caballero
602 4792 fjp
        /**
603
         * DOCUMENT ME!
604
         *
605
         * @return
606
         */
607
        public FBitSet getSelection() {
608
                /*
609
                 * try { return getRecordset().getSelection(); } catch
610
                 * (DriverLoadException e) { // TODO Auto-generated catch block
611
                 * e.printStackTrace(); } return null;
612
                 */
613
                return getRecordset().getSelection();
614
        }
615 3940 caballero
616 4792 fjp
        public void setSelection(FBitSet selection) {
617
                /*
618
                 * try { getRecordset().setSelection(selection); } catch
619
                 * (DriverLoadException e) { // TODO Auto-generated catch block
620
                 * e.printStackTrace(); }
621
                 */
622
                getRecordset().setSelection(selection);
623
        }
624 3940 caballero
625 4792 fjp
        /**
626
         * DOCUMENT ME!
627
         *
628
         * @return DOCUMENT ME!
629
         */
630
        public boolean isEditing() {
631
                return isEditing;
632
        }
633 3996 caballero
634 4792 fjp
        protected int getInversedIndex(long rowIndex) {
635
                int intervalNotDeleted = 0;
636
                int antDeleted = -1;
637
                int idPedido = (int) rowIndex;
638
                int numNotDeleted = 0;
639
                int numBorradosAnt = 0;
640 3996 caballero
641 4792 fjp
                for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
642
                                .nextSetBit(i + 1)) {
643
                        intervalNotDeleted = i - antDeleted - 1;
644
                        numNotDeleted += intervalNotDeleted;
645
                        if (i > idPedido) {
646
                                numNotDeleted = numNotDeleted + (i - idPedido);
647
                                break;
648
                        }
649
                        numBorradosAnt++;
650
                        antDeleted = i;
651
                }
652
                numNotDeleted = idPedido - numBorradosAnt;
653
                // System.out.println("Piden Viejo : "+ rowIndex + " y devuelvo como
654
                // nuevo " + (numNotDeleted));
655
                return numNotDeleted;
656
        }
657 3940 caballero
658 4792 fjp
        /**
659
         * DOCUMENT ME!
660
         *
661
         * @param rowIndex
662
         *            DOCUMENT ME!
663
         *
664
         * @return DOCUMENT ME!
665
         */
666
        protected int getCalculatedIndex(long rowIndex) {
667
                int numNotDeleted = 0;
668
                int intervalNotDeleted = 0;
669
                int antDeleted = -1;
670
                int calculatedIndex;
671
                int idPedido = (int) rowIndex;
672
                int numBorradosAnt = 0;
673 3940 caballero
674 4792 fjp
                for (int i = delRows.nextSetBit(0); i >= 0; i = delRows
675
                                .nextSetBit(i + 1)) {
676
                        intervalNotDeleted = i - antDeleted - 1;
677
                        numNotDeleted += intervalNotDeleted;
678
                        if (numNotDeleted > idPedido) {
679
                                break;
680
                        }
681
                        numBorradosAnt++;
682
                        antDeleted = i;
683
                }
684
                calculatedIndex = numBorradosAnt + idPedido;
685
                // System.out.println("Piden Registro : "+ rowIndex + " y devuelvo el "
686
                // + (calculatedIndex));
687
                return calculatedIndex;
688
        }
689 3940 caballero
690 4792 fjp
        /**
691
         * DOCUMENT ME!
692
         *
693
         * @author Vicente Caballero Navarro
694
         */
695
        private class myObjectDriver implements ObjectDriver {
696
                /*
697
                 * (non-Javadoc)
698
                 *
699
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
700
                 */
701
                public int[] getPrimaryKeys() throws DriverException {
702
                        return ods.getPrimaryKeys();
703
                        // int[] pk=new int[1];
704
                        /*
705
                         * for (int i=0;i<getRowCount();i++){ pk[i]=i; }
706
                         */
707
                        // pk[0]=1;
708
                        // return pk;
709
                }
710 3996 caballero
711 4792 fjp
                /*
712
                 * (non-Javadoc)
713
                 *
714
                 * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
715
                 */
716
                public void write(DataWare dataWare) throws DriverException {
717
                        DataWare dataWareOrig = ods
718
                                        .getDataWare(DataSourceFactory.DATA_WARE_DIRECT_MODE);
719
                        dataWareOrig.commitTrans();
720
                }
721 3940 caballero
722 4792 fjp
                /*
723
                 * (non-Javadoc)
724
                 *
725
                 * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
726
                 */
727
                public void setDataSourceFactory(DataSourceFactory dsf) {
728
                        ods.setDataSourceFactory(dsf);
729
                }
730
731
                /*
732
                 * (non-Javadoc)
733
                 *
734
                 * @see com.hardcode.driverManager.Driver#getName()
735
                 */
736
                public String getName() {
737
                        return ods.getName();
738
                }
739
740
                /*
741
                 * (non-Javadoc)
742
                 *
743
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
744
                 *      int)
745
                 */
746
                public Value getFieldValue(long rowIndex, int fieldId)
747
                                throws DriverException {
748
                        // Si no est? en el fichero de expansi?n
749
                        Integer integer = new Integer(getCalculatedIndex(rowIndex));
750
751
                        try {
752
                                if (!relations.containsKey(integer)) {
753
                                        return ods.getFieldValue(rowIndex, fieldId);
754
                                } else {
755
                                        int num = ((Integer) relations.get(integer)).intValue();
756
                                        DefaultRowEdited feat = (DefaultRowEdited) expansionFile
757
                                                        .getRow(num);
758
759
                                        if (feat == null) {
760
                                                return null;
761
                                        }
762
763
                                        return feat.getAttribute(fieldId);
764
                                }
765
                        } catch (DriverException e) {
766
                                e.printStackTrace();
767
                                throw new DriverException(e);
768
                        } catch (IOException e) {
769
                                e.printStackTrace();
770
                                throw new DriverException(e);
771
                        }
772
773
                        /**
774
                         * try { if (!relations.containsKey(integer)) { // Si ha sido
775
                         * eliminada if (delRows.get(integer.intValue())) { return null; }
776
                         * else { return ods.getFieldValue(rowIndex, fieldId); }} else { int
777
                         * num = ((Integer) relations.get(integer)).intValue();
778
                         * DefaultRowEdited feat = (DefaultRowEdited)
779
                         * expansionFile.getRow(num); if (feat==null)return null; return
780
                         * feat.getAttribute(fieldId); }} catch (DriverException e) {
781
                         * e.printStackTrace(); throw new DriverException(e); } catch
782
                         * (IOException e) { e.printStackTrace(); throw new
783
                         * DriverException(e); }
784
                         */
785
                }
786
787
                /*
788
                 * (non-Javadoc)
789
                 *
790
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
791
                 */
792
                public int getFieldCount() throws DriverException {
793
                        // TODO Por ahora, no dejamos que se a?adan campos
794
                        return ods.getFieldCount();
795
                }
796
797
                /*
798
                 * (non-Javadoc)
799
                 *
800
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
801
                 */
802
                public String getFieldName(int fieldId) throws DriverException {
803
                        return ods.getFieldName(fieldId);
804
                }
805
806
                /*
807
                 * (non-Javadoc)
808
                 *
809
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
810
                 */
811
                public long getRowCount() {
812
                        try {
813
                                return (int) (ods.getRowCount() + numAdd)
814
                                                - delRows.cardinality();// -
815
                                                                                                // expansionFile.getInvalidRows().cardinality();
816
                        } catch (DriverException e) {
817
                                // TODO Auto-generated catch block
818
                                e.printStackTrace();
819
                        }
820
821
                        return 0;
822
                }
823
824
                /*
825
                 * (non-Javadoc)
826
                 *
827
                 * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
828
                 */
829
                public int getFieldType(int i) throws DriverException {
830
                        return ods.getFieldType(i);
831
                }
832
        }
833
834
        public CommandRecord getCommandRecord() {
835
                return cr;
836
        }
837 4832 fjp
838
        protected void fireAfterRemoveRow(int index) {
839
                AfterRowEditEvent event = new AfterRowEditEvent(this, index, EditionEvent.CHANGE_TYPE_DELETE);
840
                for (int i=0; i < editionListeners.size(); i++)
841
                {
842
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
843
                        listener.afterRowEditEvent(event);
844
                }
845
846
        }
847
848
        protected boolean fireBeforeRemoveRow(int index) {
849
                Cancel cancel = new Cancel();
850
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, index, EditionEvent.CHANGE_TYPE_DELETE, cancel);
851
                for (int i=0; i < editionListeners.size(); i++)
852
                {
853
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
854
                        listener.beforeRowEditEvent(event);
855
                        if (cancel.isCanceled())
856
                                return true;
857
                }
858
                return false;
859
        }
860
861
        protected void fireAfterRowAdded(int calculatedIndex) {
862
                AfterRowEditEvent event = new AfterRowEditEvent(this, calculatedIndex, EditionEvent.CHANGE_TYPE_ADD);
863
                for (int i=0; i < editionListeners.size(); i++)
864
                {
865
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
866
                        listener.afterRowEditEvent(event);
867
                }
868
        }
869
870
        protected boolean fireBeforeRowAdded() throws DriverIOException, IOException {
871
                Cancel cancel = new Cancel();
872
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, getRowCount(), EditionEvent.CHANGE_TYPE_ADD, cancel);
873
                for (int i=0; i < editionListeners.size(); i++)
874
                {
875
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
876
                        listener.beforeRowEditEvent(event);
877
                        if (cancel.isCanceled())
878
                                return true;
879
                }
880
                return false;
881
        }
882
883
        protected boolean fireBeforeModifyRow(int index) {
884
                Cancel cancel = new Cancel();
885
                BeforeRowEditEvent event = new BeforeRowEditEvent(this, index, EditionEvent.CHANGE_TYPE_MODIFY, cancel);
886
                for (int i=0; i < editionListeners.size(); i++)
887
                {
888
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
889
                        listener.beforeRowEditEvent(event);
890
                        if (cancel.isCanceled())
891
                                return true;
892
                }
893
                return false;
894
        }
895
896
        protected void fireAfterModifyRow(int index) {
897
                AfterRowEditEvent event = new AfterRowEditEvent(this, index, EditionEvent.CHANGE_TYPE_MODIFY);
898
                for (int i=0; i < editionListeners.size(); i++)
899
                {
900
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
901
                        listener.afterRowEditEvent(event);
902
                }
903
904
905
        }
906
907
        protected void fireStartEditionEvent() {
908
                EditionEvent ev = new EditionEvent(this, EditionEvent.START_EDITION);
909
                for (int i=0; i < editionListeners.size(); i++)
910
                {
911
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
912
                        listener.processEvent(ev);
913
                }
914
915
        }
916
917
        protected void fireStopEditionEvent() {
918
                EditionEvent ev = new EditionEvent(this, EditionEvent.STOP_EDITION);
919
                for (int i=0; i < editionListeners.size(); i++)
920
                {
921
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
922
                        listener.processEvent(ev);
923
                }
924
925
        }
926
927
        protected void fireCancelEditionEvent() {
928
                EditionEvent ev = new EditionEvent(this, EditionEvent.CANCEL_EDITION);
929
                for (int i=0; i < editionListeners.size(); i++)
930
                {
931
                        IEditionListener listener = (IEditionListener) editionListeners.get(i);
932
                        listener.processEvent(ev);
933
                }
934
935
        }
936
937
938
        public void addEditionListener(IEditionListener listener)
939
        {
940
                editionListeners.add(listener);
941
        }
942
943
        public void removeEditionListener(IEditionListener listener)
944
        {
945
                editionListeners.remove(listener);
946
        }
947
948 3940 caballero
}