Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extOracleSpatial / src / org / gvsig / fmap / dal / store / oraclespatial / OracleSpatialServerExplorer.java @ 29579

History | View | Annotate | Download (8.06 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.oraclespatial;
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.spi.DataServerExplorerProviderServices;
49
import org.gvsig.fmap.dal.store.jdbc.ConnectionAction;
50
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
51
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
52
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
53
import org.gvsig.fmap.dal.store.jdbc.TransactionalAction;
54
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
55
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

    
59
/**
60
 * @author vsanjaime
61
 * 
62
 */
63
public class OracleSpatialServerExplorer extends JDBCServerExplorer {
64
        final static private Logger logger = LoggerFactory
65
                        .getLogger(OracleSpatialServerExplorer.class);
66

    
67
        public static final String NAME = "OracleSpatialExplorer";
68

    
69
        public OracleSpatialServerExplorer(
70
                        OracleSpatialServerExplorerParameters parameters,
71
                        DataServerExplorerProviderServices services)
72
                        throws InitializeException {
73
                super(parameters, services);
74
        }
75

    
76
        private OracleSpatialServerExplorerParameters getOracleSpatialParameters() {
77
                return (OracleSpatialServerExplorerParameters) getParameters();
78
        }
79

    
80
        protected JDBCHelper createHelper() throws InitializeException {
81
                return new OracleSpatialHelper(this, getOracleSpatialParameters());
82
        }
83

    
84
        protected String getStoreName() {
85
                return OracleSpatialStoreProvider.NAME;
86
        }
87

    
88
        public String getName() {
89
                return NAME;
90
        }
91

    
92
        protected JDBCStoreParameters createStoreParams()
93
                        throws InitializeException, ProviderNotRegisteredException {
94
                OracleSpatialStoreParameters orgParams = (OracleSpatialStoreParameters) super
95
                                .createStoreParams();
96

    
97
                orgParams.setUseSSL(getOracleSpatialParameters().getUseSSL());
98

    
99
                return orgParams;
100
        }
101

    
102
        // ****************************
103

    
104
        public boolean canAdd() {
105
                return true;
106
        }
107

    
108
        /**
109
         * 
110
         */
111
        protected void checkIsMine(DataStoreParameters dsp) {
112
                if (!(dsp instanceof OracleSpatialStoreParameters)) {
113
                        // FIXME Excpetion ???
114
                        throw new IllegalArgumentException(
115
                                        "not instance of OracleSpatialStoreParameters");
116
                }
117
                super.checkIsMine(dsp);
118

    
119
                OracleSpatialStoreParameters orap = (OracleSpatialStoreParameters) dsp;
120
                if (orap.getUseSSL().booleanValue() != getOracleSpatialParameters()
121
                                .getUseSSL()) {
122
                        throw new IllegalArgumentException("worng explorer: Host");
123
                }
124
        }
125

    
126
        /**
127
         * 
128
         */
129
        public void remove(DataStoreParameters dsp) throws RemoveException {
130
                final OracleSpatialStoreParameters osParams = (OracleSpatialStoreParameters) dsp;
131

    
132
                TransactionalAction action = new TransactionalAction() {
133
                        public boolean continueTransactionAllowed() {
134
                                return false;
135
                        }
136

    
137
                        public Object action(Connection conn) throws DataException {
138

    
139
                                Statement st;
140
                                try {
141
                                        st = conn.createStatement();
142
                                } catch (SQLException e) {
143
                                        throw new JDBCSQLException(e);
144
                                }
145

    
146
                                String sqlDrop = "Drop table " + osParams.tableID();
147

    
148
                                StringBuilder strb = new StringBuilder();
149
                                strb
150
                                                .append("Delete from GEOMETRY_COLUMNS where f_table_schema = ");
151
                                if (osParams.getSchema() == null
152
                                                || osParams.getSchema().length() == 0) {
153
                                        strb.append("current_schema() ");
154
                                } else {
155
                                        strb.append('\'');
156
                                        strb.append(osParams.getSchema());
157
                                        strb.append("' ");
158
                                }
159
                                strb.append("and f_table_name = '");
160
                                strb.append(osParams.getTable());
161
                                strb.append('\'');
162

    
163
                                String sqlDeleteFromGeometry_column = strb.toString();
164
                                try {
165
                                        try {
166
                                                st.execute(sqlDrop);
167
                                        } catch (SQLException e) {
168
                                                throw new JDBCExecuteSQLException(sqlDrop, e);
169
                                        }
170

    
171
                                        try {
172
                                                st.execute(sqlDeleteFromGeometry_column);
173
                                        } catch (SQLException e) {
174
                                                throw new JDBCExecuteSQLException(
175
                                                                sqlDeleteFromGeometry_column, e);
176
                                        }
177

    
178
                                } finally {
179
                                        try {
180
                                                st.close();
181
                                        } catch (SQLException e) {
182
                                        }
183
                                        ;
184
                                }
185
                                return null;
186
                        }
187
                };
188
                try {
189
                        this.helper.doConnectionAction(action);
190
                } catch (Exception e) {
191
                        throw new RemoveException(this.getName(), e);
192
                }
193
        }
194

    
195
        /**
196
         * 
197
         */
198
        public NewDataStoreParameters getAddParameters() throws DataException {
199
                OracleSpatialServerExplorerParameters parameters = getOracleSpatialParameters();
200
                OracleSpatialNewStoreParameters params = new OracleSpatialNewStoreParameters();
201
                params.setHost(parameters.getHost());
202
                params.setPort(parameters.getPort());
203
                params.setDBName(parameters.getDBName());
204
                params.setUser(parameters.getUser());
205
                params.setPassword(parameters.getPassword());
206
                params.setCatalog(parameters.getCatalog());
207
                params.setSchema(parameters.getSchema());
208
                params.setJDBCDriverClassName(parameters.getJDBCDriverClassName());
209
                params.setUrl(parameters.getUrl());
210
                params.setUseSSL(parameters.getUseSSL());
211

    
212
                params.setDefaultFeatureType(this.getServerExplorerProviderServices()
213
                                .createNewFeatureType());
214

    
215
                return params;
216
        }
217

    
218
        /**
219
         * Geometry support
220
         */
221
        public boolean hasGeometrySupport() {
222
                return true;
223
        }
224

    
225
        /**
226
         * get helper
227
         * 
228
         * @return
229
         */
230
        protected OracleSpatialHelper getOSHelper() {
231
                return (OracleSpatialHelper) getHelper();
232
        }
233

    
234
        /**
235
         * get sql sentence for list available tables
236
         */
237
        protected List<String> getSQLForList(int mode,
238
                        boolean showInformationDBTables) {
239
                List<String> list = new ArrayList<String>(1);
240
                list.add("SELECT TABLE_NAME FROM USER_SDO_GEOM_METADATA");
241

    
242
                return list;
243

    
244
        }
245
        
246
        public List list(final int mode, final boolean showInformationDBTables)
247
        throws DataException {
248

    
249
final JDBCStoreParameters orgParams = createStoreParams();
250

    
251
ConnectionAction action = new ConnectionAction() {
252

    
253
        public Object action(Connection conn) throws DataException {
254
                ResultSet rs = null;
255
                Statement st = null;
256
                List sqls = getSQLForList(mode, showInformationDBTables);
257

    
258
                try {
259
                        JDBCStoreParameters params;
260

    
261
                        List paramList = new ArrayList();
262

    
263
                        conn = helper.getConnection();
264
                        st = conn.createStatement();
265
                        String sql;
266
                        Iterator sqlIter = sqls.iterator();
267
                        while (sqlIter.hasNext()){
268
                                sql = (String) sqlIter.next();
269
                                rs = st.executeQuery(sql);
270
                                while (rs.next()) {
271
                                        params = (JDBCStoreParameters) orgParams
272
                                                        .getCopy();
273
                                        params.setTable(rs.getString(1));
274
                                        // TODO
275
//                                        if (rs.getBoolean(4)) {
276
//                                                param.setIsView(param.IS_VIEW);
277
//                                        } else {
278
//                                                param.setIsView(param.NOT_IS_VIEW);
279
//                                        }
280
                                        paramList.add(params);
281
                                }
282
                        }
283

    
284
                        return paramList;
285
                } catch (SQLException e) {
286
                        throw new JDBCSQLException(e);
287
                } finally {
288
                        try {rs.close();} catch (Exception e) {        };
289
                        try {st.close();} catch (Exception e) {        };
290
                }
291
        }
292

    
293
};
294

    
295
try {
296
        return (List) helper.doConnectionAction(action);
297
} catch (Exception e) {
298
        throw new ReadException(this.getName(), e);
299
}
300
}
301

    
302
        
303

    
304
}