001: /**********************************************************************
002: Copyright (c) 2003 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.sco;
018:
019: import java.io.ObjectStreamException;
020:
021: import org.jpox.StateManager;
022: import org.jpox.state.FetchPlanState;
023:
024: /**
025: * A mutable second-class SQLTime object.
026: *
027: * @version $Revision: 1.24 $
028: */
029: public class SqlTime extends java.sql.Time implements SCO {
030: private transient StateManager ownerSM;
031: private transient Object owner;
032: private transient String fieldName;
033:
034: /**
035: * Creates a <tt>SqlTime</tt> object that represents the time at which
036: * it was allocated. Assigns owning object and field name.
037: *
038: * @param ownerSM the owning StateManager
039: * @param fieldName the owning field name
040: */
041: public SqlTime(StateManager ownerSM, String fieldName) {
042: super (0);
043:
044: if (ownerSM != null) {
045: this .ownerSM = ownerSM;
046: this .owner = ownerSM.getObject();
047: }
048: this .fieldName = fieldName;
049: }
050:
051: /**
052: * Method to initialise the SCO for use.
053: */
054: public void initialise() {
055: }
056:
057: /**
058: * Method to initialise the SCO from an existing value.
059: * @param o The Object
060: * @param forInsert Whether the object needs inserting in the datastore with this value
061: * @param forUpdate Whether to update the datastore with this value
062: */
063: public void initialise(Object o, boolean forInsert,
064: boolean forUpdate) {
065: super .setTime(((java.sql.Time) o).getTime());
066: }
067:
068: /**
069: * Accessor for the unwrapped value that we are wrapping.
070: * @return The unwrapped value
071: */
072: public Object getValue() {
073: return new java.sql.Time(getTime());
074: }
075:
076: /**
077: * Creates and returns a copy of this object.
078: *
079: * <P>Mutable second-class Objects are required to provide a public
080: * clone method in order to allow for copying PersistenceCapable
081: * objects. In contrast to Object.clone(), this method must not throw a
082: * CloneNotSupportedException.
083: * @return Clone of the object
084: */
085: public Object clone() {
086: Object obj = super .clone();
087:
088: ((SqlTime) obj).unsetOwner();
089:
090: return obj;
091: }
092:
093: /**
094: * Sets the time of this <tt>Time</tt> object to the specified value.
095: * This <tt>Time</tt> object is modified so that it represents a point in
096: * time with the hour, minute, second as specified.
097: *
098: * @param time millisecs since 1 Jan 1970, 00:00:00 GMT
099: * @see java.util.Calendar
100: */
101: public void setTime(long time) {
102: super .setTime(time);
103: makeDirty();
104: }
105:
106: /**
107: * Utility to unset the owner.
108: **/
109: public void unsetOwner() {
110: owner = null;
111: ownerSM = null;
112: fieldName = null;
113: }
114:
115: /**
116: * Accessor for the owner.
117: * @return The owner
118: **/
119: public Object getOwner() {
120: return owner;
121: }
122:
123: /**
124: * Accessor for the field name
125: * @return The field name
126: **/
127: public String getFieldName() {
128: return fieldName;
129: }
130:
131: /**
132: * Utility to mark the object as dirty
133: **/
134: public void makeDirty() {
135: if (ownerSM != null) {
136: ownerSM.getObjectManager().getApiAdapter().makeFieldDirty(
137: owner, fieldName);
138: }
139: }
140:
141: /**
142: * Method to detach a copy of this object.
143: * @param state State for detachment process
144: * @return The detached object
145: */
146: public Object detachCopy(FetchPlanState state) {
147: return new java.sql.Time(getTime());
148: }
149:
150: /**
151: * Method to return an attached version for the passed StateManager and field, using the passed value.
152: * @param value The new value
153: */
154: public void attachCopy(Object value) {
155: long oldValue = getTime();
156: initialise(value, false, true);
157:
158: // Check if the field has changed, and set the owner field as dirty if necessary
159: long newValue = ((java.sql.Time) value).getTime();
160: if (oldValue != newValue) {
161: makeDirty();
162: }
163: }
164:
165: /**
166: * The writeReplace method is called when ObjectOutputStream is preparing to write the object to the stream. The
167: * ObjectOutputStream checks whether the class defines the writeReplace method. If the method is defined, the
168: * writeReplace method is called to allow the object to designate its replacement in the stream. The object returned
169: * should be either of the same type as the object passed in or an object that when read and resolved will result in
170: * an object of a type that is compatible with all references to the object.
171: *
172: * @return the replaced object
173: * @throws ObjectStreamException
174: */
175: protected Object writeReplace() throws ObjectStreamException {
176: return new java.sql.Time(this.getTime());
177: }
178: }
|