svn-gvsig-desktop / tags / v2_0_0_Build_2021 / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / postgresql / PostgreSQLServerExplorer.java @ 34088
History | View | Annotate | Download (6.11 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 | |||
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 | 28909 | jmvivo | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
42 | 27906 | jmvivo | import org.gvsig.fmap.dal.exception.RemoveException; |
43 | 29445 | jmvivo | import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
44 | 28909 | jmvivo | import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
45 | 27906 | jmvivo | import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
46 | 28909 | jmvivo | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
47 | 28784 | jmvivo | import org.gvsig.fmap.dal.store.jdbc.TransactionalAction; |
48 | 28785 | jmvivo | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
49 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
50 | 27906 | jmvivo | import org.slf4j.Logger; |
51 | import org.slf4j.LoggerFactory; |
||
52 | |||
53 | /**
|
||
54 | * @author jmvivo
|
||
55 | *
|
||
56 | */
|
||
57 | 28784 | jmvivo | public class PostgreSQLServerExplorer extends JDBCServerExplorer { |
58 | 27906 | jmvivo | final static private Logger logger = LoggerFactory |
59 | .getLogger(PostgreSQLServerExplorer.class); |
||
60 | |||
61 | 27953 | jmvivo | public static final String NAME = "PostgreSQLExplorer"; |
62 | 27906 | jmvivo | |
63 | |||
64 | public PostgreSQLServerExplorer(
|
||
65 | 29445 | jmvivo | PostgreSQLServerExplorerParameters parameters, |
66 | DataServerExplorerProviderServices services) |
||
67 | 27906 | jmvivo | throws InitializeException {
|
68 | 29445 | jmvivo | super(parameters, services);
|
69 | 28909 | jmvivo | } |
70 | 27906 | jmvivo | |
71 | 29445 | jmvivo | private PostgreSQLServerExplorerParameters getPostgreSQLParameters() {
|
72 | return (PostgreSQLServerExplorerParameters) getParameters();
|
||
73 | 27906 | jmvivo | } |
74 | |||
75 | 29445 | jmvivo | |
76 | 28909 | jmvivo | protected JDBCHelper createHelper() throws InitializeException { |
77 | 29445 | jmvivo | return new PostgreSQLHelper(this, getPostgreSQLParameters()); |
78 | 28909 | jmvivo | } |
79 | 27906 | jmvivo | |
80 | 28909 | jmvivo | |
81 | 27906 | jmvivo | protected String getStoreName() { |
82 | return PostgreSQLStoreProvider.NAME;
|
||
83 | } |
||
84 | |||
85 | 33717 | jjdelcerro | public String getProviderName() { |
86 | 27906 | jmvivo | return NAME;
|
87 | } |
||
88 | |||
89 | 28909 | jmvivo | protected JDBCStoreParameters createStoreParams()
|
90 | throws InitializeException, ProviderNotRegisteredException {
|
||
91 | PostgreSQLStoreParameters orgParams = (PostgreSQLStoreParameters) super
|
||
92 | .createStoreParams(); |
||
93 | 27906 | jmvivo | |
94 | 29445 | jmvivo | orgParams.setUseSSL(getPostgreSQLParameters().getUseSSL()); |
95 | 27908 | jmvivo | |
96 | 28909 | jmvivo | return orgParams;
|
97 | 27906 | jmvivo | } |
98 | |||
99 | 27908 | jmvivo | |
100 | // ****************************
|
||
101 | |||
102 | |||
103 | public boolean canAdd() { |
||
104 | 28909 | jmvivo | return true; |
105 | 27906 | jmvivo | } |
106 | |||
107 | 28909 | jmvivo | protected void checkIsMine(DataStoreParameters dsp) { |
108 | 28257 | jmvivo | if (!(dsp instanceof PostgreSQLStoreParameters)) { |
109 | // FIXME Excpetion ???
|
||
110 | throw new IllegalArgumentException( |
||
111 | 28909 | jmvivo | "not instance of PostgreSQLStoreParameters");
|
112 | 28257 | jmvivo | } |
113 | 28909 | jmvivo | super.checkIsMine(dsp);
|
114 | |||
115 | 28257 | jmvivo | PostgreSQLStoreParameters pgp = (PostgreSQLStoreParameters) dsp; |
116 | 29445 | jmvivo | if (pgp.getUseSSL().booleanValue() != getPostgreSQLParameters()
|
117 | .getUseSSL()) { |
||
118 | 28257 | jmvivo | throw new IllegalArgumentException("worng explorer: Host"); |
119 | } |
||
120 | } |
||
121 | |||
122 | 27906 | jmvivo | public void remove(DataStoreParameters dsp) throws RemoveException { |
123 | 28257 | jmvivo | final PostgreSQLStoreParameters pgParams =(PostgreSQLStoreParameters) dsp;
|
124 | 27906 | jmvivo | |
125 | 28257 | jmvivo | 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 | 33717 | jjdelcerro | throw new RemoveException(this.getProviderName(), e); |
180 | 28257 | jmvivo | } |
181 | 27906 | jmvivo | } |
182 | |||
183 | public NewDataStoreParameters getAddParameters() throws DataException { |
||
184 | 29445 | jmvivo | PostgreSQLServerExplorerParameters parameters = getPostgreSQLParameters(); |
185 | 28257 | jmvivo | PostgreSQLNewStoreParameters params = new PostgreSQLNewStoreParameters();
|
186 | 29445 | jmvivo | 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 | 28257 | jmvivo | |
197 | |||
198 | params.setDefaultFeatureType(this.getServerExplorerProviderServices()
|
||
199 | .createNewFeatureType()); |
||
200 | |||
201 | |||
202 | return params;
|
||
203 | 27906 | jmvivo | } |
204 | |||
205 | |||
206 | |||
207 | // ***********************
|
||
208 | // ***********************
|
||
209 | |||
210 | |||
211 | 28784 | jmvivo | public boolean hasGeometrySupport() { |
212 | return true; |
||
213 | } |
||
214 | 27982 | jmvivo | |
215 | 28909 | jmvivo | protected PostgreSQLHelper getPgHelper() {
|
216 | return (PostgreSQLHelper) getHelper();
|
||
217 | } |
||
218 | 27982 | jmvivo | |
219 | 27906 | jmvivo | } |