Author: aadamchik Date: Mon Nov 27 19:16:44 2006 New Revision: 479872 URL: http://svn.apache.org/viewvc?view=rev&rev=479872 Log: CAY-712: Allow modifying objects in validate* methods (fix for 1.2)
Added: incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/unit/util/ValidationDelegate.java Modified: incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectStoreGraphDiff.java incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/art/Artist.java incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/DataContextValidationTst.java Modified: incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectStoreGraphDiff.java URL: http://svn.apache.org/viewvc/incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectStoreGraphDiff.java?view=diff&rev=479872&r1=479871&r2=479872 ============================================================================== --- incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectStoreGraphDiff.java (original) +++ incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/access/ObjectStoreGraphDiff.java Mon Nov 27 19:16:44 2006 @@ -56,6 +56,7 @@ package org.objectstyle.cayenne.access; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -103,6 +104,11 @@ ValidationResult result = new ValidationResult(); boolean noop = true; + // build a new collection for validation as validation methods may result in + // ObjectStore modifications + + Collection objectsToValidate = null; + Iterator it = getChangesByObjectId().entrySet().iterator(); while (it.hasNext()) { @@ -110,10 +116,21 @@ if (!((ObjectDiff) entry.getValue()).isNoop()) { - noop = false; + if (noop) { + noop = false; + objectsToValidate = new ArrayList(); + } // accessing objectMap directly to avoid unneeded synchronization. - DataObject object = (DataObject) objectStore.getNodeNoSync(entry.getKey()); + objectsToValidate.add(objectStore.getNodeNoSync(entry.getKey())); + } + } + + if (objectsToValidate != null) { + Iterator validationIt = objectsToValidate.iterator(); + while (validationIt.hasNext()) { + DataObject object = (DataObject) validationIt.next(); + switch (object.getPersistenceState()) { case PersistenceState.NEW: object.validateForInsert(result); @@ -126,10 +143,10 @@ break; } } - } - - if (result.hasFailures()) { - throw new ValidationException(result); + + if (result.hasFailures()) { + throw new ValidationException(result); + } } return noop; Modified: incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/art/Artist.java URL: http://svn.apache.org/viewvc/incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/art/Artist.java?view=diff&rev=479872&r1=479871&r2=479872 ============================================================================== --- incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/art/Artist.java (original) +++ incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/art/Artist.java Mon Nov 27 19:16:44 2006 @@ -1,10 +1,12 @@ package org.objectstyle.art; import org.objectstyle.art.auto._Artist; +import org.objectstyle.cayenne.unit.util.ValidationDelegate; import org.objectstyle.cayenne.validation.ValidationResult; public class Artist extends _Artist { + protected transient ValidationDelegate validationDelegate; protected boolean validateForSaveCalled; public boolean isValidateForSaveCalled() { @@ -15,8 +17,15 @@ validateForSaveCalled = false; } + public void setValidationDelegate(ValidationDelegate validationDelegate) { + this.validationDelegate = validationDelegate; + } + public void validateForSave(ValidationResult validationResult) { validateForSaveCalled = true; + if (validationDelegate != null) { + validationDelegate.validateForSave(this, validationResult); + } super.validateForSave(validationResult); } } Modified: incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/DataContextValidationTst.java URL: http://svn.apache.org/viewvc/incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/DataContextValidationTst.java?view=diff&rev=479872&r1=479871&r2=479872 ============================================================================== --- incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/DataContextValidationTst.java (original) +++ incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/access/DataContextValidationTst.java Mon Nov 27 19:16:44 2006 @@ -57,7 +57,11 @@ package org.objectstyle.cayenne.access; import org.objectstyle.art.Artist; +import org.objectstyle.art.Painting; +import org.objectstyle.cayenne.query.SelectQuery; import org.objectstyle.cayenne.unit.CayenneTestCase; +import org.objectstyle.cayenne.unit.util.ValidationDelegate; +import org.objectstyle.cayenne.validation.ValidationResult; /** * @author Andrus Adamchik @@ -90,5 +94,36 @@ a2.setArtistName("a2"); context.commitChanges(); assertFalse(a2.isValidateForSaveCalled()); + } + + public void testValidationModifyingContext() throws Exception { + deleteTestData(); + + ValidationDelegate delegate = new ValidationDelegate() { + + public void validateForSave(Object object, ValidationResult validationResult) { + + Artist a = (Artist) object; + Painting p = (Painting) a.getObjectContext().newObject(Painting.class); + p.setPaintingTitle("XXX"); + p.setToArtist(a); + } + }; + + DataContext context = createDataContext(); + + context.setValidatingObjectsOnCommit(true); + Artist a1 = (Artist) context.newObject(Artist.class); + a1.setValidationDelegate(delegate); + a1.setArtistName("a1"); + + // add another artist to ensure that modifying context works when more than one + // object is committed + Artist a2 = (Artist) context.newObject(Artist.class); + a2.setValidationDelegate(delegate); + a2.setArtistName("a2"); + context.commitChanges(); + + assertEquals(2, context.performQuery(new SelectQuery(Painting.class)).size()); } } Added: incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/unit/util/ValidationDelegate.java URL: http://svn.apache.org/viewvc/incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/unit/util/ValidationDelegate.java?view=auto&rev=479872 ============================================================================== --- incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/unit/util/ValidationDelegate.java (added) +++ incubator/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/unit/util/ValidationDelegate.java Mon Nov 27 19:16:44 2006 @@ -0,0 +1,63 @@ +/* ==================================================================== + * + * 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.unit.util; + +import org.objectstyle.cayenne.validation.ValidationResult; + +public interface ValidationDelegate { + + void validateForSave(Object object, ValidationResult validationResult); +}