Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / layer / SelectableDataSource.java @ 21606

History | View | Annotate | Download (15.2 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
/************************************************
42
 *                                                                                                *
43
 *   Modfied By:                                                                *
44
 *   Prodevelop Integraci?n de Tecnolog?as SL        *
45
 *   Conde Salvatierra de ?lava , 34-10                        *
46
 *   46004 Valencia                                                                *
47
 *   Spain                                                                                *
48
 *                                                                                                *
49
 *   +34 963 510 612                                                        *
50
 *   +34 963 510 968                                                        *
51
 *   gis@prodevelop.es                                                        *
52
 *   http://www.prodevelop.es                                        *
53
 *                                                                                                *
54
 *   gvSIG Mobile Team 2006                                         *
55
 *                                                                                          *         
56
 ************************************************/
57

    
58
package es.prodevelop.gvsig.mobile.fmap.layer;
59

    
60
import java.io.File;
61
import java.io.IOException;
62

    
63
import org.apache.log4j.Logger;
64

    
65
import com.hardcode.driverManager.Driver;
66
import com.hardcode.gdbms.engine.data.DataSource;
67
import com.hardcode.gdbms.engine.data.DataSourceFactory;
68
import com.hardcode.gdbms.engine.data.IDataSourceListener;
69
import com.hardcode.gdbms.engine.data.SourceInfo;
70
import com.hardcode.gdbms.engine.data.driver.DriverException;
71
import com.hardcode.gdbms.engine.data.edition.DataWare;
72
import com.hardcode.gdbms.engine.data.persistence.Memento;
73
import com.hardcode.gdbms.engine.data.persistence.MementoException;
74
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
75
import com.hardcode.gdbms.engine.values.Value;
76
import com.hardcode.gdbms.engine.values.ValueCollection;
77

    
78
import es.prodevelop.gvsig.mobile.fmap.driver.FMapDriverException;
79
import es.prodevelop.gvsig.mobile.fmap.driver.FieldDescription;
80
import es.prodevelop.gvsig.mobile.fmap.driver.vect.dbf.DbfDiskDataSource;
81
import es.prodevelop.gvsig.mobile.fmap.driver.vect.dbf.DbfMemoryDataSource;
82
import es.prodevelop.gvsig.mobile.fmap.util.NumberUtilities;
83
import es.prodevelop.gvsig.mobile.fmap.util.graph2d.FBitSet;
84
//import es.prodevelop.gvsig.mobile.fmap.util.xml.XMLEntity;
85
//import es.prodevelop.gvsig.mobile.fmap.util.xml.XMLException;
86

    
87

    
88
/**
89
 * DataSource seleccionable.
90
 *
91
 * @author Fernando Gonz?lez Cort?s
92
 * @author jldominguez
93
 */
94
public class SelectableDataSource implements DataSource, Selectable {
95
        private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName());
96
        private SelectionSupport selectionSupport = new SelectionSupport();
97
        private DataSource dataSource;
98
        
99
        private int[] mapping = null;
100

    
101
        /**
102
         * Crea un nuevo SelectableDataSource.
103
         *
104
         * @param ds
105
         * @throws FMapDriverException
106
         */
107
        public SelectableDataSource(DataSource ds) throws DriverException {
108
                dataSource = ds;
109

    
110
                dataSource.start();
111
                // Creamos el mapping de campos externos que no muestran el PK.
112
                mapExternalFields();
113
        }
114

    
115
        /**
116
         * Maps real fields or "external" fields. We don't want to see virtual fields.
117
         * @throws FMapDriverException
118
         */
119
        public void mapExternalFields() throws DriverException {
120
                int numExternalFields = 0;
121
                for (int i=0; i < dataSource.getFieldCount(); i++)
122
                {
123
                        if (!dataSource.isVirtualField(i))
124
                                numExternalFields++;
125
                                
126
                }
127

    
128
                mapping = new int[numExternalFields];
129
                int j=0;
130
                for (int i=0; i < dataSource.getFieldCount(); i++)
131
                {
132
                        if (!dataSource.isVirtualField(i))
133
                                mapping[j++] = i;
134
                                
135
                }
136
        }
137

    
138
        public static SelectableDataSource createDbfSelectableDataSource(File dbFile, boolean inmemory)
139
        throws DriverException {
140

    
141
                SelectionSupport ss = new SelectionSupport();
142
                SelectableDataSource sds = null;
143
                
144
                DataSource dbfds = null;
145
                try {
146
                        
147
                        if (inmemory) {
148
                                dbfds = new DbfMemoryDataSource(dbFile);
149
                        } else {
150
                                dbfds = new DbfDiskDataSource(dbFile);
151
                        }
152
                        
153
                } catch (Throwable th) {
154
                        
155
                        logger.error("Primer intento: error al instanciar DbfDataSource (INMEMORY = " + inmemory + ") con dbFile = " + dbFile.getAbsolutePath());
156
                        logger.error("Message: " + th.getMessage());
157
                        
158
                        try {
159
                                dbfds = new DbfDiskDataSource(dbFile);
160
                        } catch (Throwable th2) {
161
                                logger.error("Ultimo intento: error al instanciar DbfDataSource (INMEMORY = " + inmemory + ") con dbFile = " + dbFile.getAbsolutePath());
162
                                logger.error("Message: " + th2.getMessage());
163
                                throw new DriverException(th2.getMessage());
164
                        }
165
                }
166
                
167
                sds = new SelectableDataSource(dbfds);
168

    
169
        sds.selectionSupport = ss;
170
                return sds;
171
        }
172

    
173
        public void setDataSourceFactory(DataSourceFactory dsf) {
174
                dataSource.setDataSourceFactory(dsf);
175
        }
176
        public void setSourceInfo(SourceInfo sourceInfo) {
177
                dataSource.setSourceInfo(sourceInfo);
178
        }
179
        /**
180
         * A?ade el soporte para la selecci?n.
181
         *
182
         * @param selectionSupport
183
         */
184
        public void setSelectionSupport(SelectionSupport selectionSupport) {
185
                this.selectionSupport = selectionSupport;
186
        }
187

    
188
        /**
189
         * Devuelve el n?mero de campos.
190
         *
191
         * @return N?mero de campos.
192
         *
193
         * @throws FMapDriverException
194
         */
195
        public int getFieldCount() throws DriverException {
196
                // return dataSource.getFieldCount()-numVirtual;
197
//                if (mapping.length != dataSource.getFieldCount())
198
//                {
199
//                        mapExternalFields();
200
//                        RuntimeException e = new RuntimeException("Recalculamos los campos de recordset!!");
201
//                        e.printStackTrace();
202
//                }
203
                return mapping.length;
204
        }
205

    
206
        /**
207
         * Return index field searching by its name
208
         *
209
         * @param arg0 field name.
210
         *
211
         * @return field index. -1 if not found
212
         *
213
         * @throws FMapDriverException
214
         * @throws FieldNotFoundException
215
         */
216
        public int getFieldIndexByName(String arg0)
217
                throws DriverException {
218
                int internal = dataSource.getFieldIndexByName(arg0);
219
                for (int i=0; i < mapping.length; i++)
220
                {
221
                        if (mapping[i] == internal)
222
                                return i;
223
                }
224
                return -1;
225
        }
226

    
227
        /**
228
         * Devuelve el nombre del campo a partir del ?ndice.
229
         *
230
         * @param arg0 ?ndice.
231
         *
232
         * @return nombre del campo.
233
         *
234
         * @throws FMapDriverException
235
         */
236
        public String getFieldName(int arg0) throws DriverException {
237
            // return dataSource.getFieldName(arg0);
238
                return dataSource.getFieldName(mapping[arg0]);
239
        }
240

    
241
        /**
242
         * Devuelve el valor a partir del n?mro de fila y columna.
243
         *
244
         * @param arg0 n?mero de registro.
245
         * @param arg1 n?mero de campo.
246
         *
247
         * @return Valor.
248
         *
249
         * @throws FMapDriverException
250
         */
251
        public Value getFieldValue(long arg0, int arg1) throws DriverException {
252
                return dataSource.getFieldValue(arg0, mapping[arg1]);
253
                // return dataSource.getFieldValue(arg0, arg1);
254
        }
255

    
256
        /**
257
         * Devuelve el nombre del DataSource.
258
         *
259
         * @return Nombre.
260
         */
261
        public String getName() {
262
                return dataSource.getName();
263
        }
264

    
265
        /**
266
         * Devuelve el n?mero de filas en total.
267
         *
268
         * @return n?mero de filas.
269
         *
270
         * @throws FMapDriverException
271
         */
272
        public long getRowCount() throws DriverException {
273
                return dataSource.getRowCount();
274
        }
275

    
276
        /**
277
         * Inicializa el dataSource.
278
         *
279
         * @throws FMapDriverException
280
         */
281
        public void start() throws DriverException {
282
                dataSource.start();
283
        }
284

    
285
        /**
286
         * Finaliza el DataSource.
287
         *
288
         * @throws FMapDriverException
289
         */
290
        public void stop() throws DriverException {
291
                dataSource.stop();
292
        }
293

    
294
        /**
295
         * A partir del XMLEntity se rellenan los atributos del DataSource.
296
         *
297
         * @param child
298
         *//*
299
        public void setXMLEntity03(XMLEntity child) {
300
                selectionSupport.setXMLEntity(child.getChild(0));
301
        }*/
302

    
303
        /**
304
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
305
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
306
         * eventos, se realiza la propagaci?n de manera manual al final de la
307
         * "r?faga" de eventos
308
         */
309
        public void fireSelectionEvents() {
310
                selectionSupport.fireSelectionEvents();
311
        }
312

    
313
        /**
314
         * A?ade un nuevo Listener al SelectionSupport.
315
         *
316
         * @param listener SelectionListener.
317
         */
318
        public void addSelectionListener(SelectionListener listener) {
319
                selectionSupport.addSelectionListener(listener);
320
        }
321

    
322
        /**
323
         * Borra un Listener al SelectionSupport.
324
         *
325
         * @param listener Listener a borrar.
326
         */
327
        public void removeSelectionListener(SelectionListener listener) {
328
                selectionSupport.removeSelectionListener(listener);
329
        }
330

    
331
        /**
332
         * Borra la selecci?n.
333
         */
334
        public boolean clearSelection(boolean fire_events) {
335
                return selectionSupport.clearSelection(fire_events);
336
        }
337

    
338
        /**
339
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
340
         *
341
         * @return FBitset con los elementos seleccionados.
342
         */
343
        public FBitSet getSelection() {
344
                return selectionSupport.getSelection();
345
        }
346

    
347
        /**
348
         * Devuelve el SelectionSupport.
349
         *
350
         * @return SelectinSuport.
351
         */
352
        public SelectionSupport getSelectionSupport() {
353
                return selectionSupport;
354
        }
355

    
356
        /**
357
         * Devuelve true si el elemento est? seleccionado.
358
         *
359
         * @param recordIndex ?ndice del registro.
360
         *
361
         * @return True si el registro est? seleccionado.
362
         */
363
        public boolean isSelected(int recordIndex) {
364
                return selectionSupport.isSelected(recordIndex);
365
        }
366

    
367
        /**
368
         * Inserta una nueva selecci?n.
369
         *
370
         * @param selection FBitSet.
371
         */
372
        public void setSelection(FBitSet selection) {
373
                selectionSupport.setSelection(selection);
374
        }
375

    
376

    
377
        /**
378
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
379
         * DataSource.
380
         *
381
         * @return XMLEntity.
382
         * @throws XMLException
383
         *//*
384
        public XMLEntity getXMLEntity() throws XMLException {
385
                XMLEntity xml = new XMLEntity();
386
                xml.putProperty("className",this.getClass().getName());
387
                xml.addChild(selectionSupport.getXMLEntity());
388
                return xml;
389
        }
390
*/
391
        /**
392
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
393
         */
394
        public long[] getWhereFilter() throws IOException {
395
                return dataSource.getWhereFilter();
396
        }
397

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
515

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

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

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

    
563
        public Driver getDriver() {                
564
                return this.dataSource.getDriver();
565
        }
566

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

    
573
        public void addDataSourceListener(IDataSourceListener listener) {
574
                dataSource.addDataSourceListener(listener);
575
                
576
        }
577

    
578
        public void removeDataSourceListener(IDataSourceListener listener) {
579
                dataSource.removeDataSourceListener(listener);
580
                
581
        }
582
}