1 2 12 package com.versant.core.jdo.sco.detached; 13 14 import com.versant.core.common.Debug; 15 import com.versant.core.jdo.VersantStateManager; 16 import com.versant.core.common.VersantFieldMetaData; 17 import com.versant.core.jdo.sco.VersantSimpleSCO; 18 import com.versant.core.jdo.VersantStateManager; 19 20 import javax.jdo.spi.PersistenceCapable; 21 import java.io.*; 22 23 public class DetachSCODate extends java.util.Date implements Serializable, VersantSimpleSCO { 24 25 private PersistenceCapable owner; 26 private int fieldNo; 27 private VersantStateManager stateManager; 28 29 35 public DetachSCODate(PersistenceCapable owner, 36 VersantStateManager stateManager, VersantFieldMetaData fmd, 37 long date) { 38 super(date); 39 this.owner = owner; 40 this.stateManager = stateManager; 41 this.fieldNo = fmd.getManagedFieldNo(); 42 } 43 44 51 public void setTime(long time) { 52 this.makeDirty(); 53 super.setTime(time); 54 } 55 56 64 public Object clone() { 65 Object obj = super.clone(); 66 if (obj instanceof VersantSimpleSCO) { 67 ((VersantSimpleSCO) obj).makeTransient(); 68 } 69 70 return obj; 71 } 72 73 74 75 85 public void setYear(int year) { 86 this.makeDirty(); 87 super.setYear(year); 88 } 89 90 99 public void setMonth(int month) { 100 this.makeDirty(); 101 super.setMonth(month); 102 } 103 104 114 public void setDate(int date) { 115 this.makeDirty(); 116 super.setDate(date); 117 } 118 119 128 public void setHours(int hours) { 129 this.makeDirty(); 130 super.setHours(hours); 131 } 132 133 142 public void setMinutes(int minutes) { 143 this.makeDirty(); 144 super.setMinutes(minutes); 145 } 146 147 156 public void setSeconds(int seconds) { 157 this.makeDirty(); 158 super.setSeconds(seconds); 159 } 160 161 162 163 170 public void setTimeInternal(long time) { 171 super.setTime(time); 172 } 173 174 177 public void makeTransient() { 178 this.owner = null; 179 this.stateManager = null; 180 } 181 182 187 public Object getOwner() { 188 return this.owner; 189 } 190 191 194 public void makeDirty() { 195 if (stateManager != null && owner != null) { 196 stateManager.makeDirty(owner, fieldNo); 197 } 198 } 199 200 public void reset() { 201 } 202 203 208 public Class getJavaType() { 209 return java.util.Date .class; 210 } 211 212 public void writeExternal(ObjectOutput out) throws IOException { 213 out.writeLong(super.getTime()); 214 } 215 216 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 217 super.setTime(in.readLong()); 218 } 219 220 public static void main(String [] args) throws Exception { 221 DetachSCODate d = new DetachSCODate(null, null, null, System.currentTimeMillis()); 222 final long time = System.currentTimeMillis() - 5000; 223 d.setTime(time); 224 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 225 ObjectOutputStream out = new ObjectOutputStream(bout); 226 out.writeObject(d); 227 228 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray())); 229 Object o = in.readObject(); 230 Debug.OUT.println("####### o.type = " + o.getClass().getName()); 231 Debug.OUT.println("####### equal = " + d.equals(o)); 232 } 233 } 234 | Popular Tags |