public final class DbControl
extends java.lang.Object
DbControl
object is the main object used for communicating
with the database. It contains methods for saving and deleting
items, handling transactions, etc. DbControl
objects
are created by the SessionControl.newDbControl()
method.Modifier and Type | Class and Description |
---|---|
private static class |
DbControl.SaveIf |
Modifier and Type | Field and Description |
---|---|
private java.util.List<Batcher> |
batchers
A list of batchers that will be closed when
DbControl is
closed and will be flushed when commit is called. |
private java.lang.Throwable |
calledFrom
To keep track of the code that created this DbControl.
|
private java.util.Map<BasicItem,Transactional.Action> |
commitQueue
A list of items that need special handling when commit is called.
|
private SessionWrapper |
hSession
The Hibernate session.
|
private StatelessSessionWrapper |
hStatelessSession
A Hibernate stateless session.
|
private Transaction |
hTransaction
The Hibernate transaction.
|
private boolean |
isClosed
Is the connection closed or not?
|
private boolean |
isRolledBack
If the current transaction has been rolled back or not.
|
private java.util.Map<BasicData,BasicItem> |
itemCache
A cache of already loaded items.
|
private static org.slf4j.Logger |
log
Log core events.
|
private LogControl |
logControl
Handles logging of changes to items.
|
private LoggingInterceptor |
logInterceptor |
private ProjectSpecificAnnotationsManager |
pspManager |
private java.util.Map<BasicItem,java.util.List<DbControl.SaveIf>> |
saveIfQueue
Lists items that are saved only if the parent item is also saved.
|
private SessionControl |
sc |
private java.util.List<TransactionalAction> |
transactionalActions
List of actions that should be performed when the
transaction commits or rollbacks.
|
private java.util.Set<java.lang.String> |
uniqueRandoms
Holds random strings that must be unique within a transaction.
|
Constructor and Description |
---|
DbControl(SessionControl sc)
Create a new object.
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
addBatcher(Batcher batcher)
Add a
Batcher to the batcher queue. |
void |
addTransactionalAction(TransactionalAction action)
Add a transactional action to this DbControl.
|
private void |
cleanUp()
Clear and close all objects.
|
void |
close()
Close the connection to the database.
|
void |
commit()
Commit all changes made to items and close the connection to the database.
|
void |
deleteItem(BasicItem item)
Schedule an existing item to be deleted from the database.
|
void |
detachItem(BasicItem item)
Detach an item from this
DbControl . |
void |
disconnect()
Deprecated.
In 3.12. Applications should use separate DbControl instead.
|
protected void |
finalize()
Clean up if a bad client application forgot to close the connection.
|
(package private) long |
getDiskUsage(GroupData group,
QuotaTypeData quotaType,
Location location)
Get the disk usage for the specified group at the specified
location and quota type.
|
(package private) long |
getDiskUsage(UserData user,
QuotaTypeData quotaType,
Location location)
Get the disk usage for the specified user at the specified
location and quota type.
|
(package private) Session |
getHibernateSession()
Get the underlying Hibernate session.
|
(package private) <I extends BasicItem> |
getItem(java.lang.Class<I> itemClass,
BasicData data,
java.lang.Object... extraParameters)
Get an item object for a known data object, using the cache if
possible.
|
(package private) LogControl |
getLogControl()
Get the log controller for this db control.
|
(package private) LoggingInterceptor |
getLoggingInterceptor()
Get the logging interceptor for this db control.
|
(package private) ProjectSpecificAnnotationsManager |
getProjectSpecificAnnotationsManager()
Get the manager used for handling project specific annotations
in this transaction.
|
SessionControl |
getSessionControl()
Get the
SessionControl object that owns
this DbControl object. |
(package private) StatelessSession |
getStatelessSession()
Get a stateless Hibernate session using the same database connection as
the regular session.
|
void |
initCollection(BasicItem item,
java.lang.String collectionName)
Initialise a collection on an item.
|
(package private) boolean |
isAttached(BasicItem item)
Check if an item is attached to this
DbControl . |
boolean |
isClosed()
Check if the connection to the database has been closed.
|
boolean |
isConnected()
Deprecated.
In 3.11. Applications should use separate DbControl instead.
|
boolean |
isRolledBack()
|
(package private) <I extends BasicItem> |
loadItem(java.lang.Class<I> itemClass,
int id)
Load an item from the database when you now the id.
|
<I extends BasicItem> |
loadItem(I item)
Load an item from the database.
|
(package private) <I extends BasicItem,D extends BasicData> |
loadItemByDataClass(java.lang.Class<I> itemClass,
java.lang.Class<D> dataClass,
int id)
Load an item from the database when you know the id.
|
void |
logEntry(ManualLogEntry logEntry)
Add a manual log entry to the history log.
|
(package private) <I extends BasicItem> |
newItem(java.lang.Class<I> itemClass,
java.lang.Object... extraParameters)
Create a new item.
|
<T extends BasicItem> |
reattachItem(T item,
boolean updated)
Reattach a detached item from this
DbControl . |
void |
reconnect()
Deprecated.
In 3.12. Applications should use separate DbControl instead.
|
void |
refreshItem(BasicItem item)
Reload the item from the database.
|
(package private) void |
removeBatcher(Batcher batcher)
Remove a
Batcher from the batcher queue. |
private void |
rollback()
Rollback the transaction.
|
void |
saveItem(BasicItem item)
Schedule a new item to be saved in the database.
|
void |
saveItemIf(BasicItem itemIf,
BasicItem item,
boolean before)
Schedule a new item to be saved in the database if another item is also saved.
|
java.lang.String |
uniqueRandom()
Generate a random string value that is unique for the given transaction.
|
private void |
updateDiskUsage(BasicItem item)
Update the disk usage information for a DiskConsumable item.
|
private static final org.slf4j.Logger log
private final SessionControl sc
private final java.lang.Throwable calledFrom
private LogControl logControl
private LoggingInterceptor logInterceptor
private ProjectSpecificAnnotationsManager pspManager
private SessionWrapper hSession
private StatelessSessionWrapper hStatelessSession
private Transaction hTransaction
private java.util.Map<BasicData,BasicItem> itemCache
private java.util.Map<BasicItem,Transactional.Action> commitQueue
private java.util.Map<BasicItem,java.util.List<DbControl.SaveIf>> saveIfQueue
private java.util.List<Batcher> batchers
DbControl
is
closed and will be flushed when commit is called.private java.util.List<TransactionalAction> transactionalActions
private boolean isClosed
private boolean isRolledBack
private java.util.Set<java.lang.String> uniqueRandoms
DbControl(SessionControl sc) throws BaseException
BaseException
public boolean isClosed()
DbControl
object.SessionControl.newDbControl()
public SessionControl getSessionControl()
SessionControl
object that owns
this DbControl
object.SessionControl
objectLogControl getLogControl()
ProjectSpecificAnnotationsManager getProjectSpecificAnnotationsManager()
LoggingInterceptor getLoggingInterceptor()
Session getHibernateSession()
StatelessSession getStatelessSession()
@Deprecated public void disconnect() throws ConnectionClosedException, BaseException
reconnect()
before calling
commit()
. If you have used batch items, for example
ReporterBatcher
, you should call commit()
or you will lose
everything that has already been BasicBatcher.flush()
:ed to the
database.ConnectionClosedException
- If connection to database has been closed.BaseException
- If there is another error@Deprecated public void reconnect() throws ConnectionClosedException, BaseException
disconnect()
.ConnectionClosedException
- If connection to database has been closed.BaseException
- If there is another error.@Deprecated public boolean isConnected()
public void close()
DbControl
cannot be used anymore.public boolean isRolledBack()
close()
or because of an error during commit()
. To check if a transaction
was successfully committed use: dc.isClosed() && !dc.isRolledBack()
.private void rollback()
private void cleanUp()
public void addTransactionalAction(TransactionalAction action)
public void commit() throws BaseException
BaseException
- If there is an errorprivate void updateDiskUsage(BasicItem item) throws QuotaException, BaseException
item
parameter impements the
DiskConsumable
interface and that it's data object implements the
DiskConsumableData
interface.QuotaException
- If the quota is exceededBaseException
- If there is another errorlong getDiskUsage(UserData user, QuotaTypeData quotaType, Location location) throws BaseException
BaseException
long getDiskUsage(GroupData group, QuotaTypeData quotaType, Location location) throws BaseException
BaseException
<I extends BasicItem> I newItem(java.lang.Class<I> itemClass, java.lang.Object... extraParameters)
itemClass
- The class of the itemitemClass
classBaseException
- If there is an errorpublic void logEntry(ManualLogEntry logEntry)
public <I extends BasicItem> I loadItem(I item)
item
- The item to load<I extends BasicItem> I loadItem(java.lang.Class<I> itemClass, int id) throws PermissionDeniedException, BaseException
itemClass
- The Class
of the item objectid
- The id of the objectitemClass
classPermissionDeniedException
- If the logged in user doesn't have
read permissionBaseException
- If there is another error<I extends BasicItem,D extends BasicData> I loadItemByDataClass(java.lang.Class<I> itemClass, java.lang.Class<D> dataClass, int id) throws PermissionDeniedException, BaseException
itemClass
- The item class for the objectdataClass
- The actual data class for the object which must be compatible
with the item classid
- The id of the objectitemClass
classPermissionDeniedException
BaseException
<I extends BasicItem> I getItem(java.lang.Class<I> itemClass, BasicData data, java.lang.Object... extraParameters) throws PermissionDeniedException, ItemNotFoundException, BaseException
itemClass
- The Class
of the item objectdata
- The data object, or nullitemClass
class, or null if null was passedPermissionDeniedException
- If the logged in user doesn't have
read permissionItemNotFoundException
- If the data object is a Hibernate proxy
which points to a non-existing row in the databaseBaseException
- If there is another errorpublic void saveItem(BasicItem item) throws PermissionDeniedException, ItemAlreadyExistsException, BaseException
commit()
method is called.item
- The item to be savedPermissionDeniedException
- If the logged in user doesn't have
create permissionItemAlreadyExistsException
- If the item already exists in the
databaseBaseException
- If there is another errorpublic void saveItemIf(BasicItem itemIf, BasicItem item, boolean before)
itemIf
item is saved then item
is also saved,
otherwise it is not. The method is useful for utility methods that creates a lot
of child items to a parent item and we wan't to wait putting them in the real
save queue until the parent item is saved. If the parent item is already in the
database or saveItem(itemIf) has been called this method is the same as
saveItem(item)
itemIf
- The parent item that must be saveditem
- The item to save when the parent item is savedbefore
- TRUE if the item
should be saved before the itemIf
,
FALSE to save itemIf
firstsaveItem(BasicItem)
public void deleteItem(BasicItem item) throws PermissionDeniedException, ItemNotFoundException, BaseException
commit()
method is called.
If you are deleting multiple items it is better to use
Trashcan.delete(SessionControl, java.util.Collection, boolean, ProgressReporter)
since it will automatically select the best order to delete the items.item
- The item to be deletedPermissionDeniedException
- If the logged in user doesn't have
delete permissionItemNotFoundException
- If the item doesn't exist in the
databaseBaseException
- If there is another errorpublic void detachItem(BasicItem item) throws BaseException
DbControl
. The detached item
will no longer be managed and changes will not be saved to the database.
For some items, certain operations are forbidden for detached items. For example
you are not allowed to upload files to File
items.item
- The item to be detachedBaseException
- If there is an errorreattachItem(BasicItem, boolean)
public <T extends BasicItem> T reattachItem(T item, boolean updated) throws PermissionDeniedException, ItemNotFoundException, BaseException
DbControl
. If the
reattached already exists in the database it will be managed and
changes will be saved to the database.item
- The item to be reattachedupdated
- If TRUE, the item has been updated while it was detached
and changes will be saved to the database. If FALSE, the item will
only be saved to the database if it is modified after the
reattachementPermissionDeniedException
- If the logged in user doesn't have
read/write permissionBaseException
- If there is another errorItemNotFoundException
detachItem(BasicItem)
public void refreshItem(BasicItem item)
item
- The item to reloadboolean isAttached(BasicItem item)
DbControl
.
An item is attached if it is found in either the item cache (existing items
only) or commit queue (new or existing items).public void initCollection(BasicItem item, java.lang.String collectionName) throws BaseException
Quota
object and calls the
Quota.getQuotaValue(QuotaType, Location)
method.item
- The itemcollectionName
- The name of the collectionBaseException
- If there is an errordetachItem(BasicItem)
,
reattachItem(BasicItem, boolean)
public java.lang.String uniqueRandom()
void addBatcher(Batcher batcher)
Batcher
to the batcher queue.batcher
- The Batcher
to addvoid removeBatcher(Batcher batcher)
Batcher
from the batcher queue.batcher
- The Batcher
to removeprotected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable