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: SqlTimestamp.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 timestamp object.
019: *
020: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
021: * @version $Revision: 1.6 $
022: */
023:
024: public class SqlTimestamp extends java.sql.Timestamp implements SCO {
025: private transient Object owner;
026: private transient String fieldName;
027:
028: /**
029: * Creates a <code>SqlTimestamp</code> object that represents the same time
030: * as the given <tt>java.sql.Timestamp</tt>. Assigns owning object and field
031: * name.
032: *
033: * @param owner the owning object
034: * @param fieldName the owning field name
035: * @param ts the initial timestamp value
036: */
037:
038: public SqlTimestamp(Object owner, String fieldName,
039: java.sql.Timestamp ts) {
040: super (ts.getTime());
041: super .setNanos(ts.getNanos());
042:
043: this .owner = owner;
044: this .fieldName = fieldName;
045: }
046:
047: public Object getOwner() {
048: return owner;
049: }
050:
051: public String getFieldName() {
052: return fieldName;
053: }
054:
055: public void makeDirty() {
056: if (owner != null)
057: JDOHelper.makeDirty(owner, fieldName);
058: }
059:
060: public void applyUpdates() {
061: }
062:
063: public void unsetOwner() {
064: owner = null;
065: fieldName = null;
066: }
067:
068: /**
069: * Creates and returns a copy of this object.
070: *
071: * <P>Mutable second-class Objects are required to provide a public
072: * clone method in order to allow for copying PersistenceCapable
073: * objects. In contrast to Object.clone(), this method must not throw a
074: * CloneNotSupportedException.
075: */
076:
077: public Object clone() {
078: Object obj = super .clone();
079:
080: ((SqlTimestamp) obj).unsetOwner();
081:
082: return obj;
083: }
084:
085: public void setTime(long time) {
086: super .setTime(time);
087: makeDirty();
088: }
089:
090: public void setNanos(int n) {
091: super .setNanos(n);
092: makeDirty();
093: }
094:
095: /**
096: * Sets the year of this <tt>Date</tt> object to be the specified
097: * value plus 1900. This <code>Date</code> object is modified so
098: * that it represents a point in time within the specified year,
099: * with the month, date, hour, minute, and second the same as
100: * before, as interpreted in the local time zone. (Of course, if
101: * the date was February 29, for example, and the year is set to a
102: * non-leap year, then the new date will be treated as if it were
103: * on March 1.)
104: *
105: * @param year the year value.
106: * @see java.util.Calendar
107: * @deprecated As of JDK version 1.1,
108: * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
109: */
110:
111: public void setYear(int year) {
112: super .setYear(year);
113: makeDirty();
114: }
115:
116: /**
117: * Sets the month of this date to the specified value. This
118: * <tt>Date</tt> object is modified so that it represents a point
119: * in time within the specified month, with the year, date, hour,
120: * minute, and second the same as before, as interpreted in the
121: * local time zone. If the date was October 31, for example, and
122: * the month is set to June, then the new date will be treated as
123: * if it were on July 1, because June has only 30 days.
124: *
125: * @param month the month value between 0-11.
126: * @see java.util.Calendar
127: * @deprecated As of JDK version 1.1,
128: * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
129: */
130:
131: public void setMonth(int month) {
132: super .setMonth(month);
133: makeDirty();
134: }
135:
136: /**
137: * Sets the day of the month of this <tt>Date</tt> object to the
138: * specified value. This <tt>Date</tt> object is modified so that
139: * it represents a point in time within the specified day of the
140: * month, with the year, month, hour, minute, and second the same
141: * as before, as interpreted in the local time zone. If the date
142: * was April 30, for example, and the date is set to 31, then it
143: * will be treated as if it were on May 1, because April has only
144: * 30 days.
145: *
146: * @param date the day of the month value between 1-31.
147: * @see java.util.Calendar
148: * @deprecated As of JDK version 1.1,
149: * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
150: */
151:
152: public void setDate(int date) {
153: super .setDate(date);
154: makeDirty();
155: }
156:
157: /**
158: * Sets the hour of this <tt>Date</tt> object to the specified value.
159: * This <tt>Date</tt> object is modified so that it represents a point
160: * in time within the specified hour of the day, with the year, month,
161: * date, minute, and second the same as before, as interpreted in the
162: * local time zone.
163: *
164: * @param hours the hour value.
165: * @see java.util.Calendar
166: * @deprecated As of JDK version 1.1,
167: * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
168: */
169:
170: public void setHours(int hours) {
171: super .setHours(hours);
172: makeDirty();
173: }
174:
175: /**
176: * Sets the minutes of this <tt>Date</tt> object to the specified value.
177: * This <tt>Date</tt> object is modified so that it represents a point
178: * in time within the specified minute of the hour, with the year, month,
179: * date, hour, and second the same as before, as interpreted in the
180: * local time zone.
181: *
182: * @param minutes the value of the minutes.
183: * @see java.util.Calendar
184: * @deprecated As of JDK version 1.1,
185: * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
186: */
187:
188: public void setMinutes(int minutes) {
189: super .setMinutes(minutes);
190: makeDirty();
191: }
192:
193: /**
194: * Sets the seconds of this <tt>Date</tt> to the specified value.
195: * This <tt>Date</tt> object is modified so that it represents a
196: * point in time within the specified second of the minute, with
197: * the year, month, date, hour, and minute the same as before, as
198: * interpreted in the local time zone.
199: *
200: * @param seconds the seconds value.
201: * @see java.util.Calendar
202: * @deprecated As of JDK version 1.1,
203: * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
204: */
205:
206: public void setSeconds(int seconds) {
207: super .setSeconds(seconds);
208: makeDirty();
209: }
210:
211: /**
212: * Replaces the object to be serialized with a java.sql.Timestamp object.
213: * Invoked by the serialization mechanism to obtain an alternative object
214: * to be used when writing an object to the stream.
215: *
216: * @return
217: * The <code>java.sql.Timestamp</code> to be serialized instead of this
218: * object.
219: */
220:
221: protected Object writeReplace() throws ObjectStreamException {
222: java.sql.Timestamp ts = new java.sql.Timestamp(getTime());
223:
224: ts.setNanos(getNanos());
225:
226: return ts;
227: }
228: }
|