You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by hu...@apache.org on 2002/11/21 03:07:16 UTC

cvs commit: jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql StorageBeanBase.java ResultSetUtils.java

husted      2002/11/20 18:07:16

  Modified:    scaffold/src/java/org/apache/commons/scaffold/sql
                        StorageBeanBase.java ResultSetUtils.java
  Log:
  + StorageBeanBase: Add support for reading command parameters from
  a *.param line for each query.
  + StorageBean*: Add paramList property (to ease migration to Commons.SQL)
  + StorageBean*,ProcessBean*: Move Remote* properties to ProcessBean.
  + StorageBeanBase: isNew: change to use String or Number validation
  when value is not null; store: Fix to use isNew.
  
  Revision  Changes    Path
  1.14      +162 -87   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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- StorageBeanBase.java	13 Nov 2002 12:45:44 -0000	1.13
  +++ StorageBeanBase.java	21 Nov 2002 02:07:16 -0000	1.14
  @@ -5,6 +5,8 @@
   import java.sql.Timestamp;
   
   import java.util.Collection;
  +import java.util.Iterator;
  +import java.util.List;
   import java.util.Map;
   import java.util.Properties;
   
  @@ -28,7 +30,6 @@
   import org.apache.commons.scaffold.util.ProcessResult;
   import org.apache.commons.scaffold.util.ProcessResultBase;
   
  -
   // ------------------------------------------------------------------------ 78
   
   /**
  @@ -80,7 +81,7 @@
    * <LI>The insert and update commands should use same columns in same
    * order.
    * <LI>The retrieve, delete, recycle, and restore command should use
  - * the primaryKey property for selection.
  + * the storageKey property for selection.
    * <LI>Hint: If columns cannot follow property naming conventions,
    * rename column in command
    * <LI>Hint: You can use the same object for more than one table or
  @@ -123,6 +124,7 @@
       }
   
   
  +
   // ------------------------------------------------------------------ Commands
   // TODO: Refactor this to use a CommandStore
   
  @@ -246,6 +248,12 @@
   
   
       /**
  +     * [:TODO: Javadoc]
  +     */
  +    public final static String PARAMS = DOT + "params";
  +
  +
  +    /**
        * A unique identifier for this StorageBean's
        * commands.
        */
  @@ -271,9 +279,50 @@
       private Object[] parameters = null;
   
   
  -     // See interface for Javadoc
  -    public Object[] getParameters(String command) {
  -        return parameters;
  +    /**
  +     * Return the runtime parameters for the given command.
  +     *
  +     * The comma-delimited list of parameter properties can be provided
  +     * as a ${command}.params element in the command resource. 
  +     * <code>getParameters</code> will automatically retrieve the *.params element 
  +     * and use it to create an array of the runtime values for each parameter. 
  +     * The sequence of runtime values can then be merged with a prepared 
  +     * statement (the command).
  +     * Since the parameters are inserted into the command by position, 
  +     * the column names and parameter names do not need to match. 
  +     * @exception ResourceException if the command resource or command cannot 
  +     * be found.
  +     */
  +    public Object[] getParameters(String command) throws ResourceException {
  +        
  +        if (null==command) {
  +            if ((null==parameters) && (null!=paramList)) {
  +                return paramList.toArray();
  +            }       
  +            return parameters;
  +        }
  +        
  +        // Lookup parameters for command ( * + .params)        
  +        String paramToken = command + PARAMS;        
  +        String params = lookup(paramToken);
  +
  +        // :HMMM: On null, silently return an empty list?
  +        if (null==params) return new Object[0];
  +        else {
  +
  +            String[] tokens = ConvertUtils.tokensToArray(params,PARAM_SEP);
  +            Object[] values = new Object[tokens.length];
  +            try {
  +                for (int i=0; i<values.length; i++) {
  +                    values[i] = BeanUtils.getProperty(this,tokens[i]);
  +                }
  +            }
  +            catch (Exception e) {
  +                throw new PropertiesException();
  +            }
  +            
  +            return values;
  +        }       
       }
   
   
  @@ -284,6 +333,89 @@
   
   
       /**
  +     * The list of parameters required by the
  +     * insert and update commands for this object.
  +     */
  +    private List paramList = null;
  +
  +
  +    /**
  +     * Return the vparamList or parameters field. 
  +     * If both are null, return an empty list.
  +     */
  +    private List paramListDefault() throws ResourceException {
  +        
  +        List list = paramList;
  +        if (null==list) { 
  +                // Start with empty list
  +            list = new java.util.ArrayList();
  +                // Add array parameters, if any
  +            Object[] array = getParameters(null);
  +            if (null!=array) {
  +                for (int i =0; i<array.length; i++) {
  +                    list.add(array[i]);
  +                }
  +            }
  +        }
  +        return list;
  +
  +    }
  +
  +
  +    /**
  +     * Default separator character for list of parameters [","] (comma).
  +     */
  +    public static final String PARAM_SEP = ",";
  +
  +
  +    /**
  +     * Return the runtime parameters for the given command.
  +     *
  +     * The comma-delimited list of parameter properties can be provided
  +     * as a ${command}.params element in the command resource. 
  +     * <code>getParameters</code> will automatically retrieve the *.params element 
  +     * and use it to create a list of the runtime values for each parameter. 
  +     * The sequence of runtime values can then be merged with a prepared 
  +     * statement (the command).
  +     * Since the parameters are inserted into the command by position, 
  +     * the column names and parameter names do not need to match. 
  +     * @exception ResourceException if the command resource or command cannot 
  +     * be found.
  +     */
  +    public List getParamList(String command) throws ResourceException {
  +        
  +        // If no command, return the list or array field
  +        if (null==command) return paramListDefault();
  +        
  +        List values = new java.util.ArrayList();
  +        // Lookup parameters for command ( * + .params)        
  +        String paramToken = command + PARAMS;        
  +        String params = lookup(paramToken);
  +        // :HMMM: On null, silently return an empty list?
  +        if (null!=params) {           
  +            // Tokenize into list/iterator (:TODO: Cache?) (:FIXME: List better than array?)
  +            Iterator tokens = ConvertUtils.tokensToList(params,PARAM_SEP).iterator();
  +            // Get the runtime property for each parameter (token)
  +            try {
  +                while (tokens.hasNext()) { 
  +                    values.add(BeanUtils.getProperty(this,(String)tokens.next()));
  +                }
  +            }
  +            catch (Exception e) {
  +                throw new PropertiesException();
  +            }
  +        }
  +       return values;
  +    }
  +
  +
  +     // See interface for Javadoc
  +    public void setParamList(List paramList) {
  +        this.paramList = paramList;
  +    }
  +
  +
  +    /**
        * The marked status of the record.
        * <p>
        * Records to be deleted are marked and may be
  @@ -528,24 +660,25 @@
   // ================================================================ UD Methods
   
       /**
  -     * A storage location for the primaryKey object.
  +     * The storageKey field, if needed.
        * <p>
        * Many subclasses may choose to ingore this field and
  -     * use the primaryKey property as a wrapper around
  -     * their own field of a specific type (e.g. Integer).
  +     * use the storageKey property as a wrapper around
  +     * their own field of a specific type (e.g. 
  +     * an Integer primaryKey property).
        */
  -    private Object primaryKey = null;
  +    private Object storageKey = null;
   
   
           // See interface for JavaDoc
       public Object getStorageKey() {
  -        return this.primaryKey;
  +        return this.storageKey;
       }
   
   
           // See interface for JavaDoc
  -    public void setStorageKey(Object primaryKey) {
  -        this.primaryKey = primaryKey;
  +    public void setStorageKey(Object storageKey) {
  +        this.storageKey = storageKey;
       }
   
       /**
  @@ -590,67 +723,6 @@
   
   
       /**
  -     * The network address of the last editor.
  -     * <p>
  -     * This can be used to track down the
  -     * last editor in case of a conflict.
  -     * To use this feature, record the user's
  -     * ip when they login.
  -     */
  -    private Integer remoteNode = null;
  -
  -
  -    /**
  -     * Return the network address.
  -     * <p>
  -     * @return the network address.
  -     */
  -    public Integer getRemoteNode() {
  -        return (this.remoteNode);
  -    }
  -
  -
  -    /**
  -     * Set the network address.
  -     * @param remoteNode The new remoteNode.
  -     */
  -    public void setRemoteNode(Integer remoteNode) {
  -        this.remoteNode = remoteNode;
  -    }
  -
  -
  -    /**
  -     * Set the networkNode using a String in
  -     * the format usually given
  -     * by the REMOTE_ADDR CGI variable, or
  -     * ServletRequest.getRemoteAddr().
  -     * NOTE: not implemented; returns 0.
  -     * @returns An Integer value based on RemoteAddr string.
  -     */
  -    public void setRemoteAddr(String remoteAddr) {
  -
  -        setRemoteNode(new Integer(0));
  -
  -    }
  -
  -
  -    /**
  -     * Return the String representation
  -     * of an IP address expressed
  -     * as an Integer, into the format usually given
  -     * by the REMOTE_ADDR CGI variable, or
  -     * ServletRequest.getRemoteAddr().
  -     * NOTE: not implemented; returns zeros..
  -     * @returns An Integer value based on RemoteAddr string.
  -     */
  -    public String getRemoteAddr() {
  -
  -        return new String("000.000.000.000");
  -
  -    }
  -
  -
  -    /**
        * [:TODO: Javadoc]
        */
       private int resultCode = 0;
  @@ -702,7 +774,8 @@
   
           // See interface for JavaDoc
       public boolean isNew() {
  -        return (null==getStorageKey());
  +        Object key = getStorageKey();
  +        return ((null==key) || (blank(key.toString())));
       }
   
   
  @@ -747,26 +820,29 @@
       public void store() throws Exception {
   
           boolean isInsert = isNew();
  -        if (isInsert) allocateKey();
  -
  -        String command = null;
  -        if (isInsert) {
  -            command = lookup(INSERT);
  +        String token = null;
  +        if (isInsert) { 
  +            token = INSERT; 
  +            allocateKey();
           }
           else {
  -            command = lookup(UPDATE);
  +            token = UPDATE;
           }
   
  +        String command = lookup(token);
  +
           Timestamp modified = getModified();
           if ((null==modified) || (ConvertUtils.NULL_TIMESTAMP==modified)) {
               setModified(new Timestamp(System.currentTimeMillis()));
           }
  +        
  +        // :FIXME: Way to get remoteNode
   
           int result = 0;
           try {
   
               result = StatementUtils.executeUpdate(
  -                null,command,getParameters(null));
  +                null,command,getParameters(token));
   
           }
           catch (SQLException e) {
  @@ -779,7 +855,7 @@
   
       /**
        * Commit record to storage.
  -     * If primaryKey is null, new storage for this object is created.
  +     * If storageKey is null, new storage for this object is created.
        * Otherwise, an existing object is updated.
        * @return ProcessResult with messages and this object as data
        * @exception ResourceException if data access error occurs
  @@ -790,10 +866,9 @@
   
           populate((Map) parameters);
   
  -        String message = Tokens.DATA_RECORD_UPDATED;
  -        if (null==getStorageKey()) {
  -            message = Tokens.DATA_RECORD_INSERTED;
  -        }
  +        String message = null;
  +        if (isNew()) message = Tokens.DATA_RECORD_INSERTED;
  +        else message = Tokens.DATA_RECORD_UPDATED;
   
           store();
   
  
  
  
  1.4       +8 -7      jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/ResultSetUtils.java
  
  Index: ResultSetUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/sql/ResultSetUtils.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ResultSetUtils.java	18 Oct 2002 20:44:53 -0000	1.3
  +++ ResultSetUtils.java	21 Nov 2002 02:07:16 -0000	1.4
  @@ -136,6 +136,7 @@
        *  access to the property accessor method
        * @exception InvocationTargetException if the property accessor method
        *  throws an exception
  +     * @deprecated Use BeanUtils.CopyProperties instead.
        */
       public static void setProperties(Object bean, Map properties)
           throws IllegalAccessException, InvocationTargetException {
  @@ -379,11 +380,11 @@
                   putEntry(properties,metaData,resultSet,i);
               }
               try {
  -                setProperties(target,properties);
  +                BeanUtils.copyProperties(target,properties);
               }
  -            catch (Exception e) {
  +            catch (Throwable t) {
                   throw new SQLException("ResultSetUtils.getElement: " +
  -                    e.getMessage() + properties.toString());
  +                    t.getMessage() + properties.toString());
               }
   
           } // end if
  @@ -429,12 +430,12 @@
               }
               try {
                   Object bean = factory.newInstance();
  -                setProperties(bean,properties);
  +                BeanUtils.copyProperties(bean,properties);
                   list.add(bean);
               }
  -            catch (Exception e) {
  +            catch (Throwable t) {
                   throw new SQLException("RequestUtils.getCollection: " +
  -                    e.getMessage());
  +                    t.getMessage());
               }
   
               properties.clear();
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>