Author: aadamchik
Date: Mon Jul  3 14:24:46 2006
New Revision: 418855

URL: http://svn.apache.org/viewvc?rev=418855&view=rev
Log:
CAY-586

Added:
    
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/Validating.java
    
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneContextValidationTst.java
Modified:
    
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/CayenneContext.java
    
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/DataObject.java
    
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/testdo/mt/ClientMtTable1.java

Modified: 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/CayenneContext.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/CayenneContext.java?rev=418855&r1=418854&r2=418855&view=diff
==============================================================================
--- 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/CayenneContext.java
 (original)
+++ 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/CayenneContext.java
 Mon Jul  3 14:24:46 2006
@@ -57,6 +57,7 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 
 import org.objectstyle.cayenne.event.EventManager;
@@ -68,6 +69,8 @@
 import org.objectstyle.cayenne.query.ObjectIdQuery;
 import org.objectstyle.cayenne.query.Query;
 import org.objectstyle.cayenne.util.EventUtil;
+import org.objectstyle.cayenne.validation.ValidationException;
+import org.objectstyle.cayenne.validation.ValidationResult;
 
 /**
  * A default generic implementation of ObjectContext suitable for accessing 
Cayenne from
@@ -217,21 +220,49 @@
      * service via an internal instance of CayenneConnector.
      */
     public void commitChanges() {
-        doCommitChanges();
+        doCommitChanges(true);
     }
 
-    GraphDiff doCommitChanges() {
+    GraphDiff doCommitChanges(boolean cascade) {
+
+        int syncType = cascade
+                ? DataChannel.FLUSH_CASCADE_SYNC
+                : DataChannel.FLUSH_NOCASCADE_SYNC;
+
         GraphDiff commitDiff = null;
 
         synchronized (graphManager) {
 
             if (graphManager.hasChanges()) {
 
+                ValidationResult result = new ValidationResult();
+                Iterator it = graphManager.dirtyNodes().iterator();
+                while (it.hasNext()) {
+                    Persistent p = (Persistent) it.next();
+                    if (p instanceof Validating) {
+                        switch (p.getPersistenceState()) {
+                            case PersistenceState.NEW:
+                                ((Validating) p).validateForInsert(result);
+                                break;
+                            case PersistenceState.MODIFIED:
+                                ((Validating) p).validateForUpdate(result);
+                                break;
+                            case PersistenceState.DELETED:
+                                ((Validating) p).validateForDelete(result);
+                                break;
+                        }
+                    }
+                }
+
+                if (result.hasFailures()) {
+                    throw new ValidationException(result);
+                }
+
                 graphManager.graphCommitStarted();
 
                 try {
                     commitDiff = channel.onSync(this, graphManager
-                            .getDiffsSinceLastFlush(), 
DataChannel.FLUSH_CASCADE_SYNC);
+                            .getDiffsSinceLastFlush(), syncType);
                 }
                 catch (Throwable th) {
                     graphManager.graphCommitAborted();
@@ -250,6 +281,10 @@
 
         return commitDiff;
     }
+    
+    public void commitChangesToParent() {
+        doCommitChanges(false);
+    }
 
     public void rollbackChanges() {
         synchronized (graphManager) {
@@ -259,16 +294,6 @@
                 graphManager.graphReverted();
 
                 channel.onSync(this, diff, DataChannel.ROLLBACK_CASCADE_SYNC);
-            }
-        }
-    }
-
-    public void commitChangesToParent() {
-        synchronized (graphManager) {
-            if (graphManager.hasChangesSinceLastFlush()) {
-                GraphDiff diff = graphManager.getDiffsSinceLastFlush();
-                graphManager.graphFlushed();
-                channel.onSync(this, diff, DataChannel.FLUSH_NOCASCADE_SYNC);
             }
         }
     }

Modified: 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/DataObject.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/DataObject.java?rev=418855&r1=418854&r2=418855&view=diff
==============================================================================
--- 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/DataObject.java
 (original)
+++ 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/DataObject.java
 Mon Jul  3 14:24:46 2006
@@ -56,14 +56,13 @@
 package org.objectstyle.cayenne;
 
 import org.objectstyle.cayenne.access.DataContext;
-import org.objectstyle.cayenne.validation.ValidationResult;
 
 /**
  * Defines basic methods for a persistent object in Cayenne.
  * 
  * @author Andrei Adamchik
  */
-public interface DataObject extends Persistent {
+public interface DataObject extends Persistent, Validating {
 
     public static final long DEFAULT_VERSION = Long.MIN_VALUE;
 
@@ -202,31 +201,4 @@
      * @deprecated since 1.2 use 'getObjectContext().prepareForAccess(object)'
      */
     public void resolveFault();
-
-    /**
-     * Performs property validation of the NEW object, appending any 
validation failures
-     * to the provided validationResult object. This method is invoked by 
DataContext
-     * before committing a NEW object to the database.
-     * 
-     * @since 1.1
-     */
-    public void validateForInsert(ValidationResult validationResult);
-
-    /**
-     * Performs property validation of the MODIFIED object, appending any 
validation
-     * failures to the provided validationResult object. This method is 
invoked by
-     * DataContext before committing a MODIFIED object to the database.
-     * 
-     * @since 1.1
-     */
-    public void validateForUpdate(ValidationResult validationResult);
-
-    /**
-     * Performs property validation of the DELETED object, appending any 
validation
-     * failures to the provided validationResult object. This method is 
invoked by
-     * DataContext before committing a DELETED object to the database.
-     * 
-     * @since 1.1
-     */
-    public void validateForDelete(ValidationResult validationResult);
 }

Added: 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/Validating.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/Validating.java?rev=418855&view=auto
==============================================================================
--- 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/Validating.java
 (added)
+++ 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/Validating.java
 Mon Jul  3 14:24:46 2006
@@ -0,0 +1,89 @@
+/* ====================================================================
+ * 
+ * The ObjectStyle Group Software License, version 1.1
+ * ObjectStyle Group - http://objectstyle.org/
+ * 
+ * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
+ * of the software. All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 
+ * 3. The end-user documentation included with the redistribution, if any,
+ *    must include the following acknowlegement:
+ *    "This product includes software developed by independent contributors
+ *    and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
+ *    Alternately, this acknowlegement may appear in the software itself,
+ *    if and wherever such third-party acknowlegements normally appear.
+ * 
+ * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
+ *    or promote products derived from this software without prior written
+ *    permission. For written permission, email
+ *    "andrus at objectstyle dot org".
+ * 
+ * 5. Products derived from this software may not be called "ObjectStyle"
+ *    or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
+ *    names without prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ * 
+ * This software consists of voluntary contributions made by many
+ * individuals and hosted on ObjectStyle Group web site.  For more
+ * information on the ObjectStyle Group, please see
+ * <http://objectstyle.org/>.
+ */
+package org.objectstyle.cayenne;
+
+import org.objectstyle.cayenne.validation.ValidationResult;
+
+/**
+ * Defines a number of callback methods that allow an object to be validated 
before safe.
+ * Entity class can implement this interface and its methods will be called 
automatically.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public interface Validating {
+
+    /**
+     * Performs property validation of the NEW object, appending any 
validation failures
+     * to the provided validationResult object. This method is invoked by 
ObjectContext
+     * before committing a NEW object to the database.
+     */
+    public void validateForInsert(ValidationResult validationResult);
+
+    /**
+     * Performs property validation of the MODIFIED object, appending any 
validation
+     * failures to the provided validationResult object. This method is 
invoked by
+     * ObjectContext before committing a MODIFIED object to the database.
+     */
+    public void validateForUpdate(ValidationResult validationResult);
+
+    /**
+     * Performs property validation of the DELETED object, appending any 
validation
+     * failures to the provided validationResult object. This method is 
invoked by
+     * ObjectContext before committing a DELETED object to the database.
+     */
+    public void validateForDelete(ValidationResult validationResult);
+}

Added: 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneContextValidationTst.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneContextValidationTst.java?rev=418855&view=auto
==============================================================================
--- 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneContextValidationTst.java
 (added)
+++ 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/CayenneContextValidationTst.java
 Mon Jul  3 14:24:46 2006
@@ -0,0 +1,127 @@
+/* ====================================================================
+ * 
+ * The ObjectStyle Group Software License, version 1.1
+ * ObjectStyle Group - http://objectstyle.org/
+ * 
+ * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
+ * of the software. All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 
+ * 3. The end-user documentation included with the redistribution, if any,
+ *    must include the following acknowlegement:
+ *    "This product includes software developed by independent contributors
+ *    and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
+ *    Alternately, this acknowlegement may appear in the software itself,
+ *    if and wherever such third-party acknowlegements normally appear.
+ * 
+ * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
+ *    or promote products derived from this software without prior written
+ *    permission. For written permission, email
+ *    "andrus at objectstyle dot org".
+ * 
+ * 5. Products derived from this software may not be called "ObjectStyle"
+ *    or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
+ *    names without prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ * 
+ * This software consists of voluntary contributions made by many
+ * individuals and hosted on ObjectStyle Group web site.  For more
+ * information on the ObjectStyle Group, please see
+ * <http://objectstyle.org/>.
+ */
+package org.objectstyle.cayenne;
+
+import org.objectstyle.cayenne.access.ClientServerChannel;
+import org.objectstyle.cayenne.remote.ClientChannel;
+import org.objectstyle.cayenne.remote.service.LocalConnection;
+import org.objectstyle.cayenne.testdo.mt.ClientMtTable1;
+import org.objectstyle.cayenne.testdo.mt.ClientMtTable2;
+import org.objectstyle.cayenne.unit.AccessStack;
+import org.objectstyle.cayenne.unit.CayenneTestCase;
+import org.objectstyle.cayenne.unit.CayenneTestResources;
+import org.objectstyle.cayenne.validation.ValidationException;
+
+public class CayenneContextValidationTst extends CayenneTestCase {
+
+    protected AccessStack buildAccessStack() {
+        return CayenneTestResources
+                .getResources()
+                .getAccessStack(MULTI_TIER_ACCESS_STACK);
+    }
+
+    public void testValidate() throws Exception {
+
+        deleteTestData();
+        DataChannel serverChannel = new ClientServerChannel(getDomain(), 
false);
+        ClientChannel clientChannel = new ClientChannel(
+                new LocalConnection(serverChannel),
+                true);
+
+        CayenneContext c = new CayenneContext(clientChannel);
+        
+        ClientMtTable1 o1 = (ClientMtTable1) c.newObject(ClientMtTable1.class);
+        o1.setGlobalAttribute1("G1");
+        o1.resetValidation(false);
+        
+        // this one is not validating
+        ClientMtTable2 o2 = (ClientMtTable2) c.newObject(ClientMtTable2.class);
+        o2.setTable1(o1);
+        
+        c.commitChanges();
+        assertTrue(o1.isValidatedForInsert());
+        assertFalse(o1.isValidatedForDelete());
+        assertFalse(o1.isValidatedForUpdate());
+        
+        o1.resetValidation(false);
+        o1.setGlobalAttribute1("G2");
+        
+        c.commitChanges();
+        assertFalse(o1.isValidatedForInsert());
+        assertFalse(o1.isValidatedForDelete());
+        assertTrue(o1.isValidatedForUpdate());
+        
+        o1.resetValidation(false);
+        c.deleteObject(o1);
+        
+        c.commitChanges();
+        assertFalse(o1.isValidatedForInsert());
+        assertTrue(o1.isValidatedForDelete());
+        assertFalse(o1.isValidatedForUpdate());
+        
+        ClientMtTable1 o11 = (ClientMtTable1) 
c.newObject(ClientMtTable1.class);
+        o11.setGlobalAttribute1("G1");
+        o11.resetValidation(true);
+        
+        try {
+            c.commitChanges();
+            fail("Validation failure must have prevented commit");
+        }
+        catch (ValidationException e) {
+           // expected
+        }
+    }
+}
\ No newline at end of file

Modified: 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/testdo/mt/ClientMtTable1.java
URL: 
http://svn.apache.org/viewvc/incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/testdo/mt/ClientMtTable1.java?rev=418855&r1=418854&r2=418855&view=diff
==============================================================================
--- 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/testdo/mt/ClientMtTable1.java
 (original)
+++ 
incubator/cayenne/main/branches/PROTO-3.0/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/testdo/mt/ClientMtTable1.java
 Mon Jul  3 14:24:46 2006
@@ -2,9 +2,17 @@
 
 import java.util.List;
 
+import org.objectstyle.cayenne.Validating;
 import org.objectstyle.cayenne.testdo.mt.auto._ClientMtTable1;
+import org.objectstyle.cayenne.validation.SimpleValidationFailure;
+import org.objectstyle.cayenne.validation.ValidationResult;
 
-public class ClientMtTable1 extends _ClientMtTable1 {
+public class ClientMtTable1 extends _ClientMtTable1 implements Validating {
+    
+    protected boolean validatedForDelete;
+    protected boolean validatedForInsert;
+    protected boolean validatedForUpdate;
+    protected boolean blow;
 
     // provide direct access to persistent properties for testing..
 
@@ -19,5 +27,50 @@
     public List getTable2ArrayDirect() {
         return table2Array;
     }
+    
+    public void resetValidation(boolean blow) {
+        this.blow = blow;
+        this.validatedForDelete = false;
+        this.validatedForInsert = false;
+        this.validatedForUpdate = false;
+    }
+
+    public void validateForDelete(ValidationResult validationResult) {
+        validatedForDelete = true;
+        
+        if(blow) {
+            validationResult.addFailure(new SimpleValidationFailure(this, 
"test error"));
+        }
+    }
+
+    public void validateForInsert(ValidationResult validationResult) {
+        validatedForInsert = true;
+        
+        if(blow) {
+            validationResult.addFailure(new SimpleValidationFailure(this, 
"test error"));
+        }
+    }
+
+    public void validateForUpdate(ValidationResult validationResult) {
+        validatedForUpdate = true;
+        
+        if(blow) {
+            validationResult.addFailure(new SimpleValidationFailure(this, 
"test error"));
+        }
+    }
+
+    
+    public boolean isValidatedForDelete() {
+        return validatedForDelete;
+    }
 
+    
+    public boolean isValidatedForInsert() {
+        return validatedForInsert;
+    }
+
+    
+    public boolean isValidatedForUpdate() {
+        return validatedForUpdate;
+    }
 }


Reply via email to