IDEMPIERE-377 Improve current search dialog box / Peer review on the operators and editors for advanced search

This commit is contained in:
Carlos Ruiz 2012-08-23 19:00:04 -05:00
parent 5cd19d949b
commit e26b6e6ea9
3 changed files with 92 additions and 158 deletions

View File

@ -421,71 +421,45 @@ public class MQuery implements Serializable
/** Between - 8 */ /** Between - 8 */
public static final int BETWEEN_INDEX = 8; public static final int BETWEEN_INDEX = 8;
/** For IDEMPIERE-377 */ /** For IDEMPIERE-377 */
public static final String NOT_NULL = "IS NOT NULL"; public static final String NOT_NULL = " IS NOT NULL ";
/** For IDEMPIERE-377 */ /** For IDEMPIERE-377 */
public static final String NULL = "IS NULL"; public static final String NULL = " IS NULL ";
/** Operators for Strings */ /** Operators for Strings */
public static final ValueNamePair[] OPERATORS = new ValueNamePair[] { public static final ValueNamePair[] OPERATORS = new ValueNamePair[] {
new ValueNamePair (EQUAL, " = "), // 0 new ValueNamePair (EQUAL, " = "), // 0 - EQUAL_INDEX
new ValueNamePair (NOT_EQUAL, " != "), new ValueNamePair (NOT_EQUAL, " != "), // 1 - NOT_EQUAL_INDEX
new ValueNamePair (LIKE, " ~ "), new ValueNamePair (LIKE, " ~ "),
new ValueNamePair (NOT_LIKE, " !~ "), new ValueNamePair (NOT_LIKE, " !~ "),
new ValueNamePair (GREATER, " > "), new ValueNamePair (GREATER, " > "),
new ValueNamePair (GREATER_EQUAL, " >= "), // 5 new ValueNamePair (GREATER_EQUAL, " >= "),
new ValueNamePair (LESS, " < "), new ValueNamePair (LESS, " < "),
new ValueNamePair (LESS_EQUAL, " <= "), new ValueNamePair (LESS_EQUAL, " <= "),
new ValueNamePair (BETWEEN, " >-< ") // 8 new ValueNamePair (BETWEEN, " >-< "), // 8 - BETWEEN_INDEX
new ValueNamePair (NULL, " NULL "),
new ValueNamePair (NOT_NULL, " !NULL ")
}; };
/** Operators for IDs */ /** Operators for Lookups and Lists (including Y/N) */
public static final ValueNamePair[] OPERATORS_ID = new ValueNamePair[] { public static final ValueNamePair[] OPERATORS_LOOKUP = new ValueNamePair[] {
new ValueNamePair (EQUAL, " = "), // 0 new ValueNamePair (EQUAL, " = "),
new ValueNamePair (NOT_EQUAL, " != ") new ValueNamePair (NOT_EQUAL, " != "),
}; new ValueNamePair (NULL, " NULL "),
/** Operators for Boolean */ new ValueNamePair (NOT_NULL, " !NULL ")
public static final ValueNamePair[] OPERATORS_YN = new ValueNamePair[] {
new ValueNamePair (EQUAL, " = ")
}; };
/** Operators for Number, Amount, Date, Costs+Prices, Quantity, Integer, ID */ /** Operators for Numbers, Dates, Integers */
public static final ValueNamePair[] OPERATORS_NUMBERS = new ValueNamePair[] { public static final ValueNamePair[] OPERATORS_NUMBERS = new ValueNamePair[] {
new ValueNamePair (EQUAL, " = "), // 0 new ValueNamePair (EQUAL, " = "),
new ValueNamePair (NOT_EQUAL, " != "), new ValueNamePair (NOT_EQUAL, " != "),
new ValueNamePair (GREATER, " > "), new ValueNamePair (GREATER, " > "),
new ValueNamePair (GREATER_EQUAL, " >= "), // 5 new ValueNamePair (GREATER_EQUAL, " >= "),
new ValueNamePair (LESS, " < "), new ValueNamePair (LESS, " < "),
new ValueNamePair (LESS_EQUAL, " <= "), new ValueNamePair (LESS_EQUAL, " <= "),
new ValueNamePair (BETWEEN, " >-< "), // 8 new ValueNamePair (BETWEEN, " >-< "),
new ValueNamePair (NULL, " NULL "), new ValueNamePair (NULL, " NULL "),
new ValueNamePair (NOT_NULL, " !NULL ") new ValueNamePair (NOT_NULL, " !NULL ")
};
/** Operators for URL */
public static final ValueNamePair[] OPERATORS_EQUAL_LIKE = new ValueNamePair[] {
new ValueNamePair (EQUAL, " = "), // 0
new ValueNamePair (NOT_EQUAL, " != "),
new ValueNamePair (NOT_EQUAL, " ~ "),
new ValueNamePair (LIKE, " !~ "),
new ValueNamePair (NULL, " NULL "),
new ValueNamePair (NOT_NULL, " !NULL ")
}; };
/** Operators for all */
public static final ValueNamePair[] OPERATORS_ALL = new ValueNamePair[] {
new ValueNamePair (EQUAL, " = "), // 0
new ValueNamePair (NOT_EQUAL, " != "),
new ValueNamePair (LIKE, " ~ "),
new ValueNamePair (NOT_LIKE, " !~ "),
new ValueNamePair (GREATER, " > "),
new ValueNamePair (GREATER_EQUAL, " >= "), // 5
new ValueNamePair (LESS, " < "),
new ValueNamePair (LESS_EQUAL, " <= "),
new ValueNamePair (BETWEEN, " >-< "), // 8
new ValueNamePair (NULL, " NULL "),
new ValueNamePair (NOT_NULL, " !NULL ")
};
/************************************************************************* /*************************************************************************
* Add Restriction * Add Restriction
* @param ColumnName ColumnName * @param ColumnName ColumnName
@ -1150,50 +1124,28 @@ class Restriction implements Serializable
else else
sb.append(ColumnName); sb.append(ColumnName);
// NULL Operator
if ((Operator.equals("=") || Operator.equals("!="))
&& (Code == null
|| "NULL".equals (Code.toString().toUpperCase())))
{
if (Operator.equals("="))
sb.append(" IS NULL ");
else
sb.append(" IS NOT NULL ");
}
else if ((Operator.equals(MQuery.NULL) || Operator.equals(MQuery.NOT_NULL))
&& (Code == null
|| "NULL".equals (Code.toString().toUpperCase())))
{
if (Operator.equals(MQuery.NULL))
sb.append(" IS NULL ");
else
sb.append(" IS NOT NULL ");
}
else
{
sb.append(Operator); sb.append(Operator);
if ( ! (Operator.equals(MQuery.NULL) || Operator.equals(MQuery.NOT_NULL)))
if (Code instanceof String)
sb.append(DB.TO_STRING(Code.toString()));
else if (Code instanceof Timestamp)
sb.append(DB.TO_DATE((Timestamp)Code));
else
sb.append(Code);
// Between
// if (Code_to != null && InfoDisplay_to != null)
if (MQuery.BETWEEN.equals(Operator))
{ {
sb.append(" AND "); if (Code instanceof String)
if (Code_to instanceof String) sb.append(DB.TO_STRING(Code.toString()));
sb.append(DB.TO_STRING(Code_to.toString())); else if (Code instanceof Timestamp)
else if (Code_to instanceof Timestamp) sb.append(DB.TO_DATE((Timestamp)Code));
sb.append(DB.TO_DATE((Timestamp)Code_to));
else else
sb.append(Code_to); sb.append(Code);
}
// Between
// if (Code_to != null && InfoDisplay_to != null)
if (MQuery.BETWEEN.equals(Operator))
{
sb.append(" AND ");
if (Code_to instanceof String)
sb.append(DB.TO_STRING(Code_to.toString()));
else if (Code_to instanceof Timestamp)
sb.append(DB.TO_DATE((Timestamp)Code_to));
else
sb.append(Code_to);
}
} }
return sb.toString(); return sb.toString();
} // getSQL } // getSQL

View File

@ -81,6 +81,7 @@ import org.compiere.model.MLookupFactory;
import org.compiere.model.MProduct; import org.compiere.model.MProduct;
import org.compiere.model.MQuery; import org.compiere.model.MQuery;
import org.compiere.model.MRole; import org.compiere.model.MRole;
import org.compiere.model.MTable;
import org.compiere.model.MUserQuery; import org.compiere.model.MUserQuery;
import static org.compiere.model.SystemIDs.*; import static org.compiere.model.SystemIDs.*;
import org.compiere.model.X_AD_Column; import org.compiere.model.X_AD_Column;
@ -856,12 +857,26 @@ public final class Find extends CDialog
if (columnName != null) if (columnName != null)
{ {
log.config("Column: " + columnName); log.config("Column: " + columnName);
if (columnName.endsWith("_ID") || columnName.endsWith("_Acct")) int referenceType = -1;
operators.setModel(new DefaultComboBoxModel(MQuery.OPERATORS_ID)); MTable table = MTable.get(Env.getCtx(), m_tableName);
else if (columnName.startsWith("Is")) MColumn col = table.getColumn(columnName);
operators.setModel(new DefaultComboBoxModel(MQuery.OPERATORS_YN)); referenceType = col.getAD_Reference_ID();
else if (DisplayType.isLookup(referenceType)
|| DisplayType.YesNo == referenceType
|| DisplayType.Button == referenceType)
{
operators.setModel(new DefaultComboBoxModel(MQuery.OPERATORS_LOOKUP));
}
else if (DisplayType.isNumeric(referenceType)
|| DisplayType.isDate(referenceType)
|| DisplayType.isID(referenceType)) // Note that lookups were filtered above
{
operators.setModel(new DefaultComboBoxModel(MQuery.OPERATORS_NUMBERS));
}
else // DisplayType.isText
{
operators.setModel(new DefaultComboBoxModel(MQuery.OPERATORS)); operators.setModel(new DefaultComboBoxModel(MQuery.OPERATORS));
}
} }
} }
else if (e.getSource() == fQueryName) else if (e.getSource() == fQueryName)

View File

@ -70,6 +70,7 @@ import org.compiere.model.MLookupFactory;
import org.compiere.model.MProduct; import org.compiere.model.MProduct;
import org.compiere.model.MQuery; import org.compiere.model.MQuery;
import org.compiere.model.MRole; import org.compiere.model.MRole;
import org.compiere.model.MTable;
import org.compiere.model.MUserQuery; import org.compiere.model.MUserQuery;
import static org.compiere.model.SystemIDs.*; import static org.compiere.model.SystemIDs.*;
import org.compiere.model.X_AD_Column; import org.compiere.model.X_AD_Column;
@ -678,8 +679,7 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
ValueNamePair[] cols = new ValueNamePair[items.size()]; ValueNamePair[] cols = new ValueNamePair[items.size()];
items.toArray(cols); items.toArray(cols);
Arrays.sort(cols); // sort alpha Arrays.sort(cols); // sort alpha
ValueNamePair[] op = MQuery.OPERATORS_ALL; ValueNamePair[] op = MQuery.OPERATORS;
if(fields == null) if(fields == null)
{ {
@ -1279,75 +1279,28 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
**/ **/
private void addOperators(ListItem column, Listbox listOperator) private void addOperators(ListItem column, Listbox listOperator)
{ {
String columnName = column.getValue().toString(); String columnName = column.getValue().toString();
int columnID = MColumn.getColumn_ID(this.m_tableName, columnName); int referenceType = -1;
String SQL = "SELECT ad_reference_id FROM ad_column WHERE ad_column_id = ?"; if (columnName != null) {
PreparedStatement pstmt = null; MTable table = MTable.get(Env.getCtx(), m_tableName);
ResultSet rs = null; MColumn col = table.getColumn(columnName);
int referenceType = -1; referenceType = col.getAD_Reference_ID();
try }
{ if (DisplayType.isLookup(referenceType)
pstmt = DB.prepareStatement(SQL, null); || DisplayType.YesNo == referenceType
pstmt.setInt(1, columnID); || DisplayType.Button == referenceType)
rs = pstmt.executeQuery(); {
if( rs.next() ) addOperators(MQuery.OPERATORS_LOOKUP, listOperator);
{ }
referenceType = rs.getInt(1); else if (DisplayType.isNumeric(referenceType)
} || DisplayType.isDate(referenceType)
} || DisplayType.isID(referenceType)) // Note that lookups were filtered above
catch (SQLException e2)
{
log.log(Level.SEVERE, SQL, e2);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
log.config("Column: " + columnName);
log.log(Level.INFO, "referenceType : " + referenceType);
List<Integer> numbersList = new ArrayList<Integer>();
numbersList.add(DisplayType.Number);
numbersList.add(DisplayType.Date);
numbersList.add(DisplayType.Amount);
numbersList.add(DisplayType.CostPrice);
numbersList.add(DisplayType.Quantity);
numbersList.add(DisplayType.ID);
numbersList.add(DisplayType.Integer);
List<Integer> equalNotEqualList = new ArrayList<Integer>();
equalNotEqualList.add(DisplayType.TableDir);
equalNotEqualList.add(DisplayType.Table);
equalNotEqualList.add(DisplayType.Search);
equalNotEqualList.add(DisplayType.List);
List<Integer> equalAndLikeList = new ArrayList<Integer>();
equalAndLikeList.add(DisplayType.URL);
equalAndLikeList.add(DisplayType.Memo);
equalAndLikeList.add(DisplayType.TextLong);
equalAndLikeList.add(DisplayType.Text);
if(numbersList.contains(referenceType))
{ {
addOperators(MQuery.OPERATORS_NUMBERS, listOperator); addOperators(MQuery.OPERATORS_NUMBERS, listOperator);
} }
else if (equalNotEqualList.contains(referenceType)) else // DisplayType.isText
{ {
addOperators(MQuery.OPERATORS_ID, listOperator); addOperators(MQuery.OPERATORS, listOperator);
}
else if (DisplayType.YesNo == referenceType)
{
addOperators(MQuery.OPERATORS_YN, listOperator);
}
else if (equalAndLikeList.contains(referenceType))
{
addOperators(MQuery.OPERATORS_EQUAL_LIKE, listOperator);
}
else
{
addOperators(MQuery.OPERATORS_ALL, listOperator);
} }
} // addOperators } // addOperators
@ -1389,10 +1342,24 @@ public class FindWindow extends Window implements EventListener<Event>, ValueCha
if(field == null) return new Label(""); if(field == null) return new Label("");
WEditor editor = null; WEditor editor = null;
if (field.isKey()) if (field.isKey()
|| (!DisplayType.isLookup(field.getDisplayType()) && DisplayType.isID(field.getDisplayType())))
{
editor = new WNumberEditor(field); editor = new WNumberEditor(field);
}
else if (field.getDisplayType() == DisplayType.Button)
{
if (columnName.endsWith("_ID"))
{
editor = new WNumberEditor(field);
} else {
editor = new WStringEditor(field);
}
}
else else
{
editor = WebEditorFactory.getEditor(field, true); editor = WebEditorFactory.getEditor(field, true);
}
if (editor == null) if (editor == null)
editor = new WStringEditor(field); editor = new WStringEditor(field);