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