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