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