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 @ 47174
History | View | Annotate | Download (26.5 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 | 46940 | fdiaz | import org.apache.commons.codec.digest.DigestUtils; |
10 | 44262 | jjdelcerro | 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 | 45772 | omartinez | import org.gvsig.expressionevaluator.MutableSymbolTable; |
15 | 44262 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
16 | 46938 | jjdelcerro | import org.gvsig.fmap.dal.BaseStoresRepository; |
17 | 44262 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
18 | import org.gvsig.fmap.dal.DataManager; |
||
19 | 46938 | jjdelcerro | import org.gvsig.fmap.dal.DataServerExplorer; |
20 | 46737 | jjdelcerro | import org.gvsig.fmap.dal.DataStore; |
21 | 46938 | jjdelcerro | import org.gvsig.fmap.dal.DataStoreParameters; |
22 | 44262 | jjdelcerro | import org.gvsig.fmap.dal.StoresRepository; |
23 | import org.gvsig.fmap.dal.exception.DataException; |
||
24 | import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
||
25 | 47049 | jjdelcerro | import org.gvsig.fmap.dal.feature.DisposableFeatureSetIterable; |
26 | 44262 | jjdelcerro | 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 | 45772 | omartinez | import static org.gvsig.fmap.dal.feature.ForeingKey.MAX_AVAILABLE_VALUES; |
32 | 44262 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
33 | import org.gvsig.tools.dispose.DisposeUtils; |
||
34 | 45739 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
35 | 44262 | jjdelcerro | 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 | 45739 | jjdelcerro | private DynObject contextValues;
|
61 | 44262 | jjdelcerro | private int refs; |
62 | |||
63 | 45772 | omartinez | public DefaultContextForeingKey(StoresRepository storesRepository) {
|
64 | 44262 | jjdelcerro | this.refs = 1; |
65 | 45772 | omartinez | this.storesRepository = storesRepository;
|
66 | 44262 | jjdelcerro | } |
67 | 45772 | omartinez | |
68 | 44262 | jjdelcerro | public void addRef() { |
69 | this.refs++;
|
||
70 | } |
||
71 | |||
72 | public void relese() { |
||
73 | this.refs--;
|
||
74 | } |
||
75 | 45772 | omartinez | |
76 | 44262 | jjdelcerro | @Override
|
77 | public void dispose() { |
||
78 | DisposeUtils.disposeQuietly(featureStore); |
||
79 | 45739 | jjdelcerro | DisposeUtils.disposeQuietly(contextValues); |
80 | 44262 | jjdelcerro | 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 | 45772 | omartinez | this.storesRepository = DALLocator.getDataManager().getStoresRepository();
|
91 | 44262 | jjdelcerro | } |
92 | return this.storesRepository; |
||
93 | } |
||
94 | |||
95 | @Override
|
||
96 | public FeatureStore getFeatureStore() {
|
||
97 | if (this.featureStore == null) { |
||
98 | 46938 | jjdelcerro | StoresRepository repository = DefaultForeingKey.this.getStoresRepository(tableName); |
99 | 44304 | jjdelcerro | this.featureStore = (FeatureStore) repository.getStore(tableName);
|
100 | 46938 | jjdelcerro | // 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 | 44262 | jjdelcerro | } |
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 | 45739 | jjdelcerro | |
158 | @Override
|
||
159 | public DynObject getContextValues() {
|
||
160 | return this.contextValues; |
||
161 | 45772 | omartinez | } |
162 | 45739 | jjdelcerro | |
163 | @Override
|
||
164 | public void setContextValues(DynObject values) { |
||
165 | this.contextValues = values;
|
||
166 | } |
||
167 | 44262 | jjdelcerro | } |
168 | |||
169 | private boolean foreingKey; |
||
170 | 44338 | jjdelcerro | private boolean closedList; |
171 | 44262 | jjdelcerro | private String labelFormula; |
172 | private String tableName; |
||
173 | private String codeName; |
||
174 | 44338 | jjdelcerro | private DynObjectValueItem[] availableValues; |
175 | 45946 | omartinez | private boolean loadAvailableValues; |
176 | 46737 | jjdelcerro | private String repositoryIDOfAvailableValues; |
177 | private String repositoryLabelOfAvailableValues; |
||
178 | 44363 | jjdelcerro | private boolean ensureReferentialIntegrity; |
179 | 45772 | omartinez | private StoresRepository storesRepository = null; |
180 | 44262 | jjdelcerro | private FeatureAttributeDescriptor descriptor;
|
181 | 46984 | fdiaz | private String fieldName; |
182 | 44262 | jjdelcerro | |
183 | public DefaultForeingKey() {
|
||
184 | 44363 | jjdelcerro | this.foreingKey = false; |
185 | this.closedList = false; |
||
186 | this.tableName = null; |
||
187 | this.codeName = null; |
||
188 | this.labelFormula = null; |
||
189 | this.ensureReferentialIntegrity = false; |
||
190 | 45946 | omartinez | this.loadAvailableValues = true; |
191 | 46737 | jjdelcerro | this.repositoryIDOfAvailableValues = null; |
192 | this.repositoryLabelOfAvailableValues = null; |
||
193 | 46984 | fdiaz | this.fieldName = "unknown"; |
194 | 44262 | jjdelcerro | } |
195 | |||
196 | public void setDescriptor(FeatureAttributeDescriptor descriptor) { |
||
197 | this.descriptor = descriptor;
|
||
198 | 45775 | jjdelcerro | } |
199 | |||
200 | 46984 | fdiaz | public String getFieldName() { |
201 | if(this.descriptor != null) { |
||
202 | return this.descriptor.getName(); |
||
203 | } |
||
204 | return fieldName;
|
||
205 | } |
||
206 | |||
207 | 45775 | jjdelcerro | private StoresRepository getStoresRepository() {
|
208 | 46984 | fdiaz | if (this.storesRepository == null) { |
209 | if (this.descriptor == null) { |
||
210 | 45775 | jjdelcerro | this.storesRepository = DALLocator.getDataManager().getStoresRepository();
|
211 | } else {
|
||
212 | 46984 | fdiaz | FeatureStore store = descriptor.getStore(); |
213 | if (store == null) { |
||
214 | this.storesRepository = DALLocator.getDataManager().getStoresRepository();
|
||
215 | } else {
|
||
216 | this.storesRepository = store.getStoresRepository();
|
||
217 | } |
||
218 | 45775 | jjdelcerro | } |
219 | 45772 | omartinez | } |
220 | 45775 | jjdelcerro | return this.storesRepository; |
221 | 44262 | jjdelcerro | } |
222 | 45772 | omartinez | |
223 | 45775 | jjdelcerro | @Override
|
224 | 45772 | omartinez | public void unbind() { |
225 | 45775 | jjdelcerro | this.getStoresRepository(); // Force get stores repository |
226 | 46984 | fdiaz | if(this.descriptor != null){ |
227 | this.fieldName = this.descriptor.getName(); |
||
228 | } |
||
229 | 45772 | omartinez | this.descriptor = null; |
230 | } |
||
231 | 44262 | jjdelcerro | |
232 | @Override
|
||
233 | 44338 | jjdelcerro | public boolean isClosedList() { |
234 | return this.closedList; |
||
235 | 44262 | jjdelcerro | } |
236 | |||
237 | @Override
|
||
238 | 44338 | jjdelcerro | public void setClosedList(boolean selectable) { |
239 | this.closedList = selectable;
|
||
240 | 46737 | jjdelcerro | // Force reload available values
|
241 | this.availableValues = null; |
||
242 | this.loadAvailableValues = true; |
||
243 | 44262 | jjdelcerro | } |
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 | 46737 | jjdelcerro | // Force reload available values
|
254 | this.availableValues = null; |
||
255 | this.loadAvailableValues = true; |
||
256 | 44262 | jjdelcerro | } |
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 | 45946 | omartinez | // Force reload available values with new formula
|
267 | this.availableValues = null; |
||
268 | this.loadAvailableValues = true; |
||
269 | 44262 | jjdelcerro | } |
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 | 46737 | jjdelcerro | // Force reload available values
|
290 | this.availableValues = null; |
||
291 | this.loadAvailableValues = true; |
||
292 | 44262 | jjdelcerro | } |
293 | |||
294 | @Override
|
||
295 | public boolean isEmpty() { |
||
296 | if (!this.foreingKey |
||
297 | 44338 | jjdelcerro | && !this.closedList
|
298 | 44262 | jjdelcerro | && 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 | 44338 | jjdelcerro | this.closedList = false; |
310 | 44262 | jjdelcerro | this.tableName = null; |
311 | this.codeName = null; |
||
312 | this.labelFormula = null; |
||
313 | 44363 | jjdelcerro | this.ensureReferentialIntegrity = false; |
314 | 44262 | jjdelcerro | } |
315 | |||
316 | private void disposeIfLocalContext(ContextForeingKey context) { |
||
317 | DefaultContextForeingKey c = (DefaultContextForeingKey) context; |
||
318 | c.relese(); |
||
319 | 45772 | omartinez | if (c.refs == 0) { |
320 | 44262 | jjdelcerro | context.dispose(); |
321 | } |
||
322 | } |
||
323 | |||
324 | private ContextForeingKey createLocalContextIfNull(ContextForeingKey context) {
|
||
325 | if (context == null) { |
||
326 | 45772 | omartinez | return createContext();
|
327 | 44262 | jjdelcerro | } |
328 | DefaultContextForeingKey c = (DefaultContextForeingKey) context; |
||
329 | c.addRef(); |
||
330 | return c;
|
||
331 | } |
||
332 | |||
333 | @Override
|
||
334 | public ContextForeingKey createContext() {
|
||
335 | 45775 | jjdelcerro | return new DefaultContextForeingKey(this.getStoresRepository()); |
336 | 44262 | jjdelcerro | } |
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 | 46737 | jjdelcerro | |
481 | 44262 | jjdelcerro | @Override
|
482 | public DynObjectValueItem[] getAvailableValues(ContextForeingKey context) { |
||
483 | 44338 | jjdelcerro | if (!this.isClosedList()) { |
484 | 44262 | jjdelcerro | return null; |
485 | } |
||
486 | 46938 | jjdelcerro | StoresRepository theStoresRepository = this.getStoresRepository(this.getTableName()); |
487 | // StoresRepository theStoresRepository = this.getStoresRepository(context);
|
||
488 | 46737 | jjdelcerro | if( theStoresRepository==null ) { |
489 | 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."); |
490 | 46737 | jjdelcerro | return null; |
491 | } |
||
492 | theStoresRepository = theStoresRepository.getRepositoryOfStore(this.getTableName());
|
||
493 | 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 from the table."); |
495 | 46737 | jjdelcerro | 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 | 45772 | omartinez | |
502 | FeatureStore foreingStore = null;
|
||
503 | try {
|
||
504 | 46737 | jjdelcerro | this.repositoryLabelOfAvailableValues = theStoresRepository.getLabel();
|
505 | this.repositoryIDOfAvailableValues = theStoresRepository.getID();
|
||
506 | 45772 | omartinez | foreingStore = (FeatureStore) theStoresRepository.getStore( |
507 | this.getTableName()
|
||
508 | ); |
||
509 | 47138 | jjdelcerro | 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 | 45772 | omartinez | |
522 | } catch (Exception ex) { |
||
523 | 46984 | fdiaz | LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.getFieldName() + "' from table '" + this.getTableName() + "' ("+DataStore.getFullNameQuietly(foreingStore)+").", ex); |
524 | 45772 | omartinez | } finally {
|
525 | 45946 | omartinez | this.loadAvailableValues = false; |
526 | DisposeUtils.disposeQuietly(foreingStore); |
||
527 | 45739 | jjdelcerro | } |
528 | 44262 | jjdelcerro | } |
529 | 44338 | jjdelcerro | return this.availableValues; |
530 | 44262 | jjdelcerro | } |
531 | 44338 | jjdelcerro | |
532 | 46938 | jjdelcerro | private DynObjectValueItem[] getAvailableValuesFromStore(FeatureStore store) { |
533 | 47049 | jjdelcerro | DisposableFeatureSetIterable set = null;
|
534 | 46938 | jjdelcerro | 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 | 46984 | fdiaz | LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.getFieldName() + "' from table '" + this.getTableName() + "'(" + DataStore.getFullNameQuietly(store) + ")."); |
544 | 46938 | jjdelcerro | } |
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 | 46984 | fdiaz | LOGGER.warn("Can't get available values for field '" + this.getStoreName() + "." + this.getFieldName() + "' from table '" + this.getTableName() + "' (" + DataStore.getFullNameQuietly(store) + ").", ex); |
569 | 46938 | jjdelcerro | } finally {
|
570 | DisposeUtils.disposeQuietly(set); |
||
571 | } |
||
572 | return values;
|
||
573 | } |
||
574 | |||
575 | 45772 | omartinez | private String getStoreName() { |
576 | if (this.descriptor==null) { |
||
577 | return "Unknown"; |
||
578 | } |
||
579 | 46782 | fdiaz | if (this.descriptor.getStore()==null) { |
580 | return "Unknown"; |
||
581 | } |
||
582 | 45772 | omartinez | return this.descriptor.getStore().getName(); |
583 | } |
||
584 | |||
585 | 44363 | jjdelcerro | @Override
|
586 | 44338 | jjdelcerro | public String getLabelForValue(Object value) { |
587 | DynObjectValueItem[] values = this.getAvailableValues(null); |
||
588 | 45772 | omartinez | if (values != null) { |
589 | 44338 | jjdelcerro | for (DynObjectValueItem value1 : values) {
|
590 | 45772 | omartinez | if (Objects.equals(value, value1.getValue())) {
|
591 | 44338 | jjdelcerro | return value1.getLabel();
|
592 | } |
||
593 | } |
||
594 | } |
||
595 | return Objects.toString(value, "##ERROR##"); |
||
596 | } |
||
597 | 44262 | jjdelcerro | |
598 | @Override
|
||
599 | public void loadFromState(PersistentState state) |
||
600 | throws PersistenceException {
|
||
601 | foreingKey = state.getBoolean("foreingKey");
|
||
602 | 44338 | jjdelcerro | closedList = state.getBoolean("selectable");
|
603 | 44262 | jjdelcerro | labelFormula = state.getString("labelFormula");
|
604 | codeName = state.getString("codeName");
|
||
605 | tableName = state.getString("tableName");
|
||
606 | 44363 | jjdelcerro | ensureReferentialIntegrity = state.getBoolean("ensureReferentialIntegrity");
|
607 | 44262 | jjdelcerro | } |
608 | |||
609 | @Override
|
||
610 | public void saveToState(PersistentState state) throws PersistenceException { |
||
611 | state.set("foreingKey", foreingKey);
|
||
612 | 44338 | jjdelcerro | state.set("selectable", closedList);
|
613 | 44262 | jjdelcerro | state.set("labelFormula", labelFormula);
|
614 | state.set("codeName", codeName);
|
||
615 | state.set("tableName", tableName);
|
||
616 | 44363 | jjdelcerro | state.set("ensureReferentialIntegrity", ensureReferentialIntegrity);
|
617 | 44262 | jjdelcerro | } |
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 | 44363 | jjdelcerro | definition.addDynFieldBoolean("ensureReferentialIntegrity");
|
639 | 44262 | jjdelcerro | } |
640 | } |
||
641 | |||
642 | @Override
|
||
643 | public ForeingKey clone() throws CloneNotSupportedException { |
||
644 | DefaultForeingKey other = (DefaultForeingKey) super.clone();
|
||
645 | return other;
|
||
646 | } |
||
647 | |||
648 | 44363 | jjdelcerro | @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 | 45772 | omartinez | |
658 | 45258 | omartinez | @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 | 45772 | omartinez | if (this.availableValues == null) { |
673 | 45258 | omartinez | this.getAvailableValues(null); |
674 | } |
||
675 | return this.availableValues != null; |
||
676 | } |
||
677 | 45772 | omartinez | |
678 | 46938 | jjdelcerro | public StoresRepository getStoresRepository(String tableName) { |
679 | StoresRepository repository = this.getStoresRepository();
|
||
680 | 46984 | fdiaz | FeatureStore store = null;
|
681 | if(descriptor != null) { |
||
682 | store = descriptor.getStore(); |
||
683 | } |
||
684 | |||
685 | 46938 | jjdelcerro | if( store==null ) { |
686 | return repository;
|
||
687 | } else {
|
||
688 | try {
|
||
689 | DataManager dataManager = DALLocator.getDataManager(); |
||
690 | DataServerExplorer explorer = store.getExplorer(); |
||
691 | StoresRepository repo2 = dataManager.getStoresRepository().getSubrepository(explorer); |
||
692 | if( repo2!=null && repo2.contains(tableName)) { |
||
693 | return repo2;
|
||
694 | } |
||
695 | DataStoreParameters params = explorer.get(tableName); |
||
696 | if( explorer.exists(params) ) {
|
||
697 | 46940 | fdiaz | String id = getRepositoryId(store, tableName); //tableName+"-"+dataManager.createUniqueID().replace("-", ""); |
698 | 46938 | jjdelcerro | BaseStoresRepository repo3 = new BaseStoresRepository(id, null, explorer.getParameters()); |
699 | repo3.add(tableName, params); |
||
700 | return repo3;
|
||
701 | } |
||
702 | } catch (Exception ex) { |
||
703 | LOGGER.info("Can't access to table '"+tableName+"' near store "+store.getFullName()+", search in repositories.",ex); |
||
704 | } |
||
705 | return repository;
|
||
706 | } |
||
707 | } |
||
708 | 46940 | fdiaz | |
709 | private String getRepositoryId(FeatureStore store, String tableName) { |
||
710 | String id;
|
||
711 | if(store == null){ |
||
712 | DataManager dataManager = DALLocator.getDataManager(); |
||
713 | id = tableName+"-"+dataManager.createUniqueID().replace("-", ""); |
||
714 | } else {
|
||
715 | id = tableName + "-" + DigestUtils.md5Hex(store.getFullName());
|
||
716 | } |
||
717 | return id;
|
||
718 | } |
||
719 | 46938 | jjdelcerro | |
720 | 44262 | jjdelcerro | } |