jmcnally    01/05/30 17:30:39

  Modified:    src/java/org/apache/turbine/om/security/peer
                        TurbineUserPeer.java
  Log:
  refactored to include several methods found in torque generated peers.
  
  Revision  Changes    Path
  1.19      +154 -66   
jakarta-turbine/src/java/org/apache/turbine/om/security/peer/TurbineUserPeer.java
  
  Index: TurbineUserPeer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/om/security/peer/TurbineUserPeer.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- TurbineUserPeer.java      2001/05/23 18:53:16     1.18
  +++ TurbineUserPeer.java      2001/05/31 00:30:37     1.19
  @@ -64,7 +64,7 @@
   import java.util.Date;
   import java.util.Hashtable;
   import java.util.Vector;
  -import org.apache.turbine.om.BaseObject;
  +import org.apache.turbine.om.Persistent;
   import org.apache.turbine.om.NumberKey;
   import org.apache.turbine.om.ObjectKey;
   import org.apache.turbine.om.peer.BasePeer;
  @@ -73,8 +73,10 @@
   import org.apache.turbine.util.ObjectUtils;
   import org.apache.turbine.util.StringStackBuffer;
   import org.apache.turbine.util.db.Criteria;
  +import org.apache.turbine.util.db.map.TableMap;
   import org.apache.turbine.util.db.map.MapBuilder;
   import org.apache.turbine.util.db.map.TurbineMapBuilder;
  +import org.apache.turbine.util.db.pool.DBConnection;
   import org.apache.turbine.util.security.DataBackendException;
   
   /**
  @@ -84,7 +86,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Frank Y. Kim</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>John D. McNally</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Brett McLaughlin</a>
  - * @version $Id: TurbineUserPeer.java,v 1.18 2001/05/23 18:53:16 mikeh Exp $
  + * @version $Id: TurbineUserPeer.java,v 1.19 2001/05/31 00:30:37 jmcnally Exp $
    */
   public class TurbineUserPeer extends BasePeer implements UserPeer
   {
  @@ -230,9 +232,9 @@
       {
           Hashtable permData = (Hashtable) user.getPermStorage().clone();
           Criteria criteria = new Criteria();
  -        if ( !((BaseObject)user).isNew() )
  +        if ( !((Persistent)user).isNew() )
           {
  -            criteria.add(USER_ID, ((BaseObject)user).getPrimaryKey());
  +            criteria.add(USER_ID, ((Persistent)user).getPrimaryKey());
           }
   
           for (int i=1; i<TurbineUserPeer.columnNames.length; i++ )
  @@ -247,6 +249,75 @@
           return criteria;
       }
   
  +    /** Add all the columns needed to create a new object */
  +    public static void addSelectColumns (Criteria criteria) throws Exception
  +    {
  +        for( int i=0; i<columnNames.length; i++ )
  +        {
  +            criteria.addSelectColumn(new StringBuffer()
  +                .append(TABLE_NAME)
  +                .append(".")
  +                .append(columnNames[i]).toString() );
  +        }
  +    }
  +
  +    public static void populateObject(Record row, int offset, User obj)
  +        throws Exception
  +    {
  +        // Set values are where columns are expected.  They are not
  +        // required to be in these positions, as we set the positions
  +        // immediately following.
  +        int idPosition = 1;
  +        int objectDataPosition = columnNames.length;
  +        for( int i=0; i<columnNames.length; i++ )
  +        {
  +            if (columnNames[i].equals(USER_ID_COLUMN))
  +                idPosition = i+1;
  +            if (columnNames[i].equals(OBJECT_DATA_COLUMN))
  +                objectDataPosition = i+1;
  +        }
  +
  +        ((Persistent)obj).setPrimaryKey( 
  +            new NumberKey(row.getValue(idPosition).asBigDecimal()) );
  +
  +        // Restore the Permanent Storage Hashtable.  First the
  +        // Hashtable is restored, then any explicit table columns
  +        // which should be included in the Hashtable are added.
  +        byte[] objectData = (byte[])row.getValue(objectDataPosition).asBytes();
  +        Hashtable tempHash = (Hashtable)ObjectUtils.deserialize(objectData);
  +        if (tempHash == null)
  +        {
  +            tempHash = new Hashtable(10);
  +        }
  +
  +        for( int j=0; j<columnNames.length; j++ )
  +        {
  +            if ( ! ( columnNames[j].equalsIgnoreCase( USER_ID_COLUMN )
  +                ||  columnNames[j].equalsIgnoreCase( OBJECT_DATA_COLUMN ) ))
  +            {
  +                Object obj2 = null;
  +                Value value = row.getValue(j+1);
  +                if (value.isByte()) obj2 = new Byte(value.asByte());
  +                if (value.isBigDecimal()) obj2 = value.asBigDecimal();
  +                if (value.isBytes()) obj2 = value.asBytes();
  +                if (value.isDate()) obj2 = value.asDate();
  +                if (value.isShort()) obj2 = new Short(value.asShort());
  +                if (value.isInt()) obj2 = new Integer(value.asInt());
  +                if (value.isLong()) obj2 = new Long(value.asLong());
  +                if (value.isDouble()) obj2 = new Double(value.asDouble());
  +                if (value.isFloat()) obj2 = new Float(value.asFloat());
  +                if (value.isBoolean()) obj2 = new Boolean(value.asBoolean());
  +                if (value.isString()) obj2 = value.asString();
  +                if (value.isTime()) obj2 = value.asTime();
  +                if (value.isTimestamp()) obj2 = value.asTimestamp();
  +                if (value.isUtilDate()) obj2 = value.asUtilDate();
  +                if ( obj2 != null )
  +                    tempHash.put( columnNames[j], obj2 );
  +            }
  +        }
  +        obj.setPermStorage( tempHash );
  +    }
  +
       /**
        * Issues a select based on a criteria.
        *
  @@ -275,24 +346,9 @@
                                     User current)
           throws Exception
       {
  -        // Set values are where columns are expected.  They are not
  -        // required to be in these positions, as we set the positions
  -        // while populating the select columns.
  -        int idPosition = 1;
  -        int objectDataPosition = columnNames.length;
  -
  -        for( int i=0; i<columnNames.length; i++ )
  -        {
  -            criteria.addSelectColumn(new StringBuffer()
  -                .append(TABLE_NAME)
  -                .append(".")
  -                .append(columnNames[i]).toString() );
  -            if (columnNames[i].equals(USER_ID_COLUMN))
  -                idPosition = i+1;
  -            if (columnNames[i].equals(OBJECT_DATA_COLUMN))
  -                objectDataPosition = i+1;
  -        }
  -
  +        // add User table columns
  +        addSelectColumns(criteria);
  +        
           if (criteria.getOrderByColumns() == null)
           {
               criteria.addAscendingOrderByColumn(LAST_NAME);
  @@ -316,58 +372,81 @@
           // Populate the object(s).
           for ( int i=0; i<rows.size(); i++ )
           {
  -            User obj = TurbineSecurity.getUserInstance();
               Record row = (Record)rows.elementAt(i);
  -            
  -            // FIXME!! use ObjectKey
  -            ((BaseObject)obj).setPrimaryKey(
  -                new NumberKey(row.getValue(idPosition).asBigDecimal()) );
  -
  -            // Restore the Permanent Storage Hashtable.  First the
  -            // Hashtable is restored, then any explicit table columns
  -            // which should be included in the Hashtable are added.
  -            byte[] objectData; 
  -            objectData = (byte[])row.getValue(objectDataPosition).asBytes();
  -            Hashtable tempHash = (Hashtable)ObjectUtils.deserialize(objectData);
  -            if (tempHash == null)
  +            // Add User to the return Vector.
  +            if ( current == null ) 
               {
  -                tempHash = new Hashtable(10);
  +                results.addElement( row2Object(row, 1, null) );
               }
  -
  -            for( int j=0; j<columnNames.length; j++ )
  -            {
  -                if ( ! ( columnNames[j].equalsIgnoreCase(USER_ID_COLUMN )
  -                         || columnNames[j].equalsIgnoreCase(OBJECT_DATA_COLUMN))
  -                   )
  -                {
  -                    Object obj2 = null;
  -                    Value value = row.getValue(j+1);
  -                    if (value.isByte()) obj2 = new Byte(value.asByte());
  -                    if (value.isBigDecimal()) obj2 = value.asBigDecimal();
  -                    if (value.isBytes()) obj2 = value.asBytes();
  -                    if (value.isDate()) obj2 = value.asDate();
  -                    if (value.isShort()) obj2 = new Short(value.asShort());
  -                    if (value.isInt()) obj2 = new Integer(value.asInt());
  -                    if (value.isLong()) obj2 = new Long(value.asLong());
  -                    if (value.isDouble()) obj2 = new Double(value.asDouble());
  -                    if (value.isFloat()) obj2 = new Float(value.asFloat());
  -                    if (value.isBoolean())obj2 = new Boolean(value.asBoolean());
  -                    if (value.isString()) obj2 = value.asString();
  -                    if (value.isTime()) obj2 = value.asTime();
  -                    if (value.isTimestamp()) obj2 = value.asTimestamp();
  -                    if (value.isUtilDate()) obj2 = value.asUtilDate();
  -                    if ( obj2 != null )
  -                        tempHash.put( columnNames[j], obj2 );
  -                }
  +            else 
  +            {                
  +                populateObject(row, 1, current);
  +                ((Persistent)current).setNew(false);
               }
  -            obj.setPermStorage( tempHash );
  -            ((BaseObject)obj).setNew(false);
  +        }
  +        return results;
  +    }
  +
  +    /**
  +     * Issues a select based on a criteria.
  +     *
  +     * @param criteria Object containing data that is used to create
  +     * the SELECT statement.
  +     * @param current User object that is to be used as part of the
  +     * results - if not passed, then a new one is created.
  +     * @return Vector containing TurbineUser objects.
  +     * @exception Exception, a generic exception.
  +     */
  +    public static Vector doSelect(Criteria criteria, DBConnection dbConn)
  +        throws Exception
  +    {
  +        // add User table columns
  +        addSelectColumns(criteria);
  +        
  +        if (criteria.getOrderByColumns() == null)
  +        {
  +            criteria.addAscendingOrderByColumn(LAST_NAME);
  +        }
  +
  +        // BasePeer returns a Vector of Record (Village) objects.  The
  +        // array order follows the order columns were placed in the
  +        // Select clause.
  +        Vector rows = BasePeer.doSelect(criteria, dbConn);
  +        Vector results = new Vector();
  +
  +        // Populate the object(s).
  +        for ( int i=0; i<rows.size(); i++ )
  +        {
  +            Record row = (Record)rows.elementAt(i);
               // Add User to the return Vector.
  -            results.addElement( obj );
  +            results.addElement( row2Object(row, 1, null) );
           }
           return results;
       }
   
  +    /** 
  +     * Implementss torque peers' method.  Does not use the Class argument
  +     * as Users need to go through TurbineSecurity
  +     */
  +    public static User row2Object (Record row, int offset, Class cls) 
  +        throws Exception
  +    {
  +        User obj = TurbineSecurity.getUserInstance();
  +        populateObject(row, offset, obj);
  +        ((Persistent)obj).setNew(false);
  +        ((Persistent)obj).setModified(false);
  +        return obj;
  +    }
  +
  +    /**
  +     * The type of User this peer will instantiate.
  +     */
  +    public static Class getOMClass()
  +        throws Exception
  +    {
  +        return TurbineSecurity.getUserClass();
  +    }
  +
       /**
        * Issues an update based on a criteria.
        * The criteria only uses USER_ID.
  @@ -441,5 +520,14 @@
           criteria.setIgnoreCase(true);
           return TurbineUserPeer.doSelect(criteria);
       }
  -}
   
  +    /** 
  +     * Returns the TableMap related to this peer.  This method is not 
  +     * needed for general use but a specific application could have a
  +     * need.
  +     */
  +    protected static TableMap getTableMap()
  +    {
  +        return mapBuilder.getDatabaseMap().getTable(TABLE_NAME);
  +    }     
  +}
  
  
  

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

Reply via email to