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