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