1 package org.apache.ojb.broker; 2 3 17 18 import org.apache.commons.lang.builder.ToStringBuilder; 19 import org.apache.commons.lang.builder.ToStringStyle; 20 21 33 public final class PBLifeCycleEvent extends PersistenceBrokerEvent 34 { 35 36 public static final int TYPE_BEFORE_INSERT = 1; 37 38 public static final int TYPE_BEFORE_DELETE = 2; 39 40 public static final int TYPE_BEFORE_UPDATE = 3; 41 42 public static final int TYPE_AFTER_UPDATE = 4; 43 44 public static final int TYPE_AFTER_DELETE = 5; 45 46 public static final int TYPE_AFTER_LOOKUP = 6; 47 48 public static final int TYPE_AFTER_INSERT = 7; 49 50 private Type eventType; 51 private Object target; 52 53 60 public PBLifeCycleEvent(PersistenceBroker broker, Object target, Type eventType) 61 { 62 super(broker); 63 this.target = target; 64 this.eventType = eventType; 65 } 66 67 73 public PBLifeCycleEvent(PersistenceBroker broker, Type type) 74 { 75 super(broker); 76 this.eventType = type; 77 } 78 79 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 102 public void setTarget(Object obj) 103 { 104 this.target = obj; 105 } 106 107 112 public Object getTarget() 113 { 114 return target; 115 } 116 117 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 134 public Type getEventType() 135 { 136 return eventType; 137 } 138 139 142 public static class Type 143 { 144 145 public static final Type BEFORE_INSERT = new Type(TYPE_BEFORE_INSERT); 146 147 public static final Type BEFORE_UPDATE = new Type(TYPE_BEFORE_UPDATE); 148 149 public static final Type AFTER_INSERT = new Type(TYPE_AFTER_INSERT); 150 151 public static final Type AFTER_UPDATE = new Type(TYPE_AFTER_UPDATE); 152 153 public static final Type BEFORE_DELETE = new Type(TYPE_BEFORE_DELETE); 154 155 public static final Type AFTER_DELETE = new Type(TYPE_AFTER_DELETE); 156 157 public static final Type AFTER_LOOKUP = new Type(TYPE_AFTER_LOOKUP); 158 159 private int type; 160 161 166 protected Type(int type) 167 { 168 this.type = type; 169 } 170 171 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 191 public final int hashCode() 192 { 193 return type; 194 } 195 196 201 public final int typeId() 202 { 203 return type; 204 } 205 206 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 } 251 | Popular Tags |