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