Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectableDataSource.java @ 31228

History | View | Annotate | Download (17.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
        private String[] alias = null;
87

    
88
        /**
89
         * Crea un nuevo SelectableDataSource.
90
         *
91
         * @param name
92
         * @param ds
93
         * @throws ReadDriverException TODO
94
         */
95
        public SelectableDataSource(DataSource ds) throws ReadDriverException {
96
                dataSource = ds;
97
                dataSource.start();
98
                // Creamos el mapping de campos externos que no muestran el PK.
99
                mapExternalFields();
100
                alias = new String[mapping.length];
101
                for (int i=0; i < mapping.length; i++)
102
                {
103
                        alias[i] = getFieldName(i);
104
                }
105

    
106

    
107

    
108
        }
109

    
110
        /**
111
         * Maps real fields or "external" fields. We don't want to see virtual fields.
112
         * @throws ReadDriverException
113
         */
114
        public void mapExternalFields() throws ReadDriverException {
115
                int numExternalFields = 0;
116
//                this.dataSource.start();
117
                int fieldCount=dataSource.getFieldCount();
118
                for (int i=0; i < fieldCount; i++)
119
                {
120
                        if (!dataSource.isVirtualField(i))
121
                                numExternalFields++;
122

    
123
                }
124

    
125
                mapping = new int[numExternalFields];
126
                
127
                int j=0;
128
                for (int i=0; i < fieldCount; i++)
129
                {
130
                        if (!dataSource.isVirtualField(i)) {
131
                                mapping[j] = i;
132
                                j++;
133
                        }
134

    
135
                }
136
                
137
//                this.dataSource.stop();
138
        }
139

    
140
        public static SelectableDataSource createSelectableDataSource(XMLEntity xml) throws DriverLoadException, XMLException{
141

    
142
                SelectionSupport ss = new SelectionSupport();
143
                ss.setXMLEntity(xml.getChild(0));
144
                XMLEntity xmlDS = xml.getChild(1);
145
                GDBMSParser parser = new GDBMSParser(xmlDS);
146
                MementoContentHandler gdbmsHandler = new MementoContentHandler();
147
                parser.setContentHandler(gdbmsHandler);
148
                try {
149
                        parser.parse();
150
                } catch (SAXException e) {
151
                        throw new XMLException(e);
152
                }
153
                SelectableDataSource sds;
154
        try {
155
            sds = new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.AUTOMATIC_OPENING));
156
        } catch (EvaluationException e1) {
157
            throw new XMLException(e1);
158
        } catch (ReadDriverException e) {
159
                 throw new XMLException(e);
160
                } catch (DriverLoadException e) {
161
                         throw new XMLException(e);
162
                } catch (SemanticException e) {
163
                         throw new XMLException(e);
164
                } catch (ParseException e) {
165
                         throw new XMLException(e);
166
                } catch (NoSuchTableException e) {
167
                         throw new XMLException(e);
168
                }
169
        sds.selectionSupport=ss;
170
                return sds;
171
        }
172

    
173
        public void setDataSourceFactory(DataSourceFactory dsf) {
174
                dataSource.setDataSourceFactory(dsf);
175
        }
176
        public void setSourceInfo(SourceInfo sourceInfo) {
177
                dataSource.setSourceInfo(sourceInfo);
178
        }
179
        /**
180
         * A?ade el soporte para la selecci?n.
181
         *
182
         * @param selectionSupport
183
         */
184
        public void setSelectionSupport(SelectionSupport selectionSupport) {
185
                this.selectionSupport = selectionSupport;
186
        }
187

    
188
        /**
189
         * Devuelve el n?mero de campos.
190
         *
191
         * @return N?mero de campos.
192
         *
193
         * @throws DriverException
194
         */
195
        public int getFieldCount() throws ReadDriverException {
196
                // return dataSource.getFieldCount()-numVirtual;
197
//                if (mapping.length != dataSource.getFieldCount())
198
//                {
199
//                        mapExternalFields();
200
//                        RuntimeException e = new RuntimeException("Recalculamos los campos de recordset!!");
201
//                        e.printStackTrace();
202
//                }
203
                if (mapping.length!=dataSource.getFieldCount()){
204
                        mapExternalFields();
205
                }
206
                return mapping.length;
207
        }
208

    
209
        /**
210
         * Return index field searching by its name
211
         *
212
         * @param arg0 field name.
213
         *
214
         * @return field index. -1 if not found
215
         *
216
         * @throws DriverException
217
         * @throws FieldNotFoundException
218
         */
219
        public int getFieldIndexByName(String arg0)
220
                throws ReadDriverException {
221
//                int internal = dataSource.getFieldIndexByName(arg0);
222
//                for (int i=0; i < mapping.length; i++)
223
//                {
224
//                        if (mapping[i] == internal)
225
//                                return i;
226
//                }
227
//                //OJO Parche para rodear poblema de gdbms + FileDriver
228
//                // Cuando en le fichero existe un campo de nombre 'PK'
229
//                if (arg0.equalsIgnoreCase("pk")){
230
//                        for (int i=0; i < mapping.length; i++)
231
//                        {
232
//                                if (dataSource.getFieldName(mapping[i]).equalsIgnoreCase(arg0)){
233
//                                        return i;
234
//                                }
235
//                        }
236
//
237
//                }
238
                for (int i=0; i < getFieldCount(); i++) {
239
                        // Buscamos en los alias. Si no hay alias, cada alias es igual al fieldname
240
                        if (getFieldAlias(i).compareTo(arg0) == 0)
241
                                return i;
242
                }
243
                for (int i=0; i < getFieldCount(); i++) {
244
                        // Por compatibilidad con posibles leyendas guardadas 
245
                        if (getFieldName(i).compareTo(arg0) == 0)
246
                                return i;
247
                }
248
                
249
                return -1;
250
        }
251

    
252
        /**
253
         * Devuelve el nombre del campo a partir del ?ndice.
254
         *
255
         * @param arg0 ?ndice.
256
         *
257
         * @return nombre del campo.
258
         *
259
         * @throws DriverException
260
         */
261
        public String getFieldName(int arg0) throws ReadDriverException {
262
            // return dataSource.getFieldName(arg0);
263
                return dataSource.getFieldName(mapping[arg0]);
264
        }
265

    
266
        /**
267
         * Devuelve el valor a partir del n?mro de fila y columna.
268
         *
269
         * @param arg0 n?mero de registro.
270
         * @param arg1 n?mero de campo.
271
         *
272
         * @return Valor.
273
         *
274
         * @throws DriverException
275
         */
276
        public Value getFieldValue(long arg0, int arg1) throws ReadDriverException {
277
                if (arg1==-1)
278
                        return null;
279
                return dataSource.getFieldValue(arg0, mapping[arg1]);
280
                // return dataSource.getFieldValue(arg0, arg1);
281
        }
282

    
283
        /**
284
         * Devuelve el nombre del DataSource.
285
         *
286
         * @return Nombre.
287
         */
288
        public String getName() {
289
                return dataSource.getName();
290
        }
291

    
292
        /**
293
         * Devuelve el n?mero de filas en total.
294
         *
295
         * @return n?mero de filas.
296
         *
297
         * @throws DriverException
298
         */
299
        public long getRowCount() throws ReadDriverException {
300
                return dataSource.getRowCount();
301
        }
302

    
303
        /**
304
         * Inicializa el dataSource.
305
         *
306
         * @throws DriverException
307
         */
308
        public void start() throws ReadDriverException {
309
                // logger.debug("dataSource.start()");
310
                dataSource.start();
311
        }
312

    
313
        /**
314
         * Finaliza el DataSource.
315
         *
316
         * @throws DriverException
317
         */
318
        public void stop() throws ReadDriverException {
319
                // logger.debug("dataSource.stop()");
320
                dataSource.stop();
321
        }
322

    
323
        /**
324
         * A partir del XMLEntity se rellenan los atributos del DataSource.
325
         *
326
         * @param child
327
         */
328
        public void setXMLEntity03(XMLEntity child) {
329
                selectionSupport.setXMLEntity(child.getChild(0));
330
        }
331

    
332
        /**
333
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
334
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
335
         * eventos, se realiza la propagaci?n de manera manual al final de la
336
         * "r?faga" de eventos
337
         */
338
        public void fireSelectionEvents() {
339
                selectionSupport.fireSelectionEvents();
340
        }
341

    
342
        /**
343
         * A?ade un nuevo Listener al SelectionSupport.
344
         *
345
         * @param listener SelectionListener.
346
         */
347
        public void addSelectionListener(SelectionListener listener) {
348
                selectionSupport.addSelectionListener(listener);
349
        }
350

    
351
        /**
352
         * Borra un Listener al SelectionSupport.
353
         *
354
         * @param listener Listener a borrar.
355
         */
356
        public void removeSelectionListener(SelectionListener listener) {
357
                selectionSupport.removeSelectionListener(listener);
358
        }
359

    
360
        /**
361
         * Borra la selecci?n.
362
         */
363
        public void clearSelection() {
364
                selectionSupport.clearSelection();
365
        }
366

    
367
        /**
368
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
369
         *
370
         * @return FBitset con los elementos seleccionados.
371
         */
372
        public FBitSet getSelection() {
373
                return selectionSupport.getSelection();
374
        }
375

    
376
        /**
377
         * Devuelve el SelectionSupport.
378
         *
379
         * @return SelectinSuport.
380
         */
381
        public SelectionSupport getSelectionSupport() {
382
                return selectionSupport;
383
        }
384

    
385
        /**
386
         * Devuelve true si el elemento est? seleccionado.
387
         *
388
         * @param recordIndex ?ndice del registro.
389
         *
390
         * @return True si el registro est? seleccionado.
391
         */
392
        public boolean isSelected(int recordIndex) {
393
                return selectionSupport.isSelected(recordIndex);
394
        }
395

    
396
        /**
397
         * Inserta una nueva selecci?n.
398
         *
399
         * @param selection FBitSet.
400
         */
401
        public void setSelection(FBitSet selection) {
402
                selectionSupport.setSelection(selection);
403
        }
404

    
405
        private void putMemento(XMLEntity xml) throws XMLException {
406
                try {
407
                        GDBMSHandler handler = new GDBMSHandler();
408
                        Memento m = getMemento();
409
                        m.setContentHandler(handler);
410
                        m.getXML();
411
                        XMLEntity child = handler.getXMLEntity();
412

    
413
                        xml.addChild(child);
414
                } catch (MementoException e) {
415
                        throw new XMLException(e);
416
                } catch (SAXException e) {
417
                        throw new XMLException(e);
418
                }
419
        }
420

    
421
        /**
422
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
423
         * DataSource.
424
         *
425
         * @return XMLEntity.
426
         * @throws XMLException
427
         */
428
        public XMLEntity getXMLEntity() throws XMLException {
429
                XMLEntity xml = new XMLEntity();
430
                xml.putProperty("className",this.getClass().getName());
431
                xml.addChild(selectionSupport.getXMLEntity());
432
                putMemento(xml);
433

    
434
                return xml;
435
        }
436

    
437
        /**
438
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
439
         */
440
        public long[] getWhereFilter() throws IOException {
441
                return dataSource.getWhereFilter();
442
        }
443

    
444
        /*
445
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
446
         */
447
        public int getFieldType(int i) throws ReadDriverException {
448
                // return dataSource.getFieldType(i);
449
                if (i>mapping.length-1)
450
                        return dataSource.getFieldType(i);
451
                return dataSource.getFieldType(mapping[i]);
452
        }
453

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

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

    
468
        /*
469
         * @throws DriverException
470
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
471
         */
472
        public void remove() throws WriteDriverException {
473
                dataSource.remove();
474
        }
475

    
476
        /*
477
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
478
         */
479
        public Memento getMemento() throws MementoException {
480
                return dataSource.getMemento();
481
        }
482

    
483
        /*
484
         * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
485
         */
486
        public SourceInfo getSourceInfo() {
487
                return dataSource.getSourceInfo();
488
        }
489

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

    
497
    /*
498
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
499
     */
500
    public ValueCollection getPKValue(long rowIndex) throws ReadDriverException {
501
        return dataSource.getPKValue(rowIndex);
502
    }
503

    
504
    /*
505
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
506
     */
507
    public String getPKName(int fieldId) throws ReadDriverException {
508
        return dataSource.getPKName(fieldId);
509
    }
510

    
511
    /*
512
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
513
     */
514
    public int getPKType(int i) throws ReadDriverException {
515
        return dataSource.getPKType(i);
516
    }
517

    
518
    /*
519
     * @throws DriverException
520
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
521
     */
522
    public int getPKCardinality() throws ReadDriverException {
523
        return dataSource.getPKCardinality();
524
    }
525

    
526
    /*
527
     * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
528
     */
529
    public Value[] getRow(long rowIndex) throws ReadDriverException {
530
            Value[] withoutVirtuals = new Value[mapping.length];
531
            Value[] internal = dataSource.getRow(rowIndex);
532
            for (int i=0; i < mapping.length; i++)
533
            {
534
                    if (mapping[i] < internal.length)
535
                            withoutVirtuals[i] = internal[mapping[i]];
536

    
537
            }
538
        return withoutVirtuals;
539
    }
540

    
541
    /*
542
     * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
543
     */
544
    public String[] getFieldNames() throws ReadDriverException {
545
            int fieldCount=getFieldCount();
546
            String[] fieldNames = new String[fieldCount];
547
//                int j=0;
548
//                int fieldCount=dataSource.getFieldCount();
549
//                for (int i=0; i < fieldCount; i++)
550
//                {
551
//                        if (!dataSource.isVirtualField(i))
552
//                                fieldNames[j++] = dataSource.getFieldName(i);
553
//
554
//                }
555
                for (int i=0; i < fieldCount; i++)
556
                {
557
                        fieldNames[i] = getFieldAlias(i);
558

    
559
                }
560
            
561
            return fieldNames;
562
    }
563

    
564
    /*
565
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
566
     */
567
    public String[] getPKNames() throws ReadDriverException {
568
        return dataSource.getPKNames();
569
    }
570

    
571
        public void removeLinksSelectionListener() {
572
                selectionSupport.removeLinkSelectionListener();
573
        }
574

    
575
    /*
576
     * @throws DriverException
577
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
578
     */
579
    public DataWare getDataWare(int arg0) throws ReadDriverException {
580
        return dataSource.getDataWare(arg0);
581
    }
582

    
583
        public int getFieldWidth(int i) throws ReadDriverException {
584
                return dataSource.getFieldWidth(mapping[i]);
585
                // return dataSource.getFieldWidth(i);
586
        }
587

    
588
        public boolean isVirtualField(int fieldId) throws ReadDriverException {
589
                return dataSource.isVirtualField(fieldId);
590
        }
591

    
592
        /**
593
         * Useful to writers, to know the field definitions.
594
         * NOTE: Maximun precision: 6 decimals. (We may need to change this)
595
         * @return Description of non virtual fields
596
         * @throws DriverException
597
         */
598
        public FieldDescription[] getFieldsDescription() throws ReadDriverException{
599
                int numFields = getFieldCount();
600
                FieldDescription[] fieldsDescrip = new FieldDescription[numFields];
601
                for (int i = 0; i < numFields; i++) {
602
                        fieldsDescrip[i] = new FieldDescription();
603
                        int type = getFieldType(i);
604
                        fieldsDescrip[i].setFieldType(type);
605
                        fieldsDescrip[i].setFieldName(getFieldName(i));
606
                        fieldsDescrip[i].setFieldLength(getFieldWidth(i));
607
                        fieldsDescrip[i].setFieldAlias(getFieldAlias(i));
608
                        if (NumberUtilities.isNumeric(type))
609
                        {
610
                                if (!NumberUtilities.isNumericInteger(type))
611
                                        // TODO: If there is a lost in precision, this should be changed.
612
                                        fieldsDescrip[i].setFieldDecimalCount(6);
613
                        }
614
                        else
615
                                fieldsDescrip[i].setFieldDecimalCount(0);
616
                        // TODO: ?DEFAULTVALUE?
617
                        // fieldsDescrip[i].setDefaultValue(get)
618
                }
619
                return fieldsDescrip;
620
        }
621

    
622
        public String getFieldAlias(int i) {
623
                return alias[i];
624
        }
625
        public void setFieldAlias(int idField, String aliasName) {
626
                alias[idField] = aliasName;
627
        }
628
        
629

    
630
        public Driver getDriver() {
631
                return this.dataSource.getDriver();
632
        }
633

    
634
        public void reload() throws ReloadDriverException {
635
                dataSource.reload();
636
                try {
637
                        mapExternalFields();
638
                } catch (ReadDriverException e) {
639
                        throw new ReloadDriverException(getDriver().getName(),e);
640
                }
641

    
642
        }
643

    
644
        public void addDataSourceListener(IDataSourceListener listener) {
645
                dataSource.addDataSourceListener(listener);
646

    
647
        }
648

    
649
        public void removeDataSourceListener(IDataSourceListener listener) {
650
                dataSource.removeDataSourceListener(listener);
651

    
652
        }
653
}