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

History | View | Annotate | Download (34.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * 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
 *
11
 * 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
 *
16
 * 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
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.jdbc2.spi;
25

    
26
import java.io.File;
27
import java.util.ArrayList;
28
import java.util.Arrays;
29
import java.util.Collections;
30
import java.util.HashMap;
31
import java.util.List;
32
import java.util.Map;
33
import javax.json.JsonObject;
34
import javax.json.JsonString;
35
import javax.json.JsonValue;
36
import org.apache.commons.codec.binary.Hex;
37
import org.apache.commons.collections.map.LRUMap;
38
import org.apache.commons.lang3.BooleanUtils;
39
import org.apache.commons.lang3.ObjectUtils;
40
import org.apache.commons.lang3.StringUtils;
41
import org.apache.commons.lang3.tuple.ImmutablePair;
42
import org.apache.commons.lang3.tuple.Pair;
43
import org.gvsig.expressionevaluator.ExpressionBuilder;
44
import org.gvsig.expressionevaluator.ExpressionUtils;
45
import org.gvsig.fmap.dal.DALLocator;
46
import org.gvsig.fmap.dal.DataManager;
47
import org.gvsig.fmap.dal.DataStore;
48
import org.gvsig.fmap.dal.DataStoreParameters;
49
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
50
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_NAME;
51
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_VALUE;
52
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION_NAME;
53
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME;
54
import org.gvsig.fmap.dal.NewDataStoreParameters;
55
import org.gvsig.fmap.dal.SQLBuilder;
56
import org.gvsig.fmap.dal.SQLBuilder.Privilege;
57
import org.gvsig.fmap.dal.exception.CloseException;
58
import org.gvsig.fmap.dal.exception.DataException;
59
import org.gvsig.fmap.dal.exception.InitializeException;
60
import org.gvsig.fmap.dal.exception.OpenException;
61
import org.gvsig.fmap.dal.exception.RemoveException;
62
import org.gvsig.fmap.dal.feature.EditableFeature;
63
import org.gvsig.fmap.dal.feature.EditableFeatureType;
64
import org.gvsig.fmap.dal.feature.Feature;
65
import org.gvsig.fmap.dal.feature.FeatureStore;
66
import org.gvsig.fmap.dal.feature.FeatureType;
67
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
68
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
69
import org.gvsig.fmap.dal.serverexplorer.db.spi.AbstractDBServerExplorer;
70
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
71
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
72
import org.gvsig.fmap.dal.spi.DataServerExplorerProviderServices;
73
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters;
74
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters;
75
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
76
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
77
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException;
78
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
79
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
80
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory;
81
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CanCreateTablesOperation;
82
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.CreateTableOperation;
83
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.DropTableOperation;
84
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ExecuteOperation;
85
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.FetchFeatureTypeOperation;
86
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ListTablesOperation;
87
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.UpdateTableStatisticsOperation;
88
import org.gvsig.json.Json;
89
import org.gvsig.json.JsonObjectBuilder;
90
import org.gvsig.tools.dispose.DisposeUtils;
91
import org.gvsig.tools.exception.BaseException;
92
import org.gvsig.tools.resourcesstorage.EmptyResourcesStorage;
93
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
94
import org.gvsig.tools.util.CachedValue;
95
import org.slf4j.Logger;
96
import org.slf4j.LoggerFactory;
97

    
98
@SuppressWarnings("UseSpecificCatch")
99
public class JDBCServerExplorerBase extends AbstractDBServerExplorer implements JDBCServerExplorer {
100

    
101
    private static final Logger LOG = LoggerFactory.getLogger(JDBCServerExplorerBase.class);
102
    private static final String CONFIG_NAME_CUSTOM_RESOURCES = "CUSTOM_RESOURCES";
103

    
104
    protected JDBCHelper helper = null;
105

    
106
    private Boolean canAdd;
107

    
108
    private static final Map<String, CachedValue<List<JDBCStoreParameters>>> CACHED_TABLES = Collections.synchronizedMap(new LRUMap(10));
109
    private static final Map<String, CachedValue<CustomResourcesConfig>> CACHED_CUSTOM_RESOURCES_CONFIG = Collections.synchronizedMap(new LRUMap(10));
110

    
111
    private static class CustomResourcesConfig {
112

    
113
        private static final String CUSTOM_RESOURCES_CACHETIME = "CacheTime";
114
        private static final String CUSTOM_RESOURCES_MAPPING = "Mapping";
115

    
116

    
117
        private int cacheTime;
118
        private Map<String, String> mapping;
119

    
120
        public CustomResourcesConfig(String jsonConfig) {
121
            this.cacheTime = 30*60*1000; // 30min
122
            this.mapping = new HashMap<>();
123
            if (StringUtils.isNotBlank(jsonConfig)) {
124
                try {
125
                    JsonObject config = Json.createObject(jsonConfig);
126
                    this.cacheTime = config.getInt(CUSTOM_RESOURCES_CACHETIME, this.cacheTime);
127
                    if (!config.containsKey(CUSTOM_RESOURCES_MAPPING)) {
128
                        JsonObject m = config.getJsonObject(CUSTOM_RESOURCES_MAPPING);
129
                        for (Map.Entry<String, JsonValue> entry : m.entrySet()) {
130
                            String key = entry.getKey();
131
                            JsonValue value = entry.getValue();
132
                            if (value instanceof JsonString) {
133
                                this.mapping.put(key, ((JsonString) value).getString());
134
                            }
135
                        }
136
                    }
137
                } catch (Exception ex) {
138
                    LOG.debug("Can't parse json from "+CONFIG_NAME_CUSTOM_RESOURCES+" variable", ex);
139
                    // Do nothing.
140
                }
141
            }
142
        }
143

    
144
        public String toJsonString() {
145
            JsonObjectBuilder builder = Json.createObjectBuilder();
146
            builder.add(CUSTOM_RESOURCES_CACHETIME, cacheTime);
147
            JsonObjectBuilder m = Json.createObjectBuilder();
148
            for (Map.Entry<String, String> entry : mapping.entrySet()) {
149
                String key = entry.getKey();
150
                String value = entry.getValue();
151
                m.add(key, value);
152
            }
153
            builder.add(CUSTOM_RESOURCES_MAPPING, m);
154
            return builder.build().toString();
155
        }
156

    
157
        public int getCacheExpireTimeInMillis() {
158
            return cacheTime;
159
        }
160

    
161
        public String getResourcesTablename(String tablename) {
162
            String resourceTablename = mapping.get(tablename);
163
            return resourceTablename;
164
        }
165

    
166
        public void addResourceMapping(String tablename, String resourcesTablename) {
167
            this.mapping.put(tablename, resourcesTablename);
168
        }
169

    
170
        private boolean isInternalTable(String storeName) {
171
            if (DatabaseWorkspaceManager.isInternalTable(storeName)) {
172
                return true;
173
            }
174
            for (String resourcesTablename : this.mapping.values()) {
175
                if( StringUtils.equals(storeName, resourcesTablename) ) {
176
                    return true;
177
                }
178
            }
179
            return false;
180
        }
181
    }
182
    
183
    private static class CachedCustomResourcesConfig extends CachedValue<CustomResourcesConfig> {
184

    
185
        private final JDBCStoreParameters openParameters;
186

    
187
        private CachedCustomResourcesConfig(JDBCStoreParameters openParameters) {
188
            this.openParameters = openParameters;
189
            this.setExpireTime(30*60*1000); // 30min
190
        }
191

    
192
        @Override
193
        protected void reload() {
194
            String jsonConfig = getConfigValue(this.openParameters, CONFIG_NAME_CUSTOM_RESOURCES);
195
            CustomResourcesConfig config = new CustomResourcesConfig(jsonConfig);        
196
            this.setExpireTime(config.getCacheExpireTimeInMillis());
197
            this.setValue(config);
198
        }
199
        
200
    }
201

    
202
    private static class CachedTablesValue extends CachedValue<List<JDBCStoreParameters>> {
203

    
204
        private final int mode;
205
        private final JDBCServerExplorerParameters serverParameters;
206
        private final boolean informationTables;
207
        private JDBCHelper helper;
208
        private final int tablesOrViews;
209

    
210
        public CachedTablesValue(JDBCHelper helper, int mode, JDBCServerExplorerParameters serverParameters, boolean informationTables, int tablesOrViews) {
211
            this.mode = mode;
212
            this.serverParameters = serverParameters;
213
            this.informationTables = informationTables;
214
            this.helper = helper;
215
            this.tablesOrViews = tablesOrViews;
216
        }
217

    
218
        public CachedTablesValue(JDBCHelper helper, int mode, JDBCServerExplorerParameters serverParameters, boolean informationTables, long expireTime, int tablesOrViews) {
219
            this.mode = mode;
220
            this.setExpireTime(expireTime);
221
            this.serverParameters = serverParameters;
222
            this.informationTables = informationTables;
223
            this.helper = helper;
224
            this.tablesOrViews = tablesOrViews;
225
        }
226

    
227
        @Override
228
        protected void reload() {
229
            List<JDBCStoreParameters> tables = null;
230
            if(helper == null){
231
                this.setValue(tables);
232
                return;
233
            }
234
            OperationsFactory operations = helper.getOperations();
235
            if (operations == null) {
236
                this.setValue(null);
237
                LOG.debug("Sets tables to null to force reload tables from new ServerExplorar.");
238
                return;
239
            }
240
            try {
241
                ListTablesOperation listTables = operations.createListTables(
242
                        this.mode, serverParameters, informationTables, tablesOrViews
243
                );
244
                tables = (List<JDBCStoreParameters>) listTables.perform();
245
            } catch (Exception ex) {
246
                LOG.debug("Can't reload cached list of tables.", ex);
247
            }
248
            this.setValue(tables);
249
        }
250
        
251
        public JDBCHelper getHelper(){
252
            return this.helper;
253
        }
254
        
255
        public void dispose() {
256
            this.helper = null;
257
        }
258
    }
259

    
260
    public JDBCServerExplorerBase(
261
            JDBCServerExplorerParameters parameters,
262
            DataServerExplorerProviderServices services,
263
            JDBCHelper helper
264
    ) throws InitializeException {
265
        super(parameters, services);
266
        this.helper = helper;
267
    }
268

    
269
    @Override
270
    public String getProviderName() {
271
        return this.getHelper().getProviderName();
272
    }
273

    
274
    @Override
275
    public String getStoreName() {
276
        return this.getHelper().getProviderName();
277
    }
278

    
279
    protected DataManagerProviderServices getManager() {
280
        return (DataManagerProviderServices) DALLocator.getDataManager();
281
    }
282

    
283
    @Override
284
    public JDBCServerExplorerParameters getParameters() {
285
        return (JDBCServerExplorerParameters) super.getParameters();
286
    }
287

    
288
    @Override
289
    public boolean closeResourceRequested(ResourceProvider resource) {
290
        this.getHelper().getResulSetControler().pack();
291
        return true;
292
    }
293

    
294
    @Override
295
    public void resourceChanged(ResourceProvider resource) {
296
        // Nothing to do
297
    }
298

    
299
    protected JDBCHelper getHelper() {
300
        return helper;
301
    }
302

    
303
    protected OperationsFactory getOperations() {
304
        return this.getHelper().getOperations();
305
    }
306

    
307
    @Override
308
    public DataStore open(DataStoreParameters params) throws DataException {
309
        checkIsMine(params);
310
        DataStore store = super.open(params);
311
        return store;
312
    }
313

    
314
    @Override
315
    public List list(int mode) throws DataException {
316
        return list(mode, SHOW_TABLES_AND_VIEWS);
317
    }
318

    
319
    @Override
320
    public List list(int mode, int tablesOrViews) throws DataException {
321
        boolean informationTables = BooleanUtils.isTrue(
322
                this.getParameters().getShowInformationDBTables()
323
        );
324

    
325
        JDBCServerExplorerParameters serverParams = this.getParameters();
326

    
327
        String key = buildKeyForCachedTables(mode, serverParams, informationTables, tablesOrViews);
328
        CachedValue<List<JDBCStoreParameters>> tablesValue = CACHED_TABLES.get(key);
329
        List<JDBCStoreParameters> tables = null;
330
        if (tablesValue != null) {
331
            tables = tablesValue.get();
332
        }
333
        if (tables != null) {
334
            return tables;
335
        }
336
        tablesValue = new CachedTablesValue(this.helper, mode, serverParams, informationTables, 60000, tablesOrViews); //60"
337
        CACHED_TABLES.put(key, tablesValue);
338
        tables = tablesValue.get();
339
        return tables;
340
    }
341

    
342
    public String buildKeyForCachedTables(int mode, JDBCServerExplorerParameters params, boolean informationTables, int tablesOrViews) {
343
        JDBCServerExplorerParameters clonedParams = (JDBCServerExplorerParameters) params.getCopy();
344
        clonedParams.setSchema(null); //ListTableOperation no usa el schema
345

    
346
        StringBuilder builder = new StringBuilder();
347
        builder.append(String.valueOf(mode));
348
        builder.append(Hex.encodeHex(clonedParams.toByteArray()));
349
        builder.append(String.valueOf(informationTables));
350
        builder.append(String.valueOf(tablesOrViews));
351
        String key = builder.toString();
352
        return key;
353
    }
354

    
355
    @Override
356
    public void remove(DataStoreParameters theParams) throws RemoveException {
357
        JDBCStoreParameters params = (JDBCStoreParameters) theParams;
358
        DropTableOperation removeTable = this.getOperations().createDropTable(
359
                this.getOperations().createTableReference(params)
360
        );
361
        if ((Boolean) removeTable.perform()) {
362
            this.dropCachedTables();
363
        }
364
    }
365

    
366
    @Override
367
    public JDBCStoreParameters getOpenParameters() throws DataException {
368
        JDBCStoreParameters params = this.helper.createOpenStoreParameters(this.getParameters());
369
        return params;
370
    }
371

    
372
    @Override
373
    public NewDataStoreParameters getAddParameters(String storeName)
374
            throws DataException {
375
        JDBCNewStoreParameters params = this.getAddParameters();
376
        params.setTable(storeName);
377
        return params;
378
    }
379

    
380
    @Override
381
    public JDBCNewStoreParameters getAddParameters() throws DataException {
382
        JDBCServerExplorerParameters parameters = getParameters();
383
        JDBCNewStoreParameters params = this.helper.createNewStoreParameters();
384
        params.setHost(parameters.getHost());
385
        params.setPort(parameters.getPort());
386
        params.setDBName(parameters.getDBName());
387
        params.setUser(parameters.getUser());
388
        params.setPassword(parameters.getPassword());
389
        params.setCatalog(parameters.getCatalog());
390
        params.setSchema(parameters.getSchema());
391
        params.setJDBCDriverClassName(parameters.getJDBCDriverClassName());
392
        params.setUrl(parameters.getUrl());
393
        if (parameters instanceof FilesystemStoreParameters) {
394
            File f = ((FilesystemStoreParameters) parameters).getFile();
395
            ((FilesystemStoreParameters) params).setFile(f);
396
        }
397

    
398
        params.setDefaultFeatureType(this.getServerExplorerProviderServices()
399
                .createNewFeatureType());
400

    
401
        return params;
402
    }
403

    
404
    protected void checkIsMine(DataStoreParameters dsp) {
405
        if (!(dsp instanceof JDBCConnectionParameters)) {
406
            throw new IllegalArgumentException(
407
                    "not instance of FilesystemStoreParameters");
408
        }
409
        JDBCServerExplorerParameters parameters = getParameters();
410

    
411
        JDBCConnectionParameters pgp = (JDBCConnectionParameters) dsp;
412
        if (!StringUtils.equals(pgp.getHost(), parameters.getHost())) {
413
            throw new IllegalArgumentException("wrong explorer: Host (mine: "
414
                    + parameters.getHost() + " other:" + pgp.getHost() + ")");
415
        }
416
        if (!ObjectUtils.equals(pgp.getPort(), parameters.getPort())) {
417
            throw new IllegalArgumentException("wrong explorer: Port (mine: "
418
                    + parameters.getPort() + " other:" + pgp.getPort() + ")");
419
        }
420
        if (!StringUtils.equals(pgp.getDBName(), parameters.getDBName())) {
421
            throw new IllegalArgumentException("wrong explorer: DBName (mine: "
422
                    + parameters.getDBName() + " other:" + pgp.getDBName()
423
                    + ")");
424
        }
425
        if (!StringUtils.isEmpty(parameters.getCatalog())) {
426
            if (!StringUtils.equals(pgp.getCatalog(), parameters.getCatalog())) {
427
                throw new IllegalArgumentException(
428
                        "wrong explorer: Catalog (mine: "
429
                        + parameters.getCatalog() + " other:"
430
                        + pgp.getCatalog() + ")");
431
            }
432
        }
433
        if (!StringUtils.isEmpty(parameters.getSchema())) {
434
            if (!StringUtils.equals(pgp.getSchema(), parameters.getSchema())) {
435
                throw new IllegalArgumentException(
436
                        "wrong explorer: Schema (mine: "
437
                        + parameters.getSchema() + " other:"
438
                        + pgp.getSchema() + ")");
439
            }
440
        }
441
    }
442

    
443
    @Override
444
    public void open() throws OpenException {
445

    
446
    }
447

    
448
    @Override
449
    public void close() throws CloseException {
450

    
451
    }
452

    
453
    @Override
454
    protected void doDispose() throws BaseException {
455
        synchronized (CACHED_TABLES) {
456
            List<String> toRemove = new ArrayList<>();
457
            for (Map.Entry<String, CachedValue<List<JDBCStoreParameters>>> entry : CACHED_TABLES.entrySet()) {
458
                CachedTablesValue value = (CachedTablesValue) entry.getValue();
459
                if (value.getHelper() == this.helper) {
460
                    toRemove.add(entry.getKey());
461
                    value.dispose();
462
                }
463
            }
464
            for (String key : toRemove) {
465
                CACHED_TABLES.remove(key);
466
            }
467
            helper.dispose();
468
            helper = null;
469
        }
470
    }
471

    
472
    @Override
473
    public boolean canAdd() {
474
        if (this.canAdd == null) {
475
            CanCreateTablesOperation canAdd_ = this.getOperations().createCanCreateTables();
476
            this.canAdd = (Boolean) canAdd_.perform();
477
        }
478
        return this.canAdd;
479
    }
480

    
481
    @Override
482
    public FeatureType getFeatureType(DataStoreParameters theParams)
483
            throws DataException {
484

    
485
        JDBCStoreParameters params = (JDBCStoreParameters) theParams;
486

    
487
        checkIsMine(params);
488

    
489
        EditableFeatureType fetureType
490
                = this.getServerExplorerProviderServices().createNewFeatureType();
491

    
492
        List<String> primaryKeys = null;
493
        if (params.getPkFields() != null) {
494
            primaryKeys = Arrays.asList(params.getPkFields());
495
        }
496

    
497
        FetchFeatureTypeOperation fetch = this.getOperations().createFetchFeatureType(
498
                fetureType,
499
                this.getOperations().createTableReference(params),
500
                primaryKeys,
501
                params.getDefaultGeometryField(),
502
                params.getCRS()
503
        );
504
        fetch.perform();
505
        return fetureType;
506
    }
507

    
508
    @Override
509
    public boolean add(String providerName, NewDataStoreParameters theParams, boolean overwrite)
510
            throws DataException {
511

    
512
        List<Pair<String, Privilege>> userAndPrivileges = new ArrayList<>();
513
        JDBCNewStoreParameters params = (JDBCNewStoreParameters) theParams;
514
        if (!StringUtils.isEmpty(params.getAllRole())) {
515
            userAndPrivileges.add(
516
                    new ImmutablePair<>(params.getAllRole(), Privilege.ALL)
517
            );
518
        }
519
        if (!StringUtils.isEmpty(params.getDeleteRole())) {
520
            userAndPrivileges.add(
521
                    new ImmutablePair<>(params.getDeleteRole(), Privilege.DELETE)
522
            );
523
        }
524
        if (!StringUtils.isEmpty(params.getInsertRole())) {
525
            userAndPrivileges.add(
526
                    new ImmutablePair<>(params.getInsertRole(), Privilege.INSERT)
527
            );
528
        }
529
        if (!StringUtils.isEmpty(params.getReferenceRole())) {
530
            userAndPrivileges.add(
531
                    new ImmutablePair<>(params.getReferenceRole(), Privilege.REFERENCE)
532
            );
533
        }
534
        if (!StringUtils.isEmpty(params.getSelectRole())) {
535
            userAndPrivileges.add(
536
                    new ImmutablePair<>(params.getSelectRole(), Privilege.SELECT)
537
            );
538
        }
539
        if (!StringUtils.isEmpty(params.getTriggerRole())) {
540
            userAndPrivileges.add(
541
                    new ImmutablePair<>(params.getTriggerRole(), Privilege.TRIGGER)
542
            );
543
        }
544
        if (!StringUtils.isEmpty(params.getTruncateRole())) {
545
            userAndPrivileges.add(
546
                    new ImmutablePair<>(params.getTruncateRole(), Privilege.TRUNCATE)
547
            );
548
        }
549
        if (!StringUtils.isEmpty(params.getUpdateRole())) {
550
            userAndPrivileges.add(
551
                    new ImmutablePair<>(params.getUpdateRole(), Privilege.UPDATE)
552
            );
553
        }
554
        List<String> additionalSQLs = new ArrayList<>();
555
        if (!StringUtils.isEmpty(params.getPostCreatingStatement())) {
556
            additionalSQLs.add(params.getPostCreatingStatement());
557
        }
558
        CreateTableOperation createTable = this.getOperations().createTable(
559
                this.getOperations().createTableReference(params),
560
                params.getDefaultFeatureType(),
561
                userAndPrivileges,
562
                additionalSQLs
563
        );
564

    
565
        boolean isOk = (boolean) createTable.perform();
566
        if (!isOk) {
567
            return false;
568
        }
569

    
570
        // We collect the featureType of the operation because 
571
        // the provider has been able to make changes to it
572
        params.setDefaultFeatureType(createTable.getType());
573

    
574
        if (theParams instanceof NewFeatureStoreParameters) {
575
            DataManager dataManager = DALLocator.getDataManager();
576
            ResourcesStorage resources = this.getResourcesStorage(theParams);
577
            dataManager.writeDALResource(resources, ((NewFeatureStoreParameters) theParams).getDefaultFeatureType());
578
        }
579
        this.dropCachedTables();
580
        return true;
581
    }
582

    
583
    private void dropCachedTables() {
584
        boolean informationTables = BooleanUtils.isTrue(
585
                this.getParameters().getShowInformationDBTables()
586
        );
587
        synchronized(CACHED_TABLES){
588
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_ALL, this.getParameters(), informationTables, SHOW_TABLES_AND_VIEWS));
589
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_FEATURE, this.getParameters(), informationTables, SHOW_TABLES_AND_VIEWS));
590
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_GEOMETRY, this.getParameters(), informationTables, SHOW_TABLES_AND_VIEWS));
591
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_ALL, this.getParameters(), informationTables, SHOW_TABLES));
592
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_FEATURE, this.getParameters(), informationTables, SHOW_TABLES));
593
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_GEOMETRY, this.getParameters(), informationTables, SHOW_TABLES));
594
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_ALL, this.getParameters(), informationTables, SHOW_VIEWS));
595
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_FEATURE, this.getParameters(), informationTables, SHOW_VIEWS));
596
            CACHED_TABLES.remove(buildKeyForCachedTables(MODE_GEOMETRY, this.getParameters(), informationTables, SHOW_VIEWS));
597
        }
598
    }
599

    
600
    @Override
601
    public List getDataStoreProviderNames() {
602
        List x = new ArrayList(1);
603
        x.add(this.getProviderName());
604
        return x;
605
    }
606

    
607
    @Override
608
    public void updateTableStatistics(String database, String schema, String table) throws JDBCExecuteSQLException {
609
        UpdateTableStatisticsOperation updateStatistics = this.getOperations().createUpdateTableStatistics(
610
                this.getOperations().createTableReference(database, schema, table, null)
611
        );
612
        updateStatistics.perform();
613
    }
614

    
615
    @Override
616
    public Object execute(String sql) {
617
        ExecuteOperation execute = this.getOperations().createExecute(sql);
618
        return execute.perform();
619
    }
620

    
621
    @Override
622
    public JDBCStoreParameters get(String name) throws DataException {
623
        JDBCStoreParameters params = this.getOpenParameters();
624
        params.setTable(name);
625
        return params;
626
    }
627

    
628
    @Override
629
    public SQLBuilder createSQLBuilder() {
630
        JDBCSQLBuilderBase builder = this.getHelper().createSQLBuilder();
631
        return builder;
632
    }
633

    
634
    private CustomResourcesConfig getCustomResourcesConfig() {
635
        JDBCServerExplorerParameters serverParams = this.getParameters();
636
        String key = buildKeyForCachedTables(MODE_ALL, serverParams, false, SHOW_TABLES_AND_VIEWS); //???
637
        CachedValue<CustomResourcesConfig> cachedConfig = CACHED_CUSTOM_RESOURCES_CONFIG.get(key);
638
        if (cachedConfig != null) {
639
            CustomResourcesConfig config = cachedConfig.get();
640
            return config;
641
        }
642
        JDBCStoreParameters params = this.helper.createOpenStoreParameters(this.getParameters());
643
        cachedConfig = new CachedCustomResourcesConfig(params);
644
        CACHED_CUSTOM_RESOURCES_CONFIG.put(key, cachedConfig);
645
        return cachedConfig.get();
646
    }
647

    
648
    private ResourcesStorage getResourcesStorage(DataStoreParameters parameters, String storeName) {
649
        if (DatabaseWorkspaceManager.isInternalTable(storeName)) {
650
            EmptyResourcesStorage resourcesStorage = new EmptyResourcesStorage();
651
            return resourcesStorage;
652
        }
653
        String resourcesTablename = null;
654
        try { 
655
            ResourcesStorage alternateResourcesStorage = null;
656
            DataManager dataManager = DALLocator.getDataManager();
657
            DatabaseWorkspaceManager workspace = dataManager.getDatabaseWorkspace(parameters);
658
            if (workspace != null) {
659
                alternateResourcesStorage = workspace.getAlternativeResourcesStorage(storeName);
660
            }
661
            
662
            CustomResourcesConfig config = getCustomResourcesConfig();
663
            if( config!=null ) {
664
                if( config.isInternalTable(storeName) ) {
665
                    EmptyResourcesStorage theResourcesStorage = new EmptyResourcesStorage();
666
                    return theResourcesStorage;
667
                }
668
                resourcesTablename = config.getResourcesTablename(storeName);
669
                if( StringUtils.isNotBlank(resourcesTablename) ) {
670
                    JDBCStoreParameters params = this.getOpenParameters();
671
                    params.setTable(resourcesTablename);
672
                    JDBCResourcesStorage resourcesStorage = new JDBCResourcesStorage(
673
                            alternateResourcesStorage,
674
                            params,
675
                            storeName
676
                    );
677
                    return resourcesStorage;
678
                }
679
            }
680
            // TODO: Habria que ver de localizar los parametros sin tener que hacer un list.
681
            resourcesTablename = TABLE_RESOURCES_NAME;
682
            List<JDBCStoreParameters> tables = this.list();
683
            for (JDBCStoreParameters params : tables) {
684
                String theTableName = params.getTable();
685
                if (StringUtils.equals(theTableName, resourcesTablename)) {
686
                    JDBCResourcesStorage theResourcesStorage = new JDBCResourcesStorage(
687
                            alternateResourcesStorage,
688
                            params,
689
                            storeName
690
                    );
691
                    return theResourcesStorage;
692
                }
693
            }
694
        } catch (Throwable ex) {
695
            LOG.warn("Can't retrieve reources storage for table '" + storeName + "' in '" + this.getParameters().getUrl() + " ("+resourcesTablename+").", ex);
696
        }
697
        EmptyResourcesStorage theResourcesStorage = new EmptyResourcesStorage();
698
        return theResourcesStorage;
699
    }
700

    
701
    @Override
702
    public ResourcesStorage getResourcesStorage() {
703
        JDBCStoreParameters params;
704
        try {
705
            params = this.getOpenParameters();
706
            params.setTable(DatabaseWorkspaceManager.TABLE_RESOURCES_NAME);
707
            JDBCResourcesStorage theResourcesStorage = new JDBCResourcesStorage(
708
                    null,
709
                    params,
710
                    "$ServerExplorer"
711
            );
712
            return theResourcesStorage;
713
        } catch (DataException ex) {
714
            return null;
715
        }
716
    }
717

    
718
    @Override
719
    public ResourcesStorage getResourcesStorage(DataStoreParameters parameters) {
720
        if (parameters == null) {
721
            throw new IllegalArgumentException("null is a valid value for parameters.");
722
        }
723
        String tableName;
724
        if (parameters instanceof JDBCNewStoreParameters) {
725
            tableName = ((JDBCNewStoreParameters) parameters).getTable();
726
        } else if (parameters instanceof JDBCStoreParameters) {
727
            tableName = ((JDBCStoreParameters) parameters).getTable();
728
        } else {
729
            throw new IllegalArgumentException("Required a JDBCStoreParameters or JDBCNewStoreParameters parameters, received " + parameters.getClass().getName() + ".");
730
        }
731
        return this.getResourcesStorage(parameters, tableName);
732
    }
733

    
734
    @Override
735
    public ResourcesStorage getResourcesStorage(DataStore dataStore) {
736
        return this.getResourcesStorage(
737
                (JDBCStoreParameters) dataStore.getParameters(),
738
                dataStore.getName()
739
        );
740
    }
741

    
742
    @Override
743
    public boolean exists(DataStoreParameters parameters) throws DataException {
744
        JDBCStoreParameters params = (JDBCStoreParameters) parameters;
745
        JDBCSQLBuilderBase builder = this.getHelper().createSQLBuilder();
746
        SQLBuilder.TableNameBuilder searchTable = builder.createTableNameBuilder()
747
                .database(params.getDBName())
748
                .schema(params.getSchema())
749
                .name(params.getTable());
750
        SQLBuilder.TableNameBuilder table = builder.createTableNameBuilder();
751

    
752
        List<JDBCStoreParameters> l = this.list();
753
        for (JDBCStoreParameters current : l) {
754
            table.database(current.getDBName())
755
                    .schema(current.getSchema())
756
                    .name(current.getTable());
757
            if (table.equals(searchTable)) {
758
                return true;
759
            }
760
        }
761
        return false;
762
    }
763

    
764
    @Override
765
    public void setCustomResources(String tableName, String resourcesTableName) {
766
        CustomResourcesConfig config = getCustomResourcesConfig();
767
        if( config==null ) {
768
            throw new RuntimeException("Can't retrieve alternative resources configuration");
769
        }
770
        config.addResourceMapping(tableName, resourcesTableName);
771
        JDBCStoreParameters params = this.helper.createOpenStoreParameters(this.getParameters());
772
        setConfigValue(params, CONFIG_NAME_CUSTOM_RESOURCES, config.toJsonString());
773
    }
774

    
775
    private static boolean setConfigValue(JDBCStoreParameters params, String name, String value) {
776
        FeatureStore store = null;
777
        try {
778
            DataManager dataManager = DALLocator.getDataManager();
779
            params.setTable(TABLE_CONFIGURATION_NAME);
780
            store = (FeatureStore) dataManager.openStore(
781
                    params.getProviderName(), 
782
                    params
783
            );
784
        } catch (Exception ex) {
785
            LOG.trace("Can't read configuration value '"+name+"'", ex);
786
            // Do noting
787
//        } finally {
788
//            DisposeUtils.disposeQuietly(store);
789
        }
790
        if( store == null ) {
791
            return false;
792
        }
793
        try {
794
            store.edit();
795
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
796
            String filter = builder.eq(
797
                    builder.column(FIELD_CONFIGURATION_NAME),
798
                    builder.constant(name)
799
            ).toString();
800
            Feature feature = store.findFirst(filter);
801
            EditableFeature efeature;
802
            if (feature == null) {
803
                efeature = store.createNewFeature();
804
                efeature.set(FIELD_CONFIGURATION_NAME, name);
805
                efeature.set(FIELD_CONFIGURATION_VALUE, value);
806
                store.insert(efeature);
807
            } else {
808
                efeature = feature.getEditable();
809
                efeature.set(FIELD_CONFIGURATION_VALUE, value);
810
                store.update(efeature);
811
            }
812
            store.finishEditing();
813
            return true;
814
        } catch (Exception ex) {
815
            LOG.debug("Can't write configuration value for '"+name+"'", ex);
816
            return false;
817
        } finally {
818
            DisposeUtils.disposeQuietly(store);
819
        }
820
    }
821
    
822
    private static String getConfigValue(JDBCStoreParameters params, String name) {
823
        FeatureStore store = null;
824
        try {
825
            DataManager dataManager = DALLocator.getDataManager();
826
            params.setTable(TABLE_CONFIGURATION_NAME);
827
            store = (FeatureStore) dataManager.openStore(
828
                    params.getProviderName(), 
829
                    params
830
            );
831
        } catch (Exception ex) {
832
            LOG.trace("Can't read configuration value '"+name+"'", ex);
833
            // Do noting
834
//        } finally {
835
//            DisposeUtils.disposeQuietly(store);
836
        }
837
        if( store == null ) {
838
            return null;
839
        }
840
        try {
841
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
842
            String filter = builder.eq(
843
                    builder.column(FIELD_CONFIGURATION_NAME),
844
                    builder.constant(name)
845
            ).toString();
846
            Feature feature = store.findFirst(filter);
847
            if( feature == null ) {
848
                return null;
849
            }
850
            String value = feature.getString(FIELD_CONFIGURATION_VALUE);
851
            return value;
852
            
853
        } catch (Exception ex) {
854
            LOG.debug("Can't read configuration value '"+name+"'", ex);
855
            return null;
856
        } finally {
857
            DisposeUtils.disposeQuietly(store);
858
        }
859
    }
860
    
861
}