Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectableDataSource.java @ 6220

History | View | Annotate | Download (14.2 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
import com.hardcode.gdbms.engine.data.NoSuchTableException;
53 4863 fjp
import com.hardcode.gdbms.engine.data.SourceInfo;
54 1828 fernando
import com.hardcode.gdbms.engine.data.driver.DriverException;
55 2217 fernando
import com.hardcode.gdbms.engine.data.edition.DataWare;
56 1836 fernando
import com.hardcode.gdbms.engine.data.persistence.Memento;
57
import com.hardcode.gdbms.engine.data.persistence.MementoContentHandler;
58
import com.hardcode.gdbms.engine.data.persistence.MementoException;
59 2217 fernando
import com.hardcode.gdbms.engine.instruction.EvaluationException;
60 460 fernando
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
61 1828 fernando
import com.hardcode.gdbms.engine.instruction.SemanticException;
62 460 fernando
import com.hardcode.gdbms.engine.values.Value;
63 2183 fernando
import com.hardcode.gdbms.engine.values.ValueCollection;
64 1828 fernando
import com.hardcode.gdbms.parser.ParseException;
65 4875 fjp
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
66 3963 caballero
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
67 4875 fjp
import com.iver.utiles.NumberUtilities;
68 581 vcaballero
import com.iver.utiles.XMLEntity;
69 460 fernando
70
71 527 vcaballero
/**
72 1034 vcaballero
 * DataSource seleccionable.
73 527 vcaballero
 *
74 546 fernando
 * @author Fernando Gonz?lez Cort?s
75 527 vcaballero
 */
76 3963 caballero
public class SelectableDataSource implements DataSource,Selectable {
77 527 vcaballero
        private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName());
78 460 fernando
        private SelectionSupport selectionSupport = new SelectionSupport();
79
        private DataSource dataSource;
80 4863 fjp
81
        private int[] mapping = null;
82 527 vcaballero
83 460 fernando
        /**
84 1034 vcaballero
         * Crea un nuevo SelectableDataSource.
85 527 vcaballero
         *
86 1034 vcaballero
         * @param name
87 460 fernando
         * @param ds
88 2565 fernando
         * @throws DriverException
89 460 fernando
         */
90 2565 fernando
        public SelectableDataSource(DataSource ds) throws DriverException {
91 460 fernando
                dataSource = ds;
92 5663 fjp
                dataSource.start();
93 4863 fjp
                // Creamos el mapping de campos externos que no muestran el PK.
94
                int numExternalFields = 0;
95
                for (int i=0; i < dataSource.getFieldCount(); i++)
96
                {
97
                        if (!dataSource.isVirtualField(i))
98
                                numExternalFields++;
99
100
                }
101 3963 caballero
102 4863 fjp
                mapping = new int[numExternalFields];
103
                int j=0;
104
                for (int i=0; i < dataSource.getFieldCount(); i++)
105
                {
106
                        if (!dataSource.isVirtualField(i))
107
                                mapping[j++] = i;
108
109
                }
110
111
112 460 fernando
        }
113 527 vcaballero
114 1836 fernando
        public static SelectableDataSource createSelectableDataSource(XMLEntity xml) throws NoSuchTableException, ParseException, DriverLoadException, DriverException, SemanticException, IOException, XMLException{
115 3963 caballero
116 1828 fernando
                SelectionSupport ss = new SelectionSupport();
117
                ss.setXMLEntity(xml.getChild(0));
118
                XMLEntity xmlDS = xml.getChild(1);
119 1836 fernando
                GDBMSParser parser = new GDBMSParser(xmlDS);
120
                MementoContentHandler gdbmsHandler = new MementoContentHandler();
121
                parser.setContentHandler(gdbmsHandler);
122
                try {
123
                        parser.parse();
124
                } catch (SAXException e) {
125
                        throw new XMLException(e);
126 1828 fernando
                }
127 2217 fernando
                SelectableDataSource sds;
128
        try {
129 5663 fjp
            sds = new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.MANUAL_OPENING));
130 2217 fernando
        } catch (EvaluationException e1) {
131
            throw new XMLException(e1);
132
        }
133
        sds.selectionSupport=ss;
134 2183 fernando
                return sds;
135 1828 fernando
        }
136 1836 fernando
137 1828 fernando
        public void setDataSourceFactory(DataSourceFactory dsf) {
138
                dataSource.setDataSourceFactory(dsf);
139
        }
140 2217 fernando
        public void setSourceInfo(SourceInfo sourceInfo) {
141 1828 fernando
                dataSource.setSourceInfo(sourceInfo);
142
        }
143 460 fernando
        /**
144 1034 vcaballero
         * A?ade el soporte para la selecci?n.
145 527 vcaballero
         *
146 460 fernando
         * @param selectionSupport
147
         */
148
        public void setSelectionSupport(SelectionSupport selectionSupport) {
149
                this.selectionSupport = selectionSupport;
150
        }
151
152 527 vcaballero
        /**
153 1034 vcaballero
         * Devuelve el n?mero de campos.
154 527 vcaballero
         *
155 1034 vcaballero
         * @return N?mero de campos.
156 527 vcaballero
         *
157 1034 vcaballero
         * @throws DriverException
158 527 vcaballero
         */
159 460 fernando
        public int getFieldCount() throws DriverException {
160 4863 fjp
                // return dataSource.getFieldCount()-numVirtual;
161
                return mapping.length;
162 460 fernando
        }
163 527 vcaballero
164
        /**
165 1034 vcaballero
         * Devuelve el ?ndice del campo a partir de su nombre.
166 527 vcaballero
         *
167 1034 vcaballero
         * @param arg0 nombre del campo.
168 527 vcaballero
         *
169 1034 vcaballero
         * @return ?ndice.
170 527 vcaballero
         *
171 1034 vcaballero
         * @throws DriverException
172
         * @throws FieldNotFoundException
173 527 vcaballero
         */
174
        public int getFieldIndexByName(String arg0)
175 1773 fernando
                throws DriverException {
176 4863 fjp
                int internal = dataSource.getFieldIndexByName(arg0);
177
                for (int i=0; i < mapping.length; i++)
178
                {
179
                        if (mapping[i] == internal)
180
                                return i;
181
                }
182
                return -1;
183 460 fernando
        }
184 527 vcaballero
185
        /**
186 1034 vcaballero
         * Devuelve el nombre del campo a partir del ?ndice.
187 527 vcaballero
         *
188 1034 vcaballero
         * @param arg0 ?ndice.
189 527 vcaballero
         *
190 1034 vcaballero
         * @return nombre del campo.
191 527 vcaballero
         *
192 1034 vcaballero
         * @throws DriverException
193 527 vcaballero
         */
194 460 fernando
        public String getFieldName(int arg0) throws DriverException {
195 4863 fjp
            // return dataSource.getFieldName(arg0);
196
                return dataSource.getFieldName(mapping[arg0]);
197 460 fernando
        }
198 527 vcaballero
199
        /**
200 1034 vcaballero
         * Devuelve el valor a partir del n?mro de fila y columna.
201 527 vcaballero
         *
202 1034 vcaballero
         * @param arg0 n?mero de registro.
203
         * @param arg1 n?mero de campo.
204 527 vcaballero
         *
205 1034 vcaballero
         * @return Valor.
206 527 vcaballero
         *
207 1034 vcaballero
         * @throws DriverException
208 527 vcaballero
         */
209 460 fernando
        public Value getFieldValue(long arg0, int arg1) throws DriverException {
210 4863 fjp
                return dataSource.getFieldValue(arg0, mapping[arg1]);
211
                // return dataSource.getFieldValue(arg0, arg1);
212 460 fernando
        }
213 527 vcaballero
214
        /**
215 1034 vcaballero
         * Devuelve el nombre del DataSource.
216 527 vcaballero
         *
217 1034 vcaballero
         * @return Nombre.
218 527 vcaballero
         */
219 460 fernando
        public String getName() {
220
                return dataSource.getName();
221
        }
222 527 vcaballero
223
        /**
224 2565 fernando
         * Devuelve el n?mero de filas en total.
225 527 vcaballero
         *
226 1034 vcaballero
         * @return n?mero de filas.
227 527 vcaballero
         *
228 1034 vcaballero
         * @throws DriverException
229 527 vcaballero
         */
230 460 fernando
        public long getRowCount() throws DriverException {
231
                return dataSource.getRowCount();
232
        }
233 527 vcaballero
234
        /**
235 1034 vcaballero
         * Inicializa el dataSource.
236 527 vcaballero
         *
237 1034 vcaballero
         * @throws DriverException
238 527 vcaballero
         */
239 460 fernando
        public void start() throws DriverException {
240 3076 fjp
                // logger.debug("dataSource.start()");
241 460 fernando
                dataSource.start();
242
        }
243 527 vcaballero
244
        /**
245 1034 vcaballero
         * Finaliza el DataSource.
246 527 vcaballero
         *
247 1034 vcaballero
         * @throws DriverException
248 527 vcaballero
         */
249 460 fernando
        public void stop() throws DriverException {
250 3076 fjp
                // logger.debug("dataSource.stop()");
251 460 fernando
                dataSource.stop();
252
        }
253
254
        /**
255 2183 fernando
         * A partir del XMLEntity se rellenan los atributos del DataSource.
256
         *
257
         * @param child
258
         */
259
        public void setXMLEntity03(XMLEntity child) {
260
                selectionSupport.setXMLEntity(child.getChild(0));
261
        }
262
263
        /**
264 527 vcaballero
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
265
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
266
         * eventos, se realiza la propagaci?n de manera manual al final de la
267
         * "r?faga" de eventos
268 460 fernando
         */
269
        public void fireSelectionEvents() {
270
                selectionSupport.fireSelectionEvents();
271
        }
272
273 527 vcaballero
        /**
274 1034 vcaballero
         * A?ade un nuevo Listener al SelectionSupport.
275 527 vcaballero
         *
276 1034 vcaballero
         * @param listener SelectionListener.
277 527 vcaballero
         */
278 460 fernando
        public void addSelectionListener(SelectionListener listener) {
279
                selectionSupport.addSelectionListener(listener);
280
        }
281
282 527 vcaballero
        /**
283 1034 vcaballero
         * Borra un Listener al SelectionSupport.
284 527 vcaballero
         *
285 1034 vcaballero
         * @param listener Listener a borrar.
286 527 vcaballero
         */
287 460 fernando
        public void removeSelectionListener(SelectionListener listener) {
288
                selectionSupport.removeSelectionListener(listener);
289
        }
290 527 vcaballero
291
        /**
292 1034 vcaballero
         * Borra la selecci?n.
293 527 vcaballero
         */
294 460 fernando
        public void clearSelection() {
295
                selectionSupport.clearSelection();
296
        }
297 527 vcaballero
298
        /**
299 1034 vcaballero
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
300 527 vcaballero
         *
301 1034 vcaballero
         * @return FBitset con los elementos seleccionados.
302 527 vcaballero
         */
303 884 fernando
        public FBitSet getSelection() {
304 460 fernando
                return selectionSupport.getSelection();
305
        }
306 1034 vcaballero
307
        /**
308
         * Devuelve el SelectionSupport.
309
         *
310
         * @return SelectinSuport.
311
         */
312
        public SelectionSupport getSelectionSupport() {
313 581 vcaballero
                return selectionSupport;
314
        }
315 1034 vcaballero
316 527 vcaballero
        /**
317 1034 vcaballero
         * Devuelve true si el elemento est? seleccionado.
318 527 vcaballero
         *
319 1034 vcaballero
         * @param recordIndex ?ndice del registro.
320 527 vcaballero
         *
321 1034 vcaballero
         * @return True si el registro est? seleccionado.
322 527 vcaballero
         */
323 460 fernando
        public boolean isSelected(int recordIndex) {
324
                return selectionSupport.isSelected(recordIndex);
325
        }
326 527 vcaballero
327
        /**
328 1034 vcaballero
         * Inserta una nueva selecci?n.
329 527 vcaballero
         *
330 1034 vcaballero
         * @param selection FBitSet.
331 527 vcaballero
         */
332 683 fernando
        public void setSelection(FBitSet selection) {
333 460 fernando
                selectionSupport.setSelection(selection);
334
        }
335 546 fernando
336 1828 fernando
        private void putMemento(XMLEntity xml) throws XMLException {
337
                try {
338 1836 fernando
                        GDBMSHandler handler = new GDBMSHandler();
339
                        Memento m = getMemento();
340
                        m.setContentHandler(handler);
341
                        m.getXML();
342
                        XMLEntity child = handler.getXMLEntity();
343 3963 caballero
344 1828 fernando
                        xml.addChild(child);
345
                } catch (MementoException e) {
346
                        throw new XMLException(e);
347 1836 fernando
                } catch (SAXException e) {
348
                        throw new XMLException(e);
349 1828 fernando
                }
350
        }
351
352 1034 vcaballero
        /**
353
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
354
         * DataSource.
355
         *
356
         * @return XMLEntity.
357 1828 fernando
         * @throws XMLException
358 1034 vcaballero
         */
359 1828 fernando
        public XMLEntity getXMLEntity() throws XMLException {
360 1034 vcaballero
                XMLEntity xml = new XMLEntity();
361 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
362 581 vcaballero
                xml.addChild(selectionSupport.getXMLEntity());
363 1828 fernando
                putMemento(xml);
364 1034 vcaballero
365 581 vcaballero
                return xml;
366
        }
367 732 fernando
368
        /**
369 884 fernando
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
370
         */
371
        public long[] getWhereFilter() throws IOException {
372
                return dataSource.getWhereFilter();
373
        }
374 1653 fernando
375
        /**
376
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
377
         */
378 1773 fernando
        public int getFieldType(int i) throws DriverException {
379 4863 fjp
                // return dataSource.getFieldType(i);
380
                return dataSource.getFieldType(mapping[i]);
381 1653 fernando
        }
382 1828 fernando
383
        /**
384
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
385
         */
386
        public DataSourceFactory getDataSourceFactory() {
387
                return dataSource.getDataSourceFactory();
388
        }
389
390
        /**
391
         * @see com.hardcode.gdbms.engine.data.DataSource#getAsString()
392
         */
393
        public String getAsString() throws DriverException {
394
                return dataSource.getAsString();
395
        }
396 1831 fernando
397
        /**
398 2183 fernando
         * @throws DriverException
399 1831 fernando
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
400
         */
401 2183 fernando
        public void remove() throws DriverException {
402 1831 fernando
                dataSource.remove();
403
        }
404 1836 fernando
405
        /**
406
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
407
         */
408
        public Memento getMemento() throws MementoException {
409
                return dataSource.getMemento();
410
        }
411
412
        /**
413
         * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
414
         */
415 2217 fernando
        public SourceInfo getSourceInfo() {
416 1836 fernando
                return dataSource.getSourceInfo();
417
        }
418 2183 fernando
419
    /**
420
     * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
421
     */
422
    public int[] getPrimaryKeys() throws DriverException {
423
            return dataSource.getPrimaryKeys();
424
    }
425
426
    /**
427
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
428
     */
429
    public ValueCollection getPKValue(long rowIndex) throws DriverException {
430
        return dataSource.getPKValue(rowIndex);
431
    }
432
433
    /**
434
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
435
     */
436
    public String getPKName(int fieldId) throws DriverException {
437
        return dataSource.getPKName(fieldId);
438
    }
439
440
    /**
441
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
442
     */
443
    public int getPKType(int i) throws DriverException {
444
        return dataSource.getPKType(i);
445
    }
446
447
    /**
448
     * @throws DriverException
449
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
450
     */
451
    public int getPKCardinality() throws DriverException {
452
        return dataSource.getPKCardinality();
453
    }
454
455
    /**
456
     * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
457
     */
458
    public Value[] getRow(long rowIndex) throws DriverException {
459 4863 fjp
            Value[] withoutVirtuals = new Value[mapping.length];
460
            Value[] internal = dataSource.getRow(rowIndex);
461
            for (int i=0; i < mapping.length; i++)
462
            {
463
                    withoutVirtuals[i] = internal[mapping[i]];
464
465
            }
466
        return withoutVirtuals;
467 2183 fernando
    }
468
469
    /**
470
     * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
471
     */
472
    public String[] getFieldNames() throws DriverException {
473 4863 fjp
            String[] fieldNames = new String[getFieldCount()];
474
                int j=0;
475
                for (int i=0; i < dataSource.getFieldCount(); i++)
476
                {
477
                        if (!dataSource.isVirtualField(i))
478
                                fieldNames[j++] = dataSource.getFieldName(i);
479
480
                }
481
        // return dataSource.getFieldNames();
482
            return fieldNames;
483 2183 fernando
    }
484
485
    /**
486
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
487
     */
488
    public String[] getPKNames() throws DriverException {
489
        return dataSource.getPKNames();
490
    }
491
492
        public void removeLinksSelectionListener() {
493
                selectionSupport.removeLinkSelectionListener();
494
        }
495 2217 fernando
496
    /**
497 4053 fjp
     * @throws DriverException
498 2217 fernando
     * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
499
     */
500 4053 fjp
    public DataWare getDataWare(int arg0) throws DriverException {
501 2217 fernando
        return dataSource.getDataWare(arg0);
502
    }
503 4863 fjp
504
        public int getFieldWidth(int i) throws DriverException {
505
                return dataSource.getFieldWidth(mapping[i]);
506
                // return dataSource.getFieldWidth(i);
507
        }
508
509
        public boolean isVirtualField(int fieldId) throws DriverException {
510
                return dataSource.isVirtualField(fieldId);
511
        }
512 4875 fjp
513
        /**
514
         * Useful to writers, to know the field definitions.
515
         * NOTE: Maximun precision: 6 decimals. (We may need to change this)
516
         * @return Description of non virtual fields
517
         * @throws DriverException
518
         */
519
        public FieldDescription[] getFieldsDescription() throws DriverException
520
        {
521
                int numFields = getFieldCount();
522
                FieldDescription[] fieldsDescrip = new FieldDescription[numFields];
523
                for (int i = 0; i < numFields; i++) {
524
                        fieldsDescrip[i] = new FieldDescription();
525
                        int type = getFieldType(i);
526
                        fieldsDescrip[i].setFieldType(type);
527
                        fieldsDescrip[i].setFieldName(getFieldName(i));
528
                        fieldsDescrip[i].setFieldLength(getFieldWidth(i));
529
                        if (NumberUtilities.isNumericInteger(type))
530
                                fieldsDescrip[i].setFieldDecimalCount(0);
531
                        else
532
                                // TODO: If there is a lost in precision, this should be changed.
533
                                fieldsDescrip[i].setFieldDecimalCount(6);
534
                }
535
                return fieldsDescrip;
536
        }
537 5569 jmvivo
538
        public Driver getDriver() {
539
                return this.dataSource.getDriver();
540
        }
541 460 fernando
}