KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
25  * SqlTimestamp.java
26  *
27  */

28
29 package com.sun.jdo.spi.persistence.support.sqlstore.sco;
30
31 import java.io.ObjectStreamException JavaDoc;
32
33 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapable;
34 import com.sun.jdo.spi.persistence.support.sqlstore.StateManager;
35 import com.sun.jdo.spi.persistence.support.sqlstore.SCO;
36 import com.sun.jdo.spi.persistence.support.sqlstore.SCODate;
37 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager;
38
39 /**
40  * A mutable 2nd class object date.
41  * @author Marina Vatkina
42  * @version 1.0
43  * @see java.sql.Timestamp
44  */

45 public class SqlTimestamp
46     extends java.sql.Timestamp JavaDoc
47     implements SCODate
48 {
49
50     private transient PersistenceCapable owner;
51
52     private transient String JavaDoc fieldName;
53
54     /**
55      * Creates a <code>SqlTimestamp</code> object that represents the time at which
56      * it was allocated. Assigns owning object and field name
57      * @param owner the owning object
58      * @param fieldName the owning field name
59      */

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

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

94     public void setTime(long time) {
95     this.makeDirty();
96     super.setTime(time);
97     }
98
99     /**
100      * Sets this <code>Timestamp</code> object's <code>nanos</code> value
101      * to the given value.
102      *
103      * @param n the new fractional seconds component
104      * @exception java.lang.IllegalArgumentException if the given argument
105      * is greater than 999999999 or less than 0
106      * @see java.sql.Timestamp
107      */

108     public void setNanos(int n) {
109     this.makeDirty();
110         try {
111         super.setNanos(n);
112     } catch (IllegalArgumentException JavaDoc e) {
113         throw e;
114     }
115     }
116
117
118     /**
119      * Creates and returns a copy of this object.
120      *
121      * <P>Mutable Second Class Objects are required to provide a public
122      * clone method in order to allow for copying PersistenceCapable
123      * objects. In contrast to Object.clone(), this method must not throw a
124      * CloneNotSupportedException.
125      */

126     public Object JavaDoc clone()
127     {
128         SqlTimestamp obj = (SqlTimestamp) super.clone();
129
130         obj.owner = null;
131         obj.fieldName = null;
132
133         return obj;
134     }
135
136     /** -----------Depricated Methods------------------*/
137
138     /**
139      * Sets the year of this <tt>SqlTimestamp</tt> object to be the specified
140      * value plus 1900.
141      *
142      * @param year the year value.
143      * @see java.util.Calendar
144      * @see java.sql.Timestamp
145      * @deprecated As of JDK version 1.1,
146      * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
147      */

148     public void setYear(int year) {
149     this.makeDirty();
150         super.setYear(year);
151     }
152
153     /**
154      * Sets the month of this date to the specified value.
155      * @param month the month value between 0-11.
156      * @see java.util.Calendar
157      * @see java.sql.Timestamp
158      * @deprecated As of JDK version 1.1,
159      * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
160      */

161     public void setMonth(int month) {
162     this.makeDirty();
163         super.setMonth(month);
164     }
165
166     /**
167      * Sets the day of the month of this <tt>SqlTimestamp</tt> object to the
168      * specified value.
169      *
170      * @param date the day of the month value between 1-31.
171      * @see java.util.Calendar
172      * @see java.sql.Timestamp
173      * @deprecated As of JDK version 1.1,
174      * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
175      */

176     public void setDate(int date) {
177     this.makeDirty();
178         super.setDate(date);
179     }
180
181
182     /**
183      * Sets the hour of this <tt>SqlTimestamp</tt> object to the specified value.
184      *
185      * @param hours the hour value.
186      * @see java.util.Calendar
187      * @see java.sql.Timestamp
188      * @deprecated As of JDK version 1.1,
189      * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
190      */

191     public void setHours(int hours) {
192     this.makeDirty();
193         super.setHours(hours);
194     }
195
196     /**
197      * Sets the minutes of this <tt>SqlTimestamp</tt> object to the specified value.
198      *
199      * @param minutes the value of the minutes.
200      * @see java.util.Calendar
201      * @see java.sql.Timestamp
202      * @deprecated As of JDK version 1.1,
203      * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
204      */

205     public void setMinutes(int minutes) {
206     this.makeDirty();
207         super.setMinutes(minutes);
208     }
209  
210     /**
211      * Sets the seconds of this <tt>SqlTimestamp</tt> to the specified value.
212      *
213      * @param seconds the seconds value.
214      * @see java.util.Calendar
215      * @see java.sql.Timestamp
216      * @deprecated As of JDK version 1.1,
217      * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
218      */

219     public void setSeconds(int seconds) {
220     this.makeDirty();
221         super.setSeconds(seconds);
222     }
223
224     /** ---------------- internal methods ------------------- */
225
226     /**
227      * Creates and returns a copy of this object without resetting the owner and field value.
228      *
229      */

230     public Object JavaDoc cloneInternal()
231     {
232         return super.clone();
233     }
234
235     /**
236      * Sets the <tt>SqlTimestamp</tt> object without notification of the Owner
237      * field. Used internaly to populate date from DB
238      *
239      * @param time the number of milliseconds.
240      * @see java.sql.Timestamp
241      */

242     public void setTimeInternal(long time) {
243     super.setTime(time);
244     }
245
246     /**
247      * Sets the <tt>SqlTimestamp</tt> object's <code>nanos</code> value without
248      * notification of the Owner field. Used internaly to populate date from DB
249      *
250      * @param n the new fractional seconds component
251      * @exception java.lang.IllegalArgumentException if the given argument
252      * is greater than 999999999 or less than 0
253      * @see java.sql.Timestamp
254      */

255     public void setNanosInternal(int n) {
256     super.setNanos(n);
257     }
258
259     /**
260      * Nullifies references to the owner Object and Field
261      * NOTE: This method should be called under the locking of
262      * the owener' state manager.
263      */

264     public void unsetOwner()
265     {
266         this.owner = null;
267         this.fieldName = null;
268     }
269
270     /**
271      * Returns the owner object of the SCO instance
272      *
273      * @return owner object
274      */

275     public Object JavaDoc getOwner()
276     {
277         return this.owner;
278     }
279
280     /**
281      * Returns the field name
282      *
283      * @return field name as java.lang.String
284      */

285     public String JavaDoc getFieldName()
286     {
287         return this.fieldName;
288     }
289  
290     /**
291      * Marks object dirty
292      */

293     public StateManager makeDirty()
294     {
295         if (owner != null)
296         {
297             StateManager stateManager = owner.jdoGetStateManager();
298             
299             if (stateManager != null)
300             {
301                 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal();
302
303                 pm.acquireShareLock();
304                 
305                 try
306                 {
307                     synchronized (stateManager)
308                     {
309                         //
310
// Need to recheck owner because it could be set to
311
// null before we lock the stateManager.
312
//
313
if (owner != null)
314                         {
315                             stateManager.makeDirty(fieldName);
316                             return stateManager;
317                         }
318                     }
319                 }
320                 finally
321                 {
322                     pm.releaseShareLock();
323                 }
324             }
325         }
326         return null;
327      }
328
329     /**
330      * Apply changes (no-op)
331      */

332     public void applyUpdates(StateManager sm, boolean modified)
333     {
334     }
335
336     /**
337      * Use java.sql.Timestamp as the designated object to be used when writing
338      * this object to the stream.
339      *
340      * @return java.sql.Timestamp that represents the same value.
341      * @throw ObjectStreamException.
342      */

343     Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
344     {
345         java.sql.Timestamp JavaDoc t = new java.sql.Timestamp JavaDoc(getTime());
346         t.setNanos(this.getNanos());
347         return t;
348     }
349 }
350
Popular Tags