KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdori > sql > OjbFieldManager


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

16
17 import javax.jdo.spi.PersistenceCapable;
18
19 import org.apache.ojb.broker.PersistenceBroker;
20 import org.apache.ojb.broker.metadata.AttributeDescriptorBase;
21 import org.apache.ojb.broker.metadata.ClassDescriptor;
22
23 import com.sun.jdori.FieldManager;
24 import com.sun.jdori.model.jdo.JDOClass;
25 import com.sun.jdori.model.jdo.JDOField;
26
27 /**
28  * @author Thomas Mahler
29  */

30 public class OjbFieldManager implements FieldManager
31 {
32
33     private PersistenceCapable pc;
34
35     private PersistenceBroker broker;
36
37     /**
38      * Constructor for OjbFieldManager.
39      */

40     public OjbFieldManager()
41     {
42         super();
43     }
44
45     /**
46      * Constructor for OjbFieldManager.
47      */

48     public OjbFieldManager(PersistenceCapable pPc)
49     {
50         pc = pPc;
51     }
52
53     /**
54      * Constructor for OjbFieldManager.
55      */

56     public OjbFieldManager(PersistenceCapable pPc, PersistenceBroker pBroker)
57     {
58         pc = pPc;
59         broker = pBroker;
60     }
61
62     /**
63      * @see com.sun.jdori.FieldManager#storeBooleanField(int, boolean)
64      */

65     public void storeBooleanField(int fieldNum, boolean value)
66     {
67     }
68
69     /**
70      * @see com.sun.jdori.FieldManager#fetchBooleanField(int)
71      */

72     public boolean fetchBooleanField(int fieldNum)
73     {
74         Boolean JavaDoc value = (Boolean JavaDoc) getValue(fieldNum);
75         return value.booleanValue();
76     }
77
78     /**
79      * @see com.sun.jdori.FieldManager#storeCharField(int, char)
80      */

81     public void storeCharField(int fieldNum, char value)
82     {
83     }
84
85     /**
86      * @see com.sun.jdori.FieldManager#fetchCharField(int)
87      */

88     public char fetchCharField(int fieldNum)
89     {
90         Character JavaDoc value = (Character JavaDoc) getValue(fieldNum);
91         return value.charValue();
92     }
93
94     /**
95      * @see com.sun.jdori.FieldManager#storeByteField(int, byte)
96      */

97     public void storeByteField(int fieldNum, byte value)
98     {
99     }
100
101     /**
102      * @see com.sun.jdori.FieldManager#fetchByteField(int)
103      */

104     public byte fetchByteField(int fieldNum)
105     {
106         Byte JavaDoc value = (Byte JavaDoc) getValue(fieldNum);
107         return value.byteValue();
108     }
109
110     /**
111      * @see com.sun.jdori.FieldManager#storeShortField(int, short)
112      */

113     public void storeShortField(int fieldNum, short value)
114     {
115     }
116
117     /**
118      * @see com.sun.jdori.FieldManager#fetchShortField(int)
119      */

120     public short fetchShortField(int fieldNum)
121     {
122         Short JavaDoc value = (Short JavaDoc) getValue(fieldNum);
123         return value.shortValue();
124     }
125
126     /**
127      * @see com.sun.jdori.FieldManager#storeIntField(int, int)
128      */

129     public void storeIntField(int fieldNum, int value)
130     {
131     }
132
133     /**
134      * @see com.sun.jdori.FieldManager#fetchIntField(int)
135      */

136     public int fetchIntField(int fieldNum)
137     {
138         Integer JavaDoc value = (Integer JavaDoc) getValue(fieldNum);
139         return value.intValue();
140     }
141
142     /**
143      * @see com.sun.jdori.FieldManager#storeLongField(int, long)
144      */

145     public void storeLongField(int fieldNum, long value)
146     {
147     }
148
149     /**
150      * @see com.sun.jdori.FieldManager#fetchLongField(int)
151      */

152     public long fetchLongField(int fieldNum)
153     {
154         Long JavaDoc value = (Long JavaDoc) getValue(fieldNum);
155         return value.longValue();
156     }
157
158     /**
159      * @see com.sun.jdori.FieldManager#storeFloatField(int, float)
160      */

161     public void storeFloatField(int fieldNum, float value)
162     {
163     }
164
165     /**
166      * @see com.sun.jdori.FieldManager#fetchFloatField(int)
167      */

168     public float fetchFloatField(int fieldNum)
169     {
170         Float JavaDoc value = (Float JavaDoc) getValue(fieldNum);
171         return value.floatValue();
172     }
173
174     /**
175      * @see com.sun.jdori.FieldManager#storeDoubleField(int, double)
176      */

177     public void storeDoubleField(int fieldNum, double value)
178     {
179     }
180
181     /**
182      * @see com.sun.jdori.FieldManager#fetchDoubleField(int)
183      */

184     public double fetchDoubleField(int fieldNum)
185     {
186         Double JavaDoc value = (Double JavaDoc) getValue(fieldNum);
187         return value.doubleValue();
188     }
189
190     /**
191      * @see com.sun.jdori.FieldManager#storeStringField(int, String)
192      */

193     public void storeStringField(int fieldNum, String JavaDoc value)
194     {
195     }
196
197     /**
198      * @see com.sun.jdori.FieldManager#fetchStringField(int)
199      */

200     public String JavaDoc fetchStringField(int fieldNum)
201     {
202         String JavaDoc value = (String JavaDoc) getValue(fieldNum);
203         return value;
204     }
205
206     /**
207      * @see com.sun.jdori.FieldManager#storeObjectField(int, Object)
208      */

209     public void storeObjectField(int fieldNum, Object JavaDoc value)
210     {
211     }
212
213     /**
214      * @see com.sun.jdori.FieldManager#fetchObjectField(int)
215      */

216     public Object JavaDoc fetchObjectField(int fieldNum)
217     {
218         Object JavaDoc value = getValue(fieldNum);
219         return value;
220     }
221
222     /**
223      * Returns the pc.
224      * @return PersistenceCapable
225      */

226     public PersistenceCapable getPc()
227     {
228         return pc;
229     }
230
231     /**
232      * Sets the pc.
233      * @param pc The pc to set
234      */

235     public void setPc(PersistenceCapable pc)
236     {
237         this.pc = pc;
238     }
239
240     String JavaDoc getAttributeName(int fieldNum)
241     {
242         JDOClass jdoClass = Helper.getJDOClass(pc.getClass());
243         JDOField jdoField = jdoClass.getField(fieldNum);
244         String JavaDoc attributeName = jdoField.getName();
245         return attributeName;
246     }
247
248     /**
249      * retrieve the value of attribute[fieldNum] from the object.
250      * @return Object the value of attribute[fieldNum]
251      */

252     Object JavaDoc getValue(int fieldNum)
253     {
254         String JavaDoc attributeName = getAttributeName(fieldNum);
255         ClassDescriptor cld = broker.getClassDescriptor(pc.getClass());
256         
257         // field could be a primitive typed attribute...
258
AttributeDescriptorBase fld = cld.getFieldDescriptorByName(attributeName);
259         // field could be a reference attribute...
260
if (fld == null)
261         {
262             fld = cld.getObjectReferenceDescriptorByName(attributeName);
263         }
264         // or it could be a collection attribute:
265
if (fld == null)
266         {
267             fld = cld.getCollectionDescriptorByName(attributeName);
268         }
269         Object JavaDoc value = fld.getPersistentField().get(pc);
270         return value;
271     }
272 }
273
Popular Tags