svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.oracle / src / org / gvsig / fmap / dal / store / oracle / OracleStoreProvider.java @ 29932
History | View | Annotate | Download (11.3 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.util.ArrayList; |
31 |
import java.util.List; |
32 |
import java.util.regex.Matcher; |
33 |
import java.util.regex.Pattern; |
34 |
|
35 |
import org.gvsig.fmap.dal.DALLocator; |
36 |
import org.gvsig.fmap.dal.DataManager; |
37 |
import org.gvsig.fmap.dal.DataServerExplorer; |
38 |
import org.gvsig.fmap.dal.DataTypes; |
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.exception.InitializeException; |
41 |
import org.gvsig.fmap.dal.exception.ReadException; |
42 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
43 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
44 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
47 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
48 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
49 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
50 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
51 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter; |
52 |
import org.gvsig.fmap.geom.Geometry; |
53 |
import org.gvsig.tools.ToolsLocator; |
54 |
import org.gvsig.tools.dynobject.DynClass; |
55 |
import org.gvsig.tools.dynobject.DynObjectManager; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
public class OracleStoreProvider extends JDBCStoreProviderWriter { |
60 |
|
61 |
public final static Logger logger = LoggerFactory |
62 |
.getLogger(OracleStoreProvider.class); |
63 |
|
64 |
public static String NAME = "OracleSpatial"; |
65 |
public static String DESCRIPTION = "Oracle source"; |
66 |
private static final String DYNCLASS_NAME = "OracleStore"; |
67 |
private static DynClass DYNCLASS = null; |
68 |
|
69 |
|
70 |
protected static void registerDynClass() { |
71 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
72 |
DynClass dynClass; |
73 |
if (DYNCLASS == null) { |
74 |
dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION); |
75 |
|
76 |
dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
77 |
DYNCLASS = dynClass; |
78 |
} |
79 |
} |
80 |
|
81 |
public OracleStoreProvider(OracleStoreParameters params,
|
82 |
DataStoreProviderServices storeServices) |
83 |
throws InitializeException {
|
84 |
super(params, storeServices, ToolsLocator.getDynObjectManager()
|
85 |
.createDynObject(DYNCLASS)); |
86 |
} |
87 |
|
88 |
private OracleStoreParameters getOracleParameters() {
|
89 |
return (OracleStoreParameters) this.getParameters(); |
90 |
} |
91 |
|
92 |
protected JDBCHelper createHelper() throws InitializeException { |
93 |
return new OracleHelper(this, getOracleParameters()); |
94 |
} |
95 |
|
96 |
protected String fixFilter(String filter) { |
97 |
if (filter == null) { |
98 |
return null; |
99 |
} |
100 |
|
101 |
// Transform SRS to code
|
102 |
// GeomFromText\s*\(\s*'[^']*'\s*,\s*('[^']*')\s*\)
|
103 |
Pattern pattern = Pattern |
104 |
.compile("GeomFromText\\s*\\(\\s*'[^']*'\\s*,\\s*'([^']*)'\\s*\\)");
|
105 |
Matcher matcher = pattern.matcher(filter);
|
106 |
StringBuilder strb = new StringBuilder(); |
107 |
int pos = 0; |
108 |
String srsCode;
|
109 |
while (matcher.find(pos)) {
|
110 |
strb.append(filter.substring(pos, matcher.start(1)));
|
111 |
srsCode = matcher.group(1).trim();
|
112 |
if (srsCode.startsWith("'")) { |
113 |
srsCode = srsCode.substring(1);
|
114 |
} |
115 |
if (srsCode.endsWith("'")) { |
116 |
srsCode = srsCode.substring(0, srsCode.length() - 1); |
117 |
} |
118 |
strb.append(helper.getProviderSRID(srsCode)); |
119 |
strb.append(filter.substring(matcher.end(1), matcher.end()));
|
120 |
pos = matcher.end(); |
121 |
|
122 |
} |
123 |
strb.append(filter.substring(pos)); |
124 |
|
125 |
return strb.toString();
|
126 |
} |
127 |
|
128 |
public String getName() { |
129 |
return NAME;
|
130 |
} |
131 |
|
132 |
public FeatureSetProvider createSet(FeatureQuery query,
|
133 |
FeatureType featureType) throws DataException {
|
134 |
|
135 |
return new OracleSetProvider(this, query, featureType); |
136 |
} |
137 |
|
138 |
|
139 |
public DataServerExplorer getExplorer() throws ReadException { |
140 |
DataManager manager = DALLocator.getDataManager(); |
141 |
OracleServerExplorerParameters exParams; |
142 |
OracleStoreParameters params = getOracleParameters(); |
143 |
try {
|
144 |
exParams = (OracleServerExplorerParameters) manager |
145 |
.createServerExplorerParameters(OracleServerExplorer.NAME); |
146 |
exParams.setUrl(params.getUrl()); |
147 |
exParams.setHost(params.getHost()); |
148 |
exParams.setPort(params.getPort()); |
149 |
exParams.setDBName(params.getDBName()); |
150 |
exParams.setUser(params.getUser()); |
151 |
exParams.setPassword(params.getPassword()); |
152 |
exParams.setCatalog(params.getCatalog()); |
153 |
exParams.setSchema(params.getSchema()); |
154 |
exParams.setJDBCDriverClassName(params.getJDBCDriverClassName()); |
155 |
exParams.setUseSSL(params.getUseSSL()); |
156 |
exParams.setOraDriverType(params.getOraDriverType()); |
157 |
|
158 |
return manager.createServerExplorer(exParams);
|
159 |
} catch (DataException e) {
|
160 |
throw new ReadException(this.getName(), e); |
161 |
} catch (ValidateDataParametersException e) {
|
162 |
// TODO Auto-generated catch block
|
163 |
throw new ReadException(this.getName(), e); |
164 |
} |
165 |
} |
166 |
|
167 |
public boolean allowAutomaticValues() { |
168 |
return true; |
169 |
} |
170 |
|
171 |
|
172 |
public boolean hasGeometrySupport() { |
173 |
return true; |
174 |
} |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
protected OracleHelper getOSHelper() {
|
180 |
return (OracleHelper) getHelper();
|
181 |
} |
182 |
|
183 |
|
184 |
|
185 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
186 |
throws DataException {
|
187 |
FeatureType type = getFeatureStore().getDefaultFeatureType(); |
188 |
FeatureAttributeDescriptor geomAttr = type.getAttributeDescriptor(type |
189 |
.getDefaultGeometryAttributeName()); |
190 |
if (geomAttr == null) { |
191 |
return false; |
192 |
} |
193 |
if (geometrySubtype != geomAttr.getGeometrySubType()) {
|
194 |
return false; |
195 |
} |
196 |
switch (geomAttr.getGeometryType()) {
|
197 |
case Geometry.TYPES.GEOMETRY:
|
198 |
return true; |
199 |
|
200 |
case Geometry.TYPES.MULTISURFACE:
|
201 |
return geometryType == Geometry.TYPES.MULTISURFACE
|
202 |
|| geometryType == Geometry.TYPES.SURFACE; |
203 |
|
204 |
case Geometry.TYPES.MULTIPOINT:
|
205 |
return geometryType == Geometry.TYPES.MULTIPOINT
|
206 |
|| geometryType == Geometry.TYPES.POINT; |
207 |
|
208 |
case Geometry.TYPES.MULTICURVE:
|
209 |
return geometryType == Geometry.TYPES.MULTICURVE
|
210 |
|| geometryType == Geometry.TYPES.CURVE; |
211 |
|
212 |
case Geometry.TYPES.MULTISOLID:
|
213 |
return geometryType == Geometry.TYPES.MULTISOLID
|
214 |
|| geometryType == Geometry.TYPES.SOLID; |
215 |
|
216 |
default:
|
217 |
return geometryType == geomAttr.getGeometryType();
|
218 |
} |
219 |
|
220 |
} |
221 |
|
222 |
|
223 |
protected void addToListFeatureValues(FeatureProvider featureProvider, |
224 |
FeatureAttributeDescriptor attrOfList, |
225 |
FeatureAttributeDescriptor attr, |
226 |
List values) throws DataException { |
227 |
|
228 |
super.addToListFeatureValues(featureProvider, attrOfList, attr, values);
|
229 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
230 |
values.add(helper.getProviderSRID(attr.getSRS())); |
231 |
} |
232 |
} |
233 |
|
234 |
protected void prepareAttributeForInsert( |
235 |
FeatureAttributeDescriptor attr, List fields, List values) { |
236 |
|
237 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
238 |
fields.add(helper.escapeFieldName(attr.getName())); |
239 |
values.add("GeomFromWKB(?,?)");
|
240 |
} else {
|
241 |
super.prepareAttributeForInsert(attr, fields, values);
|
242 |
} |
243 |
|
244 |
} |
245 |
|
246 |
protected void prepareAttributeForUpdate(FeatureAttributeDescriptor attr, |
247 |
List values) {
|
248 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
249 |
values.add(helper.escapeFieldName(attr.getName()) |
250 |
+ " = GeomFromWKB(?,?)");
|
251 |
} else {
|
252 |
super.prepareAttributeForUpdate(attr, values);
|
253 |
} |
254 |
} |
255 |
|
256 |
protected String getSqlStatementAddField(FeatureAttributeDescriptor attr, |
257 |
List additionalStatement) throws DataException { |
258 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
259 |
OracleStoreParameters params = getOracleParameters(); |
260 |
additionalStatement.add(((OracleHelper) helper) |
261 |
.getSqlGeometyFieldAdd(attr, params.getTable(), params |
262 |
.getSchema())); |
263 |
|
264 |
} |
265 |
return super.getSqlStatementAddField(attr, additionalStatement); |
266 |
|
267 |
} |
268 |
private Object getSqlGeometyFieldDrop(FeatureAttributeDescriptor attr) { |
269 |
StringBuilder strb = new StringBuilder(); |
270 |
OracleStoreParameters params = getOracleParameters(); |
271 |
strb.append("Delete from geometry_columns where f_geometry_column = '");
|
272 |
strb.append(attr.getName()); |
273 |
strb.append("' and f_table_nam = '");
|
274 |
strb.append(params.getTable()); |
275 |
strb.append("' and f_table_schema = ");
|
276 |
if (params.getSchema() == null || params.getSchema().length() == 0) { |
277 |
strb.append("current_schema()");
|
278 |
} else {
|
279 |
strb.append("'");
|
280 |
strb.append(params.getSchema()); |
281 |
strb.append("'");
|
282 |
} |
283 |
if (params.getCatalog() != null && params.getCatalog().length() > 0) { |
284 |
strb.append(" and f_table_catalog = '");
|
285 |
strb.append(params.getCatalog()); |
286 |
strb.append("'");
|
287 |
} |
288 |
return strb.toString();
|
289 |
} |
290 |
|
291 |
protected String getSqlStatementDropField(FeatureAttributeDescriptor attr, |
292 |
List additionalStatement) {
|
293 |
String result = super.getSqlStatementDropField(attr, |
294 |
additionalStatement); |
295 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
296 |
additionalStatement.add(getSqlGeometyFieldDrop(attr)); |
297 |
} |
298 |
return result;
|
299 |
} |
300 |
|
301 |
protected List getSqlStatementAlterField( |
302 |
FeatureAttributeDescriptor attrOrg, |
303 |
FeatureAttributeDescriptor attrTrg, List additionalStatement)
|
304 |
throws DataException {
|
305 |
//
|
306 |
List actions = super.getSqlStatementAlterField(attrOrg, attrTrg, |
307 |
additionalStatement); |
308 |
StringBuilder strb;
|
309 |
OracleStoreParameters params = getOracleParameters(); |
310 |
if (attrOrg.getDataType() != attrTrg.getDataType()) {
|
311 |
if (attrOrg.getDataType() == DataTypes.GEOMETRY) {
|
312 |
additionalStatement.add(getSqlGeometyFieldDrop(attrOrg)); |
313 |
} |
314 |
if (attrTrg.getDataType() == DataTypes.GEOMETRY) {
|
315 |
additionalStatement.addAll(((OracleHelper) helper) |
316 |
.getSqlGeometyFieldAdd(attrTrg, params.getTable(), |
317 |
params.getSchema())); |
318 |
} |
319 |
} |
320 |
if (attrOrg.getDataType() == attrTrg.getDataType()
|
321 |
&& attrTrg.getDataType() == DataTypes.GEOMETRY) { |
322 |
// TODO Checks SRS and GeomType/Subtype
|
323 |
} |
324 |
|
325 |
return actions;
|
326 |
} |
327 |
|
328 |
public List getSqlGeometyFieldAdd(FeatureAttributeDescriptor attr, |
329 |
String table, String schema) { |
330 |
|
331 |
|
332 |
List sqls = new ArrayList(); |
333 |
|
334 |
StringBuilder strb = new StringBuilder(); |
335 |
strb.append("SELECT AddGeometryColumn('");
|
336 |
if (schema != null && schema.length() > 0) { |
337 |
strb.append(schema); |
338 |
strb.append("', '");
|
339 |
} |
340 |
strb.append(table); |
341 |
strb.append("', '");
|
342 |
strb.append(attr.getName()); |
343 |
strb.append("', ");
|
344 |
// strb.append("-1");
|
345 |
strb.append(getProviderSRID(attr.getSRS())); |
346 |
strb.append(", '");
|
347 |
strb.append(getPostgisGeomType(attr.getGeometryType(), attr |
348 |
.getGeometrySubType())); |
349 |
strb.append("', ");
|
350 |
strb.append(getPostgisGeomDimensions(attr.getGeometrySubType())); |
351 |
strb.append(")");
|
352 |
|
353 |
|
354 |
sqls.add(strb.toString()); |
355 |
|
356 |
/*ALTER TABLE muni10000_peq_test DROP CONSTRAINT enforce_srid_the_geom;*/
|
357 |
/*
|
358 |
strb = new StringBuilder();
|
359 |
strb.append("Alter table ");
|
360 |
if (schema != null && schema.length() > 0) {
|
361 |
strb.append(schema);
|
362 |
strb.append(".");
|
363 |
}
|
364 |
strb.append("f_table_name = '");
|
365 |
strb.append(table);
|
366 |
strb.append("' AND f_geometry_column = '");
|
367 |
strb.append(attr.getName());
|
368 |
strb.append("' AND srid = -1");
|
369 |
|
370 |
|
371 |
sqls.add(strb.toString());
|
372 |
*/
|
373 |
return sqls;
|
374 |
} |
375 |
|
376 |
} |