KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > sco > SqlDate


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: SqlDate.java,v 1.6 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.sco;
12
13 import com.triactive.jdo.SCO;
14 import java.io.ObjectStreamException JavaDoc;
15 import javax.jdo.JDOHelper;
16
17
18 /**
19  * A mutable second-class SQL date object.
20  *
21  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
22  * @version $Revision: 1.6 $
23  */

24
25 public class SqlDate extends java.sql.Date JavaDoc implements SCO
26 {
27     private transient Object JavaDoc owner;
28     private transient String JavaDoc fieldName;
29
30
31     /**
32      * Creates a <tt>SqlDate</tt> object that represents the same time as the
33      * given <tt>java.sql.Date</tt>. Assigns owning object and field name.
34      *
35      * @param owner the owning object
36      * @param fieldName the owning field name
37      * @param date the initial date value
38      */

39
40     public SqlDate(Object JavaDoc owner, String JavaDoc fieldName, java.sql.Date JavaDoc date)
41     {
42         super(date.getTime());
43
44         this.owner = owner;
45         this.fieldName = fieldName;
46     }
47
48
49     public Object JavaDoc getOwner()
50     {
51         return owner;
52     }
53
54
55     public String JavaDoc getFieldName()
56     {
57         return fieldName;
58     }
59
60
61     public void makeDirty()
62     {
63         if (owner != null)
64             JDOHelper.makeDirty(owner, fieldName);
65     }
66
67
68     public void applyUpdates()
69     {
70     }
71
72
73     public void unsetOwner()
74     {
75         owner = null;
76         fieldName = null;
77     }
78
79
80     /**
81      * Creates and returns a copy of this object.
82      *
83      * <P>Mutable second-class Objects are required to provide a public
84      * clone method in order to allow for copying PersistenceCapable
85      * objects. In contrast to Object.clone(), this method must not throw a
86      * CloneNotSupportedException.
87      */

88
89     public Object JavaDoc clone()
90     {
91         Object JavaDoc obj = super.clone();
92
93         ((SqlDate)obj).unsetOwner();
94
95         return obj;
96     }
97
98
99     public void setTime(long time)
100     {
101         super.setTime(time);
102         makeDirty();
103     }
104
105
106     /**
107      * Sets the year of this <tt>Date</tt> object to be the specified
108      * value plus 1900. This <code>Date</code> object is modified so
109      * that it represents a point in time within the specified year,
110      * with the month, date, hour, minute, and second the same as
111      * before, as interpreted in the local time zone. (Of course, if
112      * the date was February 29, for example, and the year is set to a
113      * non-leap year, then the new date will be treated as if it were
114      * on March 1.)
115      *
116      * @param year the year value.
117      * @see java.util.Calendar
118      * @deprecated As of JDK version 1.1,
119      * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
120      */

121
122     public void setYear(int year)
123     {
124         super.setYear(year);
125         makeDirty();
126     }
127
128
129     /**
130      * Sets the month of this date to the specified value. This
131      * <tt>Date</tt> object is modified so that it represents a point
132      * in time within the specified month, with the year, date, hour,
133      * minute, and second the same as before, as interpreted in the
134      * local time zone. If the date was October 31, for example, and
135      * the month is set to June, then the new date will be treated as
136      * if it were on July 1, because June has only 30 days.
137      *
138      * @param month the month value between 0-11.
139      * @see java.util.Calendar
140      * @deprecated As of JDK version 1.1,
141      * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
142      */

143
144     public void setMonth(int month)
145     {
146         super.setMonth(month);
147         makeDirty();
148     }
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
167     public void setDate(int date)
168     {
169         super.setDate(date);
170         makeDirty();
171     }
172
173
174     /**
175      * Replaces the object to be serialized with a java.sql.Date object.
176      * Invoked by the serialization mechanism to obtain an alternative object
177      * to be used when writing an object to the stream.
178      *
179      * @return
180      * The <code>java.sql.Date</code> to be serialized instead of this
181      * object.
182      */

183
184     protected Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
185     {
186         return new java.sql.Date JavaDoc(getTime());
187     }
188 }
189
Popular Tags