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 / feature / impl / DefaultForeingKey.java
History | View | Annotate | Download (26.2 KB)
1 |
package org.gvsig.fmap.dal.feature.impl; |
---|---|
2 |
|
3 |
import java.util.HashMap; |
4 |
import org.gvsig.fmap.dal.feature.EditableForeingKey; |
5 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
6 |
import java.util.List; |
7 |
import java.util.Map; |
8 |
import java.util.Objects; |
9 |
import java.util.logging.Level; |
10 |
import org.apache.commons.codec.digest.DigestUtils; |
11 |
import org.apache.commons.lang3.StringUtils; |
12 |
import org.gvsig.expressionevaluator.Expression; |
13 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
14 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
15 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
16 |
import org.gvsig.expressionevaluator.SymbolTable; |
17 |
import org.gvsig.fmap.dal.BaseStoresRepository; |
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.DataStore; |
22 |
import org.gvsig.fmap.dal.DataStoreParameters; |
23 |
import org.gvsig.fmap.dal.DataTransaction; |
24 |
import org.gvsig.fmap.dal.StoresRepository; |
25 |
import org.gvsig.fmap.dal.exception.DataException; |
26 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
27 |
import org.gvsig.fmap.dal.feature.DisposableFeatureSetIterable; |
28 |
import org.gvsig.fmap.dal.feature.Feature; |
29 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
30 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
31 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
32 |
import org.gvsig.fmap.dal.feature.FeatureType; |
33 |
import static org.gvsig.fmap.dal.feature.ForeingKey.MAX_AVAILABLE_VALUES; |
34 |
import org.gvsig.tools.ToolsLocator; |
35 |
import org.gvsig.tools.dispose.DisposeUtils; |
36 |
import org.gvsig.tools.dynobject.DynObject; |
37 |
import org.gvsig.tools.dynobject.DynObjectValueItem; |
38 |
import org.gvsig.tools.dynobject.DynStruct; |
39 |
import org.gvsig.tools.persistence.PersistenceManager; |
40 |
import org.gvsig.tools.persistence.Persistent; |
41 |
import org.gvsig.tools.persistence.PersistentState; |
42 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
|
46 |
/**
|
47 |
*
|
48 |
* @author jjdelcerro
|
49 |
*/
|
50 |
@SuppressWarnings("UseSpecificCatch") |
51 |
public class DefaultForeingKey implements Persistent, ForeingKey, EditableForeingKey, org.gvsig.tools.lang.Cloneable { |
52 |
|
53 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultForeingKey.class); |
54 |
|
55 |
private class DefaultContextForeingKey implements ContextForeingKey { |
56 |
|
57 |
private FeatureStore featureStore = null; |
58 |
private StoresRepository storesRepository = null; |
59 |
private Expression labelExpression; |
60 |
private SymbolTable symbolTable;
|
61 |
private FeatureSymbolTable featureSymbolTable;
|
62 |
private DynObject contextValues;
|
63 |
private int refs; |
64 |
private DataTransaction transaction;
|
65 |
|
66 |
public DefaultContextForeingKey(StoresRepository storesRepository) {
|
67 |
this.refs = 1; |
68 |
this.storesRepository = storesRepository;
|
69 |
} |
70 |
|
71 |
public void addRef() { |
72 |
this.refs++;
|
73 |
} |
74 |
|
75 |
public void relese() { |
76 |
this.refs--;
|
77 |
} |
78 |
|
79 |
@Override
|
80 |
public void dispose() { |
81 |
DisposeUtils.disposeQuietly(featureStore); |
82 |
DisposeUtils.disposeQuietly(contextValues); |
83 |
DisposeUtils.disposeQuietly(this.transaction);
|
84 |
this.featureStore = null; |
85 |
this.storesRepository = null; |
86 |
this.labelExpression = null; |
87 |
this.symbolTable = null; |
88 |
this.featureSymbolTable = null; |
89 |
this.contextValues = null; |
90 |
this.transaction = null; |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public StoresRepository getStoresRepository() {
|
95 |
if (this.storesRepository == null) { |
96 |
this.storesRepository = DALLocator.getDataManager().getStoresRepository();
|
97 |
} |
98 |
return this.storesRepository; |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public FeatureStore getFeatureStore() {
|
103 |
if (DisposeUtils.isNullOrDisposed(this.featureStore)) { |
104 |
StoresRepository repository = DefaultForeingKey.this.getStoresRepository(tableName); |
105 |
this.featureStore = (FeatureStore) repository.getStore(tableName);
|
106 |
if(this.transaction != null){ |
107 |
try {
|
108 |
this.transaction.add(this.featureStore, true); |
109 |
} catch (Exception ex) { |
110 |
throw new RuntimeException("Can't add table '"+tableName+"' to transaction.", ex); |
111 |
} |
112 |
} |
113 |
} |
114 |
return this.featureStore; |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public Expression getLabelExpression() { |
119 |
if (this.labelExpression == null) { |
120 |
if (StringUtils.isBlank(labelFormula)) {
|
121 |
return null; |
122 |
} |
123 |
this.labelExpression = ExpressionUtils.createExpression(labelFormula);
|
124 |
} |
125 |
return this.labelExpression; |
126 |
} |
127 |
|
128 |
@Override
|
129 |
public FeatureSymbolTable getFeatureSymbolTable() {
|
130 |
if (this.featureSymbolTable == null) { |
131 |
DataManager dataManager = DALLocator.getDataManager(); |
132 |
this.featureSymbolTable = dataManager.createFeatureSymbolTable();
|
133 |
this.symbolTable = this.featureSymbolTable.createParent(); |
134 |
} |
135 |
return this.featureSymbolTable; |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public SymbolTable getSymbolTable() {
|
140 |
if (this.symbolTable == null) { |
141 |
DataManager dataManager = DALLocator.getDataManager(); |
142 |
this.featureSymbolTable = dataManager.createFeatureSymbolTable();
|
143 |
this.symbolTable = this.featureSymbolTable.createParent(); |
144 |
} |
145 |
return this.symbolTable; |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public DynObject getContextValues() {
|
150 |
return this.contextValues; |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void setContextValues(DynObject values) { |
155 |
this.contextValues = values;
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
public void setTransaction(DataTransaction transaction) { |
160 |
if(this.transaction != null){ |
161 |
DisposeUtils.disposeQuietly(this.transaction);
|
162 |
this.transaction = null; |
163 |
} |
164 |
this.transaction = transaction;
|
165 |
DisposeUtils.bind(this.transaction);
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
public DataTransaction getTransaction() {
|
170 |
return this.transaction; |
171 |
} |
172 |
} |
173 |
|
174 |
private boolean foreingKey; |
175 |
private boolean closedList; |
176 |
private String labelFormula; |
177 |
private String tableName; |
178 |
private String codeName; |
179 |
private DynObjectValueItem[] availableValues; |
180 |
private boolean loadAvailableValues; |
181 |
private String repositoryIDOfAvailableValues; |
182 |
private String repositoryLabelOfAvailableValues; |
183 |
private boolean ensureReferentialIntegrity; |
184 |
private StoresRepository storesRepository = null; |
185 |
private FeatureAttributeDescriptor descriptor;
|
186 |
private String fieldName; |
187 |
|
188 |
public DefaultForeingKey() {
|
189 |
this.foreingKey = false; |
190 |
this.closedList = false; |
191 |
this.tableName = null; |
192 |
this.codeName = null; |
193 |
this.labelFormula = null; |
194 |
this.ensureReferentialIntegrity = false; |
195 |
this.loadAvailableValues = true; |
196 |
this.repositoryIDOfAvailableValues = null; |
197 |
this.repositoryLabelOfAvailableValues = null; |
198 |
this.fieldName = "unknown"; |
199 |
} |
200 |
|
201 |
public void setDescriptor(FeatureAttributeDescriptor descriptor) { |
202 |
this.descriptor = descriptor;
|
203 |
} |
204 |
|
205 |
public String getFieldName() { |
206 |
if(this.descriptor != null) { |
207 |
return this.descriptor.getName(); |
208 |
} |
209 |
return fieldName;
|
210 |
} |
211 |
|
212 |
private StoresRepository getStoresRepository() {
|
213 |
if (this.storesRepository == null) { |
214 |
if (this.descriptor == null) { |
215 |
this.storesRepository = DALLocator.getDataManager().getStoresRepository();
|
216 |
} else {
|
217 |
FeatureStore store = descriptor.getStore(); |
218 |
if (store == null) { |
219 |
this.storesRepository = DALLocator.getDataManager().getStoresRepository();
|
220 |
} else {
|
221 |
this.storesRepository = store.getStoresRepository();
|
222 |
} |
223 |
} |
224 |
} |
225 |
return this.storesRepository; |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public void unbind() { |
230 |
this.getStoresRepository(); // Force get stores repository |
231 |
if(this.descriptor != null){ |
232 |
this.fieldName = this.descriptor.getName(); |
233 |
} |
234 |
this.descriptor = null; |
235 |
} |
236 |
|
237 |
@Override
|
238 |
public boolean isClosedList() { |
239 |
return this.closedList; |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public void setClosedList(boolean selectable) { |
244 |
this.closedList = selectable;
|
245 |
// Force reload available values
|
246 |
this.availableValues = null; |
247 |
this.loadAvailableValues = true; |
248 |
} |
249 |
|
250 |
@Override
|
251 |
public boolean isForeingKey() { |
252 |
return this.foreingKey; |
253 |
} |
254 |
|
255 |
@Override
|
256 |
public void setForeingKey(boolean foreingKey) { |
257 |
this.foreingKey = foreingKey;
|
258 |
// Force reload available values
|
259 |
this.availableValues = null; |
260 |
this.loadAvailableValues = true; |
261 |
} |
262 |
|
263 |
@Override
|
264 |
public String getLabelFormula() { |
265 |
return this.labelFormula; |
266 |
} |
267 |
|
268 |
@Override
|
269 |
public void setLabelFormula(String labelFormula) { |
270 |
this.labelFormula = labelFormula;
|
271 |
// Force reload available values with new formula
|
272 |
this.availableValues = null; |
273 |
this.loadAvailableValues = true; |
274 |
} |
275 |
|
276 |
@Override
|
277 |
public String getCodeName() { |
278 |
return this.codeName; |
279 |
} |
280 |
|
281 |
@Override
|
282 |
public void setCodeName(String codeName) { |
283 |
this.codeName = codeName;
|
284 |
} |
285 |
|
286 |
@Override
|
287 |
public String getTableName() { |
288 |
return this.tableName; |
289 |
} |
290 |
|
291 |
@Override
|
292 |
public void setTableName(String tableName) { |
293 |
this.tableName = tableName;
|
294 |
// Force reload available values
|
295 |
this.availableValues = null; |
296 |
this.loadAvailableValues = true; |
297 |
} |
298 |
|
299 |
@Override
|
300 |
public boolean isEmpty() { |
301 |
if (!this.foreingKey |
302 |
&& !this.closedList
|
303 |
&& StringUtils.isBlank(this.tableName)
|
304 |
&& StringUtils.isBlank(this.codeName)
|
305 |
&& StringUtils.isBlank(this.labelFormula)) {
|
306 |
return true; |
307 |
} |
308 |
return false; |
309 |
} |
310 |
|
311 |
@Override
|
312 |
public void clean() { |
313 |
this.foreingKey = false; |
314 |
this.closedList = false; |
315 |
this.tableName = null; |
316 |
this.codeName = null; |
317 |
this.labelFormula = null; |
318 |
this.ensureReferentialIntegrity = false; |
319 |
} |
320 |
|
321 |
private void disposeIfLocalContext(ContextForeingKey context) { |
322 |
DefaultContextForeingKey c = (DefaultContextForeingKey) context; |
323 |
c.relese(); |
324 |
if (c.refs == 0) { |
325 |
context.dispose(); |
326 |
} |
327 |
} |
328 |
|
329 |
private ContextForeingKey createLocalContextIfNull(ContextForeingKey context) {
|
330 |
if (context == null) { |
331 |
return createContext();
|
332 |
} |
333 |
DefaultContextForeingKey c = (DefaultContextForeingKey) context; |
334 |
c.addRef(); |
335 |
return c;
|
336 |
} |
337 |
|
338 |
@Override
|
339 |
public ContextForeingKey createContext() {
|
340 |
return new DefaultContextForeingKey(this.getStoresRepository()); |
341 |
} |
342 |
|
343 |
@Override
|
344 |
public StoresRepository getStoresRepository(ContextForeingKey context) {
|
345 |
context = createLocalContextIfNull(context); |
346 |
try {
|
347 |
return context.getStoresRepository();
|
348 |
} finally {
|
349 |
disposeIfLocalContext(context); |
350 |
} |
351 |
} |
352 |
|
353 |
@Override
|
354 |
public FeatureStore getFeatureStore(ContextForeingKey context) {
|
355 |
context = createLocalContextIfNull(context); |
356 |
try {
|
357 |
return context.getFeatureStore();
|
358 |
} finally {
|
359 |
disposeIfLocalContext(context); |
360 |
} |
361 |
} |
362 |
|
363 |
@Override
|
364 |
public FeatureType getFeatureType(ContextForeingKey context) {
|
365 |
context = createLocalContextIfNull(context); |
366 |
FeatureStore store = context.getFeatureStore(); |
367 |
if (store == null) { |
368 |
return null; |
369 |
} |
370 |
try {
|
371 |
return store.getDefaultFeatureType();
|
372 |
} catch (DataException ex) {
|
373 |
return null; |
374 |
} finally {
|
375 |
disposeIfLocalContext(context); |
376 |
} |
377 |
} |
378 |
|
379 |
@Override
|
380 |
public List<Feature> getFeatures(ContextForeingKey context) { |
381 |
context = createLocalContextIfNull(context); |
382 |
FeatureStore store = context.getFeatureStore(); |
383 |
if (store == null) { |
384 |
return null; |
385 |
} |
386 |
try {
|
387 |
return store.getFeatures();
|
388 |
} finally {
|
389 |
disposeIfLocalContext(context); |
390 |
} |
391 |
} |
392 |
|
393 |
@Override
|
394 |
public Object getCode(ContextForeingKey context, Feature feature) { |
395 |
// context = createLocalContextIfNull(context);
|
396 |
try {
|
397 |
return feature.get(codeName);
|
398 |
} finally {
|
399 |
// disposeIfLocalContext(context);
|
400 |
} |
401 |
} |
402 |
|
403 |
@Override
|
404 |
public FeatureQuery getQuery(ContextForeingKey context, Object codeValue) { |
405 |
context = createLocalContextIfNull(context); |
406 |
try {
|
407 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
408 |
FeatureStore store = context.getFeatureStore(); |
409 |
FeatureQuery query = store.createFeatureQuery(); |
410 |
query.setFilter(builder.eq( |
411 |
builder.variable(codeName), |
412 |
builder.constant(codeValue) |
413 |
).toString() |
414 |
); |
415 |
query.retrievesAllAttributes(); |
416 |
return query;
|
417 |
} finally {
|
418 |
disposeIfLocalContext(context); |
419 |
} |
420 |
} |
421 |
|
422 |
@Override
|
423 |
public Feature getFeature(ContextForeingKey context, Object codeValue) { |
424 |
context = createLocalContextIfNull(context); |
425 |
try {
|
426 |
FeatureStore store = context.getFeatureStore(); |
427 |
if (store == null) { |
428 |
return null; |
429 |
} |
430 |
FeatureQuery query = this.getQuery(context, codeValue);
|
431 |
Feature feature = store.findFirst(query); |
432 |
return feature;
|
433 |
} catch (DataException ex) {
|
434 |
return null; |
435 |
} finally {
|
436 |
disposeIfLocalContext(context); |
437 |
} |
438 |
} |
439 |
|
440 |
@Override
|
441 |
public String getLabel(ContextForeingKey context, Object codeValue) { |
442 |
context = createLocalContextIfNull(context); |
443 |
try {
|
444 |
Feature feature = this.getFeature(context, codeValue);
|
445 |
if (feature == null) { |
446 |
return null; |
447 |
} |
448 |
return getLabel(context, feature);
|
449 |
} finally {
|
450 |
disposeIfLocalContext(context); |
451 |
} |
452 |
} |
453 |
|
454 |
@Override
|
455 |
public Expression getLabelExpression(ContextForeingKey context) { |
456 |
context = createLocalContextIfNull(context); |
457 |
try {
|
458 |
return context.getLabelExpression();
|
459 |
} finally {
|
460 |
disposeIfLocalContext(context); |
461 |
} |
462 |
} |
463 |
|
464 |
@Override
|
465 |
public String getLabel(ContextForeingKey context, Feature feature) { |
466 |
if (feature == null) { |
467 |
return null; |
468 |
} |
469 |
context = createLocalContextIfNull(context); |
470 |
try {
|
471 |
Expression labelExpression = context.getLabelExpression();
|
472 |
if (labelExpression == null) { |
473 |
return feature.toString();
|
474 |
} |
475 |
context.getFeatureSymbolTable().setFeature(feature); |
476 |
Object x = labelExpression.execute(context.getSymbolTable());
|
477 |
if (x == null) { |
478 |
return null; |
479 |
} |
480 |
return x.toString();
|
481 |
} finally {
|
482 |
disposeIfLocalContext(context); |
483 |
} |
484 |
} |
485 |
|
486 |
@Override
|
487 |
public DynObjectValueItem[] getAvailableValues(ContextForeingKey context) { |
488 |
if (!this.isClosedList()) { |
489 |
return null; |
490 |
} |
491 |
StoresRepository theStoresRepository = this.getStoresRepository(this.getTableName()); |
492 |
// StoresRepository theStoresRepository = this.getStoresRepository(context);
|
493 |
if( theStoresRepository==null ) { |
494 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.getFieldName()+ "' from table '" + this.getTableName() + "', can't locate the stores-repository."); |
495 |
return null; |
496 |
} |
497 |
theStoresRepository = theStoresRepository.getRepositoryOfStore(this.getTableName());
|
498 |
if( theStoresRepository==null ) { |
499 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.getFieldName()+ "' from table '" + this.getTableName() + "', can't locate the stores-repository from the table."); |
500 |
return null; |
501 |
} |
502 |
if ((this.availableValues == null && this.loadAvailableValues) || |
503 |
!StringUtils.equalsIgnoreCase(theStoresRepository.getID(), this.repositoryIDOfAvailableValues) ||
|
504 |
!StringUtils.equalsIgnoreCase(theStoresRepository.getLabel(), this.repositoryLabelOfAvailableValues)
|
505 |
) { |
506 |
|
507 |
FeatureStore foreingStore = null;
|
508 |
try {
|
509 |
this.repositoryLabelOfAvailableValues = theStoresRepository.getLabel();
|
510 |
this.repositoryIDOfAvailableValues = theStoresRepository.getID();
|
511 |
foreingStore = (FeatureStore) theStoresRepository.getStore( |
512 |
this.getTableName()
|
513 |
); |
514 |
DataManager dataManager = DALLocator.getDataManager(); |
515 |
Map<String,DynObjectValueItem[]> availableValuesCache = (Map<String,DynObjectValueItem[]>) dataManager.getProperty("FOREIGNKEY_AVAILABLE_VALUES_CACHE"); |
516 |
if( availableValuesCache==null ) { |
517 |
availableValuesCache = new HashMap<>(); |
518 |
dataManager.setProperty("FOREIGNKEY_AVAILABLE_VALUES_CACHE",availableValuesCache);
|
519 |
} |
520 |
this.availableValues = availableValuesCache.get(foreingStore.getFullName());
|
521 |
if( this.availableValues == null ) { |
522 |
LOGGER.info("Loading available values for field '" + this.getStoreName() + "." + this.getFieldName()+ "' from table '" + this.getTableName() + "'/"+DataStore.getFullNameQuietly(foreingStore)+"."); |
523 |
this.availableValues = this.getAvailableValuesFromStore(foreingStore); |
524 |
availableValuesCache.put(foreingStore.getFullName(),this.availableValues);
|
525 |
} |
526 |
|
527 |
} catch (Exception ex) { |
528 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.getFieldName() + "' from table '" + this.getTableName() + "' ("+DataStore.getFullNameQuietly(foreingStore)+").", ex); |
529 |
} finally {
|
530 |
this.loadAvailableValues = false; |
531 |
DisposeUtils.disposeQuietly(foreingStore); |
532 |
} |
533 |
} |
534 |
return this.availableValues; |
535 |
} |
536 |
|
537 |
private DynObjectValueItem[] getAvailableValuesFromStore(FeatureStore store) { |
538 |
DisposableFeatureSetIterable set = null;
|
539 |
DynObjectValueItem[] values = null; |
540 |
try {
|
541 |
Expression labelExpression = this.getLabelExpression(null); |
542 |
String theCodeName = this.getCodeName(); |
543 |
FeatureSymbolTable featureSymbolTable = DALLocator.getDataManager().createFeatureSymbolTable(); |
544 |
MutableSymbolTable symbolTable = featureSymbolTable.createParent(); |
545 |
|
546 |
int count = (int) store.getFeatureCount(); |
547 |
if (count < 1) { |
548 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.getFieldName() + "' from table '" + this.getTableName() + "'(" + DataStore.getFullNameQuietly(store) + ")."); |
549 |
} |
550 |
values = new DynObjectValueItem[Math.min(count, MAX_AVAILABLE_VALUES)]; |
551 |
int n = 0; |
552 |
for (Feature feature : set = store.getFeatureSet().iterable()) {
|
553 |
Object code = feature.get(theCodeName);
|
554 |
Object value;
|
555 |
if (labelExpression == null) { |
556 |
value = code; |
557 |
} else {
|
558 |
featureSymbolTable.setFeature(feature); |
559 |
try {
|
560 |
value = labelExpression.execute(symbolTable); |
561 |
} catch (Exception ex) { |
562 |
LOGGER.warn("Can't get label from table: " + this.tableName + " with expression: " + labelExpression.getPhrase(), ex); |
563 |
values = null;
|
564 |
break;
|
565 |
} |
566 |
} |
567 |
values[n++] = new DynObjectValueItem(code, Objects.toString(value, Objects.toString(code, "##ERROR##"))); |
568 |
if (n >= MAX_AVAILABLE_VALUES) {
|
569 |
break;
|
570 |
} |
571 |
} |
572 |
} catch (Exception ex) { |
573 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.getFieldName() + "' from table '" + this.getTableName() + "' (" + DataStore.getFullNameQuietly(store) + ").", ex); |
574 |
} finally {
|
575 |
DisposeUtils.disposeQuietly(set); |
576 |
} |
577 |
return values;
|
578 |
} |
579 |
|
580 |
private String getStoreName() { |
581 |
if (this.descriptor==null) { |
582 |
return "Unknown"; |
583 |
} |
584 |
if (this.descriptor.getStore()==null) { |
585 |
return "Unknown"; |
586 |
} |
587 |
return this.descriptor.getStore().getName(); |
588 |
} |
589 |
|
590 |
@Override
|
591 |
public String getLabelForValue(Object value) { |
592 |
DynObjectValueItem[] values = this.getAvailableValues(null); |
593 |
if (values != null) { |
594 |
for (DynObjectValueItem value1 : values) {
|
595 |
if (Objects.equals(value, value1.getValue())) {
|
596 |
return value1.getLabel();
|
597 |
} |
598 |
} |
599 |
} |
600 |
return Objects.toString(value, "##ERROR##"); |
601 |
} |
602 |
|
603 |
@Override
|
604 |
public void loadFromState(PersistentState state) |
605 |
throws PersistenceException {
|
606 |
foreingKey = state.getBoolean("foreingKey");
|
607 |
closedList = state.getBoolean("selectable");
|
608 |
labelFormula = state.getString("labelFormula");
|
609 |
codeName = state.getString("codeName");
|
610 |
tableName = state.getString("tableName");
|
611 |
ensureReferentialIntegrity = state.getBoolean("ensureReferentialIntegrity");
|
612 |
} |
613 |
|
614 |
@Override
|
615 |
public void saveToState(PersistentState state) throws PersistenceException { |
616 |
state.set("foreingKey", foreingKey);
|
617 |
state.set("selectable", closedList);
|
618 |
state.set("labelFormula", labelFormula);
|
619 |
state.set("codeName", codeName);
|
620 |
state.set("tableName", tableName);
|
621 |
state.set("ensureReferentialIntegrity", ensureReferentialIntegrity);
|
622 |
} |
623 |
|
624 |
private static final String FOREINGKEY_PERSISTENCE_DEFINITION_NAME = "ForeingKey"; |
625 |
|
626 |
public static void registerPersistenceDefinition() { |
627 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
628 |
|
629 |
if (manager.getDefinition(FOREINGKEY_PERSISTENCE_DEFINITION_NAME)
|
630 |
== null) {
|
631 |
DynStruct definition = manager.addDefinition(DefaultForeingKey.class, |
632 |
FOREINGKEY_PERSISTENCE_DEFINITION_NAME, |
633 |
FOREINGKEY_PERSISTENCE_DEFINITION_NAME |
634 |
+ " persistent definition",
|
635 |
null,
|
636 |
null
|
637 |
); |
638 |
definition.addDynFieldBoolean("foreingKey");
|
639 |
definition.addDynFieldBoolean("selectable");
|
640 |
definition.addDynFieldString("LabelFormula");
|
641 |
definition.addDynFieldString("codeName");
|
642 |
definition.addDynFieldString("tableName");
|
643 |
definition.addDynFieldBoolean("ensureReferentialIntegrity");
|
644 |
} |
645 |
} |
646 |
|
647 |
@Override
|
648 |
public ForeingKey clone() throws CloneNotSupportedException { |
649 |
DefaultForeingKey other = (DefaultForeingKey) super.clone();
|
650 |
return other;
|
651 |
} |
652 |
|
653 |
@Override
|
654 |
public boolean getEnsureReferentialIntegrity() { |
655 |
return this.ensureReferentialIntegrity; |
656 |
} |
657 |
|
658 |
@Override
|
659 |
public void setEnsureReferentialIntegrity(boolean ensureReferentialIntegrity) { |
660 |
this.ensureReferentialIntegrity = ensureReferentialIntegrity;
|
661 |
} |
662 |
|
663 |
@Override
|
664 |
public boolean isInAvailableValues(Object valueToCheck) { |
665 |
if (this.hasAvailableValues() && availableValues.length > 0) { |
666 |
for (DynObjectValueItem availableValue : availableValues) {
|
667 |
if (Objects.equals(valueToCheck, availableValue.getValue())) {
|
668 |
return true; |
669 |
} |
670 |
} |
671 |
} |
672 |
return false; |
673 |
} |
674 |
|
675 |
@Override
|
676 |
public boolean hasAvailableValues() { |
677 |
if (this.availableValues == null) { |
678 |
this.getAvailableValues(null); |
679 |
} |
680 |
return this.availableValues != null; |
681 |
} |
682 |
|
683 |
public StoresRepository getStoresRepository(String tableName) { |
684 |
StoresRepository repository = this.getStoresRepository();
|
685 |
FeatureStore store = null;
|
686 |
if(descriptor != null) { |
687 |
store = descriptor.getStore(); |
688 |
} |
689 |
|
690 |
if( store==null ) { |
691 |
return repository;
|
692 |
} else {
|
693 |
DataServerExplorer explorer = null;
|
694 |
try {
|
695 |
DataManager dataManager = DALLocator.getDataManager(); |
696 |
explorer = store.getExplorer(); |
697 |
StoresRepository repo2 = dataManager.getStoresRepository().getSubrepository(explorer); |
698 |
if( repo2!=null && repo2.contains(tableName)) { |
699 |
return repo2;
|
700 |
} |
701 |
DataStoreParameters params = explorer.get(tableName); |
702 |
if( explorer.exists(params) ) {
|
703 |
String id = getRepositoryId(store, tableName); //tableName+"-"+dataManager.createUniqueID().replace("-", ""); |
704 |
BaseStoresRepository repo3 = new BaseStoresRepository(id, null, explorer.getParameters()); |
705 |
repo3.add(tableName, params); |
706 |
return repo3;
|
707 |
} |
708 |
} catch (Exception ex) { |
709 |
LOGGER.info("Can't access to table '"+tableName+"' near store "+store.getFullName()+", search in repositories.",ex); |
710 |
} finally {
|
711 |
DisposeUtils.disposeQuietly(explorer); |
712 |
} |
713 |
return repository;
|
714 |
} |
715 |
} |
716 |
|
717 |
private String getRepositoryId(FeatureStore store, String tableName) { |
718 |
String id;
|
719 |
if(store == null){ |
720 |
DataManager dataManager = DALLocator.getDataManager(); |
721 |
id = tableName+"-"+dataManager.createUniqueID().replace("-", ""); |
722 |
} else {
|
723 |
id = tableName + "-" + DigestUtils.md5Hex(store.getFullName());
|
724 |
} |
725 |
return id;
|
726 |
} |
727 |
|
728 |
} |