This is an automated email from the ASF dual-hosted git repository.
doebele pushed a commit to branch version3
in repository https://gitbox.apache.org/repos/asf/empire-db.git
The following commit(s) were added to refs/heads/version3 by this push:
new 211b5a0 EMPIREDB-362 cleanup
211b5a0 is described below
commit 211b5a0c368969b94382442c8cf9c1540e331d6a
Author: Rainer Döbele <[email protected]>
AuthorDate: Fri Jan 21 20:12:15 2022 +0100
EMPIREDB-362
cleanup
---
.../org/apache/empire/samples/db/SampleApp.java | 17 +-
.../jsf2/pageelements/RecordPageElement.java | 4 +-
.../empire/jsf2/utils/TagEncodingHelper.java | 4 +-
.../main/java/org/apache/empire/data/Record.java | 6 +
.../apache/empire/data/bean/BeanRecordProxy.java | 3 +-
.../main/java/org/apache/empire/db/DBReader.java | 8 +-
.../main/java/org/apache/empire/db/DBRecord.java | 575 ++++++++++-----------
.../java/org/apache/empire/db/DBRecordData.java | 116 ++---
8 files changed, 360 insertions(+), 373 deletions(-)
diff --git
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
index 726344a..b637730 100644
---
a/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
+++
b/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
@@ -132,12 +132,14 @@ public class SampleApp
int idEmp2 = insertEmployee("Fred", "Bloggs", Gender.M,
idDevDep);
int idEmp3 = insertEmployee("Emma", "White", Gender.F,
idSalDep);
- // commit
+ // commit
context.commit();
+ /*
int idEmp = testTransactionCreate(idDevDep);
testTransactionUpdate(idEmp);
testTransactionDelete(idEmp);
+ */
// STEP 7: Update Records (by setting the phone Number)
System.out.println("*** Step 7: updateEmployee() ***");
@@ -355,9 +357,11 @@ public class SampleApp
*/
private static void updatePartialRecord(int idEmp, String phoneNumber)
{
- // Update an Employee
- DBRecord rec = new DBRecord(context, db.EMPLOYEES);
- db.EMPLOYEES.readRecord(rec, DBRecord.key(idEmp), PartialMode.INCLUDE,
db.EMPLOYEES.PHONE_NUMBER);
+ SampleDB.Employees E = db.EMPLOYEES;
+ // Update an Employee with partial record
+ // this will only load the EMPLOYEE_ID and the PHONE_NUMBER
+ DBRecord rec = new DBRecord(context, E);
+ E.readRecord(rec, DBRecord.key(idEmp), PartialMode.INCLUDE,
E.PHONE_NUMBER);
// Set
rec.setValue(db.EMPLOYEES.PHONE_NUMBER, phoneNumber);
rec.update();
@@ -372,14 +376,15 @@ public class SampleApp
{
SampleDB.Employees E = db.EMPLOYEES;
SampleDB.Departments D = db.DEPARTMENTS;
-
+
+ // Create DBQuery from command
DBCommand cmd = db.createCommand();
cmd.select(E.getColumns());
cmd.select(D.getColumns());
cmd.join(E.DEPARTMENT_ID, D.DEPARTMENT_ID);
+ DBQuery query = new DBQuery(cmd, E.EMPLOYEE_ID);
// Make employee Head of Department and update salary
- DBQuery query = new DBQuery(cmd, E.EMPLOYEE_ID);
DBRecord rec = new DBRecord(context, query);
rec.read(idEmp);
rec.setValue(E.SALARY, salary);
diff --git
a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/RecordPageElement.java
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/RecordPageElement.java
index d874c44..be51615 100644
---
a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/RecordPageElement.java
+++
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/RecordPageElement.java
@@ -86,7 +86,7 @@ public class RecordPageElement<T extends DBRecord> extends
PageElement
if (!record.isValid())
return null;
// idParam
- Object[] key = record.getKeyValues();
+ Object[] key = record.getKey();
return getPage().getIdParamForKey(rowset, key);
}
@@ -203,7 +203,7 @@ public class RecordPageElement<T extends DBRecord> extends
PageElement
record.update();
// Put key on Session
this.removeSessionObject(DBRecord.class);
- this.setSessionObject(Object[].class, record.getKeyValues());
+ this.setSessionObject(Object[].class, record.getKey());
return true;
// OK
} catch(Exception e) {
diff --git
a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/TagEncodingHelper.java
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/TagEncodingHelper.java
index 335ae7c..70823ac 100644
---
a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/TagEncodingHelper.java
+++
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/TagEncodingHelper.java
@@ -541,8 +541,8 @@ public class TagEncodingHelper implements NamingContainer
{ // Debug output
if ((rec instanceof DBRecord) && (this.record instanceof
DBRecord))
{ // a database record change
- String keyOld =
StringUtils.toString(((DBRecord)this.record).getKeyValues());
- String keyNew =
StringUtils.toString(((DBRecord)rec).getKeyValues());
+ String keyOld =
StringUtils.toString(((DBRecord)this.record).getKey());
+ String keyNew =
StringUtils.toString(((DBRecord)rec).getKey());
String rowSet =
StringUtils.valueOf(((DBRecord)rec).getRowSet().getName());
log.trace("Changing
"+component.getClass().getSimpleName()+" record of rowset "+rowSet+" from {} to
{}", keyOld, keyNew);
}
diff --git a/empire-db/src/main/java/org/apache/empire/data/Record.java
b/empire-db/src/main/java/org/apache/empire/data/Record.java
index c64a8fa..196cdf1 100644
--- a/empire-db/src/main/java/org/apache/empire/data/Record.java
+++ b/empire-db/src/main/java/org/apache/empire/data/Record.java
@@ -71,6 +71,12 @@ public interface Record extends RecordData
* @return the array of key columns if any
*/
Column[] getKeyColumns();
+
+ /**
+ * returns an array of key values which uniquely identify the record.
+ * @return the key value array
+ */
+ Object[] getKey();
/**
* returns true if the field is visible to the client.
diff --git
a/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
b/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
index b301a6c..5858622 100644
--- a/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
+++ b/empire-db/src/main/java/org/apache/empire/data/bean/BeanRecordProxy.java
@@ -116,7 +116,8 @@ public class BeanRecordProxy<T> implements Record
* Returns the array of primary key columns.
* @return the array of primary key columns
*/
- public Object[] getKeyValues()
+ @Override
+ public Object[] getKey()
{
if (keyColumns==null)
return null;
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBReader.java
b/empire-db/src/main/java/org/apache/empire/db/DBReader.java
index 96a44ce..63dcff1 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBReader.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBReader.java
@@ -748,7 +748,7 @@ public class DBReader extends DBRecordData implements
DBContextAware
* @return the number of column descriptions added to the Element
*/
@Override
- public int addColumnDesc(Element parent)
+ public int getXmlMeta(Element parent)
{
if (colList == null)
throw new ObjectNotValidException(this);
@@ -766,7 +766,7 @@ public class DBReader extends DBRecordData implements
DBContextAware
* @return the number of row values added to the element
*/
@Override
- public int addRowValues(Element parent)
+ public int getXmlData(Element parent)
{
if (rset == null)
throw new ObjectNotValidException(this);
@@ -806,7 +806,7 @@ public class DBReader extends DBRecordData implements
DBContextAware
String rowElementName = getXmlDictionary().getRowElementName();
while (moveNext())
{
- addRowValues(XMLUtil.addElement(parent, rowElementName));
+ getXmlData(XMLUtil.addElement(parent, rowElementName));
count++;
}
return count;
@@ -835,7 +835,7 @@ public class DBReader extends DBRecordData implements
DBContextAware
String rowsetElementName = getXmlDictionary().getRowSetElementName();
Element root = XMLUtil.createDocument(rowsetElementName);
// Add Field Description
- addColumnDesc(root);
+ getXmlMeta(root);
// Add row rset
addRows(root);
// return Document
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
b/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
index 3e4f183..acc324b 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
@@ -56,6 +56,8 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
{
private final static long serialVersionUID = 1L;
+ private static final Logger log = LoggerFactory.getLogger(DBRecord.class);
+
/**
* DBRecordRollbackHandler
* @author doebele
@@ -70,7 +72,7 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
private final State state; /* the original state */
private Object[] fields;
private boolean[] modified;
- private Object rowSetData;
+ private Object rowsetData;
public DBRecordRollbackHandler(DBRecord record)
{
@@ -82,7 +84,7 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
this.state = record.state;
this.modified = copy(record.modified);
this.fields = copy(record.fields);
- this.rowSetData = record.rowSetData;
+ this.rowsetData = record.rowsetData;
}
@Override
@@ -98,7 +100,7 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
throw new InvalidArgumentException("successor", successor);
// combine now
DBRecordRollbackHandler s = (DBRecordRollbackHandler)successor;
- log.info("combining rollback state for record {}/{}",
record.getRowSet().getName(), StringUtils.arrayToString(record.getKeyValues(),
"|"));
+ log.info("combining rollback state for record {}/{}",
record.getRowSet().getName(), StringUtils.arrayToString(record.getKey(), "|"));
if (s.modified==null)
{
return; // not modified!
@@ -126,9 +128,9 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
record.state = this.state;
record.fields = this.fields;
record.modified = this.modified;
- record.rowSetData = rowSetData;
+ record.rowsetData = rowsetData;
// done
- log.info("Rollback for record {}[{}] performed",
record.getRowSet().getName(), StringUtils.arrayToString(record.getKeyValues(),
"|"));
+ log.info("Rollback for record {}[{}] performed",
record.getRowSet().getName(), StringUtils.arrayToString(record.getKey(), "|"));
}
@Override
@@ -191,8 +193,6 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
{
return parts;
}
-
- protected static final Logger log =
LoggerFactory.getLogger(DBRecord.class);
// the context
protected final DBContext context;
@@ -203,34 +203,16 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
private Object[] fields;
private boolean[] modified;
// Special Rowset Data (usually null)
- private Object rowSetData;
+ private Object rowsetData;
// options
private boolean validateFieldValues;
/**
- * Create a new DBRecord object.<BR>
- * The record is not attached to a RowSet and the record's state is
initially set to REC_INVALID.
- *
- * Please derive your own Objects from this class.
- protected DBRecord()
- {
- state = State.Invalid;
- rowset = null;
- fields = null;
- modified = null;
- rowSetData = null;
- validateFieldValues = true;
- }
-
- protected DBRecord(DBRowSet initialRowset)
- {
- this();
- // allow initial rowset
- rowset = initialRowset;
- }
+ * Constructs a new DBRecord.<BR>
+ * @param context the DBContext for this record
+ * @param rowset the corresponding RowSet(Table, View, Query, etc.)
*/
-
public DBRecord(DBContext context, DBRowSet rowset)
{
// Check params
@@ -242,67 +224,9 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
this.state = State.Invalid;
this.fields = null;
this.modified = null;
- this.rowSetData = null;
+ this.rowsetData = null;
this.validateFieldValues = true;
}
-
- /**
- * This method is used internally by the RowSet to initialize the record's
properties
- * @param rowset the rowset to which to attach this record
- * @param rowSetData any further RowSet specific data
- * @param newRecord
- */
- protected void initData(Object rowSetData, boolean newRecord)
- {
- // Init rowset
- int colCount = rowset.getColumns().size();
- if (fields==null || fields.length!=colCount)
- fields = new Object[colCount];
- else
- { // clear fields
- for (int i=0; i<fields.length; i++)
- if (fields[i]!=ObjectUtils.NO_VALUE)
- fields[i]=null;
- }
- // Set State
- this.rowSetData = rowSetData;
- this.modified = null;
- changeState((rowset==null ? State.Invalid : (newRecord ? State.New :
State.Valid)));
- }
-
- /**
- * 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)
- {
- // change rowSetData
- if (rowSetData!=ObjectUtils.NO_VALUE)
- this.rowSetData = rowSetData;
- // Change state
- this.modified = null;
- changeState(State.Valid);
- }
-
- /**
- * changes the state of the record
- * @param newState
- */
- protected void changeState(State newState)
- {
- this.state = newState;
- }
-
- /**
- * This function provides direct access to the record fields.<BR>
- * This method is used internally be the RowSet to fill the data.<BR>
- * @return an array of field values
- */
- protected Object[] getFields()
- {
- return fields;
- }
/**
* Closes the record by releasing all resources and resetting the record's
state to invalid.
@@ -313,7 +237,7 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
// clear fields
fields = null;
modified = null;
- rowSetData = null;
+ rowsetData = null;
// change state
if (state!=State.Invalid)
changeState(State.Invalid);
@@ -335,7 +259,7 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
rec.fields = fields.clone();
if (rec.modified == modified && modified!=null)
rec.modified = modified.clone();
- rec.rowSetData = this.rowSetData;
+ rec.rowsetData = this.rowsetData;
rec.validateFieldValues = this.validateFieldValues;
return rec;
@@ -380,16 +304,6 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
}
/**
- * Returns the DBRowSet object.
- *
- * @return the DBRowSet object
- */
- public Object getRowSetData()
- {
- return rowSetData;
- }
-
- /**
* Returns the record state.
*
* @return the record state
@@ -536,28 +450,6 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
}
/**
- * helper function to check if a given field index corresponds to one of
the given columns
- * @param index the field index
- * @param column one or more columns to check
- * @return true if the index is for one of the columns or false otherwise
- */
- protected boolean isColumn(int index, DBColumn... column)
- {
- if (index < 0 || index >= fields.length)
- throw new InvalidArgumentException("index", index);
- if (column==null)
- throw new InvalidArgumentException("column", column);
- Column col = getColumn(index);
- for (int i=0; i<column.length; i++)
- { // compare
- if (col==column[i])
- return true;
- }
- // not found
- return false;
- }
-
- /**
* Returns true if the field was modified.
*
* @param index the field index
@@ -602,48 +494,6 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
return false;
}
- /**
- * Sets the modified state of a column.<BR>
- * This will force the field to be updated in the database, if set to
TRUE.
- *
- * @param column the column
- * @param isModified modified or not
- */
- public void setModified(DBColumn column, boolean isModified)
- { // Check valid
- if (state == State.Invalid)
- throw new ObjectNotValidException(this);
- // Check modified
- if (modified == null)
- { // Init array
- modified = new boolean[fields.length];
- for (int j = 0; j < fields.length; j++)
- modified[j] = false;
- }
- int index = getFieldIndex(column);
- if (index >= 0)
- 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)
- changeState(State.Modified);
- // Reset state to unmodified, if currently modified and not modified
anymore after the change
- if (state == State.Modified && !isModified)
- {
- boolean recordNotModified = true;
- for (int j = 0; j < fields.length; j++)
- {
- if (modified[j] == true)
- {
- recordNotModified = false;
- }
- }
- if (recordNotModified)
- {
- changeState(State.Valid);
- }
- }
- }
-
/**
* returns an array of key columns which uniquely identify the record.
* @return the array of key columns if any
@@ -658,6 +508,16 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
* Returns the array of primary key columns.
* @return the array of primary key columns
*/
+ public Object[] getKey()
+ {
+ return ((rowset != null) ? rowset.getRecordKey(this) : null);
+ }
+
+ /**
+ * Returns the array of primary key columns.
+ * @Deprecated use getKey() instead
+ */
+ @Deprecated
public Object[] getKeyValues()
{
return ((rowset != null) ? rowset.getRecordKey(this) : null);
@@ -726,36 +586,6 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
{
return getFieldOptions((DBColumn)column);
}
-
- /**
- * Modifies a column value bypassing all checks made by setValue.
- * Use this to explicitly set invalid values i.e. for temporary storage.
- *
- * @param index index of the column
- * @param value the column value
- */
- protected void modifyValue(int index, Object value, boolean
fireChangeEvent)
- { // Check valid
- if (state == State.Invalid)
- throw new ObjectNotValidException(this);
- if (index < 0 || index >= fields.length)
- throw new InvalidArgumentException("index", index);
- // modified state array
- if (modified == null)
- { modified = new boolean[fields.length];
- for (int j = 0; j < fields.length; j++)
- modified[j] = false;
- }
- // set value and modified
- fields[index] = value;
- modified[index] = true;
- // set record state
- if (state.isLess(State.Modified))
- changeState(State.Modified);
- // field changed event
- if (fireChangeEvent)
- onFieldChanged(index);
- }
/**
* validates all modified values of a record
@@ -847,24 +677,6 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
// Get Column Index
setValue(getFieldIndex(column), value);
}
-
- /**
- * 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)
- {
- // Check auto generated
- if (column.isAutoGenerated() && (!isNew() || !isNull(column)))
- return false;
- // Check key Column
- if (!isNew() && rowset.isKeyColumn(column))
- return false;
- // done
- return true;
- }
/**
* Validates a value before it is set in the record.
@@ -971,17 +783,9 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
/**
* Creates a new record
*/
- public void create(boolean deferredInit)
- {
- rowset.createRecord(this, deferredInit);
- }
-
- /**
- * Creates a new record
- */
public void create()
{
- create(false);
+ rowset.createRecord(this, false);
}
/**
@@ -1051,15 +855,6 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
}
close();
}
-
- /**
- * Factory function to create createRollbackHandler();
- * @return the DBRollbackHandler
- */
- protected DBRollbackHandler createRollbackHandler()
- {
- return new DBRecordRollbackHandler(this);
- }
/**
* This function set the field descriptions to the the XML tag.
@@ -1067,7 +862,7 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
* @return the number of column descriptions added to the element
*/
@Override
- public int addColumnDesc(Element parent)
+ public int getXmlMeta(Element parent)
{
if (!isValid())
throw new ObjectNotValidException(this);
@@ -1092,7 +887,7 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
* @return the number of row values added to the element
*/
@Override
- public int addRowValues(Element parent)
+ public int getXmlData(Element parent)
{
if (!isValid())
throw new ObjectNotValidException(this);
@@ -1136,15 +931,6 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
}
return count;
}
-
- /**
- * returns the DBXmlDictionary that should used to generate
XMLDocuments<BR>
- * @return the DBXmlDictionary
- */
- protected DBXmlDictionary getXmlDictionary()
- {
- return DBXmlDictionary.getInstance();
- }
/**
* Returns a XML document with the field description an values of this
record.
@@ -1162,13 +948,200 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
if (rowset.getName() != null)
root.setAttribute("name", rowset.getName());
// Add Field Description
- if (addColumnDesc(root)>0)
+ if (getXmlMeta(root)>0)
{ // Add row Values
- addRowValues(XMLUtil.addElement(root, xmlDic.getRowElementName()));
+ getXmlData(XMLUtil.addElement(root, xmlDic.getRowElementName()));
}
// return Document
return root.getOwnerDocument();
}
+
+ /**
+ * Sets record values from the supplied java bean.
+ *
+ * @return true if at least one value has been set successfully
+ */
+ @Override
+ public int setRecordValues(Object bean, Collection<Column> ignoreList)
+ {
+ // Add all Columns
+ int count = 0;
+ for (int i = 0; i < getFieldCount(); i++)
+ { // Check Property
+ DBColumn column = getDBColumn(i);
+ if (column.isReadOnly())
+ continue;
+ if (ignoreList != null && ignoreList.contains(column))
+ continue; // ignore this property
+ // Get Property Name
+ String property = column.getBeanPropertyName();
+ setRecordValue(column, bean, property);
+ count++;
+ }
+ return count;
+ }
+
+ /**
+ * Sets record values from the suppied java bean.
+ * @return true if at least one value has been set sucessfully
+ */
+ @Override
+ public final int setRecordValues(Object bean)
+ {
+ return setRecordValues(bean, null);
+ }
+
+ /**
+ * Compares the record to another one
+ * @param otherObject
+ * @return true if it is the same record (but maybe a different instance)
+ */
+ public boolean isSame(DBRecord other)
+ { // check valid
+ if (!isValid() || !other.isValid())
+ return false;
+ // compare table
+ if (!rowset.isSame(other.getRowSet()))
+ return false;
+ // compare key
+ Object[] key1 = getKey();
+ Object[] key2 = other.getKey();
+ return ObjectUtils.compareEqual(key1, key2);
+ }
+
+ /**
+ * Returns the DBRowSet object.
+ *
+ * @return the DBRowSet object
+ */
+ protected Object getRowSetData()
+ {
+ return rowsetData;
+ }
+
+ /**
+ * This function provides direct access to the record fields.<BR>
+ * This method is used internally be the RowSet to fill the data.<BR>
+ * @return an array of field values
+ */
+ protected Object[] getFields()
+ {
+ return fields;
+ }
+
+ /**
+ * returns the DBXmlDictionary that should used to generate
XMLDocuments<BR>
+ * @return the DBXmlDictionary
+ */
+ protected DBXmlDictionary getXmlDictionary()
+ {
+ return DBXmlDictionary.getInstance();
+ }
+
+ /**
+ * changes the state of the record
+ * @param newState
+ */
+ protected void changeState(State newState)
+ {
+ this.state = newState;
+ }
+
+ /**
+ * Factory function to create createRollbackHandler();
+ * @return the DBRollbackHandler
+ */
+ protected DBRollbackHandler createRollbackHandler()
+ {
+ return new DBRecordRollbackHandler(this);
+ }
+
+ /**
+ * This method is used internally by the RowSet to initialize the record's
properties
+ * @param rowset the rowset to which to attach this record
+ * @param rowsetData any further RowSet specific data
+ * @param newRecord
+ */
+ protected void initData(Object rowsetData, boolean newRecord)
+ {
+ // Init rowset
+ int colCount = rowset.getColumns().size();
+ if (fields==null || fields.length!=colCount)
+ fields = new Object[colCount];
+ else
+ { // clear fields
+ for (int i=0; i<fields.length; i++)
+ if (fields[i]!=ObjectUtils.NO_VALUE)
+ fields[i]=null;
+ }
+ // Set State
+ this.rowsetData = rowsetData;
+ this.modified = null;
+ changeState((rowset==null ? State.Invalid : (newRecord ? State.New :
State.Valid)));
+ }
+
+ /**
+ * 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)
+ {
+ // change rowsetData
+ if (rowsetData!=ObjectUtils.NO_VALUE)
+ this.rowsetData = rowsetData;
+ // Change state
+ this.modified = null;
+ changeState(State.Valid);
+ }
+
+ /**
+ * 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)
+ {
+ // Check auto generated
+ if (column.isAutoGenerated() && (!isNew() || !isNull(column)))
+ return false;
+ // Check key Column
+ if (!isNew() && rowset.isKeyColumn(column))
+ return false;
+ // done
+ return true;
+ }
+
+ /**
+ * Modifies a column value bypassing all checks made by setValue.
+ * Use this to explicitly set invalid values i.e. for temporary storage.
+ *
+ * @param index index of the column
+ * @param value the column value
+ */
+ protected void modifyValue(int index, Object value, boolean
fireChangeEvent)
+ { // Check valid
+ if (state == State.Invalid)
+ throw new ObjectNotValidException(this);
+ if (index < 0 || index >= fields.length)
+ throw new InvalidArgumentException("index", index);
+ // modified state array
+ if (modified == null)
+ { modified = new boolean[fields.length];
+ for (int j = 0; j < fields.length; j++)
+ modified[j] = false;
+ }
+ // set value and modified
+ fields[index] = value;
+ modified[index] = true;
+ // set record state
+ if (state.isLess(State.Modified))
+ changeState(State.Modified);
+ // field changed event
+ if (fireChangeEvent)
+ onFieldChanged(index);
+ }
/**
* Set the record default value for the fields with
@@ -1178,10 +1151,9 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
*
* @return the number of fields set to default
*/
- public int fillMissingDefaults(boolean allowConnect)
+ protected int fillMissingDefaults(Connection conn)
{
int count = 0;
- Connection conn = (allowConnect ? context.getConnection() : null);
for (int i = 0; i < fields.length; i++)
{
if (fields[i] == ObjectUtils.NO_VALUE)
@@ -1197,7 +1169,49 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
}
return count;
}
-
+
+ /*
+ * Sets the modified state of a column.<BR>
+ * This will force the field to be updated in the database, if set to TRUE.
+ *
+ * @param column the column
+ * @param isModified modified or not
+ *
+ protected void setModified(DBColumn column, boolean isModified)
+ { // Check valid
+ if (state == State.Invalid)
+ throw new ObjectNotValidException(this);
+ // Check modified
+ if (modified == null)
+ { // Init array
+ modified = new boolean[fields.length];
+ for (int j = 0; j < fields.length; j++)
+ modified[j] = false;
+ }
+ int index = getFieldIndex(column);
+ if (index >= 0)
+ 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)
+ changeState(State.Modified);
+ // Reset state to unmodified, if currently modified and not modified
anymore after the change
+ if (state == State.Modified && !isModified)
+ {
+ boolean recordNotModified = true;
+ for (int j = 0; j < fields.length; j++)
+ {
+ if (modified[j] == true)
+ {
+ recordNotModified = false;
+ }
+ }
+ if (recordNotModified)
+ {
+ changeState(State.Valid);
+ }
+ }
+ }
+ */
/**
* set a record value from a particular bean property.
@@ -1214,14 +1228,12 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
if (StringUtils.isEmpty(property))
property = column.getBeanPropertyName();
try
- {
- // Get Property Value
+ { // Get Property Value
PropertyUtilsBean pub =
BeanUtilsBean.getInstance().getPropertyUtils();
Object value = pub.getSimpleProperty(bean, property);
-
// Now, set the record value
setValue( column, value );
-
+ // done
} catch (IllegalAccessException e)
{ log.error(bean.getClass().getName() + ": unable to get property '"
+ property + "'");
throw new BeanPropertyGetException(bean, property, e);
@@ -1235,56 +1247,25 @@ public class DBRecord extends DBRecordData implements
DBContextAware, Record, Cl
}
/**
- * Sets record values from the supplied java bean.
- *
- * @return true if at least one value has been set successfully
+ * helper function to check if a given field index corresponds to one of
the given columns
+ * @param index the field index
+ * @param column one or more columns to check
+ * @return true if the index is for one of the columns or false otherwise
*/
- @Override
- public int setRecordValues(Object bean, Collection<Column> ignoreList)
+ protected boolean isColumn(int index, DBColumn... column)
{
- // Add all Columns
- int count = 0;
- for (int i = 0; i < getFieldCount(); i++)
- { // Check Property
- DBColumn column = getDBColumn(i);
- if (column.isReadOnly())
- continue;
- if (ignoreList != null && ignoreList.contains(column))
- continue; // ignore this property
- // Get Property Name
- String property = column.getBeanPropertyName();
- setRecordValue(column, bean, property);
- count++;
+ if (index < 0 || index >= fields.length)
+ throw new InvalidArgumentException("index", index);
+ if (column==null)
+ throw new InvalidArgumentException("column", column);
+ Column col = getColumn(index);
+ for (int i=0; i<column.length; i++)
+ { // compare
+ if (col==column[i])
+ return true;
}
- return count;
- }
-
- /**
- * Sets record values from the suppied java bean.
- * @return true if at least one value has been set sucessfully
- */
- @Override
- public final int setRecordValues(Object bean)
- {
- return setRecordValues(bean, null);
- }
-
- /**
- * Compares the record to another one
- * @param otherObject
- * @return true if it is the same record (but maybe a different instance)
- */
- public boolean isSame(DBRecord other)
- { // check valid
- if (!isValid() || !other.isValid())
- return false;
- // compare table
- if (!rowset.isSame(other.getRowSet()))
- return false;
- // compare key
- Object[] key1 = getKeyValues();
- Object[] key2 = other.getKeyValues();
- return ObjectUtils.compareEqual(key1, key2);
+ // not found
+ return false;
}
/**
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
b/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
index 1ec4623..80c3b73 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
@@ -49,33 +49,38 @@ import org.w3c.dom.Element;
public abstract class DBRecordData extends DBObject
implements RecordData
{
- private final static long serialVersionUID = 1L;
-
+ private final static long serialVersionUID = 1L;
+
// Logger
- private static final Logger log =
LoggerFactory.getLogger(DBRecordData.class);
-
+ private static final Logger log =
LoggerFactory.getLogger(DBRecordData.class);
+
// Field Info
@Override
- public abstract int getFieldCount();
+ public abstract int getFieldCount();
+
@Override
- public abstract int getFieldIndex(ColumnExpr column);
+ public abstract int getFieldIndex(ColumnExpr column);
+
@Override
- public abstract int getFieldIndex(String column);
+ public abstract int getFieldIndex(String column);
+
// Column lookup
@Override
public abstract ColumnExpr getColumnExpr(int i);
+
// xml
- public abstract int addColumnDesc(Element parent);
- public abstract int addRowValues (Element parent);
+ public abstract int getXmlMeta(Element parent);
+ public abstract int getXmlData(Element parent);
public abstract Document getXmlDocument();
+
// others
- public abstract void close();
+ public abstract void close();
/**
* Returns a value based on an index.
*/
@Override
- public abstract Object getValue(int index);
+ public abstract Object getValue(int index);
/**
* Returns a data value for the desired column .
@@ -93,6 +98,28 @@ public abstract class DBRecordData extends DBObject
}
/**
+ * Returns the value of a field as an object of a given (wrapper)type
+ * @param index index of the field
+ * @param returnType the type of the returned value
+ * @return the value
+ */
+ public final <T> T getValue(int index, Class<T> returnType)
+ {
+ return ObjectUtils.convert(returnType, getValue(index));
+ }
+
+ /**
+ * Returns the value of a field as an object of a given (wrapper)type
+ * @param column the column for which to retrieve the value
+ * @param returnType the type of the returned value
+ * @return the value
+ */
+ public final <T> T getValue(Column column, Class<T> returnType)
+ {
+ return ObjectUtils.convert(returnType, getValue(column));
+ }
+
+ /**
* Returns an array of values for the given column expressions
*
* @param column the column expressions
@@ -118,11 +145,9 @@ public abstract class DBRecordData extends DBObject
* @param index index of the column
* @return the record value
*/
- public int getInt(int index)
+ public final int getInt(int index)
{
- // Get Integer value
- Object value = getValue(index);
- return ObjectUtils.getInteger(value);
+ return ObjectUtils.getInteger(getValue(index));
}
/**
@@ -144,11 +169,9 @@ public abstract class DBRecordData extends DBObject
* @param index index of the column
* @return the value
*/
- public long getLong(int index)
+ public final long getLong(int index)
{
- // Get Integer value
- Object value = getValue(index);
- return ObjectUtils.getLong(value);
+ return ObjectUtils.getLong(getValue(index));
}
/**
@@ -170,11 +193,9 @@ public abstract class DBRecordData extends DBObject
* @param index index of the column
* @return the value
*/
- public double getDouble(int index)
+ public final double getDouble(int index)
{
- // Get Double value
- Object value = getValue(index);
- return ObjectUtils.getDouble(value);
+ return ObjectUtils.getDouble(getValue(index));
}
/**
@@ -196,11 +217,9 @@ public abstract class DBRecordData extends DBObject
* @param index index of the column
* @return the value
*/
- public BigDecimal getDecimal(int index)
+ public final BigDecimal getDecimal(int index)
{
- // Get Double value
- Object value = getValue(index);
- return ObjectUtils.getDecimal(value);
+ return ObjectUtils.getDecimal(getValue(index));
}
/**
@@ -222,11 +241,9 @@ public abstract class DBRecordData extends DBObject
* @param index index of the column
* @return the value
*/
- public boolean getBoolean(int index)
+ public final boolean getBoolean(int index)
{
- // Get Boolean value
- Object value = getValue(index);
- return ObjectUtils.getBoolean(value);
+ return ObjectUtils.getBoolean(getValue(index));
}
/**
@@ -237,7 +254,9 @@ public abstract class DBRecordData extends DBObject
* @return the value
*/
public final boolean getBoolean(ColumnExpr column)
- { return getBoolean(getFieldIndex(column)); }
+ {
+ return getBoolean(getFieldIndex(column));
+ }
/**
* Returns a data value identified by the column index.
@@ -246,11 +265,9 @@ public abstract class DBRecordData extends DBObject
* @param index index of the column
* @return the value
*/
- public String getString(int index)
+ public final String getString(int index)
{
- // Get Integer value
- Object value = getValue(index);
- return StringUtils.toString(value);
+ return StringUtils.toString(getValue(index));
}
/**
@@ -272,11 +289,9 @@ public abstract class DBRecordData extends DBObject
* @param index index of the column
* @return the value
*/
- public Date getDateTime(int index)
+ public final Date getDateTime(int index)
{
- // Get DateTime value
- Object value = getValue(index);
- return ObjectUtils.getDate(value);
+ return ObjectUtils.getDate(getValue(index));
}
/**
@@ -291,7 +306,6 @@ public abstract class DBRecordData extends DBObject
return getDateTime(getFieldIndex(column));
}
-
/**
* Returns the value of a field as an enum
* For numeric columns the value is assumed to be an ordinal of the
enumeration item
@@ -349,26 +363,6 @@ public abstract class DBRecordData extends DBObject
}
return getEnum(getFieldIndex(column), (Class<T>)enumType);
}
-
- /**
- * Returns the value of a field as an object of a given (wrapper)type
- * @param index index of the field
- * @return the value
- */
- public final <T> T getAs(int index, Class<T> wrapperType)
- {
- return ObjectUtils.convert(wrapperType, getValue(index));
- }
-
- /**
- * Returns the value of a field as an object of a given (wrapper)type
- * @param column the column for which to retrieve the value
- * @return the value
- */
- public final <T> T getAs(Column column, Class<T> wrapperType)
- {
- return ObjectUtils.convert(wrapperType, getValue(column));
- }
/**
* Checks whether or not the value for the given column is null.