Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / impl / DefaultDatabaseWorkspaceManager.java @ 47784

History | View | Annotate | Download (39.2 KB)

1 44304 jjdelcerro
package org.gvsig.fmap.dal.impl;
2 44297 jjdelcerro
3 44346 jjdelcerro
import java.io.File;
4 44397 jjdelcerro
import java.util.ArrayList;
5
import java.util.Collection;
6
import java.util.Collections;
7
import java.util.List;
8 44750 jjdelcerro
import java.util.Map;
9 44633 jjdelcerro
import java.util.Objects;
10 44750 jjdelcerro
import org.apache.commons.collections4.map.LRUMap;
11 44297 jjdelcerro
import org.apache.commons.lang3.StringUtils;
12 44304 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
13 45165 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
14 44304 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionUtils;
15 44397 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
16 44297 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
17
import org.gvsig.fmap.dal.DataManager;
18 44304 jjdelcerro
import org.gvsig.fmap.dal.DataServerExplorer;
19
import org.gvsig.fmap.dal.DataServerExplorerParameters;
20 44297 jjdelcerro
import org.gvsig.fmap.dal.DataStoreParameters;
21 47606 fdiaz
import org.gvsig.fmap.dal.DataTransaction;
22 44297 jjdelcerro
import org.gvsig.fmap.dal.DataTypes;
23 46338 fdiaz
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
24 44346 jjdelcerro
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.CONFIG_NAME_STORESREPOSITORYID;
25
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.CONFIG_NAME_STORESREPOSITORYLABEL;
26
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_NAME;
27
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_CONFIGURATION_VALUE;
28 46338 fdiaz
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_FLAGS;
29 44304 jjdelcerro
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_NAME;
30 44346 jjdelcerro
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_PARAMETERS;
31
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_RESOURCES_NAME;
32
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_RESOURCES_RESOURCE;
33
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION;
34
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION_NAME;
35
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_REPOSITORY;
36
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_REPOSITORY_NAME;
37
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES;
38
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME;
39 46338 fdiaz
import org.gvsig.fmap.dal.StoresRepository;
40 47611 fdiaz
import org.gvsig.fmap.dal.SupportTransactionsHelper;
41 46338 fdiaz
import org.gvsig.fmap.dal.feature.EditableFeature;
42
import org.gvsig.fmap.dal.feature.EditableFeatureType;
43
import org.gvsig.fmap.dal.feature.Feature;
44
import org.gvsig.fmap.dal.feature.FeatureStore;
45 44304 jjdelcerro
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
46 47606 fdiaz
import org.gvsig.fmap.dal.feature.spi.LocalTransaction;
47 47784 jjdelcerro
import static org.gvsig.fmap.dal.impl.DatabaseWorkspaceStoresRepository.BIT_CHANGE_DBNAME_CATALOG_SCHEMA;
48
import static org.gvsig.fmap.dal.impl.DatabaseWorkspaceStoresRepository.BIT_CHANGE_PORT_HOST;
49
import static org.gvsig.fmap.dal.impl.DatabaseWorkspaceStoresRepository.BIT_CHANGE_USER;
50 45100 jjdelcerro
import org.gvsig.tools.dispose.DisposeUtils;
51 44397 jjdelcerro
import org.gvsig.tools.dynobject.DynClass;
52
import org.gvsig.tools.dynobject.DynField;
53 44346 jjdelcerro
import org.gvsig.tools.resourcesstorage.FilesResourcesStorage;
54
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
55 47784 jjdelcerro
import org.gvsig.tools.util.Bitmask;
56 44750 jjdelcerro
import org.gvsig.tools.util.CachedValue;
57 44397 jjdelcerro
import org.gvsig.tools.util.FileTools;
58
import org.gvsig.tools.util.HasAFile;
59 44297 jjdelcerro
import org.slf4j.Logger;
60
import org.slf4j.LoggerFactory;
61
62
/**
63
 *
64
 * @author jjdelcerro
65
 */
66
@SuppressWarnings("UseSpecificCatch")
67 44346 jjdelcerro
public class DefaultDatabaseWorkspaceManager
68 44397 jjdelcerro
        extends AbstractSymbolTable
69
        implements DatabaseWorkspaceManager
70 44346 jjdelcerro
    {
71 44297 jjdelcerro
72 44304 jjdelcerro
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDatabaseWorkspaceManager.class);
73 44397 jjdelcerro
74
    private static final String CONFIG_NAME_BASEFOLDER = "BASEFOLDER";
75
    private static final String CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH = "ALTERNATIVE_RESOURCES_PATH";
76 44346 jjdelcerro
77 47611 fdiaz
    private SupportTransactionsHelper transactionsHelper;
78 47606 fdiaz
79 44750 jjdelcerro
    private class CachedConfigValue extends CachedValue<String> {
80 44397 jjdelcerro
81 44750 jjdelcerro
        private final String name;
82
83
        public CachedConfigValue(String name, String value) {
84
          this.setValue(value);
85
          this.name = name;
86 44397 jjdelcerro
        }
87 44750 jjdelcerro
88
        public CachedConfigValue(String name, String value, long expireTime) {
89
          this.name = name;
90
          this.setValue(value);
91
          this.setExpireTime(expireTime);
92 44397 jjdelcerro
        }
93 44750 jjdelcerro
94 44397 jjdelcerro
        @Override
95
        protected void reload() {
96 46348 jjdelcerro
//            LOGGER.info("reload CachedConfigValue "+name);
97 44750 jjdelcerro
            String s = DefaultDatabaseWorkspaceManager.this.getConfigValue(name);
98 44397 jjdelcerro
            if( StringUtils.isBlank(s) ) {
99 44750 jjdelcerro
                this.setValue(null);
100 44397 jjdelcerro
            } else {
101 44750 jjdelcerro
                this.setValue(s);
102 44397 jjdelcerro
            }
103
        }
104 46338 fdiaz
105
        @Override
106
        public String toString() {
107
            return this.name+" = "+this.getValue();
108
        }
109
110
111 44397 jjdelcerro
    }
112
113 44346 jjdelcerro
    private final DataServerExplorerParameters serverParameters;
114 44304 jjdelcerro
    private Boolean existsConfiguration = null;
115
    private StoresRepository storesRepository;
116 46338 fdiaz
    private CachedConfigValue alternativeResourcesFolder = null;
117 44750 jjdelcerro
    private CachedValue<File> baseFolder;
118
    private Map<String,CachedConfigValue> cachedConfigValues;
119 44297 jjdelcerro
120 44346 jjdelcerro
    public DefaultDatabaseWorkspaceManager(DataServerExplorerParameters serverParameters) {
121
        this.serverParameters = serverParameters;
122 47611 fdiaz
        this.transactionsHelper = new SupportTransactionsHelper();
123 44750 jjdelcerro
        this.baseFolder = new CachedValue() {
124
          @Override
125
          protected void reload() {
126
            String s = DefaultDatabaseWorkspaceManager.this.getConfigValue(CONFIG_NAME_BASEFOLDER);
127
            if( StringUtils.isBlank(s) ) {
128
                this.setValue(null);
129
            } else {
130 46338 fdiaz
                s = ExpressionUtils.evaluateDynamicText(DefaultDatabaseWorkspaceManager.this, s);
131 44750 jjdelcerro
                this.setValue(new File(s));
132
            }
133
          }
134
135
        };
136
        this.baseFolder.setExpireTime(20000); // 20seg
137 46338 fdiaz
        this.alternativeResourcesFolder = new CachedConfigValue(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH, null, 30000);
138
//        this.alternativeResourcesFolder = new CachedValue() {
139
//          @Override
140
//          protected void reload() {
141
//            String s = DefaultDatabaseWorkspaceManager.this.getConfigValue(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH);
142
//            if( StringUtils.isBlank(s) ) {
143
//                this.setValue(null);
144
//            } else {
145
//                this.setValue(new File(s));
146
//            }
147
//          }
148
//
149
//        };
150
//        this.alternativeResourcesFolder.setExpireTime(30000); // 20seg
151 46334 jjdelcerro
152 44750 jjdelcerro
        this.cachedConfigValues = new LRUMap<>(20, 20);
153 45165 jjdelcerro
        ExpressionEvaluatorLocator.getExpressionEvaluatorManager().populateSymbolTable(this);
154
155 44297 jjdelcerro
    }
156 44390 jjdelcerro
157
    @Override
158
    public String toString() {
159
        return this.getLabel();
160
    }
161 44297 jjdelcerro
162
    @Override
163 44346 jjdelcerro
    public String getId() {
164
        String id = this.get(CONFIG_NAME_STORESREPOSITORYID);
165
        return id;
166
    }
167
168
    @Override
169
    public String getLabel() {
170
        String label = this.get(CONFIG_NAME_STORESREPOSITORYLABEL);
171 46930 fdiaz
        if (StringUtils.isBlank(label)) {
172
            return this.getId();
173
        }
174 44346 jjdelcerro
        return label;
175
    }
176 44390 jjdelcerro
177
    @Override
178
    public DatabaseWorkspaceManager getValue() {
179
        return this;
180
    }
181 44346 jjdelcerro
182
    @Override
183 44304 jjdelcerro
    public boolean existsTable(int tableid) {
184
        switch(tableid) {
185
            case TABLE_RESOURCES:
186
                return this.existsTable(TABLE_RESOURCES_NAME);
187
            case TABLE_REPOSITORY:
188
                return this.existsTable(TABLE_REPOSITORY_NAME);
189
            case TABLE_CONFIGURATION:
190
                return this.existsTable(TABLE_CONFIGURATION_NAME);
191
            default:
192
                throw new IllegalArgumentException("Invalid table identitier "+tableid);
193
        }
194 44297 jjdelcerro
    }
195 44304 jjdelcerro
196
    @Override
197
    public void createTable(int tableid) {
198
        switch(tableid) {
199
            case TABLE_RESOURCES:
200
                createTableResources();
201
                break;
202
            case TABLE_REPOSITORY:
203
                createTableRepository();
204
                break;
205
            case TABLE_CONFIGURATION:
206
                createTableConfiguration();
207
                break;
208
            default:
209
                throw new IllegalArgumentException("Invalid table identitier "+tableid);
210
        }
211
    }
212
213
    @Override
214
    public void dropTable(int tableid) {
215
        switch(tableid) {
216
            case TABLE_RESOURCES:
217
                this.dropTable(TABLE_RESOURCES_NAME);
218
                break;
219
            case TABLE_REPOSITORY:
220
                this.dropTable(TABLE_REPOSITORY_NAME);
221
                break;
222
            case TABLE_CONFIGURATION:
223
                this.dropTable(TABLE_CONFIGURATION_NAME);
224
                break;
225
            default:
226
                throw new IllegalArgumentException("Invalid table identitier "+tableid);
227
        }
228
    }
229
230
    @Override
231
    public FeatureStore getTable(int tableid) {
232
        switch(tableid) {
233
            case TABLE_RESOURCES:
234
                return this.getTable(TABLE_RESOURCES_NAME);
235
            case TABLE_REPOSITORY:
236
                return this.getTable(TABLE_REPOSITORY_NAME);
237
            case TABLE_CONFIGURATION:
238
                return this.getTable(TABLE_CONFIGURATION_NAME);
239
            default:
240
                throw new IllegalArgumentException("Invalid table identitier "+tableid);
241
        }
242
    }
243
244 44633 jjdelcerro
    @Override
245
    public DataServerExplorer getServerExplorer() {
246 47606 fdiaz
        DataManager dataManager = DALLocator.getDataManager();
247
        DataServerExplorer server = null;
248
        LocalTransaction trans = new LocalTransaction(dataManager, this.getTransaction());
249 44297 jjdelcerro
        try {
250 47606 fdiaz
            trans.begin();
251
            trans.add(this);
252
            server = dataManager.openServerExplorer(
253 44346 jjdelcerro
                    this.serverParameters.getProviderName(),
254
                    this.serverParameters
255 44297 jjdelcerro
            );
256 47606 fdiaz
            trans.add(server);
257
            trans.commit();
258 44633 jjdelcerro
            return server;
259
        } catch (Exception ex) {
260 47606 fdiaz
            trans.abortQuietly();
261 44633 jjdelcerro
            throw new RuntimeException("Can't get server explorer for workspace '"+Objects.toString(this.serverParameters)+"'", ex);
262 47606 fdiaz
        } finally {
263
            trans.closeQuietly();
264 44633 jjdelcerro
        }
265
    }
266 46338 fdiaz
267
    @Override
268
    public DataServerExplorerParameters getServerExplorerParameters() {
269
        return this.serverParameters;
270
    }
271 44633 jjdelcerro
272 46338 fdiaz
273
274 44633 jjdelcerro
    private boolean existsTable(String tableName) {
275 45100 jjdelcerro
        DataServerExplorer server = null;
276 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
277 44633 jjdelcerro
        try {
278 47606 fdiaz
            trans.begin();
279
            trans.add(this);
280 45100 jjdelcerro
            server = this.getServerExplorer();
281 47606 fdiaz
            trans.add(server);
282 46368 fdiaz
            List<DataStoreParameters> list = server.list();
283
            if(list == null){
284 47606 fdiaz
                trans.commit();
285 46368 fdiaz
                return false;
286
            }
287 44297 jjdelcerro
            for (DataStoreParameters params : server.list()) {
288 44304 jjdelcerro
                String theTableName = (String) params.getDynValue("Table");
289 44297 jjdelcerro
                if( StringUtils.equals(theTableName, tableName) ) {
290 47606 fdiaz
                    trans.commit();
291 44297 jjdelcerro
                    return true;
292
                }
293
            }
294 47606 fdiaz
            trans.commit();
295 44297 jjdelcerro
        } catch (Exception ex) {
296
            LOGGER.warn("Can't check if the table '"+tableName+"' exists.",ex);
297 47606 fdiaz
            trans.abortQuietly();
298 45100 jjdelcerro
        } finally {
299 47606 fdiaz
            trans.closeQuietly();
300 45100 jjdelcerro
            DisposeUtils.disposeQuietly(server);
301 44297 jjdelcerro
        }
302
        return false;
303
    }
304
305
    private FeatureStore getTable(String tableName) {
306 45100 jjdelcerro
        DataServerExplorer server = null;
307 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
308 44297 jjdelcerro
        try {
309 47606 fdiaz
            trans.begin();
310
            trans.add(this);
311 44297 jjdelcerro
            DataManager dataManager = DALLocator.getDataManager();
312 45100 jjdelcerro
            server = this.getServerExplorer();
313 47606 fdiaz
            trans.add(server);
314 44297 jjdelcerro
            DataStoreParameters params = server.get(tableName);
315
            if( params!=null ) {
316
                FeatureStore store = (FeatureStore) dataManager.openStore(
317 47606 fdiaz
                        this.getTransaction(),
318 44326 jjdelcerro
                        params.getProviderName(),
319 45967 jjdelcerro
                        params,
320
                        true
321 44297 jjdelcerro
                );
322 47606 fdiaz
                trans.commit();
323 44297 jjdelcerro
                return store;
324
            }
325 47606 fdiaz
            trans.commit();
326 44297 jjdelcerro
        } catch (Exception ex) {
327
            LOGGER.warn("Can't open table '"+tableName+"'.",ex);
328 47606 fdiaz
            trans.abortQuietly();
329 45100 jjdelcerro
        } finally {
330 47606 fdiaz
            trans.closeQuietly();
331 45100 jjdelcerro
            DisposeUtils.disposeQuietly(server);
332 44297 jjdelcerro
        }
333
        return null;
334
    }
335
336
    private void dropTable(String tableName) {
337 45100 jjdelcerro
        DataServerExplorer server = null;
338 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
339 44297 jjdelcerro
        try {
340 47606 fdiaz
            trans.begin();
341
            trans.add(this);
342 45100 jjdelcerro
            server = this.getServerExplorer();
343 47606 fdiaz
            trans.add(server);
344 44297 jjdelcerro
            DataStoreParameters params = server.get(tableName);
345
            if( params!=null ) {
346
                server.remove(params);
347
            }
348 47606 fdiaz
            trans.commit();
349 44297 jjdelcerro
        } catch (Exception ex) {
350
            LOGGER.warn("Can't drop table '"+tableName+"'.",ex);
351 47606 fdiaz
            trans.abortQuietly();
352 45100 jjdelcerro
        } finally {
353 47606 fdiaz
            trans.closeQuietly();
354 45100 jjdelcerro
            DisposeUtils.disposeQuietly(server);
355 44297 jjdelcerro
        }
356
    }
357
358 46126 jjdelcerro
    @Override
359 45580 jjdelcerro
    public void createTableResources(String tableName) throws RuntimeException {
360 45767 fdiaz
        // H2Spatial crea esta table a mano.
361
        // Si tocamos algo aqu? hay que modificar
362
        // la creacion de esta tabla en el helper de H2
363
364 45100 jjdelcerro
        DataServerExplorer server = null;
365 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
366 44297 jjdelcerro
        try {
367 47606 fdiaz
            trans.begin();
368
            trans.add(this);
369 45100 jjdelcerro
            server = this.getServerExplorer();
370 47606 fdiaz
            trans.add(server);
371 44304 jjdelcerro
            NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName);
372 44297 jjdelcerro
            EditableFeatureType ft = params.getDefaultFeatureType();
373 44304 jjdelcerro
            ft.add(FIELD_RESOURCES_NAME, DataTypes.STRING, 150)
374 44297 jjdelcerro
                    .setAllowNull(false)
375
                    .setIsPrimaryKey(true);
376 44304 jjdelcerro
            ft.add(FIELD_RESOURCES_RESOURCE, DataTypes.BYTEARRAY)
377 44297 jjdelcerro
                    .setAllowNull(true);
378
            server.add(tableName, params, false);
379 47606 fdiaz
            trans.commit();
380 44297 jjdelcerro
        } catch (Exception ex) {
381 45580 jjdelcerro
            LOGGER.debug("Can't create resources table '"+tableName+"'.",ex);
382 47606 fdiaz
            trans.abortQuietly();
383 45580 jjdelcerro
            throw new RuntimeException("Can't create resources table '"+tableName+"'.", ex);
384 45100 jjdelcerro
        } finally {
385 47606 fdiaz
            trans.closeQuietly();
386 45100 jjdelcerro
            DisposeUtils.disposeQuietly(server);
387 44297 jjdelcerro
        }
388
    }
389
390 45580 jjdelcerro
    private void createTableResources() {
391
        createTableResources(TABLE_RESOURCES_NAME);
392
    }
393
394 46126 jjdelcerro
    @Override
395 45580 jjdelcerro
    public void createTableRepository(String tableName) throws RuntimeException {
396 45100 jjdelcerro
        DataServerExplorer server = null;
397 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
398 44304 jjdelcerro
        try {
399 47606 fdiaz
            trans.begin();
400
            trans.add(this);
401 45100 jjdelcerro
            server = this.getServerExplorer();
402 47606 fdiaz
            trans.add(server);
403 44304 jjdelcerro
            NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName);
404
            EditableFeatureType ft = params.getDefaultFeatureType();
405
            ft.add(FIELD_REPOSITORY_NAME, DataTypes.STRING, 150)
406
                    .setAllowNull(false)
407
                    .setIsPrimaryKey(true);
408
            ft.add(FIELD_REPOSITORY_PARAMETERS, DataTypes.BYTEARRAY)
409
                    .setAllowNull(true);
410 45208 omartinez
            ft.add(FIELD_REPOSITORY_FLAGS, DataTypes.INT)
411
                    .setAllowNull(false)
412
                    .setDefaultValue(0);
413 44304 jjdelcerro
            server.add(tableName, params, false);
414 47606 fdiaz
            trans.commit();
415 44304 jjdelcerro
        } catch (Exception ex) {
416 45580 jjdelcerro
            LOGGER.debug("Can't create repository table '"+tableName+"'.",ex);
417 47606 fdiaz
            trans.abortQuietly();
418 45580 jjdelcerro
            throw new RuntimeException("Can't create repository table '"+tableName+"'.", ex);
419 45100 jjdelcerro
        } finally {
420 47606 fdiaz
            trans.closeQuietly();
421 45100 jjdelcerro
            DisposeUtils.disposeQuietly(server);
422 44304 jjdelcerro
        }
423 44297 jjdelcerro
    }
424
425 45580 jjdelcerro
    private void createTableRepository() {
426
        createTableRepository(TABLE_REPOSITORY_NAME);
427
    }
428
429 44304 jjdelcerro
    private void createTableConfiguration() {
430 45767 fdiaz
        // H2Spatial crea esta table a mano.
431
        // Si tocamos algo aqu? hay que modificar
432
        // la creacion de esta tabla en el helper de H2
433 44304 jjdelcerro
        String tableName = TABLE_CONFIGURATION_NAME;
434 45100 jjdelcerro
        DataServerExplorer server = null;
435 46126 jjdelcerro
        FeatureStore store = null;
436 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
437 44304 jjdelcerro
        try {
438 47606 fdiaz
            trans.begin();
439
            trans.add(this);
440 45100 jjdelcerro
            server = this.getServerExplorer();
441 47606 fdiaz
            trans.add(server);
442 44304 jjdelcerro
            NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName);
443
            EditableFeatureType ft = params.getDefaultFeatureType();
444
            ft.add(FIELD_CONFIGURATION_NAME, DataTypes.STRING, 200)
445
                    .setAllowNull(false)
446
                    .setIsPrimaryKey(true);
447 45767 fdiaz
            ft.add(FIELD_CONFIGURATION_VALUE, DataTypes.STRING, 10240)
448 44304 jjdelcerro
                    .setAllowNull(true);
449
            server.add(tableName, params, false);
450 46126 jjdelcerro
451
            DataStoreParameters openParams = server.get(TABLE_CONFIGURATION_NAME);
452
            store = (FeatureStore) DALLocator.getDataManager().openStore(
453 47606 fdiaz
                    this.getTransaction(), openParams.getProviderName(), openParams
454 46126 jjdelcerro
            );
455
            store.edit();
456
            EditableFeature efeature = store.createNewFeature();
457
            efeature.set(FIELD_CONFIGURATION_NAME, CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH);
458
            efeature.set(FIELD_CONFIGURATION_VALUE, null);
459
            store.insert(efeature);
460
            store.finishEditing();
461
462 45165 jjdelcerro
            this.existsConfiguration = null;
463 47606 fdiaz
            trans.commit();
464 44304 jjdelcerro
        } catch (Exception ex) {
465
            LOGGER.warn("Can't create table '"+tableName+"'.",ex);
466 47606 fdiaz
//            FeatureStore.cancelEditingQuietly(store);
467
            trans.abortQuietly();
468 45100 jjdelcerro
        } finally {
469 47606 fdiaz
            trans.closeQuietly();
470 45100 jjdelcerro
            DisposeUtils.disposeQuietly(server);
471 46126 jjdelcerro
            DisposeUtils.disposeQuietly(store);
472 44304 jjdelcerro
        }
473 44297 jjdelcerro
    }
474 44304 jjdelcerro
475
    private boolean existsConfiguration() {
476
        if( this.existsConfiguration==null ) {
477
            this.existsConfiguration = this.existsTable(TABLE_CONFIGURATION_NAME);
478
        }
479
        return this.existsConfiguration;
480
    }
481 44297 jjdelcerro
482
    @Override
483
    public String get(String name) {
484 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
485 44304 jjdelcerro
        try {
486 47606 fdiaz
            trans.begin();
487
            trans.add(this);
488
            if (!this.existsConfiguration()) {
489
                trans.commit();
490 44304 jjdelcerro
                return null;
491
            }
492 47606 fdiaz
            if (StringUtils.equalsIgnoreCase(name, CONFIG_NAME_BASEFOLDER)) {
493
                String s = Objects.toString(this.baseFolder.get(), null);
494
                trans.commit();
495
                return s;
496 44750 jjdelcerro
            }
497 47606 fdiaz
            if (StringUtils.equalsIgnoreCase(name, CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH)) {
498
                String s = Objects.toString(this.alternativeResourcesFolder.get(), null);
499
                trans.commit();
500
                return s;
501 46338 fdiaz
            }
502 44750 jjdelcerro
            CachedConfigValue cachedValue = this.cachedConfigValues.get(name);
503 47606 fdiaz
            if (cachedValue != null) {
504
                String s = cachedValue.get();
505
                trans.commit();
506
                return s;
507 44750 jjdelcerro
            }
508
            String value = this.getConfigValue(name);
509
            this.cachedConfigValues.put(name, new CachedConfigValue(name, value, 15000));
510 46338 fdiaz
            value = ExpressionUtils.evaluateDynamicText(this, value);
511 47606 fdiaz
            trans.commit();
512 44750 jjdelcerro
            return value;
513
        } catch (Exception ex) {
514
            LOGGER.warn("Can't read configuration value '"+name+"'", ex);
515 47606 fdiaz
            trans.abortQuietly();
516 44750 jjdelcerro
            return null;
517 47606 fdiaz
        } finally {
518
            trans.closeQuietly();
519 44750 jjdelcerro
        }
520
    }
521
522
    private String getConfigValue(String name) {
523 45100 jjdelcerro
        FeatureStore store = null;
524 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
525 44750 jjdelcerro
        try {
526 47606 fdiaz
            trans.begin();
527
            trans.add(this);
528 44750 jjdelcerro
            if( !this.existsConfiguration() ) {
529 47606 fdiaz
                trans.commit();
530 44750 jjdelcerro
                return null;
531
            }
532 45100 jjdelcerro
            store = this.getTable(TABLE_CONFIGURATION);
533 44304 jjdelcerro
            if( store == null ) {
534 47606 fdiaz
                trans.commit();
535 44304 jjdelcerro
                return null;
536
            }
537 47606 fdiaz
            trans.add(store);
538 44304 jjdelcerro
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
539
            String filter = builder.eq(
540
                    builder.column(FIELD_CONFIGURATION_NAME),
541
                    builder.constant(name)
542
            ).toString();
543
            Feature feature = store.findFirst(filter);
544
            if( feature == null ) {
545 47606 fdiaz
                trans.commit();
546 44304 jjdelcerro
                return null;
547
            }
548
            String value = feature.getString(FIELD_CONFIGURATION_VALUE);
549 46338 fdiaz
//            value = ExpressionUtils.evaluateDynamicText(this, value);
550 47606 fdiaz
            trans.commit();
551 44304 jjdelcerro
            return value;
552
553
        } catch (Exception ex) {
554
            LOGGER.warn("Can't read configuration value '"+name+"'", ex);
555 47606 fdiaz
            trans.abortQuietly();
556 44304 jjdelcerro
            return null;
557 45100 jjdelcerro
        } finally {
558 47606 fdiaz
            trans.closeQuietly();
559 45100 jjdelcerro
            DisposeUtils.disposeQuietly(store);
560 44304 jjdelcerro
        }
561 44297 jjdelcerro
    }
562
563
    @Override
564 44326 jjdelcerro
    public boolean set(String name, String value) {
565 45100 jjdelcerro
        FeatureStore store = null;
566 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
567 44326 jjdelcerro
        try {
568 47606 fdiaz
            trans.begin();
569
            trans.add(this);
570 44326 jjdelcerro
            if( !this.existsConfiguration() ) {
571 47606 fdiaz
                trans.commit();
572 44326 jjdelcerro
                return false;
573
            }
574 45100 jjdelcerro
            store = this.getTable(TABLE_CONFIGURATION);
575 44326 jjdelcerro
            if( store == null ) {
576 47606 fdiaz
                trans.commit();
577 44326 jjdelcerro
                return false;
578
            }
579 47606 fdiaz
            trans.add(store);
580 44326 jjdelcerro
            store.edit();
581
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
582
            String filter = builder.eq(
583
                    builder.column(FIELD_CONFIGURATION_NAME),
584
                    builder.constant(name)
585
            ).toString();
586
            Feature feature = store.findFirst(filter);
587
            EditableFeature efeature;
588
            if (feature == null) {
589
                efeature = store.createNewFeature();
590
                efeature.set(FIELD_CONFIGURATION_NAME, name);
591
                efeature.set(FIELD_CONFIGURATION_VALUE, value);
592
                store.insert(efeature);
593
            } else {
594
                efeature = feature.getEditable();
595
                efeature.set(FIELD_CONFIGURATION_VALUE, value);
596
                store.update(efeature);
597
            }
598
            store.finishEditing();
599 46338 fdiaz
            if( StringUtils.equalsIgnoreCase(name, CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH)) {
600
                this.alternativeResourcesFolder.expired();
601
            } else if( this.cachedConfigValues.containsKey(name) ) {
602
                value = ExpressionUtils.evaluateDynamicText(this, value);
603
                this.cachedConfigValues.get(name).set(value);
604 44750 jjdelcerro
            }
605 47606 fdiaz
            trans.commit();
606 44326 jjdelcerro
            return true;
607
        } catch (Exception ex) {
608
            LOGGER.warn("Can't write configuration value '"+name+"'", ex);
609 47606 fdiaz
            trans.abortQuietly();
610 44326 jjdelcerro
            return false;
611 45100 jjdelcerro
        } finally {
612 47606 fdiaz
            trans.closeQuietly();
613 45100 jjdelcerro
            DisposeUtils.disposeQuietly(store);
614 44326 jjdelcerro
        }
615 44297 jjdelcerro
    }
616
617
    @Override
618
    public StoresRepository getStoresRepository() {
619 44304 jjdelcerro
        if( this.storesRepository==null ) {
620
            String id = this.get(CONFIG_NAME_STORESREPOSITORYID);
621
            String label = this.get(CONFIG_NAME_STORESREPOSITORYLABEL);
622
            this.storesRepository = new DatabaseWorkspaceStoresRepository(
623
                    id,
624
                    label,
625 44397 jjdelcerro
                    this
626 44304 jjdelcerro
            );
627
        }
628
        return this.storesRepository;
629 44297 jjdelcerro
    }
630 44346 jjdelcerro
631
    public boolean contains(DataStoreParameters parameters) {
632
        boolean c = this.getStoresRepository().contains(parameters);
633
        return c;
634
    }
635 44297 jjdelcerro
636
    @Override
637
    public boolean canAnonymousUserWriteInTheTables() {
638
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
639
    }
640
641
    @Override
642 44397 jjdelcerro
    public File getBaseFolder() {
643
        return this.baseFolder.get();
644
    }
645
646
    @Override
647
    public void setBaseFolder(File baseFolder) {
648
        this.baseFolder.set(baseFolder);
649 44750 jjdelcerro
        this.set(CONFIG_NAME_BASEFOLDER, baseFolder.toString());
650 44397 jjdelcerro
    }
651
652 46352 jjdelcerro
    public File getWorkspaceFile() {
653 45165 jjdelcerro
        if( this.serverParameters instanceof HasAFile ) {
654
            return ((HasAFile)this.serverParameters).getFile();
655
        }
656
        return null;
657
    }
658
659 44397 jjdelcerro
    private DataStoreParameters replaceInFilesToUseBaseFolder(DataStoreParameters parameters) {
660
        DynClass dynClass = parameters.getDynClass();
661
        if( dynClass == null ) {
662
            return parameters;
663
        }
664
        File theBaseFolder = this.getBaseFolder();
665 45165 jjdelcerro
        File wsfile = getWorkspaceFile();
666
        if( theBaseFolder==null && wsfile==null ) {
667 44397 jjdelcerro
            return parameters;
668
        }
669
        DataStoreParameters replaced = parameters;
670
        for (DynField dynField : dynClass.getDynFields()) {
671
            switch(dynField.getType()) {
672
                case DataTypes.FILE:
673
                case DataTypes.FOLDER:
674
                    File f = (File) parameters.getDynValue(dynField.getName());
675 45165 jjdelcerro
                    File newf = null;
676 44397 jjdelcerro
                    if( f!=null ) {
677 45165 jjdelcerro
                        if( wsfile!=null ) {
678
                            if( wsfile.equals(f) ) {
679
                                ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
680
                                newf = ExpressionUtils.createDynamicFile(
681
                                    builder.ifnull(
682
                                        builder.variable("WORKSPACEFILE"),
683
                                        builder.constant(f.toString()),
684
                                        builder.variable("WORKSPACEFILE")
685
                                    )
686
                                );
687
                            }
688
                        }
689
                        if( newf==null ) {
690
                            File frel = FileTools.relativizeFile(theBaseFolder, f);
691
                            if( frel!=f ) {
692
                                ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
693
                                newf = ExpressionUtils.createDynamicFile(
694
                                    builder.concat(
695
                                            builder.variable(CONFIG_NAME_BASEFOLDER),
696
                                            builder.constant("/"),
697
                                            builder.variable(frel.getPath())
698
                                    )
699
                                );
700
                            }
701
                        }
702
                        if( newf!=null ) {
703 44397 jjdelcerro
                            if( replaced == parameters ) {
704
                                replaced = (DataStoreParameters) replaced.getCopy();
705
                            }
706 45165 jjdelcerro
                            replaced.setDynValue(dynField.getName(), newf);
707 44397 jjdelcerro
                        }
708
                    }
709
                    break;
710
            }
711
        }
712
        return replaced;
713
    }
714
715
    @Override
716 44346 jjdelcerro
    public boolean writeStoresRepositoryEntry(String name, DataStoreParameters parameters) {
717 47784 jjdelcerro
        return writeStoresRepositoryEntry(
718
                name,
719
                parameters,
720
                Bitmask.createBitmask(0)
721
                        .setBit(BIT_CHANGE_PORT_HOST)
722
                        .setBit(BIT_CHANGE_USER)
723
                        .setBit(BIT_CHANGE_DBNAME_CATALOG_SCHEMA)
724
        );
725
    }
726
727
    @Override
728
    public boolean writeStoresRepositoryEntry(String name, DataStoreParameters parameters, Bitmask flags) {
729 45100 jjdelcerro
        FeatureStore store = null;
730 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
731 44304 jjdelcerro
        try {
732 47606 fdiaz
            trans.begin();
733
            trans.add(this);
734 44397 jjdelcerro
            parameters = this.replaceInFilesToUseBaseFolder(parameters);
735 44304 jjdelcerro
            byte[] data = parameters.toByteArray();
736
            if( data == null ) {
737
                throw new RuntimeException("Can't convert parameters to byte array.");
738
            }
739 45100 jjdelcerro
            store = this.getTable(TABLE_REPOSITORY);
740 47606 fdiaz
            trans.add(store);
741 44304 jjdelcerro
            store.edit();
742
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
743
            String filter = builder.eq(
744
                    builder.column(FIELD_REPOSITORY_NAME),
745
                    builder.constant(name)
746
            ).toString();
747
            Feature feature = store.findFirst(filter);
748
            EditableFeature efeature;
749
            if (feature == null) {
750
                efeature = store.createNewFeature();
751
                efeature.set(FIELD_REPOSITORY_NAME, name);
752
                efeature.set(FIELD_REPOSITORY_PARAMETERS, data);
753 47784 jjdelcerro
                efeature.set(FIELD_REPOSITORY_FLAGS, flags.get());
754 44304 jjdelcerro
                store.insert(efeature);
755
            } else {
756
                efeature = feature.getEditable();
757
                efeature.set(FIELD_REPOSITORY_PARAMETERS, data);
758 47784 jjdelcerro
                efeature.set(FIELD_REPOSITORY_FLAGS, flags.get());
759 44304 jjdelcerro
                store.update(efeature);
760
            }
761
            store.finishEditing();
762 46334 jjdelcerro
            this.storesRepository = null;
763 47606 fdiaz
            trans.commit();
764 44304 jjdelcerro
            return true;
765
766
        } catch (Exception ex) {
767
            LOGGER.warn("Can't save entry '"+name+"' in repository information", ex);
768 47606 fdiaz
            trans.abortQuietly();
769 44304 jjdelcerro
            return false;
770 45100 jjdelcerro
        } finally {
771 47606 fdiaz
            trans.closeQuietly();
772 45100 jjdelcerro
            DisposeUtils.disposeQuietly(store);
773 44304 jjdelcerro
        }
774 44297 jjdelcerro
    }
775
776
    @Override
777 44380 jjdelcerro
    public boolean isValid() {
778 44728 jjdelcerro
      try {
779 44304 jjdelcerro
        String id = this.get(CONFIG_NAME_STORESREPOSITORYID);
780
        if( StringUtils.isBlank(id) ) {
781
            return false;
782
        }
783 44380 jjdelcerro
        return true;
784 44728 jjdelcerro
      } catch(Exception ex) {
785
        return false;
786
      }
787 44380 jjdelcerro
    }
788
789
    @Override
790
    public boolean isValidStoresRepository() {
791
        if( !this.isValid() ) {
792
            return false;
793
        }
794 44304 jjdelcerro
        if( !this.existsTable(TABLE_REPOSITORY_NAME) ) {
795
            return false;
796
        }
797
        return true;
798
799 44297 jjdelcerro
    }
800 44346 jjdelcerro
801
    @Override
802 44380 jjdelcerro
    public ResourcesStorage getAlternativeResourcesStorage(String tableName) {
803 46348 jjdelcerro
//        LOGGER.info("getAlternativeResourcesStorage from cache: "+!this.alternativeResourcesFolder.isExpired());
804 46338 fdiaz
        String s = this.alternativeResourcesFolder.get();
805 46348 jjdelcerro
//        LOGGER.info("alternativeResourcesStorage: "+s);
806 46338 fdiaz
        if( StringUtils.isBlank(s) ) {
807
            return null;
808
        }
809
810
        s = ExpressionUtils.evaluateDynamicText(this, s);
811
812
        File folder = new File(s);
813
814 44380 jjdelcerro
        if( folder==null ) {
815
            return null;
816
        }
817
        String resourcesRoot = folder.getAbsolutePath().replace("\\","/") + "/" + tableName;
818
        ResourcesStorage resources = new FilesResourcesStorage(resourcesRoot);
819
        return resources;
820 44346 jjdelcerro
    }
821 46338 fdiaz
822
    @Override
823
    public boolean hasAlternativeResourcesStorage() {
824
        String path = this.get(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH);
825
        return StringUtils.isNotBlank(path);
826
    }
827 44362 jjdelcerro
828
    @Override
829 46334 jjdelcerro
    public void setAlternativeResourcesStorage(String resourcesPath) {
830 46338 fdiaz
//        this.fix();
831
      this.set(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH, resourcesPath);
832
//      this.alternativeResourcesFolder.expired();
833 46334 jjdelcerro
    }
834
835
    @Override
836 44362 jjdelcerro
    public void drop() {
837
        if( !this.existsTable(TABLE_RESOURCES) ) {
838
            this.dropTable(TABLE_RESOURCES);
839
        }
840
        if( !this.existsTable(TABLE_CONFIGURATION) ) {
841
            this.dropTable(TABLE_CONFIGURATION);
842
        }
843
        if( !this.existsTable(TABLE_REPOSITORY) ) {
844
            this.dropTable(TABLE_REPOSITORY);
845
        }
846
    }
847 44346 jjdelcerro
848 44362 jjdelcerro
849
    @Override
850 44346 jjdelcerro
    public void create(String id, String description) {
851 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
852
        try {
853
            trans.begin();
854
            trans.add(this);
855
856
            if (!this.existsTable(TABLE_RESOURCES)) {
857
                this.createTable(TABLE_RESOURCES);
858
            }
859
            if (!this.existsTable(TABLE_CONFIGURATION)) {
860
                this.createTable(TABLE_CONFIGURATION);
861
            }
862
            if (!this.existsTable(TABLE_REPOSITORY)) {
863
                this.createTable(TABLE_REPOSITORY);
864
            }
865
            this.set(CONFIG_NAME_STORESREPOSITORYID, id);
866
            this.set(CONFIG_NAME_STORESREPOSITORYLABEL, description);
867
            this.set(CONFIG_CAN_ANONYMOUS_USER_WRITE_IN_THE_TABLES, "true");
868
            this.set(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH, "");
869
            File theBaseFolder = this.getBaseFolder();
870
            if (this.serverParameters instanceof HasAFile && theBaseFolder == null) {
871
                File f = ((HasAFile) this.serverParameters).getFile();
872
                if (f != null) {
873
                    theBaseFolder = f.getParentFile();
874
                    this.setBaseFolder(theBaseFolder);
875
                } else {
876
                    this.set(CONFIG_NAME_BASEFOLDER, "");
877
                }
878 44397 jjdelcerro
            } else {
879 47606 fdiaz
                this.set(CONFIG_NAME_BASEFOLDER, "");
880 44397 jjdelcerro
            }
881 47606 fdiaz
            trans.commit();
882
        } catch (RuntimeException ex) {
883
            trans.abortQuietly();
884
            throw ex;
885
        } catch (Exception ex) {
886
            trans.abortQuietly();
887
            throw new RuntimeException("Can't create table " + id, ex);
888
        } finally {
889
            trans.closeQuietly();
890 44397 jjdelcerro
        }
891 44346 jjdelcerro
    }
892 44397 jjdelcerro
893
    @Override
894
    public boolean exists(String name) {
895 45100 jjdelcerro
        FeatureStore store = null;
896 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
897 44397 jjdelcerro
        try {
898 47606 fdiaz
            trans.begin();
899
            trans.add(this);
900 45165 jjdelcerro
            if( StringUtils.equalsIgnoreCase(name, "WORKSPACEFILE") ) {
901
                File wsfile = getWorkspaceFile();
902
                if( wsfile!=null ) {
903 47606 fdiaz
                    trans.commit();
904 45165 jjdelcerro
                    return true;
905
                }
906
            }
907 44397 jjdelcerro
            if (this.existsConfiguration()) {
908 44750 jjdelcerro
              if( StringUtils.equalsIgnoreCase(name, CONFIG_NAME_BASEFOLDER) ) {
909 47606 fdiaz
                trans.commit();
910 44750 jjdelcerro
                return true;
911
              }
912
              if( this.cachedConfigValues.containsKey(name) ) {
913 47606 fdiaz
                trans.commit();
914 44750 jjdelcerro
                return true;
915
              }
916 45100 jjdelcerro
              store = this.getTable(TABLE_CONFIGURATION);
917 47606 fdiaz
              trans.add(store);
918 44750 jjdelcerro
              if (store != null) {
919
                  ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
920
                  String filter = builder.eq(
921
                          builder.column(FIELD_CONFIGURATION_NAME),
922
                          builder.constant(name)
923
                  ).toString();
924
                  Feature feature = store.findFirst(filter);
925
                  if (feature != null) {
926
                      String value = feature.getString(FIELD_CONFIGURATION_VALUE);
927
                      value = ExpressionUtils.evaluateDynamicText(this, value);
928
                      this.cachedConfigValues.put(name, new CachedConfigValue(name, value, 15000));
929 47606 fdiaz
                      trans.commit();
930 44750 jjdelcerro
                      return true;
931
                  }
932 44397 jjdelcerro
                }
933
            }
934 47606 fdiaz
            boolean b = super.exists(name);
935
            trans.commit();
936
            return b;
937 44397 jjdelcerro
        } catch (Exception ex) {
938
            LOGGER.warn("Can't read configuration value '" + name + "'", ex);
939 47606 fdiaz
            trans.abortQuietly();
940 44397 jjdelcerro
            return false;
941 45100 jjdelcerro
        } finally {
942 47606 fdiaz
            trans.closeQuietly();
943 45100 jjdelcerro
            DisposeUtils.disposeQuietly(store);
944 44397 jjdelcerro
        }
945
    }
946
947
    @Override
948
    public Object value(String name) {
949 45165 jjdelcerro
        if( StringUtils.equalsIgnoreCase(name, "WORKSPACEFILE") ) {
950
            File wsfile = getWorkspaceFile();
951
            if( wsfile!=null ) {
952
                return wsfile.toString();
953
            }
954
        }
955 44397 jjdelcerro
        String v = this.get(name);
956
        if( v!=null ) {
957
            return v;
958
        }
959
        return super.value(name);
960
    }
961
962
    @Override
963
    public Collection<String> localvariables() {
964 45100 jjdelcerro
        FeatureStore store = null;
965 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
966 44397 jjdelcerro
        try {
967 47606 fdiaz
            trans.begin();
968
            trans.add(this);
969 44397 jjdelcerro
            final List<String> theVars = new ArrayList<>();
970
            if (this.existsConfiguration()) {
971 45100 jjdelcerro
                store = this.getTable(TABLE_CONFIGURATION);
972 44397 jjdelcerro
                if (store != null) {
973 44728 jjdelcerro
                    store.accept((Object obj) -> {
974
                      Feature feature = (Feature) obj;
975
                      theVars.add(feature.getString(FIELD_CONFIGURATION_NAME));
976 44397 jjdelcerro
                    });
977
                }
978
            }
979 47606 fdiaz
            trans.commit();
980 44397 jjdelcerro
            return theVars;
981
        } catch (Exception ex) {
982
            LOGGER.warn("Can't read configuration variables", ex);
983 47606 fdiaz
            trans.abortQuietly();
984 44397 jjdelcerro
            return Collections.EMPTY_LIST;
985 45100 jjdelcerro
        } finally {
986 47606 fdiaz
            trans.closeQuietly();
987 45100 jjdelcerro
            DisposeUtils.disposeQuietly(store);
988 44397 jjdelcerro
        }
989
    }
990
991 44419 jjdelcerro
    @Override
992
    public ResourcesStorage getResourcesStorage() {
993 45100 jjdelcerro
        DataServerExplorer server = null;
994 47606 fdiaz
        LocalTransaction trans = new LocalTransaction(null, this.getTransaction());
995 44419 jjdelcerro
        try {
996 47606 fdiaz
            trans.begin();
997
            trans.add(this);
998 45100 jjdelcerro
            server = this.getServerExplorer();
999 47606 fdiaz
            trans.add(server);
1000 44419 jjdelcerro
            ResourcesStorage resourcesStorage = server.getResourcesStorage();
1001 47606 fdiaz
            trans.commit();
1002 44419 jjdelcerro
            return resourcesStorage;
1003
        } catch (Exception ex) {
1004 47606 fdiaz
            trans.abortQuietly();
1005 44419 jjdelcerro
            LOGGER.warn("Can't get resorces storage.",ex);
1006
            return null;
1007 45100 jjdelcerro
        } finally {
1008 47606 fdiaz
            trans.closeQuietly();
1009 45100 jjdelcerro
            DisposeUtils.disposeQuietly(server);
1010 44419 jjdelcerro
        }
1011
    }
1012 44728 jjdelcerro
1013
    public void fix() {
1014
      if( !(this.serverParameters instanceof HasAFile) ) {
1015
        return;
1016
      }
1017
      HasAFile f = (HasAFile) this.serverParameters;
1018
      if( f.getFile()==null ) {
1019
        return;
1020
      }
1021
      if( !this.isValid() ) {
1022
        return;
1023
      }
1024
      this.set(CONFIG_NAME_BASEFOLDER, f.getFile().getParent());
1025
    }
1026 44419 jjdelcerro
1027 44728 jjdelcerro
    @Override
1028
    public void connect() {
1029
      DataManager manager = DALLocator.getDataManager();
1030
      this.fix();
1031
      manager.addDatabaseWorkspace(this);
1032
    }
1033 44397 jjdelcerro
1034 44821 jjdelcerro
    @Override
1035
    public void disconnect() {
1036
      DataManager manager = DALLocator.getDataManager();
1037
      manager.removeDatabaseWorkspace(this);
1038
    }
1039 46338 fdiaz
1040
    @Override
1041
    public boolean isConnected() {
1042
      DataManager manager = DALLocator.getDataManager();
1043
      DatabaseWorkspaceManager ws = manager.getDatabaseWorkspace(this.getId());
1044
      if(ws == null){
1045
          return false;
1046
      }
1047
      if(ws.getServerExplorerParameters().isTheSameServerExplorer(this.getServerExplorerParameters())){
1048
          return true;
1049
      }
1050
      return false;
1051
    }
1052
1053
    @Override
1054
    public String getLabelOrId() {
1055
        String label = this.getLabel();
1056
        if(StringUtils.isBlank(label)){
1057
            label = this.getId();
1058
        }
1059
        return label;
1060
    }
1061
1062 47606 fdiaz
    @Override
1063
    public void setTransaction(DataTransaction transaction) {
1064 47611 fdiaz
        this.transactionsHelper.setTransaction(transaction);
1065 47606 fdiaz
    }
1066
1067
    @Override
1068
    public DataTransaction getTransaction() {
1069 47611 fdiaz
        return this.transactionsHelper.getTransaction();
1070 47606 fdiaz
    }
1071
1072
1073
1074 44297 jjdelcerro
}