2.17.2: 2011-06-17

net.sf.basedb.core
Class BasicBatcher<D extends BatchableData>

java.lang.Object
  extended by net.sf.basedb.core.AbstractBatcher
      extended by net.sf.basedb.core.BasicBatcher<D>
All Implemented Interfaces:
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: 2009-04-06 14:52:39 +0200 (Mon, 06 Apr 2009) $

Field Summary
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 Logger logParam
          Log all parameter bindings to prepared statement.
private  org.hibernate.metadata.ClassMetadata metaData
          Hibernate metadata for the batchable object.
private  int permissions
          The logged in user's permission to this item.
private  org.hibernate.mapping.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
           
 
Fields inherited from class net.sf.basedb.core.AbstractBatcher
debugSqlEnabled, logSql
 
Constructor Summary
BasicBatcher(org.hibernate.Session session, Class<D> dataClass, String entityName)
          Constructor that initiate everything.
 
Method Summary
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.
 
Methods inherited from class net.sf.basedb.core.AbstractBatcher
analyzeTable, close, getBatchSize, getDbControl, getSessionControl, isClosed, setBatchSize, setDbControl, updateLastAccess
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logParam

protected static final Logger logParam
Log all parameter bindings to prepared statement.


debugEnabled

protected static final boolean debugEnabled
So we don't always have to call logParam.debug()


insertCountTotal

private int insertCountTotal
Counts the number of inserts done by the batcher.


dataClass

private final Class<D extends BatchableData> dataClass
The class of data objects accepted by this batcher.


entityName

private final String entityName
The entity name as known to Hibernate.


permissions

private int permissions
The logged in user's permission to this item. Calculated by the initPermissions(int, int) method.


metaData

private final org.hibernate.metadata.ClassMetadata metaData
Hibernate metadata for the batchable object.


table

private final org.hibernate.mapping.Table table
The table where items of this type are stored.


idPropertyName

private final String idPropertyName
The name of the ID property. Usually this is 'id'.


insertStatement

private final PreparedStatement insertStatement

updateStatement

private final PreparedStatement updateStatement

deleteStatement

private final PreparedStatement deleteStatement

insertSql

private final String insertSql

updateSql

private final String updateSql

deleteSql

private final String deleteSql

insertPropertyOrder

private final List<BatchedPropertyInfo> insertPropertyOrder

updatePropertyOrder

private final List<BatchedPropertyInfo> updatePropertyOrder

insertCount

private int insertCount

updateCount

private int updateCount

deleteCount

private int deleteCount

totalCount

private int totalCount
Constructor Detail

BasicBatcher

BasicBatcher(org.hibernate.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 batcher
entityName - 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 Detail

hasPermission

public final boolean hasPermission(Permission permission)
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 interface AccessControlled
Parameters:
permission - A value from the Permission class
Returns:
TRUE if the user has the permission, FALSE otherwise

checkPermission

public final void checkPermission(Permission permission)
                           throws PermissionDeniedException
Checks if the logged in user has the specified permission on this item. If not, a PermissionDeniedException is thrown.

Specified by:
checkPermission in interface AccessControlled
Parameters:
permission - A value from the Permission class
Throws:
PermissionDeniedException - If the logged in user doesn't have the requested permission

getPermissions

public final Set<Permission> getPermissions()
Description copied from interface: AccessControlled
Get the logged in user's permissions on the item.

Specified by:
getPermissions in interface AccessControlled

flush

public void flush()
           throws BaseException
Description copied from interface: Batcher
Flush the batcher and send all remaining items in memory to the database.

Specified by:
flush in interface Batcher
Throws:
BaseException - If there is an error
See Also:
Batcher.close()

onBeforeClose

void onBeforeClose()
             throws BaseException
Close all open SQL statements.

Overrides:
onBeforeClose in class AbstractBatcher
Throws:
BaseException

initPermissions

void initPermissions(int granted,
                     int denied)
               throws BaseException
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 call super.initPermissions(granted, denied).

Parameters:
granted - Permissions that have been granted by the subclass
denied - Permissions that have been denied by the subclass
Throws:
BaseException - If the permissions couldn't be initialised

getPluginPermissions

PluginPermission getPluginPermissions()

insert

public void insert(D data)
            throws InvalidDataException,
                   PermissionDeniedException,
                   BaseException
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 the validate(BatchableData) method
PermissionDeniedException - If the logged in user doesn't have create permission
BaseException - If there is another error

update

public void update(D data)
            throws InvalidDataException,
                   PermissionDeniedException,
                   BaseException
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 the validate(BatchableData) method
PermissionDeniedException - If the logged in user doesn't have write permission
BaseException - If there is another error

delete

public void delete(D data)
            throws InvalidDataException,
                   PermissionDeniedException,
                   BaseException
Delete a data object.

Parameters:
data - Data object to be deleted
Throws:
InvalidDataException - If the data is null
PermissionDeniedException - If the logged in user doesn't have delete permission
BaseException - If there is another error

delete

public void delete(int id)
            throws PermissionDeniedException,
                   BaseException
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 permission
BaseException - If there is another error

flushInsert

public void flushInsert()
                 throws BaseException
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

public void flushUpdate()
                 throws BaseException
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

public void flushDelete()
                 throws BaseException
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 from
ps - The prepared statement to add the data object to
propertyOrder - The property names of the parameter order in the prepared statement
Throws:
DatabaseException - If there is an error

setPropertyValue

void setPropertyValue(D data,
                      String propertyName,
                      Object value)
Set a protected property value on a data object using Hibernate built-in reflection methods.

Parameters:
data - The data object to set the property on
propertyName - The name of the property, ie. same as getXxx method name minus the get part
value - The value to give to the property

getPropertyValue

Object getPropertyValue(D data,
                        String propertyName)
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 on
propertyName - The name of the property, ie. same as getXxx method name minus the get part
Returns:
The value of the property

onBeforeCommit

void onBeforeCommit(D data,
                    Transactional.Action action)
              throws BaseException
Called after the object has been validated just before the SQL is added to the batch queue.

Parameters:
data - Object to update
action - The action
Throws:
BaseException

validate

public abstract void validate(D data)
                       throws InvalidDataException
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

public abstract Item getType()
Get the type of item supported by the batcher. The returned value is one of the values defined in the Item enumeration.

Returns:
A value indicating the type of item
See Also:
Item

getTotalInsertCount

public int getTotalInsertCount()
Gets the total number of inserts done by the batcher


2.17.2: 2011-06-17