empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1387521 - in /empire-db/trunk: empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/ empire-db/src/main/java/org/apache/empire/data/ empire-db/src/main/java/org/apache/empire/data/bean/ empire-db/src/main/java/org/apache/empire/db/ em...
Date Wed, 19 Sep 2012 10:54:57 GMT
Author: doebele
Date: Wed Sep 19 10:54:57 2012
New Revision: 1387521

URL: http://svn.apache.org/viewvc?rev=1387521&view=rev
Log:
EMPIREDB-157 
core: extend column validation capabilities

Modified:
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/TagEncodingHelper.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Column.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Record.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanProperty.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java
    empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/TagEncodingHelper.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/TagEncodingHelper.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/TagEncodingHelper.java
(original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/TagEncodingHelper.java
Wed Sep 19 10:54:57 2012
@@ -166,8 +166,10 @@ public class TagEncodingHelper implement
         }
 
         @Override
-        public void validate(Object value)
+        public Object validate(Object value)
         {
+            log.warn("validate not supported for {}", expr.getName());
+            return value;
         }
     }
 
@@ -285,8 +287,11 @@ public class TagEncodingHelper implement
                     throw new FieldNotNullException(column);
                 return; // not required
             }
-            // validate through column
-            column.validate(value);
+            // validate through record (if any)
+            if ((getRecord() instanceof Record))
+               ((Record)getRecord()).validateValue(column, value);
+            else
+                column.validate(value);
         }
 
         @Override

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Column.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Column.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Column.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Column.java Wed Sep 19
10:54:57 2012
@@ -62,7 +62,8 @@ public interface Column extends ColumnEx
     /**
      * Checks if the given value is a valid value for this column 
      * If not, an exception is thrown
+     * @return the value the validated and possibly converted value
      */
-    void validate(Object value);
+    Object validate(Object value);
 
 }

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Record.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Record.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Record.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/Record.java Wed Sep 19
10:54:57 2012
@@ -117,6 +117,15 @@ public interface Record extends RecordDa
     void setValue(Column column, Object value);
 
     /**
+     * Validates a value before it is set in the record.
+     * By default, this method simply calls column.validate()
+     * @param column the column
+     * @param value the value to validate
+     * @return the value
+     */
+    Object validateValue(Column column, Object value);
+    
+    /**
      * checks whether or not the field for the given column has been modified since it has
been loaded.
      * @param column the requested column
      * @return Returns true if a column has been modified

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanProperty.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanProperty.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanProperty.java
(original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanProperty.java
Wed Sep 19 10:54:57 2012
@@ -198,10 +198,11 @@ public class BeanProperty implements Col
         return (dataMode==DataMode.NotNull);
     }
 
-    public void validate(Object value)
+    public Object validate(Object value)
     {
         if (ObjectUtils.isEmpty(value) && isRequired())
             throw new FieldNotNullException(this);
+        return value;
     }
 
     // --- others ---

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
(original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
Wed Sep 19 10:54:57 2012
@@ -233,6 +233,14 @@ public class BeanRecordProxy<T> implemen
     }
 
     /**
+     * Validates a value before it is set in the record.
+     */
+    public Object validateValue(Column column, Object value)
+    {
+        return column.validate(value);
+    }
+
+    /**
      * sets the value of a field.
      */
     public void setValue(Column column, Object value)
@@ -246,6 +254,8 @@ public class BeanRecordProxy<T> implemen
                 modified = new boolean[columns.size()]; 
             modified[getFieldIndex(column)] = true;
         }
+        // validate
+        value = validateValue(column, value);
         // Set Value
         setBeanPropertyValue(data, column, value);
     }
@@ -253,7 +263,7 @@ public class BeanRecordProxy<T> implemen
     /**
      * sets the value of a field.
      */
-    public void setValue(int i, Object value)
+    public final void setValue(int i, Object value)
     {
         setValue(getColumn(i), value);
     }

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java Wed Sep 19
10:54:57 2012
@@ -79,6 +79,11 @@ public abstract class DBColumn extends D
     public static final String DBCOLATTR_MAXVALUE  = "maxValue";
     
     /**
+     * Maximum value (Integer)
+     */
+    public static final String DBCOLATTR_DATETIMEPATTERN  = "dateTimePattern";  // default
is "yyyy-MM-dd HH:mm:ss"
+    
+    /**
      * Read only column (Boolean)
      */
     public static final String DBCOLATTR_SINGLEBYTECHARS  = "singleByteChars";
@@ -234,7 +239,7 @@ public abstract class DBColumn extends D
      * Checks if the given value is a valid value for this column 
      * If not, an exception is thrown
      */
-    public abstract void validate(Object value);
+    public abstract Object validate(Object value);
     
     /**
      * @deprecated use validate() instead 

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java Wed Sep
19 10:54:57 2012
@@ -282,12 +282,12 @@ public abstract class DBCommandExpr exte
         }
 
         /**
-         * Not applicable - always returns true.
+         * Not applicable - always return the value.
          */
         @Override
-        public void validate(Object value)
-        {
-            // Nothing to check.
+        public Object validate(Object value)
+        {   // Nothing to check.
+            return value;
         }
 
         /**

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java Wed Sep 19 10:54:57
2012
@@ -121,12 +121,12 @@ public class DBQuery extends DBRowSet
         }
 
         @Override
-        public void validate(Object value)
+        public Object validate(Object value)
         {
             DBColumn column = expr.getUpdateColumn();
             if (column==null)
-                return;
-            column.validate(value);
+                return value;
+            return column.validate(value);
         }
 
         @Override

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecord.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecord.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRecord.java Wed Sep 19
10:54:57 2012
@@ -516,7 +516,7 @@ public class DBRecord extends DBRecordDa
      * @param i index of the column
      * @param value the column value
      */
-    protected void modifyValue(int i, Object value)
+    protected void modifyValue(int i, Object value, boolean fireChangeEvent)
     {	// Check valid
         if (state == State.Invalid)
             throw new ObjectNotValidException(this);
@@ -536,7 +536,8 @@ public class DBRecord extends DBRecordDa
         if (state.isLess(State.Modified))
             changeState(State.Modified);
         // field changed
-        onFieldChanged(i);
+        if (fireChangeEvent)
+            onFieldChanged(i);
     }
 
     /**
@@ -568,9 +569,9 @@ public class DBRecord extends DBRecordDa
         }
         // Is Value valid
         if (validateFieldValues)
-        	validateValue(column, value);
+        	value = validateValue(column, value);
         // Init original values
-        modifyValue(index, value);
+        modifyValue(index, value, true);
     }
 
     /**
@@ -609,14 +610,13 @@ public class DBRecord extends DBRecordDa
 
     /**
      * Validates a value before it is set in the record.
-     * if the value is invalid, the function should call an exception that is derived from
FieldValueException
      * By default, this method simply calls column.validate()
      * @param column the column that needs to be changed
      * @param value the new value
      */
-    protected void validateValue(DBColumn column, Object value)
+    public Object validateValue(Column column, Object value)
     {
-    	column.validate(value);
+    	return column.validate(value);
     }
 
     /**
@@ -959,7 +959,7 @@ public class DBRecord extends DBRecordDa
                 if (value==null)
                     continue;
                 // Modify value
-                modifyValue(i, value);
+                modifyValue(i, value, true);
                 count++;
             }
         }

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java Wed Sep 19 10:54:57
2012
@@ -435,7 +435,7 @@ public class DBTable extends DBRowSet im
             DBTableColumn column = (DBTableColumn)columns.get(i);
             Object value = column.getRecordDefaultValue(conn);
             if (value!=null)
-                rec.modifyValue(i, value); 
+                rec.modifyValue(i, value, true); 
         }
         // Init
         completeInitRecord(rec);

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java Wed Sep
19 10:54:57 2012
@@ -26,6 +26,7 @@ import java.util.Date;
 
 import org.apache.empire.commons.Attributes;
 import org.apache.empire.commons.ObjectUtils;
+import org.apache.empire.commons.StringUtils;
 import org.apache.empire.data.DataMode;
 import org.apache.empire.data.DataType;
 import org.apache.empire.db.exceptions.FieldIllegalValueException;
@@ -314,10 +315,10 @@ public class DBTableColumn extends DBCol
      * @return true if the value is valid or false otherwise.
      */
     @Override
-    public void validate(Object value)
+    public Object validate(Object value)
     {
         // Check for NULL
-        if (isRequired() && (value == null || value.toString().length() < 1))
+        if (isRequired() && ObjectUtils.isEmpty(value))
             throw new FieldNotNullException(this);
         // Is value valid
         switch (type)
@@ -325,21 +326,24 @@ public class DBTableColumn extends DBCol
             case DATE:
             case DATETIME:
                 // Check whether value is a valid date/time value!
-                if (value!=null && (value instanceof Date)==false && value.equals(DBDatabase.SYSDATE)==false)
-                {   try
-                    {   
-                		String val = value.toString();
-                		if (val.length() > 0)
-                		{
-	                		// Parse date time value
-	                        SimpleDateFormat sdFormat = new SimpleDateFormat("dd-MM-yyyy");
-	                        sdFormat.setLenient(true);
-	                        sdFormat.parse(value.toString());
-                		}	                        
+                if (value!=null && !(value instanceof Date) && !DBDatabase.SYSDATE.equals(value))
+                {   // Parse String
+                    String dateValue = value.toString();
+                    if (dateValue.length()==0)
+                        return null;
+                    // Convert through SimpleDateFormat
+                    String datePattern = StringUtils.coalesce(StringUtils.toString(getAttribute(DBCOLATTR_DATETIMEPATTERN)),
"yyyy-MM-dd HH:mm:ss");
+                    if ((type==DataType.DATE || dateValue.length()<=12) && datePattern.indexOf('
')>0)
+                        datePattern = datePattern.substring(0, datePattern.indexOf(' '));
// Strip off time
+                    try
+                    { 	// Parse date time value
+                        SimpleDateFormat sdFormat = new SimpleDateFormat(datePattern);
+                        sdFormat.setLenient(true);
+                        value = sdFormat.parse(dateValue);
                         // OK
                     } catch (ParseException e)
                     {   // Error
-                        log.info("checkValue failed: " + e.toString() + " column=" + getName()
+ " value=" + value);
+                        log.info("Parsing '{}' to Date ("+datePattern+") failed for column
{}. Message is "+e.toString(), value, getName());
                         throw new FieldIllegalValueException(this, String.valueOf(value),
e);
                     }
                 }    
@@ -355,7 +359,7 @@ public class DBTableColumn extends DBCol
                         // throws NumberFormatException if not a number!
                     } catch (NumberFormatException e)
                     {
-                        log.info("checkValue failed: " + e.toString() + " column=" + getName()
+ " value=" + value);
+                        log.info("Parsing '{}' to Decimal failed for column {}. Message is
"+e.toString(), value, getName());
                         throw new FieldIllegalValueException(this, String.valueOf(value),
e);
                     }
                 }
@@ -373,7 +377,7 @@ public class DBTableColumn extends DBCol
                         // throws NumberFormatException if not a number!
                     } catch (NumberFormatException e)
                     {
-                        log.info("checkValue failed: " + e.toString() + " column=" + getName()
+ " value=" + value);
+                        log.info("Parsing '{}' to Double failed for column {}. Message is
"+e.toString(), value, getName());
                         throw new FieldIllegalValueException(this, String.valueOf(value),
e);
                     }
                 }
@@ -391,7 +395,7 @@ public class DBTableColumn extends DBCol
                         // throws NumberFormatException if not an integer!
                     } catch (NumberFormatException e)
                     {
-                        log.info("checkValue failed: " + e.toString() + " column=" + getName()
+ " value=" + String.valueOf(value));
+                        log.info("Parsing '{}' to Integer failed for column {}. Message is
"+e.toString(), value, getName());
                         throw new FieldIllegalValueException(this, String.valueOf(value),
e);
                     }
                 }
@@ -411,22 +415,11 @@ public class DBTableColumn extends DBCol
                 break;
 
         }
+        return value;
     }
     
     protected void validateNumber(DataType type, Number n)
     {
-        if (type==DataType.DECIMAL)
-        {   // Convert to Decimal
-            BigDecimal dv = ObjectUtils.toDecimal(n);
-            int prec = dv.precision();
-            int scale = dv.scale();
-            // check precision and scale
-            double size = getSize();
-            int reqPrec = (int)size;
-            int reqScale =((int)(size*10)-(reqPrec*10));
-            if ((prec-scale)>(reqPrec-reqScale) || scale>reqScale)
-                throw new FieldValueOutOfRangeException(this);
-        }
         // Check Range
         Object min = getAttribute(DBColumn.DBCOLATTR_MINVALUE);
         Object max = getAttribute(DBColumn.DBCOLATTR_MAXVALUE);
@@ -455,7 +448,19 @@ public class DBTableColumn extends DBCol
                 throw new FieldValueOutOfRangeException(this, maxVal, true);
             }
         }
-            
+        // Check overall
+        if (type==DataType.DECIMAL)
+        {   // Convert to Decimal
+            BigDecimal dv = ObjectUtils.toDecimal(n);
+            int prec = dv.precision();
+            int scale = dv.scale();
+            // check precision and scale
+            double size = getSize();
+            int reqPrec = (int)size;
+            int reqScale =((int)(size*10)-(reqPrec*10));
+            if ((prec-scale)>(reqPrec-reqScale) || scale>reqScale)
+                throw new FieldValueOutOfRangeException(this);
+        }
     }
 
     /**

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java Wed Sep 19 10:54:57
2012
@@ -122,11 +122,11 @@ public abstract class DBView extends DBR
         }    
 
         @Override
-        public void validate(Object value)
+        public Object validate(Object value)
         {
             if (updateColumn==null)
-                return;
-            updateColumn.validate(value);
+                return value;
+            return updateColumn.validate(value);
         }
 
         @Override

Modified: empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java?rev=1387521&r1=1387520&r2=1387521&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java (original)
+++ empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java Wed Sep
19 10:54:57 2012
@@ -85,9 +85,9 @@ public class DBCommandTest
         }
 
         @Override
-        public void validate(Object value)
+        public Object validate(Object value)
         {
-            return;
+            return value;
         }
 
         @Override



Mime
View raw message