KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dataobjects > jdbc > JoinedDataField


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.dataobjects.jdbc;
66
67 import com.jcorporate.expresso.core.dataobjects.DataException;
68 import com.jcorporate.expresso.core.dataobjects.DataField;
69 import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData;
70 import com.jcorporate.expresso.core.dataobjects.DataObject;
71 import com.jcorporate.expresso.core.db.DBException;
72 import org.apache.log4j.Logger;
73
74 import java.io.InputStream JavaDoc;
75 import java.math.BigDecimal JavaDoc;
76 import java.util.Date JavaDoc;
77 import java.util.Map JavaDoc;
78
79 /**
80  * Data Field that is aware of local/forein key relations. If you set the
81  * local key, it will set the foreign key as well. This i
82  *
83  * @author Michael Rimov
84  */

85
86 public class JoinedDataField implements DataField {
87
88     private static final Logger log = Logger.getLogger(JoinedDataField.class);
89
90     /**
91      * Instance to the owner data object
92      */

93     private JoinedDataObject myOwner;
94
95     /**
96      * Instance to the local data object
97      */

98     private DataObject localObj;
99
100     /**
101      * Intance to the local field name
102      */

103     private String JavaDoc localField;
104
105     /**
106      * Instance to the foreign data object
107      */

108     private DataObject foreignObj;
109
110     /**
111      * instance to the foreign field
112      */

113     private String JavaDoc foreignField;
114
115     /**
116      * Protected constructor. Do not use normally.
117      */

118     protected JoinedDataField() {
119     }
120
121     /**
122      * Construct a new JoinedDataField object
123      *
124      * @param owner the creating data object
125      * @param localObj the 'Left' DataObject
126      * @param localField the 'Left' join field
127      * @param remoteObj the 'Right' DataObject
128      * @param foreignField the 'Right' join field.
129      * @return an instantiated JoinedDataField instance.
130      */

131     public static JoinedDataField getInstance(JoinedDataObject owner,
132                                               DataObject localObj, String JavaDoc localField,
133                                               DataObject remoteObj, String JavaDoc foreignField) {
134
135         JoinedDataField returnValue = new JoinedDataField();
136
137         returnValue.myOwner = owner;
138         returnValue.localObj = localObj;
139         returnValue.localField = localField;
140         returnValue.foreignObj = remoteObj;
141         returnValue.foreignField = foreignField;
142         return returnValue;
143     }
144
145     /**
146      * Retrieve the value of the object. It is the same with the local key AND
147      * the foreign key.
148      *
149      * @return java.lang.Object
150      */

151     public Object JavaDoc getValue() {
152         try {
153             return localObj.getDataField(localField).getValue();
154         } catch (DBException ex) {
155             log.error("Error getting embedded data field", ex);
156             return null;
157         }
158     }
159
160
161     public String JavaDoc asString() {
162         try {
163             return localObj.getDataField(localField).asString();
164         } catch (DBException ex) {
165             log.error("Error getting embedded data field", ex);
166             return null;
167         }
168     }
169
170     public Boolean JavaDoc asBoolean() {
171         try {
172             return localObj.getDataField(localField).asBoolean();
173         } catch (DBException ex) {
174             log.error("Error getting embedded data field", ex);
175             return null;
176         }
177     }
178
179
180     public Integer JavaDoc asInteger() {
181         try {
182             return localObj.getDataField(localField).asInteger();
183         } catch (DBException ex) {
184             log.error("Error getting embedded data field", ex);
185             return null;
186         }
187     }
188
189     public Date JavaDoc asDate() {
190         try {
191             return localObj.getDataField(localField).asDate();
192         } catch (DBException ex) {
193             log.error("Error getting embedded data field", ex);
194             return null;
195         }
196     }
197
198     public BigDecimal JavaDoc asBigDecimal() {
199         try {
200             return localObj.getDataField(localField).asBigDecimal();
201         } catch (DBException ex) {
202             log.error("Error getting embedded data field", ex);
203             return null;
204         }
205     }
206
207     public Double JavaDoc asDouble() {
208         try {
209             return localObj.getDataField(localField).asDouble();
210         } catch (DBException ex) {
211             log.error("Error getting embedded data field", ex);
212             return null;
213         }
214     }
215
216     /**
217      * @deprecated 8/04 v.5.5 do not expose this cache
218      */

219     public Object JavaDoc getOriginalValue() {
220         try {
221             return localObj.getDataField(localField).getOriginalValue();
222         } catch (DBException ex) {
223             log.error("Error getting embedded data field", ex);
224             return null;
225         }
226     }
227
228     public InputStream JavaDoc asStream() {
229         try {
230             return localObj.getDataField(localField).asStream();
231         } catch (DBException ex) {
232             log.error("Error getting embedded data field", ex);
233             return null;
234         }
235     }
236
237     public boolean isChanged() {
238         try {
239             return localObj.getDataField(localField).isChanged();
240         } catch (DBException ex) {
241             log.error("Error getting embedded data field", ex);
242             return false;
243         }
244     }
245
246     public boolean isValueSet() {
247         try {
248             return localObj.getDataField(localField).isValueSet();
249         } catch (DBException ex) {
250             log.error("Error getting embedded data field", ex);
251             return false;
252         }
253     }
254
255     public void checkValue() throws DataException {
256         try {
257             localObj.getDataField(localField).checkValue();
258             foreignObj.getDataField(foreignField).checkValue();
259         } catch (DBException ex) {
260             log.error("Error getting embedded data field", ex);
261             throw new DataException("Error getting embedded data field", ex);
262         }
263     }
264
265     public void resetChanged() {
266         try {
267             localObj.getDataField(localField).resetChanged();
268             foreignObj.getDataField(foreignField).resetChanged();
269         } catch (DBException ex) {
270             log.error("Error getting embedded data field", ex);
271             return;
272         }
273     }
274
275     public void setValue(Object JavaDoc newValue) {
276         try {
277             localObj.getDataField(localField).setValue(newValue);
278             foreignObj.getDataField(foreignField).setValue(newValue);
279         } catch (DBException ex) {
280             log.error("Error getting embedded data field", ex);
281             return;
282         }
283     }
284
285     public boolean isNull() {
286         try {
287             return localObj.getDataField(localField).isNull();
288         } catch (DBException ex) {
289             log.error("Error getting embedded data field", ex);
290             return true;
291         }
292     }
293
294     public void setAttribute(String JavaDoc attributeName, Object JavaDoc value) {
295         try {
296             localObj.getDataField(localField).setAttribute(attributeName, value);
297             foreignObj.getDataField(foreignField).setAttribute(attributeName, value);
298         } catch (DBException ex) {
299             log.error("Error getting embedded data field", ex);
300             return;
301         }
302     }
303
304     public Object JavaDoc getAttribute(String JavaDoc attributeName) {
305         try {
306             return localObj.getDataField(localField).getAttribute(attributeName);
307         } catch (DBException ex) {
308             log.error("Error getting embedded data field", ex);
309             return null;
310         }
311     }
312
313     public Map JavaDoc getAllAttributes() {
314         try {
315             return localObj.getDataField(localField).getAllAttributes();
316         } catch (DBException ex) {
317             log.error("Error getting embedded data field", ex);
318             return null;
319         }
320     }
321
322     public DataObject getOwner() {
323         return this.myOwner;
324     }
325
326     public void setOwner(DataObject newOwner) {
327         if (!(newOwner instanceof JoinedDataObject)) {
328             throw new IllegalArgumentException JavaDoc("New Owner must be of type: JoinedDataObject");
329         }
330         this.myOwner = (JoinedDataObject) newOwner;
331     }
332
333     public DataFieldMetaData getFieldMetaData() {
334         try {
335             return localObj.getDataField(localField).getFieldMetaData();
336         } catch (DBException ex) {
337             log.error("Error getting embedded data field", ex);
338             return null;
339         }
340     }
341
342     public void setFieldMetaData(DataFieldMetaData newMetadata) {
343         try {
344             localObj.getDataField(localField).setFieldMetaData(newMetadata);
345         } catch (DBException ex) {
346             log.error("Error getting embedded data field", ex);
347             return;
348         }
349     }
350
351     /**
352      * call when add() or update() has occurred, and currentValue of data fields should be considered 'original value' for purposes of determining 'isChanged'
353      */

354     public void cacheIsChangedComparison() {
355         try {
356             localObj.getDataField(localField).cacheIsChangedComparison();
357         } catch (DBException e) {
358             log.error("Error setting orig value", e);
359         }
360     }
361
362 }
Popular Tags