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