public abstract class BasicBatcher<D extends BatchableData> extends AbstractBatcher implements AccessControlled
flush
to
send the batch to the database. BasicBatcher
can
also have a limit on the batch size, when the batch is
full BasicBatcher
will automatically call
flush
to send the current batch to
the database.Modifier and Type | Field and Description |
---|---|
private Class<D> |
dataClass
The class of data objects accepted by this batcher.
|
protected static boolean |
debugEnabled
So we don't always have to call logParam.debug()
|
private int |
deleteCount |
private String |
deleteSql |
private PreparedStatement |
deleteStatement |
private String |
entityName
The entity name as known to Hibernate.
|
private String |
idPropertyName
The name of the ID property.
|
private int |
insertCount |
private int |
insertCountTotal
Counts the number of inserts done by the batcher.
|
private List<BatchedPropertyInfo> |
insertPropertyOrder |
private String |
insertSql |
private PreparedStatement |
insertStatement |
protected static org.slf4j.Logger |
logParam
Log all parameter bindings to prepared statement.
|
private ClassMetadata |
metaData
Hibernate metadata for the batchable object.
|
private int |
permissions
The logged in user's permission to this item.
|
private Table |
table
The table where items of this type are stored.
|
private int |
totalCount |
private int |
updateCount |
private List<BatchedPropertyInfo> |
updatePropertyOrder |
private String |
updateSql |
private PreparedStatement |
updateStatement |
debugSqlEnabled, logSql
Constructor and Description |
---|
BasicBatcher(Session session,
Class<D> dataClass,
String entityName)
Constructor that initiate everything.
|
Modifier and Type | Method and Description |
---|---|
private void |
addToBatch(D data,
PreparedStatement ps,
List<BatchedPropertyInfo> propertyOrder)
Add the data object to the batch represented by the prepared statement
|
void |
checkPermission(Permission permission)
Checks if the logged in user has the specified permission on
this item.
|
void |
delete(D data)
Delete a data object.
|
void |
delete(int id)
Delete a data object by id.
|
void |
flush()
Flush the batcher and send all remaining items in memory to the
database.
|
void |
flushDelete()
Flush the SQL delete command buffer.
|
void |
flushInsert()
Flush the SQL insert command buffer.
|
void |
flushUpdate()
Flush the SQL insert command buffer.
|
Set<Permission> |
getPermissions()
Get the logged in user's permissions on the item.
|
(package private) PluginPermission |
getPluginPermissions() |
(package private) Object |
getPropertyValue(D data,
String propertyName)
Get the value of a protected property value on a data object using
Hibernate built-in reflection methods.
|
int |
getTotalInsertCount()
Gets the total number of inserts done by the batcher
|
abstract Item |
getType()
Get the type of item supported by the batcher.
|
boolean |
hasPermission(Permission permission)
Checks if the logged in user has the specified permission on
this item.
|
(package private) void |
initPermissions(int granted,
int denied)
Initialise the logged in user's permissions for this batcher.
|
void |
insert(D data)
Insert a data object.
|
(package private) void |
onBeforeClose()
Close all open SQL statements.
|
(package private) void |
onBeforeCommit(D data,
Transactional.Action action)
Called after the object has been validated just before the SQL is
added to the batch queue.
|
(package private) void |
setPropertyValue(D data,
String propertyName,
Object value)
Set a protected property value on a data object using Hibernate
built-in reflection methods.
|
void |
update(D data)
Update a data object.
|
abstract void |
validate(D data)
Validate the data, since no validation is possible in set methods
of the data object.
|
analyzeTable, close, getBatchSize, getDbControl, getSessionControl, isClosed, setBatchSize, setDbControl, updateLastAccess
protected static final org.slf4j.Logger logParam
protected static final boolean debugEnabled
private int insertCountTotal
private final Class<D extends BatchableData> dataClass
private final String entityName
private int permissions
initPermissions(int, int)
method.private final ClassMetadata metaData
private final Table table
private final String idPropertyName
private final PreparedStatement insertStatement
private final PreparedStatement updateStatement
private final PreparedStatement deleteStatement
private final String insertSql
private final String updateSql
private final String deleteSql
private final List<BatchedPropertyInfo> insertPropertyOrder
private final List<BatchedPropertyInfo> updatePropertyOrder
private int insertCount
private int updateCount
private int deleteCount
private int totalCount
BasicBatcher(Session session, Class<D> dataClass, String entityName) throws BaseException
dataClass
- The class of the data objects that are accepted by this batcherentityName
- The entity name of the data as known by Hibernate, use null
to use the dataClass as the entity nameBaseException
- If there is an errorpublic final boolean hasPermission(Permission permission)
Subclasses that needs to check other keys, should override the initPermissions(int,int)
method instead.
hasPermission
in interface AccessControlled
permission
- A value from the Permission
classTRUE
if the user has the permission,
FALSE
otherwisepublic final void checkPermission(Permission permission) throws PermissionDeniedException
PermissionDeniedException
is thrown.checkPermission
in interface AccessControlled
permission
- A value from the Permission
classPermissionDeniedException
- If the logged in user doesn't
have the requested permissionpublic final Set<Permission> getPermissions()
AccessControlled
getPermissions
in interface AccessControlled
public void flush() throws BaseException
Batcher
flush
in interface Batcher
BaseException
- If there is an errorBatcher.close()
void onBeforeClose() throws BaseException
onBeforeClose
in class AbstractBatcher
BaseException
void initPermissions(int granted, int denied) throws BaseException
Subclasses that needs to check other keys or properties, such
as the RawDataBatcher
should override
this method. The subclass should calculate additional permissions
to be granted or denied, and combine those with whatever was passed
as parameters. Use the binary OR operator ( | ) to combine
the permissions. Finally the subclass must call
super.initPermissions(granted, denied)
.
granted
- Permissions that have been granted by the subclassdenied
- Permissions that have been denied by the subclassBaseException
- If the permissions couldn't be initialisedPluginPermission getPluginPermissions()
public void insert(D data) throws InvalidDataException, PermissionDeniedException, BaseException
data
- Data object to be insertedInvalidDataException
- If the data is null or it doesn't validate
against the validate(BatchableData)
methodPermissionDeniedException
- If the logged in user doesn't
have create permissionBaseException
- If there is another errorpublic void update(D data) throws InvalidDataException, PermissionDeniedException, BaseException
data
- Data object to be updatedInvalidDataException
- If the data is null or it doesn't validate
against the validate(BatchableData)
methodPermissionDeniedException
- If the logged in user doesn't
have write permissionBaseException
- If there is another errorpublic void delete(D data) throws InvalidDataException, PermissionDeniedException, BaseException
data
- Data object to be deletedInvalidDataException
- If the data is nullPermissionDeniedException
- If the logged in user doesn't
have delete permissionBaseException
- If there is another errorpublic void delete(int id) throws PermissionDeniedException, BaseException
id
- Id of the object to be deletedPermissionDeniedException
- If the logged in user doesn't
have delete permissionBaseException
- If there is another errorpublic void flushInsert() throws BaseException
BaseException
- Thrown if there is a database error.public void flushUpdate() throws BaseException
BaseException
- Thrown if there is a database error.public void flushDelete() throws BaseException
BaseException
- Thrown if there is a database error.private void addToBatch(D data, PreparedStatement ps, List<BatchedPropertyInfo> propertyOrder) throws DatabaseException
data
- Data object to get property values fromps
- The prepared statement to add the data object topropertyOrder
- The property names of the parameter order in the prepared statementDatabaseException
- If there is an errorvoid setPropertyValue(D data, String propertyName, Object value)
data
- The data object to set the property onpropertyName
- The name of the property, ie. same
as getXxx
method name minus the get
partvalue
- The value to give to the propertyObject getPropertyValue(D data, String propertyName)
data
- The data object to set the property onpropertyName
- The name of the property, ie. same
as getXxx
method name minus the get
partvoid onBeforeCommit(D data, Transactional.Action action) throws BaseException
data
- Object to updateaction
- The actionBaseException
public abstract void validate(D data) throws InvalidDataException
data
- Data object to validateInvalidDataException
- If the data object is invalidpublic abstract Item getType()
Item
enumeration.Item
public int getTotalInsertCount()