001: /**********************************************************************
002: Copyright (c) 2003 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: 2003 Andy Jefferson - commented, javadocs
017: ...
018: **********************************************************************/package org.jpox.sco;
019:
020: import java.io.ObjectStreamException;
021:
022: import org.jpox.StateManager;
023: import org.jpox.state.FetchPlanState;
024:
025: /**
026: * A mutable second-class date object.
027: *
028: * @version $Revision: 1.26 $
029: */
030: public class Date extends java.util.Date implements SCO {
031: private transient StateManager ownerSM;
032: private transient Object owner;
033: private transient String fieldName;
034:
035: /**
036: * Creates a <tt>Date</tt> object that represents the time at which it was allocated.
037: * Assigns owning object and field name.
038: * @param ownerSM the owning object
039: * @param fieldName the owning field name
040: */
041: public Date(StateManager ownerSM, String fieldName) {
042: super ();
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.util.Date) o).getTime());
066: }
067:
068: /**
069: * Accessor for the unwrapped value that we are wrapping.
070: * @return The unwrapped value
071: */
072: public Object getValue() {
073: return new java.util.Date(getTime());
074: }
075:
076: /**
077: * Creates and returns a copy of this object.
078: *
079: * <p>Mutable second-class Objects are required to provide a public
080: * clone method in order to allow for copying PersistenceCapable
081: * objects. In contrast to Object.clone(), this method must not throw a
082: * CloneNotSupportedException.
083: * @return A clone of the object
084: */
085: public Object clone() {
086: Object obj = super .clone();
087:
088: ((Date) obj).unsetOwner();
089:
090: return obj;
091: }
092:
093: /**
094: * Mutator for the time.
095: * @param time The time (millisecs)
096: **/
097: public void setTime(long time) {
098: super .setTime(time);
099: makeDirty();
100: }
101:
102: /**
103: * Sets the year of this <tt>Date</tt> object to be the specified
104: * value plus 1900. This <code>Date</code> object is modified so
105: * that it represents a point in time within the specified year,
106: * with the month, date, hour, minute, and second the same as
107: * before, as interpreted in the local time zone. (Of course, if
108: * the date was February 29, for example, and the year is set to a
109: * non-leap year, then the new date will be treated as if it were
110: * on March 1.)
111: *
112: * @param year the year value.
113: * @see java.util.Calendar
114: * @deprecated As of JDK version 1.1,
115: * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
116: */
117: public void setYear(int year) {
118: super .setYear(year);
119: makeDirty();
120: }
121:
122: /**
123: * Sets the month of this date to the specified value. This
124: * <tt>Date</tt> object is modified so that it represents a point
125: * in time within the specified month, with the year, date, hour,
126: * minute, and second the same as before, as interpreted in the
127: * local time zone. If the date was October 31, for example, and
128: * the month is set to June, then the new date will be treated as
129: * if it were on July 1, because June has only 30 days.
130: *
131: * @param month the month value between 0-11.
132: * @see java.util.Calendar
133: * @deprecated As of JDK version 1.1,
134: * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
135: */
136: public void setMonth(int month) {
137: super .setMonth(month);
138: makeDirty();
139: }
140:
141: /**
142: * Sets the day of the month of this <tt>Date</tt> object to the
143: * specified value. This <tt>Date</tt> object is modified so that
144: * it represents a point in time within the specified day of the
145: * month, with the year, month, hour, minute, and second the same
146: * as before, as interpreted in the local time zone. If the date
147: * was April 30, for example, and the date is set to 31, then it
148: * will be treated as if it were on May 1, because April has only
149: * 30 days.
150: *
151: * @param date the day of the month value between 1-31.
152: * @see java.util.Calendar
153: * @deprecated As of JDK version 1.1,
154: * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
155: */
156: public void setDate(int date) {
157: super .setDate(date);
158: makeDirty();
159: }
160:
161: /**
162: * Sets the hour of this <tt>Date</tt> object to the specified value.
163: * This <tt>Date</tt> object is modified so that it represents a point
164: * in time within the specified hour of the day, with the year, month,
165: * date, minute, and second the same as before, as interpreted in the
166: * local time zone.
167: *
168: * @param hours the hour value.
169: * @see java.util.Calendar
170: * @deprecated As of JDK version 1.1,
171: * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
172: */
173: public void setHours(int hours) {
174: super .setHours(hours);
175: makeDirty();
176: }
177:
178: /**
179: * Sets the minutes of this <tt>Date</tt> object to the specified value.
180: * This <tt>Date</tt> object is modified so that it represents a point
181: * in time within the specified minute of the hour, with the year, month,
182: * date, hour, and second the same as before, as interpreted in the
183: * local time zone.
184: *
185: * @param minutes the value of the minutes.
186: * @see java.util.Calendar
187: * @deprecated As of JDK version 1.1,
188: * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
189: */
190: public void setMinutes(int minutes) {
191: super .setMinutes(minutes);
192: makeDirty();
193: }
194:
195: /**
196: * Sets the seconds of this <tt>Date</tt> to the specified value.
197: * This <tt>Date</tt> object is modified so that it represents a
198: * point in time within the specified second of the minute, with
199: * the year, month, date, hour, and minute the same as before, as
200: * interpreted in the local time zone.
201: *
202: * @param seconds the seconds value.
203: * @see java.util.Calendar
204: * @deprecated As of JDK version 1.1,
205: * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
206: */
207: public void setSeconds(int seconds) {
208: super .setSeconds(seconds);
209: makeDirty();
210: }
211:
212: /**
213: * Utility to unset the owner.
214: **/
215: public void unsetOwner() {
216: owner = null;
217: ownerSM = null;
218: fieldName = null;
219: }
220:
221: /**
222: * Accessor for the owner.
223: * @return The owner
224: **/
225: public Object getOwner() {
226: return owner;
227: }
228:
229: /**
230: * Accessor for the field name
231: * @return The field name
232: **/
233: public String getFieldName() {
234: return this .fieldName;
235: }
236:
237: /**
238: * Utility to mark the object as dirty
239: **/
240: public void makeDirty() {
241: if (ownerSM != null) {
242: ownerSM.getObjectManager().getApiAdapter().makeFieldDirty(
243: owner, fieldName);
244: }
245: }
246:
247: /**
248: * Method to detach a copy of this object.
249: * @param state State for detachment process
250: * @return The detached object
251: */
252: public Object detachCopy(FetchPlanState state) {
253: return new java.util.Date(getTime());
254: }
255:
256: /**
257: * Method to attached the passed value.
258: * @param value The new value
259: */
260: public void attachCopy(Object value) {
261: long oldValue = getTime();
262: initialise(value, false, true);
263:
264: // Check if the field has changed, and set the owner field as dirty if necessary
265: long newValue = ((java.util.Date) value).getTime();
266: if (oldValue != newValue) {
267: makeDirty();
268: }
269: }
270:
271: /**
272: * The writeReplace method is called when ObjectOutputStream is preparing to write the object to the stream. The
273: * ObjectOutputStream checks whether the class defines the writeReplace method. If the method is defined, the
274: * writeReplace method is called to allow the object to designate its replacement in the stream. The object returned
275: * should be either of the same type as the object passed in or an object that when read and resolved will result in
276: * an object of a type that is compatible with all references to the object.
277: *
278: * @return the replaced object
279: * @throws ObjectStreamException
280: */
281: protected Object writeReplace() throws ObjectStreamException {
282: return new java.util.Date(this.getTime());
283: }
284: }
|