001: /*
002: $Header: /cvsroot/xorm/xorm/tests/src/org/xorm/tests/model/JDOEnhanced.java,v 1.1 2002/09/30 03:26:08 wbiggs Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with XORM; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm.tests.model;
021:
022: import javax.jdo.spi.PersistenceCapable;
023: import javax.jdo.spi.StateManager;
024: import javax.jdo.PersistenceManager;
025:
026: /**
027: * This class simulates the structure of a reference-enhanced JDO class.
028: */
029: public class JDOEnhanced implements PersistenceCapable {
030: private String str;
031:
032: public String getStr() {
033: return jdoGetstr(this );
034: }
035:
036: public void setStr(String str) {
037: jdoSetstr(this , str);
038: }
039:
040: // 20.20.1 Generated fields
041: protected transient StateManager jdoStateManager = null;
042: protected transient byte jdoFlags = PersistenceCapable.READ_WRITE_OK;
043: private final static int jdoInheritedFieldCount = 0;
044:
045: private final static String[] jdoFieldNames = { "str" };
046: private final static Class[] jdoFieldTypes = { String.class };
047: private final static byte[] jdoFieldFlags = { CHECK_READ
048: + CHECK_WRITE };
049: private final static Class jdoPersistenceCapableSuperclass = null;
050:
051: // 20.20.2 Generated static initializer
052: static {
053: javax.jdo.spi.JDOImplHelper.registerClass(JDOEnhanced.class,
054: jdoFieldNames, jdoFieldTypes, jdoFieldFlags,
055: jdoPersistenceCapableSuperclass, new JDOEnhanced());
056: }
057:
058: // 20.20.3 Generated interrogatives
059: public final boolean jdoIsPersistent() {
060: return jdoStateManager == null ? false : jdoStateManager
061: .isPersistent(this );
062: }
063:
064: public final boolean jdoIsTransactional() {
065: return jdoStateManager == null ? false : jdoStateManager
066: .isTransactional(this );
067: }
068:
069: public final boolean jdoIsNew() {
070: return jdoStateManager == null ? false : jdoStateManager
071: .isNew(this );
072: }
073:
074: public final boolean jdoIsDirty() {
075: return jdoStateManager == null ? false : jdoStateManager
076: .isDirty(this );
077: }
078:
079: public final boolean jdoIsDeleted() {
080: return jdoStateManager == null ? false : jdoStateManager
081: .isDeleted(this );
082: }
083:
084: public final void jdoMakeDirty(String fieldName) {
085: if (jdoStateManager == null)
086: return;
087: jdoStateManager.makeDirty(this , fieldName);
088: }
089:
090: public final PersistenceManager jdoGetPersistenceManager() {
091: return jdoStateManager == null ? null : jdoStateManager
092: .getPersistenceManager(this );
093: }
094:
095: public final Object jdoGetObjectId() {
096: return jdoStateManager == null ? null : jdoStateManager
097: .getObjectId(this );
098: }
099:
100: public final Object jdoGetTransactionalObjectId() {
101: return jdoStateManager == null ? null : jdoStateManager
102: .getTransactionalObjectId(this );
103: }
104:
105: // 20.20.4 Generated jdoReplaceStateManager
106: public final synchronized void jdoReplaceStateManager(
107: StateManager sm) {
108: // throws exception if current sm didn't request the change
109: if (jdoStateManager != null) {
110: jdoStateManager = jdoStateManager.replacingStateManager(
111: this , sm);
112: } else {
113: SecurityManager sec = System.getSecurityManager();
114: if (sec != null) {
115: sec.checkPermission( // throws exception if not authorized
116: javax.jdo.spi.JDOPermission.SET_STATE_MANAGER);
117: }
118: jdoStateManager = sm;
119: }
120: }
121:
122: // 20.20.5 Generated jdoReplaceFlags
123: public final void jdoReplaceFlags() {
124: if (jdoStateManager != null) {
125: jdoFlags = jdoStateManager.replacingFlags(this );
126: }
127: }
128:
129: // 20.20.6 Generated jdoNewInstance helpers
130: public PersistenceCapable jdoNewInstance(StateManager sm) {
131: // if class is abstract, throw new JDOFatalInternalException()
132: JDOEnhanced pc = new JDOEnhanced();
133: pc.jdoStateManager = sm;
134: pc.jdoFlags = LOAD_REQUIRED;
135: return pc;
136: }
137:
138: public PersistenceCapable jdoNewInstance(StateManager sm,
139: Object object) {
140: // if class is abstract, throw new JDOFatalInternalException()
141: JDOEnhanced pc = new JDOEnhanced();
142: pc.jdoStateManager = sm;
143: pc.jdoFlags = LOAD_REQUIRED;
144: // now copy the key fields into the new instance
145: //jdoCopyKeyFieldsFromObjectId (oid);
146: return pc;
147: }
148:
149: // 20.20.7
150: public int jdoGetManagedFieldCount() {
151: return jdoFieldNames.length;
152: }
153:
154: // 20.20.8
155:
156: private final static String jdoGetstr(JDOEnhanced x) {
157: // this field is in the default fetch group (CHECK_READ)
158: if (x.jdoFlags <= READ_WRITE_OK) {
159: // ok to read
160: return x.str;
161: }
162: // field needs to be fetched from StateManager
163: // this call might result in str being stored in instance
164: StateManager sm = x.jdoStateManager;
165: if (sm != null) {
166: if (sm.isLoaded(x, 0))
167: return x.str;
168: return sm.getStringField(x, 0, x.str);
169: } else {
170: return x.str;
171: }
172: }
173:
174: private final static void jdoSetstr(JDOEnhanced x, String newValue) {
175: // this field is in the default fetch group
176: if (x.jdoFlags == READ_WRITE_OK) {
177: // ok to read, write
178: x.str = newValue;
179: return;
180: }
181: StateManager sm = x.jdoStateManager;
182: if (sm != null) {
183: sm.setStringField(x, 0, x.str, newValue);
184: } else {
185: x.str = newValue;
186: }
187: }
188:
189: // 20.20.10 Generated jdoReplaceField and jdoReplaceFields
190: public void jdoReplaceField(int fieldNumber) {
191: switch (fieldNumber) {
192: case 0:
193: str = jdoStateManager.replacingStringField(this ,
194: fieldNumber);
195: break;
196: default:
197: throw new IllegalArgumentException("fieldNumber");
198: }
199: }
200:
201: public void jdoReplaceFields(int[] fieldNumbers) {
202: for (int i = 0; i < fieldNumbers.length; ++i) {
203: int fieldNumber = fieldNumbers[i];
204: jdoReplaceField(fieldNumber);
205: }
206: }
207:
208: // 20.20.11 Generated jdoProvideField and jdoProvideFields
209: public void jdoProvideField(int fieldNumber) {
210: switch (fieldNumber) {
211: case 0:
212: jdoStateManager.providedStringField(this , fieldNumber, str);
213: break;
214: default:
215: throw new IllegalArgumentException("fieldNumber");
216: }
217: }
218:
219: public void jdoProvideFields(int[] fieldNumbers) {
220: for (int i = 0; i < fieldNumbers.length; ++i) {
221: int fieldNumber = fieldNumbers[i];
222: jdoProvideField(fieldNumber);
223: }
224: }
225:
226: // 20.20.12 Generated jdoCopyField and jdoCopyFields
227: public void jdoCopyFields(Object pc, int[] fieldNumbers) {
228: // the other instance must be owned by the same StateManager
229: // and our StateManager must not be null!
230: if (((JDOEnhanced) pc).jdoStateManager != this .jdoStateManager)
231: throw new IllegalArgumentException(
232: "this.jdoStateManager != other.jdoStateManager");
233: if (this .jdoStateManager == null)
234: throw new IllegalStateException(
235: "this.jdoStateManager == null");
236: // throw ClassCastException if other class is the wrong class
237: JDOEnhanced other = (JDOEnhanced) pc;
238: for (int i = 0; i < fieldNumbers.length; ++i) {
239: jdoCopyField(other, fieldNumbers[i]);
240: } // for loop
241: }
242:
243: protected void jdoCopyField(JDOEnhanced other, int fieldNumber) {
244: switch (fieldNumber) {
245: case 0:
246: this .str = other.str;
247: break;
248: default:
249: throw new IllegalArgumentException("fieldNumber");
250: }
251: }
252:
253: // 20.20.13
254: private void writeObject(java.io.ObjectOutputStream out)
255: throws java.io.IOException {
256: jdoPreSerialize();
257: out.defaultWriteObject();
258: }
259:
260: // 20.20.14
261: private final void jdoPreSerialize() {
262: if (jdoStateManager != null)
263: jdoStateManager.preSerialize(this );
264: }
265:
266: // 20.20.15
267:
268: public Object jdoNewObjectIdInstance() {
269: return null;
270: }
271:
272: public Object jdoNewObjectIdInstance(String arg) {
273: return null;
274: }
275:
276: // 20.20.16
277: public void jdoCopyKeyFieldsToObjectId(
278: PersistenceCapable.ObjectIdFieldSupplier supplier,
279: Object oid) {
280: }
281:
282: public void jdoCopyKeyFieldsToObjectId(Object oid) {
283: }
284:
285: // 20.20.17
286: public void jdoCopyKeyFieldsFromObjectId(
287: PersistenceCapable.ObjectIdFieldConsumer consumer,
288: Object oid) {
289: }
290:
291: }
|