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 @ 46940
History | View | Annotate | Download (25.2 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.Feature; |
24 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
25 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
26 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
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 |
|
180 |
public DefaultForeingKey() {
|
181 |
this.foreingKey = false; |
182 |
this.closedList = false; |
183 |
this.tableName = null; |
184 |
this.codeName = null; |
185 |
this.labelFormula = null; |
186 |
this.ensureReferentialIntegrity = false; |
187 |
this.loadAvailableValues = true; |
188 |
this.repositoryIDOfAvailableValues = null; |
189 |
this.repositoryLabelOfAvailableValues = null; |
190 |
} |
191 |
|
192 |
public void setDescriptor(FeatureAttributeDescriptor descriptor) { |
193 |
this.descriptor = descriptor;
|
194 |
} |
195 |
|
196 |
private StoresRepository getStoresRepository() {
|
197 |
if( this.descriptor == null ) { |
198 |
this.storesRepository = DALLocator.getDataManager().getStoresRepository();
|
199 |
} else {
|
200 |
FeatureStore store = descriptor.getStore(); |
201 |
if( store==null ) { |
202 |
this.storesRepository = DALLocator.getDataManager().getStoresRepository();
|
203 |
} else {
|
204 |
this.storesRepository = store.getStoresRepository();
|
205 |
} |
206 |
} |
207 |
return this.storesRepository; |
208 |
} |
209 |
|
210 |
@Override
|
211 |
public void unbind() { |
212 |
this.getStoresRepository(); // Force get stores repository |
213 |
this.descriptor = null; |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public boolean isClosedList() { |
218 |
return this.closedList; |
219 |
} |
220 |
|
221 |
@Override
|
222 |
public void setClosedList(boolean selectable) { |
223 |
this.closedList = selectable;
|
224 |
// Force reload available values
|
225 |
this.availableValues = null; |
226 |
this.loadAvailableValues = true; |
227 |
} |
228 |
|
229 |
@Override
|
230 |
public boolean isForeingKey() { |
231 |
return this.foreingKey; |
232 |
} |
233 |
|
234 |
@Override
|
235 |
public void setForeingKey(boolean foreingKey) { |
236 |
this.foreingKey = foreingKey;
|
237 |
// Force reload available values
|
238 |
this.availableValues = null; |
239 |
this.loadAvailableValues = true; |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public String getLabelFormula() { |
244 |
return this.labelFormula; |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public void setLabelFormula(String labelFormula) { |
249 |
this.labelFormula = labelFormula;
|
250 |
// Force reload available values with new formula
|
251 |
this.availableValues = null; |
252 |
this.loadAvailableValues = true; |
253 |
} |
254 |
|
255 |
@Override
|
256 |
public String getCodeName() { |
257 |
return this.codeName; |
258 |
} |
259 |
|
260 |
@Override
|
261 |
public void setCodeName(String codeName) { |
262 |
this.codeName = codeName;
|
263 |
} |
264 |
|
265 |
@Override
|
266 |
public String getTableName() { |
267 |
return this.tableName; |
268 |
} |
269 |
|
270 |
@Override
|
271 |
public void setTableName(String tableName) { |
272 |
this.tableName = tableName;
|
273 |
// Force reload available values
|
274 |
this.availableValues = null; |
275 |
this.loadAvailableValues = true; |
276 |
} |
277 |
|
278 |
@Override
|
279 |
public boolean isEmpty() { |
280 |
if (!this.foreingKey |
281 |
&& !this.closedList
|
282 |
&& StringUtils.isBlank(this.tableName)
|
283 |
&& StringUtils.isBlank(this.codeName)
|
284 |
&& StringUtils.isBlank(this.labelFormula)) {
|
285 |
return true; |
286 |
} |
287 |
return false; |
288 |
} |
289 |
|
290 |
@Override
|
291 |
public void clean() { |
292 |
this.foreingKey = false; |
293 |
this.closedList = false; |
294 |
this.tableName = null; |
295 |
this.codeName = null; |
296 |
this.labelFormula = null; |
297 |
this.ensureReferentialIntegrity = false; |
298 |
} |
299 |
|
300 |
private void disposeIfLocalContext(ContextForeingKey context) { |
301 |
DefaultContextForeingKey c = (DefaultContextForeingKey) context; |
302 |
c.relese(); |
303 |
if (c.refs == 0) { |
304 |
context.dispose(); |
305 |
} |
306 |
} |
307 |
|
308 |
private ContextForeingKey createLocalContextIfNull(ContextForeingKey context) {
|
309 |
if (context == null) { |
310 |
return createContext();
|
311 |
} |
312 |
DefaultContextForeingKey c = (DefaultContextForeingKey) context; |
313 |
c.addRef(); |
314 |
return c;
|
315 |
} |
316 |
|
317 |
@Override
|
318 |
public ContextForeingKey createContext() {
|
319 |
return new DefaultContextForeingKey(this.getStoresRepository()); |
320 |
} |
321 |
|
322 |
@Override
|
323 |
public StoresRepository getStoresRepository(ContextForeingKey context) {
|
324 |
context = createLocalContextIfNull(context); |
325 |
try {
|
326 |
return context.getStoresRepository();
|
327 |
} finally {
|
328 |
disposeIfLocalContext(context); |
329 |
} |
330 |
} |
331 |
|
332 |
@Override
|
333 |
public FeatureStore getFeatureStore(ContextForeingKey context) {
|
334 |
context = createLocalContextIfNull(context); |
335 |
try {
|
336 |
return context.getFeatureStore();
|
337 |
} finally {
|
338 |
disposeIfLocalContext(context); |
339 |
} |
340 |
} |
341 |
|
342 |
@Override
|
343 |
public FeatureType getFeatureType(ContextForeingKey context) {
|
344 |
context = createLocalContextIfNull(context); |
345 |
FeatureStore store = context.getFeatureStore(); |
346 |
if (store == null) { |
347 |
return null; |
348 |
} |
349 |
try {
|
350 |
return store.getDefaultFeatureType();
|
351 |
} catch (DataException ex) {
|
352 |
return null; |
353 |
} finally {
|
354 |
disposeIfLocalContext(context); |
355 |
} |
356 |
} |
357 |
|
358 |
@Override
|
359 |
public List<Feature> getFeatures(ContextForeingKey context) { |
360 |
context = createLocalContextIfNull(context); |
361 |
FeatureStore store = context.getFeatureStore(); |
362 |
if (store == null) { |
363 |
return null; |
364 |
} |
365 |
try {
|
366 |
return store.getFeatures();
|
367 |
} finally {
|
368 |
disposeIfLocalContext(context); |
369 |
} |
370 |
} |
371 |
|
372 |
@Override
|
373 |
public Object getCode(ContextForeingKey context, Feature feature) { |
374 |
// context = createLocalContextIfNull(context);
|
375 |
try {
|
376 |
return feature.get(codeName);
|
377 |
} finally {
|
378 |
// disposeIfLocalContext(context);
|
379 |
} |
380 |
} |
381 |
|
382 |
@Override
|
383 |
public FeatureQuery getQuery(ContextForeingKey context, Object codeValue) { |
384 |
context = createLocalContextIfNull(context); |
385 |
try {
|
386 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
387 |
FeatureStore store = context.getFeatureStore(); |
388 |
FeatureQuery query = store.createFeatureQuery(); |
389 |
query.setFilter(builder.eq( |
390 |
builder.variable(codeName), |
391 |
builder.constant(codeValue) |
392 |
).toString() |
393 |
); |
394 |
query.retrievesAllAttributes(); |
395 |
return query;
|
396 |
} finally {
|
397 |
disposeIfLocalContext(context); |
398 |
} |
399 |
} |
400 |
|
401 |
@Override
|
402 |
public Feature getFeature(ContextForeingKey context, Object codeValue) { |
403 |
context = createLocalContextIfNull(context); |
404 |
try {
|
405 |
FeatureStore store = context.getFeatureStore(); |
406 |
if (store == null) { |
407 |
return null; |
408 |
} |
409 |
FeatureQuery query = this.getQuery(context, codeValue);
|
410 |
Feature feature = store.findFirst(query); |
411 |
return feature;
|
412 |
} catch (DataException ex) {
|
413 |
return null; |
414 |
} finally {
|
415 |
disposeIfLocalContext(context); |
416 |
} |
417 |
} |
418 |
|
419 |
@Override
|
420 |
public String getLabel(ContextForeingKey context, Object codeValue) { |
421 |
context = createLocalContextIfNull(context); |
422 |
try {
|
423 |
Feature feature = this.getFeature(context, codeValue);
|
424 |
if (feature == null) { |
425 |
return null; |
426 |
} |
427 |
return getLabel(context, feature);
|
428 |
} finally {
|
429 |
disposeIfLocalContext(context); |
430 |
} |
431 |
} |
432 |
|
433 |
@Override
|
434 |
public Expression getLabelExpression(ContextForeingKey context) { |
435 |
context = createLocalContextIfNull(context); |
436 |
try {
|
437 |
return context.getLabelExpression();
|
438 |
} finally {
|
439 |
disposeIfLocalContext(context); |
440 |
} |
441 |
} |
442 |
|
443 |
@Override
|
444 |
public String getLabel(ContextForeingKey context, Feature feature) { |
445 |
if (feature == null) { |
446 |
return null; |
447 |
} |
448 |
context = createLocalContextIfNull(context); |
449 |
try {
|
450 |
Expression labelExpression = context.getLabelExpression();
|
451 |
if (labelExpression == null) { |
452 |
return feature.toString();
|
453 |
} |
454 |
context.getFeatureSymbolTable().setFeature(feature); |
455 |
Object x = labelExpression.execute(context.getSymbolTable());
|
456 |
if (x == null) { |
457 |
return null; |
458 |
} |
459 |
return x.toString();
|
460 |
} finally {
|
461 |
disposeIfLocalContext(context); |
462 |
} |
463 |
} |
464 |
|
465 |
@Override
|
466 |
public DynObjectValueItem[] getAvailableValues(ContextForeingKey context) { |
467 |
if (!this.isClosedList()) { |
468 |
return null; |
469 |
} |
470 |
StoresRepository theStoresRepository = this.getStoresRepository(this.getTableName()); |
471 |
// StoresRepository theStoresRepository = this.getStoresRepository(context);
|
472 |
if( theStoresRepository==null ) { |
473 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.descriptor.getName()+ "' from table '" + this.getTableName() + "', can't locate the stores-repository."); |
474 |
return null; |
475 |
} |
476 |
theStoresRepository = theStoresRepository.getRepositoryOfStore(this.getTableName());
|
477 |
if( theStoresRepository==null ) { |
478 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.descriptor.getName()+ "' from table '" + this.getTableName() + "', can't locate the stores-repository from the table."); |
479 |
return null; |
480 |
} |
481 |
if ((this.availableValues == null && this.loadAvailableValues) || |
482 |
!StringUtils.equalsIgnoreCase(theStoresRepository.getID(), this.repositoryIDOfAvailableValues) ||
|
483 |
!StringUtils.equalsIgnoreCase(theStoresRepository.getLabel(), this.repositoryLabelOfAvailableValues)
|
484 |
) { |
485 |
|
486 |
FeatureStore foreingStore = null;
|
487 |
try {
|
488 |
this.repositoryLabelOfAvailableValues = theStoresRepository.getLabel();
|
489 |
this.repositoryIDOfAvailableValues = theStoresRepository.getID();
|
490 |
foreingStore = (FeatureStore) theStoresRepository.getStore( |
491 |
this.getTableName()
|
492 |
); |
493 |
LOGGER.info("Loading available values for field '" + this.getStoreName() + "." + this.descriptor.getName()+ "' from table '" + this.getTableName() + "'/"+DataStore.getFullNameQuietly(foreingStore)+"."); |
494 |
this.availableValues = this.getAvailableValuesFromStore(foreingStore); |
495 |
|
496 |
} catch (Exception ex) { |
497 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.descriptor.getName() + "' from table '" + this.getTableName() + "' ("+DataStore.getFullNameQuietly(foreingStore)+").", ex); |
498 |
} finally {
|
499 |
this.loadAvailableValues = false; |
500 |
DisposeUtils.disposeQuietly(foreingStore); |
501 |
} |
502 |
} |
503 |
return this.availableValues; |
504 |
} |
505 |
|
506 |
private DynObjectValueItem[] getAvailableValuesFromStore(FeatureStore store) { |
507 |
FeatureSet.DisposableFeatureSetIterable set = null;
|
508 |
DynObjectValueItem[] values = null; |
509 |
try {
|
510 |
Expression labelExpression = this.getLabelExpression(null); |
511 |
String theCodeName = this.getCodeName(); |
512 |
FeatureSymbolTable featureSymbolTable = DALLocator.getDataManager().createFeatureSymbolTable(); |
513 |
MutableSymbolTable symbolTable = featureSymbolTable.createParent(); |
514 |
|
515 |
int count = (int) store.getFeatureCount(); |
516 |
if (count < 1) { |
517 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.descriptor.getName() + "' from table '" + this.getTableName() + "'(" + DataStore.getFullNameQuietly(store) + ")."); |
518 |
} |
519 |
values = new DynObjectValueItem[Math.min(count, MAX_AVAILABLE_VALUES)]; |
520 |
int n = 0; |
521 |
for (Feature feature : set = store.getFeatureSet().iterable()) {
|
522 |
Object code = feature.get(theCodeName);
|
523 |
Object value;
|
524 |
if (labelExpression == null) { |
525 |
value = code; |
526 |
} else {
|
527 |
featureSymbolTable.setFeature(feature); |
528 |
try {
|
529 |
value = labelExpression.execute(symbolTable); |
530 |
} catch (Exception ex) { |
531 |
LOGGER.warn("Can't get label from table: " + this.tableName + " with expression: " + labelExpression.getPhrase(), ex); |
532 |
values = null;
|
533 |
break;
|
534 |
} |
535 |
} |
536 |
values[n++] = new DynObjectValueItem(code, Objects.toString(value, Objects.toString(code, "##ERROR##"))); |
537 |
if (n >= MAX_AVAILABLE_VALUES) {
|
538 |
break;
|
539 |
} |
540 |
} |
541 |
} catch (Exception ex) { |
542 |
LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.descriptor.getName() + "' from table '" + this.getTableName() + "' (" + DataStore.getFullNameQuietly(store) + ").", ex); |
543 |
} finally {
|
544 |
DisposeUtils.disposeQuietly(set); |
545 |
} |
546 |
return values;
|
547 |
} |
548 |
|
549 |
private String getStoreName() { |
550 |
if (this.descriptor==null) { |
551 |
return "Unknown"; |
552 |
} |
553 |
if (this.descriptor.getStore()==null) { |
554 |
return "Unknown"; |
555 |
} |
556 |
return this.descriptor.getStore().getName(); |
557 |
} |
558 |
|
559 |
@Override
|
560 |
public String getLabelForValue(Object value) { |
561 |
DynObjectValueItem[] values = this.getAvailableValues(null); |
562 |
if (values != null) { |
563 |
for (DynObjectValueItem value1 : values) {
|
564 |
if (Objects.equals(value, value1.getValue())) {
|
565 |
return value1.getLabel();
|
566 |
} |
567 |
} |
568 |
} |
569 |
return Objects.toString(value, "##ERROR##"); |
570 |
} |
571 |
|
572 |
@Override
|
573 |
public void loadFromState(PersistentState state) |
574 |
throws PersistenceException {
|
575 |
foreingKey = state.getBoolean("foreingKey");
|
576 |
closedList = state.getBoolean("selectable");
|
577 |
labelFormula = state.getString("labelFormula");
|
578 |
codeName = state.getString("codeName");
|
579 |
tableName = state.getString("tableName");
|
580 |
ensureReferentialIntegrity = state.getBoolean("ensureReferentialIntegrity");
|
581 |
} |
582 |
|
583 |
@Override
|
584 |
public void saveToState(PersistentState state) throws PersistenceException { |
585 |
state.set("foreingKey", foreingKey);
|
586 |
state.set("selectable", closedList);
|
587 |
state.set("labelFormula", labelFormula);
|
588 |
state.set("codeName", codeName);
|
589 |
state.set("tableName", tableName);
|
590 |
state.set("ensureReferentialIntegrity", ensureReferentialIntegrity);
|
591 |
} |
592 |
|
593 |
private static final String FOREINGKEY_PERSISTENCE_DEFINITION_NAME = "ForeingKey"; |
594 |
|
595 |
public static void registerPersistenceDefinition() { |
596 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
597 |
|
598 |
if (manager.getDefinition(FOREINGKEY_PERSISTENCE_DEFINITION_NAME)
|
599 |
== null) {
|
600 |
DynStruct definition = manager.addDefinition(DefaultForeingKey.class, |
601 |
FOREINGKEY_PERSISTENCE_DEFINITION_NAME, |
602 |
FOREINGKEY_PERSISTENCE_DEFINITION_NAME |
603 |
+ " persistent definition",
|
604 |
null,
|
605 |
null
|
606 |
); |
607 |
definition.addDynFieldBoolean("foreingKey");
|
608 |
definition.addDynFieldBoolean("selectable");
|
609 |
definition.addDynFieldString("LabelFormula");
|
610 |
definition.addDynFieldString("codeName");
|
611 |
definition.addDynFieldString("tableName");
|
612 |
definition.addDynFieldBoolean("ensureReferentialIntegrity");
|
613 |
} |
614 |
} |
615 |
|
616 |
@Override
|
617 |
public ForeingKey clone() throws CloneNotSupportedException { |
618 |
DefaultForeingKey other = (DefaultForeingKey) super.clone();
|
619 |
return other;
|
620 |
} |
621 |
|
622 |
@Override
|
623 |
public boolean getEnsureReferentialIntegrity() { |
624 |
return this.ensureReferentialIntegrity; |
625 |
} |
626 |
|
627 |
@Override
|
628 |
public void setEnsureReferentialIntegrity(boolean ensureReferentialIntegrity) { |
629 |
this.ensureReferentialIntegrity = ensureReferentialIntegrity;
|
630 |
} |
631 |
|
632 |
@Override
|
633 |
public boolean isInAvailableValues(Object valueToCheck) { |
634 |
if (this.hasAvailableValues() && availableValues.length > 0) { |
635 |
for (DynObjectValueItem availableValue : availableValues) {
|
636 |
if (Objects.equals(valueToCheck, availableValue.getValue())) {
|
637 |
return true; |
638 |
} |
639 |
} |
640 |
} |
641 |
return false; |
642 |
} |
643 |
|
644 |
@Override
|
645 |
public boolean hasAvailableValues() { |
646 |
if (this.availableValues == null) { |
647 |
this.getAvailableValues(null); |
648 |
} |
649 |
return this.availableValues != null; |
650 |
} |
651 |
|
652 |
public StoresRepository getStoresRepository(String tableName) { |
653 |
StoresRepository repository = this.getStoresRepository();
|
654 |
FeatureStore store = descriptor.getStore(); |
655 |
if( store==null ) { |
656 |
return repository;
|
657 |
} else {
|
658 |
try {
|
659 |
DataManager dataManager = DALLocator.getDataManager(); |
660 |
DataServerExplorer explorer = store.getExplorer(); |
661 |
StoresRepository repo2 = dataManager.getStoresRepository().getSubrepository(explorer); |
662 |
if( repo2!=null && repo2.contains(tableName)) { |
663 |
return repo2;
|
664 |
} |
665 |
DataStoreParameters params = explorer.get(tableName); |
666 |
if( explorer.exists(params) ) {
|
667 |
String id = getRepositoryId(store, tableName); //tableName+"-"+dataManager.createUniqueID().replace("-", ""); |
668 |
BaseStoresRepository repo3 = new BaseStoresRepository(id, null, explorer.getParameters()); |
669 |
repo3.add(tableName, params); |
670 |
return repo3;
|
671 |
} |
672 |
} catch (Exception ex) { |
673 |
LOGGER.info("Can't access to table '"+tableName+"' near store "+store.getFullName()+", search in repositories.",ex); |
674 |
} |
675 |
return repository;
|
676 |
} |
677 |
} |
678 |
|
679 |
private String getRepositoryId(FeatureStore store, String tableName) { |
680 |
String id;
|
681 |
if(store == null){ |
682 |
DataManager dataManager = DALLocator.getDataManager(); |
683 |
id = tableName+"-"+dataManager.createUniqueID().replace("-", ""); |
684 |
} else {
|
685 |
id = tableName + "-" + DigestUtils.md5Hex(store.getFullName());
|
686 |
} |
687 |
return id;
|
688 |
} |
689 |
|
690 |
} |