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.5.2.1 2005/12/21 22:22:07 tomdz Exp $
032: */
033: public final class PBLifeCycleEvent extends PersistenceBrokerEvent {
034: /** Denotes an event that happens before the insertion of an object. */
035: public static final int TYPE_BEFORE_INSERT = 1;
036: /** Denotes an event that happens before the deletion of an object. */
037: public static final int TYPE_BEFORE_DELETE = 2;
038: /** Denotes an event that happens before the update of an object. */
039: public static final int TYPE_BEFORE_UPDATE = 3;
040: /** Denotes an event that happens after the update of an object. */
041: public static final int TYPE_AFTER_UPDATE = 4;
042: /** Denotes an event that happens after the deletion of an object. */
043: public static final int TYPE_AFTER_DELETE = 5;
044: /** Denotes an event that happens after the lookup of an object. */
045: public static final int TYPE_AFTER_LOOKUP = 6;
046: /** Denotes an event that happens after the insertion of an object. */
047: public static final int TYPE_AFTER_INSERT = 7;
048:
049: private Type eventType;
050: private Object target;
051:
052: /**
053: * Creates a new event instance.
054: *
055: * @param broker The broker
056: * @param target The object which caused the event
057: * @param eventType The type of the event
058: */
059: public PBLifeCycleEvent(PersistenceBroker broker, Object target,
060: Type eventType) {
061: super (broker);
062: this .target = target;
063: this .eventType = eventType;
064: }
065:
066: /**
067: * Creates a new event instance.
068: *
069: * @param broker The broker
070: * @param type The type of the event
071: */
072: public PBLifeCycleEvent(PersistenceBroker broker, Type type) {
073: super (broker);
074: this .eventType = type;
075: }
076:
077: /**
078: * Returns the target object as an instance of {@link PersistenceBrokerAware} if possible.
079: *
080: * @return The {@link PersistenceBrokerAware} instance if there is a target and it implements
081: * this interface
082: */
083: public PersistenceBrokerAware getPersitenceBrokerAware() {
084: if ((target != null)
085: && (target instanceof PersistenceBrokerAware)) {
086: return (PersistenceBrokerAware) target;
087: } else {
088: return null;
089: }
090: }
091:
092: /**
093: * Set the object that caused the event.
094: *
095: * @param obj The object
096: */
097: public void setTarget(Object obj) {
098: this .target = obj;
099: }
100:
101: /**
102: * Returns the object that caused the event.
103: *
104: * @return The object
105: */
106: public Object getTarget() {
107: return target;
108: }
109:
110: /**
111: * {@inheritDoc}
112: */
113: public String toString() {
114: ToStringBuilder buf = new ToStringBuilder(this ,
115: ToStringStyle.MULTI_LINE_STYLE);
116: buf.append("target object", target).append("source object",
117: getSource()).append("eventType", eventType.toString());
118: return buf.toString();
119: }
120:
121: /**
122: * Returns the event type.
123: *
124: * @return The event type
125: */
126: public Type getEventType() {
127: return eventType;
128: }
129:
130: /**
131: * Enum-like class for the event types.
132: */
133: public static class Type {
134: /** Denotes an event that happens before the insertion of an object. */
135: public static final Type BEFORE_INSERT = new Type(
136: TYPE_BEFORE_INSERT);
137: /** Denotes an event that happens before the update of an object. */
138: public static final Type BEFORE_UPDATE = new Type(
139: TYPE_BEFORE_UPDATE);
140: /** Denotes an event that happens after the insertion of an object. */
141: public static final Type AFTER_INSERT = new Type(
142: TYPE_AFTER_INSERT);
143: /** Denotes an event that happens after the update of an object. */
144: public static final Type AFTER_UPDATE = new Type(
145: TYPE_AFTER_UPDATE);
146: /** Denotes an event that happens before the deletion of an object. */
147: public static final Type BEFORE_DELETE = new Type(
148: TYPE_BEFORE_DELETE);
149: /** Denotes an event that happens after the deletion of an object. */
150: public static final Type AFTER_DELETE = new Type(
151: TYPE_AFTER_DELETE);
152: /** Denotes an event that happens after the lookup of an object. */
153: public static final Type AFTER_LOOKUP = new Type(
154: TYPE_AFTER_LOOKUP);
155:
156: private int type;
157:
158: /**
159: * Creates a new instance.
160: *
161: * @param type The type value
162: */
163: protected Type(int type) {
164: this .type = type;
165: }
166:
167: /**
168: * {@inheritDoc}
169: */
170: public final boolean equals(Object obj) {
171: if (obj == this ) {
172: return true;
173: }
174: if (!(obj instanceof PBStateEvent)) {
175: return false;
176: }
177:
178: return type == ((Type) obj).type;
179: }
180:
181: /**
182: * {@inheritDoc}
183: */
184: public final int hashCode() {
185: return type;
186: }
187:
188: /**
189: * Returns the type id.
190: *
191: * @return The type id
192: */
193: public final int typeId() {
194: return type;
195: }
196:
197: /**
198: * {@inheritDoc}
199: */
200: public String toString() {
201: return this .getClass().getName() + " [type= "
202: + typeAsName(type) + "]";
203: }
204:
205: private static String typeAsName(int type) {
206: if (type == TYPE_AFTER_DELETE) {
207: return "AFTER_DELETE";
208: } else if (type == TYPE_AFTER_LOOKUP) {
209: return "AFTER_LOOKUP";
210: } else if (type == TYPE_AFTER_INSERT) {
211: return "AFTER_INSERT";
212: } else if (type == TYPE_AFTER_UPDATE) {
213: return "AFTER_UPDATE";
214: } else if (type == TYPE_BEFORE_DELETE) {
215: return "BEFORE_DELETE";
216: } else if (type == TYPE_BEFORE_INSERT) {
217: return "BEFORE_INSERT";
218: } else if (type == TYPE_BEFORE_UPDATE) {
219: return "BEFORE_UPDATE";
220: } else {
221: throw new OJBRuntimeException(
222: "Could not find type with typeId " + type);
223: }
224: }
225: }
226: }
|