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 @ 43377
History | View | Annotate | Download (14.7 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 |
params.getDBName(), |
153 |
params.getSchema(), |
154 |
params.getTable() |
155 |
); |
156 |
removeTable.perform(); |
157 |
} |
158 |
|
159 |
@Override
|
160 |
public JDBCStoreParameters getOpenParameters() throws DataException { |
161 |
JDBCServerExplorerParameters parameters = getParameters(); |
162 |
JDBCStoreParameters params = this.helper.createOpenStoreParameters();
|
163 |
params.setHost(parameters.getHost()); |
164 |
params.setPort(parameters.getPort()); |
165 |
params.setDBName(parameters.getDBName()); |
166 |
params.setUser(parameters.getUser()); |
167 |
params.setPassword(parameters.getPassword()); |
168 |
params.setCatalog(parameters.getCatalog()); |
169 |
params.setSchema(parameters.getSchema()); |
170 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
171 |
params.setUrl(parameters.getUrl()); |
172 |
if( parameters instanceof FilesystemStoreParameters ) { |
173 |
File f = ((FilesystemStoreParameters) parameters).getFile();
|
174 |
((FilesystemStoreParameters) params).setFile(f); |
175 |
} |
176 |
return params;
|
177 |
} |
178 |
|
179 |
@Override
|
180 |
public JDBCNewStoreParameters getAddParameters() throws DataException { |
181 |
JDBCServerExplorerParameters parameters = getParameters(); |
182 |
JDBCNewStoreParameters params = this.helper.createNewStoreParameters();
|
183 |
params.setHost(parameters.getHost()); |
184 |
params.setPort(parameters.getPort()); |
185 |
params.setDBName(parameters.getDBName()); |
186 |
params.setUser(parameters.getUser()); |
187 |
params.setPassword(parameters.getPassword()); |
188 |
params.setCatalog(parameters.getCatalog()); |
189 |
params.setSchema(parameters.getSchema()); |
190 |
params.setJDBCDriverClassName(parameters.getJDBCDriverClassName()); |
191 |
params.setUrl(parameters.getUrl()); |
192 |
if( parameters instanceof FilesystemStoreParameters ) { |
193 |
File f = ((FilesystemStoreParameters) parameters).getFile();
|
194 |
((FilesystemStoreParameters) params).setFile(f); |
195 |
} |
196 |
|
197 |
params.setDefaultFeatureType(this.getServerExplorerProviderServices()
|
198 |
.createNewFeatureType()); |
199 |
|
200 |
return params;
|
201 |
} |
202 |
|
203 |
protected void checkIsMine(DataStoreParameters dsp) { |
204 |
if (!(dsp instanceof JDBCConnectionParameters)) { |
205 |
throw new IllegalArgumentException( |
206 |
"not instance of FilesystemStoreParameters");
|
207 |
} |
208 |
JDBCServerExplorerParameters parameters = getParameters(); |
209 |
|
210 |
JDBCConnectionParameters pgp = (JDBCConnectionParameters) dsp; |
211 |
if( !StringUtils.equals(pgp.getHost(), parameters.getHost()) ) {
|
212 |
throw new IllegalArgumentException("wrong explorer: Host (mine: " |
213 |
+ parameters.getHost() + " other:" + pgp.getHost() + ")"); |
214 |
} |
215 |
if( !ObjectUtils.equals(pgp.getPort(), parameters.getPort()) ) {
|
216 |
throw new IllegalArgumentException("wrong explorer: Port (mine: " |
217 |
+ parameters.getPort() + " other:" + pgp.getPort() + ")"); |
218 |
} |
219 |
if( !StringUtils.equals(pgp.getDBName(), parameters.getDBName()) ) {
|
220 |
throw new IllegalArgumentException("wrong explorer: DBName (mine: " |
221 |
+ parameters.getDBName() + " other:" + pgp.getDBName()
|
222 |
+ ")");
|
223 |
} |
224 |
if( ! StringUtils.isEmpty(parameters.getCatalog()) ) {
|
225 |
if( !StringUtils.equals(pgp.getCatalog(), parameters.getCatalog()) ) {
|
226 |
throw new IllegalArgumentException( |
227 |
"wrong explorer: Catalog (mine: "
|
228 |
+ parameters.getCatalog() + " other:"
|
229 |
+ pgp.getCatalog() + ")");
|
230 |
} |
231 |
} |
232 |
if( ! StringUtils.isEmpty(parameters.getSchema()) ) {
|
233 |
if( !StringUtils.equals(pgp.getSchema(), parameters.getSchema()) ) {
|
234 |
throw new IllegalArgumentException( |
235 |
"wrong explorer: Schema (mine: "
|
236 |
+ parameters.getSchema() + " other:"
|
237 |
+ pgp.getSchema() + ")");
|
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public void open() throws OpenException { |
244 |
|
245 |
} |
246 |
|
247 |
@Override
|
248 |
public void close() throws CloseException { |
249 |
|
250 |
} |
251 |
|
252 |
@Override
|
253 |
protected void doDispose() throws BaseException { |
254 |
helper.dispose(); |
255 |
helper = null;
|
256 |
} |
257 |
|
258 |
@Override
|
259 |
public boolean canAdd() { |
260 |
if (this.canAdd == null) { |
261 |
CanCreateTablesOperation canAdd = this.getOperations().createCanCreateTables();
|
262 |
this.canAdd = (Boolean) canAdd.perform(); |
263 |
} |
264 |
return this.canAdd; |
265 |
} |
266 |
|
267 |
@Override
|
268 |
public FeatureType getFeatureType(DataStoreParameters theParams)
|
269 |
throws DataException {
|
270 |
|
271 |
JDBCStoreParameters params = (JDBCStoreParameters)theParams; |
272 |
|
273 |
checkIsMine(params); |
274 |
|
275 |
EditableFeatureType fetureType = |
276 |
this.getServerExplorerProviderServices().createNewFeatureType();
|
277 |
|
278 |
List<String> primaryKeys = null; |
279 |
if( params.getPkFields() != null ) { |
280 |
primaryKeys = Arrays.asList(params.getPkFields());
|
281 |
} |
282 |
|
283 |
FetchFeatureTypeOperation fetch = this.getOperations().createFetchFeatureType(
|
284 |
fetureType, |
285 |
params.getDBName(), |
286 |
params.getSchema(), |
287 |
params.getTable(), |
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 |
params.getDBName(), |
348 |
params.getSchema(), |
349 |
params.getTable(), |
350 |
params.getDefaultFeatureType(), |
351 |
userAndPrivileges, |
352 |
additionalSQLs |
353 |
); |
354 |
return (boolean) createTable.perform(); |
355 |
} |
356 |
|
357 |
@Override
|
358 |
public List getDataStoreProviderNames() { |
359 |
List x = new ArrayList(1); |
360 |
x.add(this.getProviderName());
|
361 |
return x;
|
362 |
} |
363 |
|
364 |
@Override
|
365 |
public void updateTableStatistics(String database, String schema, String table) throws JDBCExecuteSQLException { |
366 |
JDBCServerExplorerParameters params = this.getParameters();
|
367 |
UpdateTableStatisticsOperation updateStatistics = this.getOperations().createUpdateTableStatistics(
|
368 |
database, |
369 |
schema, |
370 |
table |
371 |
); |
372 |
updateStatistics.perform(); |
373 |
} |
374 |
|
375 |
@Override
|
376 |
public void execute(String sql) { |
377 |
ExecuteOperation execute = this.getOperations().createExecute(sql);
|
378 |
execute.perform(); |
379 |
} |
380 |
|
381 |
@Override
|
382 |
public DataStoreParameters get(String name) throws DataException { |
383 |
JDBCStoreParameters params = this.getOpenParameters();
|
384 |
params.setTable(name); |
385 |
return params;
|
386 |
} |
387 |
|
388 |
@Override
|
389 |
public SQLBuilder createSQLBuilder() {
|
390 |
return this.getHelper().createSQLBuilder(); |
391 |
} |
392 |
|
393 |
} |