📄 abstractdomain.java
字号:
// Create columns for the subtype join table
Iterator iterator = i_subtypeColumnSpecs.iterator();
AbstractColumnSpec colSpec = null; // AbstractColumnSpec is intentional
while (iterator.hasNext())
{ // only AbstractColumnSpec and subclasses have buildJoinColumn()
colSpec = (AbstractColumnSpec) iterator.next();
i_subtypeTable.addJoinColumn(colSpec.buildJoinColumn());
}
}
public void setTableAlias(String alias)
{
i_tableAlias = alias;
}
public String getTableAlias()
{
return i_tableAlias;
}
public DatabasePolicy getDatabasePolicy()
{
if (i_databasePolicy == null)
{
this.setDatabasePolicy(JRFProperties.getDatabasePolicy());
}
if (i_databasePolicy == null)
{
throw new DatabaseException(
"Database policy is not set for " + this.getClass());
}
return i_databasePolicy;
}
public void setDatabasePolicy(DatabasePolicy dbPolicy)
{
i_databasePolicy = dbPolicy;
}
/**
* Return a JDBCHelper instance from the pool or return the default
* instance.
*
* @return a value of type 'JDBCHelper'
*/
protected JDBCHelper getJDBCHelper()
{
if (i_jdbcHelper != null && !i_jdbcHelper.isInPool())
{
if (LOG.isDebugEnabled()) // this avoids StringBuffer creation
{
LOG.debug(
"AbstractDomain#getJDBCHelper() -- "
+ "returning existing JDBCHelper");
}
return i_jdbcHelper;
}
if (this.getJDBCHelperPool() != null)
{
try
{
LOG.debug(
"AbstractDomain#getJDBCHelper() -- "
+ "returning a JDBCHelper from the pool.");
this.setJDBCHelper(i_jdbcHelperPool.getJDBCHelper());
}
catch (SQLException e)
{
// Since we don't expect this to happen under normal
// circumstances, convert it to a DatabaseException and rethrow.
// DatabaseException constructor will log the error.
throw new DatabaseException(e);
}
}
else if (i_jdbcHelper == null)
{
LOG.debug(
"AbstractDomain#getJDBCHelper() -- "
+ "creating a new JDBCHelper instance.");
this.setJDBCHelper(JDBCHelperFactory.create());
}
return i_jdbcHelper;
}
/**
* Set the default JDBCHelper instance.
*
* @param aJDBCHelper a value of type 'JDBCHelper'
*/
public void setJDBCHelper(JDBCHelper aJDBCHelper)
{
aJDBCHelper.setShouldAutoCommit(i_shouldAutoCommit); // framework controls committing
i_jdbcHelper = aJDBCHelper;
}
public JDBCHelperPool getJDBCHelperPool()
{
if (i_jdbcHelperPool == null && i_jdbcHelperPoolName != null)
{
i_jdbcHelperPool = JDBCHelperPool.getPool(i_jdbcHelperPoolName);
}
return i_jdbcHelperPool;
}
/**
* @param aJDBCHelperPool a value of type 'JDBCHelperPool'
*/
public void setJDBCHelperPool(JDBCHelperPool aJDBCHelperPool)
{
if (aJDBCHelperPool == null)
{
throw new IllegalArgumentException(
"AbstractDomain#setJDBCHelperPool(...) was passed a null "
+ "method parameter.");
}
i_jdbcHelperPool = aJDBCHelperPool;
}
/**
* @param aJDBCHelperPool a value of type 'JDBCHelperPool'
*/
public void setJDBCHelperPoolName(String poolName)
{
i_jdbcHelperPoolName = poolName;
}
public void addColumnSpec(ColumnSpec aColumnSpec)
{
i_columnSpecs.add(aColumnSpec);
}
public List getColumnSpecs()
{
// This list is cloned to protect someone from changing it inadvertently
return (List) i_columnSpecs.clone();
}
/** Cannot add CompoundPrimaryKeyColumnSpec using this */
public void addSubtypeColumnSpec(AbstractColumnSpec aColumnSpec)
{
i_subtypeColumnSpecs.add(aColumnSpec);
}
public List getSubtypeColumnSpecs()
{
// This list is cloned to protect someone from changing it inadvertently
return (List) i_subtypeColumnSpecs.clone();
}
/** @return the union of the regular and the subtype columnspec lists */
public List getAllColumnSpecs()
{
List list = (List) i_columnSpecs.clone();
list.addAll(i_subtypeColumnSpecs);
return list;
}
public void addJoinTable(JoinTable aJoinTable)
{
i_joinTables.add(aJoinTable);
}
public List getJoinTables()
{
return (List) i_joinTables.clone();
}
public ColumnSpec getPrimaryKeyColumnSpec()
{
return i_primaryKeyColumnSpec;
}
/**
* Return the index of the first row to be converted to an object and
* returned. The index numbers start at 1 (not 0).
*
* @return a value of type 'int'
*/
public int getStartingIndex()
{
return i_startingIndex;
}
/**
* Set the index of the first row to be converted to an object and
* returned. The index numbers start at 1 (not 0). Don't forget to reset
* these before reusing this domain instance.
* @see AbstractDomain#resetStartingAndEndingIndexes
*/
public void setStartingIndex(int v)
{
i_startingIndex = v;
}
/**
* Return the index of the last row to be converted to an object and
* returned. The index numbers start at 1 (not 0).
*
* @return a value of type 'int'
*/
public int getEndingIndex()
{
return i_endingIndex;
}
/**
* Set the index of the last row to be converted to an object and
* returned. The index numbers start at 1 (not 0). Don't forget to reset
* these before reusing this domain instance.
* @see AbstractDomain#resetStartingAndEndingIndexes
*/
public void setEndingIndex(int v)
{
i_endingIndex = v;
}
/**
* Reset the starting and ending indexes so the whole resultset will be
* returned the next time a query is called.
*/
public void resetStartingAndEndingIndexes()
{
i_startingIndex = -1;
i_endingIndex = -1;
}
/* =============== Methods to Override =============== */
/**
* This method is called by the constructors.
*
* Example implementation:
*
// this.setTableName("Customer");
// this.setJDBCHelper(JDBCHelperFactory.create());
// this.setDatabasePolicy(new OracleDatabasePolicy());
// this.validateBeforeSaving(true); // true is the default
// this.returnSavedObject(true); // true is the default
//
// this.addColumnSpec(
// new IntegerColumnSpec(
// "PersonId",
// "getPersonId",
// "setPersonId",
// DEFAULT_TO_NULL,
// SEQUENCED_PRIMARY_KEY));
// this.addColumnSpec(
// new StringColumnSpec(
// "Name",
// "getName",
// "setName",
// DEFAULT_TO_EMPTY_STRING,
// REQUIRED,
// UNIQUE));
//
// JoinTable joinTable =
// new OuterJoinTable(
// "Status",
// "StatusId", // Person column(s)
// "Id"); // Status table column(s)
// joinTable.addJoinColumn(
// new StringJoinColumn(
// "Name", // Column Name
// "Status", // Alias in case of column name conflict
// "getStatusName", // Getter method name
// "setStatusName")); // Setter method name
// this.addJoinTable(joinTable);
//
*/
protected abstract void setup();
/**
* Subclasses must implement this to create a new PersistentObject.
*
* @return a value of type 'PersistentObject'
*/
public abstract PersistentObject newPersistentObject();
/**
* This is the default, no-operation implementation which is executed for
* all found objects. Subclasses should override when they want to do
* something to a PersistentObject after it is found in the database.
*
* <p>The JDBCHelper is an argument so that we have the flexibility to do
* our own object manipulation using the current row in the result set.
*
* <p><b>Note:</b> If you wish to use the JDBCHelper to do another SQL
* call, you must first clone it to get a new connection.
*
* @param aPO a value of type 'PersistentObject'
*/
protected void postFind(PersistentObject aPO,
JDBCHelper aJDBCHelper)
{
// No-operation. Subclasses can override.
}
/**
* This is the default, no-operation implementation. Subclasses should
* override when they want to do something to a PersistentObject before it
* is validated. If the subclass wants to do an update in the same
* transaction context, the given JDBCHelper should be used.
*
* @param aPO a value of type 'PersistentObject'
*/
protected void preValidate(PersistentObject aPO,
JDBCHelper aJDBCHelper)
throws
ObjectHasChangedException,
MissingAttributeException,
DuplicateRowException
{
// No-operation. Subclasses can override.
}
/**
* This is the default, no-operation implementation. Subclasses should
* override when they want to do something to a PersistentObject before it
* is saved. If the subclass wants to do an update in the same
* transaction context, the given JDBCHelper should be used.
*
* @param aPO a value of type 'PersistentObject'
*/
protected void preSave(PersistentObject aPO,
JDBCHelper aJDBCHelper)
throws
ObjectHasChangedException,
MissingAttributeException,
DuplicateRowException
{
// No-operation. Subclasses can override.
}
/**
* This is the default, no-operation implementation. Subclasses should
* override when they want to do something to a PersistentObject after it
* is updated or inserted -- like save objects that depend on it's primary
* key. If the subclass wants to do an update in the same transaction
* context, the given JDBCHelper should be used.
*
* @param aPO a value of type 'PersistentObject'
*/
protected void postSave(PersistentObject aPO,
JDBCHelper aJDBCHelper)
throws
ObjectHasChangedException,
MissingAttributeException,
DuplicateRowException
{
// No-operation. Subclasses can override.
}
/**
* This is the default, no-operation implementation. Subclasses should
* override when they want to do something to a PersistentObject before it
* is deleted -- like delete objects that depend on it.
*
* @param aPO a value of type 'PersistentObject'
*/
protected void preDelete(PersistentObject aPO,
JDBCHelper aJDBCHelper)
{
// No-operation. Subclasses can override.
}
/**
* This is the default, no-operation implementation. Subclasses should
* override when they want to do something to a PersistentObject after it
* is deleted.
*
* @param aPO a value of type 'PersistentObject'
*/
protected void postDelete(PersistentObject aPO,
JDBCHelper aJDBCHelper)
{
// No-operation. Subclasses can override.
}
/**
* The reflection in Java 2 makes this method almost as efficient as
* hard-coding the gets and sets in a subclass. Overriding is not
* recommended unless efficiency is a demonstrated problem.
*
* This is used by the findCustom(sqlString) method to convert a JDBC row
* to a value object.<BR>
*
* Example override implementation:<BR>
*
* // StatusPO aPO = new StatusValue();
* // aPO.setStatusId(aJDBCHelper.getInteger("StatusId"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -