Statistics
| Revision:

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

History | View | Annotate | Download (6.11 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

    
37
import org.gvsig.fmap.dal.DataStoreParameters;
38
import org.gvsig.fmap.dal.NewDataStoreParameters;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.InitializeException;
41
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
42
import org.gvsig.fmap.dal.exception.RemoveException;
43
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
44
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
45
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
46
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
47
import org.gvsig.fmap.dal.store.jdbc.TransactionalAction;
48
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
49
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

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

    
61
        public static final String NAME = "PostgreSQLExplorer";
62

    
63

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

    
71
        private PostgreSQLServerExplorerParameters getPostgreSQLParameters() {
72
                return (PostgreSQLServerExplorerParameters) getParameters();
73
        }
74

    
75

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

    
80

    
81
        protected String getStoreName() {
82
                return PostgreSQLStoreProvider.NAME;
83
        }
84

    
85
        public String getProviderName() {
86
                return NAME;
87
        }
88

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

    
94
                orgParams.setUseSSL(getPostgreSQLParameters().getUseSSL());
95

    
96
                return orgParams;
97
        }
98

    
99

    
100
        // ****************************
101

    
102

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

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

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

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

    
125
                TransactionalAction action = new TransactionalAction() {
126
                        public boolean continueTransactionAllowed() {
127
                                return false;
128
                        }
129
                        public Object action(Connection conn) throws DataException {
130

    
131

    
132
                                Statement st;
133
                                try{
134
                                        st = conn.createStatement();
135
                                } catch (SQLException e) {
136
                                        throw new JDBCSQLException(e);
137
                                }
138

    
139
                                String sqlDrop = "Drop table "
140
                                        + pgParams.tableID();
141

    
142
                                StringBuilder strb = new StringBuilder();
143
                                strb.append("Delete from GEOMETRY_COLUMNS where f_table_schema = ");
144
                                if (pgParams.getSchema() == null || pgParams.getSchema().length() ==  0) {
145
                                        strb.append("current_schema() ");
146
                                } else {
147
                                        strb.append('\'');
148
                                        strb.append(pgParams.getSchema());
149
                                        strb.append("' ");
150
                                }
151
                                strb.append("and f_table_name = '");
152
                                strb.append(pgParams.getTable());
153
                                strb.append('\'');
154

    
155
                                String sqlDeleteFromGeometry_column = strb.toString();
156
                                try{
157
                                        try{
158
                                                st.execute(sqlDrop);
159
                                        } catch (SQLException e) {
160
                                                throw new JDBCExecuteSQLException(sqlDrop, e);
161
                                        }
162

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

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

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

    
197

    
198
                params.setDefaultFeatureType(this.getServerExplorerProviderServices()
199
                                .createNewFeatureType());
200

    
201

    
202
                return params;
203
        }
204

    
205

    
206

    
207
        // ***********************
208
        // ***********************
209

    
210

    
211
        public boolean hasGeometrySupport() {
212
                return true;
213
        }
214

    
215
        protected PostgreSQLHelper getPgHelper() {
216
                return (PostgreSQLHelper) getHelper();
217
        }
218

    
219
}