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 @ 44297
History | View | Annotate | Download (15.8 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 static org.gvsig.fmap.dal.DataManager.RESOURCES_TABLE_NAME; |
37 |
import org.gvsig.fmap.dal.DataStore; |
38 |
import org.gvsig.fmap.dal.DataStoreParameters; |
39 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
40 |
import org.gvsig.fmap.dal.exception.CloseException; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.exception.InitializeException; |
43 |
import org.gvsig.fmap.dal.exception.OpenException; |
44 |
import org.gvsig.fmap.dal.exception.RemoveException; |
45 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
47 |
import org.gvsig.fmap.dal.SQLBuilder; |
48 |
import org.gvsig.fmap.dal.SQLBuilder.Privilege; |
49 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
50 |
import org.gvsig.fmap.dal.serverexplorer.db.spi.AbstractDBServerExplorer; |
51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
52 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
53 |
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices; |
54 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
55 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
56 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
57 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
58 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
59 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
60 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
61 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CanCreateTablesOperation; |
62 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CreateTableOperation; |
63 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation; |
64 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ListTablesOperation; |
65 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.DropTableOperation; |
66 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ExecuteOperation; |
67 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.UpdateTableStatisticsOperation; |
68 |
import org.gvsig.tools.exception.BaseException; |
69 |
import org.gvsig.tools.resourcesstorage.EmptyResourcesStorage; |
70 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
71 |
import org.slf4j.Logger; |
72 |
import org.slf4j.LoggerFactory; |
73 |
|
74 |
|
75 |
@SuppressWarnings("UseSpecificCatch") |
76 |
public class JDBCServerExplorerBase extends AbstractDBServerExplorer implements JDBCServerExplorer { |
77 |
|
78 |
private static final Logger LOG = LoggerFactory.getLogger(JDBCServerExplorerBase.class); |
79 |
|
80 |
protected JDBCHelper helper = null; |
81 |
|
82 |
private Boolean canAdd; |
83 |
|
84 |
public JDBCServerExplorerBase(
|
85 |
JDBCServerExplorerParameters parameters, |
86 |
DataServerExplorerProviderServices services, |
87 |
JDBCHelper helper |
88 |
) throws InitializeException {
|
89 |
super(parameters, services);
|
90 |
this.helper = helper;
|
91 |
} |
92 |
|
93 |
@Override
|
94 |
public String getProviderName() { |
95 |
return this.getHelper().getProviderName(); |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public String getStoreName() { |
100 |
return this.getHelper().getProviderName(); |
101 |
} |
102 |
|
103 |
protected DataManagerProviderServices getManager() {
|
104 |
return (DataManagerProviderServices) DALLocator.getDataManager();
|
105 |
} |
106 |
|
107 |
@Override
|
108 |
public JDBCServerExplorerParameters getParameters() {
|
109 |
return (JDBCServerExplorerParameters) super.getParameters(); |
110 |
} |
111 |
|
112 |
@Override
|
113 |
public boolean closeResourceRequested(ResourceProvider resource) { |
114 |
this.getHelper().getResulSetControler().pack();
|
115 |
return true; |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public void resourceChanged(ResourceProvider resource) { |
120 |
// Nothing to do
|
121 |
} |
122 |
|
123 |
protected JDBCHelper getHelper() {
|
124 |
return helper;
|
125 |
} |
126 |
|
127 |
protected OperationsFactory getOperations() {
|
128 |
return this.getHelper().getOperations(); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public DataStore open(DataStoreParameters params) throws DataException { |
133 |
checkIsMine(params); |
134 |
DataStore store = super.open(params);
|
135 |
return store;
|
136 |
} |
137 |
|
138 |
@Override
|
139 |
public List list(int mode) throws DataException { |
140 |
boolean informationTables = BooleanUtils.isTrue(
|
141 |
this.getParameters().getShowInformationDBTables()
|
142 |
); |
143 |
JDBCStoreParameters baseParams = this.getOpenParameters();
|
144 |
ListTablesOperation listTables = this.getOperations().createListTables(
|
145 |
mode, baseParams, informationTables |
146 |
); |
147 |
List<JDBCStoreParameters> tables = (List<JDBCStoreParameters>) listTables.perform(); |
148 |
return tables;
|
149 |
} |
150 |
|
151 |
@Override
|
152 |
public void remove(DataStoreParameters theParams) throws RemoveException { |
153 |
|
154 |
JDBCStoreParameters params = (JDBCStoreParameters) theParams; |
155 |
DropTableOperation removeTable = this.getOperations().createDropTable(
|
156 |
this.getOperations().createTableReference(params)
|
157 |
); |
158 |
removeTable.perform(); |
159 |
} |
160 |
|
161 |
@Override
|
162 |
public JDBCStoreParameters getOpenParameters() throws DataException { |
163 |
JDBCServerExplorerParameters parameters = getParameters(); |
164 |
JDBCStoreParameters params = this.helper.createOpenStoreParameters();
|
165 |
params.setHost(parameters.getHost()); |
166 |
params.setPort(parameters.getPort()); |
167 |
params.setDBName(parameters.getDBName()); |
168 |
params.setUser(parameters.getUser()); |
169 |
params.setPassword(parameters.getPassword()); |
170 |
params.setCatalog(parameters.getCatalog()); |
171 |
params.setSchema(parameters.getSchema()); |
172 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
173 |
params.setUrl(parameters.getUrl()); |
174 |
if( parameters instanceof FilesystemStoreParameters ) { |
175 |
File f = ((FilesystemStoreParameters) parameters).getFile();
|
176 |
((FilesystemStoreParameters) params).setFile(f); |
177 |
} |
178 |
return params;
|
179 |
} |
180 |
|
181 |
@Override
|
182 |
public JDBCNewStoreParameters getAddParameters() throws DataException { |
183 |
JDBCServerExplorerParameters parameters = getParameters(); |
184 |
JDBCNewStoreParameters params = this.helper.createNewStoreParameters();
|
185 |
params.setHost(parameters.getHost()); |
186 |
params.setPort(parameters.getPort()); |
187 |
params.setDBName(parameters.getDBName()); |
188 |
params.setUser(parameters.getUser()); |
189 |
params.setPassword(parameters.getPassword()); |
190 |
params.setCatalog(parameters.getCatalog()); |
191 |
params.setSchema(parameters.getSchema()); |
192 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
193 |
params.setUrl(parameters.getUrl()); |
194 |
if( parameters instanceof FilesystemStoreParameters ) { |
195 |
File f = ((FilesystemStoreParameters) parameters).getFile();
|
196 |
((FilesystemStoreParameters) params).setFile(f); |
197 |
} |
198 |
|
199 |
params.setDefaultFeatureType(this.getServerExplorerProviderServices()
|
200 |
.createNewFeatureType()); |
201 |
|
202 |
return params;
|
203 |
} |
204 |
|
205 |
protected void checkIsMine(DataStoreParameters dsp) { |
206 |
if (!(dsp instanceof JDBCConnectionParameters)) { |
207 |
throw new IllegalArgumentException( |
208 |
"not instance of FilesystemStoreParameters");
|
209 |
} |
210 |
JDBCServerExplorerParameters parameters = getParameters(); |
211 |
|
212 |
JDBCConnectionParameters pgp = (JDBCConnectionParameters) dsp; |
213 |
if( !StringUtils.equals(pgp.getHost(), parameters.getHost()) ) {
|
214 |
throw new IllegalArgumentException("wrong explorer: Host (mine: " |
215 |
+ parameters.getHost() + " other:" + pgp.getHost() + ")"); |
216 |
} |
217 |
if( !ObjectUtils.equals(pgp.getPort(), parameters.getPort()) ) {
|
218 |
throw new IllegalArgumentException("wrong explorer: Port (mine: " |
219 |
+ parameters.getPort() + " other:" + pgp.getPort() + ")"); |
220 |
} |
221 |
if( !StringUtils.equals(pgp.getDBName(), parameters.getDBName()) ) {
|
222 |
throw new IllegalArgumentException("wrong explorer: DBName (mine: " |
223 |
+ parameters.getDBName() + " other:" + pgp.getDBName()
|
224 |
+ ")");
|
225 |
} |
226 |
if( ! StringUtils.isEmpty(parameters.getCatalog()) ) {
|
227 |
if( !StringUtils.equals(pgp.getCatalog(), parameters.getCatalog()) ) {
|
228 |
throw new IllegalArgumentException( |
229 |
"wrong explorer: Catalog (mine: "
|
230 |
+ parameters.getCatalog() + " other:"
|
231 |
+ pgp.getCatalog() + ")");
|
232 |
} |
233 |
} |
234 |
if( ! StringUtils.isEmpty(parameters.getSchema()) ) {
|
235 |
if( !StringUtils.equals(pgp.getSchema(), parameters.getSchema()) ) {
|
236 |
throw new IllegalArgumentException( |
237 |
"wrong explorer: Schema (mine: "
|
238 |
+ parameters.getSchema() + " other:"
|
239 |
+ pgp.getSchema() + ")");
|
240 |
} |
241 |
} |
242 |
} |
243 |
|
244 |
@Override
|
245 |
public void open() throws OpenException { |
246 |
|
247 |
} |
248 |
|
249 |
@Override
|
250 |
public void close() throws CloseException { |
251 |
|
252 |
} |
253 |
|
254 |
@Override
|
255 |
protected void doDispose() throws BaseException { |
256 |
helper.dispose(); |
257 |
helper = null;
|
258 |
} |
259 |
|
260 |
@Override
|
261 |
public boolean canAdd() { |
262 |
if (this.canAdd == null) { |
263 |
CanCreateTablesOperation canAdd_ = this.getOperations().createCanCreateTables();
|
264 |
this.canAdd = (Boolean) canAdd_.perform(); |
265 |
} |
266 |
return this.canAdd; |
267 |
} |
268 |
|
269 |
@Override
|
270 |
public FeatureType getFeatureType(DataStoreParameters theParams)
|
271 |
throws DataException {
|
272 |
|
273 |
JDBCStoreParameters params = (JDBCStoreParameters)theParams; |
274 |
|
275 |
checkIsMine(params); |
276 |
|
277 |
EditableFeatureType fetureType = |
278 |
this.getServerExplorerProviderServices().createNewFeatureType();
|
279 |
|
280 |
List<String> primaryKeys = null; |
281 |
if( params.getPkFields() != null ) { |
282 |
primaryKeys = Arrays.asList(params.getPkFields());
|
283 |
} |
284 |
|
285 |
FetchFeatureTypeOperation fetch = this.getOperations().createFetchFeatureType(
|
286 |
fetureType, |
287 |
this.getOperations().createTableReference(params),
|
288 |
primaryKeys, |
289 |
params.getDefaultGeometryField(), |
290 |
params.getCRS() |
291 |
); |
292 |
fetch.perform(); |
293 |
return fetureType;
|
294 |
} |
295 |
|
296 |
@Override
|
297 |
public boolean add(String providerName, NewDataStoreParameters theParams, boolean overwrite) |
298 |
throws DataException {
|
299 |
|
300 |
List<Pair<String, Privilege>> userAndPrivileges = new ArrayList<>(); |
301 |
JDBCNewStoreParameters params = (JDBCNewStoreParameters)theParams; |
302 |
if( !StringUtils.isEmpty(params.getAllRole()) ) {
|
303 |
userAndPrivileges.add( |
304 |
new ImmutablePair<>(params.getAllRole(), Privilege.ALL )
|
305 |
); |
306 |
} |
307 |
if( !StringUtils.isEmpty(params.getDeleteRole()) ) {
|
308 |
userAndPrivileges.add( |
309 |
new ImmutablePair<>(params.getDeleteRole(), Privilege.DELETE )
|
310 |
); |
311 |
} |
312 |
if( !StringUtils.isEmpty(params.getInsertRole()) ) {
|
313 |
userAndPrivileges.add( |
314 |
new ImmutablePair<>(params.getInsertRole(), Privilege.INSERT )
|
315 |
); |
316 |
} |
317 |
if( !StringUtils.isEmpty(params.getReferenceRole()) ) {
|
318 |
userAndPrivileges.add( |
319 |
new ImmutablePair<>(params.getReferenceRole(), Privilege.REFERENCE )
|
320 |
); |
321 |
} |
322 |
if( !StringUtils.isEmpty(params.getSelectRole()) ) {
|
323 |
userAndPrivileges.add( |
324 |
new ImmutablePair<>(params.getSelectRole(), Privilege.SELECT )
|
325 |
); |
326 |
} |
327 |
if( !StringUtils.isEmpty(params.getTriggerRole()) ) {
|
328 |
userAndPrivileges.add( |
329 |
new ImmutablePair<>(params.getTriggerRole(), Privilege.TRIGGER )
|
330 |
); |
331 |
} |
332 |
if( !StringUtils.isEmpty(params.getTruncateRole()) ) {
|
333 |
userAndPrivileges.add( |
334 |
new ImmutablePair<>(params.getTruncateRole(), Privilege.TRUNCATE )
|
335 |
); |
336 |
} |
337 |
if( !StringUtils.isEmpty(params.getUpdateRole()) ) {
|
338 |
userAndPrivileges.add( |
339 |
new ImmutablePair<>(params.getUpdateRole(), Privilege.UPDATE )
|
340 |
); |
341 |
} |
342 |
List<String> additionalSQLs = new ArrayList<>(); |
343 |
if( !StringUtils.isEmpty(params.getPostCreatingStatement()) ) {
|
344 |
additionalSQLs.add(params.getPostCreatingStatement()); |
345 |
} |
346 |
CreateTableOperation createTable = this.getOperations().createTable(
|
347 |
this.getOperations().createTableReference(params),
|
348 |
params.getDefaultFeatureType(), |
349 |
userAndPrivileges, |
350 |
additionalSQLs |
351 |
); |
352 |
return (boolean) createTable.perform(); |
353 |
} |
354 |
|
355 |
@Override
|
356 |
public List getDataStoreProviderNames() { |
357 |
List x = new ArrayList(1); |
358 |
x.add(this.getProviderName());
|
359 |
return x;
|
360 |
} |
361 |
|
362 |
@Override
|
363 |
public void updateTableStatistics(String database, String schema, String table) throws JDBCExecuteSQLException { |
364 |
UpdateTableStatisticsOperation updateStatistics = this.getOperations().createUpdateTableStatistics(
|
365 |
this.getOperations().createTableReference(database, schema, table, null) |
366 |
); |
367 |
updateStatistics.perform(); |
368 |
} |
369 |
|
370 |
@Override
|
371 |
public void execute(String sql) { |
372 |
ExecuteOperation execute = this.getOperations().createExecute(sql);
|
373 |
execute.perform(); |
374 |
} |
375 |
|
376 |
@Override
|
377 |
public JDBCStoreParameters get(String name) throws DataException { |
378 |
JDBCStoreParameters params = this.getOpenParameters();
|
379 |
params.setTable(name); |
380 |
return params;
|
381 |
} |
382 |
|
383 |
@Override
|
384 |
public SQLBuilder createSQLBuilder() {
|
385 |
return this.getHelper().createSQLBuilder(); |
386 |
} |
387 |
|
388 |
@Override
|
389 |
public ResourcesStorage getResourcesStorage(DataStore dataStore) {
|
390 |
if( this.resourcesStorage==null ) { |
391 |
try {
|
392 |
List<JDBCStoreParameters> tables = this.list(); |
393 |
for (JDBCStoreParameters params : tables) {
|
394 |
String theTableName = params.getTable();
|
395 |
if (StringUtils.equals(theTableName, RESOURCES_TABLE_NAME)) {
|
396 |
this.resourcesStorage = new JDBCResourcesStorage( |
397 |
params, |
398 |
dataStore.getName() |
399 |
); |
400 |
return this.resourcesStorage; |
401 |
} |
402 |
} |
403 |
this.resourcesStorage = new EmptyResourcesStorage(); |
404 |
} catch (Throwable ex) { |
405 |
LOG.warn("Can't retrieve reources storage from table '" + RESOURCES_TABLE_NAME + "' in '"+this.getParameters().getUrl()+"'.", ex); |
406 |
} |
407 |
} |
408 |
return this.resourcesStorage; |
409 |
} |
410 |
} |