Statistics
| Revision:

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

History | View | Annotate | Download (8.15 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
package org.gvsig.fmap.dal.store.postgresql;
31

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

    
38
import org.gvsig.fmap.dal.DataStoreParameters;
39
import org.gvsig.fmap.dal.NewDataStoreParameters;
40
import org.gvsig.fmap.dal.exception.DataException;
41
import org.gvsig.fmap.dal.exception.InitializeException;
42
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
43
import org.gvsig.fmap.dal.exception.RemoveException;
44
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
45
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
46
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer;
47
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
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

    
61
    final static private Logger logger = LoggerFactory
62
            .getLogger(PostgreSQLServerExplorer.class);
63

    
64
    public static final String NAME = "PostgreSQLExplorer";
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
    protected JDBCHelper createHelper() throws InitializeException {
78
        return new PostgreSQLHelper(this, getPostgreSQLParameters());
79
    }
80

    
81
    public 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
    public boolean canAdd() {
101
        return true;
102
    }
103

    
104
    public DataStoreParameters getOpenParameters() throws DataException {
105
        PostgreSQLServerExplorerParameters parameters = getPostgreSQLParameters();
106
        PostgreSQLStoreParameters params = new PostgreSQLStoreParameters();
107
        params.setHost(parameters.getHost());
108
        params.setPort(parameters.getPort());
109
        params.setDBName(parameters.getDBName());
110
        params.setUser(parameters.getUser());
111
        params.setPassword(parameters.getPassword());
112
        params.setCatalog(parameters.getCatalog());
113
        params.setSchema(parameters.getSchema());
114
        params.setJDBCDriverClassName(parameters.getJDBCDriverClassName());
115
        params.setUrl(parameters.getUrl());
116
        return params;
117
    }
118

    
119
    protected void checkIsMine(DataStoreParameters dsp) {
120
        if (!(dsp instanceof PostgreSQLConnectionParameters)) {
121
            // FIXME Excpetion ???
122
            throw new IllegalArgumentException(
123
                    "not instance of PostgreSQLStoreParameters");
124
        }
125
        super.checkIsMine(dsp);
126

    
127
        PostgreSQLConnectionParameters pgp = (PostgreSQLConnectionParameters) dsp;
128
        if (pgp.getUseSSL().booleanValue() != getPostgreSQLParameters()
129
                .getUseSSL()) {
130
            throw new IllegalArgumentException("worng explorer: Host");
131
        }
132
    }
133

    
134
    public void remove(DataStoreParameters dsp) throws RemoveException {
135
        final PostgreSQLStoreParameters pgParams = (PostgreSQLStoreParameters) dsp;
136

    
137
        TransactionalAction action = new TransactionalAction() {
138
            public boolean continueTransactionAllowed() {
139
                return false;
140
            }
141

    
142
            public Object action(Connection conn) throws DataException {
143

    
144
                Statement st;
145
                try {
146
                    st = conn.createStatement();
147
                } catch (SQLException e) {
148
                    throw new JDBCSQLException(e);
149
                }
150

    
151
                String sqlDrop = "Drop table "
152
                        + pgParams.tableID();
153

    
154
                StringBuilder strb = new StringBuilder();
155
                strb.append("Delete from GEOMETRY_COLUMNS where f_table_schema = ");
156
                if (pgParams.getSchema() == null || pgParams.getSchema().length() == 0) {
157
                    strb.append("current_schema() ");
158
                } else {
159
                    strb.append('\'');
160
                    strb.append(pgParams.getSchema());
161
                    strb.append("' ");
162
                }
163
                strb.append("and f_table_name = '");
164
                strb.append(pgParams.getTable());
165
                strb.append('\'');
166

    
167
                String sqlDeleteFromGeometry_column = strb.toString();
168
                try {
169
                    try {
170
                        JDBCHelper.execute(st, sqlDrop);
171
                    } catch (SQLException e) {
172
                        throw new JDBCExecuteSQLException(sqlDrop, e);
173
                    }
174

    
175
                    try {
176
                        JDBCHelper.execute(st, sqlDeleteFromGeometry_column);
177
                    } catch (SQLException e) {
178
                        throw new JDBCExecuteSQLException(
179
                                sqlDeleteFromGeometry_column, e);
180
                    }
181

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

    
198
    public NewDataStoreParameters getAddParameters() throws DataException {
199
        PostgreSQLServerExplorerParameters parameters = getPostgreSQLParameters();
200
        PostgreSQLNewStoreParameters params = new PostgreSQLNewStoreParameters();
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
    public boolean hasGeometrySupport() {
219
        return true;
220
    }
221

    
222
    protected PostgreSQLHelper getPgHelper() {
223
        return (PostgreSQLHelper) getHelper();
224
    }
225

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