View Javadoc

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