froehlich    01/11/20 08:43:46

  Modified:    apps/db/src/java/org/apache/avalon/db/basic/actions
                        SimpleWhereClause.java
  Log:
  merged "where" logic to SimpleWhereClause
  
  Revision  Changes    Path
  1.4       +184 -10   
jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/actions/SimpleWhereClause.java
  
  Index: SimpleWhereClause.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/actions/SimpleWhereClause.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SimpleWhereClause.java    2001/11/18 20:47:20     1.3
  +++ SimpleWhereClause.java    2001/11/20 16:43:46     1.4
  @@ -1,4 +1,3 @@
  -
   /*
    * Copyright (C) The Apache Software Foundation. All rights reserved.
    *
  @@ -8,14 +7,23 @@
    */
   package org.apache.avalon.db.basic.actions;
   
  -import org.apache.avalon.db.basic.data.BasicRow;
   import org.apache.avalon.db.actions.ActionException;
  +import org.apache.avalon.db.basic.data.BasicRow;
   import org.apache.avalon.db.utils.RhinoHelper;
   import org.apache.avalon.db.utils.RhinoException;
  +import org.apache.avalon.db.data.Column;
   import org.apache.avalon.db.data.DataException;
  +import org.apache.avalon.db.data.ValidationException;
  +import org.apache.avalon.db.data.impl.StringConstantColumn;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
  +import org.apache.regexp.RE;
  +import org.apache.regexp.RESyntaxException;
  +import org.w3c.dom.Element;
  +import org.w3c.dom.NodeList;
   
  +import java.util.Enumeration;
   import java.util.Iterator;
  +import java.util.Hashtable;
   
   /**
    * Class SimpleWhereClause
  @@ -23,13 +31,37 @@
    *
    * @author Paul Hammant <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
    * @author Gerhard Froehlich <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.4 $
    */
   public class SimpleWhereClause extends AbstractLogEnabled {
       private String mWhereClause;
  +    private StringBuffer mWhereBuffer;
  +    private String[] mTablenames;
  +    private Element mElement;
  +    private static Hashtable mOperators;
  +    static {
  +        mOperators = new Hashtable();
  +        addOperator("=","testEqual");
  +        addOperator(">","testGreaterThan");
  +        addOperator("<","testSmallerThan");
  +        addOperator("<>","testNotEqual");
  +        addOperator("<=","testSmallerThanOrEqual");
  +        addOperator(">=","testGreaterThanOrEqual");
  +        addOperator("like","testLike");
  +    }
  +    private static void addOperator(String operator, String method) {
  +        mOperators.put(operator,method);
  +    }
   
  -    public SimpleWhereClause(String whereClause) {
  -        this.mWhereClause = whereClause;
  +    public SimpleWhereClause(Element rootElement) {
  +        try {
  +            setupLogger(this);
  +            mElement = rootElement;
  +            parseLXSQL(rootElement);
  +            mWhereClause = mWhereBuffer.toString();
  +        } catch (Exception e) {
  +            ////getLogger().error("SimpleWhereClause(..): Exception:",e);
  +        }
       }
   
       public String getWhereClause() {
  @@ -43,6 +75,7 @@
               throw new DataException(re.getMessage());
           }
       }
  +
       public void testActionRows(Iterator rowIterator, 
SimpleWhereClauseMatchingRowCallback callBack) throws ActionException {
           try {
               testRows(rowIterator, callBack);
  @@ -53,11 +86,14 @@
   
       private void testRows(Iterator rowIterator, 
SimpleWhereClauseMatchingRowCallback callBack) throws RhinoException {
           RhinoHelper rh = new RhinoHelper();
  -        rh.addBean("iterator",rowIterator);
  -        rh.addBean("callback",callBack);
           TestHelper testHelper = new TestHelper();
  +        ColumnCheck columnCheck = new ColumnCheck();
  +
  +        rh.addBean("iterator",rowIterator);
  +        rh.addBean("callBack",callBack);
  +        rh.addBean("columnCheck",columnCheck);
           rh.addBean("testHelper",testHelper);
  -        getLogger().debug("mWhereClause" + mWhereClause.toString());
  +        //getLogger().debug("mWhereClause" + mWhereClause.toString());
           String js =
               "while(iterator.hasNext()) {" +
                  "var row = it.next();" +
  @@ -90,11 +126,14 @@
           if(!mWhereClause.toString().equals("")) {
               RhinoHelper rh = new RhinoHelper();
               MatchingHelper mh = new MatchingHelper();
  +            ColumnCheck columnCheck = new ColumnCheck();
  +
               rh.addBean("row",row);
  -            rh.addBean("callback",mh);
  +            rh.addBean("callBack",mh);
  +            rh.addBean("columnCheck",columnCheck);
               TestHelper testHelper = new TestHelper();
               rh.addBean("testHelper",testHelper);
  -            getLogger().debug("mWhereClause" + mWhereClause.toString());
  +            //getLogger().debug("mWhereClause" + mWhereClause.toString());
               rh.executeScript(
                    "if (" + mWhereClause.toString() + ") { " +
                       "callBack.rowMatches(row, true);" +
  @@ -107,10 +146,145 @@
           }
       }
   
  +    private void parseLXSQL(Element rootElement) throws ActionException {
  +        NodeList subRootNodes = this.getSubRootNodes(rootElement);
  +        boolean whereDone = false;
  +        StringBuffer whereBuffer = new StringBuffer();
  +        int indent = 0;
  +
  +        try {
  +            for (int i=0; i < subRootNodes.getLength(); i++) {
  +                if(subRootNodes.item(i).getNodeName().equals("where")) {
  +                    if (whereDone) {
  +                        throw new ActionException("<where> term must only be 
specified once");
  +                    }
  +                    Element whereElement = (Element)subRootNodes.item(i);
  +                    NodeList elements = whereElement.getChildNodes();
  +                    for (int j=0; j < elements.getLength(); j++) {
  +                        Element element = (Element)elements.item(j);
  +                        parseWhereTerm(element, whereBuffer, indent+1);
  +                    }
  +                    whereDone = true;
  +                }
  +            }
  +        } catch (ValidationException ve) {
  +            throw new ActionException("Validation Exception: " + 
ve.getMessage());
  +        }
  +        mWhereBuffer = whereBuffer;
  +        //getLogger().debug("sb=[\n" + whereBuffer + "\n]");
  +    }
  +
  +    private void parseWhereTerm(Element element, StringBuffer sb, int 
indent) throws ValidationException {
  +        if(element != null) {
  +            //getLogger().debug("Element != null " + element);
  +            if(element.getTagName().equals("and")) {
  +                //getLogger().debug("parseWhereTerm(): found and");
  +                parseWhereAndOrTerm(element.getChildNodes(),sb, indent, "&");
  +            } else if(element.getTagName().equals("or")) {
  +                //getLogger().debug("parseWhereTerm(): found or");
  +                parseWhereAndOrTerm(element.getChildNodes(),sb, indent, "|");
  +            } else if(element.getTagName().equals("condition")) {
  +                //getLogger().debug("parseWhereTerm(): found condition");
  +                parseWhereConditionTerm(element,sb,indent);
  +            } else {
  +                throw new ValidationException("Unknown where term " + 
element.getTagName());
  +            }
  +        } else {
  +            //getLogger().debug("Element == null " + element);
  +            //getLogger().debug("parseWhereTerm after if");
  +            throw new ValidationException("Null where element");
  +        }
  +        //getLogger().debug("parseWhereTerm at the end");
  +    }
  +
  +    private void parseWhereAndOrTerm(NodeList nodes, StringBuffer sb, int 
indent, String andOr) {
  +        sb.append(getIndent(indent) + "(\n");
  +        try {
  +            for(int i=0; i<nodes.getLength(); i++) {
  +                //getLogger().debug("Looping and/or");
  +                parseWhereTerm((Element)nodes.item(i),sb, indent +1);
  +                if (i+1 < nodes.getLength()) {
  +                  sb.append( getIndent(indent) + andOr + "\n");
  +                }
  +            }
  +        } catch(Exception e) {
  +            e.printStackTrace();
  +        }
  +        sb.append(getIndent(indent) + ")\n");
  +    }
  +
  +    private void parseWhereConditionTerm(Element element, StringBuffer sb, 
int indent) {
  +        try {
  +            //getLogger().debug("parseWhereConditionTerm(): found 
condition");
  +            sb.append(getIndent(indent) + 
composeExpr(element.getAttribute("expr")) + "\n");
  +        } catch(Exception e) {
  +            e.printStackTrace();
  +        }
  +    }
  +
  +    private String getIndent(int indent) {
  +        String rval = "";
  +        for (int f = 0; f < indent; f++) {
  +            rval = rval + "  ";
  +        }
  +        return rval;
  +    }
  +
  +    private String composeExpr(String expr) {
  +        String[] strarr = null;
  +        String[] result = null;
  +        String method = null;
  +        String operator = null;
  +        //getLogger().debug("expr=" + expr);
  +        try {
  +            Enumeration enum = mOperators.keys();
  +            //getLogger().debug("mOperators.size()=" + mOperators.size());
  +            while(enum.hasMoreElements()) {
  +                operator = (String)enum.nextElement();
  +                //getLogger().debug("matching operator = " + operator);
  +                //getLogger().debug("matching function = " + 
mOperators.get(operator));
  +                RE re = new RE(operator);
  +                strarr = re.split(expr);
  +                if(strarr.length > 1) {
  +                    method = (String)mOperators.get(operator);
  +                    //getLogger().debug("matched function = " + method);
  +                    result = strarr;
  +                }
  +            }
  +            method = "testHelper." + method + 
"(columnCheck.getColumn(row,\"" + result[0]
  +                     + "\"),columnCheck.getColumn(row,\"" + result[1] + 
"\"),row)";
  +            //getLogger().debug("method=" + method);
  +
  +        } catch (RESyntaxException rse) {
  +            //getLogger().error("composeExpr(): RESyntaxException",rse);
  +        } catch (Exception e) {
  +            //getLogger().error("composeExpr(): Exception",e);
  +        }
  +        return method;
  +    }
  +
  +    private NodeList getSubRootNodes(Element rootElement) {
  +        return rootElement.getChildNodes();
  +    }
  +
       public class MatchingHelper implements 
SimpleWhereClauseMatchingRowCallback {
           public boolean mMatched = false;
           public void rowMatches(BasicRow row, boolean matched) {
               mMatched = matched;
           }
  +    }
  +
  +    public class ColumnCheck {
  +       public Column getColumn(BasicRow row, String name) {
  +            if(row.getValue(name) == null) {
  +                return getConstantColumn("constantcolumn",name);
  +            } else {
  +                return (Column)row.getColumn(name,false);
  +            }
  +       }
  +
  +       private Column getConstantColumn(String name, String value) {
  +            return new StringConstantColumn(name,value);
  +       }
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to