KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > fieldaccess > AnonymousPersistentFieldForInheritance


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

17
18 import java.util.Vector JavaDoc;
19
20 import org.apache.ojb.broker.metadata.ClassDescriptor;
21 import org.apache.ojb.broker.metadata.MetadataException;
22 import org.apache.ojb.broker.util.ClassHelper;
23
24 /**
25  * @author Houar TINE
26  * @version $Id: AnonymousPersistentFieldForInheritance.java,v 1.11.2.1 2004/07/27 00:33:11 arminw Exp $
27  */

28 public class AnonymousPersistentFieldForInheritance extends AnonymousPersistentField
29 {
30     private static final long serialVersionUID = -8367827598025566663L;
31
32     ClassDescriptor cld = null;
33
34     public AnonymousPersistentFieldForInheritance(ClassDescriptor cld, String JavaDoc fieldname)
35     {
36         super(fieldname);
37         this.cld = cld;
38     }
39
40     private void copyObjectToObject(Object JavaDoc fromObject, Object JavaDoc toObject)
41     {
42         Vector JavaDoc persistentFields = cld.getSuperPersistentFieldDescriptors();
43         for (int i = persistentFields.size() - 1; i >= 0; i--)
44         {
45             PersistentField pf = (PersistentField) persistentFields.get(i);
46             pf.set(toObject, pf.get(fromObject));
47         }
48     }
49
50     /**
51      * Field values of 'value' (base object) are copied to 'obj' (derived object)
52      * then obj is saved in a map
53      *
54      * @param obj - the base object instance
55      * @param value - the derived object instance
56      * @throws MetadataException
57      */

58     public synchronized void set(Object JavaDoc obj, Object JavaDoc value) throws MetadataException
59     {
60         copyObjectToObject(value, obj);
61         putToFieldCache(obj, value);
62     }
63
64     /**
65      * Field values of 'obj' (derived object) are copied to 'value' (base object)
66      * then value is returned as a referenced object.
67      *
68      * @param obj - the base object instance
69      * @throws MetadataException
70      */

71     public synchronized Object JavaDoc get(Object JavaDoc obj) throws MetadataException
72     {
73         Object JavaDoc value = getFromFieldCache(obj);
74         if (null == value)
75         {
76             try
77             {
78                 ClassDescriptor baseCld = cld.getRepository().getDescriptorFor(cld.getBaseClass());
79                 value = ClassHelper.buildNewObjectInstance(baseCld);
80             }
81             catch (Exception JavaDoc e)
82             {
83                 throw new MetadataException("Can't create new base class object for '" + cld.getBaseClass()+"'", e);
84             }
85             putToFieldCache(obj, value);
86         }
87
88         copyObjectToObject(obj, value);
89         return value;
90     }
91 }
92
Popular Tags