svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.oracle / src / org / gvsig / fmap / dal / store / oracle / OracleServerExplorer.java @ 29865
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 IVER T.I {{Task}}
|
26 |
*/
|
27 |
|
28 |
/**
|
29 |
*
|
30 |
*/
|
31 |
package org.gvsig.fmap.dal.store.oracle; |
32 |
|
33 |
import java.sql.Connection; |
34 |
import java.sql.ResultSet; |
35 |
import java.sql.SQLException; |
36 |
import java.sql.Statement; |
37 |
import java.util.ArrayList; |
38 |
import java.util.Iterator; |
39 |
import java.util.List; |
40 |
|
41 |
import org.gvsig.fmap.dal.DataStoreParameters; |
42 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
43 |
import org.gvsig.fmap.dal.exception.DataException; |
44 |
import org.gvsig.fmap.dal.exception.InitializeException; |
45 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
46 |
import org.gvsig.fmap.dal.exception.ReadException; |
47 |
import org.gvsig.fmap.dal.exception.RemoveException; |
48 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
49 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
50 |
import org.gvsig.fmap.dal.feature.FeatureType; |
51 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
52 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
53 |
import org.gvsig.fmap.dal.store.jdbc.ConnectionAction; |
54 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
55 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
56 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
57 |
import org.gvsig.fmap.dal.store.jdbc.TransactionalAction; |
58 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
59 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
60 |
import org.slf4j.Logger; |
61 |
import org.slf4j.LoggerFactory; |
62 |
|
63 |
/**
|
64 |
* @author vsanjaime
|
65 |
*
|
66 |
*/
|
67 |
public class OracleServerExplorer extends JDBCServerExplorer { |
68 |
final static private Logger logger = LoggerFactory |
69 |
.getLogger(OracleServerExplorer.class); |
70 |
|
71 |
public static final String NAME = "OracleSpatialExplorer"; |
72 |
|
73 |
/**
|
74 |
* Constructor
|
75 |
*
|
76 |
* @param parameters
|
77 |
* @param services
|
78 |
* @throws InitializeException
|
79 |
*/
|
80 |
public OracleServerExplorer(
|
81 |
OracleServerExplorerParameters parameters, |
82 |
DataServerExplorerProviderServices services) |
83 |
throws InitializeException {
|
84 |
super(parameters, services);
|
85 |
} |
86 |
|
87 |
/**
|
88 |
* get explorer name
|
89 |
*/
|
90 |
public String getName() { |
91 |
return NAME;
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* can add
|
96 |
*/
|
97 |
public boolean canAdd() { |
98 |
return true; |
99 |
} |
100 |
|
101 |
/**
|
102 |
* remove
|
103 |
*/
|
104 |
public void remove(DataStoreParameters dsp) throws RemoveException { |
105 |
|
106 |
// TODO arreglar este metodo para borrar la tabla y los metadatos
|
107 |
final OracleStoreParameters osParams = (OracleStoreParameters) dsp;
|
108 |
|
109 |
TransactionalAction action = new TransactionalAction() {
|
110 |
public boolean continueTransactionAllowed() { |
111 |
return false; |
112 |
} |
113 |
|
114 |
public Object action(Connection conn) throws DataException { |
115 |
|
116 |
Statement st;
|
117 |
try {
|
118 |
st = conn.createStatement(); |
119 |
} catch (SQLException e) { |
120 |
throw new JDBCSQLException(e); |
121 |
} |
122 |
|
123 |
String sqlDrop = "DROP TABLE " + osParams.tableID(); |
124 |
|
125 |
StringBuilder strb = new StringBuilder(); |
126 |
strb |
127 |
.append("Delete from GEOMETRY_COLUMNS where f_table_schema = ");
|
128 |
if (osParams.getSchema() == null |
129 |
|| osParams.getSchema().length() == 0) {
|
130 |
strb.append("current_schema() ");
|
131 |
} else {
|
132 |
strb.append('\'');
|
133 |
strb.append(osParams.getSchema()); |
134 |
strb.append("' ");
|
135 |
} |
136 |
strb.append("and f_table_name = '");
|
137 |
strb.append(osParams.getTable()); |
138 |
strb.append('\'');
|
139 |
|
140 |
String sqlDeleteFromGeometry_column = strb.toString();
|
141 |
try {
|
142 |
try {
|
143 |
st.execute(sqlDrop); |
144 |
} catch (SQLException e) { |
145 |
throw new JDBCExecuteSQLException(sqlDrop, e); |
146 |
} |
147 |
|
148 |
try {
|
149 |
st.execute(sqlDeleteFromGeometry_column); |
150 |
} catch (SQLException e) { |
151 |
throw new JDBCExecuteSQLException( |
152 |
sqlDeleteFromGeometry_column, e); |
153 |
} |
154 |
|
155 |
} finally {
|
156 |
try {
|
157 |
st.close(); |
158 |
} catch (SQLException e) { |
159 |
} |
160 |
; |
161 |
} |
162 |
return null; |
163 |
} |
164 |
}; |
165 |
try {
|
166 |
this.helper.doConnectionAction(action);
|
167 |
} catch (Exception e) { |
168 |
throw new RemoveException(this.getName(), e); |
169 |
} |
170 |
} |
171 |
|
172 |
/**
|
173 |
* get parameters
|
174 |
*/
|
175 |
public NewDataStoreParameters getAddParameters() throws DataException { |
176 |
OracleServerExplorerParameters parameters = getOracleSpatialParameters(); |
177 |
OracleNewStoreParameters params = new OracleNewStoreParameters();
|
178 |
params.setHost(parameters.getHost()); |
179 |
params.setPort(parameters.getPort()); |
180 |
params.setDBName(parameters.getDBName()); |
181 |
params.setUser(parameters.getUser()); |
182 |
params.setPassword(parameters.getPassword()); |
183 |
params.setCatalog(parameters.getCatalog()); |
184 |
params.setSchema(parameters.getSchema()); |
185 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
186 |
params.setUrl(parameters.getUrl()); |
187 |
params.setUseSSL(parameters.getUseSSL()); |
188 |
|
189 |
params.setDefaultFeatureType(this.getServerExplorerProviderServices()
|
190 |
.createNewFeatureType()); |
191 |
|
192 |
return params;
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* Geometry support
|
197 |
*/
|
198 |
public boolean hasGeometrySupport() { |
199 |
return true; |
200 |
} |
201 |
|
202 |
/**
|
203 |
*
|
204 |
*/
|
205 |
public List list(final int mode, final boolean showInformationDBTables) |
206 |
throws DataException {
|
207 |
|
208 |
final JDBCStoreParameters orgParams = createStoreParams();
|
209 |
|
210 |
ConnectionAction action = new ConnectionAction() {
|
211 |
|
212 |
public Object action(Connection conn) throws DataException { |
213 |
ResultSet rs = null; |
214 |
Statement st = null; |
215 |
List sqls = getSQLForList(mode, showInformationDBTables);
|
216 |
|
217 |
try {
|
218 |
JDBCStoreParameters params; |
219 |
|
220 |
List paramList = new ArrayList(); |
221 |
|
222 |
conn = helper.getConnection(); |
223 |
st = conn.createStatement(); |
224 |
String sql;
|
225 |
Iterator sqlIter = sqls.iterator();
|
226 |
while (sqlIter.hasNext()) {
|
227 |
sql = (String) sqlIter.next();
|
228 |
rs = st.executeQuery(sql); |
229 |
while (rs.next()) {
|
230 |
params = (JDBCStoreParameters) orgParams.getCopy(); |
231 |
params.setTable(rs.getString(1));
|
232 |
paramList.add(params); |
233 |
} |
234 |
} |
235 |
|
236 |
return paramList;
|
237 |
} catch (SQLException e) { |
238 |
throw new JDBCSQLException(e); |
239 |
} finally {
|
240 |
try {
|
241 |
rs.close(); |
242 |
} catch (Exception e) { |
243 |
} |
244 |
; |
245 |
try {
|
246 |
st.close(); |
247 |
} catch (Exception e) { |
248 |
} |
249 |
; |
250 |
} |
251 |
} |
252 |
|
253 |
}; |
254 |
|
255 |
try {
|
256 |
return (List) helper.doConnectionAction(action); |
257 |
} catch (Exception e) { |
258 |
throw new ReadException(this.getName(), e); |
259 |
} |
260 |
} |
261 |
|
262 |
public boolean add(NewDataStoreParameters ndsp, boolean overwrite) |
263 |
throws DataException {
|
264 |
|
265 |
if (!(ndsp instanceof NewFeatureStoreParameters)) { |
266 |
throw new IllegalArgumentException(); |
267 |
} |
268 |
checkIsMine(ndsp); |
269 |
|
270 |
NewFeatureStoreParameters nfdsp = (NewFeatureStoreParameters) ndsp; |
271 |
|
272 |
StringBuilder sql = new StringBuilder(); |
273 |
|
274 |
if (!nfdsp.isValid()) {
|
275 |
throw new InitializeException(this.getName(), new Exception( |
276 |
"Parameters not valid"));
|
277 |
} |
278 |
try {
|
279 |
nfdsp.validate(); |
280 |
} catch (ValidateDataParametersException e1) {
|
281 |
throw new InitializeException(this.getName(), e1); |
282 |
} |
283 |
|
284 |
FeatureType fType = nfdsp.getDefaultFeatureType(); |
285 |
|
286 |
sql.append("CREATE TABLE " + ((JDBCStoreParameters) ndsp).tableID()
|
287 |
+ "(");
|
288 |
Iterator<FeatureAttributeDescriptor> attrs = fType.iterator();
|
289 |
String sqlAttr;
|
290 |
List<String> sqlAttrs = new ArrayList<String>(); |
291 |
|
292 |
while (attrs.hasNext()) {
|
293 |
sqlAttr = helper |
294 |
.getSqlFieldDescription((FeatureAttributeDescriptor) attrs |
295 |
.next()); |
296 |
if (sqlAttr != null) { |
297 |
sqlAttrs.add(sqlAttr); |
298 |
} |
299 |
} |
300 |
|
301 |
helper.stringJoin(sqlAttrs, ", ", sql);
|
302 |
|
303 |
String pk = "CONSTRAINT " |
304 |
+ getDerivedNAme(((JDBCStoreParameters) ndsp).tableID(), "PK")
|
305 |
+ " PRIMARY KEY (\""
|
306 |
+ OracleValues.DEFAULT_ID_FIELD_CASE_SENSITIVE |
307 |
+ "\") ENABLE";
|
308 |
|
309 |
sql.append(", ");
|
310 |
sql.append(pk); |
311 |
|
312 |
sql.append(")");
|
313 |
|
314 |
final String sqlCreate = sql.toString(); |
315 |
|
316 |
TransactionalAction action = new TransactionalAction() {
|
317 |
|
318 |
public boolean continueTransactionAllowed() { |
319 |
return false; |
320 |
} |
321 |
|
322 |
public Object action(Connection conn) throws DataException { |
323 |
Statement st = null; |
324 |
|
325 |
try {
|
326 |
st = conn.createStatement(); |
327 |
} catch (SQLException e1) { |
328 |
throw new JDBCSQLException(e1); |
329 |
} |
330 |
String sql = null; |
331 |
|
332 |
try {
|
333 |
sql = sqlCreate; |
334 |
st.execute(sql); |
335 |
|
336 |
} catch (SQLException e) { |
337 |
throw new JDBCExecuteSQLException(sql, e); |
338 |
} finally {
|
339 |
try {
|
340 |
st.close(); |
341 |
} catch (SQLException e) { |
342 |
logger.error("Exception clossing statement", e);
|
343 |
} |
344 |
; |
345 |
} |
346 |
|
347 |
return Boolean.TRUE; |
348 |
} |
349 |
|
350 |
}; |
351 |
|
352 |
Boolean result = Boolean.FALSE; |
353 |
|
354 |
try {
|
355 |
result = (Boolean) helper.doConnectionAction(action);
|
356 |
} catch (Exception e) { |
357 |
throw new RuntimeException(e); |
358 |
} |
359 |
|
360 |
return result.booleanValue();
|
361 |
} |
362 |
|
363 |
/**
|
364 |
* create helper
|
365 |
*/
|
366 |
protected JDBCHelper createHelper() throws InitializeException { |
367 |
return new OracleHelper(this, getOracleSpatialParameters()); |
368 |
} |
369 |
|
370 |
/**
|
371 |
* get store name
|
372 |
*/
|
373 |
protected String getStoreName() { |
374 |
return OracleStoreProvider.NAME;
|
375 |
} |
376 |
|
377 |
/**
|
378 |
* get helper
|
379 |
*
|
380 |
* @return
|
381 |
*/
|
382 |
protected OracleHelper getOSHelper() {
|
383 |
return (OracleHelper) getHelper();
|
384 |
} |
385 |
|
386 |
/**
|
387 |
* get sql sentence for list available tables
|
388 |
*/
|
389 |
protected List<String> getSQLForList(int mode, |
390 |
boolean showInformationDBTables) {
|
391 |
List<String> list = new ArrayList<String>(1); |
392 |
list.add("SELECT TABLE_NAME FROM USER_SDO_GEOM_METADATA");
|
393 |
|
394 |
return list;
|
395 |
|
396 |
} |
397 |
|
398 |
/**
|
399 |
* check parameters
|
400 |
*/
|
401 |
protected void checkIsMine(DataStoreParameters dsp) { |
402 |
if (!(dsp instanceof OracleStoreParameters)) { |
403 |
throw new IllegalArgumentException( |
404 |
"not instance of OracleSpatialStoreParameters");
|
405 |
} |
406 |
super.checkIsMine(dsp);
|
407 |
|
408 |
OracleStoreParameters orap = (OracleStoreParameters) dsp; |
409 |
if (orap.getUseSSL().booleanValue() != getOracleSpatialParameters()
|
410 |
.getUseSSL()) { |
411 |
throw new IllegalArgumentException("worng explorer: Host"); |
412 |
} |
413 |
} |
414 |
|
415 |
/**
|
416 |
* Create store parameters
|
417 |
*/
|
418 |
protected JDBCStoreParameters createStoreParams()
|
419 |
throws InitializeException, ProviderNotRegisteredException {
|
420 |
OracleStoreParameters orgParams = (OracleStoreParameters) super
|
421 |
.createStoreParams(); |
422 |
|
423 |
orgParams.setUseSSL(getOracleSpatialParameters().getUseSSL()); |
424 |
|
425 |
return orgParams;
|
426 |
} |
427 |
|
428 |
/**
|
429 |
*
|
430 |
* @param tname
|
431 |
* @param suffix
|
432 |
* @return
|
433 |
*/
|
434 |
private static String getDerivedNAme(String tname, String suffix) { |
435 |
|
436 |
int ind = tname.lastIndexOf("."); |
437 |
if (ind == -1) { |
438 |
|
439 |
int l = Math.min(28, tname.length()); |
440 |
return tname.substring(0, l) + "_" + suffix; |
441 |
|
442 |
} else {
|
443 |
|
444 |
String pre = tname.substring(0, ind); |
445 |
String post = tname.substring(ind + 1, tname.length()); |
446 |
int lpost = Math.min(24, post.length()); |
447 |
int lpre = Math.min(3, pre.length()); |
448 |
return pre.substring(0, lpre) + "_" + post.substring(0, lpost) |
449 |
+ "_" + suffix;
|
450 |
} |
451 |
|
452 |
} |
453 |
|
454 |
/**
|
455 |
* Get server explorer parameters
|
456 |
*
|
457 |
* @return
|
458 |
*/
|
459 |
private OracleServerExplorerParameters getOracleSpatialParameters() {
|
460 |
return (OracleServerExplorerParameters) getParameters();
|
461 |
} |
462 |
|
463 |
} |