KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > SimpleAttributeProperty


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.reflect;
20
21 import org.apache.cayenne.ObjectContext;
22 import org.apache.cayenne.Persistent;
23 import org.apache.cayenne.map.ObjAttribute;
24
25 /**
26  * A descriptor of an "attribute" persistent property.
27  *
28  * @since 3.0
29  * @author Andrus Adamchik
30  */

31 public class SimpleAttributeProperty extends BaseProperty implements AttributeProperty {
32
33     private ObjAttribute attribute;
34
35     public SimpleAttributeProperty(ClassDescriptor owner, Accessor accessor,
36             ObjAttribute attribute) {
37         super(owner, accessor);
38         this.attribute = attribute;
39     }
40
41     public boolean visit(PropertyVisitor visitor) {
42         return visitor.visitAttribute(this);
43     }
44
45     public ObjAttribute getAttribute() {
46         return attribute;
47     }
48
49     public Object JavaDoc readProperty(Object JavaDoc object) throws PropertyException {
50         resolveFault(object);
51         return super.readProperty(object);
52     }
53
54     public void writeProperty(Object JavaDoc object, Object JavaDoc oldValue, Object JavaDoc newValue)
55             throws PropertyException {
56         resolveFault(object);
57         super.writeProperty(object, oldValue, newValue);
58     }
59
60     protected void resolveFault(Object JavaDoc object) throws PropertyException {
61         try {
62             Persistent persistent = (Persistent) object;
63             ObjectContext context = persistent.getObjectContext();
64             if (context != null) {
65                 context.prepareForAccess(persistent, getName(), false);
66             }
67         }
68         catch (ClassCastException JavaDoc e) {
69             throw new PropertyException("Object is not a Persistent: '"
70                     + object.getClass().getName()
71                     + "'", this, object, e);
72         }
73     }
74 }
Popular Tags