svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / LayerFactory.java @ 13074
History | View | Annotate | Download (21.7 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.File; |
44 |
import java.io.IOException; |
45 |
import java.sql.SQLException; |
46 |
import java.sql.Types; |
47 |
import java.util.TreeMap; |
48 |
|
49 |
import org.apache.log4j.Logger; |
50 |
import org.cresques.cts.IProjection; |
51 |
|
52 |
import com.hardcode.driverManager.Driver; |
53 |
import com.hardcode.driverManager.DriverLoadException; |
54 |
import com.hardcode.driverManager.DriverManager; |
55 |
import com.hardcode.driverManager.WriterManager; |
56 |
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException; |
57 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
58 |
import com.hardcode.gdbms.driver.exceptions.WriteDriverException; |
59 |
import com.hardcode.gdbms.engine.customQuery.QueryManager; |
60 |
import com.hardcode.gdbms.engine.data.DataSource; |
61 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
62 |
import com.hardcode.gdbms.engine.data.NoSuchTableException; |
63 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
64 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
65 |
import com.hardcode.gdbms.engine.values.Value; |
66 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
67 |
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException; |
68 |
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
69 |
import com.iver.cit.gvsig.fmap.DriverNotLoadedExceptionType; |
70 |
import com.iver.cit.gvsig.fmap.LegendDriverExceptionType; |
71 |
import com.iver.cit.gvsig.fmap.ProgressListener; |
72 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionFactory; |
73 |
import com.iver.cit.gvsig.fmap.drivers.DBException; |
74 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
75 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
76 |
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver; |
77 |
import com.iver.cit.gvsig.fmap.drivers.IVectorialJDBCDriver; |
78 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
79 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
80 |
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend; |
81 |
import com.iver.cit.gvsig.fmap.operations.arcview.ArcJoin; |
82 |
import com.iver.cit.gvsig.fmap.rendering.IVectorialLegend; |
83 |
import com.iver.cit.gvsig.fmap.rendering.LegendFactory; |
84 |
import com.iver.cit.gvsig.fmap.rendering.styling.AttrInTableLabeling; |
85 |
import com.iver.cit.gvsig.fmap.rendering.styling.ILabelingStrategy; |
86 |
|
87 |
/**
|
88 |
* Crea un adaptador del driver que se le pasa como par?metro en los m?todos
|
89 |
* createLayer. Si hay memoria suficiente se crea un FLyrMemory que pasa todas
|
90 |
* las features del driver a memoria
|
91 |
*/
|
92 |
public class LayerFactory { |
93 |
private static Logger logger = Logger.getLogger(LayerFactory.class |
94 |
.getName()); |
95 |
|
96 |
private static String driversPath = "../FMap 03/drivers"; |
97 |
private static String writersPath = "../FMap 03/drivers"; |
98 |
private static DriverManager driverManager; |
99 |
private static WriterManager writerManager; |
100 |
private static DataSourceFactory dataSourceFactory; |
101 |
|
102 |
/**
|
103 |
* Map en el que se guarda para cada fuente de datos a?adida al sistema, el
|
104 |
* adaptador que la maneja. Ha de ser un TreeMap ya que esta clase define la
|
105 |
* igualdad entre las claves a traves del m?todo equals de las mismas. Los
|
106 |
* objetos FileSource, DBSource y WFSSource tienen definido el m?todo equals
|
107 |
* de forma que devuelven true cuando dos objetos apuntan a la misma fuente
|
108 |
* de datos
|
109 |
*/
|
110 |
private static TreeMap sourceAdapter; |
111 |
|
112 |
/*
|
113 |
* Crea un RandomVectorialFile con el driver que se le pasa como par?metro y
|
114 |
* guard?ndose el nombre del fichero para realizar los accesos, la capa
|
115 |
* tendr? asociada la proyecci?n que se pasa como parametro tambi?n
|
116 |
*
|
117 |
* @param layerName Nombre de la capa. @param driverName Nombre del driver.
|
118 |
* @param f fichero. @param proj Proyecci?n.
|
119 |
*
|
120 |
* @return FLayer. @throws DriverException
|
121 |
*
|
122 |
* @throws DriverException @throws DriverIOException
|
123 |
*/
|
124 |
public static FLayer createLayer(String layerName, String driverName, |
125 |
File f, IProjection proj) throws LoadLayerException { |
126 |
// Se obtiene el driver que lee
|
127 |
DriverManager dm = getDM();
|
128 |
|
129 |
try {
|
130 |
Driver d = dm.getDriver(driverName);
|
131 |
|
132 |
if (d instanceof VectorialFileDriver) { |
133 |
return createLayer(layerName, (VectorialFileDriver) d, f, proj);
|
134 |
} |
135 |
} catch (DriverLoadException e) {
|
136 |
//hay un poco de lio sobre que excepciones se dejan subir
|
137 |
//arriba y que excepciones se quedan en LayerFactory
|
138 |
//(esto se debe a que queremos intentar recuperar ciertas capas)
|
139 |
//las excepciones de este metodo se dejan subir todas, puesto
|
140 |
//que las excepciones de los dos otros metodos createLayer si que
|
141 |
//se atrapan
|
142 |
DriverNotLoadedExceptionType exceptionType = |
143 |
new DriverNotLoadedExceptionType();
|
144 |
exceptionType.setDriverName(driverName); |
145 |
// DriverException exception =
|
146 |
// new DriverException(e, exceptionType);
|
147 |
throw new LoadLayerException(layerName,e); |
148 |
} |
149 |
|
150 |
return null; |
151 |
} |
152 |
|
153 |
/**
|
154 |
* It creates a FLayer (FLyrVect) which reads its data from a file driver,
|
155 |
* projected in the specified projection.
|
156 |
*
|
157 |
* @param layerName
|
158 |
* name of the layer
|
159 |
* @param d
|
160 |
* vectorial file driver to read layer's data
|
161 |
* @param f
|
162 |
* file associated to the driver
|
163 |
* @param proj
|
164 |
* layer projection
|
165 |
*
|
166 |
* @return FLayer new vectorial layer
|
167 |
* @throws LoadLayerException
|
168 |
*
|
169 |
* @throws DriverException
|
170 |
*/
|
171 |
public static FLayer createLayer(String layerName, VectorialFileDriver d, |
172 |
File f, IProjection proj)
|
173 |
|
174 |
/*throws DriverException*/ {
|
175 |
FLyrVect layer = new FLyrVect();
|
176 |
try {
|
177 |
// TODO Comprobar si hay un adaptador ya
|
178 |
VectorialFileAdapter adapter = new VectorialFileAdapter(f);
|
179 |
adapter.setDriver(d); |
180 |
|
181 |
//TODO azo: adapter needs a reference to projection and to spatial index (review)
|
182 |
adapter.setProjection(proj); |
183 |
|
184 |
layer.setName(layerName); |
185 |
|
186 |
// TODO Meter esto dentro de la comprobaci?n de si hay memoria
|
187 |
if (false) { |
188 |
} else {
|
189 |
layer.setSource(adapter); |
190 |
layer.setProjection(proj); |
191 |
} |
192 |
|
193 |
// try {
|
194 |
// Le asignamos tambi?n una legenda por defecto acorde con
|
195 |
// el tipo de shape que tenga. Tampoco s? si es aqu? el
|
196 |
// sitio adecuado, pero en fin....
|
197 |
if (d instanceof WithDefaultLegend) { |
198 |
WithDefaultLegend aux = (WithDefaultLegend) d; |
199 |
|
200 |
adapter.start(); |
201 |
layer.setLegend((IVectorialLegend) aux.getDefaultLegend()); |
202 |
|
203 |
ILabelingStrategy labeler = aux.getDefaultLabelingStrategy(); |
204 |
if (labeler instanceof AttrInTableLabeling) { |
205 |
((AttrInTableLabeling) labeler).setLayer(layer); |
206 |
} |
207 |
layer.setLabelingStrategy(labeler);layer.setIsLabeled(true); // TODO: ac? no s'hauria de detectar si t? etiquetes????? |
208 |
layer.setIsLabeled(true); // TODO: ac? no s'hauria de detectar si t? etiquetes????? |
209 |
adapter.stop(); |
210 |
|
211 |
} else {
|
212 |
layer.setLegend(LegendFactory.createSingleSymbolLegend(layer |
213 |
.getShapeType())); |
214 |
} |
215 |
|
216 |
// } catch (LegendLayerException e) {
|
217 |
// //this exception is caused in legend creation
|
218 |
// LegendDriverExceptionType excepType =
|
219 |
// new LegendDriverExceptionType("Error al construir la leyenda");
|
220 |
// //TODO Para hacer esto extensible tiene que usarse puntos
|
221 |
// //de extension, y comparar las clases de leyendas registradas
|
222 |
// VectorialLegend legend = (VectorialLegend)
|
223 |
// ((WithDefaultLegend)d).getDefaultLegend();
|
224 |
//
|
225 |
// excepType.setLegendLabelField(legend.getLabelField());
|
226 |
// excepType.setLegendHeightField(legend.getLabelHeightField());
|
227 |
// excepType.setLegendRotationField(legend.getLabelRotationField());
|
228 |
// DriverException exception = new DriverException(e, excepType);
|
229 |
// capa.setAvailable(false);
|
230 |
// capa.addError(exception);
|
231 |
//
|
232 |
// }
|
233 |
//
|
234 |
// /*
|
235 |
// * catch(DriverException e){
|
236 |
// * ESTO HAY QUE CAPTURARLO,DE FORMA QUE CREATELAYER NO LANCE NINGUNA
|
237 |
// * EXCEPCION (A NO SER QUE SEA IRRECUPERABLE)
|
238 |
// * }
|
239 |
// */
|
240 |
// catch(Exception e){
|
241 |
// GenericDriverExceptionType type = new GenericDriverExceptionType();
|
242 |
// DriverException exception = new DriverException(e, type);
|
243 |
// capa.setAvailable(false);
|
244 |
// capa.addError(exception);
|
245 |
// }
|
246 |
|
247 |
} catch (ReadDriverException e) {
|
248 |
layer.setAvailable(false);
|
249 |
layer.addError(e); |
250 |
} catch (LegendLayerException e) {
|
251 |
layer.setAvailable(false);
|
252 |
layer.addError(e); |
253 |
} |
254 |
return layer;
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* Creates a new vectorial layer from a generic layer (by generic whe mean
|
259 |
* that we dont know a priory its origin: file, memory, jdbc database, etc.
|
260 |
*
|
261 |
* @param layerName
|
262 |
* @param d
|
263 |
* @param proj
|
264 |
* @return
|
265 |
* @throws DriverException
|
266 |
*/
|
267 |
public static FLayer createLayer(String layerName, VectorialDriver d, |
268 |
IProjection proj) |
269 |
/*
|
270 |
throws DriverException
|
271 |
*/{
|
272 |
VectorialAdapter adapter = null;
|
273 |
if (d instanceof VectorialFileDriver) { |
274 |
adapter = new VectorialFileAdapter(((VectorialFileDriver) d)
|
275 |
.getFile()); |
276 |
} else if (d instanceof IVectorialDatabaseDriver) { |
277 |
adapter = new VectorialDBAdapter();
|
278 |
} else {
|
279 |
adapter = new VectorialDefaultAdapter();
|
280 |
} |
281 |
adapter.setDriver((VectorialDriver) d); |
282 |
//TODO azo:adapter needs a reference to the projection
|
283 |
adapter.setProjection(proj); |
284 |
|
285 |
FLyrVect layer = new FLyrVect();
|
286 |
layer.setName(layerName); |
287 |
|
288 |
layer.setSource(adapter); |
289 |
layer.setProjection(proj); |
290 |
|
291 |
try {
|
292 |
|
293 |
// Le asignamos tambi?n una legenda por defecto acorde con
|
294 |
// el tipo de shape que tenga. Tampoco s? si es aqu? el
|
295 |
// sitio adecuado, pero en fin....
|
296 |
if (d instanceof WithDefaultLegend) { |
297 |
WithDefaultLegend aux = (WithDefaultLegend) d; |
298 |
adapter.start(); |
299 |
layer.setLegend((IVectorialLegend) aux.getDefaultLegend()); |
300 |
|
301 |
ILabelingStrategy labeler = aux.getDefaultLabelingStrategy(); |
302 |
if (labeler instanceof AttrInTableLabeling) { |
303 |
((AttrInTableLabeling) labeler).setLayer(layer); |
304 |
} |
305 |
layer.setLabelingStrategy(labeler); |
306 |
|
307 |
layer.setIsLabeled(true); // TODO: ac? no s'hauria de detectar si t? etiquetes????? |
308 |
|
309 |
adapter.stop(); |
310 |
} else {
|
311 |
layer.setLegend(LegendFactory.createSingleSymbolLegend(layer |
312 |
.getShapeType())); |
313 |
} |
314 |
} catch (LegendLayerException e) {
|
315 |
// this exception is caused in legend creation
|
316 |
// LegendDriverExceptionType excepType =
|
317 |
// new LegendDriverExceptionType("error al construir la leyenda, campo no encontrado");
|
318 |
|
319 |
//TODO Para hacer esto extensible tiene que usarse puntos
|
320 |
//de extension, y comparar las clases de leyendas registradas
|
321 |
// VectorialLegend legend = (VectorialLegend)
|
322 |
// ((WithDefaultLegend)d).getDefaultLegend();
|
323 |
// excepType.setDriverName(d.getName());
|
324 |
// excepType.setLegendLabelField(legend.getLabelField());
|
325 |
// excepType.setLegendHeightField(legend.getLabelHeightField());
|
326 |
// excepType.setLegendRotationField(legend.getLabelRotationField());
|
327 |
|
328 |
// DriverException exception = new DriverException(e, excepType);
|
329 |
layer.setAvailable(false);
|
330 |
layer.addError(e); |
331 |
// }
|
332 |
// catch(Exception e){
|
333 |
// GenericDriverExceptionType type = new GenericDriverExceptionType();
|
334 |
// DriverException exception = new DriverException(e, type);
|
335 |
// capa.setAvailable(false);
|
336 |
// capa.addError(exception);
|
337 |
// } catch (UnsupportedVersionDriverException e) {
|
338 |
// capa.setAvailable(false);
|
339 |
// capa.addError(e);
|
340 |
// } catch (InitializeDriverException e) {
|
341 |
// capa.setAvailable(false);
|
342 |
// capa.addError(e);
|
343 |
} catch (ReadDriverException e) {
|
344 |
layer.setAvailable(false);
|
345 |
layer.addError(e); |
346 |
} |
347 |
|
348 |
return layer;
|
349 |
} |
350 |
|
351 |
public static FLayer createArcSDELayer(String layerName, |
352 |
IVectorialDatabaseDriver driver, IProjection proj) { |
353 |
// throw new UnsupportedOperationException();
|
354 |
FLyrVect layer = new FLyrVect();
|
355 |
VectorialAdapter adapter = new VectorialDBAdapter();
|
356 |
adapter.setDriver(driver); |
357 |
adapter.setProjection(proj); |
358 |
|
359 |
layer.setName(layerName); |
360 |
layer.setSource(adapter); |
361 |
layer.setProjection(proj); |
362 |
try {
|
363 |
if (driver instanceof WithDefaultLegend) { |
364 |
WithDefaultLegend aux = (WithDefaultLegend) driver; |
365 |
adapter.start(); |
366 |
layer.setLegend((IVectorialLegend) aux.getDefaultLegend()); |
367 |
|
368 |
ILabelingStrategy labeler = aux.getDefaultLabelingStrategy(); |
369 |
if (labeler instanceof AttrInTableLabeling) { |
370 |
((AttrInTableLabeling) labeler).setLayer(layer); |
371 |
} |
372 |
layer.setLabelingStrategy(labeler);layer.setIsLabeled(true); // TODO: ac? no s'hauria de detectar si t? etiquetes????? |
373 |
|
374 |
adapter.stop(); |
375 |
} else {
|
376 |
layer.setLegend(LegendFactory.createSingleSymbolLegend(layer |
377 |
.getShapeType())); |
378 |
} |
379 |
} catch (LegendLayerException e) {
|
380 |
throw new UnsupportedOperationException(e.getMessage()); |
381 |
} catch (InitializeDriverException e) {
|
382 |
throw new UnsupportedOperationException(e.getMessage()); |
383 |
} catch (ReadDriverException e) {
|
384 |
throw new UnsupportedOperationException(e.getMessage()); |
385 |
} |
386 |
|
387 |
try {
|
388 |
layer.setLegend(LegendFactory.createSingleSymbolLegend(layer |
389 |
.getShapeType())); |
390 |
} catch (LegendLayerException e) {
|
391 |
e.printStackTrace(); |
392 |
} catch (ReadDriverException e) {
|
393 |
e.printStackTrace(); |
394 |
} |
395 |
return layer;
|
396 |
} |
397 |
|
398 |
/**
|
399 |
* Crea un RandomVectorialWFS con el driver que se le pasa como par?metro y
|
400 |
* guard?ndose la URL del servidor que se pasa como par?metro
|
401 |
*
|
402 |
* @param driver
|
403 |
* @param host
|
404 |
* @param port
|
405 |
* @param user
|
406 |
* @param password
|
407 |
* @param dbName
|
408 |
* @param tableName
|
409 |
* @param proj
|
410 |
*
|
411 |
* @return Capa creada.
|
412 |
*
|
413 |
* @throws UnsupportedOperationException
|
414 |
*/
|
415 |
public static FLayer createLayer(IVectorialDatabaseDriver driver, |
416 |
String host, int port, String user, String password, String dbName, |
417 |
String tableName, IProjection proj) {
|
418 |
throw new UnsupportedOperationException(); |
419 |
} |
420 |
|
421 |
public static FLayer createDBLayer(IVectorialDatabaseDriver driver, |
422 |
String layerName, IProjection proj) {
|
423 |
|
424 |
FLyrVect layer = new FLyrVect();
|
425 |
|
426 |
layer.setName(layerName); |
427 |
VectorialDBAdapter dbAdapter = new VectorialDBAdapter();
|
428 |
dbAdapter.setDriver(driver); |
429 |
dbAdapter.setProjection(proj);//adapter needs also a ref to prj. review (azo)
|
430 |
|
431 |
layer.setSource(dbAdapter); |
432 |
layer.setProjection(proj); |
433 |
try {
|
434 |
if (driver instanceof WithDefaultLegend) { |
435 |
WithDefaultLegend aux = (WithDefaultLegend) driver; |
436 |
dbAdapter.start(); |
437 |
layer.setLegend((IVectorialLegend) aux.getDefaultLegend()); |
438 |
|
439 |
ILabelingStrategy labeler = aux.getDefaultLabelingStrategy(); |
440 |
if (labeler instanceof AttrInTableLabeling) { |
441 |
((AttrInTableLabeling) labeler).setLayer(layer); |
442 |
} |
443 |
layer.setLabelingStrategy(labeler);layer.setIsLabeled(true); // TODO: ac? no s'hauria de detectar si t? etiquetes????? |
444 |
|
445 |
layer.setIsLabeled(true); // TODO: ac? no s'hauria de detectar si t? etiquetes????? |
446 |
|
447 |
dbAdapter.stop(); |
448 |
} else {
|
449 |
layer.setLegend(LegendFactory.createSingleSymbolLegend(layer |
450 |
.getShapeType())); |
451 |
} |
452 |
} catch (LegendLayerException e) {
|
453 |
LegendDriverExceptionType excepType = |
454 |
new LegendDriverExceptionType("Error al construir la leyenda, campo no encontrado"); |
455 |
//TODO Para hacer esto extensible tiene que usarse puntos
|
456 |
//de extension, y comparar las clases de leyendas registradas
|
457 |
// IVectorialLegend legend = (IVectorialLegend)
|
458 |
// ((WithDefaultLegend)driver).getDefaultLegend();
|
459 |
//
|
460 |
// excepType.setLegendLabelField(legend.getLabelField());
|
461 |
// excepType.setLegendHeightField(legend.getLabelHeightField());
|
462 |
// excepType.setLegendRotationField(legend.getLabelRotationField());
|
463 |
// DriverException exception = new DriverException(e, excepType);
|
464 |
layer.setAvailable(false);
|
465 |
layer.addError(null);
|
466 |
return layer;
|
467 |
// throw new UnsupportedOperationException(e.getMessage());
|
468 |
} catch (Exception e) { |
469 |
// ExceptionDescription excepType = new GenericDriverExceptionType();
|
470 |
// DriverException exception = new DriverException(e, excepType);
|
471 |
layer.addError(null);
|
472 |
layer.setAvailable(false);
|
473 |
return layer;
|
474 |
} |
475 |
|
476 |
return layer;
|
477 |
|
478 |
} |
479 |
|
480 |
/**
|
481 |
* @param driver
|
482 |
* @param layerName
|
483 |
* @param object
|
484 |
* @return
|
485 |
* @throws SQLException
|
486 |
* @throws DriverIOException
|
487 |
* @throws IOException
|
488 |
* @throws DriverLoadException
|
489 |
* @throws com.hardcode.gdbms.engine.data.driver.DriverException
|
490 |
* @throws NoSuchTableException
|
491 |
* @throws ClassNotFoundException
|
492 |
* @throws ReadDriverException
|
493 |
* @throws IOException
|
494 |
* @throws WriteDriverException
|
495 |
* @throws
|
496 |
*/
|
497 |
public static FLayer createDisconnectedDBLayer(IVectorialJDBCDriver driver, |
498 |
String layerName, IProjection proj, ProgressListener listener)
|
499 |
throws DBException,
|
500 |
DriverLoadException, NoSuchTableException, |
501 |
ClassNotFoundException, ReadDriverException, IOException, WriteDriverException { |
502 |
VectorialDisconnectedDBAdapter dbAdapter = new VectorialDisconnectedDBAdapter();
|
503 |
dbAdapter.setDriver(driver); |
504 |
dbAdapter.setProjection(proj); |
505 |
DataSource ds = dbAdapter.getRecordset();
|
506 |
ds.start(); |
507 |
String database = dataSourceFactory.getTempFile();
|
508 |
String[] fieldNames = new String[ds.getFieldCount() + 1]; |
509 |
System.arraycopy(ds.getFieldNames(), 0, fieldNames, 1, ds |
510 |
.getFieldCount()); |
511 |
fieldNames[0] = "the_geom"; |
512 |
int[] types = new int[fieldNames.length]; |
513 |
types[0] = Types.BINARY; |
514 |
for (int i = 1; i < types.length; i++) { |
515 |
types[i] = ds.getFieldType(i - 1);
|
516 |
} |
517 |
String dsName=null; |
518 |
try {
|
519 |
dsName = dataSourceFactory.createTable(database, |
520 |
ds.getPKNames(), fieldNames, types); |
521 |
} catch (ReadDriverException e) {
|
522 |
// TODO Auto-generated catch block
|
523 |
e.printStackTrace(); |
524 |
} catch (SQLException e) { |
525 |
throw new DBException(e); |
526 |
} |
527 |
|
528 |
DBLayerDefinition lyrDef = new DBLayerDefinition();
|
529 |
lyrDef.setTableName(dsName); |
530 |
lyrDef.setName(layerName); |
531 |
lyrDef.setFieldNames(ds.getFieldNames()); |
532 |
lyrDef.setFieldGeometry("the_geom");
|
533 |
lyrDef.setFieldID(ds.getPKNames()[0]);
|
534 |
lyrDef.setClassToInstantiate("org.hsqldb.jdbcDriver");
|
535 |
|
536 |
dataSourceFactory.addDBDataSourceByTable(dsName, null, 0, "sa", "", |
537 |
database, dsName, "GDBMS HSQLDB Transactional driver");
|
538 |
DataSource local = dataSourceFactory.createRandomDataSource(dsName,
|
539 |
DataSourceFactory.MANUAL_OPENING); |
540 |
local.start(); |
541 |
DataWare dw = local |
542 |
.getDataWare(DataSourceFactory.DATA_WARE_COHERENT_ROW_ORDER); |
543 |
dw.start(); |
544 |
long t1 = System.currentTimeMillis(); |
545 |
dw.beginTrans(); |
546 |
|
547 |
if (listener == null) { |
548 |
listener = new ProgressListener() {
|
549 |
/**
|
550 |
* @see com.iver.cit.gvsig.fmap.ProgressListener#progress(int)
|
551 |
*/
|
552 |
public void progress(int n) { |
553 |
// do nothing
|
554 |
} |
555 |
}; |
556 |
} |
557 |
|
558 |
for (int i = 0; i < dbAdapter.getShapeCount(); i++) { |
559 |
Value[] row = new Value[ds.getFieldCount() + 1]; |
560 |
|
561 |
byte[] bytes = dbAdapter.getShape(i).toWKB(); |
562 |
row[0] = ValueFactory.createValue(bytes);
|
563 |
|
564 |
for (int j = 0; j < ds.getFieldCount(); j++) { |
565 |
row[j + 1] = ds.getFieldValue(i, j);
|
566 |
} |
567 |
|
568 |
dw.insertFilledRow(row); |
569 |
listener.progress(100 * i / dbAdapter.getShapeCount());
|
570 |
} |
571 |
|
572 |
long t2 = System.currentTimeMillis(); |
573 |
dw.commitTrans(); |
574 |
long t3 = System.currentTimeMillis(); |
575 |
System.out.println((t2 - t1) + " - " + (t3 - t2)); |
576 |
dw.stop(); |
577 |
local.stop(); |
578 |
ds.stop(); |
579 |
IVectorialJDBCDriver cacheDriver = (IVectorialJDBCDriver) LayerFactory |
580 |
.getDM().getDriver("HSQLDB Driver");
|
581 |
Class.forName("org.hsqldb.jdbcDriver"); |
582 |
|
583 |
cacheDriver.setData(ConnectionFactory.createConnection( |
584 |
"jdbc:hsqldb:file:" + database, "sa", ""), lyrDef); |
585 |
cacheDriver.setWorkingArea(driver.getWorkingArea()); |
586 |
return createDBLayer(cacheDriver, layerName, proj);
|
587 |
} |
588 |
|
589 |
/**
|
590 |
* Devuelve el DriverManager.
|
591 |
*
|
592 |
* @return DriverManager.
|
593 |
*/
|
594 |
public static DriverManager getDM() { |
595 |
initializeDriverManager(); |
596 |
|
597 |
return driverManager;
|
598 |
} |
599 |
|
600 |
/**
|
601 |
* Devuelve el WriterManager.
|
602 |
*
|
603 |
* @return WriterManager.
|
604 |
*/
|
605 |
public static WriterManager getWM() { |
606 |
initializeWriterManager(); |
607 |
|
608 |
return writerManager;
|
609 |
} |
610 |
|
611 |
/**
|
612 |
* Inicializa el DriverManager.
|
613 |
*/
|
614 |
private static void initializeDriverManager() { |
615 |
if (driverManager == null) { |
616 |
driverManager = new DriverManager(); |
617 |
driverManager.loadDrivers(new File(LayerFactory.driversPath)); |
618 |
|
619 |
Throwable[] failures = driverManager.getLoadFailures(); |
620 |
|
621 |
for (int i = 0; i < failures.length; i++) { |
622 |
logger.error("", failures[i]);
|
623 |
} |
624 |
|
625 |
getDataSourceFactory().setDriverManager(driverManager); |
626 |
getDataSourceFactory().initialize(); |
627 |
QueryManager.registerQuery(new ArcJoin());
|
628 |
} |
629 |
} |
630 |
|
631 |
/**
|
632 |
* Inicializa el DriverManager.
|
633 |
*/
|
634 |
private static void initializeWriterManager() { |
635 |
if (writerManager == null) { |
636 |
writerManager = new WriterManager();
|
637 |
writerManager.loadWriters(new File(LayerFactory.writersPath)); |
638 |
|
639 |
Throwable[] failures = writerManager.getLoadFailures(); |
640 |
|
641 |
for (int i = 0; i < failures.length; i++) { |
642 |
logger.error("", failures[i]);
|
643 |
} |
644 |
|
645 |
getDataSourceFactory().setWriterManager(writerManager); |
646 |
getDataSourceFactory().initialize(); |
647 |
// QueryManager.registerQuery(new ArcJoin());
|
648 |
} |
649 |
} |
650 |
|
651 |
/**
|
652 |
* sets drivers Directory
|
653 |
*
|
654 |
* @param path
|
655 |
*/
|
656 |
public static void setDriversPath(String path) { |
657 |
LayerFactory.driversPath = path; |
658 |
initializeDriverManager(); |
659 |
} |
660 |
|
661 |
/**
|
662 |
* sets writers Directory
|
663 |
*
|
664 |
* @param path
|
665 |
*/
|
666 |
public static void setWritersPath(String path) { |
667 |
LayerFactory.writersPath = path; |
668 |
initializeWriterManager(); |
669 |
} |
670 |
|
671 |
/**
|
672 |
* @return Returns the dataSourceFactory.
|
673 |
*/
|
674 |
public static DataSourceFactory getDataSourceFactory() { |
675 |
if (dataSourceFactory == null) { |
676 |
dataSourceFactory = new DataSourceFactory();
|
677 |
} |
678 |
return dataSourceFactory;
|
679 |
} |
680 |
|
681 |
public static void initialize() { |
682 |
initializeDriverManager(); |
683 |
initializeWriterManager(); |
684 |
} |
685 |
} |