/*
* Copyright 2001 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
*/
package org.objectweb.speedo.pobjects.tck;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import javax.jdo.Extent;
import javax.jdo.InstanceCallbacks;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
public class InstanceCallbackClass implements InstanceCallbacks{
public static final int TRANSIENT = 0;
public static final int PERSISTENT_NEW = 1;
public static final int PERSISTENT_CLEAN = 2;
public static final int PERSISTENT_DIRTY = 3;
public static final int HOLLOW = 4;
public static final int TRANSIENT_CLEAN = 5;
public static final int TRANSIENT_DIRTY = 6;
public static final int PERSISTENT_NEW_DELETED = 7;
public static final int PERSISTENT_DELETED = 8;
public static final int PERSISTENT_NONTRANSACTIONAL = 9;
public static final int NUM_STATES = 10;
public static final int ILLEGAL_STATE = 10;
public static final String[] states = {
"transient",
"persistent-new",
"persistent-clean",
"persistent-dirty",
"hollow",
"transient-clean",
"transient-dirty",
"persistent-new-deleted",
"persistent-deleted",
"persistent-nontransactional",
"illegal"
};
private static final int IS_PERSISTENT = 0;
private static final int IS_TRANSACTIONAL = 1;
private static final int IS_DIRTY = 2;
private static final int IS_NEW = 3;
private static final int IS_DELETED = 4;
private static final int NUM_STATUSES = 5;
private static final boolean state_statuses[][] = {
// IS_PERSISTENT IS_TRANSACTIONAL IS_DIRTY IS_NEW IS_DELETED
// transient
{ false, false, false, false, false},
// persistent-new
{ true, true, true, true, false},
// persistent-clean
{ true, true, false, false, false},
// persistent-dirty
{ true, true, true, false, false},
// hollow
{ true, false, false, false, false},
// transient-clean
{ false, true, false, false, false},
// transient-dirty
{ false, true, true, false, false},
// persistent-new-deleted
{ true, true, true, true, true},
// persistent-deleted
{ true, true, true, false, true},
// persistent-nontransactional
{ true, false, false, false, false}
};
public static int currentState(Object o)
{
boolean[] status = new boolean[5];
status[IS_PERSISTENT] = JDOHelper.isPersistent(o);
status[IS_TRANSACTIONAL] = JDOHelper.isTransactional(o);
status[IS_DIRTY] = JDOHelper.isDirty(o);
status[IS_NEW] = JDOHelper.isNew(o);
status[IS_DELETED] = JDOHelper.isDeleted(o);
int i, j;
outerloop:
for( i = 0; i < NUM_STATES; ++i ){
for( j = 0; j < NUM_STATUSES; ++j ){
if( status[j] != state_statuses[i][j] )
continue outerloop;
}
return i;
}
return NUM_STATES;
}
static public boolean performPreClearTests;
static public boolean performPreDeleteTests;
static public boolean performPreStoreTests;
// The following two variables used in CallingJdoPreDelete tests
static boolean preDeleteCalled;
static int objectState;
// The rest of these variables used in FieldsInPredelete tests, except to set a variable to make object dirty.
static public int arraySize = 5;
static public String[] capturedName = new String[arraySize];
static public Date[] capturedTimeStamp = new Date[arraySize];
static public double[] capturedDoubleValue = new double[arraySize];
static public short[] capturedChildToDelete = new short[arraySize];
static public char[] capturedCharValue = new char[arraySize];
static public String[] capturedNextObjName = new String[arraySize];
static public int[] numberOfChildren = new int[arraySize];
static public int[] sumOfChildrenIntValue = new int[arraySize];
static public boolean[] processedIndex = new boolean[arraySize];
static public boolean[] transactionActive = new boolean[arraySize];
static private int nextKeyValue = 1;
private int keyValue; // persistent--used as key field in application identity
public String name;
public Date timeStamp;
public InstanceCallbackClass nextObj;
public HashSet children;
public int intValue;
public double doubleValue;
public short childToDelete;
public char charValue;
static public void initializeStaticsForTest()
{
performPreClearTests = false;
performPreDeleteTests = false;
performPreStoreTests = false;
preDeleteCalled = false;
for( int i = 0; i < arraySize; ++i){
processedIndex[i] = false;
transactionActive[i] = false;
}
}
static public void removeAllInstances(PersistenceManager pm)
{
//Extent e = pm.getExtent(org.objectweb.speedo.runtime.tck.InstanceCallbackClass.class, true);
Extent e = pm.getExtent(org.objectweb.speedo.pobjects.tck.InstanceCallbackClass.class, false);
Iterator i = e.iterator();
while( i.hasNext() ){
pm.deletePersistent(i.next());
}
}
InstanceCallbackClass() { // not used by application
}
public InstanceCallbackClass(String label,Date createTime,int intValue,double doubleValue,short childToDelete,char charValue,InstanceCallbackClass obj) {
keyValue = nextKeyValue++;
name = label;
timeStamp = createTime;
nextObj = obj;
children = new HashSet();
this.intValue = intValue;
this.doubleValue = doubleValue;
this.childToDelete = childToDelete;
this.charValue = charValue;
}
public void addChild(InstanceCallbackClass child) {
children.add(child);
}
public void jdoPreStore() {
if(!performPreStoreTests) { return; }
captureValues2();
PersistenceManager pm = JDOHelper.getPersistenceManager(this);
usePM(pm);
}
public void jdoPreDelete() {
if(!performPreDeleteTests) { return; }
// The following two variables set for CallingJdoPreDelete tests
preDeleteCalled = true;
objectState = InstanceCallbackClass.currentState(this);
captureValues2();
// The rest of this routine used for Accessing FieldsInPredelete tests
// check that intValue is a valid index
if(intValue >= 0 & intValue < arraySize) {
PersistenceManager pm = JDOHelper.getPersistenceManager(this);
usePM(pm);
if(nextObj != null) {
pm.deletePersistent(nextObj); // delete referenced object
// delete designated child
for(Iterator i = children.iterator(); i.hasNext();) {
InstanceCallbackClass obj = (InstanceCallbackClass)i.next();
if( obj.intValue == childToDelete) {
pm.deletePersistent(obj);
break;
}
}
}
}
}
public void jdoPostLoad() {
}
public void jdoPreClear() {
if(!performPreClearTests) { return; }
// the following code is copied from captureValues, because it must
// not be enhanced for execution during jdoPreClear.
// check that intValue is a valid index
if(intValue >= 0 & intValue < arraySize) {
processedIndex[intValue] = true;
// capture values of the attributes
capturedName[intValue] = name;
capturedTimeStamp[intValue] = timeStamp;
capturedDoubleValue[intValue] = doubleValue;
numberOfChildren[intValue] = children.size();
sumOfChildrenIntValue[intValue] = 0;
for(Iterator i = children.iterator(); i.hasNext();) {
InstanceCallbackClass o = (InstanceCallbackClass)i.next();
sumOfChildrenIntValue[intValue] += o.intValue;
}
capturedChildToDelete[intValue] = childToDelete;
capturedCharValue[intValue] = charValue;
if(nextObj != null) {
capturedNextObjName[intValue] = nextObj.name;
} else {
capturedNextObjName[intValue] = null;
}
}
}
private void captureValues2() {
// check that intValue is a valid index
if(intValue >= 0 & intValue < arraySize) {
processedIndex[intValue] = true;
// capture values of the attributes
capturedName[intValue] = name;
capturedTimeStamp[intValue] = timeStamp;
capturedDoubleValue[intValue] = doubleValue;
numberOfChildren[intValue] = children.size();
sumOfChildrenIntValue[intValue] = 0;
for(Iterator i = children.iterator(); i.hasNext();) {
InstanceCallbackClass o = (InstanceCallbackClass)i.next();
sumOfChildrenIntValue[intValue] += o.intValue;
}
capturedChildToDelete[intValue] = childToDelete;
capturedCharValue[intValue] = charValue;
if(nextObj != null) {
capturedNextObjName[intValue] = nextObj.name;
} else {
capturedNextObjName[intValue] = null;
}
}
}
private void usePM(PersistenceManager pm) {
if(intValue >= 0 & intValue < arraySize) {
Transaction t = pm.currentTransaction();
if(t.isActive()) {
transactionActive[intValue] = true;
}
}
}
public static class KeyClass implements Serializable {
public int keyValue;
public KeyClass() {
}
public KeyClass(String s) {
try{ keyValue = Integer.parseInt(s);}
catch(NumberFormatException e){
keyValue = 0;}
}
public boolean equals(Object obj) {
if( obj == null || !this.getClass().equals(obj.getClass()) ) return false;
else return keyValue == ((KeyClass)obj).keyValue;
}
public int hashCode() {
return keyValue;
}
public String toString() {
return Integer.toString(keyValue);
}
}
}