001: package org.apache.ojb.jdori.sql;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import javax.jdo.spi.PersistenceCapable;
019:
020: import org.apache.ojb.broker.PersistenceBroker;
021: import org.apache.ojb.broker.metadata.AttributeDescriptorBase;
022: import org.apache.ojb.broker.metadata.ClassDescriptor;
023:
024: import com.sun.jdori.FieldManager;
025: import com.sun.jdori.model.jdo.JDOClass;
026: import com.sun.jdori.model.jdo.JDOField;
027:
028: /**
029: * @author Thomas Mahler
030: */
031: public class OjbFieldManager implements FieldManager {
032:
033: private PersistenceCapable pc;
034:
035: private PersistenceBroker broker;
036:
037: /**
038: * Constructor for OjbFieldManager.
039: */
040: public OjbFieldManager() {
041: super ();
042: }
043:
044: /**
045: * Constructor for OjbFieldManager.
046: */
047: public OjbFieldManager(PersistenceCapable pPc) {
048: pc = pPc;
049: }
050:
051: /**
052: * Constructor for OjbFieldManager.
053: */
054: public OjbFieldManager(PersistenceCapable pPc,
055: PersistenceBroker pBroker) {
056: pc = pPc;
057: broker = pBroker;
058: }
059:
060: /**
061: * @see com.sun.jdori.FieldManager#storeBooleanField(int, boolean)
062: */
063: public void storeBooleanField(int fieldNum, boolean value) {
064: }
065:
066: /**
067: * @see com.sun.jdori.FieldManager#fetchBooleanField(int)
068: */
069: public boolean fetchBooleanField(int fieldNum) {
070: Boolean value = (Boolean) getValue(fieldNum);
071: return value.booleanValue();
072: }
073:
074: /**
075: * @see com.sun.jdori.FieldManager#storeCharField(int, char)
076: */
077: public void storeCharField(int fieldNum, char value) {
078: }
079:
080: /**
081: * @see com.sun.jdori.FieldManager#fetchCharField(int)
082: */
083: public char fetchCharField(int fieldNum) {
084: Character value = (Character) getValue(fieldNum);
085: return value.charValue();
086: }
087:
088: /**
089: * @see com.sun.jdori.FieldManager#storeByteField(int, byte)
090: */
091: public void storeByteField(int fieldNum, byte value) {
092: }
093:
094: /**
095: * @see com.sun.jdori.FieldManager#fetchByteField(int)
096: */
097: public byte fetchByteField(int fieldNum) {
098: Byte value = (Byte) getValue(fieldNum);
099: return value.byteValue();
100: }
101:
102: /**
103: * @see com.sun.jdori.FieldManager#storeShortField(int, short)
104: */
105: public void storeShortField(int fieldNum, short value) {
106: }
107:
108: /**
109: * @see com.sun.jdori.FieldManager#fetchShortField(int)
110: */
111: public short fetchShortField(int fieldNum) {
112: Short value = (Short) getValue(fieldNum);
113: return value.shortValue();
114: }
115:
116: /**
117: * @see com.sun.jdori.FieldManager#storeIntField(int, int)
118: */
119: public void storeIntField(int fieldNum, int value) {
120: }
121:
122: /**
123: * @see com.sun.jdori.FieldManager#fetchIntField(int)
124: */
125: public int fetchIntField(int fieldNum) {
126: Integer value = (Integer) getValue(fieldNum);
127: return value.intValue();
128: }
129:
130: /**
131: * @see com.sun.jdori.FieldManager#storeLongField(int, long)
132: */
133: public void storeLongField(int fieldNum, long value) {
134: }
135:
136: /**
137: * @see com.sun.jdori.FieldManager#fetchLongField(int)
138: */
139: public long fetchLongField(int fieldNum) {
140: Long value = (Long) getValue(fieldNum);
141: return value.longValue();
142: }
143:
144: /**
145: * @see com.sun.jdori.FieldManager#storeFloatField(int, float)
146: */
147: public void storeFloatField(int fieldNum, float value) {
148: }
149:
150: /**
151: * @see com.sun.jdori.FieldManager#fetchFloatField(int)
152: */
153: public float fetchFloatField(int fieldNum) {
154: Float value = (Float) getValue(fieldNum);
155: return value.floatValue();
156: }
157:
158: /**
159: * @see com.sun.jdori.FieldManager#storeDoubleField(int, double)
160: */
161: public void storeDoubleField(int fieldNum, double value) {
162: }
163:
164: /**
165: * @see com.sun.jdori.FieldManager#fetchDoubleField(int)
166: */
167: public double fetchDoubleField(int fieldNum) {
168: Double value = (Double) getValue(fieldNum);
169: return value.doubleValue();
170: }
171:
172: /**
173: * @see com.sun.jdori.FieldManager#storeStringField(int, String)
174: */
175: public void storeStringField(int fieldNum, String value) {
176: }
177:
178: /**
179: * @see com.sun.jdori.FieldManager#fetchStringField(int)
180: */
181: public String fetchStringField(int fieldNum) {
182: String value = (String) getValue(fieldNum);
183: return value;
184: }
185:
186: /**
187: * @see com.sun.jdori.FieldManager#storeObjectField(int, Object)
188: */
189: public void storeObjectField(int fieldNum, Object value) {
190: }
191:
192: /**
193: * @see com.sun.jdori.FieldManager#fetchObjectField(int)
194: */
195: public Object fetchObjectField(int fieldNum) {
196: Object value = getValue(fieldNum);
197: return value;
198: }
199:
200: /**
201: * Returns the pc.
202: * @return PersistenceCapable
203: */
204: public PersistenceCapable getPc() {
205: return pc;
206: }
207:
208: /**
209: * Sets the pc.
210: * @param pc The pc to set
211: */
212: public void setPc(PersistenceCapable pc) {
213: this .pc = pc;
214: }
215:
216: String getAttributeName(int fieldNum) {
217: JDOClass jdoClass = Helper.getJDOClass(pc.getClass());
218: JDOField jdoField = jdoClass.getField(fieldNum);
219: String attributeName = jdoField.getName();
220: return attributeName;
221: }
222:
223: /**
224: * retrieve the value of attribute[fieldNum] from the object.
225: * @return Object the value of attribute[fieldNum]
226: */
227: Object getValue(int fieldNum) {
228: String attributeName = getAttributeName(fieldNum);
229: ClassDescriptor cld = broker.getClassDescriptor(pc.getClass());
230:
231: // field could be a primitive typed attribute...
232: AttributeDescriptorBase fld = cld
233: .getFieldDescriptorByName(attributeName);
234: // field could be a reference attribute...
235: if (fld == null) {
236: fld = cld.getObjectReferenceDescriptorByName(attributeName);
237: }
238: // or it could be a collection attribute:
239: if (fld == null) {
240: fld = cld.getCollectionDescriptorByName(attributeName);
241: }
242: Object value = fld.getPersistentField().get(pc);
243: return value;
244: }
245: }
|