Statistics
| Revision:

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

History | View | Annotate | Download (16.1 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
                this.dataSource.start();
110
                for (int i=0; i < dataSource.getFieldCount(); i++)
111
                {
112
                        if (!dataSource.isVirtualField(i))
113
                                numExternalFields++;
114

    
115
                }
116

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

    
124
                }
125
                this.dataSource.stop();
126
        }
127

    
128
        public static SelectableDataSource createSelectableDataSource(XMLEntity xml) throws DriverLoadException, XMLException{
129

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

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

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

    
194
        /**
195
         * Return index field searching by its name
196
         *
197
         * @param arg0 field name.
198
         *
199
         * @return field index. -1 if not found
200
         *
201
         * @throws DriverException
202
         * @throws FieldNotFoundException
203
         */
204
        public int getFieldIndexByName(String arg0)
205
                throws ReadDriverException {
206
                int internal = dataSource.getFieldIndexByName(arg0);
207
                for (int i=0; i < mapping.length; i++)
208
                {
209
                        if (mapping[i] == internal)
210
                                return i;
211
                }
212
                //OJO Parche para rodear poblema de gdbms + FileDriver
213
                // Cuando en le fichero existe un campo de nombre 'PK'
214
                if (arg0.equalsIgnoreCase("pk")){
215
                        for (int i=0; i < mapping.length; i++)
216
                        {
217
                                if (dataSource.getFieldName(mapping[i]).equalsIgnoreCase(arg0)){
218
                                        return i;
219
                                }
220
                        }
221

    
222
                }
223
                return -1;
224
        }
225

    
226
        /**
227
         * Devuelve el nombre del campo a partir del ?ndice.
228
         *
229
         * @param arg0 ?ndice.
230
         *
231
         * @return nombre del campo.
232
         *
233
         * @throws DriverException
234
         */
235
        public String getFieldName(int arg0) throws ReadDriverException {
236
            // return dataSource.getFieldName(arg0);
237
                return dataSource.getFieldName(mapping[arg0]);
238
        }
239

    
240
        /**
241
         * Devuelve el valor a partir del n?mro de fila y columna.
242
         *
243
         * @param arg0 n?mero de registro.
244
         * @param arg1 n?mero de campo.
245
         *
246
         * @return Valor.
247
         *
248
         * @throws DriverException
249
         */
250
        public Value getFieldValue(long arg0, int arg1) throws ReadDriverException {
251
                return dataSource.getFieldValue(arg0, mapping[arg1]);
252
                // return dataSource.getFieldValue(arg0, arg1);
253
        }
254

    
255
        /**
256
         * Devuelve el nombre del DataSource.
257
         *
258
         * @return Nombre.
259
         */
260
        public String getName() {
261
                return dataSource.getName();
262
        }
263

    
264
        /**
265
         * Devuelve el n?mero de filas en total.
266
         *
267
         * @return n?mero de filas.
268
         *
269
         * @throws DriverException
270
         */
271
        public long getRowCount() throws ReadDriverException {
272
                return dataSource.getRowCount();
273
        }
274

    
275
        /**
276
         * Inicializa el dataSource.
277
         *
278
         * @throws DriverException
279
         */
280
        public void start() throws ReadDriverException {
281
                // logger.debug("dataSource.start()");
282
                dataSource.start();
283
        }
284

    
285
        /**
286
         * Finaliza el DataSource.
287
         *
288
         * @throws DriverException
289
         */
290
        public void stop() throws ReadDriverException {
291
                // logger.debug("dataSource.stop()");
292
                dataSource.stop();
293
        }
294

    
295
        /**
296
         * A partir del XMLEntity se rellenan los atributos del DataSource.
297
         *
298
         * @param child
299
         */
300
        public void setXMLEntity03(XMLEntity child) {
301
                selectionSupport.setXMLEntity(child.getChild(0));
302
        }
303

    
304
        /**
305
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
306
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
307
         * eventos, se realiza la propagaci?n de manera manual al final de la
308
         * "r?faga" de eventos
309
         */
310
        public void fireSelectionEvents() {
311
                selectionSupport.fireSelectionEvents();
312
        }
313

    
314
        /**
315
         * A?ade un nuevo Listener al SelectionSupport.
316
         *
317
         * @param listener SelectionListener.
318
         */
319
        public void addSelectionListener(SelectionListener listener) {
320
                selectionSupport.addSelectionListener(listener);
321
        }
322

    
323
        /**
324
         * Borra un Listener al SelectionSupport.
325
         *
326
         * @param listener Listener a borrar.
327
         */
328
        public void removeSelectionListener(SelectionListener listener) {
329
                selectionSupport.removeSelectionListener(listener);
330
        }
331

    
332
        /**
333
         * Borra la selecci?n.
334
         */
335
        public void clearSelection() {
336
                selectionSupport.clearSelection();
337
        }
338

    
339
        /**
340
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
341
         *
342
         * @return FBitset con los elementos seleccionados.
343
         */
344
        public FBitSet getSelection() {
345
                return selectionSupport.getSelection();
346
        }
347

    
348
        /**
349
         * Devuelve el SelectionSupport.
350
         *
351
         * @return SelectinSuport.
352
         */
353
        public SelectionSupport getSelectionSupport() {
354
                return selectionSupport;
355
        }
356

    
357
        /**
358
         * Devuelve true si el elemento est? seleccionado.
359
         *
360
         * @param recordIndex ?ndice del registro.
361
         *
362
         * @return True si el registro est? seleccionado.
363
         */
364
        public boolean isSelected(int recordIndex) {
365
                return selectionSupport.isSelected(recordIndex);
366
        }
367

    
368
        /**
369
         * Inserta una nueva selecci?n.
370
         *
371
         * @param selection FBitSet.
372
         */
373
        public void setSelection(FBitSet selection) {
374
                selectionSupport.setSelection(selection);
375
        }
376

    
377
        private void putMemento(XMLEntity xml) throws XMLException {
378
                try {
379
                        GDBMSHandler handler = new GDBMSHandler();
380
                        Memento m = getMemento();
381
                        m.setContentHandler(handler);
382
                        m.getXML();
383
                        XMLEntity child = handler.getXMLEntity();
384

    
385
                        xml.addChild(child);
386
                } catch (MementoException e) {
387
                        throw new XMLException(e);
388
                } catch (SAXException e) {
389
                        throw new XMLException(e);
390
                }
391
        }
392

    
393
        /**
394
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
395
         * DataSource.
396
         *
397
         * @return XMLEntity.
398
         * @throws XMLException
399
         */
400
        public XMLEntity getXMLEntity() throws XMLException {
401
                XMLEntity xml = new XMLEntity();
402
                xml.putProperty("className",this.getClass().getName());
403
                xml.addChild(selectionSupport.getXMLEntity());
404
                putMemento(xml);
405

    
406
                return xml;
407
        }
408

    
409
        /**
410
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
411
         */
412
        public long[] getWhereFilter() throws IOException {
413
                return dataSource.getWhereFilter();
414
        }
415

    
416
        /*
417
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
418
         */
419
        public int getFieldType(int i) throws ReadDriverException {
420
                // return dataSource.getFieldType(i);
421
                if (i>mapping.length-1)
422
                        return dataSource.getFieldType(i);
423
                return dataSource.getFieldType(mapping[i]);
424
        }
425

    
426
        /*
427
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
428
         */
429
        public DataSourceFactory getDataSourceFactory() {
430
                return dataSource.getDataSourceFactory();
431
        }
432

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

    
440
        /*
441
         * @throws DriverException
442
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
443
         */
444
        public void remove() throws WriteDriverException {
445
                dataSource.remove();
446
        }
447

    
448
        /*
449
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
450
         */
451
        public Memento getMemento() throws MementoException {
452
                return dataSource.getMemento();
453
        }
454

    
455
        /*
456
         * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
457
         */
458
        public SourceInfo getSourceInfo() {
459
                return dataSource.getSourceInfo();
460
        }
461

    
462
    /*
463
     * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
464
     */
465
    public int[] getPrimaryKeys() throws ReadDriverException {
466
            return dataSource.getPrimaryKeys();
467
    }
468

    
469
    /*
470
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
471
     */
472
    public ValueCollection getPKValue(long rowIndex) throws ReadDriverException {
473
        return dataSource.getPKValue(rowIndex);
474
    }
475

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

    
483
    /*
484
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
485
     */
486
    public int getPKType(int i) throws ReadDriverException {
487
        return dataSource.getPKType(i);
488
    }
489

    
490
    /*
491
     * @throws DriverException
492
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
493
     */
494
    public int getPKCardinality() throws ReadDriverException {
495
        return dataSource.getPKCardinality();
496
    }
497

    
498
    /*
499
     * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
500
     */
501
    public Value[] getRow(long rowIndex) throws ReadDriverException {
502
            Value[] withoutVirtuals = new Value[mapping.length];
503
            Value[] internal = dataSource.getRow(rowIndex);
504
            for (int i=0; i < mapping.length; i++)
505
            {
506
                    withoutVirtuals[i] = internal[mapping[i]];
507

    
508
            }
509
        return withoutVirtuals;
510
    }
511

    
512
    /*
513
     * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
514
     */
515
    public String[] getFieldNames() throws ReadDriverException {
516
            String[] fieldNames = new String[dataSource.getFieldCount()];
517
                int j=0;
518
                for (int i=0; i < dataSource.getFieldCount(); i++)
519
                {
520
                        if (!dataSource.isVirtualField(i))
521
                                fieldNames[j++] = dataSource.getFieldName(i);
522

    
523
                }
524
        // return dataSource.getFieldNames();
525
            return fieldNames;
526
    }
527

    
528
    /*
529
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
530
     */
531
    public String[] getPKNames() throws ReadDriverException {
532
        return dataSource.getPKNames();
533
    }
534

    
535
        public void removeLinksSelectionListener() {
536
                selectionSupport.removeLinkSelectionListener();
537
        }
538

    
539
    /*
540
     * @throws DriverException
541
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
542
     */
543
    public DataWare getDataWare(int arg0) throws ReadDriverException {
544
        return dataSource.getDataWare(arg0);
545
    }
546

    
547
        public int getFieldWidth(int i) throws ReadDriverException {
548
                return dataSource.getFieldWidth(mapping[i]);
549
                // return dataSource.getFieldWidth(i);
550
        }
551

    
552
        public boolean isVirtualField(int fieldId) throws ReadDriverException {
553
                return dataSource.isVirtualField(fieldId);
554
        }
555

    
556
        /**
557
         * Useful to writers, to know the field definitions.
558
         * NOTE: Maximun precision: 6 decimals. (We may need to change this)
559
         * @return Description of non virtual fields
560
         * @throws DriverException
561
         */
562
        public FieldDescription[] getFieldsDescription() throws ReadDriverException{
563
                int numFields = getFieldCount();
564
                FieldDescription[] fieldsDescrip = new FieldDescription[numFields];
565
                for (int i = 0; i < numFields; i++) {
566
                        fieldsDescrip[i] = new FieldDescription();
567
                        int type = getFieldType(i);
568
                        fieldsDescrip[i].setFieldType(type);
569
                        fieldsDescrip[i].setFieldName(getFieldName(i));
570
                        fieldsDescrip[i].setFieldLength(getFieldWidth(i));
571
                        if (NumberUtilities.isNumeric(type))
572
                        {
573
                                if (!NumberUtilities.isNumericInteger(type))
574
                                        // TODO: If there is a lost in precision, this should be changed.
575
                                        fieldsDescrip[i].setFieldDecimalCount(6);
576
                        }
577
                        else
578
                                fieldsDescrip[i].setFieldDecimalCount(0);
579
                        // TODO: ?DEFAULTVALUE?
580
                        // fieldsDescrip[i].setDefaultValue(get)
581
                }
582
                return fieldsDescrip;
583
        }
584

    
585
        public Driver getDriver() {
586
                return this.dataSource.getDriver();
587
        }
588

    
589
        public void reload() throws ReloadDriverException {
590
                dataSource.reload();
591
                try {
592
                        mapExternalFields();
593
                } catch (ReadDriverException e) {
594
                        throw new ReloadDriverException(getDriver().getName(),e);
595
                }
596

    
597
        }
598

    
599
        public void addDataSourceListener(IDataSourceListener listener) {
600
                dataSource.addDataSourceListener(listener);
601

    
602
        }
603

    
604
        public void removeDataSourceListener(IDataSourceListener listener) {
605
                dataSource.removeDataSourceListener(listener);
606

    
607
        }
608
}