Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / postgresql / PostgreSQLServerExplorer.java @ 30754

History | View | Annotate | Download (6.95 KB)

1 27906 jmvivo
/* 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 27919 jmvivo
package org.gvsig.fmap.dal.store.postgresql;
32 27906 jmvivo
33
import java.sql.Connection;
34
import java.sql.SQLException;
35
import java.sql.Statement;
36 28968 jmvivo
import java.util.ArrayList;
37
import java.util.List;
38 27906 jmvivo
39
import org.gvsig.fmap.dal.DataStoreParameters;
40
import org.gvsig.fmap.dal.NewDataStoreParameters;
41
import org.gvsig.fmap.dal.exception.DataException;
42
import org.gvsig.fmap.dal.exception.InitializeException;
43 28909 jmvivo
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
44 27906 jmvivo
import org.gvsig.fmap.dal.exception.RemoveException;
45 29445 jmvivo
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
46 28909 jmvivo
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
47 27906 jmvivo
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
48 28909 jmvivo
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
49 28784 jmvivo
import org.gvsig.fmap.dal.store.jdbc.TransactionalAction;
50 28785 jmvivo
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
51
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
52 27906 jmvivo
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54
55
/**
56
 * @author jmvivo
57
 *
58
 */
59 28784 jmvivo
public class PostgreSQLServerExplorer extends JDBCServerExplorer {
60 27906 jmvivo
        final static private Logger logger = LoggerFactory
61
                        .getLogger(PostgreSQLServerExplorer.class);
62
63 27953 jmvivo
        public static final String NAME = "PostgreSQLExplorer";
64 27906 jmvivo
65
66
        public PostgreSQLServerExplorer(
67 29445 jmvivo
                        PostgreSQLServerExplorerParameters parameters,
68
                        DataServerExplorerProviderServices services)
69 27906 jmvivo
                        throws InitializeException {
70 29445 jmvivo
                super(parameters, services);
71 28909 jmvivo
        }
72 27906 jmvivo
73 29445 jmvivo
        private PostgreSQLServerExplorerParameters getPostgreSQLParameters() {
74
                return (PostgreSQLServerExplorerParameters) getParameters();
75 27906 jmvivo
        }
76
77 29445 jmvivo
78 28909 jmvivo
        protected JDBCHelper createHelper() throws InitializeException {
79 29445 jmvivo
                return new PostgreSQLHelper(this, getPostgreSQLParameters());
80 28909 jmvivo
        }
81 27906 jmvivo
82 28909 jmvivo
83 27906 jmvivo
        protected String getStoreName() {
84
                return PostgreSQLStoreProvider.NAME;
85
        }
86
87
        public String getName() {
88
                return NAME;
89
        }
90
91 28909 jmvivo
        protected JDBCStoreParameters createStoreParams()
92
                        throws InitializeException, ProviderNotRegisteredException {
93
                PostgreSQLStoreParameters orgParams = (PostgreSQLStoreParameters) super
94
                                .createStoreParams();
95 27906 jmvivo
96 29445 jmvivo
                orgParams.setUseSSL(getPostgreSQLParameters().getUseSSL());
97 27908 jmvivo
98 28909 jmvivo
                return orgParams;
99 27906 jmvivo
        }
100
101 27908 jmvivo
102
        // ****************************
103
104
105
        public boolean canAdd() {
106 28909 jmvivo
                return true;
107 27906 jmvivo
        }
108
109 28909 jmvivo
        protected void checkIsMine(DataStoreParameters dsp) {
110 28257 jmvivo
                if (!(dsp instanceof PostgreSQLStoreParameters)) {
111
                        // FIXME Excpetion ???
112
                        throw new IllegalArgumentException(
113 28909 jmvivo
                                        "not instance of PostgreSQLStoreParameters");
114 28257 jmvivo
                }
115 28909 jmvivo
                super.checkIsMine(dsp);
116
117 28257 jmvivo
                PostgreSQLStoreParameters pgp = (PostgreSQLStoreParameters) dsp;
118 29445 jmvivo
                if (pgp.getUseSSL().booleanValue() != getPostgreSQLParameters()
119
                                .getUseSSL()) {
120 28257 jmvivo
                        throw new IllegalArgumentException("worng explorer: Host");
121
                }
122
        }
123
124 27906 jmvivo
        public void remove(DataStoreParameters dsp) throws RemoveException {
125 28257 jmvivo
                final PostgreSQLStoreParameters pgParams =(PostgreSQLStoreParameters) dsp;
126 27906 jmvivo
127 28257 jmvivo
                TransactionalAction action = new TransactionalAction() {
128
                        public boolean continueTransactionAllowed() {
129
                                return false;
130
                        }
131
                        public Object action(Connection conn) throws DataException {
132
133
134
                                Statement st;
135
                                try{
136
                                        st = conn.createStatement();
137
                                } catch (SQLException e) {
138
                                        throw new JDBCSQLException(e);
139
                                }
140
141
                                String sqlDrop = "Drop table "
142
                                        + pgParams.tableID();
143
144
                                StringBuilder strb = new StringBuilder();
145
                                strb.append("Delete from GEOMETRY_COLUMNS where f_table_schema = ");
146
                                if (pgParams.getSchema() == null || pgParams.getSchema().length() ==  0) {
147
                                        strb.append("current_schema() ");
148
                                } else {
149
                                        strb.append('\'');
150
                                        strb.append(pgParams.getSchema());
151
                                        strb.append("' ");
152
                                }
153
                                strb.append("and f_table_name = '");
154
                                strb.append(pgParams.getTable());
155
                                strb.append('\'');
156
157
                                String sqlDeleteFromGeometry_column = strb.toString();
158
                                try{
159
                                        try{
160
                                                st.execute(sqlDrop);
161
                                        } catch (SQLException e) {
162
                                                throw new JDBCExecuteSQLException(sqlDrop, e);
163
                                        }
164
165
                                        try {
166
                                                st.execute(sqlDeleteFromGeometry_column);
167
                                        } catch (SQLException e) {
168
                                                throw new JDBCExecuteSQLException(
169
                                                                sqlDeleteFromGeometry_column, e);
170
                                        }
171
172
                                } finally{
173
                                        try{ st.close(); } catch (SQLException e) {};
174
                                }
175
                                return null;
176
                        }
177
                };
178
                try {
179
                        this.helper.doConnectionAction(action);
180
                } catch (Exception e) {
181
                        throw new RemoveException(this.getName(), e);
182
                }
183 27906 jmvivo
        }
184
185
        public NewDataStoreParameters getAddParameters() throws DataException {
186 29445 jmvivo
                PostgreSQLServerExplorerParameters parameters = getPostgreSQLParameters();
187 28257 jmvivo
                PostgreSQLNewStoreParameters params = new PostgreSQLNewStoreParameters();
188 29445 jmvivo
                params.setHost(parameters.getHost());
189
                params.setPort(parameters.getPort());
190
                params.setDBName(parameters.getDBName());
191
                params.setUser(parameters.getUser());
192
                params.setPassword(parameters.getPassword());
193
                params.setCatalog(parameters.getCatalog());
194
                params.setSchema(parameters.getSchema());
195
                params.setJDBCDriverClassName(parameters.getJDBCDriverClassName());
196
                params.setUrl(parameters.getUrl());
197
                params.setUseSSL(parameters.getUseSSL());
198 28257 jmvivo
199
200
                params.setDefaultFeatureType(this.getServerExplorerProviderServices()
201
                                .createNewFeatureType());
202
203
204
                return params;
205 27906 jmvivo
        }
206
207
208
209
        // ***********************
210
        // ***********************
211
212
213 28784 jmvivo
        public boolean hasGeometrySupport() {
214
                return true;
215
        }
216 27982 jmvivo
217 28909 jmvivo
        protected PostgreSQLHelper getPgHelper() {
218
                return (PostgreSQLHelper) getHelper();
219
        }
220 27982 jmvivo
221 28784 jmvivo
222 28968 jmvivo
        protected List getSQLForList(int mode, boolean showInformationDBTables) {
223
                List list = new ArrayList(1);
224 28917 jmvivo
                StringBuffer sqlBuf = new StringBuffer();
225
                sqlBuf
226
                                .append("SELECT null as TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, false as ISVIEW ");
227
                sqlBuf.append(" FROM INFORMATION_SCHEMA.TABLES ");
228
                sqlBuf.append(" xxWHERExx ");
229
                sqlBuf.append(" union ");
230
                sqlBuf
231
                                .append("SELECT null as TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, true as ISVIEW ");
232
                sqlBuf.append(" FROM INFORMATION_SCHEMA.VIEWS ");
233
                sqlBuf.append(" xxWHERExx ");
234
235
                if (showInformationDBTables) {
236 28968 jmvivo
                        list.add(sqlBuf.toString().replaceAll("xxWHERExx", ""));
237 28917 jmvivo
                } else {
238 28968 jmvivo
                        list
239
                                        .add(sqlBuf
240 28917 jmvivo
                                        .toString()
241
                                        .replaceAll("xxWHERExx",
242 28968 jmvivo
                                                        "WHERE TABLE_SCHEMA NOT IN ('information_schema','pg_catalog')"));
243 28917 jmvivo
244
                }
245 28968 jmvivo
                return list;
246 28917 jmvivo
247
248
        }
249 27906 jmvivo
}