svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / searchpanel / SearchConditionFieldController.java @ 44748
History | View | Annotate | Download (22.5 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.searchpanel; |
---|---|
2 |
|
3 |
import java.awt.Cursor; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ItemEvent; |
6 |
import java.awt.event.ItemListener; |
7 |
import java.awt.event.MouseAdapter; |
8 |
import java.awt.event.MouseEvent; |
9 |
import java.util.ArrayList; |
10 |
import java.util.HashMap; |
11 |
import java.util.List; |
12 |
import java.util.Map; |
13 |
import java.util.Objects; |
14 |
import java.util.UUID; |
15 |
import javax.swing.ComboBoxModel; |
16 |
import javax.swing.DefaultComboBoxModel; |
17 |
import javax.swing.ImageIcon; |
18 |
import javax.swing.JComboBox; |
19 |
import javax.swing.JLabel; |
20 |
import javax.swing.JOptionPane; |
21 |
import javax.swing.SwingUtilities; |
22 |
import org.apache.commons.lang3.StringUtils; |
23 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
24 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
25 |
import org.gvsig.fmap.dal.DataManager; |
26 |
import org.gvsig.fmap.dal.DataStore; |
27 |
import org.gvsig.fmap.dal.complements.Search; |
28 |
import org.gvsig.fmap.dal.exception.DataException; |
29 |
import org.gvsig.fmap.dal.feature.Feature; |
30 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
31 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
32 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.dal.feature.FeatureType; |
35 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
36 |
import org.gvsig.fmap.dal.swing.impl.featuretype.DefaultFeatureAttributeSelectionPanel; |
37 |
import static org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel.getAttributeDescriptorLabel; |
38 |
import org.gvsig.tools.ToolsLocator; |
39 |
import org.gvsig.tools.dataTypes.Coercion; |
40 |
import org.gvsig.tools.dataTypes.CoercionException; |
41 |
import org.gvsig.tools.dynobject.DynField; |
42 |
import org.gvsig.tools.exception.BaseException; |
43 |
import org.gvsig.tools.i18n.I18nManager; |
44 |
import org.gvsig.tools.swing.api.DropDown; |
45 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
46 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
47 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
48 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
49 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
50 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
51 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
52 |
import org.gvsig.tools.util.LabeledValue; |
53 |
import org.gvsig.tools.util.LabeledValueImpl; |
54 |
import org.gvsig.tools.visitor.VisitCanceledException; |
55 |
import org.gvsig.tools.visitor.Visitor; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
/**
|
60 |
*
|
61 |
* @author jjdelcerro
|
62 |
*/
|
63 |
@SuppressWarnings("UseSpecificCatch") |
64 |
public class SearchConditionFieldController { |
65 |
|
66 |
private static final Logger LOGGER = LoggerFactory.getLogger(SearchConditionFieldController.class); |
67 |
|
68 |
private static class Field extends LabeledValueImpl<String> { |
69 |
|
70 |
FeatureAttributeDescriptor attrdesc; |
71 |
private final FeatureStore store; |
72 |
private final int presentationMode; |
73 |
private final boolean showStoreName; |
74 |
private final FeatureAttributeDescriptor[] path; |
75 |
|
76 |
public Field(FeatureAttributeDescriptor[] path, FeatureStore store, FeatureAttributeDescriptor attrdesc, int presentationMode) { |
77 |
this(path, store, attrdesc, presentationMode, false); |
78 |
} |
79 |
|
80 |
public Field( |
81 |
FeatureAttributeDescriptor[] path,
|
82 |
FeatureStore store, |
83 |
FeatureAttributeDescriptor attrdesc, |
84 |
int presentationMode,
|
85 |
boolean showStoreName
|
86 |
) { |
87 |
super(
|
88 |
getAttributeDescriptorLabel(attrdesc, store.getName()), |
89 |
attrdesc.getName() |
90 |
); |
91 |
this.path = path;
|
92 |
this.store = store;
|
93 |
this.attrdesc = attrdesc;
|
94 |
this.presentationMode = presentationMode;
|
95 |
this.showStoreName = showStoreName;
|
96 |
} |
97 |
|
98 |
public FeatureAttributeDescriptor[] getPath() { |
99 |
return this.path; |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public String getLabel() { |
104 |
String theLabel = getAttributeDescriptorLabel(attrdesc, showStoreName ? store.getName() : null); |
105 |
switch (this.presentationMode) { |
106 |
case Search.OrderedAttribute.TYPE_REGURAL:
|
107 |
break;
|
108 |
case Search.OrderedAttribute.TYPE_FAVORITE:
|
109 |
theLabel = "<html><b>" + theLabel + "</b></html>"; |
110 |
break;
|
111 |
case Search.OrderedAttribute.TYPE_RECENT:
|
112 |
theLabel = "<html><i><b>" + theLabel + "</b></i></html>"; |
113 |
break;
|
114 |
} |
115 |
return theLabel;
|
116 |
} |
117 |
|
118 |
public FeatureAttributeDescriptor getParentDescriptor() {
|
119 |
int l = this.path.length; |
120 |
if( l < 2 ) { |
121 |
return null; |
122 |
} |
123 |
return this.path[l-2]; |
124 |
} |
125 |
|
126 |
public FeatureAttributeDescriptor getDescriptor() {
|
127 |
return this.attrdesc; |
128 |
} |
129 |
|
130 |
public FeatureStore getFeatureStore() {
|
131 |
return this.store; |
132 |
} |
133 |
|
134 |
} |
135 |
|
136 |
private FeatureStore store;
|
137 |
private final JLabel lblFields; |
138 |
private final JLabel lblExtraFields; |
139 |
private final JLabel lblLogicalOperators; |
140 |
private final JLabel lblRelationalOperators; |
141 |
private final JComboBox cboValue; |
142 |
private Object valueAssigned = null; |
143 |
|
144 |
private DropDown ddnFields;
|
145 |
private DropDown ddnLogicalOperators;
|
146 |
private DropDown ddnRelationalOperators;
|
147 |
|
148 |
private LabeledValue[] relationalOperators; |
149 |
private LabeledValue[] logicalOperators; |
150 |
|
151 |
public SearchConditionFieldController(
|
152 |
FeatureStore store, |
153 |
JLabel lblFields,
|
154 |
JLabel lblExtraFields,
|
155 |
JLabel lblRelationalOperators,
|
156 |
JComboBox cboValue,
|
157 |
JLabel lblLogicalOperators
|
158 |
) { |
159 |
this.store = store;
|
160 |
this.lblFields = lblFields;
|
161 |
this.lblExtraFields = lblExtraFields;
|
162 |
this.lblRelationalOperators = lblRelationalOperators;
|
163 |
this.cboValue = cboValue;
|
164 |
this.lblLogicalOperators = lblLogicalOperators;
|
165 |
this.initComponents();
|
166 |
} |
167 |
|
168 |
public boolean isAValidRelationOperator(String name) { |
169 |
for (LabeledValue relationalOperator : relationalOperators) {
|
170 |
if (StringUtils.equalsIgnoreCase(name, (CharSequence) relationalOperator.getValue())) { |
171 |
return true; |
172 |
} |
173 |
} |
174 |
return false; |
175 |
} |
176 |
|
177 |
private void initComponents() { |
178 |
try {
|
179 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
180 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
181 |
|
182 |
relationalOperators = new LabeledValue[]{ |
183 |
new LabeledValueImpl(i18n.getTranslation("_Equals_to"), ExpressionBuilder.OPERATOR_EQ), |
184 |
new LabeledValueImpl(i18n.getTranslation("_Like_to"), ExpressionBuilder.OPERATOR_ILIKE), |
185 |
new LabeledValueImpl(i18n.getTranslation("_Not_equals_to"), ExpressionBuilder.OPERATOR_NE), |
186 |
new LabeledValueImpl(i18n.getTranslation("_Greater_than"), ExpressionBuilder.OPERATOR_GT), |
187 |
new LabeledValueImpl(i18n.getTranslation("_Greater_or_equal_to"), ExpressionBuilder.OPERATOR_GE), |
188 |
new LabeledValueImpl(i18n.getTranslation("_Less_than"), ExpressionBuilder.OPERATOR_LT), |
189 |
new LabeledValueImpl(i18n.getTranslation("_Less_or_equal_to"), ExpressionBuilder.OPERATOR_LE) |
190 |
}; |
191 |
|
192 |
logicalOperators = new LabeledValue[]{ |
193 |
new LabeledValueImpl(i18n.getTranslation("_Or"), ExpressionBuilder.OPERATOR_OR), |
194 |
new LabeledValueImpl(i18n.getTranslation("_And"), ExpressionBuilder.OPERATOR_AND) |
195 |
}; |
196 |
|
197 |
this.lblExtraFields.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
198 |
|
199 |
this.ddnFields = toolsSwingManager.createDropDown(lblFields);
|
200 |
this.ddnFields.setVisibleDropdownArrow(false); |
201 |
this.ddnRelationalOperators = toolsSwingManager.createDropDown(lblRelationalOperators);
|
202 |
this.ddnRelationalOperators.setVisibleDropdownArrow(false); |
203 |
if (lblLogicalOperators != null) { |
204 |
this.ddnLogicalOperators = toolsSwingManager.createDropDown(lblLogicalOperators);
|
205 |
this.ddnLogicalOperators.setVisibleDropdownArrow(false); |
206 |
} |
207 |
|
208 |
DefaultComboBoxModel modelRelationalOperators = new DefaultComboBoxModel(); |
209 |
for (LabeledValue op : relationalOperators) {
|
210 |
modelRelationalOperators.addElement(op); |
211 |
} |
212 |
this.ddnRelationalOperators.setModel(modelRelationalOperators);
|
213 |
|
214 |
if (this.ddnLogicalOperators != null) { |
215 |
DefaultComboBoxModel modelLogicalOperators = new DefaultComboBoxModel(); |
216 |
for (LabeledValue op : logicalOperators) {
|
217 |
modelLogicalOperators.addElement(op); |
218 |
} |
219 |
this.ddnLogicalOperators.setModel(modelLogicalOperators);
|
220 |
} |
221 |
FeatureType featureType = store.getDefaultFeatureType(); |
222 |
Search search = (Search) ToolsLocator.getComplementsManager().get( |
223 |
Search.COMPLEMENT_MANE, featureType |
224 |
); |
225 |
List<Search.OrderedAttribute> orderedAttributes = search.getOrderedAttributes(
|
226 |
Search.BASIC_TYPES_FILTER, |
227 |
Search.STR_INT_LONG_LABEL_ORDER, |
228 |
20
|
229 |
); |
230 |
List<ImageIcon> icons = new ArrayList<>(); |
231 |
// DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
|
232 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
233 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
234 |
for (Search.OrderedAttribute attr : orderedAttributes) {
|
235 |
FeatureAttributeDescriptor attrdesc = attr.getDescriptor(); |
236 |
Field field = new Field( |
237 |
new FeatureAttributeDescriptor[] { attrdesc }, |
238 |
this.store,
|
239 |
attrdesc, |
240 |
attr.getType() |
241 |
); |
242 |
model.addElement(field); |
243 |
String iconName = attrdesc.getDataType().getIconName();
|
244 |
if (iconTheme.exists(iconName)) {
|
245 |
icons.add(iconTheme.get(iconName)); |
246 |
} else {
|
247 |
icons.add(null);
|
248 |
} |
249 |
} |
250 |
this.ddnFields.setIcons(icons);
|
251 |
this.ddnFields.setModel(model);
|
252 |
this.ddnFields.addItemListener(new ItemListener() { |
253 |
@Override
|
254 |
public void itemStateChanged(ItemEvent e) { |
255 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
256 |
doUpdateValuesList(); |
257 |
} |
258 |
|
259 |
} |
260 |
}); |
261 |
|
262 |
this.lblExtraFields.addMouseListener(new MouseAdapter() { |
263 |
@Override
|
264 |
public void mouseClicked(MouseEvent e) { |
265 |
doSelectMoreFields(); |
266 |
} |
267 |
}); |
268 |
clear(); |
269 |
} catch (Exception ex) { |
270 |
throw new RuntimeException(ex); |
271 |
} |
272 |
} |
273 |
|
274 |
private FeatureType getFeatureType() {
|
275 |
try {
|
276 |
return this.store.getDefaultFeatureType(); |
277 |
} catch (DataException ex) {
|
278 |
return null; |
279 |
} |
280 |
} |
281 |
|
282 |
private void doSelectMoreFields() { |
283 |
DefaultFeatureAttributeSelectionPanel panel = new DefaultFeatureAttributeSelectionPanel(store);
|
284 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
285 |
final Dialog dialog = winManager.createDialog( |
286 |
panel, |
287 |
"Select attribute",
|
288 |
null,
|
289 |
WindowManager_v2.BUTTONS_OK_CANCEL |
290 |
); |
291 |
dialog.addActionListener((ActionEvent e) -> {
|
292 |
if (dialog.getAction() == WindowManager_v2.BUTTONS_OK) {
|
293 |
doAddAndSelect( |
294 |
panel.getSelectedStore(), |
295 |
panel.getSelectedAttributeDescriptor(), |
296 |
panel.getSelectedPath() |
297 |
); |
298 |
} |
299 |
}); |
300 |
dialog.show(WindowManager.MODE.DIALOG); |
301 |
|
302 |
} |
303 |
|
304 |
private void doAddAndSelect(FeatureStore theStore, FeatureAttributeDescriptor attrdesc, FeatureAttributeDescriptor[] path) { |
305 |
DefaultComboBoxModel<Field> model = (DefaultComboBoxModel) this.ddnFields.getModel(); |
306 |
for (int i = 0; i < model.getSize(); i++) { |
307 |
Field field = model.getElementAt(i);
|
308 |
FeatureAttributeDescriptor attrdescN = field.getDescriptor(); |
309 |
if ( isTheSameStore(theStore, attrdescN.getStore())
|
310 |
&& StringUtils.equalsIgnoreCase(attrdesc.getName(), attrdescN.getName())) { |
311 |
this.setAttribute(i);
|
312 |
return;
|
313 |
} |
314 |
} |
315 |
Field field = new Field( |
316 |
path, |
317 |
theStore, |
318 |
attrdesc, |
319 |
Search.OrderedAttribute.TYPE_REGURAL, |
320 |
!isTheSameStore(store, theStore) |
321 |
); |
322 |
ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
323 |
if( field.getPath().length>2 ) { |
324 |
dialogManager.messageDialog( |
325 |
"It not supported to search through this field." + "\n" |
326 |
+ "Too many links.",
|
327 |
"_Warning",
|
328 |
JOptionPane.WARNING_MESSAGE
|
329 |
); |
330 |
return;
|
331 |
} |
332 |
FeatureAttributeDescriptor parentDescriptor = field.getParentDescriptor(); |
333 |
switch(parentDescriptor.getRelationType()) {
|
334 |
case DynField.RELATION_TYPE_AGGREGATE:
|
335 |
case DynField.RELATION_TYPE_COMPOSITION:
|
336 |
if( getForeingKeyName(field.getFeatureStore(), this.store)==null ) { |
337 |
dialogManager.messageDialog( |
338 |
"It not supported to search through this field." + "\n" |
339 |
+ "The link field was not found.",
|
340 |
"_Warning",
|
341 |
JOptionPane.WARNING_MESSAGE
|
342 |
); |
343 |
return;
|
344 |
} |
345 |
if( getPrimaryKeyName(this.store)==null ) { |
346 |
dialogManager.messageDialog( |
347 |
"It not supported to search through this field." + "\n" |
348 |
+ "A simple primary key was not found.",
|
349 |
"_Warning",
|
350 |
JOptionPane.WARNING_MESSAGE
|
351 |
); |
352 |
return;
|
353 |
} |
354 |
} |
355 |
model.addElement(field); |
356 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
357 |
this.ddnFields.getIcons().add(iconTheme.get(attrdesc.getDataType().getIconName()));
|
358 |
this.setAttribute(model.getSize() - 1); |
359 |
} |
360 |
|
361 |
public void clear() { |
362 |
this.ddnRelationalOperators.setSelectedIndex(0); |
363 |
if (this.ddnLogicalOperators != null) { |
364 |
this.ddnLogicalOperators.setSelectedIndex(0); |
365 |
} |
366 |
this.cboValue.setSelectedIndex(-1); |
367 |
} |
368 |
|
369 |
private void doUpdateValuesList() { |
370 |
final Field field = (Field) this.ddnFields.getSelectedItem(); |
371 |
if (field == null) { |
372 |
return;
|
373 |
} |
374 |
|
375 |
final List<Object> values = new ArrayList<>(); |
376 |
final int limit = 60; |
377 |
final long timeLimit = System.currentTimeMillis() + limit * 1000; |
378 |
final DefaultComboBoxModel model = new DefaultComboBoxModel(); |
379 |
this.setEnabled(false); |
380 |
final FeatureStore theStore = field.getFeatureStore();
|
381 |
final FeatureQuery query = theStore.createFeatureQuery();
|
382 |
query.addAttributeName(field.getDescriptor().getName()); |
383 |
query.setLimit(1000);
|
384 |
Thread th = new Thread(new Runnable() { |
385 |
@Override
|
386 |
public void run() { |
387 |
try {
|
388 |
FeatureSet set = theStore.getFeatureSet(query); |
389 |
set.accept(new Visitor() {
|
390 |
@Override
|
391 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
392 |
Object value = ((Feature) o).get(field.getDescriptor().getName());
|
393 |
if (!values.contains(value)) {
|
394 |
values.add(value); |
395 |
} |
396 |
if (System.currentTimeMillis() > timeLimit) { |
397 |
throw new VisitCanceledException(); |
398 |
} |
399 |
if (values.size() > 1000) { |
400 |
throw new VisitCanceledException(); |
401 |
} |
402 |
} |
403 |
}); |
404 |
} catch (VisitCanceledException ex) {
|
405 |
|
406 |
} catch (Exception ex) { |
407 |
LOGGER.warn("Can't update list of values of '" + field.getLabel() + "'.", ex); |
408 |
} |
409 |
List<LabeledValue> elements = new ArrayList<>(); |
410 |
if (!values.isEmpty()) {
|
411 |
LabeledValue[] availableValues = field.getDescriptor().getAvailableValues();
|
412 |
Map<String, String> availableValuesMap = new HashMap<>(); |
413 |
if (availableValues != null) { |
414 |
for (LabeledValue availableValue : availableValues) {
|
415 |
availableValuesMap.put( |
416 |
Objects.toString(availableValue.getValue()), |
417 |
availableValue.getLabel() |
418 |
); |
419 |
} |
420 |
} |
421 |
elements.add(new LabeledValueImpl("", null)); |
422 |
for (Object value : values) { |
423 |
String key = Objects.toString(value);
|
424 |
String label = availableValuesMap.getOrDefault(key, key);
|
425 |
elements.add(new LabeledValueImpl(label, value));
|
426 |
} |
427 |
elements.sort(null);
|
428 |
|
429 |
} |
430 |
for (LabeledValue element : elements) {
|
431 |
model.addElement(element); |
432 |
} |
433 |
SwingUtilities.invokeLater(new Runnable() { |
434 |
@Override
|
435 |
public void run() { |
436 |
cboValue.setModel(model); |
437 |
if (valueAssigned != null) { |
438 |
cboValue.setSelectedItem(valueAssigned); |
439 |
valueAssigned = null;
|
440 |
} |
441 |
setEnabled(true);
|
442 |
} |
443 |
}); |
444 |
} |
445 |
}); |
446 |
th.start(); |
447 |
} |
448 |
|
449 |
public void setEnabled(boolean enabled) { |
450 |
this.ddnFields.setEnabled(enabled);
|
451 |
if (this.ddnLogicalOperators != null) { |
452 |
this.ddnLogicalOperators.setEnabled(enabled);
|
453 |
} |
454 |
this.ddnRelationalOperators.setEnabled(enabled);
|
455 |
this.lblExtraFields.setEnabled(enabled);
|
456 |
} |
457 |
|
458 |
public String getRelationalOperator() { |
459 |
LabeledValue<String> op = (LabeledValue) this.ddnRelationalOperators.getSelectedItem(); |
460 |
if (op == null) { |
461 |
return null; |
462 |
} |
463 |
return op.getValue();
|
464 |
} |
465 |
|
466 |
public int setRelationalOperator(String name) { |
467 |
int n = 0; |
468 |
for (LabeledValue relationalOperator : relationalOperators) {
|
469 |
if (StringUtils.equalsIgnoreCase(name, (CharSequence) relationalOperator.getValue())) { |
470 |
break;
|
471 |
} |
472 |
n++; |
473 |
} |
474 |
if (this.relationalOperators.length <= n) { |
475 |
return -1; |
476 |
} |
477 |
this.ddnRelationalOperators.setSelectedIndex(n);
|
478 |
return n;
|
479 |
} |
480 |
|
481 |
public String getLogicalOperator() { |
482 |
if (this.ddnLogicalOperators == null) { |
483 |
return null; |
484 |
} |
485 |
LabeledValue<String> rel = (LabeledValue) this.ddnLogicalOperators.getSelectedItem(); |
486 |
if (rel == null) { |
487 |
return null; |
488 |
} |
489 |
return rel.getValue();
|
490 |
} |
491 |
|
492 |
public Object getValue() { |
493 |
final Field field = (Field) this.ddnFields.getSelectedItem(); |
494 |
if (field == null) { |
495 |
return null; |
496 |
} |
497 |
Object v = this.cboValue.getSelectedItem(); |
498 |
if (v == null) { |
499 |
return null; |
500 |
} |
501 |
if (v instanceof LabeledValue) { |
502 |
v = ((LabeledValue) v).getValue(); |
503 |
if (v == null) { |
504 |
return null; |
505 |
} |
506 |
} |
507 |
if (v instanceof CharSequence) { |
508 |
if (StringUtils.isBlank((CharSequence) v)) { |
509 |
return null; |
510 |
} |
511 |
} |
512 |
Coercion coercion = field.getDescriptor().getDataType().getCoercion(); |
513 |
try {
|
514 |
return coercion.coerce(v);
|
515 |
} catch (CoercionException ex) {
|
516 |
return null; |
517 |
} |
518 |
} |
519 |
|
520 |
public void setValue(Object value) { |
521 |
this.cboValue.setSelectedItem(value);
|
522 |
this.valueAssigned = value;
|
523 |
} |
524 |
|
525 |
private Field getCurrentField() { |
526 |
final Field field = (Field) this.ddnFields.getSelectedItem(); |
527 |
return field;
|
528 |
} |
529 |
|
530 |
public int setAttribute(String name) { |
531 |
ComboBoxModel<Field> model = this.ddnFields.getModel(); |
532 |
for (int i = 0; i < model.getSize(); i++) { |
533 |
Field x = model.getElementAt(i);
|
534 |
if (StringUtils.equalsIgnoreCase(name, x.getValue())) {
|
535 |
this.setAttribute(i);
|
536 |
return i;
|
537 |
} |
538 |
} |
539 |
this.setAttribute(-1); |
540 |
return -1; |
541 |
} |
542 |
|
543 |
public void setAttribute(int index) { |
544 |
try {
|
545 |
this.ddnFields.setSelectedIndex(index);
|
546 |
} catch (Exception ex) { |
547 |
this.ddnFields.setSelectedIndex(-1); |
548 |
} |
549 |
doUpdateValuesList(); |
550 |
} |
551 |
|
552 |
private boolean isTheSameStore(DataStore store1, DataStore store2) { |
553 |
String store1FullName = store1.getFullName();
|
554 |
String store2FullName = store2.getFullName();
|
555 |
return StringUtils.equalsIgnoreCase(store1FullName, store2FullName);
|
556 |
} |
557 |
|
558 |
private String getPrimaryKeyName(FeatureStore store) { |
559 |
try {
|
560 |
FeatureAttributeDescriptor[] pk = store.getDefaultFeatureType().getPrimaryKey();
|
561 |
if( pk == null || pk.length!=1 ) { |
562 |
return null; |
563 |
} |
564 |
return pk[0].getName(); |
565 |
} catch (DataException ex) {
|
566 |
return null; |
567 |
} |
568 |
} |
569 |
|
570 |
private String getForeingKeyName(FeatureStore store, FeatureStore foreingStore) { |
571 |
try {
|
572 |
for (FeatureAttributeDescriptor descriptor : store.getDefaultFeatureType()) {
|
573 |
if( descriptor.isForeingKey() ) {
|
574 |
ForeingKey foreingKey = descriptor.getForeingKey(); |
575 |
if( isTheSameStore(foreingStore, foreingKey.getFeatureStore(null)) ) { |
576 |
return descriptor.getName();
|
577 |
} |
578 |
} |
579 |
} |
580 |
} catch (DataException ex) {
|
581 |
return null; |
582 |
} |
583 |
return null; |
584 |
} |
585 |
|
586 |
public ExpressionBuilder.Value getFilter() {
|
587 |
ExpressionBuilder.Value filter = null;
|
588 |
|
589 |
Object value = this.getValue(); |
590 |
if( value == null ) { |
591 |
return null; |
592 |
} |
593 |
Field field = this.getCurrentField(); |
594 |
if( field == null ) { |
595 |
return filter;
|
596 |
} |
597 |
if( field.getPath().length>2 ) { |
598 |
// No soportado
|
599 |
return null; |
600 |
} |
601 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
602 |
FeatureAttributeDescriptor parentDescriptor = field.getParentDescriptor(); |
603 |
FeatureAttributeDescriptor descriptor = field.getDescriptor(); |
604 |
if( parentDescriptor==null ) { |
605 |
filter = builder.binaryOperator( |
606 |
this.getRelationalOperator(),
|
607 |
builder.column(descriptor.getName()), |
608 |
builder.constant(value) |
609 |
); |
610 |
} else {
|
611 |
switch(parentDescriptor.getRelationType()) {
|
612 |
case DynField.RELATION_TYPE_COLLABORATION:
|
613 |
case DynField.RELATION_TYPE_IDENTITY:
|
614 |
ExpressionBuilder.Function foreing_value = builder.function( |
615 |
DataManager.FUNCTION_FOREING_VALUE, |
616 |
builder.tuple( |
617 |
builder.constant(parentDescriptor.getName()), |
618 |
builder.constant(descriptor.getName()) |
619 |
) |
620 |
); |
621 |
filter = builder.binaryOperator( |
622 |
this.getRelationalOperator(),
|
623 |
builder.column(descriptor.getName()), |
624 |
foreing_value |
625 |
); |
626 |
break;
|
627 |
|
628 |
|
629 |
case DynField.RELATION_TYPE_AGGREGATE:
|
630 |
case DynField.RELATION_TYPE_COMPOSITION:
|
631 |
ExpressionBuilder.Function select = builder.function("SELECT");
|
632 |
select.parameter( |
633 |
"TABLE",
|
634 |
builder.constant(field.getFeatureStore().getName()) |
635 |
); |
636 |
select.parameter( |
637 |
"WHERE",
|
638 |
builder.and( |
639 |
builder.eq( |
640 |
builder.getattr( |
641 |
builder.column(field.getFeatureStore().getName()), |
642 |
getForeingKeyName(field.getFeatureStore(), this.store)
|
643 |
), |
644 |
builder.getattr( |
645 |
builder.column(this.store.getName()),
|
646 |
getPrimaryKeyName(this.store)
|
647 |
) |
648 |
), |
649 |
builder.binaryOperator( |
650 |
this.getRelationalOperator(),
|
651 |
builder.column(descriptor.getName()), |
652 |
builder.constant(this.getValue())
|
653 |
) |
654 |
) |
655 |
); |
656 |
select.parameter("LIMIT",builder.constant(1)); |
657 |
|
658 |
String exists_id = "EXISTS"+UUID.randomUUID().toString().replaceAll("-", ""); |
659 |
filter = builder.function("EXISTS", select, builder.constant(exists_id));
|
660 |
break;
|
661 |
} |
662 |
} |
663 |
return filter;
|
664 |
} |
665 |
} |