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