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