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