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 @ 46501
History | View | Annotate | Download (54.9 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.text.DateFormat; |
10 |
import java.util.ArrayList; |
11 |
import java.util.Arrays; |
12 |
import java.util.Date; |
13 |
import java.util.HashMap; |
14 |
import java.util.List; |
15 |
import java.util.Locale; |
16 |
import java.util.Map; |
17 |
import java.util.Objects; |
18 |
import javax.json.Json; |
19 |
import javax.json.JsonArray; |
20 |
import javax.json.JsonArrayBuilder; |
21 |
import javax.json.JsonObject; |
22 |
import javax.json.JsonObjectBuilder; |
23 |
import javax.swing.ComboBoxModel; |
24 |
import javax.swing.DefaultComboBoxModel; |
25 |
import javax.swing.ImageIcon; |
26 |
import javax.swing.JComboBox; |
27 |
import javax.swing.JLabel; |
28 |
import javax.swing.JOptionPane; |
29 |
import javax.swing.SwingUtilities; |
30 |
import javax.swing.text.JTextComponent; |
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
33 |
import org.gvsig.fmap.dal.DALLocator; |
34 |
import org.gvsig.fmap.dal.DataManager; |
35 |
import org.gvsig.fmap.dal.DataStore; |
36 |
import org.gvsig.fmap.dal.complements.Search; |
37 |
import org.gvsig.fmap.dal.exception.DataException; |
38 |
import org.gvsig.fmap.dal.expressionevaluator.DALExpressionBuilder; |
39 |
import org.gvsig.fmap.dal.feature.Feature; |
40 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
42 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
46 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
47 |
import org.gvsig.fmap.dal.swing.impl.featuretype.DefaultFeatureAttributeSelectionPanel; |
48 |
import static org.gvsig.fmap.dal.swing.impl.searchpanel.SearchConditionPanelSimplified.PANEL_NAME; |
49 |
import static org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel.NOT_HANDLE_NULL; |
50 |
import static org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel.NULL_AS_FALSE; |
51 |
import static org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel.NULL_AS_TRUE; |
52 |
import org.gvsig.fmap.dal.swing.searchpanel.SearchParameters; |
53 |
import org.gvsig.tools.ToolsLocator; |
54 |
import org.gvsig.tools.dataTypes.Coercion; |
55 |
import org.gvsig.tools.dataTypes.CoercionException; |
56 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
57 |
import org.gvsig.tools.dataTypes.DataTypes; |
58 |
import org.gvsig.tools.dispose.DisposeUtils; |
59 |
import org.gvsig.tools.dynobject.DynField; |
60 |
import org.gvsig.tools.exception.BaseException; |
61 |
import org.gvsig.tools.i18n.I18nManager; |
62 |
import org.gvsig.tools.swing.api.DropDown; |
63 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
64 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
65 |
import org.gvsig.tools.swing.api.pickercontroller.DatePickerController; |
66 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
67 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
68 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
69 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
70 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
71 |
import org.gvsig.tools.util.LabeledValue; |
72 |
import org.gvsig.tools.util.LabeledValueImpl; |
73 |
import org.gvsig.tools.visitor.VisitCanceledException; |
74 |
import org.gvsig.tools.visitor.Visitor; |
75 |
import org.slf4j.Logger; |
76 |
import org.slf4j.LoggerFactory; |
77 |
|
78 |
/**
|
79 |
*
|
80 |
* @author jjdelcerro
|
81 |
*/
|
82 |
@SuppressWarnings("UseSpecificCatch") |
83 |
public class SearchConditionFieldController { |
84 |
|
85 |
private static final Logger LOGGER = LoggerFactory.getLogger(SearchConditionFieldController.class); |
86 |
private static final Class LOAD_MORE_ELEMENTS = SearchConditionFieldController.class; |
87 |
|
88 |
private static final int MAXLEN_IN_COMBOS = 70; |
89 |
|
90 |
private static class Field extends LabeledValueImpl<String> { |
91 |
|
92 |
FeatureAttributeDescriptor attrdesc; |
93 |
private final FeatureStore store; |
94 |
private final int presentationMode; |
95 |
private final boolean showStoreName; |
96 |
private final FeatureAttributeDescriptor[] path; |
97 |
|
98 |
public Field(FeatureAttributeDescriptor[] path, FeatureStore store, FeatureAttributeDescriptor attrdesc, int presentationMode) { |
99 |
this(path, store, attrdesc, presentationMode, false); |
100 |
} |
101 |
|
102 |
public Field( |
103 |
FeatureAttributeDescriptor[] path,
|
104 |
FeatureStore store, |
105 |
FeatureAttributeDescriptor attrdesc, |
106 |
int presentationMode,
|
107 |
boolean showStoreName
|
108 |
) { |
109 |
super(
|
110 |
DALSwingLocator.getDataSwingManager().getAttributeDescriptorLabel(attrdesc, store.getName()), |
111 |
attrdesc.getName() |
112 |
); |
113 |
this.path = path;
|
114 |
this.store = store;
|
115 |
this.attrdesc = attrdesc;
|
116 |
this.presentationMode = presentationMode;
|
117 |
this.showStoreName = showStoreName;
|
118 |
} |
119 |
|
120 |
public FeatureAttributeDescriptor[] getPath() { |
121 |
return this.path; |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public String getLabel() { |
126 |
String theLabel = DALSwingLocator.getDataSwingManager().getAttributeDescriptorLabel(attrdesc, showStoreName ? store.getName() : null); |
127 |
switch (this.presentationMode) { |
128 |
case Search.OrderedAttribute.TYPE_REGURAL:
|
129 |
break;
|
130 |
case Search.OrderedAttribute.TYPE_FAVORITE:
|
131 |
theLabel = "<html><b>" + theLabel + "</b></html>"; |
132 |
break;
|
133 |
case Search.OrderedAttribute.TYPE_RECENT:
|
134 |
theLabel = "<html><i><b>" + theLabel + "</b></i></html>"; |
135 |
break;
|
136 |
} |
137 |
return theLabel;
|
138 |
} |
139 |
|
140 |
public FeatureAttributeDescriptor getParentDescriptor() {
|
141 |
int l = this.path.length; |
142 |
if (l < 2) { |
143 |
return null; |
144 |
} |
145 |
return this.path[l - 2]; |
146 |
} |
147 |
|
148 |
public FeatureAttributeDescriptor getDescriptor() {
|
149 |
return this.attrdesc; |
150 |
} |
151 |
|
152 |
public FeatureStore getFeatureStore() {
|
153 |
return this.store; |
154 |
} |
155 |
|
156 |
} |
157 |
|
158 |
private FeatureStore store;
|
159 |
private SearchParameters parameters;
|
160 |
private final JLabel lblFields; |
161 |
private final JLabel lblExtraFields; |
162 |
private final JLabel lblLogicalOperators; |
163 |
private final JLabel lblRelationalOperators; |
164 |
private final JComboBox cboValue; |
165 |
private final JLabel lblNull; |
166 |
private Object valueAssigned = null; |
167 |
|
168 |
private DropDown ddnFields;
|
169 |
private DropDown ddnLogicalOperators;
|
170 |
private DropDown ddnRelationalOperators;
|
171 |
private DropDown ddnNullBehavior;
|
172 |
|
173 |
private LabeledValue[] relationalOperators; |
174 |
private LabeledValue[] logicalOperators; |
175 |
private LabeledValue[] nullBehaviors; |
176 |
private ArrayList<ImageIcon> nullOperatorsIcons; |
177 |
|
178 |
private final int SIZE_ORDERED_ATTRIBUTES = 20; |
179 |
private DatePickerController dateController = null; |
180 |
|
181 |
private int updateValuesTimeLimit; |
182 |
private int updateValuesFeaturesLimit; |
183 |
private boolean canHasMoreElements; |
184 |
|
185 |
public SearchConditionFieldController(
|
186 |
SearchParameters parameters, |
187 |
FeatureStore store, |
188 |
JLabel lblFields,
|
189 |
JLabel lblExtraFields,
|
190 |
JLabel lblRelationalOperators,
|
191 |
JComboBox cboValue,
|
192 |
JLabel lblNull,
|
193 |
JLabel lblLogicalOperators
|
194 |
) { |
195 |
this.parameters = parameters;
|
196 |
this.store = store;
|
197 |
this.lblFields = lblFields;
|
198 |
this.lblExtraFields = lblExtraFields;
|
199 |
this.lblRelationalOperators = lblRelationalOperators;
|
200 |
this.cboValue = cboValue;
|
201 |
this.lblNull = lblNull;
|
202 |
this.lblLogicalOperators = lblLogicalOperators;
|
203 |
this.updateValuesTimeLimit = 60; |
204 |
this.updateValuesFeaturesLimit = 1000; |
205 |
this.canHasMoreElements = false; |
206 |
this.initComponents();
|
207 |
} |
208 |
|
209 |
public boolean isAValidRelationOperator(String name) { |
210 |
for (LabeledValue relationalOperator : relationalOperators) {
|
211 |
if (StringUtils.equalsIgnoreCase(name, (CharSequence) relationalOperator.getValue())) { |
212 |
return true; |
213 |
} |
214 |
} |
215 |
return false; |
216 |
} |
217 |
|
218 |
private void initComponents() { |
219 |
try {
|
220 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
221 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
222 |
|
223 |
relationalOperators = new LabeledValue[]{ |
224 |
new LabeledValueImpl(i18n.getTranslation("_Equals_to"), ExpressionBuilder.OPERATOR_EQ), |
225 |
new LabeledValueImpl(i18n.getTranslation("_Like_to"), ExpressionBuilder.OPERATOR_ILIKE), |
226 |
new LabeledValueImpl(i18n.getTranslation("_Not_equals_to"), ExpressionBuilder.OPERATOR_NE), |
227 |
new LabeledValueImpl(i18n.getTranslation("_Greater_than"), ExpressionBuilder.OPERATOR_GT), |
228 |
new LabeledValueImpl(i18n.getTranslation("_Greater_or_equal_to"), ExpressionBuilder.OPERATOR_GE), |
229 |
new LabeledValueImpl(i18n.getTranslation("_Less_than"), ExpressionBuilder.OPERATOR_LT), |
230 |
new LabeledValueImpl(i18n.getTranslation("_Less_or_equal_to"), ExpressionBuilder.OPERATOR_LE), |
231 |
new LabeledValueImpl(i18n.getTranslation("_Is_null"), ExpressionBuilder.OPERATOR_IS_NULL), |
232 |
new LabeledValueImpl(i18n.getTranslation("_Is_not_null"), ExpressionBuilder.OPERATOR_IS_NOT_NULL) |
233 |
}; |
234 |
|
235 |
logicalOperators = new LabeledValue[]{ |
236 |
new LabeledValueImpl(i18n.getTranslation("_Or"), ExpressionBuilder.OPERATOR_OR), |
237 |
new LabeledValueImpl(i18n.getTranslation("_And"), ExpressionBuilder.OPERATOR_AND) |
238 |
}; |
239 |
|
240 |
nullBehaviors = new LabeledValue[]{ |
241 |
new LabeledValueImpl(i18n.getTranslation("_Not_handle_null_values"), NOT_HANDLE_NULL), |
242 |
new LabeledValueImpl(i18n.getTranslation("_Null_values_as_true"), NULL_AS_TRUE), |
243 |
new LabeledValueImpl(i18n.getTranslation("_Null_values_as_false"), NULL_AS_FALSE) |
244 |
}; |
245 |
|
246 |
nullOperatorsIcons = new ArrayList<ImageIcon>(); |
247 |
nullOperatorsIcons.add(ToolsSwingLocator.getIconThemeManager().getCurrent().get("search-nullbehavior-null"));
|
248 |
nullOperatorsIcons.add(ToolsSwingLocator.getIconThemeManager().getCurrent().get("search-nullbehavior-true"));
|
249 |
nullOperatorsIcons.add(ToolsSwingLocator.getIconThemeManager().getCurrent().get("search-nullbehavior-false2"));
|
250 |
|
251 |
this.lblExtraFields.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
252 |
|
253 |
this.ddnFields = toolsSwingManager.createDropDown(lblFields);
|
254 |
this.ddnFields.setVisibleDropdownArrow(false); |
255 |
this.ddnRelationalOperators = toolsSwingManager.createDropDown(lblRelationalOperators);
|
256 |
this.ddnRelationalOperators.setVisibleDropdownArrow(false); |
257 |
if (lblLogicalOperators != null) { |
258 |
this.ddnLogicalOperators = toolsSwingManager.createDropDown(lblLogicalOperators);
|
259 |
this.ddnLogicalOperators.setVisibleDropdownArrow(false); |
260 |
} |
261 |
|
262 |
DefaultComboBoxModel modelRelationalOperators = new DefaultComboBoxModel(); |
263 |
for (LabeledValue op : relationalOperators) {
|
264 |
modelRelationalOperators.addElement(op); |
265 |
} |
266 |
this.ddnRelationalOperators.setModel(modelRelationalOperators);
|
267 |
this.ddnRelationalOperators.addItemListener(new ItemListener() { |
268 |
@Override
|
269 |
public void itemStateChanged(ItemEvent ie) { |
270 |
doUpdateControllerByRelationalOperator(); |
271 |
} |
272 |
}); |
273 |
|
274 |
if (this.ddnLogicalOperators != null) { |
275 |
DefaultComboBoxModel modelLogicalOperators = new DefaultComboBoxModel(); |
276 |
for (LabeledValue op : logicalOperators) {
|
277 |
modelLogicalOperators.addElement(op); |
278 |
} |
279 |
this.ddnLogicalOperators.setModel(modelLogicalOperators);
|
280 |
this.ddnLogicalOperators.setSelectedIndex(1); |
281 |
} |
282 |
|
283 |
// this.ddnNullOperators = new DropDownLabelIcon(lblNull);
|
284 |
this.ddnNullBehavior = toolsSwingManager.createDropDownIcon(lblNull);
|
285 |
this.ddnNullBehavior.setVisibleDropdownArrow(false); |
286 |
DefaultComboBoxModel modelNullOperators = new DefaultComboBoxModel(); |
287 |
for (LabeledValue op : nullBehaviors) {
|
288 |
modelNullOperators.addElement(op); |
289 |
} |
290 |
this.ddnNullBehavior.setModel(modelNullOperators);
|
291 |
this.ddnNullBehavior.setIcons(nullOperatorsIcons);
|
292 |
this.ddnNullBehavior.setSelectedIndex(0); |
293 |
FeatureType featureType = parameters.getFeatureType(store); |
294 |
Search search = (Search) ToolsLocator.getComplementsManager().get( |
295 |
Search.COMPLEMENT_MANE, featureType |
296 |
); |
297 |
List<Search.OrderedAttribute> orderedAttributes = search.getOrderedAttributes(
|
298 |
Search.BASIC_TYPES_FILTER, |
299 |
Search.STR_INT_LONG_LABEL_ORDER, |
300 |
SIZE_ORDERED_ATTRIBUTES |
301 |
); |
302 |
List<ImageIcon> icons = new ArrayList<>(); |
303 |
// DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
|
304 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
305 |
DefaultComboBoxModel model = new DefaultComboBoxModel(); |
306 |
for (Search.OrderedAttribute attr : orderedAttributes) {
|
307 |
FeatureAttributeDescriptor attrdesc = attr.getDescriptor(); |
308 |
Field field = new Field( |
309 |
new FeatureAttributeDescriptor[]{attrdesc}, |
310 |
this.store,
|
311 |
attrdesc, |
312 |
attr.getType() |
313 |
); |
314 |
model.addElement(field); |
315 |
String iconName = attrdesc.getDataType().getIconName();
|
316 |
if (iconTheme.exists(iconName)) {
|
317 |
icons.add(iconTheme.get(iconName)); |
318 |
} else {
|
319 |
icons.add(null);
|
320 |
} |
321 |
} |
322 |
|
323 |
this.ddnFields.setIcons(icons);
|
324 |
this.ddnFields.setModel(model);
|
325 |
this.ddnFields.addItemListener(new ItemListener() { |
326 |
@Override
|
327 |
public void itemStateChanged(ItemEvent e) { |
328 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
329 |
doUpdateValuesList(); |
330 |
} |
331 |
|
332 |
} |
333 |
}); |
334 |
|
335 |
this.cboValue.addItemListener(new ItemListener() { |
336 |
@Override
|
337 |
public void itemStateChanged(ItemEvent e) { |
338 |
if (e.getStateChange() == ItemEvent.SELECTED) { |
339 |
if (cboValue.getSelectedItem() != null && cboValue.getSelectedItem() instanceof LabeledValue) { |
340 |
if (Objects.equals(((LabeledValue) cboValue.getSelectedItem()).getValue(), LOAD_MORE_ELEMENTS)) {
|
341 |
setUpdateValuesLimits(updateValuesTimeLimit + 10, updateValuesFeaturesLimit + 20000); |
342 |
} |
343 |
} |
344 |
} |
345 |
} |
346 |
}); |
347 |
|
348 |
this.lblExtraFields.addMouseListener(new MouseAdapter() { |
349 |
@Override
|
350 |
public void mouseClicked(MouseEvent e) { |
351 |
doSelectMoreFields(); |
352 |
} |
353 |
}); |
354 |
doUpdateControllerByRelationalOperator(); |
355 |
// clear();
|
356 |
} catch (Exception ex) { |
357 |
throw new RuntimeException(ex); |
358 |
} |
359 |
} |
360 |
|
361 |
private void doUpdateControllerByRelationalOperator() { |
362 |
Object item = ((LabeledValue) ddnRelationalOperators.getSelectedItem()).getValue();
|
363 |
if (ExpressionBuilder.OPERATOR_IS_NULL.equals(item) || ExpressionBuilder.OPERATOR_IS_NOT_NULL.equals(item)) {
|
364 |
lblNull.setEnabled(false);
|
365 |
cboValue.setEnabled(false);
|
366 |
cboValue.setSelectedIndex(-1);
|
367 |
} else {
|
368 |
lblNull.setEnabled(true);
|
369 |
cboValue.setEnabled(true);
|
370 |
} |
371 |
|
372 |
} |
373 |
|
374 |
private FeatureType getFeatureType() {
|
375 |
try {
|
376 |
return this.store.getDefaultFeatureType(); |
377 |
} catch (DataException ex) {
|
378 |
return null; |
379 |
} |
380 |
} |
381 |
|
382 |
private void doSelectMoreFields() { |
383 |
DefaultFeatureAttributeSelectionPanel panel = new DefaultFeatureAttributeSelectionPanel(
|
384 |
store, |
385 |
parameters.getFeatureType(store), |
386 |
parameters.getQuery() |
387 |
); |
388 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
389 |
final Dialog dialog = winManager.createDialog( |
390 |
panel, |
391 |
"Select attribute",
|
392 |
null,
|
393 |
WindowManager_v2.BUTTONS_OK_CANCEL |
394 |
); |
395 |
dialog.addActionListener((ActionEvent e) -> {
|
396 |
if (dialog.getAction() == WindowManager_v2.BUTTONS_OK) {
|
397 |
doAddAndSelect( |
398 |
panel.getSelectedStore(), |
399 |
panel.getSelectedAttributeDescriptor(), |
400 |
panel.getSelectedPath() |
401 |
); |
402 |
} |
403 |
}); |
404 |
dialog.show(WindowManager.MODE.DIALOG); |
405 |
|
406 |
} |
407 |
|
408 |
private void doAddAndSelect(FeatureStore theStore, FeatureAttributeDescriptor attrdesc, FeatureAttributeDescriptor[] path) { |
409 |
ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
410 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
411 |
DefaultComboBoxModel<Field> model = (DefaultComboBoxModel) this.ddnFields.getModel(); |
412 |
if (attrdesc == null) { |
413 |
dialogManager.messageDialog( |
414 |
i18n.getTranslation("_It_is_not_supported_to_search_through_this_field") + "\n" |
415 |
+ i18n.getTranslation("_Field_not_found"),
|
416 |
"_Warning",
|
417 |
JOptionPane.WARNING_MESSAGE
|
418 |
); |
419 |
return;
|
420 |
} |
421 |
|
422 |
for (int i = 0; i < model.getSize(); i++) { |
423 |
Field field = model.getElementAt(i);
|
424 |
FeatureAttributeDescriptor attrdescN = field.getDescriptor(); |
425 |
if (theStore!=null && attrdescN.getStore()!=null && isTheSameStore(theStore, attrdescN.getStore()) |
426 |
&& StringUtils.equalsIgnoreCase(attrdesc.getName(), attrdescN.getName())) { |
427 |
this.setAttribute(i);
|
428 |
return;
|
429 |
} |
430 |
} |
431 |
Field field = new Field( |
432 |
path, |
433 |
theStore, |
434 |
attrdesc, |
435 |
Search.OrderedAttribute.TYPE_REGURAL, |
436 |
!isTheSameStore(store, theStore) |
437 |
); |
438 |
if (field.getPath().length > 2) { |
439 |
dialogManager.messageDialog( |
440 |
i18n.getTranslation("_It_is_not_supported_to_search_through_this_field") + "\n" |
441 |
+ i18n.getTranslation("_To_many_links"),
|
442 |
"_Warning",
|
443 |
JOptionPane.WARNING_MESSAGE
|
444 |
); |
445 |
return;
|
446 |
} |
447 |
FeatureAttributeDescriptor parentDescriptor = field.getParentDescriptor(); |
448 |
if (parentDescriptor != null) { |
449 |
switch (parentDescriptor.getRelationType()) {
|
450 |
case DynField.RELATION_TYPE_AGGREGATE:
|
451 |
case DynField.RELATION_TYPE_COMPOSITION:
|
452 |
if (getForeingKeyName(field.getFeatureStore(), this.store) == null) { |
453 |
dialogManager.messageDialog( |
454 |
"It not supported to search through this field." + "\n" |
455 |
+ "The link field was not found.",
|
456 |
"_Warning",
|
457 |
JOptionPane.WARNING_MESSAGE
|
458 |
); |
459 |
return;
|
460 |
} |
461 |
if (getPrimaryKeyName(this.store) == null) { |
462 |
dialogManager.messageDialog( |
463 |
"It not supported to search through this field." + "\n" |
464 |
+ "A simple primary key was not found.",
|
465 |
"_Warning",
|
466 |
JOptionPane.WARNING_MESSAGE
|
467 |
); |
468 |
return;
|
469 |
} |
470 |
} |
471 |
} |
472 |
model.addElement(field); |
473 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
474 |
this.ddnFields.getIcons().add(iconTheme.get(attrdesc.getDataType().getIconName()));
|
475 |
this.setAttribute(model.getSize() - 1); |
476 |
} |
477 |
|
478 |
public void clear() { |
479 |
this.ddnRelationalOperators.setSelectedIndex(0); |
480 |
if (this.ddnLogicalOperators != null) { |
481 |
this.ddnLogicalOperators.setSelectedIndex(1); |
482 |
} |
483 |
this.cboValue.setSelectedIndex(-1); |
484 |
this.ddnNullBehavior.setSelectedIndex(0); |
485 |
doUpdateControllerByRelationalOperator(); |
486 |
} |
487 |
|
488 |
private void doUpdateValuesList() { |
489 |
final Field field = (Field) this.ddnFields.getSelectedItem(); |
490 |
if (field == null) { |
491 |
return;
|
492 |
} |
493 |
FeatureAttributeDescriptor descriptor = field.getDescriptor(); |
494 |
if (descriptor.getType() == DataTypes.DATE) {
|
495 |
if (this.dateController == null) { |
496 |
this.dateController = ToolsSwingLocator.getToolsSwingManager().createDatePickerController(
|
497 |
(JTextComponent) this.cboValue.getEditor().getEditorComponent(), |
498 |
null
|
499 |
); |
500 |
} |
501 |
} else {
|
502 |
if (this.dateController != null) { |
503 |
this.dateController.uninstall();
|
504 |
this.dateController = null; |
505 |
} |
506 |
} |
507 |
|
508 |
final List<Object> values = new ArrayList<>(); |
509 |
final DefaultComboBoxModel model = new DefaultComboBoxModel(); |
510 |
final FeatureStore theStore = field.getFeatureStore();
|
511 |
final FeatureQuery query;
|
512 |
if (this.store == theStore) { |
513 |
query = parameters.getQuery().getCopy(); // theStore.createFeatureQuery();
|
514 |
} else {
|
515 |
query = theStore.createFeatureQuery(); |
516 |
} |
517 |
query.addAttributeName(field.getDescriptor().getName()); |
518 |
query.setFilter("");
|
519 |
query.setLimit(updateValuesFeaturesLimit); |
520 |
query.getGroupByColumns().clear(); |
521 |
query.getAggregateFunctions().clear(); |
522 |
Thread th = new Thread(new Runnable() { |
523 |
@Override
|
524 |
public void run() { |
525 |
FeatureSet set = null;
|
526 |
try {
|
527 |
canHasMoreElements = false;
|
528 |
set = theStore.getFeatureSet(query); |
529 |
if (set.size() >= updateValuesFeaturesLimit) {
|
530 |
canHasMoreElements = true;
|
531 |
} |
532 |
final long timeLimit = System.currentTimeMillis() + updateValuesTimeLimit * 1000; |
533 |
set.accept(new Visitor() {
|
534 |
@Override
|
535 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
536 |
Object value = ((Feature) o).get(field.getDescriptor().getName());
|
537 |
if (value!=null && !values.contains(value)) { |
538 |
values.add(value); |
539 |
} |
540 |
if (System.currentTimeMillis() > timeLimit) { |
541 |
canHasMoreElements = true;
|
542 |
throw new VisitCanceledException(); |
543 |
} |
544 |
if (values.size() > 1000) { |
545 |
canHasMoreElements = true;
|
546 |
throw new VisitCanceledException(); |
547 |
} |
548 |
} |
549 |
}); |
550 |
} catch (VisitCanceledException ex) {
|
551 |
canHasMoreElements = true;
|
552 |
} catch (Exception ex) { |
553 |
LOGGER.warn("Can't update list of values of '" + field.getLabel() + "'.", ex); |
554 |
} finally {
|
555 |
DisposeUtils.disposeQuietly(set); |
556 |
} |
557 |
List<LabeledValue> elements = new ArrayList<>(); |
558 |
if (!values.isEmpty()) {
|
559 |
LabeledValue[] availableValues = field.getDescriptor().getAvailableValues();
|
560 |
Map<String, String> availableValuesMap = new HashMap<>(); |
561 |
if (availableValues != null) { |
562 |
for (LabeledValue availableValue : availableValues) {
|
563 |
availableValuesMap.put( |
564 |
Objects.toString(availableValue.getValue()), |
565 |
availableValue.getLabel() |
566 |
); |
567 |
} |
568 |
} |
569 |
elements.add(new LabeledValueImpl("", null, MAXLEN_IN_COMBOS)); |
570 |
for (Object value : values) { |
571 |
String key;
|
572 |
key = DataTypeUtils.toString(value); |
573 |
String label = availableValuesMap.getOrDefault(key, key);
|
574 |
elements.add(new LabeledValueImpl(label, value, MAXLEN_IN_COMBOS));
|
575 |
} |
576 |
elements.sort(null);
|
577 |
|
578 |
} |
579 |
for (LabeledValue element : elements) {
|
580 |
model.addElement(element); |
581 |
} |
582 |
if (canHasMoreElements) {
|
583 |
model.addElement(new LabeledValueImpl("...", LOAD_MORE_ELEMENTS)); |
584 |
} |
585 |
SwingUtilities.invokeLater(new Runnable() { |
586 |
@Override
|
587 |
public void run() { |
588 |
setEnabled(false);
|
589 |
cboValue.setModel(model); |
590 |
if (valueAssigned != null) { |
591 |
cboValue.setSelectedItem(valueAssigned); |
592 |
valueAssigned = null;
|
593 |
} |
594 |
setEnabled(true);
|
595 |
} |
596 |
}); |
597 |
} |
598 |
}); |
599 |
th.start(); |
600 |
} |
601 |
|
602 |
public void setEnabled(boolean enabled) { |
603 |
ddnFields.setEnabled(enabled); |
604 |
if (ddnLogicalOperators != null) { |
605 |
ddnLogicalOperators.setEnabled(enabled); |
606 |
} |
607 |
ddnRelationalOperators.setEnabled(enabled); |
608 |
lblExtraFields.setEnabled(enabled); |
609 |
cboValue.setEnabled(enabled); |
610 |
doUpdateControllerByRelationalOperator(); |
611 |
} |
612 |
|
613 |
public String getRelationalOperator() { |
614 |
LabeledValue<String> op = (LabeledValue) this.ddnRelationalOperators.getSelectedItem(); |
615 |
if (op == null) { |
616 |
return null; |
617 |
} |
618 |
return op.getValue();
|
619 |
} |
620 |
|
621 |
public int setRelationalOperator(String name) { |
622 |
int n = 0; |
623 |
for (LabeledValue relationalOperator : relationalOperators) {
|
624 |
if (StringUtils.equalsIgnoreCase(name, (CharSequence) relationalOperator.getValue())) { |
625 |
break;
|
626 |
} |
627 |
n++; |
628 |
} |
629 |
if (this.relationalOperators.length <= n) { |
630 |
return -1; |
631 |
} |
632 |
this.ddnRelationalOperators.setSelectedIndex(n);
|
633 |
doUpdateControllerByRelationalOperator(); |
634 |
return n;
|
635 |
} |
636 |
|
637 |
public String getLogicalOperator() { |
638 |
if (this.ddnLogicalOperators == null) { |
639 |
return null; |
640 |
} |
641 |
LabeledValue<String> rel = (LabeledValue) this.ddnLogicalOperators.getSelectedItem(); |
642 |
if (rel == null) { |
643 |
return null; |
644 |
} |
645 |
return rel.getValue();
|
646 |
} |
647 |
|
648 |
public void setLogicalOperator(String operator) { |
649 |
if (this.ddnLogicalOperators == null) { |
650 |
return;
|
651 |
} |
652 |
ComboBoxModel model = this.ddnLogicalOperators.getModel(); |
653 |
for (int i = 0; i < model.getSize(); i++) { |
654 |
LabeledValue modelValue = (LabeledValue) model.getElementAt(i); |
655 |
String value = (String) modelValue.getValue(); |
656 |
if (StringUtils.equals(value, operator)) {
|
657 |
this.ddnLogicalOperators.setSelectedIndex(i);
|
658 |
break;
|
659 |
} |
660 |
} |
661 |
} |
662 |
|
663 |
public Object getValue() { |
664 |
final Field field = (Field) this.ddnFields.getSelectedItem(); |
665 |
if (field == null) { |
666 |
return null; |
667 |
} |
668 |
Object v;
|
669 |
if (this.dateController == null) { |
670 |
v = this.cboValue.getSelectedItem();
|
671 |
} else {
|
672 |
v = this.dateController.get();
|
673 |
} |
674 |
if (v == null) { |
675 |
return null; |
676 |
} |
677 |
if (v instanceof LabeledValue) { |
678 |
v = ((LabeledValue) v).getValue(); |
679 |
if (v == null || v == LOAD_MORE_ELEMENTS) { |
680 |
return null; |
681 |
} |
682 |
} |
683 |
if (v instanceof CharSequence) { |
684 |
if (StringUtils.isBlank((CharSequence) v)) { |
685 |
return null; |
686 |
} |
687 |
} |
688 |
Coercion coercion = field.getDescriptor().getDataType().getCoercion(); |
689 |
try {
|
690 |
return coercion.coerce(v);
|
691 |
} catch (CoercionException ex) {
|
692 |
return null; |
693 |
} |
694 |
} |
695 |
|
696 |
public void setValue(Object value) { |
697 |
//this.cboValue.setSelectedItem(value);
|
698 |
SwingUtilities.invokeLater(new Runnable() { |
699 |
@Override
|
700 |
public void run() { |
701 |
DefaultComboBoxModel model = (DefaultComboBoxModel) cboValue.getModel(); |
702 |
for (int i = 0; i < model.getSize(); i++) { |
703 |
Object item = model.getElementAt(i);
|
704 |
if (item.equals(value)) {
|
705 |
cboValue.setSelectedIndex(i); |
706 |
valueAssigned = value; |
707 |
return;
|
708 |
} |
709 |
} |
710 |
// si no lo encuentra en el modelo lo a?ade
|
711 |
final Field field = (Field) ddnFields.getSelectedItem(); |
712 |
if (field == null) { |
713 |
return;
|
714 |
} else {
|
715 |
LabeledValue[] availableValues = field.getDescriptor().getAvailableValues();
|
716 |
Map<String, String> availableValuesMap = new HashMap<>(); |
717 |
if (availableValues != null) { |
718 |
for (LabeledValue availableValue : availableValues) {
|
719 |
availableValuesMap.put( |
720 |
Objects.toString(availableValue.getValue()), |
721 |
availableValue.getLabel() |
722 |
); |
723 |
} |
724 |
} |
725 |
String key;
|
726 |
if (value instanceof Date) { |
727 |
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()); |
728 |
df.setLenient(false);
|
729 |
key = df.format(value); |
730 |
} else {
|
731 |
key = Objects.toString(value); |
732 |
} |
733 |
String label = availableValuesMap.getOrDefault(key, key);
|
734 |
LabeledValueImpl newItem = new LabeledValueImpl(label, value);
|
735 |
model.addElement(newItem); |
736 |
cboValue.setSelectedItem(newItem); |
737 |
valueAssigned = newItem; |
738 |
} |
739 |
} |
740 |
}); |
741 |
} |
742 |
|
743 |
private Field getCurrentField() { |
744 |
final Field field = (Field) this.ddnFields.getSelectedItem(); |
745 |
return field;
|
746 |
} |
747 |
|
748 |
public int setAttribute(String name) { |
749 |
ComboBoxModel<Field> model = this.ddnFields.getModel(); |
750 |
for (int i = 0; i < model.getSize(); i++) { |
751 |
Field x = model.getElementAt(i);
|
752 |
if (StringUtils.equalsIgnoreCase(name, x.getValue())) {
|
753 |
this.setAttribute(i);
|
754 |
return i;
|
755 |
} |
756 |
} |
757 |
this.setAttribute(-1); |
758 |
return -1; |
759 |
} |
760 |
|
761 |
public void setAttribute(int index) { |
762 |
try {
|
763 |
this.ddnFields.setSelectedIndex(index);
|
764 |
} catch (Exception ex) { |
765 |
this.ddnFields.setSelectedIndex(-1); |
766 |
} |
767 |
doUpdateValuesList(); |
768 |
} |
769 |
|
770 |
public int setAttributePath(String[][] pathNames) { |
771 |
// [[attributeName, storeName],...]
|
772 |
try {
|
773 |
if (pathNames.length == 1) { |
774 |
String[] path = pathNames[pathNames.length - 1]; |
775 |
String name = path[0]; |
776 |
int index = this.setAttribute(name); |
777 |
if (index == -1) { |
778 |
try {
|
779 |
FeatureAttributeDescriptor attrDescriptor = store.getDefaultFeatureType().getAttributeDescriptor(name); |
780 |
FeatureAttributeDescriptor[] attributePath = new FeatureAttributeDescriptor[]{attrDescriptor}; |
781 |
if (attrDescriptor == null) { |
782 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
783 |
ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
784 |
dialogManager.messageDialog( |
785 |
i18n.getTranslation("_It_is_not_supported_to_search_through_this_field") + ":\n " + name + "\n" |
786 |
+ i18n.getTranslation("_Field_not_found_in_this_table"),
|
787 |
"_Warning",
|
788 |
JOptionPane.WARNING_MESSAGE
|
789 |
); |
790 |
this.cboValue.setModel(new DefaultComboBoxModel()); |
791 |
} else {
|
792 |
doAddAndSelect(store, store.getDefaultFeatureType().getAttributeDescriptor(name), attributePath); |
793 |
} |
794 |
} catch (Exception ex) { |
795 |
LOGGER.warn("Not able to set single path into controller", ex);
|
796 |
return -1; |
797 |
} |
798 |
} |
799 |
return index;
|
800 |
} else {
|
801 |
ComboBoxModel<Field> model = this.ddnFields.getModel(); |
802 |
String[] singleArrayPathNameDescriptors = new String[pathNames.length]; |
803 |
for (int i = 0; i < pathNames.length; i++) { |
804 |
singleArrayPathNameDescriptors[i] = pathNames[i][0];
|
805 |
} |
806 |
// check the drop
|
807 |
for (int i = 0; i < model.getSize(); i++) { |
808 |
Field x = model.getElementAt(i);
|
809 |
String[] arrayDescriptors = new String[x.getPath().length]; |
810 |
FeatureAttributeDescriptor[] path = x.getPath();
|
811 |
for (int j = 0; j < path.length; j++) { |
812 |
arrayDescriptors[j] = path[j].getName(); |
813 |
} |
814 |
if (Arrays.equals(singleArrayPathNameDescriptors, arrayDescriptors)) { |
815 |
this.setAttribute(i);
|
816 |
return i;
|
817 |
} |
818 |
} |
819 |
// if not, addit to the drop
|
820 |
DataManager dataManager = DALLocator.getDataManager(); |
821 |
String tableName = pathNames[pathNames.length - 1][1]; // del ultimo path, coger el nombre tabla |
822 |
FeatureStore theStore = (FeatureStore) dataManager.getStoresRepository().getStore(tableName); |
823 |
String attributeName = pathNames[pathNames.length - 1][0]; // del ultimo path, coger el nombre attribute |
824 |
if (theStore != null) { |
825 |
FeatureAttributeDescriptor attr; |
826 |
try {
|
827 |
attr = theStore.getDefaultFeatureType().getAttributeDescriptor(attributeName); |
828 |
FeatureAttributeDescriptor[] attributePath = new FeatureAttributeDescriptor[2]; |
829 |
String firstAttrName = pathNames[0][0]; |
830 |
FeatureAttributeDescriptor firstAttr = store.getDefaultFeatureType().getAttributeDescriptor(firstAttrName); |
831 |
attributePath[0] = firstAttr;
|
832 |
|
833 |
attributePath[1] = attr;
|
834 |
doAddAndSelect(theStore, attr, attributePath); |
835 |
return SIZE_ORDERED_ATTRIBUTES - 1; |
836 |
} catch (Exception ex) { |
837 |
LOGGER.warn("Not able to set foreign path into controller", ex);
|
838 |
} |
839 |
|
840 |
} |
841 |
|
842 |
} |
843 |
} catch (Exception ex) { |
844 |
LOGGER.warn("Controller not set.", ex);
|
845 |
} |
846 |
this.setAttribute(-1); |
847 |
return -1; |
848 |
} |
849 |
|
850 |
private boolean isTheSameStore(DataStore store1, DataStore store2) { |
851 |
String store1FullName = store1.getFullName();
|
852 |
String store2FullName = store2.getFullName();
|
853 |
return StringUtils.equalsIgnoreCase(store1FullName, store2FullName);
|
854 |
} |
855 |
|
856 |
private String getPrimaryKeyName(FeatureStore store) { |
857 |
try {
|
858 |
FeatureAttributeDescriptor[] pk = store.getDefaultFeatureType().getPrimaryKey();
|
859 |
if (pk == null || pk.length != 1) { |
860 |
return null; |
861 |
} |
862 |
return pk[0].getName(); |
863 |
} catch (DataException ex) {
|
864 |
return null; |
865 |
} |
866 |
} |
867 |
|
868 |
private String getForeingKeyName(FeatureStore store, FeatureStore foreingStore) { |
869 |
try {
|
870 |
for (FeatureAttributeDescriptor descriptor : store.getDefaultFeatureType()) {
|
871 |
if (descriptor.isForeingKey()) {
|
872 |
ForeingKey foreingKey = descriptor.getForeingKey(); |
873 |
if (isTheSameStore(foreingStore, foreingKey.getFeatureStore(null))) { |
874 |
return descriptor.getName();
|
875 |
} |
876 |
} |
877 |
} |
878 |
} catch (DataException ex) {
|
879 |
return null; |
880 |
} |
881 |
return null; |
882 |
} |
883 |
|
884 |
public boolean isValid(StringBuilder message) { |
885 |
try {
|
886 |
Object value = this.getValue(); |
887 |
if (value == null) { |
888 |
return true; |
889 |
} |
890 |
Field field = this.getCurrentField(); |
891 |
if (field == null) { |
892 |
return true; |
893 |
} |
894 |
if (field.getPath().length > 2) { |
895 |
message.append("Invalid field '").append(field.getLabel()).append("'.\n"); |
896 |
return false; |
897 |
} |
898 |
FeatureAttributeDescriptor descriptor = field.getDescriptor(); |
899 |
switch (this.getRelationalOperator()) { |
900 |
case ExpressionBuilder.OPERATOR_EQ:
|
901 |
case ExpressionBuilder.OPERATOR_NE:
|
902 |
case ExpressionBuilder.OPERATOR_GT:
|
903 |
case ExpressionBuilder.OPERATOR_GE:
|
904 |
case ExpressionBuilder.OPERATOR_LT:
|
905 |
case ExpressionBuilder.OPERATOR_LE:
|
906 |
try {
|
907 |
descriptor.getDataType().coerce(value); |
908 |
} catch (CoercionException ex) {
|
909 |
message.append("Invalid value '")
|
910 |
.append(Objects.toString(value)) |
911 |
.append("' for field '")
|
912 |
.append(descriptor.getLabel()) |
913 |
.append("'.");
|
914 |
message.append("\n");
|
915 |
message.append(ex.getMessage()); |
916 |
message.append("\n");
|
917 |
return false; |
918 |
} |
919 |
break;
|
920 |
|
921 |
default:
|
922 |
case ExpressionBuilder.OPERATOR_ILIKE:
|
923 |
break;
|
924 |
} |
925 |
return true; |
926 |
} catch (Exception ex) { |
927 |
message.append("Invalid values '").append(ex.toString());
|
928 |
return false; |
929 |
} |
930 |
} |
931 |
|
932 |
public ExpressionBuilder.Value getFilter() {
|
933 |
ExpressionBuilder.Value filter = null;
|
934 |
|
935 |
Object value = this.getValue(); |
936 |
if (value == null |
937 |
&& this.getRelationalOperator() != ExpressionBuilder.OPERATOR_IS_NULL
|
938 |
&& this.getRelationalOperator() != ExpressionBuilder.OPERATOR_IS_NOT_NULL) {
|
939 |
return null; |
940 |
} |
941 |
Field field = this.getCurrentField(); |
942 |
if (field == null) { |
943 |
return null; |
944 |
} |
945 |
if (field.getPath().length > 2) { |
946 |
// No soportado
|
947 |
return null; |
948 |
} |
949 |
DataManager dataManager = DALLocator.getDataManager(); |
950 |
DALExpressionBuilder builder = dataManager.createDALExpressionBuilder(); |
951 |
FeatureAttributeDescriptor parentDescriptor = field.getParentDescriptor(); |
952 |
FeatureAttributeDescriptor descriptor = field.getDescriptor(); |
953 |
|
954 |
ExpressionBuilder.Constant value_constant = null;
|
955 |
|
956 |
switch (this.getRelationalOperator()) { |
957 |
case ExpressionBuilder.OPERATOR_IS_NULL:
|
958 |
filter = getFilterForOperatorNull(parentDescriptor, descriptor, builder, field); |
959 |
return filter;
|
960 |
case ExpressionBuilder.OPERATOR_IS_NOT_NULL:
|
961 |
filter = getFilterForOperatorNotNull(parentDescriptor, descriptor, builder, field); |
962 |
return filter;
|
963 |
case ExpressionBuilder.OPERATOR_EQ:
|
964 |
case ExpressionBuilder.OPERATOR_NE:
|
965 |
case ExpressionBuilder.OPERATOR_GT:
|
966 |
case ExpressionBuilder.OPERATOR_GE:
|
967 |
case ExpressionBuilder.OPERATOR_LT:
|
968 |
case ExpressionBuilder.OPERATOR_LE:
|
969 |
try {
|
970 |
value_constant = builder.expression().constant( |
971 |
descriptor.getDataType().coerce(value) |
972 |
); |
973 |
} catch (CoercionException ex) {
|
974 |
return null; |
975 |
} |
976 |
break;
|
977 |
|
978 |
default:
|
979 |
case ExpressionBuilder.OPERATOR_ILIKE:
|
980 |
value_constant = builder.expression().constant(value); |
981 |
break;
|
982 |
} |
983 |
|
984 |
if (parentDescriptor == null) { |
985 |
// Se busca en campos de la misma tabla.
|
986 |
filter = builder.expression().binaryOperator( |
987 |
this.getRelationalOperator(),
|
988 |
builder.expression().column(this.store.getName(), descriptor.getName()),
|
989 |
value_constant |
990 |
); |
991 |
|
992 |
ExpressionBuilder.Value nullValue = builder.expression().column(this.store.getName(), descriptor.getName());
|
993 |
filter = addNullBehavior(builder, filter, nullValue); |
994 |
|
995 |
} else {
|
996 |
// Se busca en campos de una tabla relacionada.
|
997 |
switch (parentDescriptor.getRelationType()) {
|
998 |
case DynField.RELATION_TYPE_COLLABORATION:
|
999 |
case DynField.RELATION_TYPE_IDENTITY:
|
1000 |
filter = builder.expression().binaryOperator( |
1001 |
this.getRelationalOperator(),
|
1002 |
builder.foreing_value( |
1003 |
parentDescriptor.getName(), |
1004 |
descriptor.getName() |
1005 |
), |
1006 |
value_constant |
1007 |
); |
1008 |
ExpressionBuilder.Value nullValue = builder.foreing_value( |
1009 |
parentDescriptor.getName(), |
1010 |
descriptor.getName() |
1011 |
); |
1012 |
filter = addNullBehavior(builder, filter, nullValue); |
1013 |
break;
|
1014 |
|
1015 |
case DynField.RELATION_TYPE_AGGREGATE:
|
1016 |
case DynField.RELATION_TYPE_COMPOSITION:
|
1017 |
ExpressionBuilder.Value op_composition = null;
|
1018 |
op_composition = builder.expression().binaryOperator( |
1019 |
this.getRelationalOperator(),
|
1020 |
builder.expression().column( |
1021 |
field.getFeatureStore().getName(), |
1022 |
descriptor.getName() |
1023 |
), |
1024 |
value_constant |
1025 |
); |
1026 |
ExpressionBuilder.Value null_value = builder.expression().column( |
1027 |
field.getFeatureStore().getName(), |
1028 |
descriptor.getName() |
1029 |
); |
1030 |
op_composition = addNullBehavior(builder, op_composition, null_value); |
1031 |
|
1032 |
filter = builder.exists(builder.select() |
1033 |
.column(parentDescriptor.getFeatureType().getPrimaryKey()[0].getName())
|
1034 |
.from(field.getFeatureStore().getName()) |
1035 |
.limit(1)
|
1036 |
.where( |
1037 |
builder.expression().and( |
1038 |
builder.expression().eq( |
1039 |
builder.expression().column( |
1040 |
field.getFeatureStore().getName(), |
1041 |
getForeingKeyName(field.getFeatureStore(), this.store)
|
1042 |
), |
1043 |
builder.expression().column( |
1044 |
this.store.getName(),
|
1045 |
getPrimaryKeyName(this.store)
|
1046 |
) |
1047 |
), |
1048 |
op_composition |
1049 |
) |
1050 |
) |
1051 |
.toValue() |
1052 |
); |
1053 |
break;
|
1054 |
} |
1055 |
} |
1056 |
|
1057 |
filter = builder.expression().group(filter); |
1058 |
return filter;
|
1059 |
} |
1060 |
|
1061 |
public JsonObject toJson() {
|
1062 |
JsonObjectBuilder fieldBuilder = Json.createObjectBuilder(); |
1063 |
|
1064 |
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder(); |
1065 |
FeatureAttributeDescriptor[] path = this.getCurrentField().getPath(); |
1066 |
for (int i = 0; i < path.length; i++) { |
1067 |
FeatureAttributeDescriptor featureAttributeDescriptor = path[i]; |
1068 |
JsonArrayBuilder pathArray = Json.createArrayBuilder(); |
1069 |
|
1070 |
//first value: name field
|
1071 |
String fieldName = featureAttributeDescriptor.getName();
|
1072 |
pathArray.add(fieldName); |
1073 |
//second value: name store
|
1074 |
if(i==0){ |
1075 |
pathArray.add(this.store.getName());
|
1076 |
} else {
|
1077 |
FeatureType featureType = featureAttributeDescriptor.getFeatureType(); |
1078 |
String storeName = featureType.getStore().getName();
|
1079 |
pathArray.add(storeName); |
1080 |
} |
1081 |
arrayBuilder.add(pathArray.build()); |
1082 |
} |
1083 |
String relational = this.getRelationalOperator(); |
1084 |
Object value = this.getValue(); |
1085 |
String strValue = DataTypeUtils.toString(value);
|
1086 |
String logical = this.getLogicalOperator(); |
1087 |
|
1088 |
fieldBuilder.add("fieldPath", arrayBuilder.build());
|
1089 |
fieldBuilder.add("relational", relational);
|
1090 |
if (!StringUtils.isEmpty(strValue)) {
|
1091 |
fieldBuilder.add("strValue", strValue);
|
1092 |
} |
1093 |
if (!StringUtils.isEmpty(logical)) {
|
1094 |
fieldBuilder.add("logical", logical);
|
1095 |
} |
1096 |
int nullBehavior = this.getNullBehavior(); |
1097 |
fieldBuilder.add("nullBehavior", nullBehavior);
|
1098 |
return fieldBuilder.build();
|
1099 |
} |
1100 |
|
1101 |
public void put(SearchParameters params, int index) { |
1102 |
this.parameters = params;
|
1103 |
Map<String, JsonObject> values = params.getValues(); |
1104 |
if( values == null ) { |
1105 |
return;
|
1106 |
} |
1107 |
JsonObject panelState = values.getOrDefault(PANEL_NAME, null);
|
1108 |
if( panelState == null ) { |
1109 |
return;
|
1110 |
} |
1111 |
String name = DataTypeUtils.toString(index);
|
1112 |
this.fromJson(panelState.getJsonObject(name));
|
1113 |
} |
1114 |
|
1115 |
public void fromJson(JsonObject jsonState) { |
1116 |
if (jsonState == null) { |
1117 |
return;
|
1118 |
} |
1119 |
|
1120 |
JsonArray fieldPath = jsonState.getJsonArray("fieldPath");
|
1121 |
|
1122 |
// array of arrays
|
1123 |
String[][] arrayNew = new String[fieldPath.size()][2]; |
1124 |
for (int i = 0; i < fieldPath.size(); i++) { |
1125 |
String[] arrayField = new String[2]; |
1126 |
arrayField[0] = fieldPath.getJsonArray(i).getString(0); |
1127 |
arrayField[1] = fieldPath.getJsonArray(i).getString(1); |
1128 |
arrayNew[i] = arrayField; |
1129 |
} |
1130 |
this.setAttributePath(arrayNew); //usar el doAddAndSelect |
1131 |
|
1132 |
String relational = jsonState.getString("relational"); |
1133 |
this.setRelationalOperator(relational);
|
1134 |
|
1135 |
if (jsonState.containsKey("strValue")) { |
1136 |
String strValue = jsonState.getString("strValue"); |
1137 |
// SwingUtilities.invokeLater(new Runnable() {
|
1138 |
// @Override
|
1139 |
// public void run() {
|
1140 |
setValue(strValue); |
1141 |
// }
|
1142 |
// });
|
1143 |
} |
1144 |
if (jsonState.containsKey("logical")) { |
1145 |
String logical = jsonState.getString("logical"); |
1146 |
this.setLogicalOperator(logical);
|
1147 |
} |
1148 |
if (jsonState.containsKey("nullBehavior")) { |
1149 |
int nullBehavior = jsonState.getInt("nullBehavior"); |
1150 |
this.setNullBehavior(nullBehavior);
|
1151 |
} |
1152 |
|
1153 |
} |
1154 |
|
1155 |
public void setUpdateValuesLimits(int limit, int featuresLimit) { |
1156 |
this.updateValuesTimeLimit = limit;
|
1157 |
this.updateValuesFeaturesLimit = featuresLimit;
|
1158 |
doUpdateValuesList(); |
1159 |
} |
1160 |
|
1161 |
public int getNullBehavior() { |
1162 |
return (int) ((LabeledValue) this.ddnNullBehavior.getSelectedItem()).getValue(); |
1163 |
} |
1164 |
|
1165 |
public int setNullBehavior(int nullBehaviorValue) { |
1166 |
int n = 0; |
1167 |
for (LabeledValue nullBehavior : nullBehaviors) {
|
1168 |
int toInt = (int) nullBehavior.getValue(); |
1169 |
if (nullBehaviorValue == toInt) {
|
1170 |
break;
|
1171 |
} |
1172 |
n++; |
1173 |
} |
1174 |
if (this.nullBehaviors.length <= n) { |
1175 |
return -1; |
1176 |
} |
1177 |
this.ddnNullBehavior.setSelectedIndex(n);
|
1178 |
return n;
|
1179 |
} |
1180 |
|
1181 |
private ExpressionBuilder.Value addNullBehavior(DALExpressionBuilder builder, ExpressionBuilder.Value filter, ExpressionBuilder.Value nullValue) {
|
1182 |
if (this.getRelationalOperator() != ExpressionBuilder.OPERATOR_IS_NULL && this.getRelationalOperator() != ExpressionBuilder.OPERATOR_IS_NOT_NULL) { |
1183 |
if (this.getNullBehavior() == NULL_AS_TRUE) { |
1184 |
ExpressionBuilder.Function null_function = builder.expression().is_null(nullValue); |
1185 |
filter = builder.expression().or(null_function, filter); |
1186 |
} else if (this.getNullBehavior() == NULL_AS_FALSE) { |
1187 |
ExpressionBuilder.Function null_function = builder.expression().not_is_null(nullValue); |
1188 |
filter = builder.expression().and(null_function, filter); |
1189 |
} |
1190 |
} |
1191 |
return filter;
|
1192 |
} |
1193 |
|
1194 |
private ExpressionBuilder.Value getFilterForOperatorNull(
|
1195 |
FeatureAttributeDescriptor parentDescriptor, |
1196 |
FeatureAttributeDescriptor descriptor, |
1197 |
DALExpressionBuilder builder, |
1198 |
Field field) {
|
1199 |
|
1200 |
ExpressionBuilder.Value filter = null;
|
1201 |
if (parentDescriptor == null) { |
1202 |
filter = builder.expression().is_null(builder.expression().column(this.store.getName(), descriptor.getName()));
|
1203 |
} else {
|
1204 |
// Se busca en campos de una tabla relacionada.
|
1205 |
switch (parentDescriptor.getRelationType()) {
|
1206 |
case DynField.RELATION_TYPE_COLLABORATION:
|
1207 |
case DynField.RELATION_TYPE_IDENTITY:
|
1208 |
filter = builder.expression().is_null(builder.foreing_value( |
1209 |
parentDescriptor.getName(), |
1210 |
descriptor.getName() |
1211 |
)); |
1212 |
case DynField.RELATION_TYPE_AGGREGATE:
|
1213 |
case DynField.RELATION_TYPE_COMPOSITION:
|
1214 |
ExpressionBuilder.Value op_composition = null;
|
1215 |
op_composition = builder.expression().is_null(builder.expression().column( |
1216 |
field.getFeatureStore().getName(), |
1217 |
descriptor.getName() |
1218 |
)); |
1219 |
filter = builder.exists(builder.select() |
1220 |
.column(parentDescriptor.getFeatureType().getPrimaryKey()[0].getName())
|
1221 |
.from(field.getFeatureStore().getName()) |
1222 |
.limit(1)
|
1223 |
.where( |
1224 |
builder.expression().and( |
1225 |
builder.expression().eq( |
1226 |
builder.expression().column( |
1227 |
field.getFeatureStore().getName(), |
1228 |
getForeingKeyName(field.getFeatureStore(), this.store)
|
1229 |
), |
1230 |
builder.expression().column( |
1231 |
this.store.getName(),
|
1232 |
getPrimaryKeyName(this.store)
|
1233 |
) |
1234 |
), |
1235 |
op_composition |
1236 |
) |
1237 |
) |
1238 |
.toValue() |
1239 |
); |
1240 |
} |
1241 |
} |
1242 |
|
1243 |
filter = builder.expression().group(filter); |
1244 |
return filter;
|
1245 |
} |
1246 |
|
1247 |
private ExpressionBuilder.Value getFilterForOperatorNotNull(
|
1248 |
FeatureAttributeDescriptor parentDescriptor, |
1249 |
FeatureAttributeDescriptor descriptor, |
1250 |
DALExpressionBuilder builder, |
1251 |
Field field) {
|
1252 |
|
1253 |
ExpressionBuilder.Value filter = null;
|
1254 |
if (parentDescriptor == null) { |
1255 |
filter = builder.expression().not_is_null(builder.expression().column(this.store.getName(), descriptor.getName()));
|
1256 |
} else {
|
1257 |
// Se busca en campos de una tabla relacionada.
|
1258 |
switch (parentDescriptor.getRelationType()) {
|
1259 |
case DynField.RELATION_TYPE_COLLABORATION:
|
1260 |
case DynField.RELATION_TYPE_IDENTITY:
|
1261 |
filter = builder.expression().not_is_null(builder.foreing_value( |
1262 |
parentDescriptor.getName(), |
1263 |
descriptor.getName() |
1264 |
)); |
1265 |
case DynField.RELATION_TYPE_AGGREGATE:
|
1266 |
case DynField.RELATION_TYPE_COMPOSITION:
|
1267 |
ExpressionBuilder.Value op_composition = null;
|
1268 |
op_composition = builder.expression().not_is_null(builder.expression().column( |
1269 |
field.getFeatureStore().getName(), |
1270 |
descriptor.getName() |
1271 |
)); |
1272 |
|
1273 |
filter = builder.exists(builder.select() |
1274 |
.column(parentDescriptor.getFeatureType().getPrimaryKey()[0].getName())
|
1275 |
.from(field.getFeatureStore().getName()) |
1276 |
.limit(1)
|
1277 |
.where( |
1278 |
builder.expression().and( |
1279 |
builder.expression().eq( |
1280 |
builder.expression().column( |
1281 |
field.getFeatureStore().getName(), |
1282 |
getForeingKeyName(field.getFeatureStore(), this.store)
|
1283 |
), |
1284 |
builder.expression().column( |
1285 |
this.store.getName(),
|
1286 |
getPrimaryKeyName(this.store)
|
1287 |
) |
1288 |
), |
1289 |
op_composition |
1290 |
) |
1291 |
) |
1292 |
.toValue() |
1293 |
); |
1294 |
} |
1295 |
} |
1296 |
|
1297 |
filter = builder.expression().group(filter); |
1298 |
return filter;
|
1299 |
} |
1300 |
} |