001    package org.apache.turbine.services.security.torque;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.Serializable;
023    
024    import java.sql.Connection;
025    
026    import org.apache.torque.om.ObjectKey;
027    import org.apache.torque.om.Persistent;
028    
029    import org.apache.turbine.om.security.SecurityEntity;
030    import org.apache.turbine.util.security.TurbineSecurityException;
031    
032    /**
033     * All the Torque Security objects (User, Group, Role, Permission) are
034     * derived from this class which contains the base compare and management
035     * methods for all security objects.
036     *
037     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
038     * @version $Id: TorqueObject.java 1096130 2011-04-23 10:37:19Z ludwig $
039     */
040    
041    public abstract class TorqueObject
042        implements SecurityEntity,
043                   Comparable,
044                   Persistent,
045                   Serializable
046    {
047    
048            static final long serialVersionUID = 5619862273774652856L;
049    
050        /** The underlying database Object which is proxied */
051        protected Persistent obj = null;
052    
053        /**
054         * Constructs a new TorqueObject
055         *
056         */
057        public TorqueObject()
058        {
059        }
060    
061        /**
062         * Constructs a new Object with the specified name.
063         *
064         * @param name The name of the new object.
065         */
066        public TorqueObject(String name)
067        {
068            this.setName(name);
069        }
070    
071        /**
072         * This Constructor is used when a Manager
073         * has retrieved a list of Database Objects from the peer and
074         * must 'wrap' them into TorqueObjects.
075         *
076         * @param obj An Object from the peer
077         */
078       public  TorqueObject(Persistent obj)
079        {
080            this.obj = obj;
081        }
082    
083        /**
084         * Returns the underlying Object for the Peer
085         *
086         * @return The underlying persistent object
087         *
088         */
089        public abstract Persistent getPersistentObj();
090    
091        /**
092         * Returns the name of this object
093         *
094         * @return The name of the object
095         */
096        public abstract String getName();
097    
098        /**
099         * 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    }