Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PBLifeCycleEvent |
|
| 2.6923076923076925;2.692 | ||||
PBLifeCycleEvent$Type |
|
| 2.6923076923076925;2.692 |
1 | package org.apache.ojb.broker; | |
2 | ||
3 | /* Copyright 2003-2005 The Apache Software Foundation | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | import org.apache.commons.lang.builder.ToStringBuilder; | |
19 | import org.apache.commons.lang.builder.ToStringStyle; | |
20 | ||
21 | /** | |
22 | * The <code>PBLifeCycleEvent</code> encapsulates information about | |
23 | * the life-cycle of a persistent object. | |
24 | * <br/> | |
25 | * NOTE: | |
26 | * <br/> | |
27 | * Because of performance reasons OJB intern reuse instances of this class | |
28 | * by reset target object. | |
29 | * | |
30 | * @author Armin Waibel | |
31 | * @version $Id: PBLifeCycleEvent.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $ | |
32 | */ | |
33 | public final class PBLifeCycleEvent extends PersistenceBrokerEvent | |
34 | { | |
35 | /** Denotes an event that happens before the insertion of an object. */ | |
36 | public static final int TYPE_BEFORE_INSERT = 1; | |
37 | /** Denotes an event that happens before the deletion of an object. */ | |
38 | public static final int TYPE_BEFORE_DELETE = 2; | |
39 | /** Denotes an event that happens before the update of an object. */ | |
40 | public static final int TYPE_BEFORE_UPDATE = 3; | |
41 | /** Denotes an event that happens after the update of an object. */ | |
42 | public static final int TYPE_AFTER_UPDATE = 4; | |
43 | /** Denotes an event that happens after the deletion of an object. */ | |
44 | public static final int TYPE_AFTER_DELETE = 5; | |
45 | /** Denotes an event that happens after the lookup of an object. */ | |
46 | public static final int TYPE_AFTER_LOOKUP = 6; | |
47 | /** Denotes an event that happens after the insertion of an object. */ | |
48 | public static final int TYPE_AFTER_INSERT = 7; | |
49 | ||
50 | private Type eventType; | |
51 | private Object target; | |
52 | ||
53 | /** | |
54 | * Creates a new event instance. | |
55 | * | |
56 | * @param broker The broker | |
57 | * @param target The object which caused the event | |
58 | * @param eventType The type of the event | |
59 | */ | |
60 | public PBLifeCycleEvent(PersistenceBroker broker, Object target, Type eventType) | |
61 | { | |
62 | super(broker); | |
63 | this.target = target; | |
64 | this.eventType = eventType; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Creates a new event instance. | |
69 | * | |
70 | * @param broker The broker | |
71 | * @param type The type of the event | |
72 | */ | |
73 | public PBLifeCycleEvent(PersistenceBroker broker, Type type) | |
74 | { | |
75 | super(broker); | |
76 | this.eventType = type; | |
77 | } | |
78 | ||
79 | /** | |
80 | * Returns the target object as an instance of {@link PersistenceBrokerAware} if possible. | |
81 | * | |
82 | * @return The {@link PersistenceBrokerAware} instance if there is a target and it implements | |
83 | * this interface | |
84 | */ | |
85 | public PersistenceBrokerAware getPersitenceBrokerAware() | |
86 | { | |
87 | if ((target != null) && (target instanceof PersistenceBrokerAware)) | |
88 | { | |
89 | return (PersistenceBrokerAware)target; | |
90 | } | |
91 | else | |
92 | { | |
93 | return null; | |
94 | } | |
95 | } | |
96 | ||
97 | /** | |
98 | * Set the object that caused the event. | |
99 | * | |
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 | } |