commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hus...@apache.org
Subject cvs commit: jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql StorageBeanBase.java StatementUtils.java ScrollerBeanBase.java AccessBase.java
Date Thu, 31 Oct 2002 14:27:08 GMT
husted      2002/10/31 06:27:08

  Modified:    scaffold/src/java/org/apache/commons/scaffold/sql
                        StorageBeanBase.java StatementUtils.java
                        ScrollerBeanBase.java AccessBase.java
  Log:
  + ScrollerBean: Change getParameters to scrollerParams to avoid conflict with StorageBean
method. Add scrollOffset property. Change searchKey to searchValue to avoid confusion as to
purpose. Change return type to Object for using with non-string fields.
  + StatementUtils: Add like method for wrapping strings; remove *Like methods.
  + ScrollerBean: streamline interface and implementation; change to use standard calls rather
than like
  + Scroller/ScrollerBeanBase: change to use count and collection rather than *like.
  + ScrollBeanBase: Change getParameters to use with two or three parameters depending on
the state of hasParameter (2 if false, 3 if true).
  + ScrollerBeanBase: Debugso it plays nice with BeanUtils.
  + ScrollerBean,ScrollerBeanBase: Initial versions.
  + StatementUtils,StorageBeanBase: Fix countLike/columnLike methods
  
  Revision  Changes    Path
  1.11      +44 -64    jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/StorageBeanBase.java
  
  Index: StorageBeanBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/StorageBeanBase.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- StorageBeanBase.java	28 Oct 2002 15:18:20 -0000	1.10
  +++ StorageBeanBase.java	31 Oct 2002 14:27:08 -0000	1.11
  @@ -113,6 +113,14 @@
       /**
        * [:TODO: Javadoc]
        */
  +    public final static String like(String parameter) {
  +        return StatementUtils.like(parameter);
  +    }
  +
  +
  +    /**
  +     * [:TODO: Javadoc]
  +     */
       public final static String DOT = ".";
   
   
  @@ -227,19 +235,13 @@
       private String prefix = null;
   
   
  -    /**
  -     * Return a unique identifier for this StorageBean's
  -     * commands.
  -     */
  +     // See interface for Javadoc
       public String getPrefix() {
           return prefix;
       }
   
   
  -    /**
  -     * Set a unique identifier for this StorageBean's
  -     * commands.
  -     */
  +     // See interface for Javadoc
       public void setPrefix(String prefix) {
           this.prefix = prefix;
       }
  @@ -252,19 +254,13 @@
       private Object[] parameters = null;
   
   
  -    /**
  -     * Return the array of parameters required by
  -     * insert and update commands for this object.
  -     */
  +     // See interface for Javadoc
       public Object[] getParameters(String command) {
           return parameters;
       }
   
   
  -    /**
  -     * Set the array of parameters required by
  -     * the insert and update commands for this object.
  -     */
  +     // See interface for Javadoc
       public void setParameters(Object[] parameters) {
           this.parameters = parameters;
       }
  @@ -351,10 +347,9 @@
   // ----------------------------------------------------------- Retrieval Methods
   
   
  -        // See interface for JavaDoc
  +        // Convenience method
       public int count(
  -            String command,
  -            Object parameter)
  +            String command)
               throws ResourceException {
   
           Integer result = null;
  @@ -362,8 +357,7 @@
               result = (Integer) StatementUtils.getColumn(
                   null,
                   1,
  -                getCommand(command),
  -                parameter
  +                getCommand(command)
               );
           }
           catch (SQLException e) {
  @@ -374,14 +368,14 @@
   
   
           // See interface for JavaDoc
  -    public int countLike(
  +    public int count(
               String command,
  -            String parameter)
  +            Object parameter)
               throws ResourceException {
   
           Integer result = null;
           try {
  -            result = (Integer) StatementUtils.getColumnLike(
  +            result = (Integer) StatementUtils.getColumn(
                   null,
                   1,
                   getCommand(command),
  @@ -395,6 +389,7 @@
       }
   
   
  +
           // See interface for JavaDoc
       public boolean findElement(
               Object target,
  @@ -418,14 +413,18 @@
      } // end findElement
   
   
  -        // See interface for JavaDoc
  +    /**
  +     * Convenience method that calls
  +     * <code>findCollection(Object,String,Object[])</code>
  +     * with appropriate parameters.
  +     */
       public Collection findCollection(Object target,
  -        String command, Object[] parameters) throws ResourceException {
  +        String command) throws ResourceException {
   
           try {
   
               return StatementUtils.getCollection(null,
  -                target,getCommand(command),parameters);
  +                target,getCommand(command));
   
           }
           catch (SQLException e) {
  @@ -440,51 +439,34 @@
        * <code>findCollection(Object,String,Object[])</code>
        * with appropriate parameters.
        */
  -    public Collection findCollection(Object target,
  -        String command) throws ResourceException {
  -
  -         return findCollection(target,command,null);
  -
  -    } // end findCollection
  -
  -
  -    /**
  -     * Convenience method that calls
  -     * <code>findCollection(Object,String,Object[])</code>
  -     * with appropriate parameters.
  -     */
       protected final Collection findCollection(Object target,
  -        String command, Object parameter) throws ResourceException {
  +        String command, int parameter) throws ResourceException {
  +
   
  -        Object[] parameters = new Object[1];
  -        parameters[0] = parameter;
  +        try {
   
  -        return findCollection(target,command,parameters);
  +            return StatementUtils.getCollection(null,
  +                target,getCommand(command),parameter);
   
  +        }
  +        catch (SQLException e) {
  +            throw new ResourceException(e);
  +        }
       } // end findCollection
   
   
  +
       /**
        * Convenience method that calls
        * <code>findCollection(Object,String,Object[])</code>
        * with appropriate parameters.
        */
       protected final Collection findCollection(Object target,
  -        String command, int parameter) throws ResourceException {
  -
  -        return findCollection(target,command,new Integer(parameter));
  -
  -    } // end findCollection
  -
  -
  -
  -        // See interface for JavaDoc
  -    public Collection findCollectionLike(Object target,
  -        String command, String parameter) throws ResourceException {
  +        String command, Object parameter) throws ResourceException {
   
           try {
   
  -            return StatementUtils.getCollectionLike(null,
  +            return StatementUtils.getCollection(null,
                   target,getCommand(command),parameter);
   
           }
  @@ -492,26 +474,24 @@
               throw new ResourceException(e);
           }
   
  -    } // end findCollectionLike
  -
  +    } // end findCollection
   
   
           // See interface for JavaDoc
  -    public Collection findCollectionLike(Object target,
  +    public Collection findCollection(Object target,
           String command, Object[] parameters) throws ResourceException {
   
           try {
   
  -            return StatementUtils.getCollectionLike(null,
  -                target,getCommand(command),parameters, 0);
  +            return StatementUtils.getCollection(null,
  +                target,getCommand(command),parameters);
   
           }
           catch (SQLException e) {
               throw new ResourceException(e);
           }
   
  -    } // end findCollectionLike
  -
  +    } // end findCollection
   
   
           // See interface for JavaDoc
  
  
  
  1.5       +21 -86    jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/StatementUtils.java
  
  Index: StatementUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/StatementUtils.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- StatementUtils.java	28 Oct 2002 18:19:07 -0000	1.4
  +++ StatementUtils.java	31 Oct 2002 14:27:08 -0000	1.5
  @@ -19,6 +19,22 @@
   public final class StatementUtils {
   
       /**
  +     * The delimiter used by a SQL "LIKE" expression.
  +     */
  +    private static final String LIKE_DELIM = "%";
  +
  +
  +    /**
  +     * Prepare the parameter for use in a SQL "LIKE" expression.
  +     */
  +    public static final String like(String parameter) {
  +
  +        return LIKE_DELIM + parameter + LIKE_DELIM;
  +
  +    }
  +
  +
  +    /**
        * Create a new database table in an existing database,
        * by sending "CREATE TABLE " and the parameters to
        * the DBMS configured with the ConnectionPool.
  @@ -408,7 +424,7 @@
        * @param resource The database resource key or null for default
        * @param column The column to return from the result set.
        * @param command The SQL statement to prepare and execute.
  -     * @param key The replaceable parameter to use with LIKE.
  +     * @param key The replaceable parameter
        * @exception SQLException if SQL error occurs
        */
       public static final Object getColumn(
  @@ -433,7 +449,7 @@
        * @param resource The database resource key or null for default
        * @param column The column to return from the result set.
        * @param command The SQL statement to prepare and execute.
  -     * @param key The replaceable parameter to use with LIKE.
  +     * @param key The replaceable parameter to use, if any.
        * @exception SQLException if SQL error occurs
        */
       public static final Object getColumn(
  @@ -445,6 +461,7 @@
   
           Object[] parameters = new Object[1];
           parameters[0] = new Integer(key);
  +
           return getColumn(resource,column,command,parameters);
   
       } // end getColumn
  @@ -458,7 +475,7 @@
        * @param resource The database resource key or null for default
        * @param column The column to return from the result set.
        * @param command The SQL statement to prepare and execute.
  -     * @param key The replaceable parameter to use with LIKE.
  +     * @param key The replaceable parameter.
        * @exception SQLException if SQL error occurs
        */
       public static final Object getColumn(
  @@ -472,32 +489,6 @@
       } // end getColumn
   
   
  -    /**
  -     * Convenience method to wrap a string for a command that
  -     * uses the <code>LIKE</code> operator.
  -     * Calls <code>getCollection(String,Object,String,Object[]);</code>
  -     *
  -     * @param resource The database resource key or null for default
  -     * @param target The JavaBean object to return in the collection.
  -     * @param command The SQL statement to prepare and execute.
  -     * @param key The replaceable parameter to use with LIKE.
  -     * @exception SQLException if SQL error occurs
  -     */
  -    public static final Object getColumnLike(
  -            String resource,
  -            int column,
  -            String command,
  -            String key)
  -        throws SQLException {
  -
  -        Object[] parameters = new Object[1];
  -        parameters[0] = new String("%" + key + "%");
  -
  -        return getColumn(resource,column,command,parameters);
  -
  -    } // end getColumnLike
  -
  -
   // ------------------------------------------------------ getElement
   
       /**
  @@ -752,63 +743,7 @@
       } // end getCollection
   
   
  -    /**
  -     * Convenience method to wrap a string for a command that
  -     * uses the <code>LIKE</code> operator.
  -     * Calls <code>getCollection(String,Object,String,Object[]);</code>
  -     *
  -     * @param resource The database resource key or null for default
  -     * @param target The JavaBean object to return in the collection.
  -     * @param command The SQL statement to prepare and execute.
  -     * @param key The replaceable parameter to use with LIKE.
  -     * @exception SQLException if SQL error occurs
  -     */
  -    public static final Collection getCollectionLike(
  -            String resource,
  -            Object target,
  -            String command,
  -            String key)
  -        throws SQLException {
  -
  -        Object[] parameters = new Object[1];
  -        parameters[0] = new String("%" + key + "%");
  -
  -        return getCollection(resource,target,command,parameters);
  -
  -    } // end getCollectionLike
  -
  -
  -    /**
  -     * Convenience method to wrap a string for a command that
  -     * uses the <code>LIKE</code> operator.
  -     *
  -     * Calls <code>getCollection(String,Object,String,Object[]);</code>
  -     *
  -     * @param resource The database resource key or null for default
  -     * @param target The JavaBean object to return in the collection.
  -     * @param command The SQL statement to prepare and execute.
  -     * @param parameters The replaceable parameters to use with query
  -     * @param index The parameter index that will be used with LIKE (0-based)
  -     * @exception SQLException if SQL error occurs
  -     */
  -    public static final Collection getCollectionLike(
  -            String resource,
  -            Object target,
  -            String command,
  -            Object[] parameters,
  -            int index)
  -        throws SQLException {
  -
  -        parameters[index] = new String("%" + parameters[index].toString() + "%");
  -
  -        return getCollection(resource,target,command,parameters);
  -
  -    } // end getCollectionLike
  -
  -
   } // end StatementUtils
  -
  -
   
   
   
  
  
  
  1.2       +90 -65    jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/ScrollerBeanBase.java
  
  Index: ScrollerBeanBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/ScrollerBeanBase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ScrollerBeanBase.java	28 Oct 2002 19:46:22 -0000	1.1
  +++ ScrollerBeanBase.java	31 Oct 2002 14:27:08 -0000	1.2
  @@ -28,127 +28,152 @@
       /**
        * The entry (or page) from which to scroll [1].
        */
  -    public int scrollFrom = 1;
  +    public Integer scrollFrom = new Integer(1);
   
   
        // See interface for Javadoc
  -    public int getScrollFrom() {
  +    public Integer getScrollFrom() {
           return scrollFrom;
       }
   
   
        // See interface for Javadoc
  -    public void setScrollFrom(int scrollFrom) {
  +    public void setScrollFrom(Integer scrollFrom) {
           this.scrollFrom = scrollFrom;
       }
   
   
  -
        // See interface for Javadoc
  -    public int getScrollRows() {
  -        return Scroller.SCROLL_ROWS;
  -
  +    public Integer scrollOffset() {
  +        int offset = getScrollFrom().intValue();
  +        return new Integer(--offset);
       }
   
   
        // See interface for Javadoc
  -    public int getEntryCount() throws ResourceException {
  -
  -        return countLike(getCountKey(),getSearchKey());
  +    public int scrollRows() {
  +        return Scroller.SCROLL_ROWS;
   
       }
   
   
        // See interface for Javadoc
  -    public Scroller getScroller(int entries) throws ResourceException {
  -
  -        Scroller scroller = new Scroller();
  -        scroller.calculate(
  -            entries,
  -            getScrollFrom(),
  -            getEntryCount(),
  -            getScrollRows()
  -            );
  +    public String countKey() {
  +        return null;
  +    }
   
  -        if (isParameter()) {
  -            java.util.Map map = new java.util.HashMap();
  -            map.put(getSearchProperty(),getSearchKey());
  -            scroller.setParameters(map);
  -        }
   
  -        return scroller;
  +     // See interface for Javadoc
  +    public boolean hasParameter() {
  +        return false;
       }
   
   
        // See interface for Javadoc
  -    public String getCommandKey() {
  +    public Object searchValue() {
           return null;
       }
   
   
        // See interface for Javadoc
  -    public Object[] getParameters(Object parameter)
  -            throws ResourceException {
  +    public int entryCount() throws ResourceException {
  +
  +        if (hasParameter())
  +            return count(countKey(),searchValue());
  +         else
  +            return count(countKey());
   
  -        Object[] parameters = new Object[3];
  -        parameters[0] = parameter;
  -        parameters[1] = new Integer(getScrollFrom());
  -        parameters[2] = new Integer(getScrollRows());
  -        return parameters;
       }
   
   
        // See interface for Javadoc
  -    public Collection getResult(Object target, Object parameter)
  -            throws ResourceException {
  -
  -        return findCollectionLike(
  -            target,
  -            getCommandKey(),
  -            getParameters(parameter)
  -        );
  +    public String commandKey() {
  +        return null;
       }
   
   
  +
        // See interface for Javadoc
  -    public String getSearchProperty() {
  +    public String searchProperty() {
           return null;
       }
   
   
        // See interface for Javadoc
  -    public String getSearchKey() {
  -        return null;
  +    public boolean failsOnEmpty () {
  +        return false;
       }
   
   
        // See interface for Javadoc
  -    public String getCountKey() {
  -        return null;
  +    public String tokenEmptyMessage() {
  +        return Tokens.DATA_ACCESS_EMPTY;
       }
   
   
        // See interface for Javadoc
  -    public boolean isParameter() {
  -        return false;
  +    public String tokenEmptyDispatch() {
  +        return Tokens.FAILURE;
       }
   
   
        // See interface for Javadoc
  -    public boolean isFailsOnEmpty () {
  -        return false;
  +    public Object[] scrollerParams(String parameter) {
  +
  +        Object[] parameters = null;
  +
  +        if (hasParameter()) {
  +
  +            parameters = new Object[3];
  +            parameters[0] = parameter;
  +            parameters[1] = scrollOffset();
  +            parameters[2] = new Integer(scrollRows());
  +        }
  +
  +        else {
  +
  +            parameters = new Object[2];
  +            parameters[0] = scrollOffset();
  +            parameters[1] = new Integer(scrollRows());
  +        }
  +
  +        return parameters;
       }
   
   
        // See interface for Javadoc
  -    public String getTokenEmptyMessage() {
  -        return Tokens.DATA_ACCESS_EMPTY;
  +    public Collection result(Object target, Object parameter)
  +            throws ResourceException {
  +
  +        String param = null;
  +        if (null!=parameter) param = parameter.toString();
  +
  +        return findCollection(
  +            target,
  +            commandKey(),
  +            scrollerParams(param)
  +        );
       }
   
   
        // See interface for Javadoc
  -    public String getTokenEmptyDispatch() {
  -        return Tokens.FAILURE;
  +    public Scroller newScroller(int entries) throws ResourceException {
  +
  +        Scroller scroller = new Scroller();
  +        scroller.calculate(
  +            entries,
  +            getScrollFrom().intValue(),
  +            entryCount(),
  +            scrollRows()
  +            );
  +
  +        if (hasParameter()) {
  +            java.util.Map map = new java.util.HashMap();
  +            map.put(searchProperty(),searchValue());
  +            scroller.setParameters(map);
  +        }
  +
  +        return scroller;
       }
   
   
  @@ -158,26 +183,26 @@
       public Object scrollerSearch() throws Exception {
   
           Object key = null;
  -        if (isParameter()) {
  -            key = getSearchKey();
  +        if (hasParameter()) {
  +            key = searchValue();
               if (null==key) {
                   throw new ParameterException();
               }
           }
   
  -        ResultList list = new ResultListBase (getResult(this,key));
  -        Scroller scroller = getScroller(list.size());
  +        ResultList list = new ResultListBase (result(this,key));
  +        Scroller scroller = newScroller(list.size());
           list.setScroller(scroller);
           ProcessResult result = new ProcessResultBase(list);
   
  -        if (isParameter())
  -            list.setLegend(getSearchProperty(),key.toString());
  +        if (hasParameter())
  +            list.setLegend(searchProperty(),key.toString());
           else
  -            list.setLegend(getSearchProperty());
  +            list.setLegend(searchProperty());
   
  -        if (isFailsOnEmpty() && list.isEmpty()) {
  -            result.addMessage(getTokenEmptyMessage());
  -            result.setDispatch(getTokenEmptyDispatch());
  +        if (failsOnEmpty() && list.isEmpty()) {
  +            result.addMessage(tokenEmptyMessage());
  +            result.setDispatch(tokenEmptyDispatch());
           }
           return result;
   
  
  
  
  1.4       +4 -4      jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/AccessBase.java
  
  Index: AccessBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/AccessBase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AccessBase.java	23 Aug 2002 14:21:23 -0000	1.3
  +++ AccessBase.java	31 Oct 2002 14:27:08 -0000	1.4
  @@ -304,8 +304,8 @@
   
           try {
   
  -            return StatementUtils.getCollectionLike(null,
  -                target,getCommand(command),parameter);
  +            return StatementUtils.getCollection(null,
  +                target,getCommand(command),StatementUtils.like(parameter));
   
           }
           catch (SQLException e) {
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message