View Javadoc

1   package org.apache.torque.om;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache Turbine" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache Turbine", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  
57  import java.io.Serializable;
58  import java.sql.Connection;
59  
60  import org.apache.commons.logging.Log;
61  import org.apache.commons.logging.LogFactory;
62  
63  import org.apache.torque.TorqueException;
64  
65  /***
66   * This class contains attributes and methods that are used by all
67   * business objects within the system.
68   *
69   * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
70   * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
71   * @version $Id: BaseObject.java,v 1.18 2003/05/14 19:38:04 mpoeschl Exp $
72   */
73  public abstract class BaseObject implements Persistent, Serializable
74  {
75      /*** The constant denoting an unset numeric database identifier. */
76      public static final int NEW_ID = -1;
77  
78      /***
79       * Shared portion of the error message thrown for methods which
80       * are not implemented.
81       */
82      private static final String NOT_IMPLEMENTED
83              = "Not implemented: Method must be overridden if called";
84  
85      /*** attribute to determine if this object has previously been saved. */
86      private boolean isNew = true;
87  
88      /*** The unique id for the object which can be used for persistence. */
89      private ObjectKey primaryKey = null;
90  
91      /***
92       * A flag that indicates an object has been modified since it was
93       * last retrieved from the persistence mechanism.  This flag is
94       * used to determine if this object should be saved to the
95       * database.  We initialize it to true to force new objects to be
96       * saved.
97       */
98      private boolean modified = true;
99  
100     /*** Cache the log to avoid looking it up every time its needed. */
101     private transient Log log = null;
102 
103     /***
104      * getter for the object primaryKey.
105      *
106      * @return the object primaryKey as an Object
107      */
108     public ObjectKey getPrimaryKey()
109     {
110         return primaryKey;
111     }
112 
113     /***
114      * Returns whether the object has been modified.
115      *
116      * @return True if the object has been modified.
117      */
118     public boolean isModified()
119     {
120         return modified;
121     }
122 
123     /***
124      * Returns whether the object has ever been saved.  This will
125      * be false, if the object was retrieved from storage or was created
126      * and then saved.
127      *
128      * @return true, if the object has never been persisted.
129      */
130     public boolean isNew()
131     {
132         return isNew;
133     }
134 
135     /***
136      * Setter for the isNew attribute.  This method will be called
137      * by Torque-generated children and Peers.
138      *
139      * @param b the state of the object.
140      */
141     public void setNew(boolean b)
142     {
143         this.isNew = b;
144     }
145 
146     /***
147      * Sets the PrimaryKey for the object.
148      *
149      * @param primaryKey The new PrimaryKey for the object.
150      * @exception TorqueException This method will not throw any exceptions
151      * but this allows for children to override the method more easily
152      */
153     public void setPrimaryKey(String primaryKey) throws TorqueException
154     {
155         this.primaryKey = new StringKey(primaryKey);
156     }
157 
158     /***
159      * Sets the PrimaryKey for the object as an Object.
160      *
161      * @param primaryKey The new PrimaryKey for the object.
162      * @exception TorqueException This method will not throw any exceptions
163      * but this allows for children to override the method more easily
164      */
165     public void setPrimaryKey(SimpleKey[] primaryKey) throws TorqueException
166     {
167         this.primaryKey = new ComboKey(primaryKey);
168     }
169 
170     /***
171      * Sets the PrimaryKey for the object as an Object.
172      *
173      * @param primaryKey The new PrimaryKey for the object.
174      * @exception TorqueException This method will not throw any exceptions
175      * but this allows for children to override the method more easily
176      */
177     public void setPrimaryKey(ObjectKey primaryKey) throws TorqueException
178     {
179         this.primaryKey = primaryKey;
180     }
181 
182     /***
183      * Sets the modified state for the object.
184      *
185      * @param m The new modified state for the object.
186      */
187     public void setModified(boolean m)
188     {
189         modified = m;
190     }
191 
192     /***
193      * Sets the modified state for the object to be false.
194      */
195     public void resetModified()
196     {
197         modified = false;
198     }
199 
200     /***
201      * Retrieves a field from the object by name. Must be overridden if called.
202      * BaseObject's implementation will throw an Error.
203      *
204      * @param field The name of the field to retrieve.
205      * @return The retrieved field value
206      *
207      */
208     public Object getByName(String field)
209     {
210         throw new Error("BaseObject.getByName: " + NOT_IMPLEMENTED);
211     }
212 
213     /***
214      * Retrieves a field from the object by name passed in
215      * as a String.  Must be overridden if called.
216      * BaseObject's implementation will throw an Error.
217      *
218      * @param name field name
219      * @return value of the field
220      */
221     public Object getByPeerName(String name)
222     {
223         throw new Error("BaseObject.getByPeerName: " + NOT_IMPLEMENTED);
224     }
225 
226     /***
227      * Retrieves a field from the object by position as specified
228      * in a database schema for example.  Must be overridden if called.
229      * BaseObject's implementation will throw an Error.
230      *
231      * @param pos field position
232      * @return value of the field
233      */
234     public Object getByPosition(int pos)
235     {
236         throw new Error("BaseObject.getByPosition: " + NOT_IMPLEMENTED);
237     }
238 
239     /***
240      * Compares this with another <code>BaseObject</code> instance.  If
241      * <code>obj</code> is an instance of <code>BaseObject</code>, delegates to
242      * <code>equals(BaseObject)</code>.  Otherwise, returns <code>false</code>.
243      *
244      * @param obj The object to compare to.
245      * @return    Whether equal to the object specified.
246      */
247     public boolean equals(Object obj)
248     {
249         if (obj != null && obj instanceof BaseObject)
250         {
251             return equals((BaseObject) obj);
252         }
253         else
254         {
255             return false;
256         }
257     }
258 
259     /***
260      * Compares the primary key of this instance with the key of another.
261      *
262      * @param bo The object to compare to.
263      * @return   Whether the primary keys are equal.
264      */
265     public boolean equals(BaseObject bo)
266     {
267         if (bo == null)
268         {
269             return false;
270         }
271         if (this == bo)
272         {
273             return true;
274         }
275         else if (getPrimaryKey() == null || bo.getPrimaryKey() == null)
276         {
277             return false;
278         }
279         else
280         {
281             return getPrimaryKey().equals(bo.getPrimaryKey());
282         }
283     }
284 
285     /***
286      * If the primary key is not <code>null</code>, return the hashcode of the
287      * primary key.  Otherwise calls <code>Object.hashCode()</code>.
288      *
289      * @return an <code>int</code> value
290      */
291     public int hashCode()
292     {
293         ObjectKey ok = getPrimaryKey();
294         if (ok == null)
295         {
296             return super.hashCode();
297         }
298 
299         return ok.hashCode();
300     }
301 
302     /***
303      * gets a commons-logging Log based on class name.
304      *
305      * @return a <code>Log</code> to write log to.
306      * @deprecated Use getLog()
307      */
308     protected Log log()
309     {
310         return getLog();
311     }
312 
313     /***
314      * gets a commons-logging Log based on class name.
315      *
316      * @return a <code>Log</code> to write log to.
317      */
318     protected Log getLog()
319     {
320         if (log == null)
321         {
322             log = LogFactory.getLog(getClass().getName());
323         }
324         return log;
325     }
326 
327     /***
328      * @see org.apache.torque.om.Persistent#save()
329      */
330     public abstract void save() throws Exception;
331 
332     /***
333      * @see org.apache.torque.om.Persistent#save(String)
334      */
335     public abstract void save(String dbName) throws Exception;
336 
337     /***
338      * @see org.apache.torque.om.Persistent#save(Connection)
339      */
340     public abstract void save(Connection con) throws Exception;
341 }