svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / JDBCServerExplorerBase.java @ 44058
History | View | Annotate | Download (14.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.dal.store.jdbc2.spi; |
24 |
|
25 |
import java.io.File; |
26 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Arrays; |
29 |
import java.util.List; |
30 |
import org.apache.commons.lang3.BooleanUtils; |
31 |
import org.apache.commons.lang3.ObjectUtils; |
32 |
import org.apache.commons.lang3.StringUtils; |
33 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
34 |
import org.apache.commons.lang3.tuple.Pair; |
35 |
import org.gvsig.fmap.dal.DALLocator; |
36 |
import org.gvsig.fmap.dal.DataStore; |
37 |
import org.gvsig.fmap.dal.DataStoreParameters; |
38 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
39 |
import org.gvsig.fmap.dal.exception.CloseException; |
40 |
import org.gvsig.fmap.dal.exception.DataException; |
41 |
import org.gvsig.fmap.dal.exception.InitializeException; |
42 |
import org.gvsig.fmap.dal.exception.OpenException; |
43 |
import org.gvsig.fmap.dal.exception.RemoveException; |
44 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType; |
46 |
import org.gvsig.fmap.dal.SQLBuilder; |
47 |
import org.gvsig.fmap.dal.SQLBuilder.Privilege; |
48 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
49 |
import org.gvsig.fmap.dal.serverexplorer.db.spi.AbstractDBServerExplorer; |
50 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
51 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
52 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
53 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
54 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
55 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
56 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
57 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
58 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
59 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
60 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CanCreateTablesOperation; |
61 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CreateTableOperation; |
62 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation; |
63 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ListTablesOperation; |
64 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.DropTableOperation; |
65 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ExecuteOperation; |
66 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.UpdateTableStatisticsOperation; |
67 |
import org.gvsig.tools.exception.BaseException; |
68 |
import org.slf4j.Logger; |
69 |
import org.slf4j.LoggerFactory; |
70 |
|
71 |
|
72 |
public class JDBCServerExplorerBase extends AbstractDBServerExplorer implements JDBCServerExplorer { |
73 |
|
74 |
private static final Logger LOG = LoggerFactory.getLogger(JDBCServerExplorerBase.class); |
75 |
|
76 |
protected JDBCHelper helper = null; |
77 |
|
78 |
private Boolean canAdd; |
79 |
|
80 |
public JDBCServerExplorerBase(
|
81 |
JDBCServerExplorerParameters parameters, |
82 |
DataServerExplorerProviderServices services, |
83 |
JDBCHelper helper |
84 |
) throws InitializeException {
|
85 |
super(parameters, services);
|
86 |
this.helper = helper;
|
87 |
} |
88 |
|
89 |
@Override
|
90 |
public String getProviderName() { |
91 |
return this.getHelper().getProviderName(); |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public String getStoreName() { |
96 |
return this.getHelper().getProviderName(); |
97 |
} |
98 |
|
99 |
protected DataManagerProviderServices getManager() {
|
100 |
return (DataManagerProviderServices) DALLocator.getDataManager();
|
101 |
} |
102 |
|
103 |
@Override
|
104 |
public JDBCServerExplorerParameters getParameters() {
|
105 |
return (JDBCServerExplorerParameters) super.getParameters(); |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public boolean closeResourceRequested(ResourceProvider resource) { |
110 |
this.getHelper().getResulSetControler().pack();
|
111 |
return true; |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public void resourceChanged(ResourceProvider resource) { |
116 |
// Nothing to do
|
117 |
} |
118 |
|
119 |
protected JDBCHelper getHelper() {
|
120 |
return helper;
|
121 |
} |
122 |
|
123 |
protected OperationsFactory getOperations() {
|
124 |
return this.getHelper().getOperations(); |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public DataStore open(DataStoreParameters params) throws DataException { |
129 |
checkIsMine(params); |
130 |
DataStore store = super.open(params);
|
131 |
return store;
|
132 |
} |
133 |
|
134 |
@Override
|
135 |
public List list(int mode) throws DataException { |
136 |
boolean informationTables = BooleanUtils.isTrue(
|
137 |
this.getParameters().getShowInformationDBTables()
|
138 |
); |
139 |
JDBCStoreParameters baseParams = this.getOpenParameters();
|
140 |
ListTablesOperation listTables = this.getOperations().createListTables(
|
141 |
mode, baseParams, informationTables |
142 |
); |
143 |
List<JDBCStoreParameters> tables = (List<JDBCStoreParameters>) listTables.perform(); |
144 |
return tables;
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public void remove(DataStoreParameters theParams) throws RemoveException { |
149 |
|
150 |
JDBCStoreParameters params = (JDBCStoreParameters) theParams; |
151 |
DropTableOperation removeTable = this.getOperations().createDropTable(
|
152 |
this.getOperations().createTableReference(params)
|
153 |
); |
154 |
removeTable.perform(); |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public JDBCStoreParameters getOpenParameters() throws DataException { |
159 |
JDBCServerExplorerParameters parameters = getParameters(); |
160 |
JDBCStoreParameters params = this.helper.createOpenStoreParameters();
|
161 |
params.setHost(parameters.getHost()); |
162 |
params.setPort(parameters.getPort()); |
163 |
params.setDBName(parameters.getDBName()); |
164 |
params.setUser(parameters.getUser()); |
165 |
params.setPassword(parameters.getPassword()); |
166 |
params.setCatalog(parameters.getCatalog()); |
167 |
params.setSchema(parameters.getSchema()); |
168 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
169 |
params.setUrl(parameters.getUrl()); |
170 |
if( parameters instanceof FilesystemStoreParameters ) { |
171 |
File f = ((FilesystemStoreParameters) parameters).getFile();
|
172 |
((FilesystemStoreParameters) params).setFile(f); |
173 |
} |
174 |
return params;
|
175 |
} |
176 |
|
177 |
@Override
|
178 |
public JDBCNewStoreParameters getAddParameters() throws DataException { |
179 |
JDBCServerExplorerParameters parameters = getParameters(); |
180 |
JDBCNewStoreParameters params = this.helper.createNewStoreParameters();
|
181 |
params.setHost(parameters.getHost()); |
182 |
params.setPort(parameters.getPort()); |
183 |
params.setDBName(parameters.getDBName()); |
184 |
params.setUser(parameters.getUser()); |
185 |
params.setPassword(parameters.getPassword()); |
186 |
params.setCatalog(parameters.getCatalog()); |
187 |
params.setSchema(parameters.getSchema()); |
188 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
189 |
params.setUrl(parameters.getUrl()); |
190 |
if( parameters instanceof FilesystemStoreParameters ) { |
191 |
File f = ((FilesystemStoreParameters) parameters).getFile();
|
192 |
((FilesystemStoreParameters) params).setFile(f); |
193 |
} |
194 |
|
195 |
params.setDefaultFeatureType(this.getServerExplorerProviderServices()
|
196 |
.createNewFeatureType()); |
197 |
|
198 |
return params;
|
199 |
} |
200 |
|
201 |
protected void checkIsMine(DataStoreParameters dsp) { |
202 |
if (!(dsp instanceof JDBCConnectionParameters)) { |
203 |
throw new IllegalArgumentException( |
204 |
"not instance of FilesystemStoreParameters");
|
205 |
} |
206 |
JDBCServerExplorerParameters parameters = getParameters(); |
207 |
|
208 |
JDBCConnectionParameters pgp = (JDBCConnectionParameters) dsp; |
209 |
if( !StringUtils.equals(pgp.getHost(), parameters.getHost()) ) {
|
210 |
throw new IllegalArgumentException("wrong explorer: Host (mine: " |
211 |
+ parameters.getHost() + " other:" + pgp.getHost() + ")"); |
212 |
} |
213 |
if( !ObjectUtils.equals(pgp.getPort(), parameters.getPort()) ) {
|
214 |
throw new IllegalArgumentException("wrong explorer: Port (mine: " |
215 |
+ parameters.getPort() + " other:" + pgp.getPort() + ")"); |
216 |
} |
217 |
if( !StringUtils.equals(pgp.getDBName(), parameters.getDBName()) ) {
|
218 |
throw new IllegalArgumentException("wrong explorer: DBName (mine: " |
219 |
+ parameters.getDBName() + " other:" + pgp.getDBName()
|
220 |
+ ")");
|
221 |
} |
222 |
if( ! StringUtils.isEmpty(parameters.getCatalog()) ) {
|
223 |
if( !StringUtils.equals(pgp.getCatalog(), parameters.getCatalog()) ) {
|
224 |
throw new IllegalArgumentException( |
225 |
"wrong explorer: Catalog (mine: "
|
226 |
+ parameters.getCatalog() + " other:"
|
227 |
+ pgp.getCatalog() + ")");
|
228 |
} |
229 |
} |
230 |
if( ! StringUtils.isEmpty(parameters.getSchema()) ) {
|
231 |
if( !StringUtils.equals(pgp.getSchema(), parameters.getSchema()) ) {
|
232 |
throw new IllegalArgumentException( |
233 |
"wrong explorer: Schema (mine: "
|
234 |
+ parameters.getSchema() + " other:"
|
235 |
+ pgp.getSchema() + ")");
|
236 |
} |
237 |
} |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public void open() throws OpenException { |
242 |
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
public void close() throws CloseException { |
247 |
|
248 |
} |
249 |
|
250 |
@Override
|
251 |
protected void doDispose() throws BaseException { |
252 |
helper.dispose(); |
253 |
helper = null;
|
254 |
} |
255 |
|
256 |
@Override
|
257 |
public boolean canAdd() { |
258 |
if (this.canAdd == null) { |
259 |
CanCreateTablesOperation canAdd_ = this.getOperations().createCanCreateTables();
|
260 |
this.canAdd = (Boolean) canAdd_.perform(); |
261 |
} |
262 |
return this.canAdd; |
263 |
} |
264 |
|
265 |
@Override
|
266 |
public FeatureType getFeatureType(DataStoreParameters theParams)
|
267 |
throws DataException {
|
268 |
|
269 |
JDBCStoreParameters params = (JDBCStoreParameters)theParams; |
270 |
|
271 |
checkIsMine(params); |
272 |
|
273 |
EditableFeatureType fetureType = |
274 |
this.getServerExplorerProviderServices().createNewFeatureType();
|
275 |
|
276 |
List<String> primaryKeys = null; |
277 |
if( params.getPkFields() != null ) { |
278 |
primaryKeys = Arrays.asList(params.getPkFields());
|
279 |
} |
280 |
|
281 |
FetchFeatureTypeOperation fetch = this.getOperations().createFetchFeatureType(
|
282 |
fetureType, |
283 |
this.getOperations().createTableReference(params),
|
284 |
primaryKeys, |
285 |
params.getDefaultGeometryField(), |
286 |
params.getCRS() |
287 |
); |
288 |
fetch.perform(); |
289 |
return fetureType;
|
290 |
} |
291 |
|
292 |
@Override
|
293 |
public boolean add(String providerName, NewDataStoreParameters theParams, boolean overwrite) |
294 |
throws DataException {
|
295 |
|
296 |
List<Pair<String, Privilege>> userAndPrivileges = new ArrayList<>(); |
297 |
JDBCNewStoreParameters params = (JDBCNewStoreParameters)theParams; |
298 |
if( !StringUtils.isEmpty(params.getAllRole()) ) {
|
299 |
userAndPrivileges.add( |
300 |
new ImmutablePair<>(params.getAllRole(), Privilege.ALL )
|
301 |
); |
302 |
} |
303 |
if( !StringUtils.isEmpty(params.getDeleteRole()) ) {
|
304 |
userAndPrivileges.add( |
305 |
new ImmutablePair<>(params.getDeleteRole(), Privilege.DELETE )
|
306 |
); |
307 |
} |
308 |
if( !StringUtils.isEmpty(params.getInsertRole()) ) {
|
309 |
userAndPrivileges.add( |
310 |
new ImmutablePair<>(params.getInsertRole(), Privilege.INSERT )
|
311 |
); |
312 |
} |
313 |
if( !StringUtils.isEmpty(params.getReferenceRole()) ) {
|
314 |
userAndPrivileges.add( |
315 |
new ImmutablePair<>(params.getReferenceRole(), Privilege.REFERENCE )
|
316 |
); |
317 |
} |
318 |
if( !StringUtils.isEmpty(params.getSelectRole()) ) {
|
319 |
userAndPrivileges.add( |
320 |
new ImmutablePair<>(params.getSelectRole(), Privilege.SELECT )
|
321 |
); |
322 |
} |
323 |
if( !StringUtils.isEmpty(params.getTriggerRole()) ) {
|
324 |
userAndPrivileges.add( |
325 |
new ImmutablePair<>(params.getTriggerRole(), Privilege.TRIGGER )
|
326 |
); |
327 |
} |
328 |
if( !StringUtils.isEmpty(params.getTruncateRole()) ) {
|
329 |
userAndPrivileges.add( |
330 |
new ImmutablePair<>(params.getTruncateRole(), Privilege.TRUNCATE )
|
331 |
); |
332 |
} |
333 |
if( !StringUtils.isEmpty(params.getUpdateRole()) ) {
|
334 |
userAndPrivileges.add( |
335 |
new ImmutablePair<>(params.getUpdateRole(), Privilege.UPDATE )
|
336 |
); |
337 |
} |
338 |
List<String> additionalSQLs = new ArrayList<>(); |
339 |
if( !StringUtils.isEmpty(params.getPostCreatingStatement()) ) {
|
340 |
additionalSQLs.add(params.getPostCreatingStatement()); |
341 |
} |
342 |
CreateTableOperation createTable = this.getOperations().createTable(
|
343 |
this.getOperations().createTableReference(params),
|
344 |
params.getDefaultFeatureType(), |
345 |
userAndPrivileges, |
346 |
additionalSQLs |
347 |
); |
348 |
return (boolean) createTable.perform(); |
349 |
} |
350 |
|
351 |
@Override
|
352 |
public List getDataStoreProviderNames() { |
353 |
List x = new ArrayList(1); |
354 |
x.add(this.getProviderName());
|
355 |
return x;
|
356 |
} |
357 |
|
358 |
@Override
|
359 |
public void updateTableStatistics(String database, String schema, String table) throws JDBCExecuteSQLException { |
360 |
UpdateTableStatisticsOperation updateStatistics = this.getOperations().createUpdateTableStatistics(
|
361 |
this.getOperations().createTableReference(database, schema, table, null) |
362 |
); |
363 |
updateStatistics.perform(); |
364 |
} |
365 |
|
366 |
@Override
|
367 |
public void execute(String sql) { |
368 |
ExecuteOperation execute = this.getOperations().createExecute(sql);
|
369 |
execute.perform(); |
370 |
} |
371 |
|
372 |
@Override
|
373 |
public DataStoreParameters get(String name) throws DataException { |
374 |
JDBCStoreParameters params = this.getOpenParameters();
|
375 |
params.setTable(name); |
376 |
return params;
|
377 |
} |
378 |
|
379 |
@Override
|
380 |
public SQLBuilder createSQLBuilder() {
|
381 |
return this.getHelper().createSQLBuilder(); |
382 |
} |
383 |
|
384 |
} |