001: /*
002: * Copyright 2004 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: SqlDate.java,v 1.6 2004/01/18 03:01:06 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.sco;
012:
013: import com.triactive.jdo.SCO;
014: import java.io.ObjectStreamException;
015: import javax.jdo.JDOHelper;
016:
017: /**
018: * A mutable second-class SQL date object.
019: *
020: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
021: * @version $Revision: 1.6 $
022: */
023:
024: public class SqlDate extends java.sql.Date implements SCO {
025: private transient Object owner;
026: private transient String fieldName;
027:
028: /**
029: * Creates a <tt>SqlDate</tt> object that represents the same time as the
030: * given <tt>java.sql.Date</tt>. Assigns owning object and field name.
031: *
032: * @param owner the owning object
033: * @param fieldName the owning field name
034: * @param date the initial date value
035: */
036:
037: public SqlDate(Object owner, String fieldName, java.sql.Date date) {
038: super (date.getTime());
039:
040: this .owner = owner;
041: this .fieldName = fieldName;
042: }
043:
044: public Object getOwner() {
045: return owner;
046: }
047:
048: public String getFieldName() {
049: return fieldName;
050: }
051:
052: public void makeDirty() {
053: if (owner != null)
054: JDOHelper.makeDirty(owner, fieldName);
055: }
056:
057: public void applyUpdates() {
058: }
059:
060: public void unsetOwner() {
061: owner = null;
062: fieldName = null;
063: }
064:
065: /**
066: * Creates and returns a copy of this object.
067: *
068: * <P>Mutable second-class Objects are required to provide a public
069: * clone method in order to allow for copying PersistenceCapable
070: * objects. In contrast to Object.clone(), this method must not throw a
071: * CloneNotSupportedException.
072: */
073:
074: public Object clone() {
075: Object obj = super .clone();
076:
077: ((SqlDate) obj).unsetOwner();
078:
079: return obj;
080: }
081:
082: public void setTime(long time) {
083: super .setTime(time);
084: makeDirty();
085: }
086:
087: /**
088: * Sets the year of this <tt>Date</tt> object to be the specified
089: * value plus 1900. This <code>Date</code> object is modified so
090: * that it represents a point in time within the specified year,
091: * with the month, date, hour, minute, and second the same as
092: * before, as interpreted in the local time zone. (Of course, if
093: * the date was February 29, for example, and the year is set to a
094: * non-leap year, then the new date will be treated as if it were
095: * on March 1.)
096: *
097: * @param year the year value.
098: * @see java.util.Calendar
099: * @deprecated As of JDK version 1.1,
100: * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
101: */
102:
103: public void setYear(int year) {
104: super .setYear(year);
105: makeDirty();
106: }
107:
108: /**
109: * Sets the month of this date to the specified value. This
110: * <tt>Date</tt> object is modified so that it represents a point
111: * in time within the specified month, with the year, date, hour,
112: * minute, and second the same as before, as interpreted in the
113: * local time zone. If the date was October 31, for example, and
114: * the month is set to June, then the new date will be treated as
115: * if it were on July 1, because June has only 30 days.
116: *
117: * @param month the month value between 0-11.
118: * @see java.util.Calendar
119: * @deprecated As of JDK version 1.1,
120: * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
121: */
122:
123: public void setMonth(int month) {
124: super .setMonth(month);
125: makeDirty();
126: }
127:
128: /**
129: * Sets the day of the month of this <tt>Date</tt> object to the
130: * specified value. This <tt>Date</tt> object is modified so that
131: * it represents a point in time within the specified day of the
132: * month, with the year, month, hour, minute, and second the same
133: * as before, as interpreted in the local time zone. If the date
134: * was April 30, for example, and the date is set to 31, then it
135: * will be treated as if it were on May 1, because April has only
136: * 30 days.
137: *
138: * @param date the day of the month value between 1-31.
139: * @see java.util.Calendar
140: * @deprecated As of JDK version 1.1,
141: * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
142: */
143:
144: public void setDate(int date) {
145: super .setDate(date);
146: makeDirty();
147: }
148:
149: /**
150: * Replaces the object to be serialized with a java.sql.Date object.
151: * Invoked by the serialization mechanism to obtain an alternative object
152: * to be used when writing an object to the stream.
153: *
154: * @return
155: * The <code>java.sql.Date</code> to be serialized instead of this
156: * object.
157: */
158:
159: protected Object writeReplace() throws ObjectStreamException {
160: return new java.sql.Date(getTime());
161: }
162: }
|