Changes for object validation

From: Fabricio Voznika (fabricio.voznik..artmouth.EDU)
Date: Mon Sep 22 2003 - 14:56:45 EDT

  • Next message: Mike Kienenberger: "Re: Implementing Entity "restricting qualifier" in application?"

    Hi,

        Here are the classes for the validation. Tarballs contain new files
    and diff.txt contains the changes in the source files diff against
    nightly build 09/11/2003. Sorry for the changes in imports, my IntelliJ
    is configured to add * when more than 1 class is added and it messed up
    with some imports of yours.

    Cheers,
    Fabricio.

    Only in .: diff.txt
    diff -ru /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/CayenneDataObject.java ./org/objectstyle/cayenne/CayenneDataObject.java
    --- /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/CayenneDataObject.java Thu Sep 11 06:18:37 2003
    +++ ./org/objectstyle/cayenne/CayenneDataObject.java Mon Sep 22 12:48:17 2003
    ..-59,19 +59,16 @@
     import java.io.ObjectInputStream;
     import java.io.ObjectOutputStream;
     import java.lang.reflect.InvocationTargetException;
    -import java.util.HashMap;
    -import java.util.Iterator;
    -import java.util.List;
    -import java.util.Map;
    -import java.util.StringTokenizer;
    +import java.util.*;
     
     import org.apache.commons.beanutils.BeanUtils;
     import org.apache.log4j.Logger;
     import org.objectstyle.cayenne.access.DataContext;
     import org.objectstyle.cayenne.access.EntityResolver;
    +import org.objectstyle.cayenne.access.types.*;
    +import org.objectstyle.cayenne.validation.*;
     import org.objectstyle.cayenne.access.util.QueryUtils;
    -import org.objectstyle.cayenne.map.ObjEntity;
    -import org.objectstyle.cayenne.map.ObjRelationship;
    +import org.objectstyle.cayenne.map.*;
     import org.objectstyle.cayenne.query.SelectQuery;
     
     /**
    ..-544,4 +541,42 @@
             // 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) {
    + ObjEntity objEntity = this.dataContext.getEntityResolver().lookupObjEntity(this);
    + //TODO: Is there an easier way to get it?
    + ExtendedTypeMap extTypes = this.dataContext.dataNodeForObjEntity(objEntity).getAdapter().getExtendedTypes();
    + Class myClazz = this.getClass();
    +
    + Collection attrs = objEntity.getAttributes();
    + for(Iterator it = attrs.iterator(); it.hasNext();) {
    + ObjAttribute objAttr = (ObjAttribute)it.next();
    + DbAttribute dbAttr = objAttr.getDbAttribute();
    + Object value = this.readPropertyDirectly(objAttr.getName());
    + if(dbAttr.isMandatory()) {
    + if(!Validator.checkMandatory(this, value, objAttr.getName(), res)) {
    + continue;
    + }
    + }
    +
    + extTypes.getRegisteredType(myClazz).validate(this, value, objAttr, dbAttr, res);
    + }
    + }
    +
    +
    + public void validateForInsert(ValidationResult res) {
    + // Do nothing.
    + }
    +
    +
    + public void validateForUpdate(ValidationResult res) {
    + // Do nothing.
    + }
    +
    +
    + public void validateForDelete(ValidationResult res) {
    + // Do nothing.
    + }
    +
     }
    diff -ru /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/DataObject.java ./org/objectstyle/cayenne/DataObject.java
    --- /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/DataObject.java Thu Sep 11 06:18:37 2003
    +++ ./org/objectstyle/cayenne/DataObject.java Mon Sep 15 11:04:54 2003
    ..-58,6 +58,7 @@
     import java.util.Map;
     
     import org.objectstyle.cayenne.access.DataContext;
    +import org.objectstyle.cayenne.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 /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/DataContext.java ./org/objectstyle/cayenne/access/DataContext.java
    --- /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/DataContext.java Thu Sep 11 06:18:37 2003
    +++ ./org/objectstyle/cayenne/access/DataContext.java Mon Sep 15 15:44:46 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.validation.*;
     import org.objectstyle.cayenne.conf.Configuration;
     import org.objectstyle.cayenne.dba.PkGenerator;
     import org.objectstyle.cayenne.event.EventManager;
    ..-714,6 +715,7 @@
          *
          *..aram logLevel if logLevel is higher or equals to the level
          * set for QueryLogger, statements execution will be logged.
    + *..hrows ValidationException if any of the new, modified or deleted objects has a validation failure.
          */
         public void commitChanges(Level logLevel) throws CayenneRuntimeException {
     
    ..-728,6 +730,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 {
    diff -ru /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/AbstractType.java ./org/objectstyle/cayenne/access/types/AbstractType.java
    --- /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/AbstractType.java Thu Sep 11 06:18:37 2003
    +++ ./org/objectstyle/cayenne/access/types/AbstractType.java Mon Sep 22 12:23:34 2003
    ..-55,6 +55,9 @@
      */
     package org.objectstyle.cayenne.access.types;
     
    +import org.objectstyle.cayenne.map.*;
    +import org.objectstyle.cayenne.validation.ValidationResult;
    +
     import java.sql.PreparedStatement;
     
     /**
    ..-88,4 +91,9 @@
                 .append(getClassName());
             return buf.toString();
         }
    +
    +
    + public boolean validate(Object source, Object value, ObjAttribute objAttr, DbAttribute dbAttr, ValidationResult res) {
    + return true;
    + }
     }
    diff -ru /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/ByteArrayType.java ./org/objectstyle/cayenne/access/types/ByteArrayType.java
    --- /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/ByteArrayType.java Thu Sep 11 06:18:37 2003
    +++ ./org/objectstyle/cayenne/access/types/ByteArrayType.java Mon Sep 22 12:56:59 2003
    ..-67,6 +67,8 @@
     import java.sql.Types;
     
     import org.objectstyle.cayenne.CayenneException;
    +import org.objectstyle.cayenne.map.*;
    +import org.objectstyle.cayenne.validation.*;
     
     /**
      *..uthor Andrei Adamchik
    ..-155,6 +157,7 @@
                     return bytes;
             }
     
    +
             public void setJdbcObject(
                     PreparedStatement st,
                     Object val,
    ..-172,6 +175,18 @@
                     }
             }
     
    +
    + public boolean validate(Object source, Object value, ObjAttribute objAttr, DbAttribute dbAttr, ValidationResult res) {
    + byte[] val = (byte[])value;
    + if(val != null && val.length > dbAttr.getMaxLength()) {
    + String name = objAttr.getName();
    + res.addFailure(source, name, Validator.createMessage(name, " exceeds maximum length ("+dbAttr.getMaxLength()+")."));
    + return false;
    + }
    + return true;
    + }
    +
    +
             protected byte[] readBlob(Blob blob) throws IOException, SQLException {
                     if (blob == null) {
                             return null;
    ..-188,7 +203,7 @@
                     if(size == 0) {
                             return EMPTY_BYTES;
                     }
    -
    +
                     int bufSize = (size < BUF_SIZE) ? size : BUF_SIZE;
                     InputStream in = blob.getBinaryStream();
                     return (in != null)
    ..-247,4 +262,5 @@
             public void setTrimmingBytes(boolean trimingBytes) {
                     this.trimmingBytes = trimingBytes;
             }
    +
     }
    diff -ru /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/CharType.java ./org/objectstyle/cayenne/access/types/CharType.java
    --- /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/CharType.java Thu Sep 11 06:18:37 2003
    +++ ./org/objectstyle/cayenne/access/types/CharType.java Mon Sep 22 12:56:59 2003
    ..-69,6 +69,8 @@
     
     import org.apache.log4j.Logger;
     import org.objectstyle.cayenne.CayenneException;
    +import org.objectstyle.cayenne.validation.*;
    +import org.objectstyle.cayenne.map.*;
     
     /**
      * Handles CHAR type for JDBC drivers that don't trim trailing spaces.
    ..-160,6 +162,18 @@
             }
         }
     
    +
    + public boolean validate(Object source, Object value, ObjAttribute objAttr, DbAttribute dbAttr, ValidationResult res) {
    + String val = (String)value;
    + if(val != null && val.length() > dbAttr.getMaxLength()) {
    + String name = objAttr.getName();
    + res.addFailure(source, name, Validator.createMessage(name, " exceeds maximum length ("+dbAttr.getMaxLength()+")."));
    + return false;
    + }
    + return true;
    + }
    +
    +
         protected String readClob(Clob clob) throws IOException, SQLException {
             if (clob == null) {
                 return null;
    diff -ru /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/DefaultType.java ./org/objectstyle/cayenne/access/types/DefaultType.java
    --- /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/DefaultType.java Thu Sep 11 06:18:37 2003
    +++ ./org/objectstyle/cayenne/access/types/DefaultType.java Mon Sep 22 12:55:17 2003
    ..-231,4 +231,5 @@
                 procReadMethod.invoke(st, new Object[] { new Integer(index)});
             return (st.wasNull()) ? null : val;
         }
    +
     }
    diff -ru /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/ExtendedType.java ./org/objectstyle/cayenne/access/types/ExtendedType.java
    --- /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/ExtendedType.java Thu Sep 11 06:18:37 2003
    +++ ./org/objectstyle/cayenne/access/types/ExtendedType.java Mon Sep 22 12:23:34 2003
    ..-55,6 +55,9 @@
      */
     package org.objectstyle.cayenne.access.types;
     
    +import org.objectstyle.cayenne.validation.ValidationResult;
    +import org.objectstyle.cayenne.map.*;
    +
     import java.sql.CallableStatement;
     import java.sql.PreparedStatement;
     import java.sql.ResultSet;
    ..-101,4 +104,7 @@
          */
         public Object materializeObject(CallableStatement rs, int index, int type)
             throws Exception;
    +
    +
    + public boolean validate(Object source, Object value, ObjAttribute objAttr, DbAttribute dbAttr, ValidationResult res);
     }
    diff -ru /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/UtilDateType.java ./org/objectstyle/cayenne/access/types/UtilDateType.java
    --- /home/fabricio/projects/cayenne-1.0-dev/src/cayenne/org/objectstyle/cayenne/access/types/UtilDateType.java Thu Sep 11 06:18:37 2003
    +++ ./org/objectstyle/cayenne/access/types/UtilDateType.java Mon Sep 22 11:44:02 2003
    ..-127,4 +127,5 @@
                 type,
                 precision);
         }
    +
     }
    \ No newline at end of file
    Only in ./org/objectstyle/cayenne: validation







    This archive was generated by hypermail 2.0.0 : Mon Sep 22 2003 - 14:54:24 EDT