You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-commits@db.apache.org by mb...@apache.org on 2005/05/22 20:01:48 UTC

svn commit: r171352 [7/11] - in /incubator/jdo/trunk/runtime20: ./ src/ src/conf/ src/java/ src/java/org/ src/java/org/apache/ src/java/org/apache/jdo/ src/java/org/apache/jdo/ejb/ src/java/org/apache/jdo/impl/ src/java/org/apache/jdo/impl/model/ src/java/org/apache/jdo/impl/model/java/ src/java/org/apache/jdo/impl/model/java/runtime/ src/java/org/apache/jdo/impl/model/jdo/ src/java/org/apache/jdo/impl/model/jdo/xml/ src/java/org/apache/jdo/impl/pm/ src/java/org/apache/jdo/impl/sco/ src/java/org/apache/jdo/impl/state/ src/java/org/apache/jdo/pm/ src/java/org/apache/jdo/query/ src/java/org/apache/jdo/sco/ src/java/org/apache/jdo/state/ src/java/org/apache/jdo/store/

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/sco/Vector.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/sco/Vector.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/sco/Vector.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/sco/Vector.java Sun May 22 11:01:45 2005
@@ -0,0 +1,737 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ * sco.Vector.java
+ */
+
+package org.apache.jdo.impl.sco;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import javax.jdo.JDOFatalInternalException;
+import javax.jdo.JDOUserException;
+
+import org.apache.jdo.sco.SCO;
+import org.apache.jdo.sco.SCOCollection;
+import org.apache.jdo.state.StateManagerInternal;
+import org.apache.jdo.util.I18NHelper;
+
+
+/**
+ * A mutable 2nd class object that represents Vector.
+ * @author Marina Vatkina
+ * @version 1.0.1
+ * @see java.util.Vector
+ */
+public class Vector extends java.util.Vector
+    implements SCOCollection {
+
+    private transient StateManagerInternal owner;
+
+    private transient int fieldNumber = -1;
+
+    private transient         Class elementType;
+ 
+    private transient boolean allowNulls;
+
+    private transient java.util.Vector added = new java.util.Vector();
+
+    private transient java.util.Vector removed = new java.util.Vector();
+
+    /**
+     * I18N message handler
+     */  
+    private final static I18NHelper msg = I18NHelper.getInstance(
+        "org.apache.jdo.impl.sco.Bundle");  // NOI18N
+
+    private final static String _Vector = "Vector"; // NOI18N
+
+    /**
+     * Constructs an empty vector so that its internal data array
+     * has size <tt>10</tt> and its standard capacity increment is
+     * zero.
+     * @param elementType the element types allowed
+     * @param allowNulls true if nulls are allowed
+     */
+    public Vector(Class elementType, boolean allowNulls) {
+        super();
+        this.elementType = elementType;
+        this.allowNulls = allowNulls;
+    }
+
+    /**
+     * Constructs an empty vector with the specified initial capacity and
+     * with its capacity increment equal to zero.
+     *
+     * @param elementType         the element types allowed 
+     * @param allowNulls         true if nulls are allowed 
+     * @param initialCapacity   the initial capacity of the vector.
+     * @exception IllegalArgumentException if the specified initial capacity
+     *               is negative
+     */
+    public Vector(Class elementType, boolean allowNulls, int initialCapacity) {
+        super(initialCapacity);
+        this.elementType = elementType;
+        this.allowNulls = allowNulls;
+    }
+
+    /** ------------------Public Methods----------------*/
+
+    /**
+     * Sets the component at the specified <code>index</code> of this
+     * vector to be the specified object. The previous component at that
+     * position is discarded.<p> 
+     *
+     * @param      obj     what the component is to be set to.
+     * @param      index   the specified index.
+     * @exception  ArrayIndexOutOfBoundsException  if the index was invalid.
+     * @see java.util.Vector
+     */  
+    public synchronized void setElementAt(Object obj, int index) {
+        SCOHelper.debug(_Vector, "setElementAt"); // NOI18N
+
+        if (obj == null) {
+            SCOHelper.assertNullsAllowed(obj, allowNulls);
+            // It is actualy remove
+            this.removeElementAt(index);
+        }
+
+        SCOHelper.assertElementType(obj, elementType);
+        // Mark the field as dirty
+        this.makeDirty();
+
+        Object o = super.elementAt(index);
+        super.setElementAt(obj, index);
+
+        if (added.remove(o) == false) {
+            removed.add(o);
+        }
+        if (removed.remove(obj) == false) {
+            added.add(obj);
+        }
+        // Apply updates
+        this.trackUpdates(true);
+    }
+
+    /**
+     * Deletes the component at the specified index. 
+     *
+     * @param      index   the index of the object to remove.
+     * @exception  ArrayIndexOutOfBoundsException  if the index was invalid.
+     * @see java.util.Vector
+     */  
+    public synchronized void removeElementAt(int index) {
+        SCOHelper.debug(_Vector, "removeElementAt"); // NOI18N
+
+        // Mark the field as dirty
+        this.makeDirty();
+
+        Object obj = super.elementAt(index);
+        super.removeElementAt(index);
+        if (added.remove(obj) == false)
+                removed.add(obj);
+
+        // Apply updates
+        this.trackUpdates(true);
+
+    }
+
+    /**
+     * Inserts the specified object as a component in this vector at the
+     * specified <code>index</code>.
+     * 
+     * @param      obj     the component to insert.
+     * @param      index   where to insert the new component.
+     * @exception  ArrayIndexOutOfBoundsException  if the index was invalid.
+     * @see java.util.Vector
+     */  
+    public synchronized void insertElementAt(Object obj, int index) {
+        SCOHelper.debug(_Vector, "insertElementAt"); // NOI18N
+
+        SCOHelper.assertNullsAllowed(obj, allowNulls);
+        SCOHelper.assertElementType(obj, elementType);
+        // Mark the field as dirty
+        this.makeDirty();
+
+        super.insertElementAt(obj, index);
+        if (removed.remove(obj) == false) {
+            added.add(obj);
+        }
+
+        // Apply updates
+        this.trackUpdates(true);
+    } 
+ 
+    /**
+     * Adds the specified component to the end of this vector,
+     * increasing its size by one. 
+     *   
+     * @param   obj   the component to be added.
+     * @see java.util.Vector 
+     */  
+    public synchronized void addElement(Object obj) {
+        SCOHelper.debug(_Vector, "addElement"); // NOI18N
+
+        SCOHelper.assertNullsAllowed(obj, allowNulls);
+        SCOHelper.assertElementType(obj, elementType);
+        // Mark the field as dirty
+        this.makeDirty();
+
+        super.addElement(obj);
+        if (removed.remove(obj) == false) {
+            added.add(obj); 
+        }
+
+        // Apply updates
+        this.trackUpdates(true);
+    }
+
+    /**
+     * Removes the first (lowest-indexed) occurrence of the argument
+     * from this vector. 
+     *   
+     * @param   obj   the component to be removed.
+     * @return  <code>true</code> if the argument was a component of this
+     *          vector; <code>false</code> otherwise.
+     * @see java.util.Vector
+     */   
+    public synchronized boolean removeElement(Object obj) {
+        SCOHelper.debug(_Vector, "removeElement"); // NOI18N
+
+        // Because java.util.Vector.removeElement(Object) calls internally 
+        // removeElementAt(int) which is not supported, we cannot rely on jdk. 
+        // We need to process remove here.
+
+        // Mark the field as dirty
+        this.makeDirty();
+
+        int i = super.indexOf(obj);
+        if (i > -1) {
+            super.removeElementAt(i);
+
+            if (added.remove(obj) == false) {
+                removed.add(obj);
+            }
+            // Apply updates
+            this.trackUpdates(true);
+            return true;
+        }
+        return false;
+
+    }
+
+    /**
+     * Removes all components from this vector and sets its size to zero.<p>
+     *
+     * @see java.util.Vector
+     */ 
+    public synchronized void removeAllElements() {
+        SCOHelper.debug(_Vector, "removeElements"); // NOI18N
+
+        // Mark the field as dirty
+        this.makeDirty();
+
+        for (Iterator iter = super.iterator(); iter.hasNext();) {
+            Object o = iter.next();
+            if (added.remove(o) == false) {
+                removed.add(o);
+            }
+        }
+        added.clear();
+
+        super.removeAllElements();
+
+        // Apply updates
+        this.trackUpdates(true);
+    }
+
+
+
+    /**
+     * Replaces the element at the specified position in this Vector with the
+     * specified element.
+     *   
+     * @param index index of element to replace.
+     * @param element element to be stored at the specified position.
+     * @return the element previously at the specified position.
+     * @exception ArrayIndexOutOfBoundsException index out of range
+     *            (index &lt; 0 || index &gt;= size()).
+     * @exception IllegalArgumentException fromIndex &gt; toIndex.
+     * @see java.util.Vector 
+     */  
+    public synchronized Object set(int index, Object element) {
+        SCOHelper.debug(_Vector, "set"); // NOI18N
+
+        if (element == null) {
+            SCOHelper.assertNullsAllowed(element, allowNulls);
+            // It is actualy remove
+            return this.remove(index); 
+        }
+
+        SCOHelper.assertElementType(element, elementType);
+        // Mark the field as dirty
+        this.makeDirty();
+
+        Object o = super.set(index, element);
+        if (added.remove(o) == false) {
+                removed.add(o);
+        }
+        if (removed.remove(element) == false) {
+                added.add(element);
+        }
+
+        // Apply updates
+        this.trackUpdates(true);
+
+        return o;
+    } 
+
+
+    /**
+     * Appends the specified element to the end of this Vector.
+     *   
+     * @param o element to be appended to this Vector.
+     * @return true (as per the general contract of Collection.add).
+     * @see java.util.Vector
+     */  
+    public synchronized boolean add(Object o) {
+        SCOHelper.debug(_Vector, "add"); // NOI18N
+
+        SCOHelper.assertNullsAllowed(o, allowNulls);
+        SCOHelper.assertElementType(o, elementType);
+        // Mark the field as dirty
+        this.makeDirty();
+
+        if (removed.remove(o) == false) {
+            added.add(o);
+        }
+
+        boolean modified = super.add(o);
+
+        // Apply updates
+        this.trackUpdates(modified);
+
+        return modified;
+    }    
+
+    /**
+     * Removes the first occurrence of the specified element in this Vector
+     * If the Vector does not contain the element, it is unchanged.  
+     *
+     * @param o element to be removed from this Vector, if present.
+     * @return true if the Vector contained the specified element.
+     * @see java.util.Vector 
+     */   
+    public boolean remove(Object o) {
+        SCOHelper.debug(_Vector, "remove"); // NOI18N
+
+        return this.removeElement(o);
+    }
+
+    /**
+     * Inserts the specified element at the specified position in this Vector.
+     *   
+     * @param index index at which the specified element is to be inserted.
+     * @param element element to be inserted.
+     * @exception ArrayIndexOutOfBoundsException index is out of range
+     *            (index &lt; 0 || index &gt; size()).
+     * @see java.util.Vector
+     */  
+    public void add(int index, Object element) {
+        SCOHelper.debug(_Vector, "add by index"); // NOI18N
+
+        this.insertElementAt(element, index);
+    }
+
+    /**
+     * Removes the element at the specified position in this Vector.
+     * shifts any subsequent elements to the left (subtracts one from their
+     * indices).  Returns the element that was removed from the Vector.
+     *   
+     * @param index the index of the element to removed.
+     * @exception ArrayIndexOutOfBoundsException index out of range (index
+     *            &lt; 0 || index &gt;= size()).
+     * @see java.util.Vector 
+     */   
+    public synchronized Object remove(int index) {
+        SCOHelper.debug(_Vector, "remove by index"); // NOI18N
+
+
+        // Mark the field as dirty
+        this.makeDirty();
+
+        Object obj = super.remove(index);
+        if (added.remove(obj) == false) {
+            removed.add(obj);
+        }
+
+        // Apply updates
+        this.trackUpdates(true);
+
+        return obj;
+    } 
+
+    /**
+     * Removes all of the elements from this Vector.  The Vector will
+     * be empty after this call returns (unless it throws an exception).
+     *   
+     * @see java.util.Vector
+     */ 
+    public void clear() {
+        SCOHelper.debug(_Vector, "clear"); // NOI18N
+
+        this.removeAllElements();
+    }
+
+    /**
+     * Appends all of the elements in the specified Collection to the end of
+     * this Vector, in the order that they are returned by the specified
+     * Collection's Iterator.  
+     *   
+     * @param c elements to be inserted into this Vector.
+     * @see java.util.Vector
+     */  
+    public synchronized boolean addAll(Collection c) {
+        SCOHelper.debug(_Vector, "addAll"); // NOI18N
+
+        // iterate the collection and make a list of wrong elements.
+        Throwable[] err = new Throwable[c.size()];
+        int l = 0;
+
+        Iterator i = c.iterator();
+        while (i.hasNext()) {
+            Object o = i.next();
+            try {
+                SCOHelper.assertNullsAllowed(o, allowNulls);
+                SCOHelper.assertElementType(o, elementType);
+            } catch (Throwable e) {
+                err[l++] = e;
+            }
+        }
+        SCOHelper.validateResult(l, err);
+
+        // Mark the field as dirty
+        this.makeDirty();
+
+        removed.removeAll(c);
+        added.addAll(c);
+
+        boolean modified = super.addAll(c);
+
+        // Apply updates
+        this.trackUpdates(modified);
+
+        return modified;
+    }
+
+    /**
+     * Removes from this Vector all of its elements that are contained in the
+     * specified Collection.
+     *
+     * @return true if this Vector changed as a result of the call.
+     * @see java.util.Vector 
+     */   
+    public synchronized boolean removeAll(Collection c) {
+        SCOHelper.debug(_Vector, "removeAll"); // NOI18N
+
+        boolean modified = false;
+        // Mark the field as dirty
+        this.makeDirty();
+
+        Iterator e = c.iterator();
+        while (e.hasNext()) {
+            Object o = e.next();
+            if(super.contains(o)) {
+                removeInternal(o);
+                if (added.remove(o) == false)
+                    removed.add(o);
+                    modified = true;
+            }
+        }
+
+        // Apply updates
+        this.trackUpdates(modified);
+
+        return modified;
+    } 
+
+    /**
+     * Inserts all of the elements in in the specified Collection into this
+     * Vector at the specified position.  Shifts the element currently at
+     * that position (if any) and any subsequent elements to the right
+     * (increases their indices).  The new elements will appear in the Vector
+     * in the order that they are returned by the specified Collection's
+     * iterator.
+     *   
+     * @param index index at which to insert first element
+     *              from the specified collection.
+     * @param c elements to be inserted into this Vector.
+     * @exception ArrayIndexOutOfBoundsException index out of range (index
+     *            &lt; 0 || index &gt; size()).
+     * @see java.util.Vector  
+     */   
+    public synchronized boolean addAll(int index, Collection c) {
+        SCOHelper.debug(_Vector, "addAll from index"); // NOI18N
+
+        // iterate the collection and make a list of wrong elements.
+        Throwable[] err = new Throwable[c.size()];
+        int l = 0;
+
+        Iterator i = c.iterator();
+        while (i.hasNext()) {
+            Object o = i.next();
+            try {
+                SCOHelper.assertNullsAllowed(o, allowNulls);
+                SCOHelper.assertElementType(o, elementType);
+            } catch (Throwable e) {
+                err[l++] = e;
+            }
+        }
+        SCOHelper.validateResult(l, err);
+
+        // Mark the field as dirty
+        this.makeDirty();
+
+        removed.removeAll(c); 
+        added.addAll(c); 
+
+        boolean modified = super.addAll(index, c);
+
+        // Apply updates
+        this.trackUpdates(modified);
+
+        return modified;
+    }    
+
+    /**
+     * Retains only the elements in this Vector that are contained in the
+     * specified Collection.  
+     *   
+     * @return true if this Vector changed as a result of the call.
+     * @see java.util.Vector   
+     */    
+    public synchronized boolean retainAll(Collection c)  {
+        SCOHelper.debug(_Vector, "retainAll"); // NOI18N
+
+        boolean modified = false;
+        java.util.ArrayList v = new java.util.ArrayList();
+
+        // Mark the field as dirty
+        this.makeDirty();
+        for (Iterator iter = super.iterator(); iter.hasNext();) {
+            Object o = iter.next();
+            if (!c.contains(o)) {
+                v.add(o);
+                if (added.remove(o) == false) {
+                    removed.add(o);
+                }
+                modified = true;
+            }
+        }
+
+        // Now remove the rest (stored in "v")
+        for (Iterator iter = v.iterator(); iter.hasNext();) {
+            removeInternal(iter.next());
+        }
+
+        // Apply updates
+        this.trackUpdates(modified);
+
+
+        return modified;
+    }
+
+    /**
+     * Creates and returns a copy of this object.
+     *
+     * <P>Mutable Second Class Objects are required to provide a public
+     * clone method in order to allow for copying PersistenceCapable
+     * objects. In contrast to Object.clone(), this method must not throw a
+     * CloneNotSupportedException.
+     */
+    public Object clone() {
+        SCOHelper.debug(_Vector, "clone"); // NOI18N
+
+        Object obj = super.clone();
+        if (obj instanceof SCO) {
+            ((SCO)obj).unsetOwner(owner, fieldNumber);
+        }
+        return obj;
+    }
+
+    /**
+     * @see SCOCollection#reset()
+     */  
+    public void reset() {
+        added.clear();
+        removed.clear();
+    }
+
+    /**
+     * @see SCOCollection#addInternal(Object o)
+     */
+    public void addInternal(Object o) {
+        super.addElement(o);
+    }
+
+ 
+    /** 
+     * @see SCOCollection#addAllInternal(Collection c)
+     */ 
+    public void addAllInternal(Collection c) { 
+        super.addAll(c); 
+    } 
+ 
+    /**
+     * @see SCOCollection#getAdded()
+     */ 
+    public Collection getAdded() {
+        return (Collection)added;
+    }    
+
+    /** 
+     * @see SCOCollection#getRemoved()
+     */ 
+    public Collection getRemoved() { 
+        return (Collection)removed; 
+    }     
+
+    /**
+     * @see SCOCollection#clearInternal()
+     */  
+    public void clearInternal() {
+        //Cannot call super.clear() as it internally calls removeAllElements()
+        // which causes marking field as dirty.
+
+        int s = super.size() - 1;
+
+        // Need to loop backwards to avoid resetting size of the collection
+        for (int i = s; i > -1; i--) {
+                super.removeElementAt(i);
+        }
+                
+        this.reset();
+    }
+
+
+    /**
+     * @see SCOCollection#removeInternal(Object o)
+     */  
+    public void removeInternal(Object o) {
+        int i = super.indexOf(o);
+        super.remove(i);
+    }
+
+    /**
+     * @see SCO#unsetOwner(Object owner, int fieldNumber)
+     */
+    public void unsetOwner(Object owner, int fieldNumber) { 
+        // Unset only if owner and fieldNumber match.
+        if (this.owner == owner && this.fieldNumber == fieldNumber) {
+            this.owner = null; 
+            this.fieldNumber = -1; 
+        }
+    }
+
+    /**
+     * @see SCO#setOwner (Object owner, int fieldNumber)
+     */
+    public void setOwner (Object owner, int fieldNumber) {
+        // Set only if it was not set before.
+        if (this.owner == null && owner instanceof StateManagerInternal) {
+            this.owner = (StateManagerInternal)owner;    
+            this.fieldNumber = fieldNumber;
+        }
+    }
+    /** 
+     * @see SCO#getOwner()
+     */   
+    public Object getOwner() {   
+        return SCOHelper.getOwner(owner);  
+    } 
+ 
+    /**  
+     * @see SCO#getFieldName()
+     */   
+    public String getFieldName() {
+        return SCOHelper.getFieldName(owner, fieldNumber);
+    }
+
+    /**
+     * Marks object dirty
+     */
+    private void makeDirty() {
+        if (owner != null) {
+            owner.makeDirty(fieldNumber); //
+        }
+     }   
+
+    /**
+     * Apply changes 
+     */  
+    private void trackUpdates(boolean modified) {
+        if (modified && owner != null) {
+            owner.trackUpdates(fieldNumber, this);
+        }
+    }
+
+    /**
+     * @see SCOCollection#getElementType() {
+     */  
+    public Class getElementType() {
+        return elementType;
+    }
+
+    /**
+     * @see SCOCollection#allowNulls() {
+     */  
+    public boolean allowNulls() {
+        return allowNulls;
+    }
+
+    /** Get an iterator over the frozen elements of this collection. 
+     * This class does not require freezing, so this method returns 
+     * a standard iterator.
+     * @since 1.0.1
+     * @return an iterator over the elements.
+     */
+    public Iterator frozenIterator() {
+        return iterator();
+    }
+    
+    /** Set the contents of this Collection from the frozen elements.
+     * This class does not support explicit frozen operations, and this method
+     * always throws an exception.
+     * @since 1.0.1
+     * @param elements not used.
+     */
+    public void setFrozen(Object[] elements) {
+        throw new JDOFatalInternalException(msg.msg("EXC_UnsupportedFreezerOperation")); //NOI18N
+    }
+    
+    /** Get an iterator regardless of whether the collection is frozen. 
+     * This class does not support frozen operations and always returns 
+     * a regular iterator.
+     * @since 1.0.1
+     * @return an iterator over the elements.
+     */
+    public Iterator eitherIterator() {
+        return iterator();
+    }
+    
+}

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/sco/package.html
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/sco/package.html?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/sco/package.html (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/sco/package.html Sun May 22 11:01:45 2005
@@ -0,0 +1,27 @@
+<!--
+ Copyright 2005 The Apache Software Foundation.
+ 
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at 
+ 
+     http://www.apache.org/licenses/LICENSE-2.0
+ 
+ Unless required by applicable law or agreed to in writing, software 
+ distributed under the License is distributed on an "AS IS" BASIS, 
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ See the License for the specific language governing permissions and 
+ limitations under the License.
+-->
+
+<html>
+<head>
+<title>Package org.apache.jdo.impl.sco</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
+</head>
+
+<body bgcolor="#FFFFFF">
+This package contains implementation of the SCO-related interfaces.
+The interfaces are defined in <a href="../../package-summary.html">org.apache.jdo.sco</a>. 
+</body>
+</html>

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNew.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNew.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNew.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNew.java Sun May 22 11:01:45 2005
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  AutoPersistentNew.java    September 4, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import java.util.BitSet;
+
+import javax.jdo.Transaction;
+
+import org.apache.jdo.state.StateManagerInternal;
+import org.apache.jdo.store.StoreManager;
+
+
+/**
+ * This class represents AutoPersistentNew state specific state transitions as 
+ * requested by StateManagerImpl. This state is a result of a call to makePersistent 
+ * on a transient instance that references this instance (persistence-by-reachability).
+ *
+ * @author Marina Vatkina
+ */
+class AutoPersistentNew extends PersistentNew {
+
+    AutoPersistentNew() {
+        super();
+
+        isAutoPersistent = true;
+        stateType = AP_NEW;
+    }
+
+   /**
+    * @see LifeCycleState#transitionMakePersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionMakePersistent(StateManagerImpl sm) {
+        return changeState(P_NEW);
+    }
+
+   /**
+    * @see LifeCycleState#flush(BitSet loadedFields, BitSet dirtyFields,
+    *   StoreManager srm, StateManagerImpl sm)
+    */
+    protected LifeCycleState flush(BitSet loadedFields, BitSet dirtyFields,
+        StoreManager srm, StateManagerImpl sm) { 
+        if (sm.getPersistenceManager().insideCommit()) {
+            // Nothing to do - state is unreachable at commit.
+            sm.markAsFlushed();
+            return changeState(AP_PENDING);
+        }
+
+        int result = srm.insert(loadedFields, dirtyFields, sm); 
+
+        switch (result) {
+          case StateManagerInternal.FLUSHED_COMPLETE:
+            // Do NOT mark as flushed - it needs to be available for possible delete
+            // at commit.
+            return changeState(AP_NEW_FLUSHED);
+
+          case StateManagerInternal.FLUSHED_PARTIAL:
+            return changeState(AP_NEW_FLUSHED_DIRTY);
+
+          case StateManagerInternal.FLUSHED_NONE:
+          default:
+            return this;
+
+        }
+    }
+
+   /**
+    * @see LifeCycleState#transitionFromAutoPersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionFromAutoPersistent(StateManagerImpl sm) {
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+
+   /**
+    * Differs from the generic implementation in LifeCycleState that state transitions
+    * to Transient and keeps all fields. Called only if state becomes not reachable.
+    * @see LifeCycleState#transitionCommit(boolean retainValues, StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionCommit(boolean retainValues, StateManagerImpl sm) {
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+
+   /**
+    * Differs from the generic implementation in LifeCycleState that state transitions
+    * to Transient and restores all fields. Called only if state becomes not reachable.
+    * @see LifeCycleState#transitionRollback(boolean restoreValues, StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionRollback(boolean restoreValues, StateManagerImpl sm) {
+        if (restoreValues) {
+            sm.restoreFields();
+        } else {
+            sm.unsetSCOFields();
+        }
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+
+}
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNewFlushed.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNewFlushed.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNewFlushed.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNewFlushed.java Sun May 22 11:01:45 2005
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  AutoPersistentNewFlushed.java    September 4, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import java.util.BitSet;
+
+import javax.jdo.JDOFatalInternalException;
+import javax.jdo.Transaction;
+
+import org.apache.jdo.state.StateManagerInternal;
+import org.apache.jdo.store.StoreManager;
+
+
+/**
+ * This class represents AutoPersistentNewFlushed state specific state
+ * transitions as requested by StateManagerImpl. This state differs from
+ * AutoPersistentNew state as the correspondinfg instance has been flushed
+ * to a datastore.
+ *
+ * @author Marina Vatkina
+ */
+class AutoPersistentNewFlushed extends PersistentNewFlushed {
+    
+    protected AutoPersistentNewFlushed() {
+        super();
+
+        isAutoPersistent = true;
+        stateType = AP_NEW_FLUSHED;
+    }
+
+   /**
+    * @see LifeCycleState#transitionMakePersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionMakePersistent(StateManagerImpl sm) {
+        return changeState(P_NEW_FLUSHED);
+    }
+
+   /**
+    * @see LifeCycleState#transitionDeletePersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionDeletePersistent(StateManagerImpl sm) {
+        sm.registerTransactional();
+        sm.preDelete();
+        return changeState(P_NEW_FLUSHED_DELETED);
+    }
+
+   /**
+    * @see LifeCycleState#transitionWriteField(StateManagerImpl sm, 
+    * Transaction tx)
+    */
+    protected LifeCycleState transitionWriteField(StateManagerImpl sm, 
+        Transaction tx) { 
+        sm.registerTransactional();
+        return changeState(AP_NEW_FLUSHED_DIRTY); 
+    }
+
+   /**
+    * This is a reverse operation if reached.
+    * @see LifeCycleState#flush(BitSet loadedFields, BitSet dirtyFields,
+    *   StoreManager srm, StateManagerImpl sm)
+    */
+    protected LifeCycleState flush(BitSet loadedFields, BitSet dirtyFields,
+        StoreManager srm, StateManagerImpl sm) {
+        if (sm.getPersistenceManager().insideCommit()) {
+            // Need to restore state - it is unreachable at commit:          
+            if (srm.delete(loadedFields, dirtyFields, sm) ==
+                StateManagerInternal.FLUSHED_COMPLETE) {
+
+                sm.markAsFlushed();
+                return changeState(AP_PENDING); 
+            }
+        }
+        return this;
+    }
+
+   /**
+    * Differs from the generic implementation in LifeCycleState that state transitions
+    * to Transient and restores all fields. Called only if state becomes not reachable.
+    * @see LifeCycleState#transitionRollback(boolean restoreValues, StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionRollback(boolean restoreValues, StateManagerImpl sm) {
+        if (restoreValues) {
+            sm.restoreFields();
+        } else {
+            sm.unsetSCOFields();
+        }
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+
+   /**
+    * @see LifeCycleState#transitionCommit(boolean retainValues, StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionCommit(boolean retainValues, StateManagerImpl sm) {
+        throw new JDOFatalInternalException(msg.msg(
+            "EXC_InconsistentState", // NOI18N
+            "commit", this.toString())); // NOI18N
+    }
+}
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNewFlushedDirty.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNewFlushedDirty.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNewFlushedDirty.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentNewFlushedDirty.java Sun May 22 11:01:45 2005
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  AutoPersistentNewFlushedDirty.java    September 4, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import java.util.BitSet;
+
+import org.apache.jdo.state.StateManagerInternal;
+import org.apache.jdo.store.StoreManager;
+
+
+/**
+ * This class represents AutoPersistentNewFlushedDirty state specific state
+ * transitions as requested by StateManagerImpl. This state is the result
+ * calls: makePersistent-flush-write_field on the same instance.
+ *
+ * @author Marina Vatkina
+ */
+class AutoPersistentNewFlushedDirty extends PersistentNewFlushedDirty {
+    
+    protected AutoPersistentNewFlushedDirty() {
+        super();
+
+        isAutoPersistent = true;
+        stateType = AP_NEW_FLUSHED_DIRTY;
+    }
+
+   /**
+    * @see LifeCycleState#transitionMakePersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionMakePersistent(StateManagerImpl sm) {
+        return changeState(P_NEW_FLUSHED_DIRTY);
+    }
+
+   /**
+    * @see LifeCycleState#transitionDeletePersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionDeletePersistent(StateManagerImpl sm) {
+        sm.preDelete();
+        return changeState(P_NEW_FLUSHED_DELETED);
+    }
+
+   /**
+    * @see LifeCycleState#flush(BitSet loadedFields, BitSet dirtyFields,
+    *   StoreManager srm, StateManagerImpl sm)
+    */
+    protected LifeCycleState flush(BitSet loadedFields, BitSet dirtyFields,
+        StoreManager srm, StateManagerImpl sm) {
+        if (sm.getPersistenceManager().insideCommit()) {
+            // Need to restore state - it is unreachable at commit:
+            if (srm.delete(loadedFields, dirtyFields, sm) ==
+                StateManagerInternal.FLUSHED_COMPLETE) {
+                sm.markAsFlushed();
+                return changeState(AP_PENDING); 
+            }
+
+        } else { // flush for query
+            // Need to update state - it is flush for query
+            if (srm.update(loadedFields, dirtyFields, sm) ==
+                StateManagerInternal.FLUSHED_COMPLETE) {
+
+                // Do NOT mark as flushed - it needs to be processed again at commit.
+                return changeState(AP_NEW_FLUSHED);
+            }
+        }
+        return this;
+    }
+
+   /**
+    * Differs from the generic implementation in LifeCycleState that state transitions
+    * to Transient and restores all fields. Called only if state becomes not reachable.
+    * @see LifeCycleState#transitionRollback(boolean restoreValues, StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionRollback(boolean restoreValues, StateManagerImpl sm) {
+        if (restoreValues) {
+            sm.restoreFields();
+        } else {
+            sm.unsetSCOFields();
+        }
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+}
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentPending.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentPending.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentPending.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/AutoPersistentPending.java Sun May 22 11:01:45 2005
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  AutoPersistentPending.java    September 6, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import java.util.BitSet;
+
+import org.apache.jdo.store.StoreManager;
+
+
+/**
+ * This class represents AutoPersistentPending state that is a result of a 
+ * previous call to makePersistent on a transient instance that referenced this 
+ * instance (persistence-by-reachability). This state represents instance
+ * not reachable any more.
+ *
+ * @author Marina Vatkina
+ */
+class AutoPersistentPending extends AutoPersistentNewFlushed {
+
+    AutoPersistentPending() {
+        super();
+
+        stateType = AP_PENDING;
+    }
+
+   /**
+    * @see LifeCycleState#transitionFromAutoPersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionFromAutoPersistent(StateManagerImpl sm) {
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+
+   /**
+    * This is a no-op operation if reached.
+    * @see LifeCycleState#flush(BitSet loadedFields, BitSet dirtyFields,
+    *   StoreManager srm, StateManagerImpl sm)
+    */
+    protected LifeCycleState flush(BitSet loadedFields, BitSet dirtyFields,
+        StoreManager srm, StateManagerImpl sm) {
+        return this;
+    }
+
+   /**
+    * Differs from the generic implementation in LifeCycleState that state transitions
+    * to Transient and keeps all fields. Called only if state becomes not reachable.
+    * @see LifeCycleState#transitionCommit(boolean retainValues, StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionCommit(boolean retainValues, StateManagerImpl sm) {
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+
+   /**
+    * Differs from the generic implementation in LifeCycleState that state transitions
+    * to Transient and restores all fields. Called only if state becomes not reachable.
+    * @see LifeCycleState#transitionRollback(boolean restoreValues, StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionRollback(boolean restoreValues, StateManagerImpl sm) {
+        if (restoreValues) {
+            sm.restoreFields();
+        } else {
+            sm.unsetSCOFields();
+        }
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+
+}
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/Bundle.properties
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/Bundle.properties?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/Bundle.properties (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/Bundle.properties Sun May 22 11:01:45 2005
@@ -0,0 +1,68 @@
+#
+# Copyright 2005 The Apache Software Foundation.
+# 
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at 
+# 
+#     http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing, software 
+# distributed under the License is distributed on an "AS IS" BASIS, 
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+# See the License for the specific language governing permissions and 
+# limitations under the License.
+
+# This file should conform to netbeans standards
+# (http://www.netbeans.org/i18n)
+
+# resource bundle for the messages
+# key consists of: <PREFIX_><description>
+# <PREFIX_> - any valid prefix like MSG_, EXC_, etc.
+# <description> - short description started with the upper case letter and used
+# upper case to represent each next word.
+
+# XXX TBD: this message is not used:
+notsupported=Operation is not supported in this release.
+
+#
+# StateManagerImpl
+#
+EXC_CannotGetJDOImplHelper=A SecurityException was thrown when trying to get \
+the singleton JDOImplHelper instance. In order to get runtime metadata, you \
+must grant javax.jdo.spi.JDOPermission("getMetadata") to the codeBases \
+containing the JDO Reference Implementation (jdo.jar and jdori.jar).
+EXC_CannotSetStateManager=A SecurityException was thrown when calling \
+jdoReplaceStateManager on persistence capable class instance. In order to set \
+the state manager, you must grant \
+javax.jdo.spi.JDOPermission("setStateManager") to the codeBases containing \
+the JDO Reference Implementation (jdo.jar and jdori.jar) and to the codeBase \
+containing the class files of the persistent capable classes.
+EXC_PersistentInAnotherPersistenceManager=Invalid attempt to make persistent an object that is owned by a different PersistenceManager.
+EXC_NotNeededByRI=Method {0} is not needed in the reference implementation and should not have been invoked.
+EXC_CannotAccessModel=Cannot access JDO Model for {0}.
+EXC_SCONotCloneable=An instance of class {0} is of an SCO type but is not cloneable.
+
+# Exceptions for SCO type validation:
+EXC_WrongElementType=Element type of this tracked instance ({0}) is not assignable from type specified in the JDO Model ({1}).
+EXC_WrongValueType=Value type of this tracked instance ({0}) is not assignable from type specified in the JDO Model ({1}).
+EXC_WrongKeyType=Key type of this tracked instance ({0}) is not assignable from type specified in the JDO Model ({1}).
+
+# NOI18N
+EXC_NullLifeCycle=Internal error on replacingStateManager - LifeCycle is null.
+# NOI18N
+EXC_NotTransitionTransient=Internal error on replacingStateManager - not transitioning to transient.
+# NOI18N
+EXC_SameStateManager=Internal error on replacingStateManager - same StateManager.
+
+#
+# Lifecycle
+#
+EXC_DirtyInstance=Instance is dirty.
+EXC_AccessDeletedField=Access to fields is not allowed for deleted instances.
+# NOI18N
+# In the following exception {0} is "commit" or "rollback", and {1} is Lifecycle state.
+EXC_InconsistentState=Inconsistent state on {0}: {1}.
+EXC_TransactionNotActive=Transaction is not active.
+
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/Hollow.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/Hollow.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/Hollow.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/Hollow.java Sun May 22 11:01:45 2005
@@ -0,0 +1,219 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  Hollow.java    March 10, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import javax.jdo.Transaction;
+import javax.jdo.JDOFatalInternalException;
+
+import org.apache.jdo.state.FieldManager;
+
+/**
+ * This class represents Hollow state specific state transitions as requested
+ * by StateManagerImpl. This state is the result of commit or rollback of a
+ * persistent instance when retainValues flag on the Transaction is set to 
+ * false.
+ *
+ * @author Marina Vatkina
+ */
+class Hollow extends LifeCycleState {
+
+    Hollow() {
+        // these flags are set only in the constructor 
+        // and shouldn't be changed afterwards
+        // (cannot make them final since they are declared in superclass 
+        // but their values are specific to subclasses)
+        isPersistent = true;
+        isTransactional = false;
+        isDirty = false;
+        isNew = false;
+        isDeleted = false;
+
+        isBeforeImageUpdatable = false;
+        isRefreshable = false;
+        isFlushed = true;
+        
+        stateType = HOLLOW;
+    }
+
+   /**
+    * @see LifeCycleState#transitionDeletePersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionDeletePersistent(StateManagerImpl sm) {
+
+        sm.refresh();
+        sm.registerTransactional();
+        sm.preDelete();
+        return changeState(P_DELETED);
+    }
+
+   /**
+    * @see LifeCycleState#transitionMakeNontransactional(StateManagerImpl sm,
+    * Transaction tx)
+    */
+    protected LifeCycleState transitionMakeNontransactional(StateManagerImpl sm,
+        Transaction tx) {
+        return this;
+    }
+ 
+   /**
+    * @see LifeCycleState#transitionMakeTransient(StateManagerImpl sm, 
+      Transaction tx)
+    */
+    protected LifeCycleState transitionMakeTransient(StateManagerImpl sm, 
+        Transaction tx) { 
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+
+   /**
+    * @see LifeCycleState#transitionMakeTransactional(
+    * StateManagerImpl sm, Transaction tx)
+    */
+    protected LifeCycleState transitionMakeTransactional(StateManagerImpl sm, 
+        Transaction tx) {
+
+        if (tx.isActive()) {
+            sm.registerTransactional();
+            sm.refresh();
+            sm.postLoad();
+        
+            if (tx.getOptimistic()) {
+                sm.createBeforeImage();
+                return changeState(P_CLEAN_TX);
+            } else { // pessimistic transaction
+                sm.markAsFlushed();
+                return changeState(P_CLEAN);
+            }
+        } else { // transaction not active
+            return this;
+        }
+    }
+
+   /**
+    * @see LifeCycleState#transitionReload(StateManagerImpl sm, Transaction tx)
+    */
+    protected LifeCycleState transitionReload(StateManagerImpl sm, 
+        Transaction tx) {
+
+        sm.refresh();
+        return this.transitionLoad(sm, tx);
+    }
+
+   /**
+    * @see LifeCycleState#transitionRetrieve(StateManagerImpl sm, Transaction tx)
+    */
+    protected LifeCycleState transitionRetrieve(StateManagerImpl sm, 
+        Transaction tx) {
+
+        sm.loadUnloaded();
+        // This state behaves the same on retrieve request and reload:
+        return this.transitionLoad(sm, tx);
+    }
+    
+    /**
+     * @see LifeCycleState#transitionReplace(StateManagerImpl sm, 
+     * Transaction tx, int[] fields, FieldManager fieldManager)
+     */
+    protected LifeCycleState transitionReplace(StateManagerImpl sm, 
+        Transaction tx, int[] fields, FieldManager fieldManager) {
+
+        sm.replaceFields(fields, fieldManager);
+        return this.transitionLoad(sm, tx);
+    }
+    
+   /**
+    * @see LifeCycleState#transitionReadField(StateManagerImpl sm,
+    * Transaction tx)
+    */
+    protected LifeCycleState transitionReadField(StateManagerImpl sm, 
+        Transaction tx) {
+
+        boolean transactionActive = tx.isActive();
+        if (!tx.getNontransactionalRead()) {
+            assertTransaction(transactionActive);
+        }
+
+        sm.postLoad();
+        if (!tx.getOptimistic() && transactionActive) {
+            sm.registerTransactional();
+            // Need to remove from non-flushed cache
+            sm.markAsFlushed();
+            return changeState(P_CLEAN);
+        }
+
+        sm.registerNonTransactional();
+        return changeState(P_NON_TX);
+    }
+
+   /**
+    * @see LifeCycleState#transitionWriteField(StateManagerImpl sm,
+    * Transaction tx)
+    */
+    protected LifeCycleState transitionWriteField(StateManagerImpl sm, 
+        Transaction tx) {
+
+        boolean transactionActive = tx.isActive();
+        if (!tx.getNontransactionalWrite()) {
+            assertTransaction(transactionActive);
+        }
+
+        sm.postLoad();
+        if(transactionActive) {
+            //sm.createBeforeImage();
+            sm.registerTransactional();
+            return changeState(P_DIRTY);
+        } else {
+            sm.registerNonTransactional();
+            return changeState(P_NON_TX);
+        }
+            
+    }
+
+   /**
+    * This state transition is invalid for the rollback.
+    * @see LifeCycleState#transitionRollback(boolean restoreValues,
+    * StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionRollback(boolean restoreValues,
+                                                StateManagerImpl sm) {
+        throw new JDOFatalInternalException(msg.msg(
+            "EXC_InconsistentState", // NOI18N
+            "rollback", this.toString())); // NOI18N
+    }
+
+   /**
+    * Transitions LifeCycle state on reload or retrieve call.
+    */
+   private LifeCycleState transitionLoad(StateManagerImpl sm, Transaction tx) {
+        sm.postLoad();
+        if (tx.isActive() && !tx.getOptimistic()) {
+            sm.registerTransactional();
+            // Need to remove from non-flushed cache
+            sm.markAsFlushed();
+
+            return changeState(P_CLEAN);
+        } else {
+            sm.registerNonTransactional();
+        }
+        return changeState(P_NON_TX);
+    }
+}
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/LifeCycleState.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/LifeCycleState.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/LifeCycleState.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/LifeCycleState.java Sun May 22 11:01:45 2005
@@ -0,0 +1,573 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  LifeCycleState.java    March 10, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import java.util.BitSet;
+
+import javax.jdo.JDOUserException;
+import javax.jdo.JDOFatalInternalException;
+import javax.jdo.Transaction;
+
+import org.apache.jdo.state.FieldManager;
+import org.apache.jdo.store.StoreManager;
+import org.apache.jdo.util.I18NHelper;
+
+
+/**
+ * This is an abstract LifeCycleState that provides the most common implementation
+ * for the state transitions and most common values for the state flags. Each 
+ * specific state overrides only necessary methods. All states are represented 
+ * by the static array of state instances. States can only call back StateManagerImpl 
+ * if it is passed as a parameter to a method call. States do not hold references 
+ * to any instances and are StateManger independent.
+ * 
+ * @author Marina Vatkina
+ */
+abstract class LifeCycleState {
+
+    /**
+      * I18N message handler
+      */
+    protected final static I18NHelper msg = 
+        I18NHelper.getInstance("org.apache.jdo.impl.state.Bundle"); // NOI18N
+    
+    // These are state flags that are set to required valuers in specific state 
+    protected boolean isPersistent;
+    protected boolean isTransactional;
+    protected boolean isDirty;
+    protected boolean isNew;
+    protected boolean isDeleted;
+
+    // These are internal flags.
+    protected boolean isNavigable;
+    protected boolean isRefreshable;
+    protected boolean isBeforeImageUpdatable;
+    protected boolean isFlushed;
+    protected boolean isAutoPersistent = false;
+
+    // This flag is set to true if the state corresponds to an instance
+    // that exists in a datastore.
+    protected boolean isStored = true;
+
+    // The following flag states that merge is needed
+    protected boolean needMerge = true;
+
+    protected int     stateType;
+
+    /**
+     * Constants to specify the life cycle state type
+     */
+    final static protected int
+        TRANSIENT             = 0,
+        HOLLOW                = 1,
+        P_NON_TX              = 2,
+
+        P_CLEAN               = 3,
+        P_CLEAN_TX            = 4,
+ 
+        P_DIRTY               = 5,
+        P_DIRTY_FLUSHED       = 6,
+
+        P_NEW                 = 7,
+        P_NEW_FLUSHED         = 8,
+        P_NEW_FLUSHED_DELETED = 9,
+        P_NEW_FLUSHED_DIRTY   = 10,
+        P_NEW_DELETED         = 11,
+
+        P_DELETED             = 12,
+        P_DELETED_FLUSHED     = 13,
+
+        T_CLEAN               = 14,
+        T_DIRTY               = 15,
+
+        AP_NEW                = 16,
+        AP_NEW_FLUSHED        = 17,
+        AP_NEW_FLUSHED_DIRTY  = 18,
+        AP_PENDING            = 19,
+
+        TOTAL                 = 20;
+    
+    private static LifeCycleState stateTypes[];
+
+    
+    // ******************************************************************
+    // Initialisation stuff
+    // ******************************************************************
+
+    /**
+     * Static initialiser.
+     * Initialises the life cycle.
+     */
+    static {
+        initLifeCycleState();
+    }   
+    
+    /**
+     * Initialises the objects. This class implements the "state pattern".
+     */
+    
+    // This method is called (through the static initializer) 
+    // when the LifeCycleState class or any of its subclasses is loaded.
+    
+    // It is extremely important that this method is called before any of isNew etc is called,
+    // and before stateType() is called !!!
+    
+    protected static void initLifeCycleState() {
+        stateTypes = new LifeCycleState[TOTAL];
+
+        stateTypes[TRANSIENT] = null;
+        stateTypes[T_CLEAN] = new TransientClean();
+        stateTypes[T_DIRTY] = new TransientDirty();
+
+        stateTypes[P_CLEAN] = new PersistentClean();
+        stateTypes[P_CLEAN_TX] = new PersistentCleanTransactional();
+
+        stateTypes[P_DIRTY] = new PersistentDirty();
+        stateTypes[P_DIRTY_FLUSHED] = new PersistentDirtyFlushed();
+
+        stateTypes[P_NEW] = new PersistentNew();
+        stateTypes[P_NEW_FLUSHED] = new PersistentNewFlushed();
+        stateTypes[P_NEW_DELETED] = new PersistentNewDeleted();
+        stateTypes[P_NEW_FLUSHED_DELETED] = new PersistentNewFlushedDeleted();
+        stateTypes[P_NEW_FLUSHED_DIRTY] = new PersistentNewFlushedDirty();
+
+        stateTypes[AP_NEW] = new AutoPersistentNew();
+        stateTypes[AP_NEW_FLUSHED] = new AutoPersistentNewFlushed();
+        stateTypes[AP_NEW_FLUSHED_DIRTY] = new AutoPersistentNewFlushedDirty();
+        stateTypes[AP_PENDING] = new AutoPersistentPending();
+
+        stateTypes[P_DELETED] = new PersistentDeleted();
+        stateTypes[P_DELETED_FLUSHED] = new PersistentDeletedFlushed();              
+
+        stateTypes[HOLLOW] = new Hollow();
+        stateTypes[P_NON_TX] = new PersistentNonTransactional();
+    }
+
+    /**
+     * Returns the LifeCycleState for the state constant.
+     *
+     * @param state the state type as integer
+     * @return the type as LifeCycleState object
+     */
+    protected static LifeCycleState getLifeCycleState(int state) {
+        return stateTypes[state];
+    }
+    
+    /**
+     * Returns the type of the life cycle state as an int.
+     *
+     * @return the type of this life cycle state as an int.
+     *
+     */
+    protected int stateType() {
+        return stateType;
+    }
+    
+    /**
+     * Transitions LifeCycleState on call to makeTransient.
+     * 
+     * @see javax.jdo.PersistenceManager#makeTransient(Object pc)
+     * @param sm StateManagerImpl requested the transition.
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @return new LifeCycleState.
+     */
+    protected LifeCycleState transitionMakeTransient(StateManagerImpl sm, 
+        Transaction tx) {
+        throw new JDOUserException(msg.msg(
+                    "EXC_DirtyInstance"), sm.getObject()); // NOI18N
+    }
+
+    /**
+     * Transitions LifeCycleState on call to makeTransactional
+     *  
+     * @see javax.jdo.PersistenceManager#makeTransactional(Object pc) 
+     * @param sm StateManagerImpl requested the transition.
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @return new LifeCycleState.
+     */ 
+    protected LifeCycleState transitionMakeTransactional(StateManagerImpl sm, 
+        Transaction tx) {
+        return this;
+    }
+
+    /**
+     * Transitions LifeCycleState on call to makeNontransactional 
+     *   
+     * @see javax.jdo.PersistenceManager#makeNontransactional(Object pc) 
+     * @param sm StateManagerImpl requested the transition. 
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @return new LifeCycleState.
+     */ 
+    protected LifeCycleState transitionMakeNontransactional(StateManagerImpl sm, 
+        Transaction tx) {
+        throw new JDOUserException(msg.msg(
+                    "EXC_DirtyInstance"), sm.getObject()); // NOI18N
+    }
+
+
+    /** 
+     * Transitions LifeCycleState on call to makePersistent  
+     *   
+     * @see javax.jdo.PersistenceManager#makePersistent(Object pc)  
+     * @param sm StateManagerImpl requested the transition.  
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionMakePersistent(StateManagerImpl sm) {
+        return this;
+    }
+
+    /** 
+     * Transitions LifeCycleState as a result of call to makePersistent  
+     * of a referencing instance (persistence-by-reachability)
+     *   
+     * @see javax.jdo.PersistenceManager#makePersistent(Object pc)  
+     * @param sm StateManagerImpl requested the transition.  
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionToAutoPersistent(StateManagerImpl sm) {
+        return this;
+    }
+
+    /** 
+     * Transitions LifeCycleState to transient for AutoPersistent instance
+     * that is not referenced anymore (persistence-by-reachability)
+     *   
+     * @see javax.jdo.PersistenceManager#makePersistent(Object pc)  
+     * @param sm StateManagerImpl requested the transition.  
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionFromAutoPersistent(StateManagerImpl sm) {
+        return this;
+    }
+
+    /** 
+     * Transitions LifeCycleState on call to deletePersistent  
+     *   
+     * @see javax.jdo.PersistenceManager#deletePersistent(Object pc)  
+     * @param sm StateManagerImpl requested the transition.  
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionDeletePersistent(StateManagerImpl sm) {
+        return this;
+    }
+
+    /** 
+     * Transitions LifeCycleState on call to evict an instance.
+     *   
+     * @see javax.jdo.PersistenceManager#evict(Object pc)  
+     * @param sm StateManagerImpl requested the transition.  
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionEvict(StateManagerImpl sm, Transaction tx) {
+        return this;
+    }
+
+    /** 
+     * Transitions LifeCycleState on call to refresh an instance.
+     *   
+     * @see javax.jdo.PersistenceManager#refresh(Object pc)  
+     * @param sm StateManagerImpl requested the transition.  
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionRefresh(StateManagerImpl sm, Transaction tx) {
+        return this;
+    }
+
+    /** 
+     * Transitions LifeCycleState on call to retrieve an instance.
+     *   
+     * @see javax.jdo.PersistenceManager#retrieve(Object pc)  
+     * @param sm StateManagerImpl requested the transition.  
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionRetrieve(StateManagerImpl sm, Transaction tx) {
+        return this;
+    }
+    
+    /** 
+     * Transitions the lifecycle state as if the instance is retrieved from
+     * the datastore, but use the specified field values instead of loading
+     * them from the datastore.
+     * @param sm StateManagerImpl requested the transition.  
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @param fields Indicates which fields should be replaced in the PC.
+     * @param fieldManager FieldManager from which the field's value should be
+     * @return new LifeCycleState. 
+     */
+    protected LifeCycleState transitionReplace(StateManagerImpl sm, 
+        Transaction tx, int[] fields, FieldManager fieldManager) {
+
+        sm.replaceUnloadedFields(fields, fieldManager);
+        return this;
+    }
+
+
+    /** 
+     * Transitions LifeCycleState on call to reload an instance. This is the
+     * result of a request to validate non-transactional instance.
+     *   
+     * @see javax.jdo.PersistenceManager#getObjectById(Object oid, boolean validate)  
+     * @param sm StateManagerImpl requested the transition.  
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionReload(StateManagerImpl sm, Transaction tx) {
+        return this;
+    }
+
+    /** 
+     * Transitions LifeCycleState on commit. Called by TransactionImpl.afterCompletion.
+     *   
+     * @param retainValues the value of the flag in the Transaction instance 
+     * associated with the hashing PersistenceManager
+     * @param sm StateManagerImpl requested the transition.  
+     * @return new LifeCycleState. 
+     */  
+     protected LifeCycleState transitionCommit(boolean retainValues, StateManagerImpl sm) {
+         throw new JDOFatalInternalException(msg.msg(
+            "EXC_InconsistentState", // NOI18N
+            "commit", this.toString())); // NOI18N
+     }
+
+    /** 
+     * Transitions LifeCycleState on rollback. Called by TransactionImpl.afterCompletion.
+     *   
+     * @param restoreValues the value of the flag in the Transaction instance 
+     * associated with the hashing PersistenceManager
+     * @param sm StateManagerImpl requested the transition.  
+     * @return new LifeCycleState. 
+     */  
+     protected LifeCycleState transitionRollback(boolean restoreValues, StateManagerImpl sm) {
+         int newstate;
+
+         if (restoreValues) {
+              sm.restoreFields();
+              newstate = P_NON_TX;
+         } else {
+              sm.clearFields();
+              newstate = HOLLOW;
+         }
+
+         sm.reset();
+         return changeState(newstate);
+
+    }
+
+    /** 
+     * Performs state specific flush operation and transitions LifeCycleState depending
+     * on the result.
+     *   
+     * @param loadedFields BitSet of fields loaded from the database.
+     * @param dirtyFields BitSet of changed fields that are to be flushed and/or
+     * verified against those in the database, if this <code>flush</code> is within the
+     * context of an optimistic transaction.  After return, bits will remain set
+     * for fields that were not flushed, and in such case the return will be
+     * <code>StateManagerInternal.FLUSHED_PARTIAL</code>. If the <code>flush</code>
+     * was not performed because of the dependency or other restrictions, the
+     * return will be <code>StateManagerInternal.FLUSHED_NONE</code>. If operation
+     * was successful, the return will be <code>StateManagerInternal.FLUSHED_COMPLETE</code>.
+     * @param sm StateManagerImpl requested the transition.  
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState flush(BitSet loadedFields, BitSet dirtyFields,
+        StoreManager srm, StateManagerImpl sm) {
+        return this;
+    }
+
+    /** 
+     * Transitions LifeCycleState on call to read a field.
+     *   
+     * @param sm StateManagerImpl requested the transition.  
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionReadField(StateManagerImpl sm, Transaction tx) {
+        assertTransaction(tx.isActive());
+        return this;
+    }
+
+    /** 
+     * Transitions LifeCycleState on call to write a field.
+     *   
+     * @param sm StateManagerImpl requested the transition.  
+     * @param tx Transaction associated with the hashing PersistenceManager
+     * @return new LifeCycleState. 
+     */  
+    protected LifeCycleState transitionWriteField(StateManagerImpl sm, Transaction tx) {
+        assertTransaction(tx.isActive());
+        return this;
+    }
+
+    /**
+     * Asserts that current transaction is active.
+     * 
+     * @throws JDOUserException if transaction is not active
+     * @param transactionActive true if the current transaction is active
+     */
+    protected void assertTransaction(boolean transactionActive) {
+        if (!transactionActive) {
+            throw new JDOUserException(msg.msg(
+                "EXC_TransactionNotActive")); // NOI18N
+        }
+    }
+                                       
+    /***************************************************************/
+    /************** Methods that return values for flags ***********/
+    /***************************************************************/
+    
+    /**
+     * Return whether the object state is persistent.
+     */  
+    protected boolean isPersistent() {
+        return isPersistent;
+    }
+    
+    /**
+     * Return whether the object state is transactional.
+     */    
+    protected boolean isTransactional() {
+        return isTransactional;
+    }
+        
+    /**
+     * Return whether the object state is dirty, that is, the object has been 
+     * changed (created, updated, deleted) in this Tx.
+     */
+    protected boolean isDirty() {
+        return isDirty;
+    }
+    
+    /**
+     * Return whether the state represents a newly created object. 
+     */
+    protected boolean isNew() {
+        return isNew;
+    }    
+    
+    /**
+     * Return whether the state represents a deleted object. 
+     */
+    protected boolean isDeleted() {
+        return isDeleted;
+    }
+
+    /**
+     * Return whether the object can dynamically navigate to fields that are
+     * not present.
+     */
+    protected boolean isNavigable() {
+        return isNavigable;
+    }
+
+    /**
+     * Return whether the object can be refreshed from the datastore.
+     */
+    protected boolean isRefreshable() {
+        return isRefreshable;
+    }
+
+    /**
+     * Return whether the beforeImage can be updated.
+     */
+    protected boolean isBeforeImageUpdatable() {
+        return isBeforeImageUpdatable;
+    }
+
+    /**
+     * Return whether the object has been flushed to the datastore.
+     */
+    protected boolean isFlushed() {
+        return isFlushed;
+    }
+
+    /**
+     * Return whether the object state is persistent by reachabilty only.
+     */  
+    protected boolean isAutoPersistent() {
+        return isAutoPersistent;
+    }
+    
+    /**
+     * Return whether the object is stored in the datastore.
+     */
+    protected boolean isStored() {
+        return isStored;
+    }
+
+    /**
+     * Return whether the merge is needed.
+     */
+    protected boolean needMerge() {
+        return needMerge;
+    }
+
+    /*************************************************************/
+    /********************* Helper methods ************************/
+    /*************************************************************/
+    
+    /** 
+     * Changes Life Cycle State.
+     *
+     * @return new LifeCycleState. 
+     */
+    protected LifeCycleState changeState(int newStateType) {
+        LifeCycleState lc = stateTypes[newStateType];
+        return lc;
+    }
+
+    public String toString() {
+        switch (stateType) {
+            case HOLLOW: return "HOLLOW"; // NOI18N
+            case P_NON_TX: return "P_NON_TX"; // NOI18N
+
+            case T_CLEAN: return "T_CLEAN"; // NOI18N
+            case T_DIRTY: return "T_DIRTY"; // NOI18N
+
+            case P_CLEAN: return "P_CLEAN"; // NOI18N
+            case P_CLEAN_TX: return "P_CLEAN_TX"; // NOI18N
+
+            case P_DIRTY: return "P_DIRTY"; // NOI18N
+            case P_DIRTY_FLUSHED: return "P_DIRTY_FLUSHED"; // NOI18N
+
+            case P_NEW: return "P_NEW"; // NOI18N
+            case P_NEW_FLUSHED: return "P_NEW_FLUSHED"; // NOI18N
+            case P_NEW_FLUSHED_DELETED: return "P_NEW_FLUSHED_DELETED"; // NOI18N
+            case P_NEW_FLUSHED_DIRTY: return "P_NEW_FLUSHED_DIRTY"; // NOI18N
+            case P_NEW_DELETED: return "P_NEW_DELETED"; // NOI18N
+
+            case AP_NEW: return "AP_NEW"; // NOI18N
+            case AP_NEW_FLUSHED: return "AP_NEW_FLUSHED"; // NOI18N
+            case AP_NEW_FLUSHED_DIRTY: return "AP_NEW_FLUSHED_DIRTY"; // NOI18N
+            case AP_PENDING: return "AP_PENDING"; // NOI18N
+
+            case P_DELETED: return "P_DELETED"; // NOI18N
+            case P_DELETED_FLUSHED: return "P_DELETED_FLUSHED"; // NOI18N
+        }
+
+        return null;
+    }
+
+}
+
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentClean.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentClean.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentClean.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentClean.java Sun May 22 11:01:45 2005
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  PersistentClean.java    March 10, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import javax.jdo.Transaction;
+
+/**
+ * This class represents PersistentClean state specific state transitions as 
+ * requested by StateManagerImpl. This state is the result of any of the 
+ * following operations:
+ *     - read field in an active datastore transaction;
+ *     - makeTransactional call on a PersistentNonTransactional instance;
+ *     - refresh of a PersistentDirty instance in an active datastore transaction.
+ *
+ * @author Marina Vatkina
+ */
+class PersistentClean extends LifeCycleState {
+
+    PersistentClean() {
+        // these flags are set only in the constructor 
+        // and shouldn't be changed afterwards
+        // (cannot make them final since they are declared in superclass 
+        // but their values are specific to subclasses)
+        isPersistent = true;
+        isTransactional = true;
+        isDirty = false;
+        isNew = false;
+        isDeleted = false;
+
+        isFlushed = true;
+        isNavigable = true;
+        isRefreshable = true;
+        isBeforeImageUpdatable = false;
+
+        stateType = P_CLEAN;
+    }
+
+   /**
+    * @see LifeCycleState#transitionDeletePersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionDeletePersistent(StateManagerImpl sm) {    
+        sm.registerTransactional();
+        sm.preDelete();
+        return changeState(P_DELETED);
+    }
+
+   /**
+    * @see LifeCycleState#transitionMakeNontransactional(StateManagerImpl sm,
+    *  Transaction tx)
+    */
+    protected LifeCycleState transitionMakeNontransactional(StateManagerImpl sm,
+        Transaction tx) {
+        sm.registerNonTransactional();
+        return changeState(P_NON_TX);
+    }
+
+   /**
+    * @see LifeCycleState#transitionMakeTransient(StateManagerImpl sm, 
+    * Transaction tx)
+    */
+    protected LifeCycleState transitionMakeTransient(StateManagerImpl sm, 
+        Transaction tx) {
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+    
+   /**
+    * @see LifeCycleState#transitionEvict(StateManagerImpl sm, Transaction tx)
+    */
+    protected LifeCycleState transitionEvict(StateManagerImpl sm, Transaction tx) {
+        sm.clearFields();
+        sm.reset();
+        sm.registerNonTransactional();
+        return changeState(HOLLOW);
+    }
+
+   /**
+    * @see LifeCycleState#transitionRetrieve(StateManagerImpl sm, Transaction tx)
+    */
+    protected LifeCycleState transitionRetrieve(StateManagerImpl sm,
+        Transaction tx) {
+
+        sm.loadUnloaded();
+        return this;
+    }
+
+   /**
+    * @see LifeCycleState#transitionWriteField(StateManagerImpl sm, Transaction tx)
+    */
+    protected LifeCycleState transitionWriteField(StateManagerImpl sm, 
+        Transaction tx) {
+
+        int newState;
+        boolean transactionActive = tx.isActive();
+
+        if (transactionActive) {
+            // This is the first write request: prepare BeforeImage
+            sm.createBeforeImage();
+            sm.registerTransactional();
+            newState = P_DIRTY;
+
+        } else {
+            if (!tx.getNontransactionalWrite()) {
+                assertTransaction(false);
+            }
+            sm.registerNonTransactional();
+            newState = P_NON_TX;
+        }
+        return changeState(newState);
+    }
+
+   /** 
+    * @see LifeCycleState#transitionCommit(boolean retainValues, StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionCommit(boolean retainValues, StateManagerImpl sm) {
+        int newstate;
+
+        if (retainValues) {
+            sm.replaceSCOFields();
+            newstate = P_NON_TX;
+        } else {
+            sm.clearFields();
+            newstate = HOLLOW;
+        }
+
+        sm.reset();
+        return changeState(newstate);
+    }
+}
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentCleanTransactional.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentCleanTransactional.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentCleanTransactional.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentCleanTransactional.java Sun May 22 11:01:45 2005
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  PersistentCleanTransactional.java    August 13, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import java.util.BitSet;
+
+import javax.jdo.Transaction;
+
+import org.apache.jdo.state.StateManagerInternal;
+import org.apache.jdo.store.StoreManager;
+
+
+/**
+ * This class represents PersistentCleanTransactional state specific state 
+ * transitions as requested by StateManagerImpl. This state differs from 
+ * PersistentClean in that it is the result of call to makeTransactional
+ * of a PersistentNonTransactional instance in an active optimistic transaction.
+ * This state verifies itself at flush.
+ *
+ * @author Marina Vatkina
+ */
+class PersistentCleanTransactional extends PersistentClean {
+
+    PersistentCleanTransactional() {
+        isFlushed = false;
+        stateType = P_CLEAN_TX;
+    }
+
+   /**
+    * @see LifeCycleState#transitionWriteField(StateManagerImpl sm,
+    * Transaction tx)
+    */
+    protected LifeCycleState transitionWriteField(StateManagerImpl sm,
+        Transaction tx) {
+        return changeState(P_DIRTY);
+    }
+
+   /** 
+    * @see LifeCycleState#flush(BitSet loadedFields, BitSet dirtyFields,
+    *   StoreManager srm, StateManagerImpl sm)
+    */
+    protected LifeCycleState flush(BitSet loadedFields, BitSet dirtyFields,
+        StoreManager srm, StateManagerImpl sm) {
+        if (srm.verifyFields(loadedFields, dirtyFields, sm) ==
+            StateManagerInternal.FLUSHED_COMPLETE) {
+            sm.markAsFlushed();
+            return changeState(P_CLEAN);
+        }
+        return this;
+    }
+
+}

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDeleted.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDeleted.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDeleted.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDeleted.java Sun May 22 11:01:45 2005
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  PersistentDeleted.java    March 10, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import java.util.BitSet;
+
+import javax.jdo.Transaction;
+import javax.jdo.JDOUserException;
+
+import org.apache.jdo.state.StateManagerInternal;
+import org.apache.jdo.store.StoreManager;
+
+
+/**
+ * This class represents PersistentDeleted state specific state transitions as requested
+ * by StateManagerImpl. This state is the result of a call to deletePersistent of a
+ * persistent instance.
+ *
+ * @author Marina Vatkina
+ */
+class PersistentDeleted extends LifeCycleState {
+
+    PersistentDeleted() {
+        // these flags are set only in the constructor 
+        // and shouldn't be changed afterwards
+        // (cannot make them final since they are declared in superclass 
+        // but their values are specific to subclasses)
+        isPersistent = true;
+        isTransactional = true;
+        isDirty = true;
+        isNew = false;
+        isDeleted = true;
+
+        isNavigable = false;
+        isRefreshable = true;
+        isBeforeImageUpdatable = false;
+        isFlushed = false;
+
+    // The following flag does not allow merge
+        needMerge = false;
+
+        stateType =  P_DELETED;
+    }
+
+
+   /**
+    * @see LifeCycleState#transitionReadField(StateManagerImpl sm,
+    * Transaction tx)
+    * @throws JDOUserException on read access
+    */
+    protected LifeCycleState transitionReadField(StateManagerImpl sm,
+        Transaction tx) {
+        // Cannot read a deleted object
+        throw new JDOUserException(msg.msg(
+                "EXC_AccessDeletedField")); // NOI18N
+    }
+
+   /**
+    * @see LifeCycleState#transitionWriteField(StateManagerImpl sm,
+    * Transaction tx)
+    * @throws JDOUserException on write access
+    */
+    protected LifeCycleState transitionWriteField(StateManagerImpl sm,
+        Transaction tx) {
+        // Cannot update a deleted object
+        throw new JDOUserException(msg.msg(
+                "EXC_AccessDeletedField")); // NOI18N
+    }
+
+   /**   
+    * @see LifeCycleState#flush(BitSet loadedFields, BitSet dirtyFields,
+    *   StoreManager srm, StateManagerImpl sm)
+    */
+    protected LifeCycleState flush(BitSet loadedFields, BitSet dirtyFields,
+        StoreManager srm, StateManagerImpl sm) {
+        if (srm.delete(loadedFields, dirtyFields, sm) ==
+            StateManagerInternal.FLUSHED_COMPLETE) {
+            sm.markAsFlushed();
+            return changeState(P_DELETED_FLUSHED);
+        }
+        return this;
+    }
+}
+
+
+
+
+
+
+
+
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDeletedFlushed.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDeletedFlushed.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDeletedFlushed.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDeletedFlushed.java Sun May 22 11:01:45 2005
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  PersistentDeletedFlushed.java    March 10, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+/**
+ * This class represents PersistentDeletedFlushed state specific state
+ * transitions as requested by StateManagerImpl. This state differs from
+ * PersistentDeleted in that it is the result of a flush operation on
+ * a deleted instance.
+ *
+ * @author Marina Vatkina
+ */
+class PersistentDeletedFlushed extends PersistentDeleted {
+
+    protected PersistentDeletedFlushed() {
+        super();
+        isFlushed = true;
+        isStored = false;
+
+        stateType =  P_DELETED_FLUSHED;
+    }
+
+   /**
+    * @see LifeCycleState#transitionCommit(boolean retainValues, StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionCommit(boolean retainValues, StateManagerImpl sm) {
+        sm.clearFields();
+        sm.disconnect();
+        return changeState(TRANSIENT);
+    }
+}
+
+
+
+
+
+
+
+
+

Added: incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDirty.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDirty.java?rev=171352&view=auto
==============================================================================
--- incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDirty.java (added)
+++ incubator/jdo/trunk/runtime20/src/java/org/apache/jdo/impl/state/PersistentDirty.java Sun May 22 11:01:45 2005
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+
+/*
+ *  PersistentDirty.java    March 10, 2001
+ */
+
+package org.apache.jdo.impl.state;
+
+import java.util.BitSet;
+
+import javax.jdo.Transaction;
+import javax.jdo.JDOFatalInternalException;
+
+import org.apache.jdo.state.StateManagerInternal;
+import org.apache.jdo.store.StoreManager;
+
+
+/**
+ * This class represents PersistentDirty state specific state transitions as requested
+ * by StateManagerImpl. This state is a result of a write operation on a persistent
+ * instance.
+ *
+ * @author Marina Vatkina
+ */
+class PersistentDirty extends LifeCycleState {
+
+    PersistentDirty() {
+        // these flags are set only in the constructor 
+        // and shouldn't be changed afterwards
+        // (cannot make them final since they are declared in superclass 
+        // but their values are specific to subclasses)
+        isPersistent = true;
+        isTransactional = true;
+        isDirty = true;
+        isNew = false;
+        isDeleted = false;
+
+        isNavigable = true;
+        isRefreshable = true;
+        isBeforeImageUpdatable = true;
+        isFlushed = false;
+        
+        stateType =  P_DIRTY;
+    }
+
+   /**
+    * @see LifeCycleState#transitionDeletePersistent(StateManagerImpl sm)
+    */
+    protected LifeCycleState transitionDeletePersistent(StateManagerImpl sm) {    
+        sm.preDelete();
+        return changeState(P_DELETED);
+    }
+    
+   /**
+    * @see LifeCycleState#transitionRefresh(StateManagerImpl sm, Transaction tx)
+    */
+    protected LifeCycleState transitionRefresh(StateManagerImpl sm,
+        Transaction tx) {
+        sm.unsetBeforeImage();
+        sm.refresh();
+
+        if (tx.isActive() && !tx.getOptimistic()) {
+            return changeState(P_CLEAN);
+        } else {
+            sm.registerNonTransactional();
+        }
+        return changeState(P_NON_TX);
+    }
+
+   /**
+    * @see LifeCycleState#transitionRetrieve(StateManagerImpl sm, Transaction tx)
+    */
+    protected LifeCycleState transitionRetrieve(StateManagerImpl sm,
+        Transaction tx) {
+
+        sm.loadUnloaded();
+        return this;
+    }
+
+   /**
+    * @see LifeCycleState#flush(BitSet loadedFields, BitSet dirtyFields,
+    *   StoreManager srm, StateManagerImpl sm) 
+    */ 
+    protected LifeCycleState flush(BitSet loadedFields, BitSet dirtyFields,
+        StoreManager srm, StateManagerImpl sm) {
+
+        if (srm.update(loadedFields, dirtyFields, sm) ==
+            StateManagerInternal.FLUSHED_COMPLETE) {
+
+            sm.markAsFlushed();
+            return changeState(P_DIRTY_FLUSHED);
+        }
+
+        return this;
+    }
+}