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 @ 44750
History | View | Annotate | Download (25.4 KB)
1 |
package org.gvsig.fmap.dal.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Collection; |
6 |
import java.util.Collections; |
7 |
import java.util.List; |
8 |
import java.util.Map; |
9 |
import java.util.Objects; |
10 |
import org.apache.commons.collections4.map.LRUMap; |
11 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.apache.commons.lang3.mutable.Mutable; |
14 |
import org.apache.commons.lang3.mutable.MutableObject; |
15 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
16 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
17 |
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable; |
18 |
import org.gvsig.fmap.dal.DALLocator; |
19 |
import org.gvsig.fmap.dal.DataManager; |
20 |
import org.gvsig.fmap.dal.DataServerExplorer; |
21 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
22 |
import org.gvsig.fmap.dal.DataStoreParameters; |
23 |
import org.gvsig.fmap.dal.DataTypes; |
24 |
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 |
import org.gvsig.fmap.dal.StoresRepository; |
29 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
30 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
31 |
import org.gvsig.fmap.dal.feature.Feature; |
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
33 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_NAME; |
34 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_REPOSITORY_PARAMETERS; |
35 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_RESOURCES_NAME; |
36 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_RESOURCES_RESOURCE; |
37 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION; |
38 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_CONFIGURATION_NAME; |
39 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_REPOSITORY; |
40 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_REPOSITORY_NAME; |
41 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES; |
42 |
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME; |
43 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
44 |
import org.gvsig.tools.dynobject.DynClass; |
45 |
import org.gvsig.tools.dynobject.DynField; |
46 |
import org.gvsig.tools.resourcesstorage.FilesResourcesStorage; |
47 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
48 |
import org.gvsig.tools.util.CachedValue; |
49 |
import org.gvsig.tools.util.FileTools; |
50 |
import org.gvsig.tools.util.HasAFile; |
51 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
/**
|
55 |
*
|
56 |
* @author jjdelcerro
|
57 |
*/
|
58 |
@SuppressWarnings("UseSpecificCatch") |
59 |
public class DefaultDatabaseWorkspaceManager |
60 |
extends AbstractSymbolTable
|
61 |
implements DatabaseWorkspaceManager
|
62 |
{ |
63 |
|
64 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultDatabaseWorkspaceManager.class); |
65 |
|
66 |
private static final String CONFIG_NAME_BASEFOLDER = "BASEFOLDER"; |
67 |
private static final String CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH = "ALTERNATIVE_RESOURCES_PATH"; |
68 |
|
69 |
private class CachedConfigValue extends CachedValue<String> { |
70 |
|
71 |
private final String name; |
72 |
|
73 |
public CachedConfigValue(String name, String value) { |
74 |
this.setValue(value);
|
75 |
this.name = name;
|
76 |
} |
77 |
|
78 |
public CachedConfigValue(String name, String value, long expireTime) { |
79 |
this.name = name;
|
80 |
this.setValue(value);
|
81 |
this.setExpireTime(expireTime);
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
protected void reload() { |
86 |
String s = DefaultDatabaseWorkspaceManager.this.getConfigValue(name);
|
87 |
if( StringUtils.isBlank(s) ) {
|
88 |
this.setValue(null); |
89 |
} else {
|
90 |
this.setValue(s);
|
91 |
} |
92 |
} |
93 |
} |
94 |
|
95 |
private final DataServerExplorerParameters serverParameters; |
96 |
private Boolean existsConfiguration = null; |
97 |
private StoresRepository storesRepository;
|
98 |
private Mutable<File> alternativeResourcesFolder = null; |
99 |
private CachedValue<File> baseFolder; |
100 |
private Map<String,CachedConfigValue> cachedConfigValues; |
101 |
|
102 |
public DefaultDatabaseWorkspaceManager(DataServerExplorerParameters serverParameters) {
|
103 |
this.serverParameters = serverParameters;
|
104 |
this.baseFolder = new CachedValue() { |
105 |
@Override
|
106 |
protected void reload() { |
107 |
String s = DefaultDatabaseWorkspaceManager.this.getConfigValue(CONFIG_NAME_BASEFOLDER);
|
108 |
if( StringUtils.isBlank(s) ) {
|
109 |
this.setValue(null); |
110 |
} else {
|
111 |
this.setValue(new File(s)); |
112 |
} |
113 |
} |
114 |
|
115 |
}; |
116 |
this.baseFolder.setExpireTime(20000); // 20seg |
117 |
this.cachedConfigValues = new LRUMap<>(20, 20); |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public String toString() { |
122 |
return this.getLabel(); |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public String getId() { |
127 |
String id = this.get(CONFIG_NAME_STORESREPOSITORYID); |
128 |
return id;
|
129 |
} |
130 |
|
131 |
@Override
|
132 |
public String getLabel() { |
133 |
String label = this.get(CONFIG_NAME_STORESREPOSITORYLABEL); |
134 |
return label;
|
135 |
} |
136 |
|
137 |
@Override
|
138 |
public DatabaseWorkspaceManager getValue() {
|
139 |
return this; |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public boolean existsTable(int tableid) { |
144 |
switch(tableid) {
|
145 |
case TABLE_RESOURCES:
|
146 |
return this.existsTable(TABLE_RESOURCES_NAME); |
147 |
case TABLE_REPOSITORY:
|
148 |
return this.existsTable(TABLE_REPOSITORY_NAME); |
149 |
case TABLE_CONFIGURATION:
|
150 |
return this.existsTable(TABLE_CONFIGURATION_NAME); |
151 |
default:
|
152 |
throw new IllegalArgumentException("Invalid table identitier "+tableid); |
153 |
} |
154 |
} |
155 |
|
156 |
@Override
|
157 |
public void createTable(int tableid) { |
158 |
switch(tableid) {
|
159 |
case TABLE_RESOURCES:
|
160 |
createTableResources(); |
161 |
break;
|
162 |
case TABLE_REPOSITORY:
|
163 |
createTableRepository(); |
164 |
break;
|
165 |
case TABLE_CONFIGURATION:
|
166 |
createTableConfiguration(); |
167 |
break;
|
168 |
default:
|
169 |
throw new IllegalArgumentException("Invalid table identitier "+tableid); |
170 |
} |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void dropTable(int tableid) { |
175 |
switch(tableid) {
|
176 |
case TABLE_RESOURCES:
|
177 |
this.dropTable(TABLE_RESOURCES_NAME);
|
178 |
break;
|
179 |
case TABLE_REPOSITORY:
|
180 |
this.dropTable(TABLE_REPOSITORY_NAME);
|
181 |
break;
|
182 |
case TABLE_CONFIGURATION:
|
183 |
this.dropTable(TABLE_CONFIGURATION_NAME);
|
184 |
break;
|
185 |
default:
|
186 |
throw new IllegalArgumentException("Invalid table identitier "+tableid); |
187 |
} |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public FeatureStore getTable(int tableid) { |
192 |
switch(tableid) {
|
193 |
case TABLE_RESOURCES:
|
194 |
return this.getTable(TABLE_RESOURCES_NAME); |
195 |
case TABLE_REPOSITORY:
|
196 |
return this.getTable(TABLE_REPOSITORY_NAME); |
197 |
case TABLE_CONFIGURATION:
|
198 |
return this.getTable(TABLE_CONFIGURATION_NAME); |
199 |
default:
|
200 |
throw new IllegalArgumentException("Invalid table identitier "+tableid); |
201 |
} |
202 |
} |
203 |
|
204 |
@Override
|
205 |
public DataServerExplorer getServerExplorer() {
|
206 |
try {
|
207 |
DataManager dataManager = DALLocator.getDataManager(); |
208 |
DataServerExplorer server = dataManager.openServerExplorer( |
209 |
this.serverParameters.getProviderName(),
|
210 |
this.serverParameters
|
211 |
); |
212 |
return server;
|
213 |
} catch (Exception ex) { |
214 |
throw new RuntimeException("Can't get server explorer for workspace '"+Objects.toString(this.serverParameters)+"'", ex); |
215 |
} |
216 |
} |
217 |
|
218 |
private boolean existsTable(String tableName) { |
219 |
try {
|
220 |
DataServerExplorer server = this.getServerExplorer();
|
221 |
for (DataStoreParameters params : server.list()) {
|
222 |
String theTableName = (String) params.getDynValue("Table"); |
223 |
if( StringUtils.equals(theTableName, tableName) ) {
|
224 |
return true; |
225 |
} |
226 |
} |
227 |
} catch (Exception ex) { |
228 |
LOGGER.warn("Can't check if the table '"+tableName+"' exists.",ex); |
229 |
} |
230 |
return false; |
231 |
} |
232 |
|
233 |
private FeatureStore getTable(String tableName) { |
234 |
try {
|
235 |
DataManager dataManager = DALLocator.getDataManager(); |
236 |
DataServerExplorer server = this.getServerExplorer();
|
237 |
DataStoreParameters params = server.get(tableName); |
238 |
if( params!=null ) { |
239 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
240 |
params.getProviderName(), |
241 |
params |
242 |
); |
243 |
return store;
|
244 |
} |
245 |
} catch (Exception ex) { |
246 |
LOGGER.warn("Can't open table '"+tableName+"'.",ex); |
247 |
} |
248 |
return null; |
249 |
} |
250 |
|
251 |
private void dropTable(String tableName) { |
252 |
try {
|
253 |
DataServerExplorer server = this.getServerExplorer();
|
254 |
DataStoreParameters params = server.get(tableName); |
255 |
if( params!=null ) { |
256 |
server.remove(params); |
257 |
} |
258 |
} catch (Exception ex) { |
259 |
LOGGER.warn("Can't drop table '"+tableName+"'.",ex); |
260 |
} |
261 |
} |
262 |
|
263 |
private void createTableResources() { |
264 |
String tableName = TABLE_RESOURCES_NAME;
|
265 |
try {
|
266 |
DataServerExplorer server = this.getServerExplorer();
|
267 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName); |
268 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
269 |
ft.add(FIELD_RESOURCES_NAME, DataTypes.STRING, 150)
|
270 |
.setAllowNull(false)
|
271 |
.setIsPrimaryKey(true);
|
272 |
ft.add(FIELD_RESOURCES_RESOURCE, DataTypes.BYTEARRAY) |
273 |
.setAllowNull(true);
|
274 |
server.add(tableName, params, false);
|
275 |
} catch (Exception ex) { |
276 |
LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
277 |
} |
278 |
} |
279 |
|
280 |
private void createTableRepository() { |
281 |
String tableName = TABLE_REPOSITORY_NAME;
|
282 |
try {
|
283 |
DataServerExplorer server = this.getServerExplorer();
|
284 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName); |
285 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
286 |
ft.add(FIELD_REPOSITORY_NAME, DataTypes.STRING, 150)
|
287 |
.setAllowNull(false)
|
288 |
.setIsPrimaryKey(true);
|
289 |
ft.add(FIELD_REPOSITORY_PARAMETERS, DataTypes.BYTEARRAY) |
290 |
.setAllowNull(true);
|
291 |
server.add(tableName, params, false);
|
292 |
} catch (Exception ex) { |
293 |
LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
294 |
} |
295 |
} |
296 |
|
297 |
private void createTableConfiguration() { |
298 |
String tableName = TABLE_CONFIGURATION_NAME;
|
299 |
try {
|
300 |
DataServerExplorer server = this.getServerExplorer();
|
301 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName); |
302 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
303 |
ft.add(FIELD_CONFIGURATION_NAME, DataTypes.STRING, 200)
|
304 |
.setAllowNull(false)
|
305 |
.setIsPrimaryKey(true);
|
306 |
ft.add(FIELD_CONFIGURATION_VALUE, DataTypes.STRING, 200)
|
307 |
.setAllowNull(true);
|
308 |
server.add(tableName, params, false);
|
309 |
} catch (Exception ex) { |
310 |
LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
311 |
} |
312 |
} |
313 |
|
314 |
private boolean existsConfiguration() { |
315 |
if( this.existsConfiguration==null ) { |
316 |
this.existsConfiguration = this.existsTable(TABLE_CONFIGURATION_NAME); |
317 |
} |
318 |
return this.existsConfiguration; |
319 |
} |
320 |
|
321 |
@Override
|
322 |
public String get(String name) { |
323 |
try {
|
324 |
if( !this.existsConfiguration() ) { |
325 |
return null; |
326 |
} |
327 |
if( StringUtils.equalsIgnoreCase(name, CONFIG_NAME_BASEFOLDER) ) {
|
328 |
return this.baseFolder.get().toString(); |
329 |
} |
330 |
CachedConfigValue cachedValue = this.cachedConfigValues.get(name);
|
331 |
if( cachedValue != null ) { |
332 |
return cachedValue.get();
|
333 |
} |
334 |
String value = this.getConfigValue(name); |
335 |
this.cachedConfigValues.put(name, new CachedConfigValue(name, value, 15000)); |
336 |
return value;
|
337 |
} catch (Exception ex) { |
338 |
LOGGER.warn("Can't read configuration value '"+name+"'", ex); |
339 |
return null; |
340 |
} |
341 |
} |
342 |
|
343 |
private String getConfigValue(String name) { |
344 |
try {
|
345 |
if( !this.existsConfiguration() ) { |
346 |
return null; |
347 |
} |
348 |
FeatureStore store = this.getTable(TABLE_CONFIGURATION);
|
349 |
if( store == null ) { |
350 |
return null; |
351 |
} |
352 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
353 |
String filter = builder.eq(
|
354 |
builder.column(FIELD_CONFIGURATION_NAME), |
355 |
builder.constant(name) |
356 |
).toString(); |
357 |
Feature feature = store.findFirst(filter); |
358 |
if( feature == null ) { |
359 |
return null; |
360 |
} |
361 |
String value = feature.getString(FIELD_CONFIGURATION_VALUE);
|
362 |
value = ExpressionUtils.evaluateDynamicText(this, value);
|
363 |
return value;
|
364 |
|
365 |
} catch (Exception ex) { |
366 |
LOGGER.warn("Can't read configuration value '"+name+"'", ex); |
367 |
return null; |
368 |
} |
369 |
} |
370 |
|
371 |
@Override
|
372 |
public boolean set(String name, String value) { |
373 |
try {
|
374 |
if( !this.existsConfiguration() ) { |
375 |
return false; |
376 |
} |
377 |
FeatureStore store = this.getTable(TABLE_CONFIGURATION);
|
378 |
if( store == null ) { |
379 |
return false; |
380 |
} |
381 |
store.edit(); |
382 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
383 |
String filter = builder.eq(
|
384 |
builder.column(FIELD_CONFIGURATION_NAME), |
385 |
builder.constant(name) |
386 |
).toString(); |
387 |
Feature feature = store.findFirst(filter); |
388 |
EditableFeature efeature; |
389 |
if (feature == null) { |
390 |
efeature = store.createNewFeature(); |
391 |
efeature.set(FIELD_CONFIGURATION_NAME, name); |
392 |
efeature.set(FIELD_CONFIGURATION_VALUE, value); |
393 |
store.insert(efeature); |
394 |
} else {
|
395 |
efeature = feature.getEditable(); |
396 |
efeature.set(FIELD_CONFIGURATION_VALUE, value); |
397 |
store.update(efeature); |
398 |
} |
399 |
store.finishEditing(); |
400 |
if( this.cachedConfigValues.containsKey(name) ) { |
401 |
this.cachedConfigValues.get(name).set(value);
|
402 |
} |
403 |
return true; |
404 |
} catch (Exception ex) { |
405 |
LOGGER.warn("Can't write configuration value '"+name+"'", ex); |
406 |
return false; |
407 |
} |
408 |
} |
409 |
|
410 |
@Override
|
411 |
public StoresRepository getStoresRepository() {
|
412 |
if( this.storesRepository==null ) { |
413 |
String id = this.get(CONFIG_NAME_STORESREPOSITORYID); |
414 |
String label = this.get(CONFIG_NAME_STORESREPOSITORYLABEL); |
415 |
this.storesRepository = new DatabaseWorkspaceStoresRepository( |
416 |
id, |
417 |
label, |
418 |
this
|
419 |
); |
420 |
} |
421 |
return this.storesRepository; |
422 |
} |
423 |
|
424 |
public boolean contains(DataStoreParameters parameters) { |
425 |
boolean c = this.getStoresRepository().contains(parameters); |
426 |
return c;
|
427 |
} |
428 |
|
429 |
@Override
|
430 |
public boolean canAnonymousUserWriteInTheTables() { |
431 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
432 |
} |
433 |
|
434 |
@Override
|
435 |
public File getBaseFolder() { |
436 |
return this.baseFolder.get(); |
437 |
} |
438 |
|
439 |
@Override
|
440 |
public void setBaseFolder(File baseFolder) { |
441 |
this.baseFolder.set(baseFolder);
|
442 |
this.set(CONFIG_NAME_BASEFOLDER, baseFolder.toString());
|
443 |
} |
444 |
|
445 |
private DataStoreParameters replaceInFilesToUseBaseFolder(DataStoreParameters parameters) {
|
446 |
DynClass dynClass = parameters.getDynClass(); |
447 |
if( dynClass == null ) { |
448 |
return parameters;
|
449 |
} |
450 |
File theBaseFolder = this.getBaseFolder(); |
451 |
if( theBaseFolder==null ) { |
452 |
return parameters;
|
453 |
} |
454 |
DataStoreParameters replaced = parameters; |
455 |
for (DynField dynField : dynClass.getDynFields()) {
|
456 |
switch(dynField.getType()) {
|
457 |
case DataTypes.FILE:
|
458 |
case DataTypes.FOLDER:
|
459 |
File f = (File) parameters.getDynValue(dynField.getName()); |
460 |
if( f!=null ) { |
461 |
File f2 = FileTools.relativizeFile(theBaseFolder, f);
|
462 |
if( f2!=f ) {
|
463 |
f = new File("<%="+CONFIG_NAME_BASEFOLDER+"%>/"+f2.getPath()); |
464 |
if( replaced == parameters ) {
|
465 |
replaced = (DataStoreParameters) replaced.getCopy(); |
466 |
} |
467 |
replaced.setDynValue(dynField.getName(), f); |
468 |
} |
469 |
} |
470 |
break;
|
471 |
} |
472 |
} |
473 |
return replaced;
|
474 |
} |
475 |
|
476 |
@Override
|
477 |
public boolean writeStoresRepositoryEntry(String name, DataStoreParameters parameters) { |
478 |
try {
|
479 |
parameters = this.replaceInFilesToUseBaseFolder(parameters);
|
480 |
byte[] data = parameters.toByteArray(); |
481 |
if( data == null ) { |
482 |
throw new RuntimeException("Can't convert parameters to byte array."); |
483 |
} |
484 |
FeatureStore store = this.getTable(TABLE_REPOSITORY);
|
485 |
store.edit(); |
486 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
487 |
String filter = builder.eq(
|
488 |
builder.column(FIELD_REPOSITORY_NAME), |
489 |
builder.constant(name) |
490 |
).toString(); |
491 |
Feature feature = store.findFirst(filter); |
492 |
EditableFeature efeature; |
493 |
if (feature == null) { |
494 |
efeature = store.createNewFeature(); |
495 |
efeature.set(FIELD_REPOSITORY_NAME, name); |
496 |
efeature.set(FIELD_REPOSITORY_PARAMETERS, data); |
497 |
store.insert(efeature); |
498 |
} else {
|
499 |
efeature = feature.getEditable(); |
500 |
efeature.set(FIELD_REPOSITORY_PARAMETERS, data); |
501 |
store.update(efeature); |
502 |
} |
503 |
store.finishEditing(); |
504 |
return true; |
505 |
|
506 |
} catch (Exception ex) { |
507 |
LOGGER.warn("Can't save entry '"+name+"' in repository information", ex); |
508 |
return false; |
509 |
} |
510 |
} |
511 |
|
512 |
@Override
|
513 |
public boolean isValid() { |
514 |
try {
|
515 |
String id = this.get(CONFIG_NAME_STORESREPOSITORYID); |
516 |
if( StringUtils.isBlank(id) ) {
|
517 |
return false; |
518 |
} |
519 |
return true; |
520 |
} catch(Exception ex) { |
521 |
return false; |
522 |
} |
523 |
} |
524 |
|
525 |
@Override
|
526 |
public boolean isValidStoresRepository() { |
527 |
if( !this.isValid() ) { |
528 |
return false; |
529 |
} |
530 |
if( !this.existsTable(TABLE_REPOSITORY_NAME) ) { |
531 |
return false; |
532 |
} |
533 |
return true; |
534 |
|
535 |
} |
536 |
|
537 |
@Override
|
538 |
public ResourcesStorage getAlternativeResourcesStorage(String tableName) { |
539 |
if( this.alternativeResourcesFolder==null ) { |
540 |
this.alternativeResourcesFolder = new MutableObject<>(null); |
541 |
String resourcesPath = this.get(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH); |
542 |
if( !StringUtils.isBlank(resourcesPath) ) {
|
543 |
try {
|
544 |
resourcesPath = (String) ExpressionUtils.evaluate(resourcesPath);
|
545 |
} catch(Throwable th) { |
546 |
// Do nothing
|
547 |
} |
548 |
File f = new File(resourcesPath); |
549 |
if( f.exists() ) {
|
550 |
this.alternativeResourcesFolder.setValue(f);
|
551 |
} |
552 |
} |
553 |
} |
554 |
File folder = this.alternativeResourcesFolder.getValue(); |
555 |
if( folder==null ) { |
556 |
return null; |
557 |
} |
558 |
String resourcesRoot = folder.getAbsolutePath().replace("\\","/") + "/" + tableName; |
559 |
ResourcesStorage resources = new FilesResourcesStorage(resourcesRoot);
|
560 |
return resources;
|
561 |
} |
562 |
|
563 |
@Override
|
564 |
public void drop() { |
565 |
if( !this.existsTable(TABLE_RESOURCES) ) { |
566 |
this.dropTable(TABLE_RESOURCES);
|
567 |
} |
568 |
if( !this.existsTable(TABLE_CONFIGURATION) ) { |
569 |
this.dropTable(TABLE_CONFIGURATION);
|
570 |
} |
571 |
if( !this.existsTable(TABLE_REPOSITORY) ) { |
572 |
this.dropTable(TABLE_REPOSITORY);
|
573 |
} |
574 |
} |
575 |
|
576 |
|
577 |
@Override
|
578 |
public void create(String id, String description) { |
579 |
if( !this.existsTable(TABLE_RESOURCES) ) { |
580 |
this.createTable(TABLE_RESOURCES);
|
581 |
} |
582 |
if( !this.existsTable(TABLE_CONFIGURATION) ) { |
583 |
this.createTable(TABLE_CONFIGURATION);
|
584 |
} |
585 |
if( !this.existsTable(TABLE_REPOSITORY) ) { |
586 |
this.createTable(TABLE_REPOSITORY);
|
587 |
} |
588 |
this.set(CONFIG_NAME_STORESREPOSITORYID,id);
|
589 |
this.set(CONFIG_NAME_STORESREPOSITORYLABEL,description);
|
590 |
this.set(CONFIG_CAN_ANONYMOUS_USER_WRITE_IN_THE_TABLES,"true"); |
591 |
this.set(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH,""); |
592 |
File theBaseFolder = this.getBaseFolder(); |
593 |
if( this.serverParameters instanceof HasAFile && theBaseFolder==null ) { |
594 |
File f = ((HasAFile)this.serverParameters).getFile(); |
595 |
if( f != null ) { |
596 |
theBaseFolder = f.getParentFile(); |
597 |
this.setBaseFolder(theBaseFolder);
|
598 |
} else {
|
599 |
this.set(CONFIG_NAME_BASEFOLDER,""); |
600 |
} |
601 |
} else {
|
602 |
this.set(CONFIG_NAME_BASEFOLDER,""); |
603 |
} |
604 |
} |
605 |
|
606 |
@Override
|
607 |
public boolean exists(String name) { |
608 |
try {
|
609 |
if (this.existsConfiguration()) { |
610 |
if( StringUtils.equalsIgnoreCase(name, CONFIG_NAME_BASEFOLDER) ) {
|
611 |
return true; |
612 |
} |
613 |
if( this.cachedConfigValues.containsKey(name) ) { |
614 |
return true; |
615 |
} |
616 |
FeatureStore store = this.getTable(TABLE_CONFIGURATION);
|
617 |
if (store != null) { |
618 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
619 |
String filter = builder.eq(
|
620 |
builder.column(FIELD_CONFIGURATION_NAME), |
621 |
builder.constant(name) |
622 |
).toString(); |
623 |
Feature feature = store.findFirst(filter); |
624 |
if (feature != null) { |
625 |
String value = feature.getString(FIELD_CONFIGURATION_VALUE);
|
626 |
value = ExpressionUtils.evaluateDynamicText(this, value);
|
627 |
this.cachedConfigValues.put(name, new CachedConfigValue(name, value, 15000)); |
628 |
return true; |
629 |
} |
630 |
} |
631 |
} |
632 |
return super.exists(name); |
633 |
} catch (Exception ex) { |
634 |
LOGGER.warn("Can't read configuration value '" + name + "'", ex); |
635 |
return false; |
636 |
} |
637 |
} |
638 |
|
639 |
@Override
|
640 |
public Object value(String name) { |
641 |
String v = this.get(name); |
642 |
if( v!=null ) { |
643 |
return v;
|
644 |
} |
645 |
return super.value(name); |
646 |
} |
647 |
|
648 |
@Override
|
649 |
public Collection<String> localvariables() { |
650 |
try {
|
651 |
final List<String> theVars = new ArrayList<>(); |
652 |
if (this.existsConfiguration()) { |
653 |
FeatureStore store = this.getTable(TABLE_CONFIGURATION);
|
654 |
if (store != null) { |
655 |
store.accept((Object obj) -> {
|
656 |
Feature feature = (Feature) obj; |
657 |
theVars.add(feature.getString(FIELD_CONFIGURATION_NAME)); |
658 |
}); |
659 |
} |
660 |
} |
661 |
return theVars;
|
662 |
} catch (Exception ex) { |
663 |
LOGGER.warn("Can't read configuration variables", ex);
|
664 |
return Collections.EMPTY_LIST; |
665 |
} |
666 |
} |
667 |
|
668 |
@Override
|
669 |
public ResourcesStorage getResourcesStorage() {
|
670 |
try {
|
671 |
DataManager dataManager = DALLocator.getDataManager(); |
672 |
DataServerExplorer server = dataManager.openServerExplorer( |
673 |
this.serverParameters.getProviderName(),
|
674 |
this.serverParameters
|
675 |
); |
676 |
ResourcesStorage resourcesStorage = server.getResourcesStorage(); |
677 |
return resourcesStorage;
|
678 |
} catch (Exception ex) { |
679 |
LOGGER.warn("Can't get resorces storage.",ex);
|
680 |
return null; |
681 |
} |
682 |
} |
683 |
|
684 |
public void fix() { |
685 |
if( !(this.serverParameters instanceof HasAFile) ) { |
686 |
return;
|
687 |
} |
688 |
HasAFile f = (HasAFile) this.serverParameters;
|
689 |
if( f.getFile()==null ) { |
690 |
return;
|
691 |
} |
692 |
if( !this.isValid() ) { |
693 |
return;
|
694 |
} |
695 |
this.set(CONFIG_NAME_BASEFOLDER, f.getFile().getParent());
|
696 |
} |
697 |
|
698 |
@Override
|
699 |
public void connect() { |
700 |
DataManager manager = DALLocator.getDataManager(); |
701 |
this.fix();
|
702 |
manager.addDatabaseWorkspace(this);
|
703 |
} |
704 |
|
705 |
} |