Package net.sf.basedb.core
Class BasicBatcher<D extends BatchableData>
java.lang.Object
net.sf.basedb.core.AbstractBatcher
net.sf.basedb.core.BasicBatcher<D>
- All Implemented Interfaces:
AutoCloseable
,AccessControlled
,Batcher
- Direct Known Subclasses:
FeatureBatcher
,RawDataBatcher
,ReporterBatcher
public abstract class BasicBatcher<D extends BatchableData>
extends AbstractBatcher
implements AccessControlled
This is an abstract class for batching functionallity.
Add all objects and then call
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.- Version:
- 2.0
- Author:
- Samuel, Nicklas
- Last modified
- $Date: 2014-06-10 13:27:02 +0200 (ti, 10 jun 2014) $
-
Field Summary
Modifier and TypeFieldDescriptionThe class of data objects accepted by this batcher.protected static final boolean
So we don't always have to call logParam.debug()private int
private final String
private final PreparedStatement
private final String
The entity name as known to Hibernate.private final String
The name of the ID property.private int
private int
Counts the number of inserts done by the batcher.private final List<BatchedPropertyInfo>
private final String
private final PreparedStatement
protected static final Logger
Log all parameter bindings to prepared statement.private final ClassMetadata
Hibernate metadata for the batchable object.private int
The logged in user's permission to this item.private final Table
The table where items of this type are stored.private int
private int
private final List<BatchedPropertyInfo>
private final String
private final PreparedStatement
Fields inherited from class net.sf.basedb.core.AbstractBatcher
debugSqlEnabled, logSql
-
Constructor Summary
ConstructorDescriptionBasicBatcher
(Session session, Class<D> dataClass, String entityName) Constructor that initiate everything. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addToBatch
(D data, PreparedStatement ps, List<BatchedPropertyInfo> propertyOrder) Add the data object to the batch represented by the prepared statementfinal void
checkPermission
(Permission permission) Checks if the logged in user has the specified permission on this item.void
delete
(int id) Delete a data object by id.void
Delete a data object.void
flush()
Flush the batcher and send all remaining items in memory to the database.void
Flush the SQL delete command buffer.void
Flush the SQL insert command buffer.void
Flush the SQL insert command buffer.final Set<Permission>
Get the logged in user's permissions on the item.(package private) PluginPermission
(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
Gets the total number of inserts done by the batcherabstract Item
getType()
Get the type of item supported by the batcher.final 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 a data object.(package private) void
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 a data object.abstract void
Validate the data, since no validation is possible in set methods of the data object.Methods inherited from class net.sf.basedb.core.AbstractBatcher
analyzeTable, close, getBatchSize, getDbControl, getSessionControl, isClosed, setBatchSize, setDbControl, updateLastAccess
-
Field Details
-
logParam
Log all parameter bindings to prepared statement. -
debugEnabled
protected static final boolean debugEnabledSo we don't always have to call logParam.debug() -
insertCountTotal
private int insertCountTotalCounts the number of inserts done by the batcher. -
dataClass
The class of data objects accepted by this batcher. -
entityName
The entity name as known to Hibernate. -
permissions
private int permissionsThe logged in user's permission to this item. Calculated by theinitPermissions(int, int)
method. -
metaData
Hibernate metadata for the batchable object. -
table
The table where items of this type are stored. -
idPropertyName
The name of the ID property. Usually this is 'id'. -
insertStatement
-
updateStatement
-
deleteStatement
-
insertSql
-
updateSql
-
deleteSql
-
insertPropertyOrder
-
updatePropertyOrder
-
insertCount
private int insertCount -
updateCount
private int updateCount -
deleteCount
private int deleteCount -
totalCount
private int totalCount
-
-
Constructor Details
-
BasicBatcher
BasicBatcher(Session session, Class<D> dataClass, String entityName) throws BaseException Constructor that initiate everything.- Parameters:
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 name- Throws:
BaseException
- If there is an error
-
-
Method Details
-
hasPermission
Checks if the logged in user has the specified permission on this item. The default implementation only checks the role keys for permissions. A subclass may not override this method since it would make it possible to bypass security checks.Subclasses that needs to check other keys, should override the
initPermissions(int,int)
method instead.- Specified by:
hasPermission
in interfaceAccessControlled
- Parameters:
permission
- A value from thePermission
class- Returns:
TRUE
if the user has the permission,FALSE
otherwise
-
checkPermission
Checks if the logged in user has the specified permission on this item. If not, aPermissionDeniedException
is thrown.- Specified by:
checkPermission
in interfaceAccessControlled
- Parameters:
permission
- A value from thePermission
class- Throws:
PermissionDeniedException
- If the logged in user doesn't have the requested permission
-
getPermissions
Description copied from interface:AccessControlled
Get the logged in user's permissions on the item.- Specified by:
getPermissions
in interfaceAccessControlled
-
flush
Description copied from interface:Batcher
Flush the batcher and send all remaining items in memory to the database.- Specified by:
flush
in interfaceBatcher
- Throws:
BaseException
- If there is an error- See Also:
-
onBeforeClose
Close all open SQL statements.- Overrides:
onBeforeClose
in classAbstractBatcher
- Throws:
BaseException
-
initPermissions
Initialise the logged in user's permissions for this batcher. The default implementation checks the role keys.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 callsuper.initPermissions(granted, denied)
.- Parameters:
granted
- Permissions that have been granted by the subclassdenied
- Permissions that have been denied by the subclass- Throws:
BaseException
- If the permissions couldn't be initialised
-
getPluginPermissions
PluginPermission getPluginPermissions() -
insert
Insert a data object. The data object will not be synchronized with the database. The object values may be modified.- Parameters:
data
- Data object to be inserted- Throws:
InvalidDataException
- If the data is null or it doesn't validate against thevalidate(BatchableData)
methodPermissionDeniedException
- If the logged in user doesn't have create permissionBaseException
- If there is another error
-
update
Update a data object. The data object will not be synchronized with the database. The object values may be modified.- Parameters:
data
- Data object to be updated- Throws:
InvalidDataException
- If the data is null or it doesn't validate against thevalidate(BatchableData)
methodPermissionDeniedException
- If the logged in user doesn't have write permissionBaseException
- If there is another error
-
delete
Delete a data object.- Parameters:
data
- Data object to be deleted- Throws:
InvalidDataException
- If the data is nullPermissionDeniedException
- If the logged in user doesn't have delete permissionBaseException
- If there is another error
-
delete
Delete a data object by id.- Parameters:
id
- Id of the object to be deleted- Throws:
PermissionDeniedException
- If the logged in user doesn't have delete permissionBaseException
- If there is another error
-
flushInsert
Flush the SQL insert command buffer. Sends the remaining SQL commands in the batch to the database for execution.- Throws:
BaseException
- Thrown if there is a database error.
-
flushUpdate
Flush the SQL insert command buffer. Sends the remaining SQL commands in the batch to the database for execution.- Throws:
BaseException
- Thrown if there is a database error.
-
flushDelete
Flush the SQL delete command buffer. Sends the remaining SQL commands in the batch to the database for execution.- Throws:
BaseException
- Thrown if there is a database error.
-
addToBatch
private void addToBatch(D data, PreparedStatement ps, List<BatchedPropertyInfo> propertyOrder) throws DatabaseException Add the data object to the batch represented by the prepared statement- Parameters:
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 statement- Throws:
DatabaseException
- If there is an error
-
setPropertyValue
Set a protected property value on a data object using Hibernate built-in reflection methods.- Parameters:
data
- The data object to set the property onpropertyName
- The name of the property, ie. same asgetXxx
method name minus theget
partvalue
- The value to give to the property
-
getPropertyValue
Get the value of a protected property value on a data object using Hibernate built-in reflection methods.- Parameters:
data
- The data object to set the property onpropertyName
- The name of the property, ie. same asgetXxx
method name minus theget
part- Returns:
- The value of the property
-
onBeforeCommit
Called after the object has been validated just before the SQL is added to the batch queue.- Parameters:
data
- Object to updateaction
- The action- Throws:
BaseException
-
validate
Validate the data, since no validation is possible in set methods of the data object. Ie. this is very similar to case 2 validation for items.- Parameters:
data
- Data object to validate- Throws:
InvalidDataException
- If the data object is invalid
-
getType
Get the type of item supported by the batcher. The returned value is one of the values defined in theItem
enumeration.- Returns:
- A value indicating the type of item
- See Also:
-
getTotalInsertCount
public int getTotalInsertCount()Gets the total number of inserts done by the batcher
-