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