001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdo.sco.detached;
012:
013: import com.versant.core.common.Debug;
014: import com.versant.core.jdo.VersantStateManager;
015: import com.versant.core.common.VersantFieldMetaData;
016: import com.versant.core.jdo.sco.VersantSimpleSCO;
017: import com.versant.core.jdo.VersantStateManager;
018:
019: import javax.jdo.spi.PersistenceCapable;
020: import java.io.*;
021:
022: public class DetachSCODate extends java.util.Date implements
023: Serializable, VersantSimpleSCO {
024:
025: private PersistenceCapable owner;
026: private int fieldNo;
027: private VersantStateManager stateManager;
028:
029: /**
030: * Creates a <code>DetachSCODate</code> object that represents the given time
031: * in milliseconds.
032: *
033: * @param date the number of milliseconds
034: */
035: public DetachSCODate(PersistenceCapable owner,
036: VersantStateManager stateManager, VersantFieldMetaData fmd,
037: long date) {
038: super (date);
039: this .owner = owner;
040: this .stateManager = stateManager;
041: this .fieldNo = fmd.getManagedFieldNo();
042: }
043:
044: /**
045: * Sets the <tt>DetachSCODate</tt> object to represent a point in time that is
046: * <tt>time</tt> milliseconds after January 1, 1970 00:00:00 GMT.
047: *
048: * @param time the number of milliseconds.
049: * @see java.util.Date
050: */
051: public void setTime(long time) {
052: this .makeDirty();
053: super .setTime(time);
054: }
055:
056: /**
057: * Creates and returns a copy of this object.
058: * <p/>
059: * <P>Mutable Second Class Objects are required to provide a public
060: * clone method in order to allow for copying PersistenceCapable
061: * objects. In contrast to Object.clone(), this method must not throw a
062: * CloneNotSupportedException.
063: */
064: public Object clone() {
065: Object obj = super .clone();
066: if (obj instanceof VersantSimpleSCO) {
067: ((VersantSimpleSCO) obj).makeTransient();
068: }
069:
070: return obj;
071: }
072:
073: /** -----------Depricated Methods------------------*/
074:
075: /**
076: * Sets the year of this <tt>DetachSCODate</tt> object to be the specified
077: * value plus 1900.
078: *
079: * @param year the year value.
080: * @see java.util.Calendar
081: * @see java.util.Date
082: * @deprecated As of JDK version 1.1,
083: * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
084: */
085: public void setYear(int year) {
086: this .makeDirty();
087: super .setYear(year);
088: }
089:
090: /**
091: * Sets the month of this date to the specified value.
092: *
093: * @param month the month value between 0-11.
094: * @see java.util.Calendar
095: * @see java.util.Date
096: * @deprecated As of JDK version 1.1,
097: * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
098: */
099: public void setMonth(int month) {
100: this .makeDirty();
101: super .setMonth(month);
102: }
103:
104: /**
105: * Sets the day of the month of this <tt>DetachSCODate</tt> object to the
106: * specified value.
107: *
108: * @param date the day of the month value between 1-31.
109: * @see java.util.Calendar
110: * @see java.util.Date
111: * @deprecated As of JDK version 1.1,
112: * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
113: */
114: public void setDate(int date) {
115: this .makeDirty();
116: super .setDate(date);
117: }
118:
119: /**
120: * Sets the hour of this <tt>DetachSCODate</tt> object to the specified value.
121: *
122: * @param hours the hour value.
123: * @see java.util.Calendar
124: * @see java.util.Date
125: * @deprecated As of JDK version 1.1,
126: * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
127: */
128: public void setHours(int hours) {
129: this .makeDirty();
130: super .setHours(hours);
131: }
132:
133: /**
134: * Sets the minutes of this <tt>DetachSCODate</tt> object to the specified value.
135: *
136: * @param minutes the value of the minutes.
137: * @see java.util.Calendar
138: * @see java.util.Date
139: * @deprecated As of JDK version 1.1,
140: * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
141: */
142: public void setMinutes(int minutes) {
143: this .makeDirty();
144: super .setMinutes(minutes);
145: }
146:
147: /**
148: * Sets the seconds of this <tt>DetachSCODate</tt> to the specified value.
149: *
150: * @param seconds the seconds value.
151: * @see java.util.Calendar
152: * @see java.util.Date
153: * @deprecated As of JDK version 1.1,
154: * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
155: */
156: public void setSeconds(int seconds) {
157: this .makeDirty();
158: super .setSeconds(seconds);
159: }
160:
161: /** ---------------- internal methods ------------------- */
162:
163: /**
164: * Sets the <tt>DetachSCODate</tt> object without notification of the Owner
165: * field. Used internaly to populate date from DB
166: *
167: * @param time the number of milliseconds.
168: * @see java.util.Date
169: */
170: public void setTimeInternal(long time) {
171: super .setTime(time);
172: }
173:
174: /**
175: * Nullifies references to the owner Object and Field
176: */
177: public void makeTransient() {
178: this .owner = null;
179: this .stateManager = null;
180: }
181:
182: /**
183: * Returns the owner object of the SCO instance
184: *
185: * @return owner object
186: */
187: public Object getOwner() {
188: return this .owner;
189: }
190:
191: /**
192: * Marks object dirty
193: */
194: public void makeDirty() {
195: if (stateManager != null && owner != null) {
196: stateManager.makeDirty(owner, fieldNo);
197: }
198: }
199:
200: public void reset() {
201: }
202:
203: /**
204: * Return the java class that is represented by this sco.
205: *
206: * @return
207: */
208: public Class getJavaType() {
209: return java.util.Date.class;
210: }
211:
212: public void writeExternal(ObjectOutput out) throws IOException {
213: out.writeLong(super .getTime());
214: }
215:
216: public void readExternal(ObjectInput in) throws IOException,
217: ClassNotFoundException {
218: super .setTime(in.readLong());
219: }
220:
221: public static void main(String[] args) throws Exception {
222: DetachSCODate d = new DetachSCODate(null, null, null, System
223: .currentTimeMillis());
224: final long time = System.currentTimeMillis() - 5000;
225: d.setTime(time);
226: ByteArrayOutputStream bout = new ByteArrayOutputStream();
227: ObjectOutputStream out = new ObjectOutputStream(bout);
228: out.writeObject(d);
229:
230: ObjectInputStream in = new ObjectInputStream(
231: new ByteArrayInputStream(bout.toByteArray()));
232: Object o = in.readObject();
233: Debug.OUT.println("####### o.type = " + o.getClass().getName());
234: Debug.OUT.println("####### equal = " + d.equals(o));
235: }
236: }
|