001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: OwnerWidget.java,v 1.5 2004/03/22 04:58:13 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: import javax.jdo.InstanceCallbacks;
014: import javax.jdo.JDOHelper;
015: import javax.jdo.PersistenceManager;
016:
017: public class OwnerWidget extends TestObject implements
018: InstanceCallbacks {
019: private Cloneable cloneable;
020: private TestObject testObject;
021:
022: public OwnerWidget() {
023: cloneable = null;
024: testObject = null;
025: }
026:
027: public Object clone() {
028: OwnerWidget ow = (OwnerWidget) super .clone();
029:
030: ow.cloneable = (Cloneable) ((TestObject) cloneable).clone();
031: ow.testObject = (TestObject) testObject.clone();
032:
033: return ow;
034: }
035:
036: public Cloneable getCloneable() {
037: return cloneable;
038: }
039:
040: public TestObject getTestObject() {
041: return testObject;
042: }
043:
044: private Widget randomWidget() {
045: Widget obj;
046:
047: switch (r.nextInt(4)) {
048: case 0:
049: default:
050: obj = new Widget();
051: break;
052:
053: case 1:
054: obj = new DateWidget();
055: break;
056:
057: case 2:
058: obj = new StringWidget();
059: break;
060:
061: case 3:
062: obj = new BinaryWidget();
063: break;
064: }
065:
066: obj.fillRandom();
067:
068: return obj;
069: }
070:
071: /**
072: * Fills all of the object's fields with random data values.
073: */
074:
075: public void fillRandom() {
076: PersistenceManager myPM = JDOHelper.getPersistenceManager(this );
077:
078: if (myPM != null) {
079: myPM.deletePersistent(cloneable);
080: myPM.deletePersistent(testObject);
081: }
082:
083: cloneable = randomWidget();
084: testObject = randomWidget();
085: }
086:
087: /**
088: * Indicates whether some other object is "equal to" this one. By comparing
089: * against an original copy of the object, <code>compareTo()</code> can be
090: * used to verify that the object has been written to a database and read
091: * back correctly.
092: *
093: * @param obj the reference object with which to compare
094: *
095: * @return <code>true</code> if this object is equal to the obj argument;
096: * <code>false</code> otherwise.
097: */
098:
099: public boolean compareTo(Object obj) {
100: if (obj == this )
101: return true;
102:
103: if (!(obj instanceof OwnerWidget))
104: return false;
105:
106: OwnerWidget w = (OwnerWidget) obj;
107:
108: return ((TestObject) cloneable).compareTo(w.cloneable)
109: && testObject.compareTo(w.testObject);
110: }
111:
112: /**
113: * Returns a string representation for this object. All of the field
114: * values are included in the string for debugging purposes.
115: *
116: * @return a string representation for this object.
117: */
118:
119: public String toString() {
120: StringBuffer s = new StringBuffer(super .toString());
121:
122: s.append(" cloneable = ").append(cloneable);
123: s.append('\n');
124: s.append(" testObject = ").append(testObject);
125: s.append('\n');
126:
127: return s.toString();
128: }
129:
130: public void jdoPostLoad() {
131: return;
132: }
133:
134: public void jdoPreClear() {
135: return;
136: }
137:
138: public void jdoPreDelete() {
139: PersistenceManager myPM = JDOHelper.getPersistenceManager(this );
140:
141: myPM.deletePersistent(cloneable);
142: myPM.deletePersistent(testObject);
143: }
144:
145: public void jdoPreStore() {
146: return;
147: }
148: }
|