Statistics
| Revision:

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

History | View | Annotate | Download (15.5 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
//                 CHEMA: AUTOMATIC DATA SOURCE
146
            //sds = new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.MANUAL_OPENING));
147
            sds = new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.AUTOMATIC_OPENING));
148
        } catch (EvaluationException e1) {
149
            throw new XMLException(e1);
150
        }
151
        sds.selectionSupport=ss;
152
                return sds;
153
        }
154

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
389
                return xml;
390
        }
391

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
516
        public void removeLinksSelectionListener() {
517
                selectionSupport.removeLinkSelectionListener();
518
        }
519

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

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

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

    
567
        public Driver getDriver() {                
568
                return this.dataSource.getDriver();
569
        }
570

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

    
577
        public void addDataSourceListener(IDataSourceListener listener) {
578
                dataSource.addDataSourceListener(listener);
579
                
580
        }
581

    
582
        public void removeDataSourceListener(IDataSourceListener listener) {
583
                dataSource.removeDataSourceListener(listener);
584
                
585
        }
586
}