KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SqlDate.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.Date
44  */

45 public class SqlDate
46     extends java.sql.Date JavaDoc
47     implements SCODate
48 {
49     private transient PersistenceCapable owner;
50
51     private transient String JavaDoc fieldName;
52
53     /**
54      * Creates a <code>SqlDate</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 SqlDate(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>SqlDate</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 SqlDate(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>SqlDate</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.Date
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         SqlDate obj = (SqlDate) 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 year of this <tt>SqlDate</tt> object to be the specified
120      * value plus 1900.
121      *
122      * @param year the year value.
123      * @see java.util.Calendar
124      * @see java.sql.Date
125      * @deprecated As of JDK version 1.1,
126      * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
127      */

128     public void setYear(int year) {
129     this.makeDirty();
130         super.setYear(year);
131     }
132
133     /**
134      * Sets the month of this date to the specified value.
135      * @param month the month value between 0-11.
136      * @see java.util.Calendar
137      * @see java.sql.Date
138      * @deprecated As of JDK version 1.1,
139      * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
140      */

141     public void setMonth(int month) {
142     this.makeDirty();
143         super.setMonth(month);
144     }
145
146     /**
147      * Sets the day of the month of this <tt>SqlDate</tt> object to the
148      * specified value.
149      *
150      * @param date the day of the month value between 1-31.
151      * @see java.util.Calendar
152      * @see java.sql.Date
153      * @deprecated As of JDK version 1.1,
154      * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
155      */

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

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

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

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

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

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

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

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

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