Statistics
| Revision:

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 @ 46830

History | View | Annotate | Download (44.1 KB)

1 43020 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4 45065 jjdelcerro
 * Copyright (C) 2007-2020 gvSIG Association.
5 43020 jjdelcerro
 *
6 45065 jjdelcerro
 * 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 3
9
 * of the License, or (at your option) any later version.
10 43020 jjdelcerro
 *
11 45065 jjdelcerro
 * 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 43020 jjdelcerro
 *
16 45065 jjdelcerro
 * 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 43020 jjdelcerro
 *
21 45065 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 43020 jjdelcerro
 */
24
package org.gvsig.fmap.dal.store.jdbc2.spi;
25
26 45773 jjdelcerro
import org.gvsig.tools.resourcesstorage.CompoundResourcesStorage;
27 43377 jjdelcerro
import java.io.File;
28 43020 jjdelcerro
import java.util.ArrayList;
29
import java.util.Arrays;
30 45152 fdiaz
import java.util.Collections;
31 45578 jjdelcerro
import java.util.HashMap;
32 43020 jjdelcerro
import java.util.List;
33 45152 fdiaz
import java.util.Map;
34 46356 jjdelcerro
import java.util.Objects;
35 45578 jjdelcerro
import javax.json.JsonObject;
36
import javax.json.JsonString;
37
import javax.json.JsonValue;
38 45152 fdiaz
import org.apache.commons.codec.binary.Hex;
39
import org.apache.commons.collections.map.LRUMap;
40 43020 jjdelcerro
import org.apache.commons.lang3.BooleanUtils;
41
import org.apache.commons.lang3.ObjectUtils;
42
import org.apache.commons.lang3.StringUtils;
43
import org.apache.commons.lang3.tuple.ImmutablePair;
44
import org.apache.commons.lang3.tuple.Pair;
45 45578 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
46
import org.gvsig.expressionevaluator.ExpressionUtils;
47 43020 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
48 44328 jjdelcerro
import org.gvsig.fmap.dal.DataManager;
49 43020 jjdelcerro
import org.gvsig.fmap.dal.DataStore;
50
import org.gvsig.fmap.dal.DataStoreParameters;
51 46315 jjdelcerro
import org.gvsig.fmap.dal.DataTransaction;
52 44346 jjdelcerro
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
53 45578 jjdelcerro
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_NAME;
54
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_VALUE;
55
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION_NAME;
56 44304 jjdelcerro
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME;
57 43020 jjdelcerro
import org.gvsig.fmap.dal.NewDataStoreParameters;
58 45614 fdiaz
import org.gvsig.fmap.dal.SQLBuilder;
59
import org.gvsig.fmap.dal.SQLBuilder.Privilege;
60 43020 jjdelcerro
import org.gvsig.fmap.dal.exception.CloseException;
61
import org.gvsig.fmap.dal.exception.DataException;
62
import org.gvsig.fmap.dal.exception.InitializeException;
63
import org.gvsig.fmap.dal.exception.OpenException;
64
import org.gvsig.fmap.dal.exception.RemoveException;
65 45614 fdiaz
import org.gvsig.fmap.dal.feature.EditableFeature;
66 43020 jjdelcerro
import org.gvsig.fmap.dal.feature.EditableFeatureType;
67 45578 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
68
import org.gvsig.fmap.dal.feature.FeatureStore;
69 45614 fdiaz
import org.gvsig.fmap.dal.feature.FeatureType;
70 44328 jjdelcerro
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
71 43020 jjdelcerro
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
72
import org.gvsig.fmap.dal.serverexplorer.db.spi.AbstractDBServerExplorer;
73 43377 jjdelcerro
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
74 43020 jjdelcerro
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
75
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
76 46315 jjdelcerro
import org.gvsig.fmap.dal.spi.DataTransactionServices;
77 43020 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
78
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
79
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
80
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
81
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
82
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
83 45614 fdiaz
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
84 43020 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
85 46338 fdiaz
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCResourcesStorage.TABLENAME_SERVER_EXPLORER_MARK;
86 43020 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CanCreateTablesOperation;
87
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CreateTableOperation;
88 45614 fdiaz
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.DropTableOperation;
89
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ExecuteOperation;
90 43020 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation;
91
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ListTablesOperation;
92 46356 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.RetrieveValueOperation;
93 43020 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.UpdateTableStatisticsOperation;
94 45578 jjdelcerro
import org.gvsig.json.Json;
95
import org.gvsig.json.JsonObjectBuilder;
96 46356 jjdelcerro
import org.gvsig.tools.ToolsLocator;
97 45578 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
98 43020 jjdelcerro
import org.gvsig.tools.exception.BaseException;
99 44297 jjdelcerro
import org.gvsig.tools.resourcesstorage.EmptyResourcesStorage;
100
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
101 45152 fdiaz
import org.gvsig.tools.util.CachedValue;
102 43020 jjdelcerro
import org.slf4j.Logger;
103
import org.slf4j.LoggerFactory;
104
105 44297 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
106 43020 jjdelcerro
public class JDBCServerExplorerBase extends AbstractDBServerExplorer implements JDBCServerExplorer {
107
108
    private static final Logger LOG = LoggerFactory.getLogger(JDBCServerExplorerBase.class);
109 45578 jjdelcerro
    private static final String CONFIG_NAME_CUSTOM_RESOURCES = "CUSTOM_RESOURCES";
110 43020 jjdelcerro
111
    protected JDBCHelper helper = null;
112 45578 jjdelcerro
113 43020 jjdelcerro
    private Boolean canAdd;
114 45578 jjdelcerro
115 45156 jjdelcerro
    private static final Map<String, CachedValue<List<JDBCStoreParameters>>> CACHED_TABLES = Collections.synchronizedMap(new LRUMap(10));
116 45578 jjdelcerro
    private static final Map<String, CachedValue<CustomResourcesConfig>> CACHED_CUSTOM_RESOURCES_CONFIG = Collections.synchronizedMap(new LRUMap(10));
117
118
    private static class CustomResourcesConfig {
119
120 45581 fdiaz
        private static final String CUSTOM_RESOURCES_CACHETIME = "CacheTime";
121
        private static final String CUSTOM_RESOURCES_MAPPING = "Mapping";
122 45697 jjdelcerro
        private static final String CUSTOM_RESOURCES_READONLY = "ResourcesReadonly";
123 45581 fdiaz
124
125 45578 jjdelcerro
        private int cacheTime;
126
        private Map<String, String> mapping;
127 45697 jjdelcerro
        private Map<String,Boolean> resourcesReadonly;
128
129 45578 jjdelcerro
        public CustomResourcesConfig(String jsonConfig) {
130
            this.cacheTime = 30*60*1000; // 30min
131
            this.mapping = new HashMap<>();
132 45697 jjdelcerro
            this.resourcesReadonly = new HashMap<>();
133
134 45578 jjdelcerro
            if (StringUtils.isNotBlank(jsonConfig)) {
135
                try {
136
                    JsonObject config = Json.createObject(jsonConfig);
137 45581 fdiaz
                    this.cacheTime = config.getInt(CUSTOM_RESOURCES_CACHETIME, this.cacheTime);
138 45697 jjdelcerro
                    if (config.containsKey(CUSTOM_RESOURCES_MAPPING)) {
139 45581 fdiaz
                        JsonObject m = config.getJsonObject(CUSTOM_RESOURCES_MAPPING);
140 45578 jjdelcerro
                        for (Map.Entry<String, JsonValue> entry : m.entrySet()) {
141
                            String key = entry.getKey();
142
                            JsonValue value = entry.getValue();
143
                            if (value instanceof JsonString) {
144
                                this.mapping.put(key, ((JsonString) value).getString());
145
                            }
146
                        }
147
                    }
148 45697 jjdelcerro
                    if (config.containsKey(CUSTOM_RESOURCES_READONLY)) {
149
                        JsonObject resreadonly = config.getJsonObject(CUSTOM_RESOURCES_READONLY);
150
                        for (Map.Entry<String, JsonValue> entry : resreadonly.entrySet()) {
151
                            String key = entry.getKey();
152
                            JsonValue value = entry.getValue();
153
                            if (value == JsonValue.TRUE) {
154
                                this.resourcesReadonly.put(key, true);
155
                            } else if (value == JsonValue.FALSE) {
156
                                this.resourcesReadonly.put(key, false);
157
                            }
158
                        }
159
                    }
160 45578 jjdelcerro
                } catch (Exception ex) {
161
                    LOG.debug("Can't parse json from "+CONFIG_NAME_CUSTOM_RESOURCES+" variable", ex);
162
                    // Do nothing.
163
                }
164
            }
165
        }
166
167
        public String toJsonString() {
168
            JsonObjectBuilder builder = Json.createObjectBuilder();
169 45697 jjdelcerro
            builder.add(CUSTOM_RESOURCES_CACHETIME, this.cacheTime);
170 45578 jjdelcerro
            JsonObjectBuilder m = Json.createObjectBuilder();
171 45697 jjdelcerro
            for (Map.Entry<String, String> entry : this.mapping.entrySet()) {
172 45578 jjdelcerro
                String key = entry.getKey();
173
                String value = entry.getValue();
174
                m.add(key, value);
175
            }
176 45581 fdiaz
            builder.add(CUSTOM_RESOURCES_MAPPING, m);
177 45697 jjdelcerro
            JsonObjectBuilder resreadonly = Json.createObjectBuilder();
178
            for (Map.Entry<String, Boolean> entry : this.resourcesReadonly.entrySet()) {
179
                String key = entry.getKey();
180
                Boolean readonly = entry.getValue();
181
                resreadonly.add(key,readonly);
182
            }
183
            builder.add(CUSTOM_RESOURCES_READONLY, resreadonly);
184 45578 jjdelcerro
            return builder.build().toString();
185
        }
186
187
        public int getCacheExpireTimeInMillis() {
188
            return cacheTime;
189
        }
190
191
        public String getResourcesTablename(String tablename) {
192
            String resourceTablename = mapping.get(tablename);
193 45764 jjdelcerro
//            LOG.info("Resource of table "+tablename+" = "+resourceTablename);
194 45578 jjdelcerro
            return resourceTablename;
195
        }
196
197
        public void addResourceMapping(String tablename, String resourcesTablename) {
198
            this.mapping.put(tablename, resourcesTablename);
199
        }
200
201
        private boolean isInternalTable(String storeName) {
202
            if (DatabaseWorkspaceManager.isInternalTable(storeName)) {
203
                return true;
204
            }
205
            for (String resourcesTablename : this.mapping.values()) {
206
                if( StringUtils.equals(storeName, resourcesTablename) ) {
207
                    return true;
208
                }
209
            }
210
            return false;
211
        }
212 45697 jjdelcerro
213
        public void setResourcesReadOnly(String resourcesTableName, boolean readonly) {
214
            this.resourcesReadonly.put(resourcesTableName,readonly);
215
        }
216
217
        public boolean isResourcesReadOnly(String resourcesTableName) {
218
            return this.resourcesReadonly.getOrDefault(resourcesTableName,false);
219
        }
220 45578 jjdelcerro
    }
221 45152 fdiaz
222 45578 jjdelcerro
    private static class CachedCustomResourcesConfig extends CachedValue<CustomResourcesConfig> {
223
224
        private final JDBCStoreParameters openParameters;
225
226
        private CachedCustomResourcesConfig(JDBCStoreParameters openParameters) {
227
            this.openParameters = openParameters;
228
            this.setExpireTime(30*60*1000); // 30min
229
        }
230
231
        @Override
232
        protected void reload() {
233
            String jsonConfig = getConfigValue(this.openParameters, CONFIG_NAME_CUSTOM_RESOURCES);
234 46361 jjdelcerro
//            LOGGER.info("reload CustomResourcesConfig "+jsonConfig);
235 45578 jjdelcerro
            CustomResourcesConfig config = new CustomResourcesConfig(jsonConfig);
236
            this.setExpireTime(config.getCacheExpireTimeInMillis());
237
            this.setValue(config);
238
        }
239
240
    }
241
242 45481 fdiaz
    private static class CachedTablesValue extends CachedValue<List<JDBCStoreParameters>> {
243 45578 jjdelcerro
244 45152 fdiaz
        private final int mode;
245 45165 jjdelcerro
        private final JDBCServerExplorerParameters serverParameters;
246 45152 fdiaz
        private final boolean informationTables;
247 45614 fdiaz
        private JDBCHelper helper;
248 45627 fdiaz
        private final int tablesOrViews;
249 45578 jjdelcerro
250 45627 fdiaz
        public CachedTablesValue(JDBCHelper helper, int mode, JDBCServerExplorerParameters serverParameters, boolean informationTables, int tablesOrViews) {
251 45152 fdiaz
            this.mode = mode;
252 45165 jjdelcerro
            this.serverParameters = serverParameters;
253 45152 fdiaz
            this.informationTables = informationTables;
254 45481 fdiaz
            this.helper = helper;
255 45627 fdiaz
            this.tablesOrViews = tablesOrViews;
256 45152 fdiaz
        }
257 45578 jjdelcerro
258 45627 fdiaz
        public CachedTablesValue(JDBCHelper helper, int mode, JDBCServerExplorerParameters serverParameters, boolean informationTables, long expireTime, int tablesOrViews) {
259 45152 fdiaz
            this.mode = mode;
260
            this.setExpireTime(expireTime);
261 45165 jjdelcerro
            this.serverParameters = serverParameters;
262 45152 fdiaz
            this.informationTables = informationTables;
263 45481 fdiaz
            this.helper = helper;
264 45627 fdiaz
            this.tablesOrViews = tablesOrViews;
265 45152 fdiaz
        }
266 45578 jjdelcerro
267 45152 fdiaz
        @Override
268
        protected void reload() {
269 46348 jjdelcerro
//            LOGGER.info("reload list of tables");
270 45156 jjdelcerro
            List<JDBCStoreParameters> tables = null;
271 45614 fdiaz
            if(helper == null){
272
                this.setValue(tables);
273
                return;
274
            }
275 45481 fdiaz
            OperationsFactory operations = helper.getOperations();
276 45578 jjdelcerro
            if (operations == null) {
277 45481 fdiaz
                this.setValue(null);
278
                LOG.debug("Sets tables to null to force reload tables from new ServerExplorar.");
279
                return;
280
            }
281 45156 jjdelcerro
            try {
282 45481 fdiaz
                ListTablesOperation listTables = operations.createListTables(
283 45627 fdiaz
                        this.mode, serverParameters, informationTables, tablesOrViews
284 45156 jjdelcerro
                );
285
                tables = (List<JDBCStoreParameters>) listTables.perform();
286 45578 jjdelcerro
            } catch (Exception ex) {
287 45481 fdiaz
                LOG.debug("Can't reload cached list of tables.", ex);
288 45156 jjdelcerro
            }
289 45152 fdiaz
            this.setValue(tables);
290
        }
291 45614 fdiaz
292
        public JDBCHelper getHelper(){
293
            return this.helper;
294
        }
295
296
        public void dispose() {
297
            this.helper = null;
298
        }
299 45152 fdiaz
    }
300
301 43020 jjdelcerro
    public JDBCServerExplorerBase(
302
            JDBCServerExplorerParameters parameters,
303
            DataServerExplorerProviderServices services,
304
            JDBCHelper helper
305 45578 jjdelcerro
    ) throws InitializeException {
306 43020 jjdelcerro
        super(parameters, services);
307
        this.helper = helper;
308
    }
309
310
    @Override
311
    public String getProviderName() {
312
        return this.getHelper().getProviderName();
313
    }
314
315
    @Override
316
    public String getStoreName() {
317
        return this.getHelper().getProviderName();
318
    }
319
320
    protected DataManagerProviderServices getManager() {
321
        return (DataManagerProviderServices) DALLocator.getDataManager();
322
    }
323 45578 jjdelcerro
324 43020 jjdelcerro
    @Override
325
    public JDBCServerExplorerParameters getParameters() {
326
        return (JDBCServerExplorerParameters) super.getParameters();
327
    }
328 45578 jjdelcerro
329 43020 jjdelcerro
    @Override
330
    public boolean closeResourceRequested(ResourceProvider resource) {
331
        this.getHelper().getResulSetControler().pack();
332
        return true;
333
    }
334
335
    @Override
336
    public void resourceChanged(ResourceProvider resource) {
337
        // Nothing to do
338
    }
339
340
    protected JDBCHelper getHelper() {
341
        return helper;
342
    }
343 45578 jjdelcerro
344 43020 jjdelcerro
    protected OperationsFactory getOperations() {
345
        return this.getHelper().getOperations();
346
    }
347
348
    @Override
349
    public DataStore open(DataStoreParameters params) throws DataException {
350
        checkIsMine(params);
351
        DataStore store = super.open(params);
352
        return store;
353
    }
354
355
    @Override
356 46208 jjdelcerro
    public DataStore open(String tableName) throws DataException {
357
        JDBCStoreParameters params = this.get(tableName);
358
        DataStore store = super.open(params);
359
        return store;
360
    }
361
362
    @Override
363 43020 jjdelcerro
    public List list(int mode) throws DataException {
364 45627 fdiaz
        return list(mode, SHOW_TABLES_AND_VIEWS);
365
    }
366
367
    @Override
368
    public List list(int mode, int tablesOrViews) throws DataException {
369 43020 jjdelcerro
        boolean informationTables = BooleanUtils.isTrue(
370
                this.getParameters().getShowInformationDBTables()
371
        );
372 45578 jjdelcerro
373 45165 jjdelcerro
        JDBCServerExplorerParameters serverParams = this.getParameters();
374 45152 fdiaz
375 45627 fdiaz
        String key = buildKeyForCachedTables(mode, serverParams, informationTables, tablesOrViews);
376 45156 jjdelcerro
        CachedValue<List<JDBCStoreParameters>> tablesValue = CACHED_TABLES.get(key);
377
        List<JDBCStoreParameters> tables = null;
378 45578 jjdelcerro
        if (tablesValue != null) {
379 45156 jjdelcerro
            tables = tablesValue.get();
380 45152 fdiaz
        }
381 45578 jjdelcerro
        if (tables != null) {
382 45156 jjdelcerro
            return tables;
383
        }
384 45627 fdiaz
        tablesValue = new CachedTablesValue(this.helper, mode, serverParams, informationTables, 60000, tablesOrViews); //60"
385 45156 jjdelcerro
        CACHED_TABLES.put(key, tablesValue);
386
        tables = tablesValue.get();
387
        return tables;
388 43020 jjdelcerro
    }
389
390 45627 fdiaz
    public String buildKeyForCachedTables(int mode, JDBCServerExplorerParameters params, boolean informationTables, int tablesOrViews) {
391 45165 jjdelcerro
        JDBCServerExplorerParameters clonedParams = (JDBCServerExplorerParameters) params.getCopy();
392 46411 jjdelcerro
        clonedParams.setSchema(null);
393
        clonedParams.setCatalog(null);
394 45578 jjdelcerro
395 45152 fdiaz
        StringBuilder builder = new StringBuilder();
396
        builder.append(String.valueOf(mode));
397
        builder.append(Hex.encodeHex(clonedParams.toByteArray()));
398
        builder.append(String.valueOf(informationTables));
399 45627 fdiaz
        builder.append(String.valueOf(tablesOrViews));
400 45152 fdiaz
        String key = builder.toString();
401
        return key;
402
    }
403 45578 jjdelcerro
404 43020 jjdelcerro
    @Override
405
    public void remove(DataStoreParameters theParams) throws RemoveException {
406
        JDBCStoreParameters params = (JDBCStoreParameters) theParams;
407
        DropTableOperation removeTable = this.getOperations().createDropTable(
408 44058 jjdelcerro
                this.getOperations().createTableReference(params)
409 43020 jjdelcerro
        );
410 45578 jjdelcerro
        if ((Boolean) removeTable.perform()) {
411 45165 jjdelcerro
            this.dropCachedTables();
412 45152 fdiaz
        }
413 43020 jjdelcerro
    }
414
415
    @Override
416
    public JDBCStoreParameters getOpenParameters() throws DataException {
417 45165 jjdelcerro
        JDBCStoreParameters params = this.helper.createOpenStoreParameters(this.getParameters());
418 46601 fdiaz
        if(StringUtils.isBlank(params.getSchema())){
419
            params.setSchema(this.createSQLBuilder().default_schema());
420
        }
421 43020 jjdelcerro
        return params;
422
    }
423
424
    @Override
425 44304 jjdelcerro
    public NewDataStoreParameters getAddParameters(String storeName)
426
            throws DataException {
427
        JDBCNewStoreParameters params = this.getAddParameters();
428
        params.setTable(storeName);
429
        return params;
430
    }
431
432
    @Override
433 43020 jjdelcerro
    public JDBCNewStoreParameters getAddParameters() throws DataException {
434
        JDBCServerExplorerParameters parameters = getParameters();
435
        JDBCNewStoreParameters params = this.helper.createNewStoreParameters();
436
        params.setHost(parameters.getHost());
437
        params.setPort(parameters.getPort());
438
        params.setDBName(parameters.getDBName());
439
        params.setUser(parameters.getUser());
440
        params.setPassword(parameters.getPassword());
441
        params.setCatalog(parameters.getCatalog());
442
        params.setSchema(parameters.getSchema());
443
        params.setJDBCDriverClassName(parameters.getJDBCDriverClassName());
444
        params.setUrl(parameters.getUrl());
445 45578 jjdelcerro
        if (parameters instanceof FilesystemStoreParameters) {
446 43377 jjdelcerro
            File f = ((FilesystemStoreParameters) parameters).getFile();
447
            ((FilesystemStoreParameters) params).setFile(f);
448
        }
449 43020 jjdelcerro
450
        params.setDefaultFeatureType(this.getServerExplorerProviderServices()
451
                .createNewFeatureType());
452
453
        return params;
454
    }
455
456
    protected void checkIsMine(DataStoreParameters dsp) {
457
        if (!(dsp instanceof JDBCConnectionParameters)) {
458
            throw new IllegalArgumentException(
459
                    "not instance of FilesystemStoreParameters");
460
        }
461
        JDBCServerExplorerParameters parameters = getParameters();
462
463
        JDBCConnectionParameters pgp = (JDBCConnectionParameters) dsp;
464 45578 jjdelcerro
        if (!StringUtils.equals(pgp.getHost(), parameters.getHost())) {
465 43020 jjdelcerro
            throw new IllegalArgumentException("wrong explorer: Host (mine: "
466
                    + parameters.getHost() + " other:" + pgp.getHost() + ")");
467
        }
468 45578 jjdelcerro
        if (!ObjectUtils.equals(pgp.getPort(), parameters.getPort())) {
469 43020 jjdelcerro
            throw new IllegalArgumentException("wrong explorer: Port (mine: "
470
                    + parameters.getPort() + " other:" + pgp.getPort() + ")");
471
        }
472 45578 jjdelcerro
        if (!StringUtils.equals(pgp.getDBName(), parameters.getDBName())) {
473 43020 jjdelcerro
            throw new IllegalArgumentException("wrong explorer: DBName (mine: "
474
                    + parameters.getDBName() + " other:" + pgp.getDBName()
475
                    + ")");
476
        }
477 45578 jjdelcerro
        if (!StringUtils.isEmpty(parameters.getCatalog())) {
478
            if (!StringUtils.equals(pgp.getCatalog(), parameters.getCatalog())) {
479 43020 jjdelcerro
                throw new IllegalArgumentException(
480
                        "wrong explorer: Catalog (mine: "
481
                        + parameters.getCatalog() + " other:"
482
                        + pgp.getCatalog() + ")");
483
            }
484
        }
485 45578 jjdelcerro
        if (!StringUtils.isEmpty(parameters.getSchema())) {
486
            if (!StringUtils.equals(pgp.getSchema(), parameters.getSchema())) {
487 43020 jjdelcerro
                throw new IllegalArgumentException(
488
                        "wrong explorer: Schema (mine: "
489
                        + parameters.getSchema() + " other:"
490
                        + pgp.getSchema() + ")");
491
            }
492
        }
493
    }
494
495
    @Override
496
    public void open() throws OpenException {
497
498
    }
499
500
    @Override
501
    public void close() throws CloseException {
502
503
    }
504
505
    @Override
506
    protected void doDispose() throws BaseException {
507 45614 fdiaz
        synchronized (CACHED_TABLES) {
508
            List<String> toRemove = new ArrayList<>();
509
            for (Map.Entry<String, CachedValue<List<JDBCStoreParameters>>> entry : CACHED_TABLES.entrySet()) {
510
                CachedTablesValue value = (CachedTablesValue) entry.getValue();
511
                if (value.getHelper() == this.helper) {
512
                    toRemove.add(entry.getKey());
513
                    value.dispose();
514
                }
515
            }
516
            for (String key : toRemove) {
517
                CACHED_TABLES.remove(key);
518
            }
519
            helper.dispose();
520
            helper = null;
521
        }
522 43020 jjdelcerro
    }
523
524
    @Override
525
    public boolean canAdd() {
526
        if (this.canAdd == null) {
527 44058 jjdelcerro
            CanCreateTablesOperation canAdd_ = this.getOperations().createCanCreateTables();
528
            this.canAdd = (Boolean) canAdd_.perform();
529 43020 jjdelcerro
        }
530
        return this.canAdd;
531
    }
532
533
    @Override
534
    public FeatureType getFeatureType(DataStoreParameters theParams)
535
            throws DataException {
536 45578 jjdelcerro
537
        JDBCStoreParameters params = (JDBCStoreParameters) theParams;
538
539 43020 jjdelcerro
        checkIsMine(params);
540
541 45578 jjdelcerro
        EditableFeatureType fetureType
542
                = this.getServerExplorerProviderServices().createNewFeatureType();
543
544 43020 jjdelcerro
        List<String> primaryKeys = null;
545 45578 jjdelcerro
        if (params.getPkFields() != null) {
546 43020 jjdelcerro
            primaryKeys = Arrays.asList(params.getPkFields());
547
        }
548 45578 jjdelcerro
549 43020 jjdelcerro
        FetchFeatureTypeOperation fetch = this.getOperations().createFetchFeatureType(
550 45578 jjdelcerro
                fetureType,
551 44058 jjdelcerro
                this.getOperations().createTableReference(params),
552 43020 jjdelcerro
                primaryKeys,
553
                params.getDefaultGeometryField(),
554
                params.getCRS()
555
        );
556
        fetch.perform();
557
        return fetureType;
558
    }
559
560
    @Override
561
    public boolean add(String providerName, NewDataStoreParameters theParams, boolean overwrite)
562
            throws DataException {
563
564
        List<Pair<String, Privilege>> userAndPrivileges = new ArrayList<>();
565 45578 jjdelcerro
        JDBCNewStoreParameters params = (JDBCNewStoreParameters) theParams;
566
        if (!StringUtils.isEmpty(params.getAllRole())) {
567
            userAndPrivileges.add(
568
                    new ImmutablePair<>(params.getAllRole(), Privilege.ALL)
569 43020 jjdelcerro
            );
570
        }
571 45578 jjdelcerro
        if (!StringUtils.isEmpty(params.getDeleteRole())) {
572
            userAndPrivileges.add(
573
                    new ImmutablePair<>(params.getDeleteRole(), Privilege.DELETE)
574 43020 jjdelcerro
            );
575
        }
576 45578 jjdelcerro
        if (!StringUtils.isEmpty(params.getInsertRole())) {
577
            userAndPrivileges.add(
578
                    new ImmutablePair<>(params.getInsertRole(), Privilege.INSERT)
579 43020 jjdelcerro
            );
580
        }
581 45578 jjdelcerro
        if (!StringUtils.isEmpty(params.getReferenceRole())) {
582
            userAndPrivileges.add(
583
                    new ImmutablePair<>(params.getReferenceRole(), Privilege.REFERENCE)
584 43020 jjdelcerro
            );
585
        }
586 45578 jjdelcerro
        if (!StringUtils.isEmpty(params.getSelectRole())) {
587
            userAndPrivileges.add(
588
                    new ImmutablePair<>(params.getSelectRole(), Privilege.SELECT)
589 43020 jjdelcerro
            );
590
        }
591 45578 jjdelcerro
        if (!StringUtils.isEmpty(params.getTriggerRole())) {
592
            userAndPrivileges.add(
593
                    new ImmutablePair<>(params.getTriggerRole(), Privilege.TRIGGER)
594 43020 jjdelcerro
            );
595
        }
596 45578 jjdelcerro
        if (!StringUtils.isEmpty(params.getTruncateRole())) {
597
            userAndPrivileges.add(
598
                    new ImmutablePair<>(params.getTruncateRole(), Privilege.TRUNCATE)
599 43020 jjdelcerro
            );
600
        }
601 45578 jjdelcerro
        if (!StringUtils.isEmpty(params.getUpdateRole())) {
602
            userAndPrivileges.add(
603
                    new ImmutablePair<>(params.getUpdateRole(), Privilege.UPDATE)
604 43020 jjdelcerro
            );
605
        }
606 45578 jjdelcerro
        List<String> additionalSQLs = new ArrayList<>();
607
        if (!StringUtils.isEmpty(params.getPostCreatingStatement())) {
608 43020 jjdelcerro
            additionalSQLs.add(params.getPostCreatingStatement());
609
        }
610
        CreateTableOperation createTable = this.getOperations().createTable(
611 44058 jjdelcerro
                this.getOperations().createTableReference(params),
612 43020 jjdelcerro
                params.getDefaultFeatureType(),
613 45578 jjdelcerro
                userAndPrivileges,
614 43020 jjdelcerro
                additionalSQLs
615
        );
616 45152 fdiaz
617 44328 jjdelcerro
        boolean isOk = (boolean) createTable.perform();
618 45578 jjdelcerro
        if (!isOk) {
619 44328 jjdelcerro
            return false;
620
        }
621 45578 jjdelcerro
622 45152 fdiaz
        // We collect the featureType of the operation because
623
        // the provider has been able to make changes to it
624
        params.setDefaultFeatureType(createTable.getType());
625
626 45578 jjdelcerro
        if (theParams instanceof NewFeatureStoreParameters) {
627 44328 jjdelcerro
            DataManager dataManager = DALLocator.getDataManager();
628
            ResourcesStorage resources = this.getResourcesStorage(theParams);
629
            dataManager.writeDALResource(resources, ((NewFeatureStoreParameters) theParams).getDefaultFeatureType());
630
        }
631 45165 jjdelcerro
        this.dropCachedTables();
632 44328 jjdelcerro
        return true;
633 43020 jjdelcerro
    }
634 45578 jjdelcerro
635 45165 jjdelcerro
    private void dropCachedTables() {
636
        boolean informationTables = BooleanUtils.isTrue(
637 45578 jjdelcerro
                this.getParameters().getShowInformationDBTables()
638 45165 jjdelcerro
        );
639 45614 fdiaz
        synchronized(CACHED_TABLES){
640 45627 fdiaz
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_ALL, this.getParameters(), informationTables, SHOW_TABLES_AND_VIEWS));
641
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_FEATURE, this.getParameters(), informationTables, SHOW_TABLES_AND_VIEWS));
642
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_GEOMETRY, this.getParameters(), informationTables, SHOW_TABLES_AND_VIEWS));
643
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_ALL, this.getParameters(), informationTables, SHOW_TABLES));
644
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_FEATURE, this.getParameters(), informationTables, SHOW_TABLES));
645
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_GEOMETRY, this.getParameters(), informationTables, SHOW_TABLES));
646
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_ALL, this.getParameters(), informationTables, SHOW_VIEWS));
647
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_FEATURE, this.getParameters(), informationTables, SHOW_VIEWS));
648
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_GEOMETRY, this.getParameters(), informationTables, SHOW_VIEWS));
649 45614 fdiaz
        }
650 45165 jjdelcerro
    }
651 43020 jjdelcerro
652
    @Override
653
    public List getDataStoreProviderNames() {
654
        List x = new ArrayList(1);
655
        x.add(this.getProviderName());
656
        return x;
657
    }
658
659
    @Override
660
    public void updateTableStatistics(String database, String schema, String table) throws JDBCExecuteSQLException {
661
        UpdateTableStatisticsOperation updateStatistics = this.getOperations().createUpdateTableStatistics(
662 44058 jjdelcerro
                this.getOperations().createTableReference(database, schema, table, null)
663 43020 jjdelcerro
        );
664
        updateStatistics.perform();
665
    }
666
667
    @Override
668 45425 jjdelcerro
    public Object execute(String sql) {
669 43377 jjdelcerro
        ExecuteOperation execute = this.getOperations().createExecute(sql);
670 45425 jjdelcerro
        return execute.perform();
671 43377 jjdelcerro
    }
672
673
    @Override
674 44297 jjdelcerro
    public JDBCStoreParameters get(String name) throws DataException {
675 46338 fdiaz
        JDBCStoreParameters params = this.getOpenParameters(name);
676 43020 jjdelcerro
        return params;
677
    }
678
679
    @Override
680
    public SQLBuilder createSQLBuilder() {
681 44632 jjdelcerro
        JDBCSQLBuilderBase builder = this.getHelper().createSQLBuilder();
682
        return builder;
683 43020 jjdelcerro
    }
684 44297 jjdelcerro
685 45578 jjdelcerro
    private CustomResourcesConfig getCustomResourcesConfig() {
686
        JDBCServerExplorerParameters serverParams = this.getParameters();
687 45627 fdiaz
        String key = buildKeyForCachedTables(MODE_ALL, serverParams, false, SHOW_TABLES_AND_VIEWS); //???
688 45578 jjdelcerro
        CachedValue<CustomResourcesConfig> cachedConfig = CACHED_CUSTOM_RESOURCES_CONFIG.get(key);
689
        if (cachedConfig != null) {
690 46348 jjdelcerro
//            LOG.info("Get CustomResourcesConfig from CACHE_1: "+!cachedConfig.isExpired());
691 45578 jjdelcerro
            CustomResourcesConfig config = cachedConfig.get();
692
            return config;
693
        }
694
        JDBCStoreParameters params = this.helper.createOpenStoreParameters(this.getParameters());
695
        cachedConfig = new CachedCustomResourcesConfig(params);
696
        CACHED_CUSTOM_RESOURCES_CONFIG.put(key, cachedConfig);
697 46348 jjdelcerro
//        LOG.info("Get CustomResourcesConfig from CACHE_2: "+!cachedConfig.isExpired());
698 45578 jjdelcerro
        return cachedConfig.get();
699
    }
700 45697 jjdelcerro
701
    private void refreshCustomResourcesConfig() {
702
        JDBCServerExplorerParameters serverParams = this.getParameters();
703
        String key = buildKeyForCachedTables(MODE_ALL, serverParams, false, SHOW_TABLES_AND_VIEWS); //???
704
        CachedValue<CustomResourcesConfig> cachedConfig = CACHED_CUSTOM_RESOURCES_CONFIG.get(key);
705
        if (cachedConfig != null) {
706 46391 jjdelcerro
//            LOG.info("Force expire CustomResourcesConfig cache");
707 45697 jjdelcerro
            cachedConfig.expired();
708
        }
709
    }
710 45578 jjdelcerro
711 44328 jjdelcerro
    private ResourcesStorage getResourcesStorage(DataStoreParameters parameters, String storeName) {
712 45578 jjdelcerro
        if (DatabaseWorkspaceManager.isInternalTable(storeName)) {
713 46830 fdiaz
            ResourcesStorage resourcesStorage = ResourcesStorage.EMPTY_RESOURCESSTORAGE;
714 45578 jjdelcerro
            return resourcesStorage;
715
        }
716
        String resourcesTablename = null;
717
        try {
718 44346 jjdelcerro
            ResourcesStorage alternateResourcesStorage = null;
719 45578 jjdelcerro
            DataManager dataManager = DALLocator.getDataManager();
720
            DatabaseWorkspaceManager workspace = dataManager.getDatabaseWorkspace(parameters);
721
            if (workspace != null) {
722
                alternateResourcesStorage = workspace.getAlternativeResourcesStorage(storeName);
723
            }
724
725 45773 jjdelcerro
            ResourcesStorage defaultResourcesStorage = null;
726
            ResourcesStorage customResourcesStorage = null;
727
728 45578 jjdelcerro
            // TODO: Habria que ver de localizar los parametros sin tener que hacer un list.
729
            resourcesTablename = TABLE_RESOURCES_NAME;
730
            List<JDBCStoreParameters> tables = this.list();
731
            for (JDBCStoreParameters params : tables) {
732
                String theTableName = params.getTable();
733
                if (StringUtils.equals(theTableName, resourcesTablename)) {
734 45773 jjdelcerro
                    defaultResourcesStorage = new JDBCResourcesStorage(
735 46277 jjdelcerro
                            this.helper,
736 45578 jjdelcerro
                            alternateResourcesStorage,
737
                            params,
738
                            storeName
739
                    );
740 45773 jjdelcerro
                    break;
741 44346 jjdelcerro
                }
742
            }
743 45773 jjdelcerro
            CustomResourcesConfig config = getCustomResourcesConfig();
744
            if( config!=null ) {
745
                if( config.isInternalTable(storeName) ) {
746 46830 fdiaz
                    defaultResourcesStorage = ResourcesStorage.EMPTY_RESOURCESSTORAGE;
747 45773 jjdelcerro
                } else {
748
                    resourcesTablename = config.getResourcesTablename(storeName);
749
                    if( StringUtils.isNotBlank(resourcesTablename) ) {
750 46338 fdiaz
                        JDBCStoreParameters params = this.getOpenParameters(resourcesTablename);
751 45773 jjdelcerro
                        customResourcesStorage = new JDBCResourcesStorage(
752 46277 jjdelcerro
                                this.helper,
753 45773 jjdelcerro
                                alternateResourcesStorage,
754
                                params,
755
                                storeName,
756
                                config.isResourcesReadOnly(resourcesTablename)
757
                        );
758
                    }
759
                }
760
            }
761
            return new CompoundResourcesStorage(defaultResourcesStorage, customResourcesStorage);
762 45578 jjdelcerro
        } catch (Throwable ex) {
763
            LOG.warn("Can't retrieve reources storage for table '" + storeName + "' in '" + this.getParameters().getUrl() + " ("+resourcesTablename+").", ex);
764 44297 jjdelcerro
        }
765 46830 fdiaz
        ResourcesStorage theResourcesStorage = ResourcesStorage.EMPTY_RESOURCESSTORAGE;
766 45578 jjdelcerro
        return theResourcesStorage;
767 44297 jjdelcerro
    }
768 45578 jjdelcerro
769 46768 jjdelcerro
    public ResourcesStorage getResourcesStorage(String name) {
770
        JDBCStoreParameters params;
771
        try {
772
            ResourcesStorage customResourcesStorage = null;
773
            CustomResourcesConfig config = getCustomResourcesConfig();
774
            if( config!=null ) {
775
                if( !config.isInternalTable(name) ) {
776
                    String resourcesTablename = config.getResourcesTablename(name);
777
                    if( StringUtils.isNotBlank(resourcesTablename) ) {
778
                        params = this.getOpenParameters(resourcesTablename);
779
                        customResourcesStorage = new JDBCResourcesStorage(
780
                                this.helper,
781
                                null,
782
                                params,
783
                                TABLENAME_SERVER_EXPLORER_MARK,
784
                                config.isResourcesReadOnly(resourcesTablename)
785
                        );
786
                    }
787
                }
788
            }
789
            params = this.getOpenParameters(DatabaseWorkspaceManager.TABLE_RESOURCES_NAME);
790
            JDBCResourcesStorage defaultResourcesStorage = new JDBCResourcesStorage(
791
                    this.helper,
792
                    null,
793
                    params,
794
                    TABLENAME_SERVER_EXPLORER_MARK
795
            );
796
            if( customResourcesStorage==null ) {
797
                return defaultResourcesStorage;
798
            }
799
            return new CompoundResourcesStorage(defaultResourcesStorage, customResourcesStorage);
800
        } catch (DataException ex) {
801
            return null;
802
        }
803
    }
804
805 44632 jjdelcerro
    @Override
806 44419 jjdelcerro
    public ResourcesStorage getResourcesStorage() {
807
        JDBCStoreParameters params;
808
        try {
809 46338 fdiaz
            params = this.getOpenParameters(DatabaseWorkspaceManager.TABLE_RESOURCES_NAME);
810
//            params.setTable(DatabaseWorkspaceManager.TABLE_RESOURCES_NAME);
811 44419 jjdelcerro
            JDBCResourcesStorage theResourcesStorage = new JDBCResourcesStorage(
812 46277 jjdelcerro
                    this.helper,
813 44419 jjdelcerro
                    null,
814
                    params,
815 46338 fdiaz
                    TABLENAME_SERVER_EXPLORER_MARK
816 44419 jjdelcerro
            );
817
            return theResourcesStorage;
818
        } catch (DataException ex) {
819
            return null;
820
        }
821
    }
822 45578 jjdelcerro
823 44328 jjdelcerro
    @Override
824
    public ResourcesStorage getResourcesStorage(DataStoreParameters parameters) {
825 45578 jjdelcerro
        if (parameters == null) {
826 44328 jjdelcerro
            throw new IllegalArgumentException("null is a valid value for parameters.");
827
        }
828
        String tableName;
829 45578 jjdelcerro
        if (parameters instanceof JDBCNewStoreParameters) {
830
            tableName = ((JDBCNewStoreParameters) parameters).getTable();
831
        } else if (parameters instanceof JDBCStoreParameters) {
832
            tableName = ((JDBCStoreParameters) parameters).getTable();
833 44328 jjdelcerro
        } else {
834 45578 jjdelcerro
            throw new IllegalArgumentException("Required a JDBCStoreParameters or JDBCNewStoreParameters parameters, received " + parameters.getClass().getName() + ".");
835 44328 jjdelcerro
        }
836
        return this.getResourcesStorage(parameters, tableName);
837
    }
838 45578 jjdelcerro
839 44328 jjdelcerro
    @Override
840
    public ResourcesStorage getResourcesStorage(DataStore dataStore) {
841 46542 fdiaz
        return this.getResourcesStorage((JDBCStoreParameters) dataStore.getParameters(),
842 44328 jjdelcerro
                dataStore.getName()
843
        );
844
    }
845 44329 jjdelcerro
846
    @Override
847
    public boolean exists(DataStoreParameters parameters) throws DataException {
848
        JDBCStoreParameters params = (JDBCStoreParameters) parameters;
849
        JDBCSQLBuilderBase builder = this.getHelper().createSQLBuilder();
850
        SQLBuilder.TableNameBuilder searchTable = builder.createTableNameBuilder()
851
                .database(params.getDBName())
852
                .schema(params.getSchema())
853
                .name(params.getTable());
854
        SQLBuilder.TableNameBuilder table = builder.createTableNameBuilder();
855 45578 jjdelcerro
856 44329 jjdelcerro
        List<JDBCStoreParameters> l = this.list();
857
        for (JDBCStoreParameters current : l) {
858
            table.database(current.getDBName())
859 45578 jjdelcerro
                    .schema(current.getSchema())
860
                    .name(current.getTable());
861
            if (table.equals(searchTable)) {
862 44329 jjdelcerro
                return true;
863
            }
864
        }
865
        return false;
866
    }
867
868 45578 jjdelcerro
    @Override
869
    public void setCustomResources(String tableName, String resourcesTableName) {
870 45697 jjdelcerro
        this.setCustomResources(tableName, resourcesTableName, false);
871
    }
872
873
    @Override
874
    public void setCustomResources(String tableName, String resourcesTableName, boolean readonly) {
875 45578 jjdelcerro
        CustomResourcesConfig config = getCustomResourcesConfig();
876
        if( config==null ) {
877
            throw new RuntimeException("Can't retrieve alternative resources configuration");
878
        }
879
        config.addResourceMapping(tableName, resourcesTableName);
880 45697 jjdelcerro
        config.setResourcesReadOnly(resourcesTableName,readonly);
881 45578 jjdelcerro
        JDBCStoreParameters params = this.helper.createOpenStoreParameters(this.getParameters());
882 45767 fdiaz
        if(!setConfigValue(params, CONFIG_NAME_CUSTOM_RESOURCES, config.toJsonString())) {
883
            throw new RuntimeException("Can't save custom resources configuration");
884
        }
885 45697 jjdelcerro
        refreshCustomResourcesConfig();
886 45736 fdiaz
887
888
        if (StringUtils.isNotBlank(resourcesTableName)) {
889
            try {
890 46338 fdiaz
                params = this.getOpenParameters(resourcesTableName);
891 45736 fdiaz
                JDBCResourcesStorage resourcesStorage = new JDBCResourcesStorage(
892 46277 jjdelcerro
                        this.helper,
893 45736 fdiaz
                        null,
894
                        params,
895
                        tableName,
896
                        config.isResourcesReadOnly(resourcesTableName)
897
                );
898
                resourcesStorage.clearCache();
899
            } catch (Exception ex) {
900
                LOG.debug("Can't remove local cache for table "+tableName, ex);
901
            }
902
        }
903
904
905 45578 jjdelcerro
    }
906
907
    private static boolean setConfigValue(JDBCStoreParameters params, String name, String value) {
908
        FeatureStore store = null;
909
        try {
910
            DataManager dataManager = DALLocator.getDataManager();
911
            params.setTable(TABLE_CONFIGURATION_NAME);
912
            store = (FeatureStore) dataManager.openStore(
913
                    params.getProviderName(),
914
                    params
915
            );
916
        } catch (Exception ex) {
917
            LOG.trace("Can't read configuration value '"+name+"'", ex);
918
            // Do noting
919 45581 fdiaz
//        } finally {
920
//            DisposeUtils.disposeQuietly(store);
921 45578 jjdelcerro
        }
922
        if( store == null ) {
923
            return false;
924
        }
925
        try {
926
            store.edit();
927
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
928
            String filter = builder.eq(
929
                    builder.column(FIELD_CONFIGURATION_NAME),
930
                    builder.constant(name)
931
            ).toString();
932
            Feature feature = store.findFirst(filter);
933
            EditableFeature efeature;
934
            if (feature == null) {
935
                efeature = store.createNewFeature();
936
                efeature.set(FIELD_CONFIGURATION_NAME, name);
937
                efeature.set(FIELD_CONFIGURATION_VALUE, value);
938
                store.insert(efeature);
939
            } else {
940
                efeature = feature.getEditable();
941
                efeature.set(FIELD_CONFIGURATION_VALUE, value);
942
                store.update(efeature);
943
            }
944
            store.finishEditing();
945
            return true;
946
        } catch (Exception ex) {
947 45581 fdiaz
            LOG.debug("Can't write configuration value for '"+name+"'", ex);
948 45578 jjdelcerro
            return false;
949
        } finally {
950
            DisposeUtils.disposeQuietly(store);
951
        }
952
    }
953
954 46356 jjdelcerro
//    private static String getConfigValue_useStore(JDBCStoreParameters params, String name) {
955
//        FeatureStore store = null;
956
//        try {
957
//            DataManager dataManager = DALLocator.getDataManager();
958
//            params.setTable(TABLE_CONFIGURATION_NAME);
959
//            store = (FeatureStore) dataManager.openStore(
960
//                    params.getProviderName(),
961
//                    params
962
//            );
963
//        } catch (Exception ex) {
964
//            LOG.trace("Can't read configuration value '"+name+"'", ex);
965
//            // Do noting
966
////        } finally {
967
////            DisposeUtils.disposeQuietly(store);
968
//        }
969
//        if( store == null ) {
970
//            return null;
971
//        }
972
//        try {
973
//            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
974
//            String filter = builder.eq(
975
//                    builder.column(FIELD_CONFIGURATION_NAME),
976
//                    builder.constant(name)
977
//            ).toString();
978
//            Feature feature = store.findFirst(filter);
979
//            if( feature == null ) {
980
//                return null;
981
//            }
982
//            String value = feature.getString(FIELD_CONFIGURATION_VALUE);
983
//            return value;
984
//        } catch (Exception ex) {
985
//            LOG.debug("Can't read configuration value '"+name+"'", ex);
986
//            return null;
987
//        } finally {
988
//            DisposeUtils.disposeQuietly(store);
989
//        }
990
//    }
991
992 45578 jjdelcerro
    private static String getConfigValue(JDBCStoreParameters params, String name) {
993 46356 jjdelcerro
        JDBCServerExplorerBase explorer = null;
994 45578 jjdelcerro
        try {
995
            DataManager dataManager = DALLocator.getDataManager();
996
            params.setTable(TABLE_CONFIGURATION_NAME);
997 46356 jjdelcerro
            JDBCServerExplorerParameters explorerParams = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(params.getProviderName());
998
            ToolsLocator.getDynObjectManager().copy(params, explorerParams);
999
1000
            explorer = (JDBCServerExplorerBase) dataManager.openServerExplorer(explorerParams.getProviderName(), explorerParams);
1001
1002 45578 jjdelcerro
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
1003
            String filter = builder.eq(
1004
                    builder.column(FIELD_CONFIGURATION_NAME),
1005
                    builder.constant(name)
1006
            ).toString();
1007 46356 jjdelcerro
            OperationsFactory operations = explorer.getOperations();
1008
            RetrieveValueOperation op = operations.createRetrieveValue(
1009
                    operations.createTableReference(params),
1010
                    filter,
1011
                    null, //order
1012
                    FIELD_CONFIGURATION_VALUE
1013
            );
1014
            Object value = op.perform();
1015
            return Objects.toString(value,null);
1016 45578 jjdelcerro
        } catch (Exception ex) {
1017 45581 fdiaz
            LOG.debug("Can't read configuration value '"+name+"'", ex);
1018 45578 jjdelcerro
            return null;
1019
        } finally {
1020 46356 jjdelcerro
            DisposeUtils.disposeQuietly(explorer);
1021 45578 jjdelcerro
        }
1022
    }
1023 46356 jjdelcerro
1024 45867 jjdelcerro
    @Override
1025
    public boolean exists() {
1026
        try {
1027
            JDBCServerExplorerParameters serverParameters = this.getParameters();
1028
            OperationsFactory operations = helper.getOperations();
1029
            ListTablesOperation listTables = operations.createListTables(
1030
                    MODE_ALL, serverParameters, false, SHOW_TABLES
1031
            );
1032
            List<JDBCStoreParameters> tables = (List<JDBCStoreParameters>) listTables.perform();
1033
            return true;
1034
        } catch(Throwable th) {
1035
            return false;
1036
        }
1037
    }
1038
1039 46120 jjdelcerro
    @Override
1040
    public String getConnectionProviderStatus() {
1041
        return this.helper.getConnectionProviderStatus();
1042
    }
1043
1044 46315 jjdelcerro
    @Override
1045
    public void setTransaction(DataTransaction transaction) {
1046
        if( this.helper!=null ) {
1047
            this.helper.setTransaction((DataTransactionServices) transaction);
1048
        }
1049
    }
1050
1051
    @Override
1052
    public DataTransaction getTransaction() {
1053
        if( this.helper==null ) {
1054
            return null;
1055
        }
1056
        return this.helper.getTransaction();
1057
    }
1058
1059 46338 fdiaz
    private JDBCStoreParameters getOpenParameters(String tableName) throws DataException {
1060
        //No esta claro si se debe hacer este bucle o no. NO BORRAR ALEGREMENTE
1061
1062
//        List<JDBCStoreParameters> tables = this.list();
1063
//        for (JDBCStoreParameters params : tables) {
1064
//            String theTableName = params.getTable();
1065
//            if (StringUtils.equals(theTableName, tableName)) {
1066
//                return params;
1067
//            }
1068
//        }
1069
        JDBCStoreParameters p = this.getOpenParameters();
1070
        p.setTable(tableName);
1071
        return p;
1072
    }
1073 43020 jjdelcerro
}