Statistics
| Revision:

root / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectableDataSource.java @ 11006

History | View | Annotate | Download (15.3 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
                // CHEMA: No deberiamos abrir el dataSource porque si
94
                // Lo tiene que hacer quien lo va a usar
95
                //dataSource.start();
96
                
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 DriverException
106
         */
107
        public void mapExternalFields() throws DriverException {
108
                //CHEMA: Abrimos y cerramos el dataSource para preparar
109
                //                el mapping
110
                this.dataSource.start();
111
                int numExternalFields = 0;
112
                for (int i=0; i < dataSource.getFieldCount(); i++)
113
                {
114
                        if (!dataSource.isVirtualField(i))
115
                                numExternalFields++;
116
                                
117
                }
118

    
119
                mapping = new int[numExternalFields];
120
                int j=0;
121
                for (int i=0; i < dataSource.getFieldCount(); i++)
122
                {
123
                        if (!dataSource.isVirtualField(i))
124
                                mapping[j++] = i;
125
                                
126
                }
127
                this.dataSource.stop();
128
        }
129

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

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

    
153
        public void setDataSourceFactory(DataSourceFactory dsf) {
154
                dataSource.setDataSourceFactory(dsf);
155
        }
156
        public void setSourceInfo(SourceInfo sourceInfo) {
157
                dataSource.setSourceInfo(sourceInfo);
158
        }
159
        /**
160
         * A?ade el soporte para la selecci?n.
161
         *
162
         * @param selectionSupport
163
         */
164
        public void setSelectionSupport(SelectionSupport selectionSupport) {
165
                this.selectionSupport = selectionSupport;
166
        }
167

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

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

    
207
        /**
208
         * Devuelve el nombre del campo a partir del ?ndice.
209
         *
210
         * @param arg0 ?ndice.
211
         *
212
         * @return nombre del campo.
213
         *
214
         * @throws DriverException
215
         */
216
        public String getFieldName(int arg0) throws DriverException {
217
            // return dataSource.getFieldName(arg0);
218
                return dataSource.getFieldName(mapping[arg0]);
219
        }
220

    
221
        /**
222
         * Devuelve el valor a partir del n?mro de fila y columna.
223
         *
224
         * @param arg0 n?mero de registro.
225
         * @param arg1 n?mero de campo.
226
         *
227
         * @return Valor.
228
         *
229
         * @throws DriverException
230
         */
231
        public Value getFieldValue(long arg0, int arg1) throws DriverException {
232
                return dataSource.getFieldValue(arg0, mapping[arg1]);
233
                // return dataSource.getFieldValue(arg0, arg1);
234
        }
235

    
236
        /**
237
         * Devuelve el nombre del DataSource.
238
         *
239
         * @return Nombre.
240
         */
241
        public String getName() {
242
                return dataSource.getName();
243
        }
244

    
245
        /**
246
         * Devuelve el n?mero de filas en total.
247
         *
248
         * @return n?mero de filas.
249
         *
250
         * @throws DriverException
251
         */
252
        public long getRowCount() throws DriverException {
253
                return dataSource.getRowCount();
254
        }
255

    
256
        /**
257
         * Inicializa el dataSource.
258
         *
259
         * @throws DriverException
260
         */
261
        public void start() throws DriverException {
262
                // logger.debug("dataSource.start()");
263
                dataSource.start();
264
        }
265

    
266
        /**
267
         * Finaliza el DataSource.
268
         *
269
         * @throws DriverException
270
         */
271
        public void stop() throws DriverException {
272
                // logger.debug("dataSource.stop()");
273
                dataSource.stop();
274
        }
275

    
276
        /**
277
         * A partir del XMLEntity se rellenan los atributos del DataSource.
278
         *
279
         * @param child
280
         */
281
        public void setXMLEntity03(XMLEntity child) {
282
                selectionSupport.setXMLEntity(child.getChild(0));
283
        }
284

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

    
295
        /**
296
         * A?ade un nuevo Listener al SelectionSupport.
297
         *
298
         * @param listener SelectionListener.
299
         */
300
        public void addSelectionListener(SelectionListener listener) {
301
                selectionSupport.addSelectionListener(listener);
302
        }
303

    
304
        /**
305
         * Borra un Listener al SelectionSupport.
306
         *
307
         * @param listener Listener a borrar.
308
         */
309
        public void removeSelectionListener(SelectionListener listener) {
310
                selectionSupport.removeSelectionListener(listener);
311
        }
312

    
313
        /**
314
         * Borra la selecci?n.
315
         */
316
        public void clearSelection() {
317
                selectionSupport.clearSelection();
318
        }
319

    
320
        /**
321
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
322
         *
323
         * @return FBitset con los elementos seleccionados.
324
         */
325
        public FBitSet getSelection() {
326
                return selectionSupport.getSelection();
327
        }
328

    
329
        /**
330
         * Devuelve el SelectionSupport.
331
         *
332
         * @return SelectinSuport.
333
         */
334
        public SelectionSupport getSelectionSupport() {
335
                return selectionSupport;
336
        }
337

    
338
        /**
339
         * Devuelve true si el elemento est? seleccionado.
340
         *
341
         * @param recordIndex ?ndice del registro.
342
         *
343
         * @return True si el registro est? seleccionado.
344
         */
345
        public boolean isSelected(int recordIndex) {
346
                return selectionSupport.isSelected(recordIndex);
347
        }
348

    
349
        /**
350
         * Inserta una nueva selecci?n.
351
         *
352
         * @param selection FBitSet.
353
         */
354
        public void setSelection(FBitSet selection) {
355
                selectionSupport.setSelection(selection);
356
        }
357

    
358
        private void putMemento(XMLEntity xml) throws XMLException {
359
                try {
360
                        GDBMSHandler handler = new GDBMSHandler();
361
                        Memento m = getMemento();
362
                        m.setContentHandler(handler);
363
                        m.getXML();
364
                        XMLEntity child = handler.getXMLEntity();
365

    
366
                        xml.addChild(child);
367
                } catch (MementoException e) {
368
                        throw new XMLException(e);
369
                } catch (SAXException e) {
370
                        throw new XMLException(e);
371
                }
372
        }
373

    
374
        /**
375
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
376
         * DataSource.
377
         *
378
         * @return XMLEntity.
379
         * @throws XMLException
380
         */
381
        public XMLEntity getXMLEntity() throws XMLException {
382
                XMLEntity xml = new XMLEntity();
383
                xml.putProperty("className",this.getClass().getName());
384
                xml.addChild(selectionSupport.getXMLEntity());
385
                putMemento(xml);
386

    
387
                return xml;
388
        }
389

    
390
        /**
391
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
392
         */
393
        public long[] getWhereFilter() throws IOException {
394
                return dataSource.getWhereFilter();
395
        }
396

    
397
        /**
398
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
399
         */
400
        public int getFieldType(int i) throws DriverException {
401
                // return dataSource.getFieldType(i);
402
                return dataSource.getFieldType(mapping[i]);
403
        }
404

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

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

    
419
        /**
420
         * @throws DriverException
421
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
422
         */
423
        public void remove() throws DriverException {
424
                dataSource.remove();
425
        }
426

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

    
434
        /**
435
         * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
436
         */
437
        public SourceInfo getSourceInfo() {
438
                return dataSource.getSourceInfo();
439
        }
440

    
441
    /**
442
     * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
443
     */
444
    public int[] getPrimaryKeys() throws DriverException {
445
            return dataSource.getPrimaryKeys();
446
    }
447

    
448
    /**
449
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
450
     */
451
    public ValueCollection getPKValue(long rowIndex) throws DriverException {
452
        return dataSource.getPKValue(rowIndex);
453
    }
454

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

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

    
469
    /**
470
     * @throws DriverException
471
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
472
     */
473
    public int getPKCardinality() throws DriverException {
474
        return dataSource.getPKCardinality();
475
    }
476

    
477
    /**
478
     * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
479
     */
480
    public Value[] getRow(long rowIndex) throws DriverException {
481
            Value[] withoutVirtuals = new Value[mapping.length];
482
            Value[] internal = dataSource.getRow(rowIndex);
483
            for (int i=0; i < mapping.length; i++)
484
            {
485
                    withoutVirtuals[i] = internal[mapping[i]];
486
            
487
            }
488
        return withoutVirtuals;
489
    }
490

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

    
507
    /**
508
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
509
     */
510
    public String[] getPKNames() throws DriverException {
511
        return dataSource.getPKNames();
512
    }
513

    
514
        public void removeLinksSelectionListener() {
515
                selectionSupport.removeLinkSelectionListener();
516
        }
517

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

    
526
        public int getFieldWidth(int i) throws DriverException {
527
                return dataSource.getFieldWidth(mapping[i]);
528
                // return dataSource.getFieldWidth(i);
529
        }
530

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

    
565
        public Driver getDriver() {                
566
                return this.dataSource.getDriver();
567
        }
568

    
569
        public void reload() throws DriverException, IOException {
570
                dataSource.reload();
571
                mapExternalFields();
572
                
573
        }
574

    
575
        public void addDataSourceListener(IDataSourceListener listener) {
576
                dataSource.addDataSourceListener(listener);
577
                
578
        }
579

    
580
        public void removeDataSourceListener(IDataSourceListener listener) {
581
                dataSource.removeDataSourceListener(listener);
582
                
583
        }
584
}