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

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