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