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 @ 43114
History | View | Annotate | Download (12.9 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 |
public JDBCHelperBase(JDBCConnectionParameters connectionParameters) {
|
74 |
this.connectionParameters = connectionParameters;
|
75 |
} |
76 |
|
77 |
protected void initialize( |
78 |
ResourceConsumer helperClient, |
79 |
JDBCConnectionParameters connectionParameters, |
80 |
JDBCStoreProvider store |
81 |
) { |
82 |
this.store = store;
|
83 |
this.helperClient = helperClient;
|
84 |
this.connectionParameters = connectionParameters;
|
85 |
initializeResource(connectionParameters); |
86 |
} |
87 |
|
88 |
protected String getResourceType() { |
89 |
return JDBCResource.NAME;
|
90 |
} |
91 |
|
92 |
@Override
|
93 |
public String getProviderName() { |
94 |
return JDBCLibrary.NAME;
|
95 |
} |
96 |
|
97 |
@Override
|
98 |
public GeometrySupportType getGeometrySupportType() {
|
99 |
// El proveedor generico de JDBC guadara las geoemtrias como WKT
|
100 |
return GeometrySupportType.WKT;
|
101 |
} |
102 |
|
103 |
@Override
|
104 |
public boolean hasSpatialFunctions() { |
105 |
// Por defecto el proveedor generico de JDBC asume que la BBDD no
|
106 |
// tiene soporte para funciones espaciales.
|
107 |
return false; |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
112 |
// Como va a guardar las geometrias en WKT, puede escribirlas todas.
|
113 |
return true; |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public JDBCSQLBuilderBase createSQLBuilder() {
|
118 |
return new JDBCSQLBuilderBase(); |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public String getQuoteForIdentifiers() { |
123 |
return QUOTE_FOR_USE_IN_IDENTIFIERS;
|
124 |
} |
125 |
|
126 |
@Override
|
127 |
public boolean allowAutomaticValues() { |
128 |
return ALLOW_AUTOMATIC_VALUES;
|
129 |
} |
130 |
|
131 |
@Override
|
132 |
public boolean supportOffsetInSelect() { |
133 |
// Asumimos que la BBDD soporta OFFSET
|
134 |
return true; |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public String getQuoteForStrings() { |
139 |
return QUOTE_FOR_USE_IN_STRINGS;
|
140 |
} |
141 |
|
142 |
@Override
|
143 |
public OperationsFactory getOperations() {
|
144 |
if (this.operationsFactory== null) { |
145 |
this.operationsFactory = new OperationsFactoryBase(this); |
146 |
} |
147 |
return operationsFactory;
|
148 |
} |
149 |
|
150 |
protected void initializeResource(JDBCConnectionParameters params) { |
151 |
// Object[] resourceParams = new Object[]{
|
152 |
// params.getUrl(),
|
153 |
// params.getHost(),
|
154 |
// params.getPort(),
|
155 |
// params.getDBName(),
|
156 |
// params.getUser(),
|
157 |
// params.getPassword(),
|
158 |
// params.getJDBCDriverClassName()
|
159 |
// };
|
160 |
//
|
161 |
// try {
|
162 |
// ResourceManagerProviderServices manager
|
163 |
// = (ResourceManagerProviderServices) DALLocator.getResourceManager();
|
164 |
// JDBCResource resource = (JDBCResource) manager.createAddResource(
|
165 |
// this.getResourceType(),
|
166 |
// resourceParams
|
167 |
// );
|
168 |
// this.resource = resource;
|
169 |
// this.resource.addConsumer(this);
|
170 |
// } catch (InitializeException ex) {
|
171 |
// logger.debug("Can't initialize resource (" + ArrayUtils.toString(resourceParams) + ").", ex);
|
172 |
// throw new RuntimeException("Can't initialize resource (" + ArrayUtils.toString(resourceParams) + ").", ex);
|
173 |
// }
|
174 |
|
175 |
} |
176 |
|
177 |
@Override
|
178 |
public String getSourceId() { |
179 |
return this.store.getSourceId(); |
180 |
} |
181 |
|
182 |
@Override
|
183 |
public JDBCResource getResource() {
|
184 |
return null; |
185 |
// return this.resource;
|
186 |
} |
187 |
|
188 |
@Override
|
189 |
public Connection getConnection() throws AccessResourceException { |
190 |
// return this.resource.getJDBCConnection();
|
191 |
return null; |
192 |
} |
193 |
|
194 |
@Override
|
195 |
public String getConnectionURL() { |
196 |
return null; |
197 |
} |
198 |
|
199 |
public JDBCConnectionParameters getConnectionParameters() {
|
200 |
return connectionParameters;
|
201 |
} |
202 |
|
203 |
@Override
|
204 |
public void closeConnection(Connection connection) { |
205 |
// this.getResource().closeConnection(connection);
|
206 |
JDBCUtils.closeQuietly(connection); |
207 |
} |
208 |
|
209 |
@Override
|
210 |
protected void doDispose() throws BaseException { |
211 |
JDBCUtils.closeQuietly(this);
|
212 |
} |
213 |
|
214 |
@Override
|
215 |
public void close() throws Exception { |
216 |
// this.resource.removeConsumer(this);
|
217 |
JDBCUtils.closeQuietly(this.resulSetControler);
|
218 |
// this.resource = null;
|
219 |
this.resulSetControler = null; |
220 |
} |
221 |
|
222 |
@Override
|
223 |
public boolean closeResourceRequested(ResourceProvider resource) { |
224 |
return this.helperClient.closeResourceRequested(resource); |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public void resourceChanged(ResourceProvider resource) { |
229 |
this.helperClient.resourceChanged(resource);
|
230 |
} |
231 |
|
232 |
@Override
|
233 |
public GeometryManager getGeometryManager() {
|
234 |
if (this.geometryManager == null) { |
235 |
this.geometryManager = GeometryLocator.getGeometryManager();
|
236 |
} |
237 |
return this.geometryManager; |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public ResulSetControler getResulSetControler() {
|
242 |
if (this.resulSetControler == null) { |
243 |
this.resulSetControler = new ResulSetControlerBase(this); |
244 |
} |
245 |
return this.resulSetControler; |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public void fetchFeature(FeatureProvider feature, ResultSetEntry rs) throws DataException { |
250 |
fetchFeature(feature, rs.get()); |
251 |
} |
252 |
|
253 |
@Override
|
254 |
public void fetchFeature(FeatureProvider feature, ResultSet rs) throws DataException { |
255 |
try {
|
256 |
int index;
|
257 |
Object value;
|
258 |
for (FeatureAttributeDescriptor attr : feature.getType()) {
|
259 |
index = attr.getIndex(); |
260 |
switch (attr.getType()) {
|
261 |
case DataTypes.GEOMETRY:
|
262 |
value = this.getGeometryFromColumn(rs, index + 1); |
263 |
break;
|
264 |
default:
|
265 |
value = rs.getObject(index + 1);
|
266 |
} |
267 |
feature.set(index, value); |
268 |
} |
269 |
} catch (Exception ex) { |
270 |
throw new JDBCCantFetchValueException(ex); |
271 |
} |
272 |
} |
273 |
|
274 |
@Override
|
275 |
public Geometry getGeometryFromColumn(ResultSetEntry rs, int index) throws DataException { |
276 |
return getGeometryFromColumn(rs.get(), index);
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
public Geometry getGeometryFromColumn(ResultSet rs, int index) throws DataException { |
281 |
try {
|
282 |
Object value;
|
283 |
switch (this.getGeometrySupportType()) { |
284 |
case WKB:
|
285 |
value = rs.getBytes(index); |
286 |
if (value == null) { |
287 |
return null; |
288 |
} |
289 |
return this.getGeometryManager().createFrom((byte[]) value); |
290 |
|
291 |
case EWKB:
|
292 |
value = rs.getBytes(index); |
293 |
if (value == null) { |
294 |
return null; |
295 |
} |
296 |
return this.getGeometryManager().createFrom((byte[]) value); |
297 |
case WKT:
|
298 |
default:
|
299 |
value = rs.getString(index); |
300 |
if (value == null) { |
301 |
return null; |
302 |
} |
303 |
return this.getGeometryManager().createFrom((String) value); |
304 |
|
305 |
} |
306 |
} catch (Exception ex) { |
307 |
throw new JDBCCantFetchValueException(ex); |
308 |
} |
309 |
} |
310 |
|
311 |
@Override
|
312 |
public FeatureProvider createFeature(FeatureType featureType) throws DataException { |
313 |
return this.store.getStoreServices().createDefaultFeatureProvider(featureType); |
314 |
} |
315 |
|
316 |
@Override
|
317 |
public boolean useSubquery() { |
318 |
if( this.store == null ) { |
319 |
return false; |
320 |
} |
321 |
return !StringUtils.isEmpty(this.store.getParameters().getSQL()); |
322 |
} |
323 |
|
324 |
@Override
|
325 |
public int getSRSCode(IProjection crs) { |
326 |
// Dumb implementation
|
327 |
String abrev = crs.getAbrev();
|
328 |
if( StringUtils.isEmpty(abrev) ) {
|
329 |
return -1; |
330 |
} |
331 |
String[] ss = abrev.split(":"); |
332 |
if( ss.length < 2 ) { |
333 |
return -1; |
334 |
} |
335 |
int id = Integer.parseInt(ss[1]); |
336 |
return id;
|
337 |
} |
338 |
|
339 |
@Override
|
340 |
public IProjection getProjectionFromSRSId(int srsid) { |
341 |
// Dumb implementation
|
342 |
if( srsid < 2000 ) { |
343 |
return null; |
344 |
} |
345 |
IProjection proj = CRSFactory.getCRS("EPSG:"+srsid);
|
346 |
return proj;
|
347 |
} |
348 |
|
349 |
@Override
|
350 |
public JDBCStoreProvider createProvider(
|
351 |
JDBCStoreParameters parameters, |
352 |
DataStoreProviderServices providerServices |
353 |
) throws InitializeException {
|
354 |
|
355 |
JDBCStoreProviderBase store = new JDBCStoreProviderBase(
|
356 |
parameters, |
357 |
providerServices, |
358 |
DBHelper.newMetadataContainer(JDBCLibrary.NAME), |
359 |
this
|
360 |
); |
361 |
this.initialize(store, parameters, store);
|
362 |
return store;
|
363 |
} |
364 |
|
365 |
@Override
|
366 |
public JDBCServerExplorer createServerExplorer(
|
367 |
JDBCServerExplorerParameters parameters, |
368 |
DataServerExplorerProviderServices providerServices |
369 |
) throws InitializeException {
|
370 |
|
371 |
JDBCServerExplorer explorer = new JDBCServerExplorerBase(
|
372 |
parameters, |
373 |
providerServices, |
374 |
this
|
375 |
); |
376 |
this.initialize(explorer, parameters, null); |
377 |
return explorer;
|
378 |
} |
379 |
|
380 |
@Override
|
381 |
public JDBCNewStoreParameters createNewStoreParameters() {
|
382 |
return new JDBCNewStoreParameters(); |
383 |
} |
384 |
|
385 |
@Override
|
386 |
public JDBCStoreParameters createOpenStoreParameters() {
|
387 |
return new JDBCStoreParameters(); |
388 |
} |
389 |
|
390 |
public JDBCServerExplorerParameters createServerExplorerParameters() {
|
391 |
return new JDBCServerExplorerParameters(); |
392 |
} |
393 |
|
394 |
@Override
|
395 |
public String getSourceId(JDBCStoreParameters parameters) { |
396 |
return parameters.getHost() + ":" + |
397 |
parameters.getDBName() + ":" +
|
398 |
parameters.getSchema()+ ":" +
|
399 |
parameters.tableID(); |
400 |
} |
401 |
|
402 |
} |