Object validation classes

From: Fabricio Voznika (fabricio.voznik..artmouth.EDU)
Date: Thu Sep 11 2003 - 16:49:10 EDT

  • Next message: Mike Kienenberger: "Patch for RFE 803827: Log Oracle sequence SQL"

    Hi all,

        I've implemented the object validation objects. I'm sending the new
    classes in 2 tars:

    src.tar.gz - new classes, all in ...cayenne.access.validation package.
    tests.tar.gz - just 1 class with junit tests.

        The file diff.txt contains the result of a diff -ru for the modified
    classes. I diffed it against 9/11/2003 nightly build due to the already
    mentioned problems with CVS.
        I'm haven't done any documentation yet, but I plan to do so. I would
    appreciate if someone could have a look and validate what I did. There
    are some TODO tags to move methods to an Utility class, I would
    appreciate if someone could tell me where to put them.

    Thanks,
    Fabricio.

    Only in .: diff.txt
    diff -ru ./org/objectstyle/cayenne/CayenneDataObject.java /home/fabricio/projects/cayenne/src/cayenne/org/objectstyle/cayenne/CayenneDataObject.java
    --- ./org/objectstyle/cayenne/CayenneDataObject.java Thu Sep 11 06:18:37 2003
    +++ /home/fabricio/projects/cayenne/src/cayenne/org/objectstyle/cayenne/CayenneDataObject.java Thu Sep 11 16:34:10 2003
    ..-69,6 +69,7 @@
     import org.apache.log4j.Logger;
     import org.objectstyle.cayenne.access.DataContext;
     import org.objectstyle.cayenne.access.EntityResolver;
    +import org.objectstyle.cayenne.access.validation.ValidationResult;
     import org.objectstyle.cayenne.access.util.QueryUtils;
     import org.objectstyle.cayenne.map.ObjEntity;
     import org.objectstyle.cayenne.map.ObjRelationship;
    ..-544,4 +545,25 @@
             // dataContext will be set *IFF* the datacontext it came from is also
             // deserialized. Setting of datacontext is handled by the datacontext itself
         }
    +
    +
    + public void validateForSave(ValidationResult res) {
    +// Do nothing.
    + }
    +
    +
    + public void validateForInsert(ValidationResult res) {
    + // Do nothing.
    + }
    +
    +
    + public void validateForUpdate(ValidationResult res) {
    + // Do nothing.
    + }
    +
    +
    + public void validateForDelete(ValidationResult res) {
    + // Do nothing.
    + }
    +
     }
    diff -ru ./org/objectstyle/cayenne/DataObject.java /home/fabricio/projects/cayenne/src/cayenne/org/objectstyle/cayenne/DataObject.java
    --- ./org/objectstyle/cayenne/DataObject.java Thu Sep 11 06:18:37 2003
    +++ /home/fabricio/projects/cayenne/src/cayenne/org/objectstyle/cayenne/DataObject.java Thu Sep 11 16:38:18 2003
    ..-58,6 +58,7 @@
     import java.util.Map;
     
     import org.objectstyle.cayenne.access.DataContext;
    +import org.objectstyle.cayenne.access.validation.ValidationResult;
     
     /**
      * Defines basic methods for a persistent object in Cayenne.
    ..-127,4 +128,30 @@
          * was read from the database.
          */
         public void fetchFinished();
    +
    +
    + /**
    + * Usage example:<br>
    +* <pre>
    +* super.validate(res);
    +* Validator.checkMandatory(this, "name", res);
    +* Validator.checkMandatory(this, "fooBar", res);
    +* boolean b = Validator.checkMandatory(this, "age", res);
    +* if(b && this.getAge().intValue() < 18)) {
    +* res.addFailure(this, "age", "You must be older than 18 to enter this site "); // :-)
    +* }
    +* </pre>
    + *..aram res
    + */
    + void validateForSave(ValidationResult res);
    +
    +
    + void validateForInsert(ValidationResult res);
    +
    +
    + void validateForUpdate(ValidationResult res);
    +
    +
    + void validateForDelete(ValidationResult res);
    +
     }
    diff -ru ./org/objectstyle/cayenne/access/DataContext.java /home/fabricio/projects/cayenne/src/cayenne/org/objectstyle/cayenne/access/DataContext.java
    --- ./org/objectstyle/cayenne/access/DataContext.java Thu Sep 11 06:18:37 2003
    +++ /home/fabricio/projects/cayenne/src/cayenne/org/objectstyle/cayenne/access/DataContext.java Thu Sep 11 16:14:13 2003
    ..-84,6 +84,7 @@
     import org.objectstyle.cayenne.access.util.QueryUtils;
     import org.objectstyle.cayenne.access.util.RelationshipDataSource;
     import org.objectstyle.cayenne.access.util.SelectObserver;
    +import org.objectstyle.cayenne.access.validation.*;
     import org.objectstyle.cayenne.conf.Configuration;
     import org.objectstyle.cayenne.dba.PkGenerator;
     import org.objectstyle.cayenne.event.EventManager;
    ..-728,6 +729,29 @@
                     return;
                 }
     
    + ValidationResult res = new ValidationResult();
    + for(Iterator it = this.objectStore.getObjectIterator(); it.hasNext();) {
    + DataObject dataObject = (DataObject)it.next();
    + switch(dataObject.getPersistenceState()) {
    + case PersistenceState.NEW:
    + dataObject.validateForSave(res);
    + dataObject.validateForInsert(res);
    + break;
    + case PersistenceState.MODIFIED:
    + dataObject.validateForSave(res);
    + dataObject.validateForUpdate(res);
    + break;
    + case PersistenceState.DELETED:
    + dataObject.validateForSave(res);
    + dataObject.validateForDelete(res);
    + break;
    + }
    + }
    +
    + if(res.hasFailure()) {
    + throw new ValidationException(res);
    + }
    +
                 ContextCommit worker = new ContextCommit(this);
     
                 try {
    Only in /home/fabricio/projects/cayenne/src/cayenne/org/objectstyle/cayenne/access: validation







    This archive was generated by hypermail 2.0.0 : Thu Sep 11 2003 - 16:48:01 EDT