svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / JDBCHelperBase.java @ 43377
History | View | Annotate | Download (14 KB)
1 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi; |
---|---|---|---|
2 | |||
3 | import org.gvsig.fmap.dal.store.jdbc2.impl.ResulSetControlerBase; |
||
4 | import org.gvsig.fmap.dal.store.jdbc2.spi.operations.OperationsFactoryBase; |
||
5 | import java.sql.Connection; |
||
6 | import java.sql.ResultSet; |
||
7 | 43377 | jjdelcerro | import java.sql.SQLException; |
8 | import org.apache.commons.dbcp.BasicDataSource; |
||
9 | import org.apache.commons.lang3.BooleanUtils; |
||
10 | 43020 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
11 | import org.cresques.cts.IProjection; |
||
12 | 43114 | jjdelcerro | import org.gvsig.fmap.crs.CRSFactory; |
13 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.DataTypes; |
14 | import org.gvsig.fmap.dal.exception.DataException; |
||
15 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
16 | import org.gvsig.fmap.dal.ExpressionBuilder.GeometrySupportType; |
||
17 | import static org.gvsig.fmap.dal.ExpressionBuilder.GeometrySupportType.EWKB; |
||
18 | import static org.gvsig.fmap.dal.ExpressionBuilder.GeometrySupportType.WKB; |
||
19 | import static org.gvsig.fmap.dal.ExpressionBuilder.GeometrySupportType.WKT; |
||
20 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
21 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
22 | |||
23 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
24 | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
||
25 | import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
||
26 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
27 | import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
||
28 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
29 | import org.gvsig.fmap.dal.store.db.DBHelper; |
||
30 | import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
||
31 | import org.gvsig.fmap.dal.store.jdbc.JDBCLibrary; |
||
32 | import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
||
33 | import org.gvsig.fmap.dal.store.jdbc.JDBCResource; |
||
34 | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
||
35 | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
||
36 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCCantFetchValueException; |
||
37 | 43377 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
38 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
39 | import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
||
40 | import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider; |
||
41 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
42 | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
||
43 | import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler; |
||
44 | import org.gvsig.fmap.dal.store.jdbc2.ResulSetControler.ResultSetEntry; |
||
45 | import org.gvsig.fmap.geom.Geometry; |
||
46 | import org.gvsig.fmap.geom.GeometryLocator; |
||
47 | import org.gvsig.fmap.geom.GeometryManager; |
||
48 | import org.gvsig.tools.dispose.impl.AbstractDisposable; |
||
49 | import org.gvsig.tools.exception.BaseException; |
||
50 | 43377 | jjdelcerro | import org.gvsig.tools.exception.NotYetImplemented; |
51 | 43020 | jjdelcerro | import org.slf4j.Logger; |
52 | import org.slf4j.LoggerFactory; |
||
53 | |||
54 | public class JDBCHelperBase extends AbstractDisposable implements ResourceConsumer, JDBCHelper { |
||
55 | |||
56 | private static final boolean ALLOW_AUTOMATIC_VALUES = true; |
||
57 | private static final String QUOTE_FOR_USE_IN_IDENTIFIERS = "\""; |
||
58 | private static final String QUOTE_FOR_USE_IN_STRINGS = "'"; |
||
59 | |||
60 | private static final Logger logger = LoggerFactory.getLogger(JDBCHelperBase.class); |
||
61 | |||
62 | private ResulSetControler resulSetControler = null; |
||
63 | |||
64 | // Quien ha creado este helper.
|
||
65 | // Se le reenviaran las notificaciones del recurso asociado al helper.
|
||
66 | private ResourceConsumer helperClient = null; |
||
67 | |||
68 | private GeometryManager geometryManager = null; |
||
69 | |||
70 | 43035 | jjdelcerro | private JDBCConnectionParameters connectionParameters;
|
71 | 43020 | jjdelcerro | |
72 | private JDBCStoreProvider store;
|
||
73 | |||
74 | protected OperationsFactory operationsFactory = null; |
||
75 | |||
76 | 43355 | jjdelcerro | protected JDBCSRSs srss;
|
77 | |||
78 | 43020 | jjdelcerro | public JDBCHelperBase(JDBCConnectionParameters connectionParameters) {
|
79 | this.connectionParameters = connectionParameters;
|
||
80 | 43355 | jjdelcerro | |
81 | // If a particular treatment is required to convert SRS to the
|
||
82 | // BBDD format, overwrite JDBCSRSsBase and use it instead of JDBCSRSsDumb.
|
||
83 | this.srss = new JDBCSRSsDumb(this); |
||
84 | 43020 | jjdelcerro | } |
85 | |||
86 | protected void initialize( |
||
87 | ResourceConsumer helperClient, |
||
88 | JDBCConnectionParameters connectionParameters, |
||
89 | JDBCStoreProvider store |
||
90 | ) { |
||
91 | 43093 | jjdelcerro | this.store = store;
|
92 | 43020 | jjdelcerro | this.helperClient = helperClient;
|
93 | this.connectionParameters = connectionParameters;
|
||
94 | initializeResource(connectionParameters); |
||
95 | } |
||
96 | |||
97 | protected String getResourceType() { |
||
98 | return JDBCResource.NAME;
|
||
99 | } |
||
100 | |||
101 | @Override
|
||
102 | public String getProviderName() { |
||
103 | return JDBCLibrary.NAME;
|
||
104 | } |
||
105 | |||
106 | @Override
|
||
107 | public GeometrySupportType getGeometrySupportType() {
|
||
108 | // El proveedor generico de JDBC guadara las geoemtrias como WKT
|
||
109 | return GeometrySupportType.WKT;
|
||
110 | } |
||
111 | |||
112 | @Override
|
||
113 | public boolean hasSpatialFunctions() { |
||
114 | // Por defecto el proveedor generico de JDBC asume que la BBDD no
|
||
115 | // tiene soporte para funciones espaciales.
|
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | @Override
|
||
120 | public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
||
121 | // Como va a guardar las geometrias en WKT, puede escribirlas todas.
|
||
122 | return true; |
||
123 | } |
||
124 | |||
125 | @Override
|
||
126 | public JDBCSQLBuilderBase createSQLBuilder() {
|
||
127 | return new JDBCSQLBuilderBase(); |
||
128 | } |
||
129 | |||
130 | @Override
|
||
131 | public String getQuoteForIdentifiers() { |
||
132 | return QUOTE_FOR_USE_IN_IDENTIFIERS;
|
||
133 | } |
||
134 | |||
135 | @Override
|
||
136 | public boolean allowAutomaticValues() { |
||
137 | return ALLOW_AUTOMATIC_VALUES;
|
||
138 | } |
||
139 | |||
140 | @Override
|
||
141 | public boolean supportOffsetInSelect() { |
||
142 | // Asumimos que la BBDD soporta OFFSET
|
||
143 | return true; |
||
144 | } |
||
145 | |||
146 | @Override
|
||
147 | public String getQuoteForStrings() { |
||
148 | return QUOTE_FOR_USE_IN_STRINGS;
|
||
149 | } |
||
150 | |||
151 | @Override
|
||
152 | public OperationsFactory getOperations() {
|
||
153 | if (this.operationsFactory== null) { |
||
154 | this.operationsFactory = new OperationsFactoryBase(this); |
||
155 | } |
||
156 | return operationsFactory;
|
||
157 | } |
||
158 | |||
159 | protected void initializeResource(JDBCConnectionParameters params) { |
||
160 | // Object[] resourceParams = new Object[]{
|
||
161 | // params.getUrl(),
|
||
162 | // params.getHost(),
|
||
163 | // params.getPort(),
|
||
164 | // params.getDBName(),
|
||
165 | // params.getUser(),
|
||
166 | // params.getPassword(),
|
||
167 | // params.getJDBCDriverClassName()
|
||
168 | // };
|
||
169 | //
|
||
170 | // try {
|
||
171 | // ResourceManagerProviderServices manager
|
||
172 | // = (ResourceManagerProviderServices) DALLocator.getResourceManager();
|
||
173 | // JDBCResource resource = (JDBCResource) manager.createAddResource(
|
||
174 | // this.getResourceType(),
|
||
175 | // resourceParams
|
||
176 | // );
|
||
177 | // this.resource = resource;
|
||
178 | // this.resource.addConsumer(this);
|
||
179 | // } catch (InitializeException ex) {
|
||
180 | // logger.debug("Can't initialize resource (" + ArrayUtils.toString(resourceParams) + ").", ex);
|
||
181 | // throw new RuntimeException("Can't initialize resource (" + ArrayUtils.toString(resourceParams) + ").", ex);
|
||
182 | // }
|
||
183 | |||
184 | } |
||
185 | |||
186 | @Override
|
||
187 | public String getSourceId() { |
||
188 | return this.store.getSourceId(); |
||
189 | } |
||
190 | |||
191 | @Override
|
||
192 | public JDBCResource getResource() {
|
||
193 | return null; |
||
194 | // return this.resource;
|
||
195 | } |
||
196 | |||
197 | @Override
|
||
198 | public Connection getConnection() throws AccessResourceException { |
||
199 | 43377 | jjdelcerro | throw new NotYetImplemented(); |
200 | 43020 | jjdelcerro | } |
201 | |||
202 | @Override
|
||
203 | 43377 | jjdelcerro | public Connection getConnectionWritable() throws AccessResourceException { |
204 | return this.getConnection(); |
||
205 | } |
||
206 | |||
207 | @Override
|
||
208 | 43035 | jjdelcerro | public String getConnectionURL() { |
209 | return null; |
||
210 | } |
||
211 | |||
212 | 43358 | jjdelcerro | @Override
|
213 | 43035 | jjdelcerro | public JDBCConnectionParameters getConnectionParameters() {
|
214 | return connectionParameters;
|
||
215 | } |
||
216 | |||
217 | @Override
|
||
218 | 43020 | jjdelcerro | public void closeConnection(Connection connection) { |
219 | 43377 | jjdelcerro | if( connection != null ) { |
220 | logger.debug("Clossing connection "+connection.hashCode());
|
||
221 | try {
|
||
222 | connection.close(); |
||
223 | } catch(Exception ex) { |
||
224 | logger.warn("Can't close connection.", ex);
|
||
225 | } |
||
226 | 43020 | jjdelcerro | } |
227 | 43377 | jjdelcerro | } |
228 | 43020 | jjdelcerro | |
229 | @Override
|
||
230 | protected void doDispose() throws BaseException { |
||
231 | JDBCUtils.closeQuietly(this);
|
||
232 | } |
||
233 | |||
234 | @Override
|
||
235 | public void close() throws Exception { |
||
236 | // this.resource.removeConsumer(this);
|
||
237 | JDBCUtils.closeQuietly(this.resulSetControler);
|
||
238 | // this.resource = null;
|
||
239 | this.resulSetControler = null; |
||
240 | } |
||
241 | |||
242 | @Override
|
||
243 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
244 | return this.helperClient.closeResourceRequested(resource); |
||
245 | } |
||
246 | |||
247 | @Override
|
||
248 | public void resourceChanged(ResourceProvider resource) { |
||
249 | this.helperClient.resourceChanged(resource);
|
||
250 | } |
||
251 | |||
252 | @Override
|
||
253 | public GeometryManager getGeometryManager() {
|
||
254 | if (this.geometryManager == null) { |
||
255 | this.geometryManager = GeometryLocator.getGeometryManager();
|
||
256 | } |
||
257 | return this.geometryManager; |
||
258 | } |
||
259 | |||
260 | @Override
|
||
261 | public ResulSetControler getResulSetControler() {
|
||
262 | if (this.resulSetControler == null) { |
||
263 | this.resulSetControler = new ResulSetControlerBase(this); |
||
264 | } |
||
265 | return this.resulSetControler; |
||
266 | } |
||
267 | |||
268 | @Override
|
||
269 | public void fetchFeature(FeatureProvider feature, ResultSetEntry rs) throws DataException { |
||
270 | 43358 | jjdelcerro | fetchFeature(feature, rs.get(), rs.getColumns()); |
271 | 43020 | jjdelcerro | } |
272 | |||
273 | @Override
|
||
274 | 43358 | jjdelcerro | public void fetchFeature(FeatureProvider feature, ResultSet rs, FeatureAttributeDescriptor[] columns) throws DataException { |
275 | 43020 | jjdelcerro | try {
|
276 | Object value;
|
||
277 | 43358 | jjdelcerro | for (int index = 0; index < columns.length; index++) { |
278 | FeatureAttributeDescriptor column = columns[index]; |
||
279 | switch (column.getType()) {
|
||
280 | 43020 | jjdelcerro | case DataTypes.GEOMETRY:
|
281 | value = this.getGeometryFromColumn(rs, index + 1); |
||
282 | break;
|
||
283 | default:
|
||
284 | value = rs.getObject(index + 1);
|
||
285 | } |
||
286 | 43358 | jjdelcerro | feature.set(column.getIndex(), value); |
287 | 43020 | jjdelcerro | } |
288 | } catch (Exception ex) { |
||
289 | throw new JDBCCantFetchValueException(ex); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | @Override
|
||
294 | public Geometry getGeometryFromColumn(ResultSetEntry rs, int index) throws DataException { |
||
295 | return getGeometryFromColumn(rs.get(), index);
|
||
296 | } |
||
297 | |||
298 | @Override
|
||
299 | public Geometry getGeometryFromColumn(ResultSet rs, int index) throws DataException { |
||
300 | try {
|
||
301 | Object value;
|
||
302 | switch (this.getGeometrySupportType()) { |
||
303 | 43358 | jjdelcerro | case NATIVE:
|
304 | 43020 | jjdelcerro | case WKB:
|
305 | value = rs.getBytes(index); |
||
306 | if (value == null) { |
||
307 | return null; |
||
308 | } |
||
309 | return this.getGeometryManager().createFrom((byte[]) value); |
||
310 | |||
311 | case EWKB:
|
||
312 | value = rs.getBytes(index); |
||
313 | if (value == null) { |
||
314 | return null; |
||
315 | } |
||
316 | return this.getGeometryManager().createFrom((byte[]) value); |
||
317 | case WKT:
|
||
318 | default:
|
||
319 | value = rs.getString(index); |
||
320 | if (value == null) { |
||
321 | return null; |
||
322 | } |
||
323 | return this.getGeometryManager().createFrom((String) value); |
||
324 | |||
325 | } |
||
326 | } catch (Exception ex) { |
||
327 | throw new JDBCCantFetchValueException(ex); |
||
328 | } |
||
329 | } |
||
330 | |||
331 | @Override
|
||
332 | public FeatureProvider createFeature(FeatureType featureType) throws DataException { |
||
333 | return this.store.getStoreServices().createDefaultFeatureProvider(featureType); |
||
334 | } |
||
335 | |||
336 | @Override
|
||
337 | public boolean useSubquery() { |
||
338 | if( this.store == null ) { |
||
339 | return false; |
||
340 | } |
||
341 | return !StringUtils.isEmpty(this.store.getParameters().getSQL()); |
||
342 | } |
||
343 | |||
344 | @Override
|
||
345 | public int getSRSCode(IProjection crs) { |
||
346 | 43355 | jjdelcerro | String databaseCode = this.srss.getDatabaseCode(crs.getAbrev()); |
347 | return Integer.parseInt(databaseCode); |
||
348 | 43020 | jjdelcerro | } |
349 | 43114 | jjdelcerro | |
350 | @Override
|
||
351 | public IProjection getProjectionFromSRSId(int srsid) { |
||
352 | 43355 | jjdelcerro | return getProjectionFromDatabaseCode(String.valueOf(srsid)); |
353 | } |
||
354 | |||
355 | @Override
|
||
356 | public String getDatabaseCodeFromProyection(IProjection proj) { |
||
357 | return this.srss.getDatabaseCode(proj.getAbrev()); |
||
358 | } |
||
359 | |||
360 | @Override
|
||
361 | public IProjection getProjectionFromDatabaseCode(String databaseCode) { |
||
362 | 43377 | jjdelcerro | if( StringUtils.trimToEmpty(databaseCode).equals("0") ) { |
363 | return null; |
||
364 | } |
||
365 | 43355 | jjdelcerro | String abbrev = this.srss.getApplicationAbbrev(databaseCode); |
366 | if( StringUtils.isEmpty(abbrev) ) {
|
||
367 | 43114 | jjdelcerro | return null; |
368 | } |
||
369 | 43355 | jjdelcerro | IProjection proj = CRSFactory.getCRS(abbrev); |
370 | 43114 | jjdelcerro | return proj;
|
371 | } |
||
372 | 43020 | jjdelcerro | |
373 | @Override
|
||
374 | public JDBCStoreProvider createProvider(
|
||
375 | JDBCStoreParameters parameters, |
||
376 | DataStoreProviderServices providerServices |
||
377 | ) throws InitializeException {
|
||
378 | |||
379 | JDBCStoreProviderBase store = new JDBCStoreProviderBase(
|
||
380 | parameters, |
||
381 | providerServices, |
||
382 | DBHelper.newMetadataContainer(JDBCLibrary.NAME), |
||
383 | this
|
||
384 | ); |
||
385 | this.initialize(store, parameters, store);
|
||
386 | return store;
|
||
387 | } |
||
388 | |||
389 | @Override
|
||
390 | public JDBCServerExplorer createServerExplorer(
|
||
391 | JDBCServerExplorerParameters parameters, |
||
392 | DataServerExplorerProviderServices providerServices |
||
393 | ) throws InitializeException {
|
||
394 | |||
395 | JDBCServerExplorer explorer = new JDBCServerExplorerBase(
|
||
396 | parameters, |
||
397 | providerServices, |
||
398 | this
|
||
399 | ); |
||
400 | this.initialize(explorer, parameters, null); |
||
401 | return explorer;
|
||
402 | } |
||
403 | |||
404 | @Override
|
||
405 | public JDBCNewStoreParameters createNewStoreParameters() {
|
||
406 | return new JDBCNewStoreParameters(); |
||
407 | } |
||
408 | |||
409 | @Override
|
||
410 | public JDBCStoreParameters createOpenStoreParameters() {
|
||
411 | return new JDBCStoreParameters(); |
||
412 | } |
||
413 | |||
414 | 43358 | jjdelcerro | @Override
|
415 | 43020 | jjdelcerro | public JDBCServerExplorerParameters createServerExplorerParameters() {
|
416 | return new JDBCServerExplorerParameters(); |
||
417 | } |
||
418 | |||
419 | @Override
|
||
420 | public String getSourceId(JDBCStoreParameters parameters) { |
||
421 | return parameters.getHost() + ":" + |
||
422 | parameters.getDBName() + ":" +
|
||
423 | parameters.getSchema()+ ":" +
|
||
424 | parameters.tableID(); |
||
425 | } |
||
426 | |||
427 | } |