001: /**********************************************************************
002: Copyright (c) 2002 Mike Martin and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.sco;
018:
019: import java.io.ObjectStreamException;
020:
021: import org.jpox.StateManager;
022: import org.jpox.state.FetchPlanState;
023:
024: /**
025: * A mutable second-class SQL timestamp object.
026: *
027: * @version $Revision: 1.25 $
028: */
029: public class SqlTimestamp extends java.sql.Timestamp implements SCO {
030: private transient StateManager ownerSM;
031: private transient Object owner;
032: private transient String fieldName;
033:
034: /**
035: * Creates a <code>SqlTimestamp</code> object that represents the time at
036: * which it was allocated. Assigns owning object and field name.
037: *
038: * @param ownerSM the owning object
039: * @param fieldName the owning field name
040: */
041: public SqlTimestamp(StateManager ownerSM, String fieldName) {
042: super (0);
043:
044: if (ownerSM != null) {
045: this .ownerSM = ownerSM;
046: this .owner = ownerSM.getObject();
047: }
048: this .fieldName = fieldName;
049: }
050:
051: /**
052: * Method to initialise the SCO for use.
053: */
054: public void initialise() {
055: }
056:
057: /**
058: * Method to initialise the SCO from an existing value.
059: * @param o The Object
060: * @param forInsert Whether the object needs inserting in the datastore with this value
061: * @param forUpdate Whether to update the datastore with this value
062: */
063: public void initialise(Object o, boolean forInsert,
064: boolean forUpdate) {
065: super .setTime(((java.sql.Timestamp) o).getTime());
066: super .setNanos(((java.sql.Timestamp) o).getNanos());
067: }
068:
069: /**
070: * Accessor for the unwrapped value that we are wrapping.
071: * @return The unwrapped value
072: */
073: public Object getValue() {
074: return new java.sql.Timestamp(getTime());
075: }
076:
077: /**
078: * Creates and returns a copy of this object.
079: *
080: * <P>Mutable second-class Objects are required to provide a public
081: * clone method in order to allow for copying PersistenceCapable
082: * objects. In contrast to Object.clone(), this method must not throw a
083: * CloneNotSupportedException.
084: * @return A clone of the object
085: */
086: public Object clone() {
087: Object obj = super .clone();
088:
089: ((SqlTimestamp) obj).unsetOwner();
090:
091: return obj;
092: }
093:
094: /**
095: * Mutator for the time.
096: * @param time The time (millisecs)
097: **/
098: public void setTime(long time) {
099: super .setTime(time);
100: makeDirty();
101: }
102:
103: /**
104: * Mutator for the time in nanos.
105: * @param time_nanos The time (nanos)
106: **/
107: public void setNanos(int time_nanos) {
108: super .setNanos(time_nanos);
109: makeDirty();
110: }
111:
112: /**
113: * Sets the year of this <tt>Date</tt> object to be the specified
114: * value plus 1900. This <code>Date</code> object is modified so
115: * that it represents a point in time within the specified year,
116: * with the month, date, hour, minute, and second the same as
117: * before, as interpreted in the local time zone. (Of course, if
118: * the date was February 29, for example, and the year is set to a
119: * non-leap year, then the new date will be treated as if it were
120: * on March 1.)
121: *
122: * @param year the year value.
123: * @see java.util.Calendar
124: * @deprecated As of JDK version 1.1,
125: * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
126: */
127: public void setYear(int year) {
128: super .setYear(year);
129: makeDirty();
130: }
131:
132: /**
133: * Sets the month of this date to the specified value. This
134: * <tt>Date</tt> object is modified so that it represents a point
135: * in time within the specified month, with the year, date, hour,
136: * minute, and second the same as before, as interpreted in the
137: * local time zone. If the date was October 31, for example, and
138: * the month is set to June, then the new date will be treated as
139: * if it were on July 1, because June has only 30 days.
140: *
141: * @param month the month value between 0-11.
142: * @see java.util.Calendar
143: * @deprecated As of JDK version 1.1,
144: * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
145: */
146: public void setMonth(int month) {
147: super .setMonth(month);
148: makeDirty();
149: }
150:
151: /**
152: * Sets the day of the month of this <tt>Date</tt> object to the
153: * specified value. This <tt>Date</tt> object is modified so that
154: * it represents a point in time within the specified day of the
155: * month, with the year, month, hour, minute, and second the same
156: * as before, as interpreted in the local time zone. If the date
157: * was April 30, for example, and the date is set to 31, then it
158: * will be treated as if it were on May 1, because April has only
159: * 30 days.
160: *
161: * @param date the day of the month value between 1-31.
162: * @see java.util.Calendar
163: * @deprecated As of JDK version 1.1,
164: * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
165: */
166: public void setDate(int date) {
167: super .setDate(date);
168: makeDirty();
169: }
170:
171: /**
172: * Sets the hour of this <tt>Date</tt> object to the specified value.
173: * This <tt>Date</tt> object is modified so that it represents a point
174: * in time within the specified hour of the day, with the year, month,
175: * date, minute, and second the same as before, as interpreted in the
176: * local time zone.
177: *
178: * @param hours the hour value.
179: * @see java.util.Calendar
180: * @deprecated As of JDK version 1.1,
181: * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
182: */
183: public void setHours(int hours) {
184: super .setHours(hours);
185: makeDirty();
186: }
187:
188: /**
189: * Sets the minutes of this <tt>Date</tt> object to the specified value.
190: * This <tt>Date</tt> object is modified so that it represents a point
191: * in time within the specified minute of the hour, with the year, month,
192: * date, hour, and second the same as before, as interpreted in the
193: * local time zone.
194: *
195: * @param minutes the value of the minutes.
196: * @see java.util.Calendar
197: * @deprecated As of JDK version 1.1,
198: * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
199: */
200: public void setMinutes(int minutes) {
201: super .setMinutes(minutes);
202: makeDirty();
203: }
204:
205: /**
206: * Sets the seconds of this <tt>Date</tt> to the specified value.
207: * This <tt>Date</tt> object is modified so that it represents a
208: * point in time within the specified second of the minute, with
209: * the year, month, date, hour, and minute the same as before, as
210: * interpreted in the local time zone.
211: *
212: * @param seconds the seconds value.
213: * @see java.util.Calendar
214: * @deprecated As of JDK version 1.1,
215: * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
216: */
217: public void setSeconds(int seconds) {
218: super .setSeconds(seconds);
219: makeDirty();
220: }
221:
222: /**
223: * Utility to unset the owner.
224: **/
225: public void unsetOwner() {
226: owner = null;
227: ownerSM = null;
228: fieldName = null;
229: }
230:
231: /**
232: * Accessor for the owner.
233: * @return The owner
234: **/
235: public Object getOwner() {
236: return owner;
237: }
238:
239: /**
240: * Accessor for the field name
241: * @return The field name
242: **/
243: public String getFieldName() {
244: return fieldName;
245: }
246:
247: /**
248: * Utility to mark the object as dirty
249: **/
250: public void makeDirty() {
251: if (ownerSM != null) {
252: ownerSM.getObjectManager().getApiAdapter().makeFieldDirty(
253: owner, fieldName);
254: }
255: }
256:
257: /**
258: * Method to detach a copy of this object.
259: * @param state State for detachment process
260: * @return The detached object
261: */
262: public Object detachCopy(FetchPlanState state) {
263: return new java.sql.Timestamp(getTime());
264: }
265:
266: /**
267: * Method to return an attached version for the passed StateManager and field, using the passed value.
268: * @param value The new value
269: */
270: public void attachCopy(Object value) {
271: long oldValue = getTime();
272: initialise(value, false, true);
273:
274: // Check if the field has changed, and set the owner field as dirty if necessary
275: long newValue = ((java.sql.Timestamp) value).getTime();
276: if (oldValue != newValue) {
277: makeDirty();
278: }
279: }
280:
281: /**
282: * The writeReplace method is called when ObjectOutputStream is preparing to write the object to the stream. The
283: * ObjectOutputStream checks whether the class defines the writeReplace method. If the method is defined, the
284: * writeReplace method is called to allow the object to designate its replacement in the stream. The object returned
285: * should be either of the same type as the object passed in or an object that when read and resolved will result in
286: * an object of a type that is compatible with all references to the object.
287: *
288: * @return the replaced object
289: * @throws ObjectStreamException
290: */
291: protected Object writeReplace() throws ObjectStreamException {
292: return new java.sql.Timestamp(this.getTime());
293: }
294: }
|