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