001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.core.dataobjects.jdbc;
066:
067: import com.jcorporate.expresso.core.dataobjects.DataException;
068: import com.jcorporate.expresso.core.dataobjects.DataField;
069: import com.jcorporate.expresso.core.dataobjects.DataFieldMetaData;
070: import com.jcorporate.expresso.core.dataobjects.DataObject;
071: import com.jcorporate.expresso.core.db.DBException;
072: import org.apache.log4j.Logger;
073:
074: import java.io.InputStream;
075: import java.math.BigDecimal;
076: import java.util.Date;
077: import java.util.Map;
078:
079: /**
080: * Data Field that is aware of local/forein key relations. If you set the
081: * local key, it will set the foreign key as well. This i
082: *
083: * @author Michael Rimov
084: */
085:
086: public class JoinedDataField implements DataField {
087:
088: private static final Logger log = Logger
089: .getLogger(JoinedDataField.class);
090:
091: /**
092: * Instance to the owner data object
093: */
094: private JoinedDataObject myOwner;
095:
096: /**
097: * Instance to the local data object
098: */
099: private DataObject localObj;
100:
101: /**
102: * Intance to the local field name
103: */
104: private String localField;
105:
106: /**
107: * Instance to the foreign data object
108: */
109: private DataObject foreignObj;
110:
111: /**
112: * instance to the foreign field
113: */
114: private String foreignField;
115:
116: /**
117: * Protected constructor. Do not use normally.
118: */
119: protected JoinedDataField() {
120: }
121:
122: /**
123: * Construct a new JoinedDataField object
124: *
125: * @param owner the creating data object
126: * @param localObj the 'Left' DataObject
127: * @param localField the 'Left' join field
128: * @param remoteObj the 'Right' DataObject
129: * @param foreignField the 'Right' join field.
130: * @return an instantiated JoinedDataField instance.
131: */
132: public static JoinedDataField getInstance(JoinedDataObject owner,
133: DataObject localObj, String localField,
134: DataObject remoteObj, String foreignField) {
135:
136: JoinedDataField returnValue = new JoinedDataField();
137:
138: returnValue.myOwner = owner;
139: returnValue.localObj = localObj;
140: returnValue.localField = localField;
141: returnValue.foreignObj = remoteObj;
142: returnValue.foreignField = foreignField;
143: return returnValue;
144: }
145:
146: /**
147: * Retrieve the value of the object. It is the same with the local key AND
148: * the foreign key.
149: *
150: * @return java.lang.Object
151: */
152: public Object getValue() {
153: try {
154: return localObj.getDataField(localField).getValue();
155: } catch (DBException ex) {
156: log.error("Error getting embedded data field", ex);
157: return null;
158: }
159: }
160:
161: public String 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 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: public Integer asInteger() {
180: try {
181: return localObj.getDataField(localField).asInteger();
182: } catch (DBException ex) {
183: log.error("Error getting embedded data field", ex);
184: return null;
185: }
186: }
187:
188: public Date asDate() {
189: try {
190: return localObj.getDataField(localField).asDate();
191: } catch (DBException ex) {
192: log.error("Error getting embedded data field", ex);
193: return null;
194: }
195: }
196:
197: public BigDecimal asBigDecimal() {
198: try {
199: return localObj.getDataField(localField).asBigDecimal();
200: } catch (DBException ex) {
201: log.error("Error getting embedded data field", ex);
202: return null;
203: }
204: }
205:
206: public Double asDouble() {
207: try {
208: return localObj.getDataField(localField).asDouble();
209: } catch (DBException ex) {
210: log.error("Error getting embedded data field", ex);
211: return null;
212: }
213: }
214:
215: /**
216: * @deprecated 8/04 v.5.5 do not expose this cache
217: */
218: public Object getOriginalValue() {
219: try {
220: return localObj.getDataField(localField).getOriginalValue();
221: } catch (DBException ex) {
222: log.error("Error getting embedded data field", ex);
223: return null;
224: }
225: }
226:
227: public InputStream asStream() {
228: try {
229: return localObj.getDataField(localField).asStream();
230: } catch (DBException ex) {
231: log.error("Error getting embedded data field", ex);
232: return null;
233: }
234: }
235:
236: public boolean isChanged() {
237: try {
238: return localObj.getDataField(localField).isChanged();
239: } catch (DBException ex) {
240: log.error("Error getting embedded data field", ex);
241: return false;
242: }
243: }
244:
245: public boolean isValueSet() {
246: try {
247: return localObj.getDataField(localField).isValueSet();
248: } catch (DBException ex) {
249: log.error("Error getting embedded data field", ex);
250: return false;
251: }
252: }
253:
254: public void checkValue() throws DataException {
255: try {
256: localObj.getDataField(localField).checkValue();
257: foreignObj.getDataField(foreignField).checkValue();
258: } catch (DBException ex) {
259: log.error("Error getting embedded data field", ex);
260: throw new DataException(
261: "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 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 attributeName, Object value) {
295: try {
296: localObj.getDataField(localField).setAttribute(
297: attributeName, value);
298: foreignObj.getDataField(foreignField).setAttribute(
299: attributeName, value);
300: } catch (DBException ex) {
301: log.error("Error getting embedded data field", ex);
302: return;
303: }
304: }
305:
306: public Object getAttribute(String attributeName) {
307: try {
308: return localObj.getDataField(localField).getAttribute(
309: attributeName);
310: } catch (DBException ex) {
311: log.error("Error getting embedded data field", ex);
312: return null;
313: }
314: }
315:
316: public Map getAllAttributes() {
317: try {
318: return localObj.getDataField(localField).getAllAttributes();
319: } catch (DBException ex) {
320: log.error("Error getting embedded data field", ex);
321: return null;
322: }
323: }
324:
325: public DataObject getOwner() {
326: return this .myOwner;
327: }
328:
329: public void setOwner(DataObject newOwner) {
330: if (!(newOwner instanceof JoinedDataObject)) {
331: throw new IllegalArgumentException(
332: "New Owner must be of type: JoinedDataObject");
333: }
334: this .myOwner = (JoinedDataObject) newOwner;
335: }
336:
337: public DataFieldMetaData getFieldMetaData() {
338: try {
339: return localObj.getDataField(localField).getFieldMetaData();
340: } catch (DBException ex) {
341: log.error("Error getting embedded data field", ex);
342: return null;
343: }
344: }
345:
346: public void setFieldMetaData(DataFieldMetaData newMetadata) {
347: try {
348: localObj.getDataField(localField).setFieldMetaData(
349: newMetadata);
350: } catch (DBException ex) {
351: log.error("Error getting embedded data field", ex);
352: return;
353: }
354: }
355:
356: /**
357: * call when add() or update() has occurred, and currentValue of data fields should be considered 'original value' for purposes of determining 'isChanged'
358: */
359: public void cacheIsChangedComparison() {
360: try {
361: localObj.getDataField(localField)
362: .cacheIsChangedComparison();
363: } catch (DBException e) {
364: log.error("Error setting orig value", e);
365: }
366: }
367:
368: }
|