IDEMPIERE-2836 SQL Searches for advanced users

This commit is contained in:
Diego Ruiz 2019-11-21 22:32:14 +01:00 committed by Carlos Ruiz
parent 853315f087
commit ca714c3774
3 changed files with 241 additions and 161 deletions

View File

@ -0,0 +1,28 @@
SET SQLBLANKLINES ON
SET DEFINE OFF
-- IDEMPIERE-2836 SQL Searches for advanced users
-- Nov 21, 2019, 10:28:50 PM CET
UPDATE AD_Field SET Help='The Validation Code displays the date, time and message of the error.
You can add advanced SQL queries to your searches by filling this field with @SQL=[WHERE CLAUSE].
Do not include the WHERE statement and use Fully qualified SQL statements. F.e:
@SQL=C_Order.isActive=''Y'' AND SalesRep_ID=@AD_User_ID@
will filter orders by active and where the sales representative is the current user.', IsCentrallyMaintained='N', AD_Reference_Value_ID=NULL, AD_Val_Rule_ID=NULL, IsToolbarButton=NULL,Updated=TO_DATE('2019-11-21 22:28:50','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=12249
;
-- Nov 21, 2019, 6:54:31 PM CET
UPDATE AD_Field SET Help='The Validation Code displays the date, time and message of the error.
You can add advanced SQL queries to your searches by filling this field with @SQL=[WHERE CLAUSE].
Do not include the WHERE statement and use Fully qualified SQL statements. F.e:
@SQL=C_Order.isActive=''Y'' AND SalesRep_ID=@AD_User_ID@
will filter orders by active and where the sales representative is the current user.', IsCentrallyMaintained='N', AD_Reference_Value_ID=NULL, AD_Val_Rule_ID=NULL, IsToolbarButton=NULL,Updated=TO_DATE('2019-11-21 18:54:31','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=206305
;
SELECT register_migration_script('201911211857_IDEMPIERE-2836.sql') FROM dual
;

View File

@ -0,0 +1,25 @@
-- IDEMPIERE-2836 SQL Searches for advanced users
-- Nov 21, 2019, 10:28:50 PM CET
UPDATE AD_Field SET Help='The Validation Code displays the date, time and message of the error.
You can add advanced SQL queries to your searches by filling this field with @SQL=[WHERE CLAUSE].
Do not include the WHERE statement and use Fully qualified SQL statements. F.e:
@SQL=C_Order.isActive=''Y'' AND SalesRep_ID=@AD_User_ID@
will filter orders by active and where the sales representative is the current user.', IsCentrallyMaintained='N', AD_Reference_Value_ID=NULL, AD_Val_Rule_ID=NULL, IsToolbarButton=NULL,Updated=TO_TIMESTAMP('2019-11-21 22:28:50','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=12249
;
-- Nov 21, 2019, 6:54:31 PM CET
UPDATE AD_Field SET Help='The Validation Code displays the date, time and message of the error.
You can add advanced SQL queries to your searches by filling this field with @SQL=[WHERE CLAUSE].
Do not include the WHERE statement and use Fully qualified SQL statements. F.e:
@SQL=C_Order.isActive=''Y'' AND SalesRep_ID=@AD_User_ID@
will filter orders by active and where the sales representative is the current user.', IsCentrallyMaintained='N', AD_Reference_Value_ID=NULL, AD_Val_Rule_ID=NULL, IsToolbarButton=NULL,Updated=TO_TIMESTAMP('2019-11-21 18:54:31','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Field_ID=206305
;
SELECT register_migration_script('201911211857_IDEMPIERE-2836.sql') FROM dual
;

View File

@ -243,6 +243,10 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
private StatusBarPanel statusBar = new StatusBarPanel(); private StatusBarPanel statusBar = new StatusBarPanel();
/** END DEVCOFFEE **/ /** END DEVCOFFEE **/
/** IDEMPIERE-2836 User Query Where */
private String m_whereUserQuery;
private ToolBar advancedPanelToolBar;
/** /**
* FindPanel Constructor * FindPanel Constructor
* @param targetWindowNo targetWindowNo * @param targetWindowNo targetWindowNo
@ -469,10 +473,10 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
pnlButtonRight.appendChild(btnCancel); pnlButtonRight.appendChild(btnCancel);
pnlButtonRight.setStyle("text-align: right"); pnlButtonRight.setStyle("text-align: right");
ToolBar toolBar = new ToolBar(); advancedPanelToolBar = new ToolBar();
toolBar.appendChild(btnNew); advancedPanelToolBar.appendChild(btnNew);
toolBar.appendChild(btnDelete); advancedPanelToolBar.appendChild(btnDelete);
ZKUpdateUtil.setWidth(toolBar, "100%"); ZKUpdateUtil.setWidth(advancedPanelToolBar, "100%");
fQueryName.addEventListener(Events.ON_SELECT, this); fQueryName.addEventListener(Events.ON_SELECT, this);
@ -541,9 +545,9 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
North north =new North(); North north =new North();
layout.appendChild(north); layout.appendChild(north);
north.appendChild(toolBar); north.appendChild(advancedPanelToolBar);
ZKUpdateUtil.setVflex(toolBar, "0"); ZKUpdateUtil.setVflex(advancedPanelToolBar, "0");
Center center = new Center(); Center center = new Center();
layout.appendChild(center); layout.appendChild(center);
@ -1313,6 +1317,8 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
} }
else if (event.getTarget() == fQueryName) else if (event.getTarget() == fQueryName)
{ {
m_whereUserQuery = null;
showAdvanced();
btnSave.setDisabled(false); btnSave.setDisabled(false);
btnShare.setDisabled(false); btnShare.setDisabled(false);
int index = fQueryName.getSelectedIndex(); int index = fQueryName.getSelectedIndex();
@ -1483,31 +1489,37 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
private void parseUserQuery(MUserQuery userQuery) private void parseUserQuery(MUserQuery userQuery)
{ {
String code = userQuery.getCode(); String code = userQuery.getCode();
String[] segments = code.split(Pattern.quote(SEGMENT_SEPARATOR)); if (code.startsWith("@SQL=")) {
m_whereUserQuery = "(" + code.substring(code.indexOf("=")+1, code.length()) + ")";
log.log(Level.INFO, m_whereUserQuery);
hideAdvanced();
} else {
String[] segments = code.split(Pattern.quote(SEGMENT_SEPARATOR));
List<?> rowList = advancedPanel.getChildren(); List<?> rowList = advancedPanel.getChildren();
for (int rowIndex = rowList.size() - 1; rowIndex >= 1; rowIndex--) for (int rowIndex = rowList.size() - 1; rowIndex >= 1; rowIndex--)
rowList.remove(rowIndex); rowList.remove(rowIndex);
for (int i = 0; i < segments.length; i++) for (int i = 0; i < segments.length; i++)
{ {
String[] fields = segments[i].split(Pattern.quote(FIELD_SEPARATOR)); String[] fields = segments[i].split(Pattern.quote(FIELD_SEPARATOR));
createFields(fields, i); createFields(fields, i);
} }
String[] historysegments = code.split(Pattern.quote(HISTORY_SEPARATOR)); String[] historysegments = code.split(Pattern.quote(HISTORY_SEPARATOR));
String history = historysegments.length > INDEX_HISTORY? historysegments[INDEX_HISTORY] : ""; String history = historysegments.length > INDEX_HISTORY? historysegments[INDEX_HISTORY] : "";
if(history.length() > 0) if(history.length() > 0)
{ {
historyCombo.setAttribute("history", history); historyCombo.setAttribute("history", history);
//historyCombo.setSelectedItem(new Comboitem(history)); //historyCombo.setSelectedItem(new Comboitem(history));
//historyCombo.setSelectedItem(new Comboitem(history, history)); //historyCombo.setSelectedItem(new Comboitem(history, history));
historyCombo.setSelectedIndex(getHistoryIndex(history)+1); historyCombo.setSelectedIndex(getHistoryIndex(history)+1);
}
} }
advancedPanel.invalidate(); winAdvanced.invalidate();
} }
private int getHistoryIndex(String value) private int getHistoryIndex(String value)
@ -1613,146 +1625,151 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
// //
m_query = new MQuery(m_tableName); m_query = new MQuery(m_tableName);
m_query.addRestriction(Env.parseContext(Env.getCtx(), m_targetWindowNo, m_whereExtended, false)); m_query.addRestriction(Env.parseContext(Env.getCtx(), m_targetWindowNo, m_whereExtended, false));
StringBuilder code = new StringBuilder();
int openBrackets = 0; if (m_whereUserQuery == null) {
StringBuilder code = new StringBuilder();
List<?> rowList = advancedPanel.getChildren(); int openBrackets = 0;
for (int rowIndex = 1; rowIndex < rowList.size() ; rowIndex++) List<?> rowList = advancedPanel.getChildren();
{
// Column
ListItem row = (ListItem)rowList.get(rowIndex);
Listbox column = (Listbox)row.getFellow("listColumn"+row.getId());
if (column == null)
continue;
String ColumnName = column.getSelectedItem().getValue().toString();
String infoName = column.toString();
//
GridField field = getTargetMField(ColumnName);
if (field == null || field.isVirtualUIColumn())
continue;
boolean isProductCategoryField = isProductCategoryField(field.getColumnName());
String ColumnSQL = field.getSearchColumnSQL();
// Left brackets
Listbox listLeftBracket = (Listbox)row.getFellow("listLeftBracket"+row.getId());
String lBrackets = listLeftBracket.getSelectedItem().getValue().toString();
if ( lBrackets != null )
openBrackets += lBrackets.length();
else lBrackets = "";
// Right brackets
Listbox listRightBracket = (Listbox)row.getFellow("listRightBracket"+row.getId());
String rBrackets = listRightBracket.getSelectedItem().getValue().toString();
if ( rBrackets != null )
openBrackets -= rBrackets.length();
else rBrackets = "";
// And Or
Listbox listAndOr = (Listbox)row.getFellow("listAndOr"+row.getId());
String andOr = listAndOr.getSelectedItem().getValue().toString();
boolean and = true;
if ( rowIndex > 1 ) {
and = !"OR".equals(andOr);
}
// Op
Listbox op = (Listbox)row.getFellow("listOperator"+row.getId());
if (op == null)
continue;
String Operator = op.getSelectedItem().getValue().toString();
// Value ****** for (int rowIndex = 1; rowIndex < rowList.size() ; rowIndex++)
ListCell cellQueryFrom = (ListCell)row.getFellow("cellQueryFrom"+row.getId()); {
Object value = null; // Column
ListItem row = (ListItem)rowList.get(rowIndex);
Listbox column = (Listbox)row.getFellow("listColumn"+row.getId());
if (column == null)
continue;
String ColumnName = column.getSelectedItem().getValue().toString();
String infoName = column.toString();
//
GridField field = getTargetMField(ColumnName);
if (field == null || field.isVirtualUIColumn())
continue;
boolean isProductCategoryField = isProductCategoryField(field.getColumnName());
String ColumnSQL = field.getSearchColumnSQL();
// Left brackets
Listbox listLeftBracket = (Listbox)row.getFellow("listLeftBracket"+row.getId());
String lBrackets = listLeftBracket.getSelectedItem().getValue().toString();
if ( lBrackets != null )
openBrackets += lBrackets.length();
else lBrackets = "";
// Right brackets
Listbox listRightBracket = (Listbox)row.getFellow("listRightBracket"+row.getId());
String rBrackets = listRightBracket.getSelectedItem().getValue().toString();
if ( rBrackets != null )
openBrackets -= rBrackets.length();
else rBrackets = "";
// And Or
Listbox listAndOr = (Listbox)row.getFellow("listAndOr"+row.getId());
String andOr = listAndOr.getSelectedItem().getValue().toString();
boolean and = true;
if ( rowIndex > 1 ) {
and = !"OR".equals(andOr);
}
// Op
Listbox op = (Listbox)row.getFellow("listOperator"+row.getId());
if (op == null)
continue;
String Operator = op.getSelectedItem().getValue().toString();
//Allowing Date validation before save // Value ******
Component compo = cellQueryFrom.getFirstChild(); ListCell cellQueryFrom = (ListCell)row.getFellow("cellQueryFrom"+row.getId());
if(compo instanceof Datebox) { Object value = null;
Datebox dbox = (Datebox)compo;
if(dbox.getValue() != null)
value = new Timestamp(((Date)dbox.getValue()).getTime());
}
else if(compo instanceof DatetimeBox) {
DatetimeBox dtbox = (DatetimeBox)compo;
if(dtbox.getValue() != null)
value = new Timestamp(((Date)dtbox.getValue()).getTime());
}
else {
value = cellQueryFrom.getAttribute("value");
}
if (value == null) //Allowing Date validation before save
{ Component compo = cellQueryFrom.getFirstChild();
if(Operator.equals(MQuery.NULL) || Operator.equals(MQuery.NOT_NULL)) if(compo instanceof Datebox) {
{ Datebox dbox = (Datebox)compo;
m_query.addRestriction(ColumnSQL, Operator, null, if(dbox.getValue() != null)
infoName, null, and, openBrackets); value = new Timestamp(((Date)dbox.getValue()).getTime());
appendCode(code, ColumnName, Operator, "", "", andOr, lBrackets, rBrackets); }
} else if(compo instanceof DatetimeBox) {
continue; DatetimeBox dtbox = (DatetimeBox)compo;
} if(dtbox.getValue() != null)
Object parsedValue = parseValue(field, value); value = new Timestamp(((Date)dtbox.getValue()).getTime());
if (parsedValue == null) }
continue; else {
String infoDisplay = value.toString(); value = cellQueryFrom.getAttribute("value");
if (field.isLookup()) }
infoDisplay = field.getLookup().getDisplay(value);
else if (field.getDisplayType() == DisplayType.YesNo)
infoDisplay = Msg.getMsg(Env.getCtx(), infoDisplay);
// Value2 ******
Object value2 = null;
if (MQuery.OPERATORS[MQuery.BETWEEN_INDEX].getValue().equals(Operator))
{
ListCell cellQueryTo = (ListCell)row.getFellow("cellQueryTo"+row.getId());
//Allowing Date validation before save if (value == null)
compo = cellQueryTo.getFirstChild(); {
if(compo instanceof Datebox) { if(Operator.equals(MQuery.NULL) || Operator.equals(MQuery.NOT_NULL))
Datebox dbox = (Datebox)compo; {
if(dbox.getValue() != null) m_query.addRestriction(ColumnSQL, Operator, null,
value2 = new Timestamp(((Date)dbox.getValue()).getTime()); infoName, null, and, openBrackets);
} appendCode(code, ColumnName, Operator, "", "", andOr, lBrackets, rBrackets);
else if(compo instanceof DatetimeBox) { }
DatetimeBox dtbox = (DatetimeBox)compo; continue;
if(dtbox.getValue() != null) }
value2 = new Timestamp(((Date)dtbox.getValue()).getTime()); Object parsedValue = parseValue(field, value);
} if (parsedValue == null)
else { continue;
value2 = cellQueryFrom.getAttribute("value"); String infoDisplay = value.toString();
} if (field.isLookup())
infoDisplay = field.getLookup().getDisplay(value);
else if (field.getDisplayType() == DisplayType.YesNo)
infoDisplay = Msg.getMsg(Env.getCtx(), infoDisplay);
// Value2 ******
Object value2 = null;
if (MQuery.OPERATORS[MQuery.BETWEEN_INDEX].getValue().equals(Operator))
{
ListCell cellQueryTo = (ListCell)row.getFellow("cellQueryTo"+row.getId());
//Allowing Date validation before save
compo = cellQueryTo.getFirstChild();
if(compo instanceof Datebox) {
Datebox dbox = (Datebox)compo;
if(dbox.getValue() != null)
value2 = new Timestamp(((Date)dbox.getValue()).getTime());
}
else if(compo instanceof DatetimeBox) {
DatetimeBox dtbox = (DatetimeBox)compo;
if(dtbox.getValue() != null)
value2 = new Timestamp(((Date)dtbox.getValue()).getTime());
}
else {
value2 = cellQueryFrom.getAttribute("value");
}
value2 = cellQueryTo.getAttribute("value"); value2 = cellQueryTo.getAttribute("value");
if (value2 == null) if (value2 == null)
continue; continue;
Object parsedValue2 = parseValue(field, value2); Object parsedValue2 = parseValue(field, value2);
String infoDisplay_to = value2.toString(); String infoDisplay_to = value2.toString();
if (parsedValue2 == null) if (parsedValue2 == null)
continue; continue;
m_query.addRangeRestriction(ColumnSQL, parsedValue, parsedValue2, m_query.addRangeRestriction(ColumnSQL, parsedValue, parsedValue2,
infoName, infoDisplay, infoDisplay_to, and, openBrackets); infoName, infoDisplay, infoDisplay_to, and, openBrackets);
} }
else if (isProductCategoryField && MQuery.OPERATORS[MQuery.EQUAL_INDEX].getValue().equals(Operator)) { else if (isProductCategoryField && MQuery.OPERATORS[MQuery.EQUAL_INDEX].getValue().equals(Operator)) {
if (!(parsedValue instanceof Integer)) { if (!(parsedValue instanceof Integer)) {
continue; continue;
} }
m_query.addRestriction(getSubCategoryWhereClause(field, ((Integer) parsedValue).intValue()), and, openBrackets); m_query.addRestriction(getSubCategoryWhereClause(field, ((Integer) parsedValue).intValue()), and, openBrackets);
} }
else if ((field.getDisplayType()==DisplayType.ChosenMultipleSelectionList||field.getDisplayType()==DisplayType.ChosenMultipleSelectionSearch||field.getDisplayType()==DisplayType.ChosenMultipleSelectionTable) && else if ((field.getDisplayType()==DisplayType.ChosenMultipleSelectionList||field.getDisplayType()==DisplayType.ChosenMultipleSelectionSearch||field.getDisplayType()==DisplayType.ChosenMultipleSelectionTable) &&
(MQuery.OPERATORS[MQuery.EQUAL_INDEX].getValue().equals(Operator) || MQuery.OPERATORS[MQuery.NOT_EQUAL_INDEX].getValue().equals(Operator))) (MQuery.OPERATORS[MQuery.EQUAL_INDEX].getValue().equals(Operator) || MQuery.OPERATORS[MQuery.NOT_EQUAL_INDEX].getValue().equals(Operator)))
{ {
String clause = DB.intersectClauseForCSV(ColumnSQL, parsedValue.toString()); String clause = DB.intersectClauseForCSV(ColumnSQL, parsedValue.toString());
if (MQuery.OPERATORS[MQuery.EQUAL_INDEX].getValue().equals(Operator)) if (MQuery.OPERATORS[MQuery.EQUAL_INDEX].getValue().equals(Operator))
m_query.addRestriction(clause, and, openBrackets); m_query.addRestriction(clause, and, openBrackets);
else else
m_query.addRestriction("NOT (" + clause + ")", and, openBrackets); m_query.addRestriction("NOT (" + clause + ")", and, openBrackets);
} }
else else
m_query.addRestriction(ColumnSQL, Operator, parsedValue, m_query.addRestriction(ColumnSQL, Operator, parsedValue,
infoName, infoDisplay, and, openBrackets); infoName, infoDisplay, and, openBrackets);
appendCode(code, ColumnName, Operator, value.toString(), value2 != null ? value2.toString() : "", andOr, lBrackets, rBrackets); appendCode(code, ColumnName, Operator, value.toString(), value2 != null ? value2.toString() : "", andOr, lBrackets, rBrackets);
} }
saveQuery(saveQuery, code, shareAllUsers); saveQuery(saveQuery, code, shareAllUsers);
} else {
m_query.addRestriction(Env.parseContext(Env.getCtx(), m_targetWindowNo, m_whereUserQuery, false));
}
} // cmd_saveAdvanced } // cmd_saveAdvanced
@ -2709,4 +2726,14 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
} // setDtatusDB } // setDtatusDB
/** END DEVCOFFEE **/ /** END DEVCOFFEE **/
private void hideAdvanced() {
advancedPanelToolBar.setVisible(false);
advancedPanel.setVisible(false);
}
private void showAdvanced() {
advancedPanelToolBar.setVisible(true);
advancedPanel.setVisible(true);
}
} // FindPanel } // FindPanel