Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_903+3D / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectableDataSource.java @ 10722

History | View | Annotate | Download (15.1 KB)

1 1100 fjp
/* 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 460 fernando
package com.iver.cit.gvsig.fmap.layers;
42
43 1828 fernando
import java.io.IOException;
44
45
import org.apache.log4j.Logger;
46 1836 fernando
import org.xml.sax.SAXException;
47 1828 fernando
48 5569 jmvivo
import com.hardcode.driverManager.Driver;
49 1828 fernando
import com.hardcode.driverManager.DriverLoadException;
50 460 fernando
import com.hardcode.gdbms.engine.data.DataSource;
51 546 fernando
import com.hardcode.gdbms.engine.data.DataSourceFactory;
52 6323 fjp
import com.hardcode.gdbms.engine.data.IDataSourceListener;
53 546 fernando
import com.hardcode.gdbms.engine.data.NoSuchTableException;
54 4863 fjp
import com.hardcode.gdbms.engine.data.SourceInfo;
55 1828 fernando
import com.hardcode.gdbms.engine.data.driver.DriverException;
56 2217 fernando
import com.hardcode.gdbms.engine.data.edition.DataWare;
57 1836 fernando
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 2217 fernando
import com.hardcode.gdbms.engine.instruction.EvaluationException;
61 460 fernando
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
62 1828 fernando
import com.hardcode.gdbms.engine.instruction.SemanticException;
63 460 fernando
import com.hardcode.gdbms.engine.values.Value;
64 2183 fernando
import com.hardcode.gdbms.engine.values.ValueCollection;
65 1828 fernando
import com.hardcode.gdbms.parser.ParseException;
66 4875 fjp
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
67 3963 caballero
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
68 4875 fjp
import com.iver.utiles.NumberUtilities;
69 581 vcaballero
import com.iver.utiles.XMLEntity;
70 460 fernando
71
72 527 vcaballero
/**
73 1034 vcaballero
 * DataSource seleccionable.
74 527 vcaballero
 *
75 546 fernando
 * @author Fernando Gonz?lez Cort?s
76 527 vcaballero
 */
77 3963 caballero
public class SelectableDataSource implements DataSource,Selectable {
78 527 vcaballero
        private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName());
79 460 fernando
        private SelectionSupport selectionSupport = new SelectionSupport();
80
        private DataSource dataSource;
81 4863 fjp
82
        private int[] mapping = null;
83 527 vcaballero
84 460 fernando
        /**
85 1034 vcaballero
         * Crea un nuevo SelectableDataSource.
86 527 vcaballero
         *
87 1034 vcaballero
         * @param name
88 460 fernando
         * @param ds
89 2565 fernando
         * @throws DriverException
90 460 fernando
         */
91 2565 fernando
        public SelectableDataSource(DataSource ds) throws DriverException {
92 460 fernando
                dataSource = ds;
93 5663 fjp
                dataSource.start();
94 4863 fjp
                // Creamos el mapping de campos externos que no muestran el PK.
95 6259 fjp
                mapExternalFields();
96
97
98
        }
99
100
        /**
101 6335 fjp
         * Maps real fields or "external" fields. We don't want to see virtual fields.
102 6259 fjp
         * @throws DriverException
103
         */
104
        public void mapExternalFields() throws DriverException {
105 4863 fjp
                int numExternalFields = 0;
106
                for (int i=0; i < dataSource.getFieldCount(); i++)
107
                {
108
                        if (!dataSource.isVirtualField(i))
109
                                numExternalFields++;
110
111
                }
112 3963 caballero
113 4863 fjp
                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 460 fernando
        }
122 527 vcaballero
123 1836 fernando
        public static SelectableDataSource createSelectableDataSource(XMLEntity xml) throws NoSuchTableException, ParseException, DriverLoadException, DriverException, SemanticException, IOException, XMLException{
124 3963 caballero
125 1828 fernando
                SelectionSupport ss = new SelectionSupport();
126
                ss.setXMLEntity(xml.getChild(0));
127
                XMLEntity xmlDS = xml.getChild(1);
128 1836 fernando
                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 1828 fernando
                }
136 2217 fernando
                SelectableDataSource sds;
137
        try {
138 5663 fjp
            sds = new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.MANUAL_OPENING));
139 2217 fernando
        } catch (EvaluationException e1) {
140
            throw new XMLException(e1);
141
        }
142
        sds.selectionSupport=ss;
143 2183 fernando
                return sds;
144 1828 fernando
        }
145 1836 fernando
146 1828 fernando
        public void setDataSourceFactory(DataSourceFactory dsf) {
147
                dataSource.setDataSourceFactory(dsf);
148
        }
149 2217 fernando
        public void setSourceInfo(SourceInfo sourceInfo) {
150 1828 fernando
                dataSource.setSourceInfo(sourceInfo);
151
        }
152 460 fernando
        /**
153 1034 vcaballero
         * A?ade el soporte para la selecci?n.
154 527 vcaballero
         *
155 460 fernando
         * @param selectionSupport
156
         */
157
        public void setSelectionSupport(SelectionSupport selectionSupport) {
158
                this.selectionSupport = selectionSupport;
159
        }
160
161 527 vcaballero
        /**
162 1034 vcaballero
         * Devuelve el n?mero de campos.
163 527 vcaballero
         *
164 1034 vcaballero
         * @return N?mero de campos.
165 527 vcaballero
         *
166 1034 vcaballero
         * @throws DriverException
167 527 vcaballero
         */
168 460 fernando
        public int getFieldCount() throws DriverException {
169 4863 fjp
                // return dataSource.getFieldCount()-numVirtual;
170 6478 fjp
//                if (mapping.length != dataSource.getFieldCount())
171
//                {
172
//                        mapExternalFields();
173
//                        RuntimeException e = new RuntimeException("Recalculamos los campos de recordset!!");
174
//                        e.printStackTrace();
175
//                }
176 4863 fjp
                return mapping.length;
177 460 fernando
        }
178 527 vcaballero
179
        /**
180 8765 jjdelcerro
         * Return index field searching by its name
181 527 vcaballero
         *
182 8765 jjdelcerro
         * @param arg0 field name.
183 527 vcaballero
         *
184 8765 jjdelcerro
         * @return field index. -1 if not found
185 527 vcaballero
         *
186 1034 vcaballero
         * @throws DriverException
187
         * @throws FieldNotFoundException
188 527 vcaballero
         */
189
        public int getFieldIndexByName(String arg0)
190 1773 fernando
                throws DriverException {
191 4863 fjp
                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 460 fernando
        }
199 527 vcaballero
200
        /**
201 1034 vcaballero
         * Devuelve el nombre del campo a partir del ?ndice.
202 527 vcaballero
         *
203 1034 vcaballero
         * @param arg0 ?ndice.
204 527 vcaballero
         *
205 1034 vcaballero
         * @return nombre del campo.
206 527 vcaballero
         *
207 1034 vcaballero
         * @throws DriverException
208 527 vcaballero
         */
209 460 fernando
        public String getFieldName(int arg0) throws DriverException {
210 4863 fjp
            // return dataSource.getFieldName(arg0);
211
                return dataSource.getFieldName(mapping[arg0]);
212 460 fernando
        }
213 527 vcaballero
214
        /**
215 1034 vcaballero
         * Devuelve el valor a partir del n?mro de fila y columna.
216 527 vcaballero
         *
217 1034 vcaballero
         * @param arg0 n?mero de registro.
218
         * @param arg1 n?mero de campo.
219 527 vcaballero
         *
220 1034 vcaballero
         * @return Valor.
221 527 vcaballero
         *
222 1034 vcaballero
         * @throws DriverException
223 527 vcaballero
         */
224 460 fernando
        public Value getFieldValue(long arg0, int arg1) throws DriverException {
225 4863 fjp
                return dataSource.getFieldValue(arg0, mapping[arg1]);
226
                // return dataSource.getFieldValue(arg0, arg1);
227 460 fernando
        }
228 527 vcaballero
229
        /**
230 1034 vcaballero
         * Devuelve el nombre del DataSource.
231 527 vcaballero
         *
232 1034 vcaballero
         * @return Nombre.
233 527 vcaballero
         */
234 460 fernando
        public String getName() {
235
                return dataSource.getName();
236
        }
237 527 vcaballero
238
        /**
239 2565 fernando
         * Devuelve el n?mero de filas en total.
240 527 vcaballero
         *
241 1034 vcaballero
         * @return n?mero de filas.
242 527 vcaballero
         *
243 1034 vcaballero
         * @throws DriverException
244 527 vcaballero
         */
245 460 fernando
        public long getRowCount() throws DriverException {
246
                return dataSource.getRowCount();
247
        }
248 527 vcaballero
249
        /**
250 1034 vcaballero
         * Inicializa el dataSource.
251 527 vcaballero
         *
252 1034 vcaballero
         * @throws DriverException
253 527 vcaballero
         */
254 460 fernando
        public void start() throws DriverException {
255 3076 fjp
                // logger.debug("dataSource.start()");
256 460 fernando
                dataSource.start();
257
        }
258 527 vcaballero
259
        /**
260 1034 vcaballero
         * Finaliza el DataSource.
261 527 vcaballero
         *
262 1034 vcaballero
         * @throws DriverException
263 527 vcaballero
         */
264 460 fernando
        public void stop() throws DriverException {
265 3076 fjp
                // logger.debug("dataSource.stop()");
266 460 fernando
                dataSource.stop();
267
        }
268
269
        /**
270 2183 fernando
         * 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 527 vcaballero
         * 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 460 fernando
         */
284
        public void fireSelectionEvents() {
285
                selectionSupport.fireSelectionEvents();
286
        }
287
288 527 vcaballero
        /**
289 1034 vcaballero
         * A?ade un nuevo Listener al SelectionSupport.
290 527 vcaballero
         *
291 1034 vcaballero
         * @param listener SelectionListener.
292 527 vcaballero
         */
293 460 fernando
        public void addSelectionListener(SelectionListener listener) {
294
                selectionSupport.addSelectionListener(listener);
295
        }
296
297 527 vcaballero
        /**
298 1034 vcaballero
         * Borra un Listener al SelectionSupport.
299 527 vcaballero
         *
300 1034 vcaballero
         * @param listener Listener a borrar.
301 527 vcaballero
         */
302 460 fernando
        public void removeSelectionListener(SelectionListener listener) {
303
                selectionSupport.removeSelectionListener(listener);
304
        }
305 527 vcaballero
306
        /**
307 1034 vcaballero
         * Borra la selecci?n.
308 527 vcaballero
         */
309 460 fernando
        public void clearSelection() {
310
                selectionSupport.clearSelection();
311
        }
312 527 vcaballero
313
        /**
314 1034 vcaballero
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
315 527 vcaballero
         *
316 1034 vcaballero
         * @return FBitset con los elementos seleccionados.
317 527 vcaballero
         */
318 884 fernando
        public FBitSet getSelection() {
319 460 fernando
                return selectionSupport.getSelection();
320
        }
321 1034 vcaballero
322
        /**
323
         * Devuelve el SelectionSupport.
324
         *
325
         * @return SelectinSuport.
326
         */
327
        public SelectionSupport getSelectionSupport() {
328 581 vcaballero
                return selectionSupport;
329
        }
330 1034 vcaballero
331 527 vcaballero
        /**
332 1034 vcaballero
         * Devuelve true si el elemento est? seleccionado.
333 527 vcaballero
         *
334 1034 vcaballero
         * @param recordIndex ?ndice del registro.
335 527 vcaballero
         *
336 1034 vcaballero
         * @return True si el registro est? seleccionado.
337 527 vcaballero
         */
338 460 fernando
        public boolean isSelected(int recordIndex) {
339
                return selectionSupport.isSelected(recordIndex);
340
        }
341 527 vcaballero
342
        /**
343 1034 vcaballero
         * Inserta una nueva selecci?n.
344 527 vcaballero
         *
345 1034 vcaballero
         * @param selection FBitSet.
346 527 vcaballero
         */
347 683 fernando
        public void setSelection(FBitSet selection) {
348 460 fernando
                selectionSupport.setSelection(selection);
349
        }
350 546 fernando
351 1828 fernando
        private void putMemento(XMLEntity xml) throws XMLException {
352
                try {
353 1836 fernando
                        GDBMSHandler handler = new GDBMSHandler();
354
                        Memento m = getMemento();
355
                        m.setContentHandler(handler);
356
                        m.getXML();
357
                        XMLEntity child = handler.getXMLEntity();
358 3963 caballero
359 1828 fernando
                        xml.addChild(child);
360
                } catch (MementoException e) {
361
                        throw new XMLException(e);
362 1836 fernando
                } catch (SAXException e) {
363
                        throw new XMLException(e);
364 1828 fernando
                }
365
        }
366
367 1034 vcaballero
        /**
368
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
369
         * DataSource.
370
         *
371
         * @return XMLEntity.
372 1828 fernando
         * @throws XMLException
373 1034 vcaballero
         */
374 1828 fernando
        public XMLEntity getXMLEntity() throws XMLException {
375 1034 vcaballero
                XMLEntity xml = new XMLEntity();
376 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
377 581 vcaballero
                xml.addChild(selectionSupport.getXMLEntity());
378 1828 fernando
                putMemento(xml);
379 1034 vcaballero
380 581 vcaballero
                return xml;
381
        }
382 732 fernando
383
        /**
384 884 fernando
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
385
         */
386
        public long[] getWhereFilter() throws IOException {
387
                return dataSource.getWhereFilter();
388
        }
389 1653 fernando
390
        /**
391
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
392
         */
393 1773 fernando
        public int getFieldType(int i) throws DriverException {
394 4863 fjp
                // return dataSource.getFieldType(i);
395
                return dataSource.getFieldType(mapping[i]);
396 1653 fernando
        }
397 1828 fernando
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 1831 fernando
412
        /**
413 2183 fernando
         * @throws DriverException
414 1831 fernando
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
415
         */
416 2183 fernando
        public void remove() throws DriverException {
417 1831 fernando
                dataSource.remove();
418
        }
419 1836 fernando
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 2217 fernando
        public SourceInfo getSourceInfo() {
431 1836 fernando
                return dataSource.getSourceInfo();
432
        }
433 2183 fernando
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 4863 fjp
            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 2183 fernando
    }
483
484
    /**
485
     * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
486
     */
487
    public String[] getFieldNames() throws DriverException {
488 4863 fjp
            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 2183 fernando
    }
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 2217 fernando
511
    /**
512 4053 fjp
     * @throws DriverException
513 2217 fernando
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
514
     */
515 4053 fjp
    public DataWare getDataWare(int arg0) throws DriverException {
516 2217 fernando
        return dataSource.getDataWare(arg0);
517
    }
518 4863 fjp
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 4875 fjp
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 6483 fjp
                        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 4875 fjp
                                fieldsDescrip[i].setFieldDecimalCount(0);
552 6628 fjp
                        // TODO: ?DEFAULTVALUE?
553
                        // fieldsDescrip[i].setDefaultValue(get)
554 4875 fjp
                }
555
                return fieldsDescrip;
556
        }
557 5569 jmvivo
558
        public Driver getDriver() {
559
                return this.dataSource.getDriver();
560
        }
561 6323 fjp
562
        public void reload() throws DriverException, IOException {
563
                dataSource.reload();
564 6326 fjp
                mapExternalFields();
565 6323 fjp
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 460 fernando
}