⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 searchcriteria.java

📁 利用java反射机制
💻 JAVA
字号:
package org.julp;import java.util.*;public abstract class SearchCriteria {        /**      *  This object not indended to be a *real* Query engine.     *  It indended to be used with GUI to allow end users uppend WHERE clause to SELECT statement.     *  How it works?     *  <ul>     *    <li>User would add Criteria</li>     *    <li>Criteria would have      *      <ol>     *        <li>List of fileds to search. User must select one of the fields</li>     *        <li>List of operators for each field. You can specify which operators can be used with selected field. For example you can remove "LIKE" operator for numeric field</li>     *        <li>Some fields can have list of values to select from (codeTables). Example: field "State" can have a drop-down control (combobox) with list of states</li>     *        <li>Boolean condition AND/OR. User must select one of them if there will be another Criteria</li>     *      </ol>     *     </li>     *     <li>After user done buildCriteria() would generate WHERE clause, which would be added to pre-defined SELECT statement</li>     *     <li>To make it user-friendly fields, operators, etc. should have "display values" and "real values"     *       Example:     *       <code>     *       fields.put("firstName", "CUSTOMER.FIRST_NAME");      *       fields.put("lastName", "CUSTOMER.LAST_NAME");      *       fields.put("state", "CUSTOMER.STATE");<br>     *       ...            *       </code>     *     </li>     *   </ul>     */    protected Map codeTables = new LinkedHashMap();    protected Map fields = new LinkedHashMap();    protected Map operators = new LinkedHashMap();    protected Map allOperators = new HashMap();    protected Map operatorsDisplay = new HashMap();    protected List searchCriteriaHolders = new ArrayList();    protected List arguments = new ArrayList();    protected String whereStatement = "";            public SearchCriteria() {        operatorsDisplay.put("=", "Equals");        operatorsDisplay.put("<>", "Not Equals");        operatorsDisplay.put("LIKE", "Contains");        operatorsDisplay.put("NOT LIKE", "Does not Contain");        operatorsDisplay.put(">", "Greater Then");        operatorsDisplay.put("<", "Less Then");        operatorsDisplay.put(">=", "Greater Then or Equals");        operatorsDisplay.put("<", "Less Then");        operatorsDisplay.put("<=", "Less Then or Equals");        operatorsDisplay.put("NOT BETWEEN", "Not Between");        operatorsDisplay.put("BETWEEN", "Between");        operatorsDisplay.put("IN", "In");        operatorsDisplay.put("NOT IN", "Not In");        operatorsDisplay.put("EXISTS", "Exists");        operatorsDisplay.put("NOT EXISTS", "Doesn't Exist");        operatorsDisplay.put("!=", "Not Equals");                //operatorsDisplay.put("", "Not Equals");                        allOperators.put("Equals", "=");        allOperators.put("Not Equals", "<>");        allOperators.put("Contains", "LIKE");        allOperators.put("Does not Contain", "NOT LIKE");        allOperators.put("Greater Then", ">");        allOperators.put("Less Then", "<");        allOperators.put("Greater Then or Equals", ">=");        allOperators.put("Less Then", "<");        allOperators.put("Less Then or Equals", "<=");        allOperators.put("Not Between", "NOT BETWEEN");        allOperators.put("Between", "BETWEEN");        allOperators.put("In", "IN");        allOperators.put("Not In", "NOT IN");        allOperators.put("Exists", "EXISTS");        allOperators.put("Doesn't Exist", "NOT EXISTS");        //allOperators.put("Not Equals", "!=");            }        /** Getter for property codeTables.     * @return Value of property codeTables.     *     */    public java.util.Map getCodeTable(String field) {        return (Map) this.codeTables.get(field);    }        /** Setter for property codeTables.     * @param codeTables New value of property codeTables.     *     */    public void setCodeTables(java.util.Map codeTables) {        this.codeTables = codeTables;    }        /** Getter for property fields.     * @return Value of property fields.     *     */    public java.util.Map getFields() {        return fields;    }        /** Setter for property fields.     * @param fields New value of property fields.     *     */    public void setFields(java.util.Map fields) {        this.fields = fields;    }        /** Setter for property codeTable.     * @param codeTable New value of property codeTable.     *     */    public void setCodeTable(String field, java.util.Map codeTable) {        this.codeTables.put(field, codeTable);    }        /** Getter for property searchCriteriaHolders.     * @return Value of property searchCriteriaHolders.     *     */    public java.util.List getSearchCriteriaHolders() {        return searchCriteriaHolders;    }        /** Setter for property searchCriteriaHolders.     * @param searchCriteriaHolders New value of property searchCriteriaHolders.     *     */    public void setSearchCriteriaHolders(java.util.List searchCriteriaHolders) {        this.searchCriteriaHolders = searchCriteriaHolders;    }        public void addSearch(){        SearchCriteriaHolder holder = new SearchCriteriaHolder();        /*        Iterator it = this.fields.entrySet().iterator();        if (it.hasNext()){            Map.Entry entry = (Map.Entry) it.next();            holder.setFieldName(entry.getKey().toString());            holder.setFieldLabel(entry.getValue().toString());        }         */        this.searchCriteriaHolders.add(holder);    }        public void removeSearch(int idx){        try{            searchCriteriaHolders.remove(idx);        }catch(IndexOutOfBoundsException e){            throw new RuntimeException("Invalid operation: removeSearch(" + idx + ")");        }    }        /**      * @return operators allowed for this field.     *     */    public java.util.Map getOperators(String fieldName) {        return (Map) operators.get(fieldName);    }        public java.util.Map getOperators() {        return operators;    }        /**      * set operators allowed for this field.     *     */    public void setOperators(String fieldName, java.util.Map operators) {        this.operators.put(fieldName, operators);    }        public void setOperators(java.util.Map operators) {        this.operators = operators;    }        /** Getter for property size.     * @return Value of property size.     *     */    public int size() {        return searchCriteriaHolders.size();    }        public String buildCriteria() {        this.arguments.clear();        StringBuffer sb = new StringBuffer();        Iterator it = searchCriteriaHolders.iterator();        while (it.hasNext()){            SearchCriteriaHolder holder = (SearchCriteriaHolder) it.next();            String fieldName = holder.getFieldName();            String functionName = holder.getFunctionName();            if (fieldName == null || fieldName.trim().equals("")){                throw new IllegalArgumentException("Missing field name to search");            }            String operator = holder.getOperator();            Object searchValue = holder.getSearchValue();            String booleanCondition = holder.getBooleanCondition();            if (functionName != null){                sb.append(functionName).append("(").append(fieldName).append(")");            }else{                sb.append(fieldName);            }            if (searchValue == null || searchValue.toString().trim().equals("")){                if (operator.equals("=")){                    //operator = "IS NULL";                    sb.append(" ").append("IS NULL").append(" ");                }else if (operator.equals("<>") || operator.equals("!=")){                    //operator = "NOT IS NULL";                    sb.append(" ").append("IS NOT NULL").append(" ");                }else{                    throw new IllegalArgumentException("Invalid operator for empty search value");                }            }else{                sb.append(" ").append(operator).append(" ");                sb.append("?");                this.arguments.add(searchValue);            }            sb.append(" ").append(booleanCondition).append(" ");        }        if (searchCriteriaHolders.size() != 0){            int len = sb.length();            sb.delete(len - 4,  len - 1);        }else{            throw new RuntimeException("No search criteria");        }         this.whereStatement = sb.toString();        return this.whereStatement;            }        public String getOperator(String operatorLabel){        return (String) allOperators.get(operatorLabel);    }        /** Getter for property arguments.     * @return Value of property arguments.     *     */    public java.util.List getArguments() {        return arguments;    }        /** Setter for property arguments.     * @param arguments New value of property arguments.     *     */    public void setArguments(java.util.List arguments) {        this.arguments = arguments;    }        /** Getter for property whereStatement.     * @return Value of property whereStatement.     *     */    public java.lang.String getWhereStatement() {        return whereStatement;    }         /** Getter for property operatorsDisplay.     * @return Value of property operatorsDisplay.     *     */    public java.util.Map getOperatorsDisplay() {        return operatorsDisplay;    }        public String getOperatorsDisplay(String key) {        return (String) operatorsDisplay.get(key);    }        /** Setter for property operatorsDisplay.     * @param operatorsDisplay New value of property operatorsDisplay.     *     */    public void setOperatorsDisplay(java.util.Map operatorsDisplay) {        this.operatorsDisplay = operatorsDisplay;    }        public void setOperatorsDisplay(String key, String value) {        this.operatorsDisplay.put(key, value);    }    }    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -