Statistics
| Revision:

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

History | View | Annotate | Download (9.77 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

    
47
import com.hardcode.driverManager.DriverLoadException;
48
import com.hardcode.gdbms.engine.data.DataSource;
49
import com.hardcode.gdbms.engine.data.DataSourceFactory;
50
import com.hardcode.gdbms.engine.data.DataSourceLayerMemento;
51
import com.hardcode.gdbms.engine.data.Memento;
52
import com.hardcode.gdbms.engine.data.MementoException;
53
import com.hardcode.gdbms.engine.data.NoSuchTableException;
54
import com.hardcode.gdbms.engine.data.OperationLayerMemento;
55
import com.hardcode.gdbms.engine.data.driver.DriverException;
56
import com.hardcode.gdbms.engine.data.driver.DriverInfo;
57
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
58
import com.hardcode.gdbms.engine.instruction.SemanticException;
59
import com.hardcode.gdbms.engine.values.Value;
60
import com.hardcode.gdbms.parser.ParseException;
61
import com.iver.utiles.XMLEntity;
62

    
63

    
64
/**
65
 * DataSource seleccionable.
66
 *
67
 * @author Fernando Gonz?lez Cort?s
68
 */
69
public class SelectableDataSource implements DataSource {
70
        private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName());
71
        private SelectionSupport selectionSupport = new SelectionSupport();
72
        private DataSource dataSource;
73

    
74
        /**
75
         * Crea un nuevo SelectableDataSource.
76
         *
77
         * @param name
78
         * @param ds
79
         */
80
        public SelectableDataSource(DataSource ds) {
81
                dataSource = ds;
82
        }
83

    
84
        public static SelectableDataSource createSelectableDataSource(XMLEntity xml) throws NoSuchTableException, ParseException, DriverLoadException, DriverException, SemanticException, IOException{
85
                
86
                SelectionSupport ss = new SelectionSupport();
87
                ss.setXMLEntity(xml.getChild(0));
88
                XMLEntity xmlDS = xml.getChild(1);
89
                DataSource dataSource = null;
90
                if (xmlDS.contains("tableName")){
91
                        dataSource = LayerFactory.getDataSourceFactory().createRandomDataSource(
92
                                        xmlDS.getStringProperty("tableName"),
93
                                        xmlDS.getStringProperty("tableAlias"), DataSourceFactory.AUTOMATIC_MODE);
94
                }else if (xmlDS.contains("sql")){
95
                        dataSource = LayerFactory.getDataSourceFactory().executeSQL(xmlDS.getStringProperty("sql"), DataSourceFactory.AUTOMATIC_MODE);
96
                }
97
                
98
                SelectableDataSource ret = new SelectableDataSource(dataSource);
99
                ret.selectionSupport = ss;
100
                
101
                return ret;
102
        }
103
        
104
        public Memento getMemento() throws MementoException {
105
                return dataSource.getMemento();
106
        }
107
        public void setDataSourceFactory(DataSourceFactory dsf) {
108
                dataSource.setDataSourceFactory(dsf);
109
        }
110
        public void setSourceInfo(DriverInfo sourceInfo) {
111
                dataSource.setSourceInfo(sourceInfo);
112
        }
113
        /**
114
         * A?ade el soporte para la selecci?n.
115
         *
116
         * @param selectionSupport
117
         */
118
        public void setSelectionSupport(SelectionSupport selectionSupport) {
119
                this.selectionSupport = selectionSupport;
120
        }
121

    
122
        /**
123
         * Devuelve el DBMS.
124
         *
125
         * @return String.
126
         */
127
        public String getDBMS() {
128
                return dataSource.getDBMS();
129
        }
130
        
131
        /**
132
         * Devuelve el n?mero de campos.
133
         *
134
         * @return N?mero de campos.
135
         *
136
         * @throws DriverException
137
         */
138
        public int getFieldCount() throws DriverException {
139
                return dataSource.getFieldCount();
140
        }
141

    
142
        /**
143
         * Devuelve el ?ndice del campo a partir de su nombre.
144
         *
145
         * @param arg0 nombre del campo.
146
         *
147
         * @return ?ndice.
148
         *
149
         * @throws DriverException
150
         * @throws FieldNotFoundException
151
         */
152
        public int getFieldIndexByName(String arg0)
153
                throws DriverException {
154
                return dataSource.getFieldIndexByName(arg0);
155
        }
156

    
157
        /**
158
         * Devuelve el nombre del campo a partir del ?ndice.
159
         *
160
         * @param arg0 ?ndice.
161
         *
162
         * @return nombre del campo.
163
         *
164
         * @throws DriverException
165
         */
166
        public String getFieldName(int arg0) throws DriverException {
167
                return dataSource.getFieldName(arg0);
168
        }
169

    
170
        /**
171
         * Devuelve el valor a partir del n?mro de fila y columna.
172
         *
173
         * @param arg0 n?mero de registro.
174
         * @param arg1 n?mero de campo.
175
         *
176
         * @return Valor.
177
         *
178
         * @throws DriverException
179
         */
180
        public Value getFieldValue(long arg0, int arg1) throws DriverException {
181
                return dataSource.getFieldValue(arg0, arg1);
182
        }
183

    
184
        /**
185
         * Devuelve el nombre del DataSource.
186
         *
187
         * @return Nombre.
188
         */
189
        public String getName() {
190
                return dataSource.getName();
191
        }
192

    
193
        /**
194
         * Devuelve el n?mro de filas en total.
195
         *
196
         * @return n?mero de filas.
197
         *
198
         * @throws DriverException
199
         */
200
        public long getRowCount() throws DriverException {
201
                return dataSource.getRowCount();
202
        }
203

    
204
        /**
205
         * Inicializa el dataSource.
206
         *
207
         * @throws DriverException
208
         */
209
        public void start() throws DriverException {
210
                logger.debug("dataSource.start()");
211
                dataSource.start();
212
        }
213

    
214
        /**
215
         * Finaliza el DataSource.
216
         *
217
         * @throws DriverException
218
         */
219
        public void stop() throws DriverException {
220
                logger.debug("dataSource.stop()");
221
                dataSource.stop();
222
        }
223

    
224
        /**
225
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
226
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
227
         * eventos, se realiza la propagaci?n de manera manual al final de la
228
         * "r?faga" de eventos
229
         */
230
        public void fireSelectionEvents() {
231
                selectionSupport.fireSelectionEvents();
232
        }
233

    
234
        /**
235
         * A?ade un nuevo Listener al SelectionSupport.
236
         *
237
         * @param listener SelectionListener.
238
         */
239
        public void addSelectionListener(SelectionListener listener) {
240
                selectionSupport.addSelectionListener(listener);
241
        }
242

    
243
        /**
244
         * Borra un Listener al SelectionSupport.
245
         *
246
         * @param listener Listener a borrar.
247
         */
248
        public void removeSelectionListener(SelectionListener listener) {
249
                selectionSupport.removeSelectionListener(listener);
250
        }
251

    
252
        /**
253
         * Borra la selecci?n.
254
         */
255
        public void clearSelection() {
256
                selectionSupport.clearSelection();
257
        }
258

    
259
        /**
260
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
261
         *
262
         * @return FBitset con los elementos seleccionados.
263
         */
264
        public FBitSet getSelection() {
265
                return selectionSupport.getSelection();
266
        }
267

    
268
        /**
269
         * Devuelve el SelectionSupport.
270
         *
271
         * @return SelectinSuport.
272
         */
273
        public SelectionSupport getSelectionSupport() {
274
                return selectionSupport;
275
        }
276

    
277
        /**
278
         * Devuelve true si el elemento est? seleccionado.
279
         *
280
         * @param recordIndex ?ndice del registro.
281
         *
282
         * @return True si el registro est? seleccionado.
283
         */
284
        public boolean isSelected(int recordIndex) {
285
                return selectionSupport.isSelected(recordIndex);
286
        }
287

    
288
        /**
289
         * Inserta una nueva selecci?n.
290
         *
291
         * @param selection FBitSet.
292
         */
293
        public void setSelection(FBitSet selection) {
294
                selectionSupport.setSelection(selection);
295
        }
296

    
297
        /**
298
         * Inserta un nuevo nombre para el dataSource.
299
         *
300
         * @param name Nuevo nombre del DataSource.
301
         *
302
         * @throws RuntimeException
303
         */
304
        public void setName(String name) {
305
                try {
306
                        LayerFactory.getDataSourceFactory().changeDataSourceName(dataSource.getName(), name);
307
                } catch (NoSuchTableException e) {
308
                        throw new RuntimeException("No se encuentra la tabla????");
309
                }
310
        }
311

    
312
        private void putMemento(XMLEntity xml) throws XMLException {
313
                try {
314
                        XMLEntity child = new XMLEntity();
315
                        Memento memento = getMemento();
316
                        if (memento instanceof DataSourceLayerMemento){
317
                                DataSourceLayerMemento m = (DataSourceLayerMemento) memento;
318
                                child.putProperty("tableAlias", m.getTableAlias());
319
                                child.putProperty("tableName", m.getTableName());
320
                        }else if (memento instanceof OperationLayerMemento){
321
                                OperationLayerMemento m = (OperationLayerMemento) memento;
322
                                child.putProperty("sql", m.getSql());
323
                        }else{
324
                                throw new RuntimeException("Tipo de dataSource no contemplado");
325
                        }
326
                        
327
                        xml.addChild(child);
328
                } catch (MementoException e) {
329
                        throw new XMLException(e);
330
                }
331
        }
332

    
333
        /**
334
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
335
         * DataSource.
336
         *
337
         * @return XMLEntity.
338
         * @throws XMLException
339
         */
340
        public XMLEntity getXMLEntity() throws XMLException {
341
                XMLEntity xml = new XMLEntity();
342
                xml.putProperty("className",this.getClass().getName());
343
                xml.addChild(selectionSupport.getXMLEntity());
344
                putMemento(xml);
345

    
346
                return xml;
347
        }
348

    
349
        /**
350
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
351
         */
352
        public long[] getWhereFilter() throws IOException {
353
                return dataSource.getWhereFilter();
354
        }
355

    
356
        /**
357
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
358
         */
359
        public int getFieldType(int i) throws DriverException {
360
                return dataSource.getFieldType(i);
361
        }
362

    
363
        /**
364
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
365
         */
366
        public DataSourceFactory getDataSourceFactory() {
367
                return dataSource.getDataSourceFactory();
368
        }
369

    
370
        /**
371
         * @see com.hardcode.gdbms.engine.data.HasProperties#getProperty(java.lang.String)
372
         */
373
        public String getProperty(String propertyName) {
374
                return dataSource.getProperty(propertyName);
375
        }
376

    
377
        /**
378
         * @see com.hardcode.gdbms.engine.data.DataSource#getAsString()
379
         */
380
        public String getAsString() throws DriverException {
381
                return dataSource.getAsString();
382
        }
383
}