Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectableDataSource.java @ 10665

History | View | Annotate | Download (15.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.layers;
42

    
43
import java.io.IOException;
44

    
45
import org.apache.log4j.Logger;
46
import org.xml.sax.SAXException;
47

    
48
import com.hardcode.driverManager.Driver;
49
import com.hardcode.driverManager.DriverLoadException;
50
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
51
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
52
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
53
import com.hardcode.gdbms.engine.data.DataSource;
54
import com.hardcode.gdbms.engine.data.DataSourceFactory;
55
import com.hardcode.gdbms.engine.data.IDataSourceListener;
56
import com.hardcode.gdbms.engine.data.NoSuchTableException;
57
import com.hardcode.gdbms.engine.data.SourceInfo;
58
import com.hardcode.gdbms.engine.data.driver.DriverException;
59
import com.hardcode.gdbms.engine.data.edition.DataWare;
60
import com.hardcode.gdbms.engine.data.persistence.Memento;
61
import com.hardcode.gdbms.engine.data.persistence.MementoContentHandler;
62
import com.hardcode.gdbms.engine.data.persistence.MementoException;
63
import com.hardcode.gdbms.engine.instruction.EvaluationException;
64
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
65
import com.hardcode.gdbms.engine.instruction.SemanticException;
66
import com.hardcode.gdbms.engine.values.Value;
67
import com.hardcode.gdbms.engine.values.ValueCollection;
68
import com.hardcode.gdbms.parser.ParseException;
69
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
70
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
71
import com.iver.utiles.NumberUtilities;
72
import com.iver.utiles.XMLEntity;
73

    
74

    
75
/**
76
 * DataSource seleccionable.
77
 *
78
 * @author Fernando Gonz?lez Cort?s
79
 */
80
public class SelectableDataSource implements DataSource,Selectable {
81
        private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName());
82
        private SelectionSupport selectionSupport = new SelectionSupport();
83
        private DataSource dataSource;
84

    
85
        private int[] mapping = null;
86

    
87
        /**
88
         * Crea un nuevo SelectableDataSource.
89
         *
90
         * @param name
91
         * @param ds
92
         * @throws ReadDriverException TODO
93
         */
94
        public SelectableDataSource(DataSource ds) throws ReadDriverException {
95
                dataSource = ds;
96
                dataSource.start();
97
                // Creamos el mapping de campos externos que no muestran el PK.
98
                mapExternalFields();
99

    
100

    
101
        }
102

    
103
        /**
104
         * Maps real fields or "external" fields. We don't want to see virtual fields.
105
         * @throws ReadDriverException
106
         */
107
        public void mapExternalFields() throws ReadDriverException {
108
                int numExternalFields = 0;
109
                for (int i=0; i < dataSource.getFieldCount(); i++)
110
                {
111
                        if (!dataSource.isVirtualField(i))
112
                                numExternalFields++;
113

    
114
                }
115

    
116
                mapping = new int[numExternalFields];
117
                int j=0;
118
                for (int i=0; i < dataSource.getFieldCount(); i++)
119
                {
120
                        if (!dataSource.isVirtualField(i))
121
                                mapping[j++] = i;
122

    
123
                }
124
        }
125

    
126
        public static SelectableDataSource createSelectableDataSource(XMLEntity xml) throws DriverLoadException, XMLException{
127

    
128
                SelectionSupport ss = new SelectionSupport();
129
                ss.setXMLEntity(xml.getChild(0));
130
                XMLEntity xmlDS = xml.getChild(1);
131
                GDBMSParser parser = new GDBMSParser(xmlDS);
132
                MementoContentHandler gdbmsHandler = new MementoContentHandler();
133
                parser.setContentHandler(gdbmsHandler);
134
                try {
135
                        parser.parse();
136
                } catch (SAXException e) {
137
                        throw new XMLException(e);
138
                }
139
                SelectableDataSource sds;
140
        try {
141
            sds = new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.MANUAL_OPENING));
142
        } catch (EvaluationException e1) {
143
            throw new XMLException(e1);
144
        } catch (ReadDriverException e) {
145
                 throw new XMLException(e);
146
                } catch (DriverLoadException e) {
147
                         throw new XMLException(e);
148
                } catch (SemanticException e) {
149
                         throw new XMLException(e);
150
                } catch (ParseException e) {
151
                         throw new XMLException(e);
152
                } catch (NoSuchTableException e) {
153
                         throw new XMLException(e);
154
                }
155
        sds.selectionSupport=ss;
156
                return sds;
157
        }
158

    
159
        public void setDataSourceFactory(DataSourceFactory dsf) {
160
                dataSource.setDataSourceFactory(dsf);
161
        }
162
        public void setSourceInfo(SourceInfo sourceInfo) {
163
                dataSource.setSourceInfo(sourceInfo);
164
        }
165
        /**
166
         * A?ade el soporte para la selecci?n.
167
         *
168
         * @param selectionSupport
169
         */
170
        public void setSelectionSupport(SelectionSupport selectionSupport) {
171
                this.selectionSupport = selectionSupport;
172
        }
173

    
174
        /**
175
         * Devuelve el n?mero de campos.
176
         *
177
         * @return N?mero de campos.
178
         *
179
         * @throws DriverException
180
         */
181
        public int getFieldCount() throws ReadDriverException {
182
                // return dataSource.getFieldCount()-numVirtual;
183
//                if (mapping.length != dataSource.getFieldCount())
184
//                {
185
//                        mapExternalFields();
186
//                        RuntimeException e = new RuntimeException("Recalculamos los campos de recordset!!");
187
//                        e.printStackTrace();
188
//                }
189
                return mapping.length;
190
        }
191

    
192
        /**
193
         * Return index field searching by its name
194
         *
195
         * @param arg0 field name.
196
         *
197
         * @return field index. -1 if not found
198
         *
199
         * @throws DriverException
200
         * @throws FieldNotFoundException
201
         */
202
        public int getFieldIndexByName(String arg0)
203
                throws ReadDriverException {
204
                int internal = dataSource.getFieldIndexByName(arg0);
205
                for (int i=0; i < mapping.length; i++)
206
                {
207
                        if (mapping[i] == internal)
208
                                return i;
209
                }
210
                return -1;
211
        }
212

    
213
        /**
214
         * Devuelve el nombre del campo a partir del ?ndice.
215
         *
216
         * @param arg0 ?ndice.
217
         *
218
         * @return nombre del campo.
219
         *
220
         * @throws DriverException
221
         */
222
        public String getFieldName(int arg0) throws ReadDriverException {
223
            // return dataSource.getFieldName(arg0);
224
                return dataSource.getFieldName(mapping[arg0]);
225
        }
226

    
227
        /**
228
         * Devuelve el valor a partir del n?mro de fila y columna.
229
         *
230
         * @param arg0 n?mero de registro.
231
         * @param arg1 n?mero de campo.
232
         *
233
         * @return Valor.
234
         *
235
         * @throws DriverException
236
         */
237
        public Value getFieldValue(long arg0, int arg1) throws ReadDriverException {
238
                return dataSource.getFieldValue(arg0, mapping[arg1]);
239
                // return dataSource.getFieldValue(arg0, arg1);
240
        }
241

    
242
        /**
243
         * Devuelve el nombre del DataSource.
244
         *
245
         * @return Nombre.
246
         */
247
        public String getName() {
248
                return dataSource.getName();
249
        }
250

    
251
        /**
252
         * Devuelve el n?mero de filas en total.
253
         *
254
         * @return n?mero de filas.
255
         *
256
         * @throws DriverException
257
         */
258
        public long getRowCount() throws ReadDriverException {
259
                return dataSource.getRowCount();
260
        }
261

    
262
        /**
263
         * Inicializa el dataSource.
264
         *
265
         * @throws DriverException
266
         */
267
        public void start() throws ReadDriverException {
268
                // logger.debug("dataSource.start()");
269
                dataSource.start();
270
        }
271

    
272
        /**
273
         * Finaliza el DataSource.
274
         *
275
         * @throws DriverException
276
         */
277
        public void stop() throws ReadDriverException {
278
                // logger.debug("dataSource.stop()");
279
                dataSource.stop();
280
        }
281

    
282
        /**
283
         * A partir del XMLEntity se rellenan los atributos del DataSource.
284
         *
285
         * @param child
286
         */
287
        public void setXMLEntity03(XMLEntity child) {
288
                selectionSupport.setXMLEntity(child.getChild(0));
289
        }
290

    
291
        /**
292
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
293
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
294
         * eventos, se realiza la propagaci?n de manera manual al final de la
295
         * "r?faga" de eventos
296
         */
297
        public void fireSelectionEvents() {
298
                selectionSupport.fireSelectionEvents();
299
        }
300

    
301
        /**
302
         * A?ade un nuevo Listener al SelectionSupport.
303
         *
304
         * @param listener SelectionListener.
305
         */
306
        public void addSelectionListener(SelectionListener listener) {
307
                selectionSupport.addSelectionListener(listener);
308
        }
309

    
310
        /**
311
         * Borra un Listener al SelectionSupport.
312
         *
313
         * @param listener Listener a borrar.
314
         */
315
        public void removeSelectionListener(SelectionListener listener) {
316
                selectionSupport.removeSelectionListener(listener);
317
        }
318

    
319
        /**
320
         * Borra la selecci?n.
321
         */
322
        public void clearSelection() {
323
                selectionSupport.clearSelection();
324
        }
325

    
326
        /**
327
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
328
         *
329
         * @return FBitset con los elementos seleccionados.
330
         */
331
        public FBitSet getSelection() {
332
                return selectionSupport.getSelection();
333
        }
334

    
335
        /**
336
         * Devuelve el SelectionSupport.
337
         *
338
         * @return SelectinSuport.
339
         */
340
        public SelectionSupport getSelectionSupport() {
341
                return selectionSupport;
342
        }
343

    
344
        /**
345
         * Devuelve true si el elemento est? seleccionado.
346
         *
347
         * @param recordIndex ?ndice del registro.
348
         *
349
         * @return True si el registro est? seleccionado.
350
         */
351
        public boolean isSelected(int recordIndex) {
352
                return selectionSupport.isSelected(recordIndex);
353
        }
354

    
355
        /**
356
         * Inserta una nueva selecci?n.
357
         *
358
         * @param selection FBitSet.
359
         */
360
        public void setSelection(FBitSet selection) {
361
                selectionSupport.setSelection(selection);
362
        }
363

    
364
        private void putMemento(XMLEntity xml) throws XMLException {
365
                try {
366
                        GDBMSHandler handler = new GDBMSHandler();
367
                        Memento m = getMemento();
368
                        m.setContentHandler(handler);
369
                        m.getXML();
370
                        XMLEntity child = handler.getXMLEntity();
371

    
372
                        xml.addChild(child);
373
                } catch (MementoException e) {
374
                        throw new XMLException(e);
375
                } catch (SAXException e) {
376
                        throw new XMLException(e);
377
                }
378
        }
379

    
380
        /**
381
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
382
         * DataSource.
383
         *
384
         * @return XMLEntity.
385
         * @throws XMLException
386
         */
387
        public XMLEntity getXMLEntity() throws XMLException {
388
                XMLEntity xml = new XMLEntity();
389
                xml.putProperty("className",this.getClass().getName());
390
                xml.addChild(selectionSupport.getXMLEntity());
391
                putMemento(xml);
392

    
393
                return xml;
394
        }
395

    
396
        /**
397
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
398
         */
399
        public long[] getWhereFilter() throws IOException {
400
                return dataSource.getWhereFilter();
401
        }
402

    
403
        /**
404
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
405
         */
406
        public int getFieldType(int i) throws ReadDriverException {
407
                // return dataSource.getFieldType(i);
408
                return dataSource.getFieldType(mapping[i]);
409
        }
410

    
411
        /**
412
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
413
         */
414
        public DataSourceFactory getDataSourceFactory() {
415
                return dataSource.getDataSourceFactory();
416
        }
417

    
418
        /**
419
         * @see com.hardcode.gdbms.engine.data.DataSource#getAsString()
420
         */
421
        public String getAsString() throws ReadDriverException {
422
                return dataSource.getAsString();
423
        }
424

    
425
        /**
426
         * @throws DriverException
427
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
428
         */
429
        public void remove() throws WriteDriverException {
430
                dataSource.remove();
431
        }
432

    
433
        /**
434
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
435
         */
436
        public Memento getMemento() throws MementoException {
437
                return dataSource.getMemento();
438
        }
439

    
440
        /**
441
         * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
442
         */
443
        public SourceInfo getSourceInfo() {
444
                return dataSource.getSourceInfo();
445
        }
446

    
447
    /**
448
     * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
449
     */
450
    public int[] getPrimaryKeys() throws ReadDriverException {
451
            return dataSource.getPrimaryKeys();
452
    }
453

    
454
    /**
455
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
456
     */
457
    public ValueCollection getPKValue(long rowIndex) throws ReadDriverException {
458
        return dataSource.getPKValue(rowIndex);
459
    }
460

    
461
    /**
462
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
463
     */
464
    public String getPKName(int fieldId) throws ReadDriverException {
465
        return dataSource.getPKName(fieldId);
466
    }
467

    
468
    /**
469
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
470
     */
471
    public int getPKType(int i) throws ReadDriverException {
472
        return dataSource.getPKType(i);
473
    }
474

    
475
    /**
476
     * @throws DriverException
477
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
478
     */
479
    public int getPKCardinality() throws ReadDriverException {
480
        return dataSource.getPKCardinality();
481
    }
482

    
483
    /**
484
     * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
485
     */
486
    public Value[] getRow(long rowIndex) throws ReadDriverException {
487
            Value[] withoutVirtuals = new Value[mapping.length];
488
            Value[] internal = dataSource.getRow(rowIndex);
489
            for (int i=0; i < mapping.length; i++)
490
            {
491
                    withoutVirtuals[i] = internal[mapping[i]];
492

    
493
            }
494
        return withoutVirtuals;
495
    }
496

    
497
    /**
498
     * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
499
     */
500
    public String[] getFieldNames() throws ReadDriverException {
501
            String[] fieldNames = new String[getFieldCount()];
502
                int j=0;
503
                for (int i=0; i < dataSource.getFieldCount(); i++)
504
                {
505
                        if (!dataSource.isVirtualField(i))
506
                                fieldNames[j++] = dataSource.getFieldName(i);
507

    
508
                }
509
        // return dataSource.getFieldNames();
510
            return fieldNames;
511
    }
512

    
513
    /**
514
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
515
     */
516
    public String[] getPKNames() throws ReadDriverException {
517
        return dataSource.getPKNames();
518
    }
519

    
520
        public void removeLinksSelectionListener() {
521
                selectionSupport.removeLinkSelectionListener();
522
        }
523

    
524
    /**
525
     * @throws DriverException
526
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
527
     */
528
    public DataWare getDataWare(int arg0) throws ReadDriverException {
529
        return dataSource.getDataWare(arg0);
530
    }
531

    
532
        public int getFieldWidth(int i) throws ReadDriverException {
533
                return dataSource.getFieldWidth(mapping[i]);
534
                // return dataSource.getFieldWidth(i);
535
        }
536

    
537
        public boolean isVirtualField(int fieldId) throws ReadDriverException {
538
                return dataSource.isVirtualField(fieldId);
539
        }
540

    
541
        /**
542
         * Useful to writers, to know the field definitions.
543
         * NOTE: Maximun precision: 6 decimals. (We may need to change this)
544
         * @return Description of non virtual fields
545
         * @throws DriverException
546
         */
547
        public FieldDescription[] getFieldsDescription() throws ReadDriverException{
548
                int numFields = getFieldCount();
549
                FieldDescription[] fieldsDescrip = new FieldDescription[numFields];
550
                for (int i = 0; i < numFields; i++) {
551
                        fieldsDescrip[i] = new FieldDescription();
552
                        int type = getFieldType(i);
553
                        fieldsDescrip[i].setFieldType(type);
554
                        fieldsDescrip[i].setFieldName(getFieldName(i));
555
                        fieldsDescrip[i].setFieldLength(getFieldWidth(i));
556
                        if (NumberUtilities.isNumeric(type))
557
                        {
558
                                if (!NumberUtilities.isNumericInteger(type))
559
                                        // TODO: If there is a lost in precision, this should be changed.
560
                                        fieldsDescrip[i].setFieldDecimalCount(6);
561
                        }
562
                        else
563
                                fieldsDescrip[i].setFieldDecimalCount(0);
564
                        // TODO: ?DEFAULTVALUE?
565
                        // fieldsDescrip[i].setDefaultValue(get)
566
                }
567
                return fieldsDescrip;
568
        }
569

    
570
        public Driver getDriver() {
571
                return this.dataSource.getDriver();
572
        }
573

    
574
        public void reload() throws ReloadDriverException {
575
                dataSource.reload();
576
                try {
577
                        mapExternalFields();
578
                } catch (ReadDriverException e) {
579
                        throw new ReloadDriverException(getDriver().getName(),e);
580
                }
581

    
582
        }
583

    
584
        public void addDataSourceListener(IDataSourceListener listener) {
585
                dataSource.addDataSourceListener(listener);
586

    
587
        }
588

    
589
        public void removeDataSourceListener(IDataSourceListener listener) {
590
                dataSource.removeDataSourceListener(listener);
591

    
592
        }
593
}