Statistics
| Revision:

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

History | View | Annotate | Download (9.25 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.text.MessageFormat;
36
import java.util.ArrayList;
37
import java.util.Iterator;
38
import java.util.List;
39
import java.util.logging.Level;
40
import org.apache.commons.lang3.StringUtils;
41

    
42
import org.gvsig.fmap.dal.DataStoreParameters;
43
import org.gvsig.fmap.dal.NewDataStoreParameters;
44
import org.gvsig.fmap.dal.exception.DataException;
45
import org.gvsig.fmap.dal.exception.InitializeException;
46
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
47
import org.gvsig.fmap.dal.exception.RemoveException;
48
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
49
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
50
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper;
51
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerBase;
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 jmvivo
61
 *
62
 */
63
public class PostgreSQLServerExplorer extends JDBCServerExplorerBase {
64

    
65
    final static private Logger logger = LoggerFactory
66
            .getLogger(PostgreSQLServerExplorer.class);
67

    
68
    public static final String NAME = "PostgreSQLExplorer";
69

    
70
    public PostgreSQLServerExplorer(
71
            PostgreSQLServerExplorerParameters parameters,
72
            DataServerExplorerProviderServices services)
73
            throws InitializeException {
74
        super(parameters, services);
75
    }
76

    
77
    private PostgreSQLServerExplorerParameters getPostgreSQLParameters() {
78
        return (PostgreSQLServerExplorerParameters) getParameters();
79
    }
80

    
81
    protected JDBCHelper createHelper() throws InitializeException {
82
        return new PostgreSQLHelper(this, getPostgreSQLParameters());
83
    }
84

    
85
    public String getStoreName() {
86
        return PostgreSQLStoreProvider.NAME;
87
    }
88

    
89
    public String getProviderName() {
90
        return NAME;
91
    }
92

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

    
98
        orgParams.setUseSSL(getPostgreSQLParameters().getUseSSL());
99

    
100
        return orgParams;
101
    }
102

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

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

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

    
131
        PostgreSQLConnectionParameters pgp = (PostgreSQLConnectionParameters) dsp;
132
        if (pgp.getUseSSL().booleanValue() != getPostgreSQLParameters()
133
                .getUseSSL()) {
134
            throw new IllegalArgumentException("worng explorer: Host");
135
        }
136
    }
137

    
138
    public void remove(DataStoreParameters dsp) throws RemoveException {
139
        final PostgreSQLStoreParameters pgParams = (PostgreSQLStoreParameters) dsp;
140

    
141
        TransactionalAction action = new TransactionalAction() {
142
            public boolean continueTransactionAllowed() {
143
                return false;
144
            }
145

    
146
            public Object action(Connection conn) throws DataException {
147

    
148
                Statement st;
149
                try {
150
                    st = conn.createStatement();
151
                } catch (SQLException e) {
152
                    throw new JDBCSQLException(e);
153
                }
154

    
155
                String sqlDrop = "Drop table "
156
                        + pgParams.tableID();
157

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

    
171
                String sqlDeleteFromGeometry_column = strb.toString();
172
                try {
173
                    try {
174
                        JDBCHelper.execute(st, sqlDrop);
175
                    } catch (SQLException e) {
176
                        throw new JDBCExecuteSQLException(sqlDrop, e);
177
                    }
178

    
179
                    try {
180
                        JDBCHelper.execute(st, sqlDeleteFromGeometry_column);
181
                    } catch (SQLException e) {
182
                        throw new JDBCExecuteSQLException(
183
                                sqlDeleteFromGeometry_column, e);
184
                    }
185

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

    
202
    public NewDataStoreParameters getAddParameters() throws DataException {
203
        PostgreSQLServerExplorerParameters parameters = getPostgreSQLParameters();
204
        PostgreSQLNewStoreParameters params = new PostgreSQLNewStoreParameters();
205
        params.setHost(parameters.getHost());
206
        params.setPort(parameters.getPort());
207
        params.setDBName(parameters.getDBName());
208
        params.setUser(parameters.getUser());
209
        params.setPassword(parameters.getPassword());
210
        params.setCatalog(parameters.getCatalog());
211
        params.setSchema(parameters.getSchema());
212
        params.setJDBCDriverClassName(parameters.getJDBCDriverClassName());
213
        params.setUrl(parameters.getUrl());
214
        params.setUseSSL(parameters.getUseSSL());
215

    
216
        params.setDefaultFeatureType(this.getServerExplorerProviderServices()
217
                .createNewFeatureType());
218

    
219
        return params;
220
    }
221

    
222
    public boolean hasGeometrySupport() {
223
        return true;
224
    }
225

    
226
    protected PostgreSQLHelper getPgHelper() {
227
        return (PostgreSQLHelper) getHelper();
228
    }
229

    
230
    @Override
231
    public List getDataStoreProviderNames() {
232
        List x = new ArrayList(1);
233
        x.add(PostgreSQLStoreProvider.NAME);
234
        return x;
235
    }
236

    
237
    @Override
238
    public void updateTableStatistics(String database, String schema, String table) throws JDBCExecuteSQLException {
239
        String sql = null;
240
        if( StringUtils.isEmpty(database) ) {
241
            if( StringUtils.isEmpty(schema) ) {
242
                sql = MessageFormat.format("VACUUM ANALIZE \"{0}\".\"{1}\".\"{2}\"", 
243
                        database, schema, table
244
                );
245
            }
246
        } else if( StringUtils.isEmpty(schema) ) {
247
            sql = MessageFormat.format("VACUUM ANALIZE \"{0}\".\"{1}\"", 
248
                    schema, table
249
            );
250
        } else {
251
            sql = MessageFormat.format("VACUUM ANALIZE \"{0}\"", 
252
                    table
253
            );
254
        }
255
        if( sql == null ) {
256
            throw new IllegalArgumentException("Can't update statistics, identifier of table incorrect (database '"+database+"', schema '"+schema+"', table '"+table+"')");
257
        }
258
        this.getHelper().execute(sql);
259
    }
260

    
261
}