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