KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SqlTime.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.Time
44  */

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

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

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

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

106     public Object JavaDoc clone()
107     {
108         SqlTime obj = (SqlTime) super.clone();
109
110         obj.owner = null;
111         obj.fieldName = null;
112
113         return obj;
114     }
115
116     /** -----------Depricated Methods------------------*/
117
118     /**
119      * Sets the hour of this <tt>SqlTime</tt> object to the specified value.
120      *
121      * @param hours the hour value.
122      * @see java.util.Calendar
123      * @see java.sql.Time
124      * @deprecated As of JDK version 1.1,
125      * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
126      */

127     public void setHours(int hours) {
128     this.makeDirty();
129         super.setHours(hours);
130     }
131
132     /**
133      * Sets the minutes of this <tt>SqlTime</tt> object to the specified value.
134      *
135      * @param minutes the value of the minutes.
136      * @see java.util.Calendar
137      * @see java.sql.Time
138      * @deprecated As of JDK version 1.1,
139      * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
140      */

141     public void setMinutes(int minutes) {
142     this.makeDirty();
143         super.setMinutes(minutes);
144     }
145  
146     /**
147      * Sets the seconds of this <tt>SqlTime</tt> to the specified value.
148      *
149      * @param seconds the seconds value.
150      * @see java.util.Calendar
151      * @see java.sql.Time
152      * @deprecated As of JDK version 1.1,
153      * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
154      */

155     public void setSeconds(int seconds) {
156     this.makeDirty();
157         super.setSeconds(seconds);
158     }
159
160     /** ---------------- internal methods ------------------- */
161
162     /**
163      * Creates and returns a copy of this object without resetting the owner and field value.
164      *
165      */

166     public Object JavaDoc cloneInternal()
167     {
168         return super.clone();
169     }
170
171     /**
172      * Sets the <tt>SqlTime</tt> object without notification of the Owner
173      * field. Used internaly to populate date from DB
174      *
175      * @param time the number of milliseconds.
176      * @see java.sql.Time
177      */

178     public void setTimeInternal(long time) {
179     super.setTime(time);
180     }
181
182     /**
183      * Nullifies references to the owner Object and Field
184      * NOTE: This method should be called under the locking of
185      * the owener' state manager.
186      */

187     public void unsetOwner()
188     {
189         this.owner = null;
190         this.fieldName = null;
191     }
192
193     /**
194      * Returns the owner object of the SCO instance
195      *
196      * @return owner object
197      */

198     public Object JavaDoc getOwner()
199     {
200         return this.owner;
201     }
202
203     /**
204      * Returns the field name
205      *
206      * @return field name as java.lang.String
207      */

208     public String JavaDoc getFieldName()
209     {
210         return this.fieldName;
211     }
212  
213     /**
214      * Marks object dirty
215      */

216     public StateManager makeDirty()
217     {
218         if (owner != null)
219         {
220             StateManager stateManager = owner.jdoGetStateManager();
221             
222             if (stateManager != null)
223             {
224                 PersistenceManager pm = (PersistenceManager) stateManager.getPersistenceManagerInternal();
225
226                 pm.acquireShareLock();
227                 
228                 try
229                 {
230                     synchronized (stateManager)
231                     {
232                         //
233
// Need to recheck owner because it could be set to
234
// null before we lock the stateManager.
235
//
236
if (owner != null)
237                         {
238                             stateManager.makeDirty(fieldName);
239                             return stateManager;
240                         }
241                     }
242                 }
243                 finally
244                 {
245                     pm.releaseShareLock();
246                 }
247             }
248         }
249         return null;
250      }
251
252     /**
253      * Apply changes (no-op)
254      */

255     public void applyUpdates(StateManager sm, boolean modified)
256     {
257     }
258
259     /**
260      * Use java.sql.Time as the designated object to be used when writing
261      * this object to the stream.
262      *
263      * @return java.sql.Time that represents the same value.
264      * @throw ObjectStreamException.
265      */

266     Object JavaDoc writeReplace() throws ObjectStreamException JavaDoc
267     {
268         return new java.sql.Time JavaDoc(getTime());
269     }
270 }
271
Popular Tags