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 @ 45426
History | View | Annotate | Download (29.1 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 |
private void createTableResources() { |
279 |
String tableName = TABLE_RESOURCES_NAME;
|
280 |
DataServerExplorer server = null;
|
281 |
try {
|
282 |
server = this.getServerExplorer();
|
283 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName); |
284 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
285 |
ft.add(FIELD_RESOURCES_NAME, DataTypes.STRING, 150)
|
286 |
.setAllowNull(false)
|
287 |
.setIsPrimaryKey(true);
|
288 |
ft.add(FIELD_RESOURCES_RESOURCE, DataTypes.BYTEARRAY) |
289 |
.setAllowNull(true);
|
290 |
server.add(tableName, params, false);
|
291 |
} catch (Exception ex) { |
292 |
LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
293 |
} finally {
|
294 |
DisposeUtils.disposeQuietly(server); |
295 |
} |
296 |
} |
297 |
|
298 |
private void createTableRepository() { |
299 |
String tableName = TABLE_REPOSITORY_NAME;
|
300 |
DataServerExplorer server = null;
|
301 |
try {
|
302 |
server = this.getServerExplorer();
|
303 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName); |
304 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
305 |
ft.add(FIELD_REPOSITORY_NAME, DataTypes.STRING, 150)
|
306 |
.setAllowNull(false)
|
307 |
.setIsPrimaryKey(true);
|
308 |
ft.add(FIELD_REPOSITORY_PARAMETERS, DataTypes.BYTEARRAY) |
309 |
.setAllowNull(true);
|
310 |
ft.add(FIELD_REPOSITORY_FLAGS, DataTypes.INT) |
311 |
.setAllowNull(false)
|
312 |
.setDefaultValue(0);
|
313 |
server.add(tableName, params, false);
|
314 |
} catch (Exception ex) { |
315 |
LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
316 |
} finally {
|
317 |
DisposeUtils.disposeQuietly(server); |
318 |
} |
319 |
} |
320 |
|
321 |
private void createTableConfiguration() { |
322 |
String tableName = TABLE_CONFIGURATION_NAME;
|
323 |
DataServerExplorer server = null;
|
324 |
try {
|
325 |
server = this.getServerExplorer();
|
326 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) server.getAddParameters(tableName); |
327 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
328 |
ft.add(FIELD_CONFIGURATION_NAME, DataTypes.STRING, 200)
|
329 |
.setAllowNull(false)
|
330 |
.setIsPrimaryKey(true);
|
331 |
ft.add(FIELD_CONFIGURATION_VALUE, DataTypes.STRING, 200)
|
332 |
.setAllowNull(true);
|
333 |
server.add(tableName, params, false);
|
334 |
this.existsConfiguration = null; |
335 |
} catch (Exception ex) { |
336 |
LOGGER.warn("Can't create table '"+tableName+"'.",ex); |
337 |
} finally {
|
338 |
DisposeUtils.disposeQuietly(server); |
339 |
} |
340 |
} |
341 |
|
342 |
private boolean existsConfiguration() { |
343 |
if( this.existsConfiguration==null ) { |
344 |
this.existsConfiguration = this.existsTable(TABLE_CONFIGURATION_NAME); |
345 |
} |
346 |
return this.existsConfiguration; |
347 |
} |
348 |
|
349 |
@Override
|
350 |
public String get(String name) { |
351 |
try {
|
352 |
if( !this.existsConfiguration() ) { |
353 |
return null; |
354 |
} |
355 |
if( StringUtils.equalsIgnoreCase(name, CONFIG_NAME_BASEFOLDER) ) {
|
356 |
return this.baseFolder.get().toString(); |
357 |
} |
358 |
CachedConfigValue cachedValue = this.cachedConfigValues.get(name);
|
359 |
if( cachedValue != null ) { |
360 |
return cachedValue.get();
|
361 |
} |
362 |
String value = this.getConfigValue(name); |
363 |
this.cachedConfigValues.put(name, new CachedConfigValue(name, value, 15000)); |
364 |
return value;
|
365 |
} catch (Exception ex) { |
366 |
LOGGER.warn("Can't read configuration value '"+name+"'", ex); |
367 |
return null; |
368 |
} |
369 |
} |
370 |
|
371 |
private String getConfigValue(String name) { |
372 |
FeatureStore store = null;
|
373 |
try {
|
374 |
if( !this.existsConfiguration() ) { |
375 |
return null; |
376 |
} |
377 |
store = this.getTable(TABLE_CONFIGURATION);
|
378 |
if( store == null ) { |
379 |
return null; |
380 |
} |
381 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
382 |
String filter = builder.eq(
|
383 |
builder.column(FIELD_CONFIGURATION_NAME), |
384 |
builder.constant(name) |
385 |
).toString(); |
386 |
Feature feature = store.findFirst(filter); |
387 |
if( feature == null ) { |
388 |
return null; |
389 |
} |
390 |
String value = feature.getString(FIELD_CONFIGURATION_VALUE);
|
391 |
value = ExpressionUtils.evaluateDynamicText(this, value);
|
392 |
return value;
|
393 |
|
394 |
} catch (Exception ex) { |
395 |
LOGGER.warn("Can't read configuration value '"+name+"'", ex); |
396 |
return null; |
397 |
} finally {
|
398 |
DisposeUtils.disposeQuietly(store); |
399 |
} |
400 |
} |
401 |
|
402 |
@Override
|
403 |
public boolean set(String name, String value) { |
404 |
FeatureStore store = null;
|
405 |
try {
|
406 |
if( !this.existsConfiguration() ) { |
407 |
return false; |
408 |
} |
409 |
store = this.getTable(TABLE_CONFIGURATION);
|
410 |
if( store == null ) { |
411 |
return false; |
412 |
} |
413 |
store.edit(); |
414 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
415 |
String filter = builder.eq(
|
416 |
builder.column(FIELD_CONFIGURATION_NAME), |
417 |
builder.constant(name) |
418 |
).toString(); |
419 |
Feature feature = store.findFirst(filter); |
420 |
EditableFeature efeature; |
421 |
if (feature == null) { |
422 |
efeature = store.createNewFeature(); |
423 |
efeature.set(FIELD_CONFIGURATION_NAME, name); |
424 |
efeature.set(FIELD_CONFIGURATION_VALUE, value); |
425 |
store.insert(efeature); |
426 |
} else {
|
427 |
efeature = feature.getEditable(); |
428 |
efeature.set(FIELD_CONFIGURATION_VALUE, value); |
429 |
store.update(efeature); |
430 |
} |
431 |
store.finishEditing(); |
432 |
if( this.cachedConfigValues.containsKey(name) ) { |
433 |
this.cachedConfigValues.get(name).set(value);
|
434 |
} |
435 |
return true; |
436 |
} catch (Exception ex) { |
437 |
LOGGER.warn("Can't write configuration value '"+name+"'", ex); |
438 |
return false; |
439 |
} finally {
|
440 |
DisposeUtils.disposeQuietly(store); |
441 |
} |
442 |
} |
443 |
|
444 |
@Override
|
445 |
public StoresRepository getStoresRepository() {
|
446 |
if( this.storesRepository==null ) { |
447 |
String id = this.get(CONFIG_NAME_STORESREPOSITORYID); |
448 |
String label = this.get(CONFIG_NAME_STORESREPOSITORYLABEL); |
449 |
this.storesRepository = new DatabaseWorkspaceStoresRepository( |
450 |
id, |
451 |
label, |
452 |
this
|
453 |
); |
454 |
} |
455 |
return this.storesRepository; |
456 |
} |
457 |
|
458 |
public boolean contains(DataStoreParameters parameters) { |
459 |
boolean c = this.getStoresRepository().contains(parameters); |
460 |
return c;
|
461 |
} |
462 |
|
463 |
@Override
|
464 |
public boolean canAnonymousUserWriteInTheTables() { |
465 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
466 |
} |
467 |
|
468 |
@Override
|
469 |
public File getBaseFolder() { |
470 |
return this.baseFolder.get(); |
471 |
} |
472 |
|
473 |
@Override
|
474 |
public void setBaseFolder(File baseFolder) { |
475 |
this.baseFolder.set(baseFolder);
|
476 |
this.set(CONFIG_NAME_BASEFOLDER, baseFolder.toString());
|
477 |
} |
478 |
|
479 |
private File getWorkspaceFile() { |
480 |
if( this.serverParameters instanceof HasAFile ) { |
481 |
return ((HasAFile)this.serverParameters).getFile(); |
482 |
} |
483 |
return null; |
484 |
} |
485 |
|
486 |
private DataStoreParameters replaceInFilesToUseBaseFolder(DataStoreParameters parameters) {
|
487 |
DynClass dynClass = parameters.getDynClass(); |
488 |
if( dynClass == null ) { |
489 |
return parameters;
|
490 |
} |
491 |
File theBaseFolder = this.getBaseFolder(); |
492 |
File wsfile = getWorkspaceFile();
|
493 |
if( theBaseFolder==null && wsfile==null ) { |
494 |
return parameters;
|
495 |
} |
496 |
DataStoreParameters replaced = parameters; |
497 |
for (DynField dynField : dynClass.getDynFields()) {
|
498 |
switch(dynField.getType()) {
|
499 |
case DataTypes.FILE:
|
500 |
case DataTypes.FOLDER:
|
501 |
File f = (File) parameters.getDynValue(dynField.getName()); |
502 |
File newf = null; |
503 |
if( f!=null ) { |
504 |
if( wsfile!=null ) { |
505 |
if( wsfile.equals(f) ) {
|
506 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
507 |
newf = ExpressionUtils.createDynamicFile( |
508 |
builder.ifnull( |
509 |
builder.variable("WORKSPACEFILE"),
|
510 |
builder.constant(f.toString()), |
511 |
builder.variable("WORKSPACEFILE")
|
512 |
) |
513 |
); |
514 |
} |
515 |
} |
516 |
if( newf==null ) { |
517 |
File frel = FileTools.relativizeFile(theBaseFolder, f);
|
518 |
if( frel!=f ) {
|
519 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
520 |
newf = ExpressionUtils.createDynamicFile( |
521 |
builder.concat( |
522 |
builder.variable(CONFIG_NAME_BASEFOLDER), |
523 |
builder.constant("/"),
|
524 |
builder.variable(frel.getPath()) |
525 |
) |
526 |
); |
527 |
} |
528 |
} |
529 |
if( newf!=null ) { |
530 |
if( replaced == parameters ) {
|
531 |
replaced = (DataStoreParameters) replaced.getCopy(); |
532 |
} |
533 |
replaced.setDynValue(dynField.getName(), newf); |
534 |
} |
535 |
} |
536 |
break;
|
537 |
} |
538 |
} |
539 |
return replaced;
|
540 |
} |
541 |
|
542 |
@Override
|
543 |
public boolean writeStoresRepositoryEntry(String name, DataStoreParameters parameters) { |
544 |
FeatureStore store = null;
|
545 |
try {
|
546 |
parameters = this.replaceInFilesToUseBaseFolder(parameters);
|
547 |
byte[] data = parameters.toByteArray(); |
548 |
if( data == null ) { |
549 |
throw new RuntimeException("Can't convert parameters to byte array."); |
550 |
} |
551 |
store = this.getTable(TABLE_REPOSITORY);
|
552 |
store.edit(); |
553 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
554 |
String filter = builder.eq(
|
555 |
builder.column(FIELD_REPOSITORY_NAME), |
556 |
builder.constant(name) |
557 |
).toString(); |
558 |
Feature feature = store.findFirst(filter); |
559 |
EditableFeature efeature; |
560 |
if (feature == null) { |
561 |
efeature = store.createNewFeature(); |
562 |
efeature.set(FIELD_REPOSITORY_NAME, name); |
563 |
efeature.set(FIELD_REPOSITORY_PARAMETERS, data); |
564 |
// algun set sobre los flags?
|
565 |
efeature.set(FIELD_REPOSITORY_FLAGS, 0);
|
566 |
store.insert(efeature); |
567 |
} else {
|
568 |
efeature = feature.getEditable(); |
569 |
efeature.set(FIELD_REPOSITORY_PARAMETERS, data); |
570 |
store.update(efeature); |
571 |
} |
572 |
store.finishEditing(); |
573 |
return true; |
574 |
|
575 |
} catch (Exception ex) { |
576 |
LOGGER.warn("Can't save entry '"+name+"' in repository information", ex); |
577 |
return false; |
578 |
} finally {
|
579 |
DisposeUtils.disposeQuietly(store); |
580 |
} |
581 |
} |
582 |
|
583 |
@Override
|
584 |
public boolean isValid() { |
585 |
try {
|
586 |
String id = this.get(CONFIG_NAME_STORESREPOSITORYID); |
587 |
if( StringUtils.isBlank(id) ) {
|
588 |
return false; |
589 |
} |
590 |
return true; |
591 |
} catch(Exception ex) { |
592 |
return false; |
593 |
} |
594 |
} |
595 |
|
596 |
@Override
|
597 |
public boolean isValidStoresRepository() { |
598 |
if( !this.isValid() ) { |
599 |
return false; |
600 |
} |
601 |
if( !this.existsTable(TABLE_REPOSITORY_NAME) ) { |
602 |
return false; |
603 |
} |
604 |
return true; |
605 |
|
606 |
} |
607 |
|
608 |
@Override
|
609 |
public ResourcesStorage getAlternativeResourcesStorage(String tableName) { |
610 |
if( this.alternativeResourcesFolder==null ) { |
611 |
this.alternativeResourcesFolder = new MutableObject<>(null); |
612 |
String resourcesPath = this.get(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH); |
613 |
if( !StringUtils.isBlank(resourcesPath) ) {
|
614 |
try {
|
615 |
resourcesPath = (String) ExpressionUtils.evaluate(resourcesPath);
|
616 |
} catch(Throwable th) { |
617 |
// Do nothing
|
618 |
} |
619 |
File f = new File(resourcesPath); |
620 |
if( f.exists() ) {
|
621 |
this.alternativeResourcesFolder.setValue(f);
|
622 |
} |
623 |
} |
624 |
} |
625 |
File folder = this.alternativeResourcesFolder.getValue(); |
626 |
if( folder==null ) { |
627 |
return null; |
628 |
} |
629 |
String resourcesRoot = folder.getAbsolutePath().replace("\\","/") + "/" + tableName; |
630 |
ResourcesStorage resources = new FilesResourcesStorage(resourcesRoot);
|
631 |
return resources;
|
632 |
} |
633 |
|
634 |
@Override
|
635 |
public void drop() { |
636 |
if( !this.existsTable(TABLE_RESOURCES) ) { |
637 |
this.dropTable(TABLE_RESOURCES);
|
638 |
} |
639 |
if( !this.existsTable(TABLE_CONFIGURATION) ) { |
640 |
this.dropTable(TABLE_CONFIGURATION);
|
641 |
} |
642 |
if( !this.existsTable(TABLE_REPOSITORY) ) { |
643 |
this.dropTable(TABLE_REPOSITORY);
|
644 |
} |
645 |
} |
646 |
|
647 |
|
648 |
@Override
|
649 |
public void create(String id, String description) { |
650 |
if( !this.existsTable(TABLE_RESOURCES) ) { |
651 |
this.createTable(TABLE_RESOURCES);
|
652 |
} |
653 |
if( !this.existsTable(TABLE_CONFIGURATION) ) { |
654 |
this.createTable(TABLE_CONFIGURATION);
|
655 |
} |
656 |
if( !this.existsTable(TABLE_REPOSITORY) ) { |
657 |
this.createTable(TABLE_REPOSITORY);
|
658 |
} |
659 |
this.set(CONFIG_NAME_STORESREPOSITORYID,id);
|
660 |
this.set(CONFIG_NAME_STORESREPOSITORYLABEL,description);
|
661 |
this.set(CONFIG_CAN_ANONYMOUS_USER_WRITE_IN_THE_TABLES,"true"); |
662 |
this.set(CONFIG_NAME_ALTERNATIVE_RESOURCES_PATH,""); |
663 |
File theBaseFolder = this.getBaseFolder(); |
664 |
if( this.serverParameters instanceof HasAFile && theBaseFolder==null ) { |
665 |
File f = ((HasAFile)this.serverParameters).getFile(); |
666 |
if( f != null ) { |
667 |
theBaseFolder = f.getParentFile(); |
668 |
this.setBaseFolder(theBaseFolder);
|
669 |
} else {
|
670 |
this.set(CONFIG_NAME_BASEFOLDER,""); |
671 |
} |
672 |
} else {
|
673 |
this.set(CONFIG_NAME_BASEFOLDER,""); |
674 |
} |
675 |
} |
676 |
|
677 |
@Override
|
678 |
public boolean exists(String name) { |
679 |
FeatureStore store = null;
|
680 |
try {
|
681 |
if( StringUtils.equalsIgnoreCase(name, "WORKSPACEFILE") ) { |
682 |
File wsfile = getWorkspaceFile();
|
683 |
if( wsfile!=null ) { |
684 |
return true; |
685 |
} |
686 |
} |
687 |
if (this.existsConfiguration()) { |
688 |
if( StringUtils.equalsIgnoreCase(name, CONFIG_NAME_BASEFOLDER) ) {
|
689 |
return true; |
690 |
} |
691 |
if( this.cachedConfigValues.containsKey(name) ) { |
692 |
return true; |
693 |
} |
694 |
store = this.getTable(TABLE_CONFIGURATION);
|
695 |
if (store != null) { |
696 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
697 |
String filter = builder.eq(
|
698 |
builder.column(FIELD_CONFIGURATION_NAME), |
699 |
builder.constant(name) |
700 |
).toString(); |
701 |
Feature feature = store.findFirst(filter); |
702 |
if (feature != null) { |
703 |
String value = feature.getString(FIELD_CONFIGURATION_VALUE);
|
704 |
value = ExpressionUtils.evaluateDynamicText(this, value);
|
705 |
this.cachedConfigValues.put(name, new CachedConfigValue(name, value, 15000)); |
706 |
return true; |
707 |
} |
708 |
} |
709 |
} |
710 |
return super.exists(name); |
711 |
} catch (Exception ex) { |
712 |
LOGGER.warn("Can't read configuration value '" + name + "'", ex); |
713 |
return false; |
714 |
} finally {
|
715 |
DisposeUtils.disposeQuietly(store); |
716 |
} |
717 |
} |
718 |
|
719 |
@Override
|
720 |
public Object value(String name) { |
721 |
if( StringUtils.equalsIgnoreCase(name, "WORKSPACEFILE") ) { |
722 |
File wsfile = getWorkspaceFile();
|
723 |
if( wsfile!=null ) { |
724 |
return wsfile.toString();
|
725 |
} |
726 |
} |
727 |
String v = this.get(name); |
728 |
if( v!=null ) { |
729 |
return v;
|
730 |
} |
731 |
return super.value(name); |
732 |
} |
733 |
|
734 |
@Override
|
735 |
public Collection<String> localvariables() { |
736 |
FeatureStore store = null;
|
737 |
try {
|
738 |
final List<String> theVars = new ArrayList<>(); |
739 |
if (this.existsConfiguration()) { |
740 |
store = this.getTable(TABLE_CONFIGURATION);
|
741 |
if (store != null) { |
742 |
store.accept((Object obj) -> {
|
743 |
Feature feature = (Feature) obj; |
744 |
theVars.add(feature.getString(FIELD_CONFIGURATION_NAME)); |
745 |
}); |
746 |
} |
747 |
} |
748 |
return theVars;
|
749 |
} catch (Exception ex) { |
750 |
LOGGER.warn("Can't read configuration variables", ex);
|
751 |
return Collections.EMPTY_LIST; |
752 |
} finally {
|
753 |
DisposeUtils.disposeQuietly(store); |
754 |
} |
755 |
} |
756 |
|
757 |
@Override
|
758 |
public ResourcesStorage getResourcesStorage() {
|
759 |
DataServerExplorer server = null;
|
760 |
try {
|
761 |
server = this.getServerExplorer();
|
762 |
ResourcesStorage resourcesStorage = server.getResourcesStorage(); |
763 |
return resourcesStorage;
|
764 |
} catch (Exception ex) { |
765 |
LOGGER.warn("Can't get resorces storage.",ex);
|
766 |
return null; |
767 |
} finally {
|
768 |
DisposeUtils.disposeQuietly(server); |
769 |
} |
770 |
} |
771 |
|
772 |
public void fix() { |
773 |
if( !(this.serverParameters instanceof HasAFile) ) { |
774 |
return;
|
775 |
} |
776 |
HasAFile f = (HasAFile) this.serverParameters;
|
777 |
if( f.getFile()==null ) { |
778 |
return;
|
779 |
} |
780 |
if( !this.isValid() ) { |
781 |
return;
|
782 |
} |
783 |
this.set(CONFIG_NAME_BASEFOLDER, f.getFile().getParent());
|
784 |
} |
785 |
|
786 |
@Override
|
787 |
public void connect() { |
788 |
DataManager manager = DALLocator.getDataManager(); |
789 |
this.fix();
|
790 |
manager.addDatabaseWorkspace(this);
|
791 |
} |
792 |
|
793 |
@Override
|
794 |
public void disconnect() { |
795 |
DataManager manager = DALLocator.getDataManager(); |
796 |
manager.removeDatabaseWorkspace(this);
|
797 |
} |
798 |
} |