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 534527 2007-05-02 16:10:59Z tv $
039: */
040:
041: public abstract class TorqueObject implements SecurityEntity,
042: Comparable, Persistent, Serializable {
043:
044: static final long serialVersionUID = 5619862273774652856L;
045:
046: /** The underlying database Object which is proxied */
047: protected Persistent obj = null;
048:
049: /**
050: * Constructs a new TorqueObject
051: *
052: */
053: public TorqueObject() {
054: }
055:
056: /**
057: * Constructs a new Object with the specified name.
058: *
059: * @param name The name of the new object.
060: */
061: public TorqueObject(String name) {
062: this .setName(name);
063: }
064:
065: /**
066: * This Constructor is used when a Manager
067: * has retrieved a list of Database Objects from the peer and
068: * must 'wrap' them into TorqueObjects.
069: *
070: * @param obj An Object from the peer
071: */
072: public TorqueObject(Persistent obj) {
073: this .obj = obj;
074: }
075:
076: /**
077: * Returns the underlying Object for the Peer
078: *
079: * @return The underlying persistent object
080: *
081: */
082: public abstract Persistent getPersistentObj();
083:
084: /**
085: * Returns the name of this object
086: *
087: * @return The name of the object
088: */
089: public abstract String getName();
090:
091: /**
092: * Sets the name of this object
093: *
094: * @param name The name of the object
095: */
096: public abstract void setName(String name);
097:
098: /**
099: * getter for the object primaryKey.
100: *
101: * @return the object primaryKey as an Object
102: */
103: public ObjectKey getPrimaryKey() {
104: Persistent p = getPersistentObj();
105: if (p != null) {
106: return p.getPrimaryKey();
107: } else {
108: return null;
109: }
110: }
111:
112: /**
113: * Sets the PrimaryKey for the object.
114: *
115: * @param primaryKey The new PrimaryKey for the object.
116: *
117: * @exception Exception This method might throw an exceptions
118: */
119: public void setPrimaryKey(ObjectKey primaryKey) throws Exception {
120: getPersistentObj().setPrimaryKey(primaryKey);
121: }
122:
123: /**
124: * Sets the PrimaryKey for the object.
125: *
126: * @param primaryKey the String should be of the form produced by
127: * ObjectKey.toString().
128: *
129: * @exception Exception This method might throw an exceptions
130: */
131: public void setPrimaryKey(String primaryKey) throws Exception {
132: getPersistentObj().setPrimaryKey(primaryKey);
133: }
134:
135: /**
136: * Returns whether the object has been modified, since it was
137: * last retrieved from storage.
138: *
139: * @return True if the object has been modified.
140: */
141: public boolean isModified() {
142: return getPersistentObj().isModified();
143: }
144:
145: /**
146: * Returns whether the object has ever been saved. This will
147: * be false, if the object was retrieved from storage or was created
148: * and then saved.
149: *
150: * @return true, if the object has never been persisted.
151: */
152: public boolean isNew() {
153: return getPersistentObj().isNew();
154: }
155:
156: /**
157: * Setter for the isNew attribute. This method will be called
158: * by Torque-generated children and Peers.
159: *
160: * @param b the state of the object.
161: */
162: public void setNew(boolean b) {
163: getPersistentObj().setNew(b);
164: }
165:
166: /**
167: * Sets the modified state for the object.
168: *
169: * @param m The new modified state for the object.
170: */
171: public void setModified(boolean m) {
172: getPersistentObj().setModified(m);
173: }
174:
175: /**
176: * Stores the object in the database. If the object is new,
177: * it inserts it; otherwise an update is performed.
178: *
179: * @param torqueName The name under which the object should be stored.
180: *
181: * @exception Exception This method might throw an exceptions
182: */
183: public void save(String torqueName) throws Exception {
184: getPersistentObj().save(torqueName);
185: }
186:
187: /**
188: * Stores the object in the database. If the object is new,
189: * it inserts it; otherwise an update is performed. This method
190: * is meant to be used as part of a transaction, otherwise use
191: * the save() method and the connection details will be handled
192: * internally
193: *
194: * @param con A Connection object to save the object
195: *
196: * @exception Exception This method might throw an exceptions
197: */
198: public void save(Connection con) throws Exception {
199: getPersistentObj().save(con);
200: }
201:
202: /**
203: * Makes changes made to the TorqueObject permanent.
204: *
205: * @throws TurbineSecurityException if there is a problem while
206: * saving data.
207: */
208: public abstract void save() throws TurbineSecurityException;
209:
210: /**
211: * Used for ordering TorqueObjects.
212: *
213: * @param obj The Object to compare to.
214: * @return -1 if the name of the other object is lexically greater than this
215: * group, 1 if it is lexically lesser, 0 if they are equal.
216: */
217: public int compareTo(Object obj) {
218: if (this .getClass() != obj.getClass()) {
219: throw new ClassCastException();
220: }
221: String name1 = ((SecurityEntity) obj).getName();
222: String name2 = this .getName();
223:
224: return name2.compareTo(name1);
225: }
226:
227: /**
228: * Compares this with another <code>BaseObject</code> instance. If
229: * <code>obj</code> is an instance of <code>BaseObject</code>, delegates to
230: * <code>equals(BaseObject)</code>. Otherwise, returns <code>false</code>.
231: *
232: * @param obj The object to compare to.
233: * @return Whether equal to the object specified.
234: */
235: public boolean equals(Object obj) {
236: if (obj != null && obj instanceof TorqueObject) {
237: return equals((TorqueObject) obj);
238: } else {
239: return false;
240: }
241: }
242:
243: /**
244: * Compares the primary key of this instance with the key of another.
245: *
246: * @param torqueObject The TorqueObject to compare to.
247: * @return Whether the primary keys are equal.
248: */
249: public boolean equals(TorqueObject torqueObject) {
250: if (torqueObject == null) {
251: return false;
252: }
253: if (this == torqueObject) {
254: return true;
255: } else if (getPrimaryKey() == null
256: || torqueObject.getPrimaryKey() == null) {
257: return false;
258: } else {
259: return getPrimaryKey().equals(torqueObject.getPrimaryKey());
260: }
261: }
262:
263: /**
264: * If the primary key is not <code>null</code>, return the hashcode of the
265: * primary key. Otherwise calls <code>Object.hashCode()</code>.
266: *
267: * @return an <code>int</code> value
268: */
269: public int hashCode() {
270: ObjectKey ok = getPrimaryKey();
271: if (ok == null) {
272: return super.hashCode();
273: }
274:
275: return ok.hashCode();
276: }
277: }
|