001 package org.apache.ojb.broker;
002
003 /* Copyright 2003-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import org.apache.commons.lang.builder.ToStringBuilder;
019 import org.apache.commons.lang.builder.ToStringStyle;
020
021 /**
022 * The <code>PBLifeCycleEvent</code> encapsulates information about
023 * the life-cycle of a persistent object.
024 * <br/>
025 * NOTE:
026 * <br/>
027 * Because of performance reasons OJB intern reuse instances of this class
028 * by reset target object.
029 *
030 * @author Armin Waibel
031 * @version $Id: PBLifeCycleEvent.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
032 */
033 public final class PBLifeCycleEvent extends PersistenceBrokerEvent
034 {
035 /** Denotes an event that happens before the insertion of an object. */
036 public static final int TYPE_BEFORE_INSERT = 1;
037 /** Denotes an event that happens before the deletion of an object. */
038 public static final int TYPE_BEFORE_DELETE = 2;
039 /** Denotes an event that happens before the update of an object. */
040 public static final int TYPE_BEFORE_UPDATE = 3;
041 /** Denotes an event that happens after the update of an object. */
042 public static final int TYPE_AFTER_UPDATE = 4;
043 /** Denotes an event that happens after the deletion of an object. */
044 public static final int TYPE_AFTER_DELETE = 5;
045 /** Denotes an event that happens after the lookup of an object. */
046 public static final int TYPE_AFTER_LOOKUP = 6;
047 /** Denotes an event that happens after the insertion of an object. */
048 public static final int TYPE_AFTER_INSERT = 7;
049
050 private Type eventType;
051 private Object target;
052
053 /**
054 * Creates a new event instance.
055 *
056 * @param broker The broker
057 * @param target The object which caused the event
058 * @param eventType The type of the event
059 */
060 public PBLifeCycleEvent(PersistenceBroker broker, Object target, Type eventType)
061 {
062 super(broker);
063 this.target = target;
064 this.eventType = eventType;
065 }
066
067 /**
068 * Creates a new event instance.
069 *
070 * @param broker The broker
071 * @param type The type of the event
072 */
073 public PBLifeCycleEvent(PersistenceBroker broker, Type type)
074 {
075 super(broker);
076 this.eventType = type;
077 }
078
079 /**
080 * Returns the target object as an instance of {@link PersistenceBrokerAware} if possible.
081 *
082 * @return The {@link PersistenceBrokerAware} instance if there is a target and it implements
083 * this interface
084 */
085 public PersistenceBrokerAware getPersitenceBrokerAware()
086 {
087 if ((target != null) && (target instanceof PersistenceBrokerAware))
088 {
089 return (PersistenceBrokerAware)target;
090 }
091 else
092 {
093 return null;
094 }
095 }
096
097 /**
098 * Set the object that caused the event.
099 *
100 * @param obj The object
101 */
102 public void setTarget(Object obj)
103 {
104 this.target = obj;
105 }
106
107 /**
108 * Returns the object that caused the event.
109 *
110 * @return The object
111 */
112 public Object getTarget()
113 {
114 return target;
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 public String toString()
121 {
122 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
123 buf.append("target object", target).
124 append("source object", getSource()).
125 append("eventType", eventType.toString());
126 return buf.toString();
127 }
128
129 /**
130 * Returns the event type.
131 *
132 * @return The event type
133 */
134 public Type getEventType()
135 {
136 return eventType;
137 }
138
139 /**
140 * Enum-like class for the event types.
141 */
142 public static class Type
143 {
144 /** Denotes an event that happens before the insertion of an object. */
145 public static final Type BEFORE_INSERT = new Type(TYPE_BEFORE_INSERT);
146 /** Denotes an event that happens before the update of an object. */
147 public static final Type BEFORE_UPDATE = new Type(TYPE_BEFORE_UPDATE);
148 /** Denotes an event that happens after the insertion of an object. */
149 public static final Type AFTER_INSERT = new Type(TYPE_AFTER_INSERT);
150 /** Denotes an event that happens after the update of an object. */
151 public static final Type AFTER_UPDATE = new Type(TYPE_AFTER_UPDATE);
152 /** Denotes an event that happens before the deletion of an object. */
153 public static final Type BEFORE_DELETE = new Type(TYPE_BEFORE_DELETE);
154 /** Denotes an event that happens after the deletion of an object. */
155 public static final Type AFTER_DELETE = new Type(TYPE_AFTER_DELETE);
156 /** Denotes an event that happens after the lookup of an object. */
157 public static final Type AFTER_LOOKUP = new Type(TYPE_AFTER_LOOKUP);
158
159 private int type;
160
161 /**
162 * Creates a new instance.
163 *
164 * @param type The type value
165 */
166 protected Type(int type)
167 {
168 this.type = type;
169 }
170
171 /**
172 * {@inheritDoc}
173 */
174 public final boolean equals(Object obj)
175 {
176 if (obj == this)
177 {
178 return true;
179 }
180 if (!(obj instanceof PBStateEvent))
181 {
182 return false;
183 }
184
185 return type == ((Type) obj).type;
186 }
187
188 /**
189 * {@inheritDoc}
190 */
191 public final int hashCode()
192 {
193 return type;
194 }
195
196 /**
197 * Returns the type id.
198 *
199 * @return The type id
200 */
201 public final int typeId()
202 {
203 return type;
204 }
205
206 /**
207 * {@inheritDoc}
208 */
209 public String toString()
210 {
211 return this.getClass().getName() + " [type= " + typeAsName(type) + "]";
212 }
213
214 private static String typeAsName(int type)
215 {
216 if (type == TYPE_AFTER_DELETE)
217 {
218 return "AFTER_DELETE";
219 }
220 else if (type == TYPE_AFTER_LOOKUP)
221 {
222 return "AFTER_LOOKUP";
223 }
224 else if (type == TYPE_AFTER_INSERT)
225 {
226 return "AFTER_INSERT";
227 }
228 else if (type == TYPE_AFTER_UPDATE)
229 {
230 return "AFTER_UPDATE";
231 }
232 else if (type == TYPE_BEFORE_DELETE)
233 {
234 return "BEFORE_DELETE";
235 }
236 else if (type == TYPE_BEFORE_INSERT)
237 {
238 return "BEFORE_INSERT";
239 }
240 else if (type == TYPE_BEFORE_UPDATE)
241 {
242 return "BEFORE_UPDATE";
243 }
244 else
245 {
246 throw new OJBRuntimeException("Could not find type with typeId " + type);
247 }
248 }
249 }
250 }