View Javadoc

    1   package org.apache.turbine.services.security.torque;
    2
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *   http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21
    22  import java.io.Serializable;
    23
    24  import java.sql.Connection;
    25
    26  import org.apache.torque.om.ObjectKey;
    27  import org.apache.torque.om.Persistent;
    28
    29  import org.apache.turbine.om.security.SecurityEntity;
    30  import org.apache.turbine.util.security.TurbineSecurityException;
    31
    32  /**
    33   * All the Torque Security objects (User, Group, Role, Permission) are
    34   * derived from this class which contains the base compare and management
    35   * methods for all security objects.
    36   *
    37   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    38   * @version $Id: TorqueObject.java 1096130 2011-04-23 10:37:19Z ludwig $
    39   */
    40
    41  public abstract class TorqueObject
    42      implements SecurityEntity,
    43                 Comparable,
    44                 Persistent,
    45                 Serializable
    46  {
    47
    48  	static final long serialVersionUID = 5619862273774652856L;
    49
    50      /** The underlying database Object which is proxied */
    51      protected Persistent obj = null;
    52
    53      /**
    54       * Constructs a new TorqueObject
    55       *
    56       */
    57      public TorqueObject()
    58      {
    59      }
    60
    61      /**
    62       * Constructs a new Object with the specified name.
    63       *
    64       * @param name The name of the new object.
    65       */
    66      public TorqueObject(String name)
    67      {
    68          this.setName(name);
    69      }
    70
    71      /**
    72       * This Constructor is used when a Manager
    73       * has retrieved a list of Database Objects from the peer and
    74       * must 'wrap' them into TorqueObjects.
    75       *
    76       * @param obj An Object from the peer
    77       */
    78     public  TorqueObject(Persistent obj)
    79      {
    80          this.obj = obj;
    81      }
    82
    83      /**
    84       * Returns the underlying Object for the Peer
    85       *
    86       * @return The underlying persistent object
    87       *
    88       */
    89      public abstract Persistent getPersistentObj();
    90
    91      /**
    92       * Returns the name of this object
    93       *
    94       * @return The name of the object
    95       */
    96      public abstract String getName();
    97
    98      /**
    99       * Sets the name of this object
    100      *
    101      * @param name The name of the object
    102      */
    103     public abstract void setName(String name);
    104
    105     /**
    106      * getter for the object primaryKey.
    107      *
    108      * @return the object primaryKey as an Object
    109      */
    110     public ObjectKey getPrimaryKey()
    111     {
    112         Persistent p = getPersistentObj();
    113         if (p != null)
    114         {
    115             return p.getPrimaryKey();
    116         }
    117         else
    118         {
    119             return null;
    120         }
    121     }
    122
    123     /**
    124      * Sets the PrimaryKey for the object.
    125      *
    126      * @param primaryKey The new PrimaryKey for the object.
    127      *
    128      * @exception Exception This method might throw an exceptions
    129      */
    130     public void setPrimaryKey(ObjectKey primaryKey)
    131         throws Exception
    132     {
    133         getPersistentObj().setPrimaryKey(primaryKey);
    134     }
    135
    136     /**
    137      * Sets the PrimaryKey for the object.
    138      *
    139      * @param primaryKey the String should be of the form produced by
    140      *        ObjectKey.toString().
    141      *
    142      * @exception Exception This method might throw an exceptions
    143      */
    144     public void setPrimaryKey(String primaryKey)
    145         throws Exception
    146     {
    147         getPersistentObj().setPrimaryKey(primaryKey);
    148     }
    149
    150     /**
    151      * Returns whether the object has been modified, since it was
    152      * last retrieved from storage.
    153      *
    154      * @return True if the object has been modified.
    155      */
    156     public boolean isModified()
    157     {
    158         return getPersistentObj().isModified();
    159     }
    160
    161     /**
    162      * Returns whether the object has ever been saved.  This will
    163      * be false, if the object was retrieved from storage or was created
    164      * and then saved.
    165      *
    166      * @return true, if the object has never been persisted.
    167      */
    168     public boolean isNew()
    169     {
    170         return getPersistentObj().isNew();
    171     }
    172
    173     /**
    174      * Setter for the isNew attribute.  This method will be called
    175      * by Torque-generated children and Peers.
    176      *
    177      * @param b the state of the object.
    178      */
    179     public void setNew(boolean b)
    180     {
    181         getPersistentObj().setNew(b);
    182     }
    183
    184     /**
    185      * Sets the modified state for the object.
    186      *
    187      * @param m The new modified state for the object.
    188      */
    189     public void setModified(boolean m)
    190     {
    191         getPersistentObj().setModified(m);
    192     }
    193
    194     /**
    195      * Stores the object in the database.  If the object is new,
    196      * it inserts it; otherwise an update is performed.
    197      *
    198      * @param torqueName The name under which the object should be stored.
    199      *
    200      * @exception Exception This method might throw an exceptions
    201      */
    202     public void save(String torqueName)
    203         throws Exception
    204     {
    205         getPersistentObj().save(torqueName);
    206     }
    207
    208     /**
    209      * Stores the object in the database.  If the object is new,
    210      * it inserts it; otherwise an update is performed.  This method
    211      * is meant to be used as part of a transaction, otherwise use
    212      * the save() method and the connection details will be handled
    213      * internally
    214      *
    215      * @param con A Connection object to save the object
    216      *
    217      * @exception Exception This method might throw an exceptions
    218      */
    219     public void save(Connection con)
    220         throws Exception
    221     {
    222         getPersistentObj().save(con);
    223     }
    224
    225     /**
    226      * Makes changes made to the TorqueObject permanent.
    227      *
    228      * @throws TurbineSecurityException if there is a problem while
    229      *  saving data.
    230      */
    231     public abstract void save()
    232         throws TurbineSecurityException;
    233
    234     /**
    235      * Used for ordering TorqueObjects.
    236      *
    237      * @param obj The Object to compare to.
    238      * @return -1 if the name of the other object is lexically greater than this
    239      *         group, 1 if it is lexically lesser, 0 if they are equal.
    240      */
    241     public int compareTo(Object obj)
    242     {
    243         if (this.getClass() != obj.getClass())
    244         {
    245             throw new ClassCastException();
    246         }
    247         String name1 = ((SecurityEntity) obj).getName();
    248         String name2 = this.getName();
    249
    250         return name2.compareTo(name1);
    251     }
    252
    253     /**
    254      * Compares this with another <code>BaseObject</code> instance.  If
    255      * <code>obj</code> is an instance of <code>BaseObject</code>, delegates to
    256      * <code>equals(BaseObject)</code>.  Otherwise, returns <code>false</code>.
    257      *
    258      * @param obj The object to compare to.
    259      * @return    Whether equal to the object specified.
    260      */
    261     public boolean equals(Object obj)
    262     {
    263         if (obj != null && obj instanceof TorqueObject)
    264         {
    265             return equals((TorqueObject) obj);
    266         }
    267         else
    268         {
    269             return false;
    270         }
    271     }
    272
    273     /**
    274      * Compares the primary key of this instance with the key of another.
    275      *
    276      * @param torqueObject The TorqueObject to compare to.
    277      * @return   Whether the primary keys are equal.
    278      */
    279     public boolean equals(TorqueObject torqueObject)
    280     {
    281         if (torqueObject == null)
    282         {
    283             return false;
    284         }
    285         if (this == torqueObject)
    286         {
    287             return true;
    288         }
    289         else if (getPrimaryKey() == null || torqueObject.getPrimaryKey() == null)
    290         {
    291             return false;
    292         }
    293         else
    294         {
    295             return getPrimaryKey().equals(torqueObject.getPrimaryKey());
    296         }
    297     }
    298
    299     /**
    300      * If the primary key is not <code>null</code>, return the hashcode of the
    301      * primary key.  Otherwise calls <code>Object.hashCode()</code>.
    302      *
    303      * @return an <code>int</code> value
    304      */
    305     public int hashCode()
    306     {
    307         ObjectKey ok = getPrimaryKey();
    308         if (ok == null)
    309         {
    310             return super.hashCode();
    311         }
    312
    313         return ok.hashCode();
    314     }
    315 }