KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > intercept > ObjectContextCallbackInterceptor


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.intercept;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.cayenne.DeleteDenyException;
25 import org.apache.cayenne.LifecycleListener;
26 import org.apache.cayenne.ObjectContext;
27 import org.apache.cayenne.PersistenceState;
28 import org.apache.cayenne.Persistent;
29 import org.apache.cayenne.map.DeleteRule;
30 import org.apache.cayenne.map.ObjEntity;
31 import org.apache.cayenne.map.ObjRelationship;
32 import org.apache.cayenne.reflect.ClassDescriptor;
33 import org.apache.cayenne.reflect.LifecycleCallbackRegistry;
34
35 /**
36  * Implements JPA-compliant "PrePersist", "PreRemove" callbacks for the ObjectContext
37  * operations. <p/>Depending on how callbacks are registered, they are invoked either on
38  * the persistent object instances themselves or on an instance of an arbitrary listener
39  * class. Signature of a method of a persistent object is <code>"void method()"</code>,
40  * while for a non-persistent listener it is <code>"void
41  * method(Object)"</code>.
42  *
43  * @since 3.0
44  * @author Andrus Adamchik
45  */

46 public class ObjectContextCallbackInterceptor extends ObjectContextDecorator {
47
48     protected LifecycleCallbackRegistry callbackRegistry;
49
50     public void setContext(ObjectContext context) {
51         super.setContext(context);
52
53         callbackRegistry = (context != null) ? context
54                 .getEntityResolver()
55                 .getCallbackRegistry() : null;
56     }
57
58     /**
59      * Creates a new object, applying "PrePersist" callbacks to it.
60      */

61     public Persistent newObject(Class JavaDoc persistentClass) {
62         Persistent object = super.newObject(persistentClass);
63         callbackRegistry.performCallbacks(LifecycleListener.PRE_PERSIST, object);
64         return object;
65     }
66
67     /**
68      * Registers a new object and performs a "PrePersist" callback on it.
69      */

70     public void registerNewObject(Object JavaDoc object) {
71         super.registerNewObject(object);
72         callbackRegistry.performCallbacks(LifecycleListener.PRE_PERSIST, object);
73     }
74
75     /**
76      * Deletes an object, applying "PreRemove" callbacks to it and all its cascaded
77      * dependencies.
78      */

79     public void deleteObject(Persistent object) throws DeleteDenyException {
80         applyPreRemoveCallbacks(object);
81         super.deleteObject(object);
82     }
83
84     /**
85      * Recursively applies PreRemove callbacks to an object and objects that will be
86      * cascaded
87      */

88     void applyPreRemoveCallbacks(Persistent object) {
89
90         if (object.getPersistenceState() != PersistenceState.NEW) {
91             callbackRegistry.performCallbacks(LifecycleListener.PRE_REMOVE, object);
92         }
93
94         ObjEntity entity = getEntityResolver().lookupObjEntity(object);
95         ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(
96                 entity.getName());
97
98         Iterator JavaDoc it = entity.getRelationships().iterator();
99         while (it.hasNext()) {
100
101             ObjRelationship relationship = (ObjRelationship) it.next();
102             if (relationship.getDeleteRule() == DeleteRule.CASCADE) {
103
104                 Object JavaDoc related = descriptor
105                         .getProperty(relationship.getName())
106                         .readProperty(object);
107
108                 if (related == null) {
109                     // do nothing
110
}
111                 else if (related instanceof Collection JavaDoc) {
112                     Iterator JavaDoc relatedObjects = ((Collection JavaDoc) related).iterator();
113                     while (relatedObjects.hasNext()) {
114                         applyPreRemoveCallbacks((Persistent) relatedObjects.next());
115                     }
116                 }
117                 else {
118                     applyPreRemoveCallbacks((Persistent) related);
119                 }
120             }
121         }
122     }
123 }
124
Popular Tags