Statistics
| Revision:

root / trunk / org.gvsig.postgresql / org.gvsig.postgresql.provider / src / main / java / org / gvsig / fmap / dal / store / postgresql / PostgreSQLServerExplorer.java @ 58

History | View | Annotate | Download (6.48 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.postgresql;
32

    
33
import java.sql.Connection;
34
import java.sql.SQLException;
35
import java.sql.Statement;
36
import java.util.ArrayList;
37
import java.util.List;
38

    
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
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
44
import org.gvsig.fmap.dal.exception.RemoveException;
45
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
46
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
47
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
48
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
49
import org.gvsig.fmap.dal.store.jdbc.TransactionalAction;
50
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
51
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
/**
56
 * @author jmvivo
57
 *
58
 */
59
public class PostgreSQLServerExplorer extends JDBCServerExplorer {
60
        final static private Logger logger = LoggerFactory
61
                        .getLogger(PostgreSQLServerExplorer.class);
62

    
63
        public static final String NAME = "PostgreSQLExplorer";
64

    
65

    
66
        public PostgreSQLServerExplorer(
67
                        PostgreSQLServerExplorerParameters parameters,
68
                        DataServerExplorerProviderServices services)
69
                        throws InitializeException {
70
                super(parameters, services);
71
        }
72

    
73
        private PostgreSQLServerExplorerParameters getPostgreSQLParameters() {
74
                return (PostgreSQLServerExplorerParameters) getParameters();
75
        }
76

    
77

    
78
        protected JDBCHelper createHelper() throws InitializeException {
79
                return new PostgreSQLHelper(this, getPostgreSQLParameters());
80
        }
81

    
82

    
83
        protected String getStoreName() {
84
                return PostgreSQLStoreProvider.NAME;
85
        }
86

    
87
        public String getProviderName() {
88
                return NAME;
89
        }
90

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

    
96
                orgParams.setUseSSL(getPostgreSQLParameters().getUseSSL());
97

    
98
                return orgParams;
99
        }
100

    
101

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

    
104

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

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

    
117
                PostgreSQLStoreParameters pgp = (PostgreSQLStoreParameters) dsp;
118
                if (pgp.getUseSSL().booleanValue() != getPostgreSQLParameters()
119
                                .getUseSSL()) {
120
                        throw new IllegalArgumentException("worng explorer: Host");
121
                }
122
        }
123

    
124
        public void remove(DataStoreParameters dsp) throws RemoveException {
125
                final PostgreSQLStoreParameters pgParams =(PostgreSQLStoreParameters) dsp;
126

    
127
                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
                                                logger.debug("execute: "+sqlDrop);
161
                                                st.execute(sqlDrop);
162
                                        } catch (SQLException e) {
163
                                                throw new JDBCExecuteSQLException(sqlDrop, e);
164
                                        }
165

    
166
                                        try {
167
                                                logger.debug("execute: "+sqlDeleteFromGeometry_column);
168
                                                st.execute(sqlDeleteFromGeometry_column);
169
                                        } catch (SQLException e) {
170
                                                throw new JDBCExecuteSQLException(
171
                                                                sqlDeleteFromGeometry_column, e);
172
                                        }
173

    
174
                                } finally{
175
                                        try{ st.close(); } catch (SQLException e) {};
176
                                }
177
                                return null;
178
                        }
179
                };
180
                try {
181
                        this.helper.doConnectionAction(action);
182
                } catch (Exception e) {
183
                        throw new RemoveException(this.getProviderName(), e);
184
                }
185
        }
186

    
187
        public NewDataStoreParameters getAddParameters() throws DataException {
188
                PostgreSQLServerExplorerParameters parameters = getPostgreSQLParameters();
189
                PostgreSQLNewStoreParameters params = new PostgreSQLNewStoreParameters();
190
                params.setHost(parameters.getHost());
191
                params.setPort(parameters.getPort());
192
                params.setDBName(parameters.getDBName());
193
                params.setUser(parameters.getUser());
194
                params.setPassword(parameters.getPassword());
195
                params.setCatalog(parameters.getCatalog());
196
                params.setSchema(parameters.getSchema());
197
                params.setJDBCDriverClassName(parameters.getJDBCDriverClassName());
198
                params.setUrl(parameters.getUrl());
199
                params.setUseSSL(parameters.getUseSSL());
200

    
201

    
202
                params.setDefaultFeatureType(this.getServerExplorerProviderServices()
203
                                .createNewFeatureType());
204

    
205

    
206
                return params;
207
        }
208

    
209

    
210

    
211
        // ***********************
212
        // ***********************
213

    
214

    
215
        public boolean hasGeometrySupport() {
216
                return true;
217
        }
218

    
219
        protected PostgreSQLHelper getPgHelper() {
220
                return (PostgreSQLHelper) getHelper();
221
        }
222

    
223
        @Override
224
        public List getDataStoreProviderNames() {
225
                        List x = new ArrayList(1);
226
                        x.add(PostgreSQLStoreProvider.NAME);
227
                        return x;
228
        }
229
}