empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1379382 - in /empire-db/trunk: empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/db/records/ empire-db-struts2/src/main/java/org/apache/empire/struts2/actionsupport/ empire-db/src/main/java/org/apache...
Date Fri, 31 Aug 2012 09:28:27 GMT
Author: doebele
Date: Fri Aug 31 09:28:27 2012
New Revision: 1379382

URL: http://svn.apache.org/viewvc?rev=1379382&view=rev
Log:
EMPIREDB-167
Optimize state changes and provide new overrideables for DBRecord

Modified:
    empire-db/trunk/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/db/records/SampleRecord.java
    empire-db/trunk/empire-db-struts2/src/main/java/org/apache/empire/struts2/actionsupport/RecordActionSupport.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/DBRowSet.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java
    empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java

Modified: empire-db/trunk/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/db/records/SampleRecord.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/db/records/SampleRecord.java?rev=1379382&r1=1379381&r2=1379382&view=diff
==============================================================================
--- empire-db/trunk/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/db/records/SampleRecord.java
(original)
+++ empire-db/trunk/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/db/records/SampleRecord.java
Fri Aug 31 09:28:27 2012
@@ -31,7 +31,7 @@ public abstract class SampleRecord<T ext
 	protected T T; // The table
 
 	public SampleRecord(T table) {
-		super.init(table, DBRecord.State.Invalid, null);
+		super(table);
 		this.T = table;
 	}
 

Modified: empire-db/trunk/empire-db-struts2/src/main/java/org/apache/empire/struts2/actionsupport/RecordActionSupport.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-struts2/src/main/java/org/apache/empire/struts2/actionsupport/RecordActionSupport.java?rev=1379382&r1=1379381&r2=1379382&view=diff
==============================================================================
--- empire-db/trunk/empire-db-struts2/src/main/java/org/apache/empire/struts2/actionsupport/RecordActionSupport.java
(original)
+++ empire-db/trunk/empire-db-struts2/src/main/java/org/apache/empire/struts2/actionsupport/RecordActionSupport.java
Fri Aug 31 09:28:27 2012
@@ -554,7 +554,13 @@ public class RecordActionSupport extends
         }
         else
         {   // No Checking
-            record.modifyValue(i, value);
+        	boolean validate = record.isValidateFieldValues();
+        	try {
+	        	record.setValidateFieldValues(false);
+	            record.setValue(i, value);
+        	} finally {
+	        	record.setValidateFieldValues(validate);
+        	}
             return true;
         }
     }

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=1379382&r1=1379381&r2=1379382&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 Fri Aug
31 09:28:27 2012
@@ -135,7 +135,7 @@ public abstract class DBCommandExpr exte
 
         /** Returns the error message: ERR_NOTSUPPORTED */
         @Override
-        public void initRecord(DBRecord rec, DBRecord.State initialState, Object[] keyValues)
+        public void initRecord(DBRecord rec, Object[] keyValues, boolean insert)
         {
             throw new NotSupportedException(this, "initRecord");
         }

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=1379382&r1=1379381&r2=1379382&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 Fri Aug 31 09:28:27
2012
@@ -343,10 +343,10 @@ public class DBQuery extends DBRowSet
      * @param keyValues an array of the primary key columns
      */
     @Override
-    public void initRecord(DBRecord rec, DBRecord.State initialState, Object[] keyValues)
+    public void initRecord(DBRecord rec, Object[] keyValues, boolean insert)
     {
         // Prepare
-        prepareInitRecord(rec, initialState, keyValues);
+        prepareInitRecord(rec, keyValues, insert);
         // Initialize all Fields
         Object[] fields = rec.getFields();
         for (int i = 0; i < fields.length; i++)
@@ -405,7 +405,7 @@ public class DBQuery extends DBRowSet
             // Read Record
             readRecord(rec, cmd, conn);
             // Set RowSetData
-            rec.changeState(DBRecord.State.Valid, key.clone());
+            rec.updateComplete(key.clone());
         } catch (QueryNoResultException e) {
             // Record not found
             throw new RecordNotFoundException(this, key);
@@ -584,7 +584,7 @@ public class DBQuery extends DBRowSet
             }
         }
         // success
-        rec.changeState(DBRecord.State.Valid, keys);
+        rec.updateComplete(keys);
     }
 
     /**

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=1379382&r1=1379381&r2=1379382&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 Fri Aug 31
09:28:27 2012
@@ -77,6 +77,7 @@ public class DBRecord extends DBRecordDa
     private DBRowSet        rowset;
     private Object[]        fields;
     private boolean[]       modified;
+    private boolean         validateFieldValues;
     // Special Rowset Data (usually null)
     private Object          rowsetData;
 
@@ -93,6 +94,14 @@ public class DBRecord extends DBRecordDa
         fields = null;
         modified = null;
         rowsetData = null;
+        validateFieldValues = true;
+    }
+
+    public DBRecord(DBRowSet initialRowset)
+    {
+    	this();
+    	// allow initial rowset
+    	rowset = initialRowset;
     }
     
     /**
@@ -101,25 +110,39 @@ public class DBRecord extends DBRecordDa
      * @param state the state of the record 
      * @param rowSetData any further RowSet specific data
      */
-    protected void init(DBRowSet rowset, State state, Object rowSetData)
+    protected void init(DBRowSet rowset, Object rowSetData, boolean newRecord)
     {
         // Init
-        if (this.rowset != rowset)
+    	boolean rowsetChanged = (this.rowset != rowset); 
+        if (rowsetChanged)
         {   // Set Rowset
             this.rowset = rowset;
             if (rowset!=null)
                 fields = new Object[rowset.getColumns().size()];
             else
                 fields = null;
-            onRowSetChanged();
         }
         else if (fields!=null)
         {   // clear fields
             for (int i=0; i<fields.length; i++)
-                fields[i]=null;
+                fields[i]=null; // ObjectUtils.NO_VALUE -> works too (difference?);
         }
         // Set State
-        changeState(state, rowSetData);
+        this.rowsetData = rowSetData;
+        this.modified = null;
+        changeState((rowset==null ? State.Invalid : (newRecord ? State.New : State.Valid)));
+        // notify
+        if (rowsetChanged)
+        	onRowSetChanged();
+    }
+
+    /**
+     * changes the state of the record
+     * @param newState
+     */
+    protected void changeState(State newState)
+    {
+    	this.state = newState;
     }
     
     /**
@@ -131,18 +154,6 @@ public class DBRecord extends DBRecordDa
     {
         return fields;
     }
-    
-    /**
-     * This method is used internally be the RowSet to change the record's state<BR>
-     * @param state
-     * @param rowSetData
-     */
-    protected void changeState(State state, Object rowSetData)
-    {
-        this.state = state;
-        this.rowsetData = rowSetData;
-        this.modified = null;
-    }
 
     /**
      * Closes the record by releasing all resources and resetting the record's state to invalid.
@@ -150,7 +161,7 @@ public class DBRecord extends DBRecordDa
     @Override
     public void close()
     {
-        init(null, State.Invalid, null);
+        init(null, null, false);
     }
     
     /** {@inheritDoc} */
@@ -160,13 +171,14 @@ public class DBRecord extends DBRecordDa
         try 
         {
             DBRecord rec = (DBRecord)super.clone();
-            rec.rowset = rowset;
-            rec.state = state;
+            rec.rowset = this.rowset;
+            rec.state = this.state;
             if (rec.fields == fields && fields!=null)
                 rec.fields = fields.clone();
             if (rec.modified == modified && modified!=null)
                 rec.modified = modified.clone();
-            rec.rowsetData = rowsetData;
+            rec.rowsetData = this.rowsetData;
+            rec.validateFieldValues = this.validateFieldValues;
             return rec;
             
         } catch (CloneNotSupportedException e)
@@ -389,7 +401,7 @@ public class DBRecord extends DBRecordDa
             modified[index] = isModified;
         // Set State to modified, if not already at least modified and isModified is set
to true
         if (state.isLess(State.Modified) && isModified)
-            state = State.Modified;
+            changeState(State.Modified);
         // Reset state to unmodified, if currently modified and not modified anymore after
the change
         if (state == State.Modified && !isModified)
         {
@@ -403,12 +415,12 @@ public class DBRecord extends DBRecordDa
             }
             if (recordNotModified)
             {
-            	state = State.Valid;
+            	changeState(State.Valid);
             }
         }
     }
-    
-    /**
+
+	/**
      * returns an array of key columns which uniquely identify the record.
      * @return the array of key columns if any
      */
@@ -496,7 +508,7 @@ public class DBRecord extends DBRecordDa
      * @param i index of the column
      * @param value the column value
      */
-    public void modifyValue(int i, Object value)
+    protected void modifyValue(int i, Object value)
     {	// Check valid
         if (state == State.Invalid)
             throw new ObjectNotValidException(this);
@@ -514,7 +526,7 @@ public class DBRecord extends DBRecordDa
         fields[i] = value;
         // Set State
         if (state.isLess(State.Modified))
-            state = State.Modified;
+            changeState(State.Modified);
         // field changed
         onFieldChanged(i);
     }
@@ -541,16 +553,14 @@ public class DBRecord extends DBRecordDa
             return; // no change
         // Field has changed
         DBColumn column = rowset.getColumn(index);
-        if (column.isAutoGenerated())
+        // Check whether we can change this field
+        if (!allowFieldChange(column))
         {   // Read Only column may be set
             throw new FieldIsReadOnlyException(column);
         }
-        if (state!=State.New && rowset.isKeyColumn(column))
-        {   // Key columns cannot be changed
-            throw new FieldIsReadOnlyException(column);
-        }
         // Is Value valid
-        column.validate(value);
+        if (validateFieldValues)
+        	validateValue(column, value);
         // Init original values
         modifyValue(index, value);
     }
@@ -572,6 +582,53 @@ public class DBRecord extends DBRecordDa
     }
     
     /**
+     * Checks whether or not this field can be changed at all.
+     * Note: This is not equivalent to isFieldReadOnly() 
+     * @param column the column that needs to be changed
+     * @return true if it is possible to change this field for this record context
+     */
+    protected boolean allowFieldChange(DBColumn column)
+    {
+        if (column.isAutoGenerated())
+        	return false;
+        if (state!=State.New && rowset.isKeyColumn(column))
+        	return false;
+        return true;
+    }
+
+    /**
+     * 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)
+    {
+    	column.validate(value);
+    }
+
+    /**
+     * Returns whether or not values are checked for validity when calling setValue().
+     * If set to true validateValue() is called to check validity
+     * @return true if the validity of values is checked or false otherwise
+     */
+    public boolean isValidateFieldValues() 
+    {
+		return validateFieldValues;
+	}
+
+    /**
+     * Set whether or not values are checked for validity when calling setValue().
+     * If set to true validateValue() is called to check validity, otherwise not.
+     * @param validateFieldValues flag whether to check validity
+     */
+	public void setValidateFieldValues(boolean validateFieldValues) 
+	{
+		this.validateFieldValues = validateFieldValues;
+	}
+    
+    /**
      * returns whether a field is visible to the client or not
      * <P>
      * May be overridden to implement context specific logic.
@@ -632,9 +689,8 @@ public class DBRecord extends DBRecordDa
      */
      
     public void init(DBRowSet table, Object[] keyValues, boolean insert)
-    { // Init with keys
-    	State initialState = (insert ? State.New : State.Valid);  
-        table.initRecord(this, initialState, keyValues);
+    { 	// Init with keys
+        table.initRecord(this, keyValues, insert);
     }
     
     /**
@@ -708,11 +764,25 @@ public class DBRecord extends DBRecordDa
      */
     public void update(Connection conn)
     {
-        if (rowset == null)
+        if (!isValid())
             throw new ObjectNotValidException(this);
+        if (!isModified())
+        	return; /* Not modified. Nothing to do! */
         // update
         rowset.updateRecord(this, conn);
     }
+    
+    /**
+     * This method is used internally to indicate that the record update has completed<BR>
+     * This will set change the record's state to Valid
+     * @param rowSetData additional data held by the rowset for this record (optional)
+     */
+    protected void updateComplete(Object rowSetData)
+    {
+        this.rowsetData = rowSetData;
+        this.modified = null;
+        changeState(State.Valid);
+    }
 
     /**
      * This helper function calls the DBRowset.deleteRecord method 

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java?rev=1379382&r1=1379381&r2=1379382&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java Fri Aug 31
09:28:27 2012
@@ -468,14 +468,14 @@ public abstract class DBRowSet extends D
      * @param rec the DBRecord object to initialize this DBRowSet object
      * @param state the state of this DBRowSet object
      */
-    protected void prepareInitRecord(DBRecord rec, DBRecord.State state, Object rowSetData)
+    protected void prepareInitRecord(DBRecord rec, Object rowSetData, boolean insert)
     {
         if (rec==null)
             throw new InvalidArgumentException("rec", rec);
         if (columns.size() < 1)
             throw new ObjectNotValidException(this);
         // Init
-        rec.init(this, state, rowSetData);
+        rec.init(this, rowSetData, insert);
     }
 
     /**
@@ -485,10 +485,10 @@ public abstract class DBRowSet extends D
      * @param rec the Record object
      * @param keyValues an array of the primary key columns
      */
-    public void initRecord(DBRecord rec, DBRecord.State initialState, Object[] keyValues)
+    public void initRecord(DBRecord rec, Object[] keyValues, boolean insert)
     {
         // Prepare
-        prepareInitRecord(rec, initialState, null);
+        prepareInitRecord(rec, null, insert);
         // Initialize all Fields
         Object[] fields = rec.getFields();
         for (int i = 0; i < fields.length; i++)
@@ -521,7 +521,7 @@ public abstract class DBRowSet extends D
     public void initRecord(DBRecord rec, DBRecordData recData)
     {
         // Initialize the record
-        prepareInitRecord(rec, DBRecord.State.Valid, null);
+        prepareInitRecord(rec, null, false);
         // Get Record Field Values
         Object[] fields = rec.getFields();
         for (int i = 0; i < fields.length; i++)
@@ -767,7 +767,7 @@ public abstract class DBRowSet extends D
             	boolean empty = ObjectUtils.isEmpty(value); 
                 DBTableColumn col = (DBTableColumn) columns.get(i);
                 if (primaryKey.contains(col))
-                { // Check for Modification
+                { 	// Check for Modification
                     if (modified == true)
                     { // Requires a primary key
                         log.warn("updateRecord: " + name + " primary has been modified!");
@@ -791,7 +791,7 @@ public abstract class DBRowSet extends D
                     cmd.set(col.to(timestamp));
                 } 
                 else if (modified && value!=ObjectUtils.NO_VALUE)
-                { // Update a field
+                { 	// Update a field
                     if (col.isReadOnly())
                         log.warn("updateRecord: Read-only column '" + col.getName() + " has
been modified!");
                     // Check the value
@@ -836,7 +836,7 @@ public abstract class DBRowSet extends D
                 fields[i] = timestamp;
         }
         // Change State
-        rec.changeState(DBRecord.State.Valid, null);
+        rec.updateComplete(rec.getRowSetData());
     }
     
     /**

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=1379382&r1=1379381&r2=1379382&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 Fri Aug 31 09:28:27
2012
@@ -427,7 +427,7 @@ public class DBTable extends DBRowSet im
     public void createRecord(DBRecord rec, Connection conn)
     {
         // Prepare
-        prepareInitRecord(rec, DBRecord.State.New, null);
+        prepareInitRecord(rec, null, true);
         // Set Defaults
         int count = columns.size();
         for (int i = 0; i < count; i++)

Modified: empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java?rev=1379382&r1=1379381&r2=1379382&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java (original)
+++ empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/SerializeTest.java Fri Aug
31 09:28:27 2012
@@ -57,7 +57,7 @@ public class SerializeTest {
   public DBObject[] getObjectsToSerialize() {
     final TestDatabase db = new TestDatabase();
     DBRecord rec = new DBRecord();
-    rec.init(db.T_TEST, DBRecord.State.New, null);
+    rec.init(db.T_TEST, null, true);
     db.open(new MockDriver(), null);
     return new DBObject[] {
             db,



Mime
View raw message