Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.oracle / src / org / gvsig / fmap / dal / store / oracle / OracleStoreProvider.java @ 31889

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
import java.util.regex.Matcher;
35
import java.util.regex.Pattern;
36

    
37
import oracle.sql.STRUCT;
38

    
39
import org.cresques.cts.IProjection;
40
import org.gvsig.fmap.dal.DALLocator;
41
import org.gvsig.fmap.dal.DataManager;
42
import org.gvsig.fmap.dal.DataServerExplorer;
43
import org.gvsig.fmap.dal.DataTypes;
44
import org.gvsig.fmap.dal.exception.DataException;
45
import org.gvsig.fmap.dal.exception.InitializeException;
46
import org.gvsig.fmap.dal.exception.ReadException;
47
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
48
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
49
import org.gvsig.fmap.dal.feature.FeatureQuery;
50
import org.gvsig.fmap.dal.feature.FeatureStore;
51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
53
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider;
54
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
55
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
56
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter;
57
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
58
import org.gvsig.fmap.geom.Geometry;
59
import org.gvsig.tools.ToolsLocator;
60
import org.gvsig.tools.dynobject.DynClass;
61
import org.gvsig.tools.dynobject.DynObjectManager;
62
import org.gvsig.tools.exception.BaseException;
63
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
65

    
66
/**
67
 * Oracle Provider
68
 * 
69
 * @author vsanjaime
70
 * 
71
 */
72
public class OracleStoreProvider extends JDBCStoreProviderWriter {
73

    
74
        public final static Logger logger = LoggerFactory
75
                        .getLogger(OracleStoreProvider.class);
76

    
77
        public static String NAME = "Oracle";
78
        public static String DESCRIPTION = "Oracle source";
79
        private static final String DYNCLASS_NAME = "OracleStore";
80
        private static DynClass DYNCLASS = null;
81

    
82
        protected static void registerDynClass() {
83
                DynObjectManager dynman = ToolsLocator.getDynObjectManager();
84
                DynClass dynClass;
85
                if (DYNCLASS == null) {
86
                        dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION);
87

    
88
                        dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME));
89
                        DYNCLASS = dynClass;
90
                }
91
        }
92

    
93
        public OracleStoreProvider(OracleStoreParameters params,
94
                        DataStoreProviderServices storeServices) throws InitializeException {
95
                super(params, storeServices, ToolsLocator.getDynObjectManager()
96
                                .createDynObject(DYNCLASS));
97
        }
98

    
99
        private OracleStoreParameters getOracleStoreParameters() {
100
                return (OracleStoreParameters) this.getParameters();
101
        }
102

    
103
        protected JDBCHelper createHelper() throws InitializeException {
104
                return new OracleHelper(this, getOracleStoreParameters());
105
        }
106

    
107
        protected String fixFilter(String filter) {
108
                if (filter == null) {
109
                        return null;
110
                } else {
111
                        throw new RuntimeException("Not implemented: fixFilter in OracleStoreProvider. Input was: " + filter);
112
                }
113
        }
114

    
115
        public String getName() {
116
                return NAME;
117
        }
118

    
119
        public FeatureSetProvider createSet(FeatureQuery query,
120
                        FeatureType featureType) throws DataException {
121

    
122
                return new OracleSetProvider(this, query, featureType);
123
        }
124

    
125
        public DataServerExplorer getExplorer() throws ReadException {
126
                DataManager manager = DALLocator.getDataManager();
127
                OracleServerExplorerParameters exParams;
128
                OracleStoreParameters params = getOracleStoreParameters();
129
                try {
130
                        exParams = (OracleServerExplorerParameters) manager
131
                                        .createServerExplorerParameters(OracleServerExplorer.NAME);
132
                        exParams.setUrl(params.getUrl());
133
                        exParams.setHost(params.getHost());
134
                        exParams.setPort(params.getPort());
135
                        exParams.setDBName(params.getDBName());
136
                        exParams.setUser(params.getUser());
137
                        exParams.setPassword(params.getPassword());
138
                        exParams.setCatalog(params.getCatalog());
139
                        exParams.setSchema(params.getSchema());
140
                        exParams.setJDBCDriverClassName(params.getJDBCDriverClassName());
141
                        exParams.setUseSSL(params.getUseSSL());
142
                        exParams.setOraDriverType(params.getOraDriverType());
143

    
144
                        return manager.createServerExplorer(exParams);
145
                } catch (DataException e) {
146
                        throw new ReadException(this.getName(), e);
147
                } catch (ValidateDataParametersException e) {
148
                        throw new ReadException(this.getName(), e);
149
                }
150
        }
151

    
152
        public boolean allowAutomaticValues() {
153
                return true;
154
        }
155

    
156
        public boolean hasGeometrySupport() {
157
                return true;
158
        }
159

    
160
        protected OracleHelper getOraHelper() {
161
                return (OracleHelper) getHelper();
162
        }
163

    
164
        public boolean canWriteGeometry(int geometryType, int geometrySubtype)
165
                        throws DataException {
166
                FeatureType type = getFeatureStore().getDefaultFeatureType();
167
                FeatureAttributeDescriptor geomAttr = type.getAttributeDescriptor(type
168
                                .getDefaultGeometryAttributeName());
169
                if (geomAttr == null) {
170
                        return false;
171
                }
172
                if (geometrySubtype != geomAttr.getGeometrySubType()) {
173
                        return false;
174
                }
175
                switch (geomAttr.getGeometryType()) {
176
                case Geometry.TYPES.GEOMETRY:
177
                        return true;
178

    
179
                case Geometry.TYPES.MULTISURFACE:
180
                        return geometryType == Geometry.TYPES.MULTISURFACE;
181

    
182
                case Geometry.TYPES.MULTIPOINT:
183
                        return geometryType == Geometry.TYPES.MULTIPOINT;
184

    
185
                case Geometry.TYPES.MULTICURVE:
186
                        return geometryType == Geometry.TYPES.MULTICURVE;
187

    
188
                case Geometry.TYPES.MULTISOLID:
189
                        return geometryType == Geometry.TYPES.MULTISOLID;
190

    
191
                case Geometry.TYPES.SURFACE:
192
                        return geometryType == Geometry.TYPES.SURFACE;
193

    
194
                case Geometry.TYPES.POINT:
195
                        return geometryType == Geometry.TYPES.POINT;
196

    
197
                case Geometry.TYPES.CURVE:
198
                        return geometryType == Geometry.TYPES.CURVE;
199

    
200
                case Geometry.TYPES.SOLID:
201
                        return geometryType == Geometry.TYPES.SOLID;
202

    
203
                default:
204
                        return geometryType == geomAttr.getGeometryType();
205
                }
206

    
207
        }
208

    
209
        protected void addToListFeatureValues(FeatureProvider featureProvider,
210
                        FeatureAttributeDescriptor attrOfList,
211
                        FeatureAttributeDescriptor attr, List values) throws DataException {
212

    
213
                
214
                
215
                // geometry
216
                if (attr.getDataType() == DataTypes.GEOMETRY) {
217
                        Geometry geom = (Geometry) featureProvider.get(attr.getIndex());
218
                        
219
                        STRUCT stru = OracleUtils.buildSTRUCT(geom, 0, helper.getConnection(), null, false, false, false);
220
                        values.add(stru);
221
                } else {
222
                        super.addToListFeatureValues(featureProvider, attrOfList, attr, values);
223
                }
224
        }
225

    
226
        protected String getSqlStatementAddField(FeatureAttributeDescriptor attr,
227
                        List additionalStatement) throws DataException {
228

    
229
                if (attr.getDataType() == DataTypes.GEOMETRY) {
230
                        OracleStoreParameters params = getOracleStoreParameters();
231
                        additionalStatement.add(((OracleHelper) helper)
232
                                        .getSqlGeometryFieldAdd(attr, params.getTable(), params
233
                                                        .getSchema()));
234
                }
235

    
236
                return super.getSqlStatementAddField(attr, additionalStatement);
237

    
238
        }
239

    
240
        protected String getSqlStatementDropField(FeatureAttributeDescriptor attr,
241
                        List additionalStatement) {
242
                String result = super.getSqlStatementDropField(attr,
243
                                additionalStatement);
244
                if (attr.getDataType() == DataTypes.GEOMETRY) {
245
                        additionalStatement.add(getSqlGeometryFieldDrop(attr));
246
                }
247
                return result;
248
        }
249

    
250
        protected List<String> getSqlStatementAlterField(
251
                        FeatureAttributeDescriptor attrOrg,
252
                        FeatureAttributeDescriptor attrTrg, List additionalStatement)
253
                        throws DataException {
254

    
255
                List<String> actions = new ArrayList<String>();
256
                StringBuilder strb;
257
                OracleStoreParameters params = getOracleStoreParameters();
258

    
259
                // diferent column type
260
                if (attrOrg.getDataType() != attrTrg.getDataType()) {
261
                        strb = new StringBuilder();
262
                        strb.append("MODIFY (");
263
                        strb.append(helper.escapeFieldName(attrTrg.getName()));
264
                        strb.append(" ");
265
                        strb.append(helper.getSqlColumnTypeDescription(attrTrg));
266
                        strb.append(")");
267

    
268
                        if (attrOrg.getDataType() == DataTypes.GEOMETRY) {
269
                                additionalStatement.add(getSqlGeometryFieldDrop(attrOrg));
270
                        }
271
                        if (attrTrg.getDataType() == DataTypes.GEOMETRY) {
272
                                additionalStatement.addAll(((OracleHelper) helper)
273
                                                .getSqlGeometryFieldAdd(attrTrg, params.getTable(),
274
                                                                params.getSchema()));
275
                        }
276

    
277
                        actions.add(strb.toString());
278
                }
279

    
280
                if (attrOrg.allowNull() != attrTrg.allowNull()) {
281

    
282
                        strb = new StringBuilder();
283
                        strb.append("MODIFY (");
284
                        strb.append(helper.escapeFieldName(attrTrg.getName()));
285
                        strb.append(" ");
286
                        if (attrTrg.allowNull()) {
287
                                strb.append("SET ");
288
                        } else {
289
                                strb.append("DROP ");
290
                        }
291
                        strb.append("NOT NULL)");
292
                        actions.add(strb.toString());
293
                }
294

    
295
                if (attrOrg.getDefaultValue() != attrTrg.getDefaultValue()) {
296
                        if (attrTrg.getDefaultValue() == null) {
297

    
298
                                strb = new StringBuilder();
299
                                strb.append("MODIFY (");
300
                                strb.append(helper.escapeFieldName(attrTrg.getName()));
301
                                strb.append(" DROP DEFAULT)");
302
                                actions.add(strb.toString());
303
                        } else if (!attrTrg.getDefaultValue().equals(
304
                                        attrOrg.getDefaultValue())) {
305
                                // ALTER [ COLUMN ] column DROP DEFAULT
306

    
307
                                strb = new StringBuilder();
308
                                strb.append("MODIFY (");
309
                                strb.append(helper.escapeFieldName(attrTrg.getName()));
310
                                strb.append(" SET DEFAULT ");
311
                                strb.append(helper.dalValueToJDBC(attrTrg, attrTrg
312
                                                .getDefaultValue()));
313
                                strb.append(")");
314
                                actions.add(strb.toString());
315
                        }
316
                }
317

    
318
                if (attrOrg.getDataType() == attrTrg.getDataType()
319
                                && attrTrg.getDataType() == DataTypes.GEOMETRY) {
320
                        // TODO Checks SRS and GeomType/Subtype
321
                }
322

    
323
                return actions;
324
        }
325

    
326
        private Object getSqlGeometryFieldDrop(FeatureAttributeDescriptor attr) {
327
                StringBuilder strb = new StringBuilder();
328
                OracleStoreParameters params = getOracleStoreParameters();
329
                
330
                strb.append("DELETE FROM ");
331
                strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW);
332
                strb.append(" WHERE ");
333
                strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_TABLE_NAME);
334
                strb.append(" = '");
335
                strb.append(params.getTable());
336
                strb.append("' AND ");
337
                strb.append(OracleValues.USER_ORACLE_GEOMETADATA_VIEW_COLUMN_NAME);
338
                strb.append(" = '");
339
                strb.append(attr.getName());
340
                strb.append("'");
341

    
342
                return strb.toString();
343
        }
344
        
345
        
346
        protected void loadFeatureProviderValue(FeatureProvider data, ResultSet rs,
347
                        FeatureAttributeDescriptor attr) throws DataException {
348
                if (attr.getDataType() == DataTypes.GEOMETRY) {
349

    
350
                        try {
351
                                Object geo_str_obj = rs.getObject(attr.getIndex() + 1);
352
                                if (geo_str_obj == null) {
353
                                        data.set(attr.getIndex(), null);
354
                                } else {
355
                                        STRUCT geo_str = (STRUCT) geo_str_obj;
356
                                        
357
                                        IProjection proj = attr.getSRS();
358
                                        // OracleUtils.
359
                                        
360
                                        Geometry geom = OracleUtils.getGeometry(
361
                                                        geo_str,
362
                                                        false,
363
                                                        true,
364
                                                        "88888",
365
                                                        helper.getConnection());
366
                                        data.set(attr.getIndex(), geom);
367
                                }
368
                        } catch (SQLException e) {
369
                                throw new JDBCSQLException(e);
370
                        } catch (BaseException e) {
371
                                throw new ReadException(getName(), e);
372
                        }
373

    
374
                } else {
375
                        try {
376
                                data.set(attr.getIndex(), rs.getObject(attr.getIndex() + 1));
377
                        } catch (SQLException e) {
378
                                throw new JDBCSQLException(e);
379
                        }
380
                }
381
        }
382

    
383
}