Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_1_RELEASE / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectableDataSource.java @ 9531

History | View | Annotate | Download (15.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.engine.data.DataSource;
51
import com.hardcode.gdbms.engine.data.DataSourceFactory;
52
import com.hardcode.gdbms.engine.data.IDataSourceListener;
53
import com.hardcode.gdbms.engine.data.NoSuchTableException;
54
import com.hardcode.gdbms.engine.data.SourceInfo;
55
import com.hardcode.gdbms.engine.data.driver.DriverException;
56
import com.hardcode.gdbms.engine.data.edition.DataWare;
57
import com.hardcode.gdbms.engine.data.persistence.Memento;
58
import com.hardcode.gdbms.engine.data.persistence.MementoContentHandler;
59
import com.hardcode.gdbms.engine.data.persistence.MementoException;
60
import com.hardcode.gdbms.engine.instruction.EvaluationException;
61
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
62
import com.hardcode.gdbms.engine.instruction.SemanticException;
63
import com.hardcode.gdbms.engine.values.Value;
64
import com.hardcode.gdbms.engine.values.ValueCollection;
65
import com.hardcode.gdbms.parser.ParseException;
66
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
67
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
68
import com.iver.utiles.NumberUtilities;
69
import com.iver.utiles.XMLEntity;
70

    
71

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

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

    
97

    
98
        }
99

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

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

    
123
        public static SelectableDataSource createSelectableDataSource(XMLEntity xml) throws NoSuchTableException, ParseException, DriverLoadException, DriverException, SemanticException, IOException, XMLException{
124

    
125
                SelectionSupport ss = new SelectionSupport();
126
                ss.setXMLEntity(xml.getChild(0));
127
                XMLEntity xmlDS = xml.getChild(1);
128
                GDBMSParser parser = new GDBMSParser(xmlDS);
129
                MementoContentHandler gdbmsHandler = new MementoContentHandler();
130
                parser.setContentHandler(gdbmsHandler);
131
                try {
132
                        parser.parse();
133
                } catch (SAXException e) {
134
                        throw new XMLException(e);
135
                }
136
                SelectableDataSource sds;
137
        try {
138
            sds = new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.MANUAL_OPENING));
139
        } catch (EvaluationException e1) {
140
            throw new XMLException(e1);
141
        }
142
        sds.selectionSupport=ss;
143
                return sds;
144
        }
145

    
146
        public void setDataSourceFactory(DataSourceFactory dsf) {
147
                dataSource.setDataSourceFactory(dsf);
148
        }
149
        public void setSourceInfo(SourceInfo sourceInfo) {
150
                dataSource.setSourceInfo(sourceInfo);
151
        }
152
        /**
153
         * A?ade el soporte para la selecci?n.
154
         *
155
         * @param selectionSupport
156
         */
157
        public void setSelectionSupport(SelectionSupport selectionSupport) {
158
                this.selectionSupport = selectionSupport;
159
        }
160

    
161
        /**
162
         * Devuelve el n?mero de campos.
163
         *
164
         * @return N?mero de campos.
165
         *
166
         * @throws DriverException
167
         */
168
        public int getFieldCount() throws DriverException {
169
                // return dataSource.getFieldCount()-numVirtual;
170
//                if (mapping.length != dataSource.getFieldCount())
171
//                {
172
//                        mapExternalFields();
173
//                        RuntimeException e = new RuntimeException("Recalculamos los campos de recordset!!");
174
//                        e.printStackTrace();
175
//                }
176
                return mapping.length;
177
        }
178

    
179
        /**
180
         * Return index field searching by its name
181
         *
182
         * @param arg0 field name.
183
         *
184
         * @return field index. -1 if not found
185
         *
186
         * @throws DriverException
187
         * @throws FieldNotFoundException
188
         */
189
        public int getFieldIndexByName(String arg0)
190
                throws DriverException {
191
                int internal = dataSource.getFieldIndexByName(arg0);
192
                for (int i=0; i < mapping.length; i++)
193
                {
194
                        if (mapping[i] == internal)
195
                                return i;
196
                }
197
                return -1;
198
        }
199

    
200
        /**
201
         * Devuelve el nombre del campo a partir del ?ndice.
202
         *
203
         * @param arg0 ?ndice.
204
         *
205
         * @return nombre del campo.
206
         *
207
         * @throws DriverException
208
         */
209
        public String getFieldName(int arg0) throws DriverException {
210
            // return dataSource.getFieldName(arg0);
211
                return dataSource.getFieldName(mapping[arg0]);
212
        }
213

    
214
        /**
215
         * Devuelve el valor a partir del n?mro de fila y columna.
216
         *
217
         * @param arg0 n?mero de registro.
218
         * @param arg1 n?mero de campo.
219
         *
220
         * @return Valor.
221
         *
222
         * @throws DriverException
223
         */
224
        public Value getFieldValue(long arg0, int arg1) throws DriverException {
225
                return dataSource.getFieldValue(arg0, mapping[arg1]);
226
                // return dataSource.getFieldValue(arg0, arg1);
227
        }
228

    
229
        /**
230
         * Devuelve el nombre del DataSource.
231
         *
232
         * @return Nombre.
233
         */
234
        public String getName() {
235
                return dataSource.getName();
236
        }
237

    
238
        /**
239
         * Devuelve el n?mero de filas en total.
240
         *
241
         * @return n?mero de filas.
242
         *
243
         * @throws DriverException
244
         */
245
        public long getRowCount() throws DriverException {
246
                return dataSource.getRowCount();
247
        }
248

    
249
        /**
250
         * Inicializa el dataSource.
251
         *
252
         * @throws DriverException
253
         */
254
        public void start() throws DriverException {
255
                // logger.debug("dataSource.start()");
256
                dataSource.start();
257
        }
258

    
259
        /**
260
         * Finaliza el DataSource.
261
         *
262
         * @throws DriverException
263
         */
264
        public void stop() throws DriverException {
265
                // logger.debug("dataSource.stop()");
266
                dataSource.stop();
267
        }
268

    
269
        /**
270
         * A partir del XMLEntity se rellenan los atributos del DataSource.
271
         *
272
         * @param child
273
         */
274
        public void setXMLEntity03(XMLEntity child) {
275
                selectionSupport.setXMLEntity(child.getChild(0));
276
        }
277

    
278
        /**
279
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
280
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
281
         * eventos, se realiza la propagaci?n de manera manual al final de la
282
         * "r?faga" de eventos
283
         */
284
        public void fireSelectionEvents() {
285
                selectionSupport.fireSelectionEvents();
286
        }
287

    
288
        /**
289
         * A?ade un nuevo Listener al SelectionSupport.
290
         *
291
         * @param listener SelectionListener.
292
         */
293
        public void addSelectionListener(SelectionListener listener) {
294
                selectionSupport.addSelectionListener(listener);
295
        }
296

    
297
        /**
298
         * Borra un Listener al SelectionSupport.
299
         *
300
         * @param listener Listener a borrar.
301
         */
302
        public void removeSelectionListener(SelectionListener listener) {
303
                selectionSupport.removeSelectionListener(listener);
304
        }
305

    
306
        /**
307
         * Borra la selecci?n.
308
         */
309
        public void clearSelection() {
310
                selectionSupport.clearSelection();
311
        }
312

    
313
        /**
314
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
315
         *
316
         * @return FBitset con los elementos seleccionados.
317
         */
318
        public FBitSet getSelection() {
319
                return selectionSupport.getSelection();
320
        }
321

    
322
        /**
323
         * Devuelve el SelectionSupport.
324
         *
325
         * @return SelectinSuport.
326
         */
327
        public SelectionSupport getSelectionSupport() {
328
                return selectionSupport;
329
        }
330

    
331
        /**
332
         * Devuelve true si el elemento est? seleccionado.
333
         *
334
         * @param recordIndex ?ndice del registro.
335
         *
336
         * @return True si el registro est? seleccionado.
337
         */
338
        public boolean isSelected(int recordIndex) {
339
                return selectionSupport.isSelected(recordIndex);
340
        }
341

    
342
        /**
343
         * Inserta una nueva selecci?n.
344
         *
345
         * @param selection FBitSet.
346
         */
347
        public void setSelection(FBitSet selection) {
348
                selectionSupport.setSelection(selection);
349
        }
350

    
351
        private void putMemento(XMLEntity xml) throws XMLException {
352
                try {
353
                        GDBMSHandler handler = new GDBMSHandler();
354
                        Memento m = getMemento();
355
                        m.setContentHandler(handler);
356
                        m.getXML();
357
                        XMLEntity child = handler.getXMLEntity();
358

    
359
                        xml.addChild(child);
360
                } catch (MementoException e) {
361
                        throw new XMLException(e);
362
                } catch (SAXException e) {
363
                        throw new XMLException(e);
364
                }
365
        }
366

    
367
        /**
368
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
369
         * DataSource.
370
         *
371
         * @return XMLEntity.
372
         * @throws XMLException
373
         */
374
        public XMLEntity getXMLEntity() throws XMLException {
375
                XMLEntity xml = new XMLEntity();
376
                xml.putProperty("className",this.getClass().getName());
377
                xml.addChild(selectionSupport.getXMLEntity());
378
                putMemento(xml);
379

    
380
                return xml;
381
        }
382

    
383
        /**
384
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
385
         */
386
        public long[] getWhereFilter() throws IOException {
387
                return dataSource.getWhereFilter();
388
        }
389

    
390
        /**
391
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
392
         */
393
        public int getFieldType(int i) throws DriverException {
394
                // return dataSource.getFieldType(i);
395
                return dataSource.getFieldType(mapping[i]);
396
        }
397

    
398
        /**
399
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
400
         */
401
        public DataSourceFactory getDataSourceFactory() {
402
                return dataSource.getDataSourceFactory();
403
        }
404

    
405
        /**
406
         * @see com.hardcode.gdbms.engine.data.DataSource#getAsString()
407
         */
408
        public String getAsString() throws DriverException {
409
                return dataSource.getAsString();
410
        }
411

    
412
        /**
413
         * @throws DriverException
414
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
415
         */
416
        public void remove() throws DriverException {
417
                dataSource.remove();
418
        }
419

    
420
        /**
421
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
422
         */
423
        public Memento getMemento() throws MementoException {
424
                return dataSource.getMemento();
425
        }
426

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

    
434
    /**
435
     * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
436
     */
437
    public int[] getPrimaryKeys() throws DriverException {
438
            return dataSource.getPrimaryKeys();
439
    }
440

    
441
    /**
442
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
443
     */
444
    public ValueCollection getPKValue(long rowIndex) throws DriverException {
445
        return dataSource.getPKValue(rowIndex);
446
    }
447

    
448
    /**
449
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
450
     */
451
    public String getPKName(int fieldId) throws DriverException {
452
        return dataSource.getPKName(fieldId);
453
    }
454

    
455
    /**
456
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
457
     */
458
    public int getPKType(int i) throws DriverException {
459
        return dataSource.getPKType(i);
460
    }
461

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

    
470
    /**
471
     * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
472
     */
473
    public Value[] getRow(long rowIndex) throws DriverException {
474
            Value[] withoutVirtuals = new Value[mapping.length];
475
            Value[] internal = dataSource.getRow(rowIndex);
476
            for (int i=0; i < mapping.length; i++)
477
            {
478
                    withoutVirtuals[i] = internal[mapping[i]];
479
            
480
            }
481
        return withoutVirtuals;
482
    }
483

    
484
    /**
485
     * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
486
     */
487
    public String[] getFieldNames() throws DriverException {
488
            String[] fieldNames = new String[getFieldCount()];
489
                int j=0;
490
                for (int i=0; i < dataSource.getFieldCount(); i++)
491
                {
492
                        if (!dataSource.isVirtualField(i))
493
                                fieldNames[j++] = dataSource.getFieldName(i);
494
                                
495
                }
496
        // return dataSource.getFieldNames();
497
            return fieldNames;
498
    }
499

    
500
    /**
501
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
502
     */
503
    public String[] getPKNames() throws DriverException {
504
        return dataSource.getPKNames();
505
    }
506

    
507
        public void removeLinksSelectionListener() {
508
                selectionSupport.removeLinkSelectionListener();
509
        }
510

    
511
    /**
512
     * @throws DriverException 
513
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
514
     */
515
    public DataWare getDataWare(int arg0) throws DriverException {
516
        return dataSource.getDataWare(arg0);
517
    }
518

    
519
        public int getFieldWidth(int i) throws DriverException {
520
                return dataSource.getFieldWidth(mapping[i]);
521
                // return dataSource.getFieldWidth(i);
522
        }
523

    
524
        public boolean isVirtualField(int fieldId) throws DriverException {
525
                return dataSource.isVirtualField(fieldId);
526
        }
527
        
528
        /**
529
         * Useful to writers, to know the field definitions.
530
         * NOTE: Maximun precision: 6 decimals. (We may need to change this)
531
         * @return Description of non virtual fields
532
         * @throws DriverException
533
         */
534
        public FieldDescription[] getFieldsDescription() throws DriverException
535
        {
536
                int numFields = getFieldCount();
537
                FieldDescription[] fieldsDescrip = new FieldDescription[numFields];
538
                for (int i = 0; i < numFields; i++) {
539
                        fieldsDescrip[i] = new FieldDescription();
540
                        int type = getFieldType(i);
541
                        fieldsDescrip[i].setFieldType(type);
542
                        fieldsDescrip[i].setFieldName(getFieldName(i));
543
                        fieldsDescrip[i].setFieldLength(getFieldWidth(i));
544
                        if (NumberUtilities.isNumeric(type))
545
                        {
546
                                if (!NumberUtilities.isNumericInteger(type))
547
                                        // TODO: If there is a lost in precision, this should be changed.
548
                                        fieldsDescrip[i].setFieldDecimalCount(6);
549
                        }
550
                        else
551
                                fieldsDescrip[i].setFieldDecimalCount(0);
552
                        // TODO: ?DEFAULTVALUE?
553
                        // fieldsDescrip[i].setDefaultValue(get)
554
                }
555
                return fieldsDescrip;
556
        }
557

    
558
        public Driver getDriver() {                
559
                return this.dataSource.getDriver();
560
        }
561

    
562
        public void reload() throws DriverException, IOException {
563
                dataSource.reload();
564
                mapExternalFields();
565
                
566
        }
567

    
568
        public void addDataSourceListener(IDataSourceListener listener) {
569
                dataSource.addDataSourceListener(listener);
570
                
571
        }
572

    
573
        public void removeDataSourceListener(IDataSourceListener listener) {
574
                dataSource.removeDataSourceListener(listener);
575
                
576
        }
577
}