KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > sco > Date


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.jdo.spi.persistence.support.sqlstore.sco;
25
26 import java.io.ObjectStreamException JavaDoc;
27
28 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapable;
29 import com.sun.jdo.spi.persistence.support.sqlstore.StateManager;
30 import com.sun.jdo.spi.persistence.support.sqlstore.SCO;
31 import com.sun.jdo.spi.persistence.support.sqlstore.SCODate;
32 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager;
33
34 /**
35  * A mutable 2nd class object date.
36  * @author Marina Vatkina
37  * @version 1.0
38  * @see java.util.Date
39  */

40 public class Date
41     extends java.util.Date JavaDoc
42     implements SCODate
43 {
44
45     private transient PersistenceCapable owner;
46
47     private transient String JavaDoc fieldName;
48
49     /**
50      * Creates a <code>Date</code> object that represents the time at which
51      * it was allocated. Assigns owning object and field name
52      * @param owner the owning object
53      * @param fieldName the owning field name
54      */

55     public Date(Object JavaDoc owner, String JavaDoc fieldName)
56     {
57     super();
58     if (owner instanceof PersistenceCapable)
59         {
60                 this.owner = (PersistenceCapable)owner;
61         this.fieldName = fieldName;
62         }
63     }
64
65     /**
66      * Creates a <code>Date</code> object that represents the given time
67      * in milliseconds. Assigns owning object and field name
68      * @param owner the owning object
69      * @param fieldName the owning field name
70      * @param date the number of milliseconds
71      */

72     public Date(Object JavaDoc owner, String JavaDoc fieldName, long date)
73     {
74     super(date);
75     if (owner instanceof PersistenceCapable)
76         {
77             this.owner = (PersistenceCapable)owner;
78             this.fieldName = fieldName;
79         }
80     }
81
82     /**
83      * Sets the <tt>Date</tt> object to represent a point in time that is
84      * <tt>time</tt> milliseconds after January 1, 1970 00:00:00 GMT.
85      *
86      * @param time the number of milliseconds.
87      * @see java.util.Date
88      */

89     public void setTime(long time) {
90     this.makeDirty();
91     super.setTime(time);
92     }
93
94     /**
95      * Creates and returns a copy of this object.
96      *
97      * <P>Mutable Second Class Objects are required to provide a public
98      * clone method in order to allow for copying PersistenceCapable
99      * objects. In contrast to Object.clone(), this method must not throw a
100      * CloneNotSupportedException.
101      */

102     public Object JavaDoc clone()
103     {
104         Date obj = (Date) super.clone();
105
106         obj.owner = null;
107         obj.fieldName = null;
108
109         return obj;
110     }
111
112     /** -----------Depricated Methods------------------*/
113
114     /**
115      * Sets the year of this <tt>Date</tt> object to be the specified
116      * value plus 1900.
117      *
118      * @param year the year value.
119      * @see java.util.Calendar
120      * @see java.util.Date
121      * @deprecated As of JDK version 1.1,
122      * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
123      */

124     public void setYear(int year) {
125         this.makeDirty();
126         super.setYear(year);
127     }
128
129     /**
130      * Sets the month of this date to the specified value.
131      * @param month the month value between 0-11.
132      * @see java.util.Calendar
133      * @see java.util.Date
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         this.makeDirty();
139         super.setMonth(month);
140     }
141
142     /**
143      * Sets the day of the month of this <tt>Date</tt> object to the
144      * specified value.
145      *
146      * @param date the day of the month value between 1-31.
147      * @see java.util.Calendar
148      * @see java.util.Date
149      * @deprecated As of JDK version 1.1,
150      * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
151      */

152     public void setDate(int date) {
153         this.makeDirty();
154         super.setDate(date);
155     }
156
157     /**
158      * Sets the hour of this <tt>Date</tt> object to the specified value.
159      *
160      * @param hours the hour value.
161      * @see java.util.Calendar
162      * @see java.util.Date
163      * @deprecated As of JDK version 1.1,
164      * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
165      */

166     public void setHours(int hours) {
167         this.makeDirty();
168         super.setHours(hours);
169     }
170
171     /**
172      * Sets the minutes of this <tt>Date</tt> object to the specified value.
173      *
174      * @param minutes the value of the minutes.
175      * @see java.util.Calendar
176      * @see java.util.Date
177      * @deprecated As of JDK version 1.1,
178      * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
179      */

180     public void setMinutes(int minutes) {
181         this.makeDirty();
182         super.setMinutes(minutes);
183     }
184  
185     /**
186      * Sets the seconds of this <tt>Date</tt> to the specified value.
187      *
188      * @param seconds the seconds value.
189      * @see java.util.Calendar
190      * @see java.util.Date
191      * @deprecated As of JDK version 1.1,
192      * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
193      */

194     public void setSeconds(int seconds) {
195         this.makeDirty();
196         super.setSeconds(seconds);
197     }
198
199     /** ---------------- internal methods ------------------- */
200
201     /**
202      * Creates and returns a copy of this object without resetting the owner and field value.
203      *
204      */

205     public Object JavaDoc cloneInternal()
206     {
207         return super.clone();
208     }
209
210     /**
211      * Sets the <tt>Date</tt> object without notification of the Owner
212      * field. Used internaly to populate date from DB
213      *
214      * @param time the number of milliseconds.
215      * @see java.util.Date
216      */

217     public void setTimeInternal(long time) {
218     super.setTime(time);
219     }
220
221     /**
222      * Nullifies references to the owner Object and Field
223      * NOTE: This method should be called under the locking of
224      * the owener' state manager.
225      */

226     public void unsetOwner()
227     {
228         this.owner = null;
229         this.fieldName = null;
230     }
231
232     /**
233      * Returns the owner object of the SCO instance
234      *
235      * @return owner object
236      */

237     public Object JavaDoc getOwner()
238     {
239         return this.owner;
240     }
241
242     /**
243      * Returns the field name
244      *
245      * @return field name as java.lang.String
246      */

247     public String JavaDoc getFieldName()
248     {
249         return this.fieldName;
250     }
251  
252     /**
253      * Marks object dirty
254      */

255     public StateManager makeDirty()
256     {
257         if (owner != null)
258         {
259             StateManager stateManager = owner.jdoGetStateManager();
260
261             if (stateManager != null)
262             {
263                 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal();
264
265                 pm.acquireShareLock();
266                 
267                 try
268                 {
269                     synchronized (stateManager)
270                     {
271                         //
272
// Need to recheck owner because it could be set to
273
// null before we lock the stateManager.
274
//
275
if (owner != null)
276                         {
277                             stateManager.makeDirty(fieldName);
278                             return stateManager;
279                         }
280                     }
281                 }
282                 finally
283                 {
284                     pm.releaseShareLock();
285                 }
286             }
287
288         }
289     
290         return null;
291      }
292
293     /**
294      * Apply changes (no-op)
295      */

296     public void applyUpdates(StateManager sm, boolean modified)
297     {
298     }
299
300
301     /**
302      * Use java.util.Date as the designated object to be used when writing
303      * this object to the stream.
304      *
305      * @return java.util.Date that represents the same value.
306      * @throw ObjectStreamException.
307      */

308     Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
309     {
310         return new java.util.Date JavaDoc(getTime());
311     }
312
313 }
314
315
316
317
Popular Tags