Statistics
| Revision:

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

History | View | Annotate | Download (14.7 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
         * @throws DriverException
102
         */
103
        public void mapExternalFields() throws DriverException {
104
                int numExternalFields = 0;
105
                for (int i=0; i < dataSource.getFieldCount(); i++)
106
                {
107
                        if (!dataSource.isVirtualField(i))
108
                                numExternalFields++;
109
                                
110
                }
111

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

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

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

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

    
160
        /**
161
         * Devuelve el n?mero de campos.
162
         *
163
         * @return N?mero de campos.
164
         *
165
         * @throws DriverException
166
         */
167
        public int getFieldCount() throws DriverException {
168
                // return dataSource.getFieldCount()-numVirtual;
169
                return mapping.length;
170
        }
171

    
172
        /**
173
         * Devuelve el ?ndice del campo a partir de su nombre.
174
         *
175
         * @param arg0 nombre del campo.
176
         *
177
         * @return ?ndice.
178
         *
179
         * @throws DriverException
180
         * @throws FieldNotFoundException
181
         */
182
        public int getFieldIndexByName(String arg0)
183
                throws DriverException {
184
                int internal = dataSource.getFieldIndexByName(arg0);
185
                for (int i=0; i < mapping.length; i++)
186
                {
187
                        if (mapping[i] == internal)
188
                                return i;
189
                }
190
                return -1;
191
        }
192

    
193
        /**
194
         * Devuelve el nombre del campo a partir del ?ndice.
195
         *
196
         * @param arg0 ?ndice.
197
         *
198
         * @return nombre del campo.
199
         *
200
         * @throws DriverException
201
         */
202
        public String getFieldName(int arg0) throws DriverException {
203
            // return dataSource.getFieldName(arg0);
204
                return dataSource.getFieldName(mapping[arg0]);
205
        }
206

    
207
        /**
208
         * Devuelve el valor a partir del n?mro de fila y columna.
209
         *
210
         * @param arg0 n?mero de registro.
211
         * @param arg1 n?mero de campo.
212
         *
213
         * @return Valor.
214
         *
215
         * @throws DriverException
216
         */
217
        public Value getFieldValue(long arg0, int arg1) throws DriverException {
218
                return dataSource.getFieldValue(arg0, mapping[arg1]);
219
                // return dataSource.getFieldValue(arg0, arg1);
220
        }
221

    
222
        /**
223
         * Devuelve el nombre del DataSource.
224
         *
225
         * @return Nombre.
226
         */
227
        public String getName() {
228
                return dataSource.getName();
229
        }
230

    
231
        /**
232
         * Devuelve el n?mero de filas en total.
233
         *
234
         * @return n?mero de filas.
235
         *
236
         * @throws DriverException
237
         */
238
        public long getRowCount() throws DriverException {
239
                return dataSource.getRowCount();
240
        }
241

    
242
        /**
243
         * Inicializa el dataSource.
244
         *
245
         * @throws DriverException
246
         */
247
        public void start() throws DriverException {
248
                // logger.debug("dataSource.start()");
249
                dataSource.start();
250
        }
251

    
252
        /**
253
         * Finaliza el DataSource.
254
         *
255
         * @throws DriverException
256
         */
257
        public void stop() throws DriverException {
258
                // logger.debug("dataSource.stop()");
259
                dataSource.stop();
260
        }
261

    
262
        /**
263
         * A partir del XMLEntity se rellenan los atributos del DataSource.
264
         *
265
         * @param child
266
         */
267
        public void setXMLEntity03(XMLEntity child) {
268
                selectionSupport.setXMLEntity(child.getChild(0));
269
        }
270

    
271
        /**
272
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
273
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
274
         * eventos, se realiza la propagaci?n de manera manual al final de la
275
         * "r?faga" de eventos
276
         */
277
        public void fireSelectionEvents() {
278
                selectionSupport.fireSelectionEvents();
279
        }
280

    
281
        /**
282
         * A?ade un nuevo Listener al SelectionSupport.
283
         *
284
         * @param listener SelectionListener.
285
         */
286
        public void addSelectionListener(SelectionListener listener) {
287
                selectionSupport.addSelectionListener(listener);
288
        }
289

    
290
        /**
291
         * Borra un Listener al SelectionSupport.
292
         *
293
         * @param listener Listener a borrar.
294
         */
295
        public void removeSelectionListener(SelectionListener listener) {
296
                selectionSupport.removeSelectionListener(listener);
297
        }
298

    
299
        /**
300
         * Borra la selecci?n.
301
         */
302
        public void clearSelection() {
303
                selectionSupport.clearSelection();
304
        }
305

    
306
        /**
307
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
308
         *
309
         * @return FBitset con los elementos seleccionados.
310
         */
311
        public FBitSet getSelection() {
312
                return selectionSupport.getSelection();
313
        }
314

    
315
        /**
316
         * Devuelve el SelectionSupport.
317
         *
318
         * @return SelectinSuport.
319
         */
320
        public SelectionSupport getSelectionSupport() {
321
                return selectionSupport;
322
        }
323

    
324
        /**
325
         * Devuelve true si el elemento est? seleccionado.
326
         *
327
         * @param recordIndex ?ndice del registro.
328
         *
329
         * @return True si el registro est? seleccionado.
330
         */
331
        public boolean isSelected(int recordIndex) {
332
                return selectionSupport.isSelected(recordIndex);
333
        }
334

    
335
        /**
336
         * Inserta una nueva selecci?n.
337
         *
338
         * @param selection FBitSet.
339
         */
340
        public void setSelection(FBitSet selection) {
341
                selectionSupport.setSelection(selection);
342
        }
343

    
344
        private void putMemento(XMLEntity xml) throws XMLException {
345
                try {
346
                        GDBMSHandler handler = new GDBMSHandler();
347
                        Memento m = getMemento();
348
                        m.setContentHandler(handler);
349
                        m.getXML();
350
                        XMLEntity child = handler.getXMLEntity();
351

    
352
                        xml.addChild(child);
353
                } catch (MementoException e) {
354
                        throw new XMLException(e);
355
                } catch (SAXException e) {
356
                        throw new XMLException(e);
357
                }
358
        }
359

    
360
        /**
361
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
362
         * DataSource.
363
         *
364
         * @return XMLEntity.
365
         * @throws XMLException
366
         */
367
        public XMLEntity getXMLEntity() throws XMLException {
368
                XMLEntity xml = new XMLEntity();
369
                xml.putProperty("className",this.getClass().getName());
370
                xml.addChild(selectionSupport.getXMLEntity());
371
                putMemento(xml);
372

    
373
                return xml;
374
        }
375

    
376
        /**
377
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
378
         */
379
        public long[] getWhereFilter() throws IOException {
380
                return dataSource.getWhereFilter();
381
        }
382

    
383
        /**
384
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
385
         */
386
        public int getFieldType(int i) throws DriverException {
387
                // return dataSource.getFieldType(i);
388
                return dataSource.getFieldType(mapping[i]);
389
        }
390

    
391
        /**
392
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
393
         */
394
        public DataSourceFactory getDataSourceFactory() {
395
                return dataSource.getDataSourceFactory();
396
        }
397

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

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

    
413
        /**
414
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
415
         */
416
        public Memento getMemento() throws MementoException {
417
                return dataSource.getMemento();
418
        }
419

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

    
427
    /**
428
     * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
429
     */
430
    public int[] getPrimaryKeys() throws DriverException {
431
            return dataSource.getPrimaryKeys();
432
    }
433

    
434
    /**
435
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
436
     */
437
    public ValueCollection getPKValue(long rowIndex) throws DriverException {
438
        return dataSource.getPKValue(rowIndex);
439
    }
440

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

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

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

    
463
    /**
464
     * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
465
     */
466
    public Value[] getRow(long rowIndex) throws DriverException {
467
            Value[] withoutVirtuals = new Value[mapping.length];
468
            Value[] internal = dataSource.getRow(rowIndex);
469
            for (int i=0; i < mapping.length; i++)
470
            {
471
                    withoutVirtuals[i] = internal[mapping[i]];
472
            
473
            }
474
        return withoutVirtuals;
475
    }
476

    
477
    /**
478
     * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
479
     */
480
    public String[] getFieldNames() throws DriverException {
481
            String[] fieldNames = new String[getFieldCount()];
482
                int j=0;
483
                for (int i=0; i < dataSource.getFieldCount(); i++)
484
                {
485
                        if (!dataSource.isVirtualField(i))
486
                                fieldNames[j++] = dataSource.getFieldName(i);
487
                                
488
                }
489
        // return dataSource.getFieldNames();
490
            return fieldNames;
491
    }
492

    
493
    /**
494
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
495
     */
496
    public String[] getPKNames() throws DriverException {
497
        return dataSource.getPKNames();
498
    }
499

    
500
        public void removeLinksSelectionListener() {
501
                selectionSupport.removeLinkSelectionListener();
502
        }
503

    
504
    /**
505
     * @throws DriverException 
506
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
507
     */
508
    public DataWare getDataWare(int arg0) throws DriverException {
509
        return dataSource.getDataWare(arg0);
510
    }
511

    
512
        public int getFieldWidth(int i) throws DriverException {
513
                return dataSource.getFieldWidth(mapping[i]);
514
                // return dataSource.getFieldWidth(i);
515
        }
516

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

    
546
        public Driver getDriver() {                
547
                return this.dataSource.getDriver();
548
        }
549

    
550
        public void reload() throws DriverException, IOException {
551
                dataSource.reload();
552
                
553
        }
554

    
555
        public void addDataSourceListener(IDataSourceListener listener) {
556
                dataSource.addDataSourceListener(listener);
557
                
558
        }
559

    
560
        public void removeDataSourceListener(IDataSourceListener listener) {
561
                dataSource.removeDataSourceListener(listener);
562
                
563
        }
564
}