KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > ObjectContextGraphAction


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.util;
20
21 import java.io.Serializable JavaDoc;
22
23 import org.apache.cayenne.ObjectContext;
24 import org.apache.cayenne.PersistenceState;
25 import org.apache.cayenne.Persistent;
26 import org.apache.cayenne.reflect.ArcProperty;
27 import org.apache.cayenne.reflect.ClassDescriptor;
28 import org.apache.cayenne.reflect.Property;
29
30 /**
31  * A base implementation of a helper class to handle
32  * {@link ObjectContext#propertyChanged(org.apache.cayenne.Persistent, String, Object, Object)}
33  * processing on behalf of an ObjectContext.
34  *
35  * @since 3.0
36  * @author Andrus Adamchik
37  */

38 public abstract class ObjectContextGraphAction implements Serializable JavaDoc {
39
40     protected ObjectContext context;
41
42     public ObjectContextGraphAction(ObjectContext context) {
43         this.context = context;
44     }
45
46     /**
47      * Handles property change in a Peristent object, routing to either
48      * {@link #handleArcPropertyChange(Persistent, ArcProperty, Object, Object)} or
49      * {@link #handleSimplePropertyChange(Persistent, String, Object, Object)}.
50      */

51     public void handlePropertyChange(
52             Persistent object,
53             String JavaDoc propertyName,
54             Object JavaDoc oldValue,
55             Object JavaDoc newValue) {
56
57         // translate ObjectContext generic property change callback to GraphManager terms
58
// (simple properties vs. relationships)
59

60         ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(
61                 object.getObjectId().getEntityName());
62         Property property = descriptor.getProperty(propertyName);
63
64         if (property instanceof ArcProperty) {
65             handleArcPropertyChange(object, (ArcProperty) property, oldValue, newValue);
66         }
67         else {
68             handleSimplePropertyChange(object, propertyName, oldValue, newValue);
69         }
70     }
71
72     protected abstract void handleArcPropertyChange(
73             Persistent object,
74             ArcProperty property,
75             Object JavaDoc oldValue,
76             Object JavaDoc newValue);
77
78     protected abstract void handleSimplePropertyChange(
79             Persistent object,
80             String JavaDoc propertyName,
81             Object JavaDoc oldValue,
82             Object JavaDoc newValue);
83
84     /**
85      * Changes object state to MODIFIED if needed, returning true if the change has
86      * occured, false if not.
87      */

88     protected boolean markAsDirty(Persistent object) {
89         if (object.getPersistenceState() == PersistenceState.COMMITTED) {
90             object.setPersistenceState(PersistenceState.MODIFIED);
91             return true;
92         }
93
94         return false;
95     }
96 }
97
Popular Tags