KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > generic > DataObjectBaseProperty


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.generic;
20
21 import org.apache.cayenne.DataObject;
22 import org.apache.cayenne.reflect.Property;
23 import org.apache.cayenne.reflect.PropertyException;
24 import org.apache.cayenne.reflect.PropertyVisitor;
25
26 /**
27  * A superclass of DataObject properties that accesses object via DataObject methods.
28  *
29  * @since 3.0
30  * @author Andrus Adamchik
31  */

32 abstract class DataObjectBaseProperty implements Property {
33
34     public abstract String JavaDoc getName();
35
36     public abstract void injectValueHolder(Object JavaDoc object) throws PropertyException;
37
38     public abstract boolean visit(PropertyVisitor visitor);
39
40     public Object JavaDoc readProperty(Object JavaDoc object) throws PropertyException {
41         try {
42             return toDataObject(object).readProperty(getName());
43         }
44         catch (Throwable JavaDoc th) {
45             throw new PropertyException(
46                     "Error reading DataObject property: " + getName(),
47                     this,
48                     object,
49                     th);
50         }
51     }
52
53     public void writeProperty(Object JavaDoc object, Object JavaDoc oldValue, Object JavaDoc newValue)
54             throws PropertyException {
55         try {
56             toDataObject(object).writeProperty(getName(), newValue);
57         }
58         catch (Throwable JavaDoc th) {
59             throw new PropertyException(
60                     "Error writing DataObject property: " + getName(),
61                     this,
62                     object,
63                     th);
64         }
65     }
66
67     public Object JavaDoc readPropertyDirectly(Object JavaDoc object) throws PropertyException {
68         try {
69             return toDataObject(object).readPropertyDirectly(getName());
70         }
71         catch (Throwable JavaDoc th) {
72             throw new PropertyException(
73                     "Error reading DataObject property: " + getName(),
74                     this,
75                     object,
76                     th);
77         }
78     }
79
80     public void writePropertyDirectly(Object JavaDoc object, Object JavaDoc oldValue, Object JavaDoc newValue)
81             throws PropertyException {
82         try {
83             toDataObject(object).writePropertyDirectly(getName(), newValue);
84         }
85         catch (Throwable JavaDoc th) {
86             throw new PropertyException(
87                     "Error writing DataObject property: " + getName(),
88                     this,
89                     object,
90                     th);
91         }
92     }
93
94     protected final DataObject toDataObject(Object JavaDoc object) throws PropertyException {
95         try {
96             return (DataObject) object;
97         }
98         catch (ClassCastException JavaDoc e) {
99             throw new PropertyException("Object is not a DataObject: '"
100                     + object.getClass().getName()
101                     + "'", this, object, e);
102         }
103     }
104 }
105
Popular Tags