root / branches / v2_0_0_prep / extensions / org.gvsig.oracle / src / org / gvsig / fmap / dal / store / oracle / OracleStoreProvider.java @ 32213
History | View | Annotate | Download (11.5 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 Prodevelop S.L. main development
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.dal.store.oracle; |
29 |
|
30 |
import java.sql.ResultSet; |
31 |
import java.sql.SQLException; |
32 |
import java.util.ArrayList; |
33 |
import java.util.List; |
34 |
|
35 |
import oracle.sql.STRUCT; |
36 |
|
37 |
import org.cresques.cts.IProjection; |
38 |
import org.gvsig.fmap.dal.DALLocator; |
39 |
import org.gvsig.fmap.dal.DataManager; |
40 |
import org.gvsig.fmap.dal.DataServerExplorer; |
41 |
import org.gvsig.fmap.dal.DataTypes; |
42 |
import org.gvsig.fmap.dal.exception.DataException; |
43 |
import org.gvsig.fmap.dal.exception.InitializeException; |
44 |
import org.gvsig.fmap.dal.exception.ReadException; |
45 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
46 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
47 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
48 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
49 |
import org.gvsig.fmap.dal.feature.FeatureType; |
50 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
51 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
52 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
53 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
54 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter; |
55 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
56 |
import org.gvsig.fmap.geom.Geometry; |
57 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
58 |
import org.gvsig.tools.ToolsLocator; |
59 |
import org.gvsig.tools.dynobject.DynClass; |
60 |
import org.gvsig.tools.dynobject.DynObjectManager; |
61 |
import org.gvsig.tools.exception.BaseException; |
62 |
import org.slf4j.Logger; |
63 |
import org.slf4j.LoggerFactory; |
64 |
|
65 |
/**
|
66 |
* Oracle Provider
|
67 |
*
|
68 |
* @author vsanjaime
|
69 |
*
|
70 |
*/
|
71 |
public class OracleStoreProvider extends JDBCStoreProviderWriter { |
72 |
|
73 |
public final static Logger logger = LoggerFactory |
74 |
.getLogger(OracleStoreProvider.class); |
75 |
|
76 |
public static String NAME = "Oracle"; |
77 |
public static String DESCRIPTION = "Oracle source"; |
78 |
private static final String DYNCLASS_NAME = "OracleStore"; |
79 |
private static DynClass DYNCLASS = null; |
80 |
|
81 |
protected static void registerDynClass() { |
82 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
83 |
DynClass dynClass; |
84 |
if (DYNCLASS == null) { |
85 |
dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION); |
86 |
|
87 |
dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
88 |
DYNCLASS = dynClass; |
89 |
} |
90 |
} |
91 |
|
92 |
public OracleStoreProvider(OracleStoreParameters params,
|
93 |
DataStoreProviderServices storeServices) throws InitializeException {
|
94 |
super(params, storeServices, ToolsLocator.getDynObjectManager()
|
95 |
.createDynObject(DYNCLASS)); |
96 |
} |
97 |
|
98 |
private OracleStoreParameters getOracleStoreParameters() {
|
99 |
return (OracleStoreParameters) this.getParameters(); |
100 |
} |
101 |
|
102 |
protected JDBCHelper createHelper() throws InitializeException { |
103 |
return new OracleHelper(this, getOracleStoreParameters()); |
104 |
} |
105 |
|
106 |
protected String fixFilter(String filter) { |
107 |
if (filter == null) { |
108 |
return null; |
109 |
} else {
|
110 |
throw new RuntimeException("Not implemented: fixFilter in OracleStoreProvider. Input was: " + filter); |
111 |
} |
112 |
} |
113 |
|
114 |
public String getName() { |
115 |
return NAME;
|
116 |
} |
117 |
|
118 |
public FeatureSetProvider createSet(FeatureQuery query,
|
119 |
FeatureType featureType) throws DataException {
|
120 |
|
121 |
return new OracleSetProvider(this, query, featureType); |
122 |
} |
123 |
|
124 |
public DataServerExplorer getExplorer() throws ReadException { |
125 |
DataManager manager = DALLocator.getDataManager(); |
126 |
OracleServerExplorerParameters exParams; |
127 |
OracleStoreParameters params = getOracleStoreParameters(); |
128 |
try {
|
129 |
exParams = (OracleServerExplorerParameters) manager |
130 |
.createServerExplorerParameters(OracleServerExplorer.NAME); |
131 |
exParams.setUrl(params.getUrl()); |
132 |
exParams.setHost(params.getHost()); |
133 |
exParams.setPort(params.getPort()); |
134 |
exParams.setDBName(params.getDBName()); |
135 |
exParams.setUser(params.getUser()); |
136 |
exParams.setPassword(params.getPassword()); |
137 |
exParams.setCatalog(params.getCatalog()); |
138 |
exParams.setSchema(params.getSchema()); |
139 |
exParams.setJDBCDriverClassName(params.getJDBCDriverClassName()); |
140 |
exParams.setUseSSL(params.getUseSSL()); |
141 |
exParams.setOraDriverType(params.getOraDriverType()); |
142 |
|
143 |
return manager.createServerExplorer(exParams);
|
144 |
} catch (DataException e) {
|
145 |
throw new ReadException(this.getName(), e); |
146 |
} catch (ValidateDataParametersException e) {
|
147 |
throw new ReadException(this.getName(), e); |
148 |
} |
149 |
} |
150 |
|
151 |
public boolean allowAutomaticValues() { |
152 |
return true; |
153 |
} |
154 |
|
155 |
public boolean hasGeometrySupport() { |
156 |
return true; |
157 |
} |
158 |
|
159 |
protected OracleHelper getOraHelper() {
|
160 |
return (OracleHelper) getHelper();
|
161 |
} |
162 |
|
163 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
164 |
throws DataException {
|
165 |
FeatureType type = getFeatureStore().getDefaultFeatureType(); |
166 |
FeatureAttributeDescriptor geomAttr = type.getAttributeDescriptor(type |
167 |
.getDefaultGeometryAttributeName()); |
168 |
if (geomAttr == null) { |
169 |
return false; |
170 |
} |
171 |
if (geometrySubtype != geomAttr.getGeometrySubType()) {
|
172 |
return false; |
173 |
} |
174 |
switch (geomAttr.getGeometryType()) {
|
175 |
case Geometry.TYPES.GEOMETRY:
|
176 |
return true; |
177 |
|
178 |
case Geometry.TYPES.MULTISURFACE:
|
179 |
return geometryType == Geometry.TYPES.MULTISURFACE;
|
180 |
|
181 |
case Geometry.TYPES.MULTIPOINT:
|
182 |
return geometryType == Geometry.TYPES.MULTIPOINT;
|
183 |
|
184 |
case Geometry.TYPES.MULTICURVE:
|
185 |
return geometryType == Geometry.TYPES.MULTICURVE;
|
186 |
|
187 |
case Geometry.TYPES.MULTISOLID:
|
188 |
return geometryType == Geometry.TYPES.MULTISOLID;
|
189 |
|
190 |
case Geometry.TYPES.SURFACE:
|
191 |
return geometryType == Geometry.TYPES.SURFACE;
|
192 |
|
193 |
case Geometry.TYPES.POINT:
|
194 |
return geometryType == Geometry.TYPES.POINT;
|
195 |
|
196 |
case Geometry.TYPES.CURVE:
|
197 |
return geometryType == Geometry.TYPES.CURVE;
|
198 |
|
199 |
case Geometry.TYPES.SOLID:
|
200 |
return geometryType == Geometry.TYPES.SOLID;
|
201 |
|
202 |
default:
|
203 |
return geometryType == geomAttr.getGeometryType();
|
204 |
} |
205 |
|
206 |
} |
207 |
|
208 |
protected void addToListFeatureValues(FeatureProvider featureProvider, |
209 |
FeatureAttributeDescriptor attrOfList, |
210 |
FeatureAttributeDescriptor attr, List values) throws DataException { |
211 |
|
212 |
|
213 |
|
214 |
// geometry
|
215 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
216 |
Geometry geom = (Geometry) featureProvider.get(attr.getIndex()); |
217 |
|
218 |
STRUCT stru = OracleUtils.buildSTRUCT(geom, 0, helper.getConnection(), null, false, false, false); |
219 |
values.add(stru); |
220 |
} else {
|
221 |
super.addToListFeatureValues(featureProvider, attrOfList, attr, values);
|
222 |
} |
223 |
} |
224 |
|
225 |
protected String getSqlStatementAddField(FeatureAttributeDescriptor attr, |
226 |
List additionalStatement) throws DataException { |
227 |
|
228 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
229 |
OracleStoreParameters params = getOracleStoreParameters(); |
230 |
additionalStatement.add(((OracleHelper) helper) |
231 |
.getSqlGeometryFieldAdd(attr, params.getTable(), params |
232 |
.getSchema())); |
233 |
} |
234 |
|
235 |
return super.getSqlStatementAddField(attr, additionalStatement); |
236 |
|
237 |
} |
238 |
|
239 |
protected String getSqlStatementDropField(FeatureAttributeDescriptor attr, |
240 |
List additionalStatement) {
|
241 |
String result = super.getSqlStatementDropField(attr, |
242 |
additionalStatement); |
243 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
244 |
additionalStatement.add(getSqlGeometryFieldDrop(attr)); |
245 |
} |
246 |
return result;
|
247 |
} |
248 |
|
249 |
protected List<String> getSqlStatementAlterField( |
250 |
FeatureAttributeDescriptor attrOrg, |
251 |
FeatureAttributeDescriptor attrTrg, List additionalStatement)
|
252 |
throws DataException {
|
253 |
|
254 |
List<String> actions = new ArrayList<String>(); |
255 |
StringBuilder strb;
|
256 |
OracleStoreParameters params = getOracleStoreParameters(); |
257 |
|
258 |
// diferent column type
|
259 |
if (attrOrg.getDataType() != attrTrg.getDataType()) {
|
260 |
strb = new StringBuilder(); |
261 |
strb.append("MODIFY (");
|
262 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
263 |
strb.append(" ");
|
264 |
strb.append(helper.getSqlColumnTypeDescription(attrTrg)); |
265 |
strb.append(")");
|
266 |
|
267 |
if (attrOrg.getDataType() == DataTypes.GEOMETRY) {
|
268 |
additionalStatement.add(getSqlGeometryFieldDrop(attrOrg)); |
269 |
} |
270 |
if (attrTrg.getDataType() == DataTypes.GEOMETRY) {
|
271 |
additionalStatement.addAll(((OracleHelper) helper) |
272 |
.getSqlGeometryFieldAdd(attrTrg, params.getTable(), |
273 |
params.getSchema())); |
274 |
} |
275 |
|
276 |
actions.add(strb.toString()); |
277 |
} |
278 |
|
279 |
if (attrOrg.allowNull() != attrTrg.allowNull()) {
|
280 |
|
281 |
strb = new StringBuilder(); |
282 |
strb.append("MODIFY (");
|
283 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
284 |
strb.append(" ");
|
285 |
if (attrTrg.allowNull()) {
|
286 |
strb.append("SET ");
|
287 |
} else {
|
288 |
strb.append("DROP ");
|
289 |
} |
290 |
strb.append("NOT NULL)");
|
291 |
actions.add(strb.toString()); |
292 |
} |
293 |
|
294 |
if (attrOrg.getDefaultValue() != attrTrg.getDefaultValue()) {
|
295 |
if (attrTrg.getDefaultValue() == null) { |
296 |
|
297 |
strb = new StringBuilder(); |
298 |
strb.append("MODIFY (");
|
299 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
300 |
strb.append(" DROP DEFAULT)");
|
301 |
actions.add(strb.toString()); |
302 |
} else if (!attrTrg.getDefaultValue().equals( |
303 |
attrOrg.getDefaultValue())) { |
304 |
// ALTER [ COLUMN ] column DROP DEFAULT
|
305 |
|
306 |
strb = new StringBuilder(); |
307 |
strb.append("MODIFY (");
|
308 |
strb.append(helper.escapeFieldName(attrTrg.getName())); |
309 |
strb.append(" SET DEFAULT ");
|
310 |
strb.append(helper.dalValueToJDBC(attrTrg, attrTrg |
311 |
.getDefaultValue())); |
312 |
strb.append(")");
|
313 |
actions.add(strb.toString()); |
314 |
} |
315 |
} |
316 |
|
317 |
if (attrOrg.getDataType() == attrTrg.getDataType()
|
318 |
&& attrTrg.getDataType() == DataTypes.GEOMETRY) { |
319 |
// TODO Checks SRS and GeomType/Subtype
|
320 |
} |
321 |
|
322 |
return actions;
|
323 |
} |
324 |
|
325 |
private Object getSqlGeometryFieldDrop(FeatureAttributeDescriptor attr) { |
326 |
StringBuilder strb = new StringBuilder(); |
327 |
OracleStoreParameters params = getOracleStoreParameters(); |
328 |
|
329 |
strb.append("DELETE FROM ");
|
330 |
strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW); |
331 |
strb.append(" WHERE ");
|
332 |
strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_TABLE_NAME); |
333 |
strb.append(" = '");
|
334 |
strb.append(params.getTable()); |
335 |
strb.append("' AND ");
|
336 |
strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_COLUMN_NAME); |
337 |
strb.append(" = '");
|
338 |
strb.append(attr.getName()); |
339 |
strb.append("'");
|
340 |
|
341 |
return strb.toString();
|
342 |
} |
343 |
|
344 |
|
345 |
protected void loadFeatureProviderValue(FeatureProvider data, ResultSet rs, |
346 |
FeatureAttributeDescriptor attr) throws DataException {
|
347 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
348 |
|
349 |
try {
|
350 |
Object geo_str_obj = rs.getObject(attr.getIndex() + 1); |
351 |
if (geo_str_obj == null) { |
352 |
data.set(attr.getIndex(), OracleUtils.createNullGeometry(SUBTYPES.GEOM2D)); |
353 |
} else {
|
354 |
STRUCT geo_str = (STRUCT) geo_str_obj; |
355 |
|
356 |
IProjection proj = attr.getSRS(); |
357 |
// OracleUtils.
|
358 |
|
359 |
Geometry geom = OracleUtils.getGeometry( |
360 |
geo_str, |
361 |
false,
|
362 |
false,
|
363 |
"",
|
364 |
helper.getConnection()); |
365 |
data.set(attr.getIndex(), geom); |
366 |
} |
367 |
} catch (SQLException e) { |
368 |
throw new JDBCSQLException(e); |
369 |
} catch (BaseException e) {
|
370 |
throw new ReadException(getName(), e); |
371 |
} |
372 |
|
373 |
} else {
|
374 |
try {
|
375 |
data.set(attr.getIndex(), rs.getObject(attr.getIndex() + 1));
|
376 |
} catch (SQLException e) { |
377 |
throw new JDBCSQLException(e); |
378 |
} |
379 |
} |
380 |
} |
381 |
|
382 |
} |