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:
016: Contributors:
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 SQL date object.
027: *
028: * @version $Revision: 1.24 $
029: */
030: public class SqlDate extends java.sql.Date implements SCO {
031: private transient StateManager ownerSM;
032: private transient Object owner;
033: private transient String fieldName;
034:
035: /**
036: * Creates a <tt>SqlDate</tt> object that represents the time at which
037: * it was allocated. Assigns owning object and field name.
038: *
039: * @param ownerSM the owning object
040: * @param fieldName the owning field name
041: */
042: public SqlDate(StateManager ownerSM, String fieldName) {
043: super (0);
044:
045: if (ownerSM != null) {
046: this .ownerSM = ownerSM;
047: this .owner = ownerSM.getObject();
048: }
049: this .fieldName = fieldName;
050: }
051:
052: /**
053: * Method to initialise the SCO for use.
054: */
055: public void initialise() {
056: }
057:
058: /**
059: * Method to initialise the SCO from an existing value.
060: * @param o The Object
061: * @param forInsert Whether the object needs inserting in the datastore with this value
062: * @param forUpdate Whether to update the datastore with this value
063: */
064: public void initialise(Object o, boolean forInsert,
065: boolean forUpdate) {
066: super .setTime(((java.sql.Date) o).getTime());
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.Date(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 Clone of the object
085: */
086: public Object clone() {
087: Object obj = super .clone();
088:
089: ((SqlDate) 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: * Sets the year of this <tt>Date</tt> object to be the specified
105: * value plus 1900. This <code>Date</code> object is modified so
106: * that it represents a point in time within the specified year,
107: * with the month, date, hour, minute, and second the same as
108: * before, as interpreted in the local time zone. (Of course, if
109: * the date was February 29, for example, and the year is set to a
110: * non-leap year, then the new date will be treated as if it were
111: * on March 1.)
112: *
113: * @param year the year value.
114: * @see java.util.Calendar
115: * @deprecated As of JDK version 1.1,
116: * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
117: */
118: public void setYear(int year) {
119: super .setYear(year);
120: makeDirty();
121: }
122:
123: /**
124: * Sets the month of this date to the specified value. This
125: * <tt>Date</tt> object is modified so that it represents a point
126: * in time within the specified month, with the year, date, hour,
127: * minute, and second the same as before, as interpreted in the
128: * local time zone. If the date was October 31, for example, and
129: * the month is set to June, then the new date will be treated as
130: * if it were on July 1, because June has only 30 days.
131: *
132: * @param month the month value between 0-11.
133: * @see java.util.Calendar
134: * @deprecated As of JDK version 1.1,
135: * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
136: */
137: public void setMonth(int month) {
138: super .setMonth(month);
139: makeDirty();
140: }
141:
142: /**
143: * Sets the day of the month of this <tt>Date</tt> object to the
144: * specified value. This <tt>Date</tt> object is modified so that
145: * it represents a point in time within the specified day of the
146: * month, with the year, month, hour, minute, and second the same
147: * as before, as interpreted in the local time zone. If the date
148: * was April 30, for example, and the date is set to 31, then it
149: * will be treated as if it were on May 1, because April has only
150: * 30 days.
151: *
152: * @param date the day of the month value between 1-31.
153: * @see java.util.Calendar
154: * @deprecated As of JDK version 1.1,
155: * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
156: */
157: public void setDate(int date) {
158: super .setDate(date);
159: makeDirty();
160: }
161:
162: /**
163: * Utility to unset the owner.
164: **/
165: public void unsetOwner() {
166: owner = null;
167: ownerSM = null;
168: fieldName = null;
169: }
170:
171: /**
172: * Accessor for the owner.
173: * @return The owner
174: **/
175: public Object getOwner() {
176: return owner;
177: }
178:
179: /**
180: * Accessor for the field name
181: * @return The field name
182: **/
183: public String getFieldName() {
184: return fieldName;
185: }
186:
187: /**
188: * Utility to mark the object as dirty
189: **/
190: public void makeDirty() {
191: if (ownerSM != null) {
192: ownerSM.getObjectManager().getApiAdapter().makeFieldDirty(
193: owner, fieldName);
194: }
195: }
196:
197: /**
198: * Method to detach a copy of this object.
199: * @param state State for detachment process
200: * @return The detached object
201: */
202: public Object detachCopy(FetchPlanState state) {
203: return new java.sql.Date(getTime());
204: }
205:
206: /**
207: * Method to return an attached version for the passed StateManager and field, using the passed value.
208: * @param value The new value
209: */
210: public void attachCopy(Object value) {
211: long oldValue = getTime();
212: initialise(value, false, true);
213:
214: // Check if the field has changed, and set the owner field as dirty if necessary
215: long newValue = ((java.sql.Date) value).getTime();
216: if (oldValue != newValue) {
217: makeDirty();
218: }
219: }
220:
221: /**
222: * The writeReplace method is called when ObjectOutputStream is preparing to write the object to the stream. The
223: * ObjectOutputStream checks whether the class defines the writeReplace method. If the method is defined, the
224: * writeReplace method is called to allow the object to designate its replacement in the stream. The object returned
225: * should be either of the same type as the object passed in or an object that when read and resolved will result in
226: * an object of a type that is compatible with all references to the object.
227: *
228: * @return the replaced object
229: * @throws ObjectStreamException
230: */
231: protected Object writeReplace() throws ObjectStreamException {
232: return new java.sql.Date(this.getTime());
233: }
234: }
|