public abstract class DataObject extends java.lang.Object implements ContextIndexed, Identified, Releasable
This class
is sub-classed to create objects corresponding to particular tables.
Essentially this class is implemented as a wrapper round the
Repository.Record
class. Where Repository.Record
accesses
database fields as named properties a DataObject
subclass will typically
provide conventional get and set methods.
Sub-classes should implement these methods by calling
record.setProperty
/record.get(Type)Property
.
The primary constructor is one that takes a Record
Unless the objects are only to be created from the appropriate factory
sub-classes need constructors that create new records or
retrieve existing ones. DataObject
contains several protected static methods
returning Record
that can be used to implement these.
In most cases there is a one to one correspondance between a Java class and a SQL table. In these cases the table name can be hardwired into the constructors. In a few cases we may want multiple tables to map to the same class. In this case the constructors that specify the table name must be used or the objects should only be created via a factory.
Though it is possible to refer to DataObjects via their integer handle this should be done sparingly.
Any method call that takes an integer handle as a parameter is intrinsically less type safe then one that takes a
reference to the appropriate object. The IndexedReference
class exists to implement type-safe lightweight references to
objects (like DataObject
) that implement Indexed
Most sub-classes will also override the getIdentifier()
method. This generates
a unique text identifier for the DataObject
. This is used in pull down menus etc.
so typically should reflect the end users concept of an objects name.
Modifier and Type | Field and Description |
---|---|
protected static boolean |
DEBUG |
Repository.Record |
record
All the fields for this record.
|
MAX_IDENTIFIER
Modifier | Constructor and Description |
---|---|
protected |
DataObject(Repository.Record r)
create an object using an existing Record
this is for use within the Factory class when a factory may return different
sub-types.
|
Modifier and Type | Method and Description |
---|---|
static java.util.Map<java.lang.String,java.lang.Object> |
addToMap(java.util.Map<java.lang.String,java.lang.Object> table,
java.util.Map<java.lang.String,java.lang.Object> values)
Utility routine to merge 2 Hashtables
|
static java.util.Map<java.lang.String,java.lang.Object> |
addToMap(java.util.Map<java.lang.String,java.lang.Object> table,
java.lang.Object[][] values)
A utility method for putting values into hashtables in the constructors
|
static java.util.Map<java.lang.String,java.lang.Object> |
addToMap(java.util.Map<java.lang.String,java.lang.Object> table,
java.lang.String name,
java.lang.Object value)
A utility method for putting values into hashtables in the constructors
only modifies table if key not already in use.
|
boolean |
commit()
Writes any changed values back to the database.
|
boolean |
delete()
Used to remove the data from the database - USE WITH CAUTION!
In particular deleting records from within an iterator may
throw off record chunking.
|
static boolean |
empty(java.lang.String input) |
boolean |
equals(java.lang.Object o) |
void |
formUpdate(Form f)
update this DataObject based on Form fields.
|
AppContext |
getContext()
Get the application context used to create this object.
|
protected DatabaseService |
getDatabaseService() |
protected java.lang.String |
getFactoryTag() |
int |
getID()
Returns the unique ID for this particular object.
|
java.lang.String |
getIdentifier()
produce a unique identifying string for this object for use in forms and
pull-down menus and charts.
|
java.lang.String |
getIdentifier(int max_length)
Generate Identifier with advisory max length.
|
protected Logger |
getLogger() |
java.util.Map<java.lang.String,java.lang.Object> |
getMap()
create Hashtable of contents of Object
|
java.util.Map<java.lang.String,java.lang.Object> |
getMap(boolean include_null) |
static <D extends DataObject> |
getOwningFactory(D obj)
A static method to locate the
DataObjectFactory used to create a DataObject
This will be necessary when behaviour added as Composite s is required but
only the DataObject is available. |
protected static Repository.Record |
getRecord(AppContext ctx,
java.lang.String table)
Helper method for subclass constructors.
|
protected static Repository.Record |
getRecord(AppContext c,
java.lang.String table,
int id) |
protected static Repository.Record |
getRecord(AppContext ctx,
java.lang.String table,
java.util.Map<java.lang.String,java.lang.Object> values)
Helper method for use in Subclass constructors.
|
protected SQLContext |
getSQLContext() |
protected java.lang.String |
getUniqueIdName()
Returns the field name for the unique ID field.
|
int |
hashCode() |
protected boolean |
isLocked() |
protected void |
lock()
Lock record so it is unmodifiable.
|
protected void |
post_commit(boolean changed)
Extension point called at end of commit
|
protected void |
postCreate()
extension point to allow sub-classes to add additional setup after an
object has been created from a creation form.
|
protected void |
pre_commit(boolean dirty)
Extension point called at start of commit.
|
void |
release()
This is used like a destructor - means less connections, etc lying around
This will also clear the record from the
Repository cache if this is enabled |
void |
setContents(java.util.Map<java.lang.String,java.lang.Object> m)
update multiple fields at once using a Map
|
protected void |
setDirty(java.lang.String key,
boolean value) |
void |
setMap(java.util.Map<java.lang.String,java.lang.Object> h)
Use Hashtable to update Object.
|
java.lang.String |
toString() |
protected static final boolean DEBUG
public Repository.Record record
protected DataObject(Repository.Record r)
r
- protected static Repository.Record getRecord(AppContext ctx, java.lang.String table) throws ConsistencyError
ctx
- table
- ConsistencyError
protected static Repository.Record getRecord(AppContext ctx, java.lang.String table, java.util.Map<java.lang.String,java.lang.Object> values) throws ConsistencyError, DataFault
ctx
- values
- ConsistencyError
DataFault
protected static Repository.Record getRecord(AppContext c, java.lang.String table, int id) throws DataException
DataException
public final boolean commit() throws DataFault
DataFault
protected void pre_commit(boolean dirty) throws DataFault
dirty
- is record known to be modified.DataFault
protected void post_commit(boolean changed) throws DataFault
changed
- record was changed by commit.DataFault
public boolean delete() throws DataFault
Removable
and be removed from iterator (which will recursively call delete).true
if the object was successfully deleted.DataFault
public final boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public void formUpdate(Form f)
f
- Form to useprotected final SQLContext getSQLContext() throws java.sql.SQLException
java.sql.SQLException
protected final DatabaseService getDatabaseService()
public final AppContext getContext()
getContext
in interface Contexed
public final java.util.Map<java.lang.String,java.lang.Object> getMap()
public final java.util.Map<java.lang.String,java.lang.Object> getMap(boolean include_null)
public final int getID()
public java.lang.String getIdentifier(int max_length)
Identified
getIdentifier
in interface Identified
max_length
- advisory max lengthpublic final java.lang.String getIdentifier()
Identified
getIdentifier
in interface Identified
protected Logger getLogger()
protected final java.lang.String getFactoryTag()
protected final java.lang.String getUniqueIdName()
public final int hashCode()
hashCode
in class java.lang.Object
protected final void postCreate()
public void release()
Repository
cache if this is enabledrelease
in interface Releasable
protected void lock() throws DataFault
DataObject
s are being cached to reduce riskDataFault
protected boolean isLocked()
public final void setContents(java.util.Map<java.lang.String,java.lang.Object> m)
m
- Map containing updateprotected void setDirty(java.lang.String key, boolean value)
public java.lang.String toString()
toString
in class java.lang.Object
public final void setMap(java.util.Map<java.lang.String,java.lang.Object> h)
h
- public static java.util.Map<java.lang.String,java.lang.Object> addToMap(java.util.Map<java.lang.String,java.lang.Object> table, java.util.Map<java.lang.String,java.lang.Object> values)
table
- Primary table to be modifiedvalues
- additional table to be merged values here never overwritepublic static java.util.Map<java.lang.String,java.lang.Object> addToMap(java.util.Map<java.lang.String,java.lang.Object> table, java.lang.Object[][] values)
table
- values
- array of key value pairs to be added if not already definedpublic static java.util.Map<java.lang.String,java.lang.Object> addToMap(java.util.Map<java.lang.String,java.lang.Object> table, java.lang.String name, java.lang.Object value)
table
- original Hashtablename
- key to usevalue
- addedpublic static boolean empty(java.lang.String input)
public static <D extends DataObject> DataObjectFactory<D> getOwningFactory(D obj)
DataObjectFactory
used to create a DataObject
This will be necessary when behaviour added as Composite
s is required but
only the DataObject
is available.conn
- obj
-