KickJava   Java API By Example, From Geeks To Geeks.

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


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.CayenneRuntimeException;
22
23 /**
24  * An accessor for fields storing embedded objects. This accessor will initialize null
25  * fields with appropriate embeddable objects when needed during get and set calls.
26  *
27  * @since 3.0
28  * @author Andrus Adamchik
29  */

30 public class EmbeddedFieldAccessor implements Accessor {
31
32     protected String JavaDoc propertyPath;
33     protected Accessor embeddedAccessor;
34     protected Accessor embeddableAccessor;
35     protected Class JavaDoc embeddedClass;
36
37     public EmbeddedFieldAccessor(Class JavaDoc embeddableClass, Accessor embeddedAccessor,
38             Accessor embeddableAccessor) {
39         this.propertyPath = embeddedAccessor.getName()
40                 + "."
41                 + embeddableAccessor.getName();
42         this.embeddedClass = embeddableClass;
43         this.embeddableAccessor = embeddableAccessor;
44         this.embeddedAccessor = embeddedAccessor;
45     }
46
47     public String JavaDoc getName() {
48         return propertyPath;
49     }
50
51     public Object JavaDoc getValue(Object JavaDoc object) throws PropertyException {
52         return embeddableAccessor.getValue(getEmbeddable(object));
53     }
54
55     public void setValue(Object JavaDoc object, Object JavaDoc newValue) throws PropertyException {
56         embeddableAccessor.setValue(getEmbeddable(object), newValue);
57     }
58
59     /**
60      * Returns an embeddable object for the owner object, initializing embeddable if it is
61      * null. Currently supports only one level of embedding.
62      */

63     protected Object JavaDoc getEmbeddable(Object JavaDoc owner) {
64         Object JavaDoc embeddable = embeddedAccessor.getValue(owner);
65         if (embeddable == null) {
66             embeddable = createEmbeddable();
67             embeddedAccessor.setValue(owner, embeddable);
68         }
69
70         return embeddable;
71     }
72
73     protected Object JavaDoc createEmbeddable() {
74         if (embeddedClass == null) {
75             throw new NullPointerException JavaDoc(
76                     "Null embedded objectClass. Accessor wasn't initialized properly.");
77         }
78
79         try {
80             return embeddedClass.newInstance();
81         }
82         catch (Throwable JavaDoc e) {
83             throw new CayenneRuntimeException("Error creating embedded object of class '"
84                     + embeddedClass.getName()
85                     + "'", e);
86         }
87     }
88 }
89
Popular Tags