org.sakaiproject.util.storage
Class BaseDbSingleStorage

java.lang.Object
  extended byorg.sakaiproject.util.storage.BaseDbSingleStorage
Direct Known Subclasses:
DbAliasService.DbStorageOld, DbAssignmentService.DbCachedAssignmentContentStorage, DbAssignmentService.DbCachedAssignmentStorage, DbAssignmentService.DbCachedAssignmentSubmissionStorage, DbDigestService.DbStorage, DbNotificationService.DbStorage, DbPreferencesService.DbStorage, DbUserService.DbStorageOld

public class BaseDbSingleStorage
extends Object

BaseDbSingleStorage is a class that stores Resources (of some type) in a database, provides locked access, and generally implements a services "storage" class. The service's storage class can extend this to provide covers to turn Resource and Edit into something more type specific to the service.

Note: the methods here are all "id" based, with the following assumptions: - just the Resource Id field is enough to distinguish one Resource from another - a resource's reference is based on no more than the resource id - a resource's id cannot change. In order to handle Unicode characters properly, the SQL statements executed by this class should not embed Unicode characters into the SQL statement text; rather, Unicode values should be inserted as fields in a PreparedStatement. Databases handle Unicode better in fields.

Version:
$Revision: 2371 $
Author:
University of Michigan, CHEF Software Development Team

Field Summary
protected  boolean m_caseInsensitive
          If set, we treat reasource ids as case insensitive.
protected  Hashtable m_locks
          Locks, keyed by reference, holding Connections (or, if locks are done locally, holding an Edit).
protected  boolean m_locksAreInDb
          If true, we do our locks in the remote database.
protected  boolean m_locksAreInTable
          If true, we do our locks in the remove database using a separate locking table.
protected  String m_resourceEntryTagName
          The xml tag name for the element holding each actual resource entry.
protected  String m_resourceTableIdField
          The field in the resource table that holds the resource id.
protected  String m_resourceTableName
          Table name for resource records.
protected  String[] m_resourceTableOtherFields
          The additional field names in the resource table that go between the two ids and the xml
protected  SqlService m_sql
          Injected (by constructor) SqlService.
protected  StorageUser m_user
          The StorageUser to callback for new Resource and Edit objects.
 
Constructor Summary
BaseDbSingleStorage(String resourceTableName, String resourceTableIdField, String[] resourceTableOtherFields, boolean locksInDb, String resourceEntryName, StorageUser user, SqlService sqlService)
          Construct.
 
Method Summary
 void cancelResource(Edit edit)
          Cancel the changes and release the lock.
protected  String caseId(String id)
          Fix the case of resource ids to support case insensitive ids if enabled
 boolean checkResource(String id)
          Check if a Resource by this id exists.
 void close()
          Close.
 void commitDeleteResource(Edit edit, String uuid)
          update XML attribute on properties and remove locks
 void commitResource(Edit edit)
          Commit the changes and release the lock.
 int countAllResources()
           
 int countSelectedResourcesWhere(String sqlWhere)
           
 Edit editResource(String id)
          Get a lock on the Resource with this id, or null if a lock cannot be gotten.
 List getAllResources()
           
 List getAllResources(int first, int last)
           
 List getAllResourcesWhere(String field, String value)
          Get all Resources where the given field matches the given value.
 List getAllResourcesWhereLike(String field, String value)
           
 Entity getResource(String id)
          Get the Resource with this id, or null if not found.
 List getSelectedResources(Filter filter)
          Get selected Resources, filtered by a test on the id field
 List getSelectedResourcesWhere(String sqlWhere)
          Get selected Resources, using a supplied where clause
protected  String insertDeleteFields(String before, String[] fields, String uuid, String date, String userId, String after)
          Construct the SQL statement
protected  String insertFields(String before, String[] fields, String after)
          Form a string of (field, field, field), for sql insert statements, one for each item in the fields array, plus one before, and one after.
 boolean isEmpty()
           
protected  List loadResources(String sql, Object[] fields)
           
 void open()
          Open and be ready to read / write.
 Edit putDeleteResource(String id, String uuid, String userId, Object[] others)
          store the record in content_resource_delete table along with resource_uuid and date
 Edit putResource(String id, Object[] others)
          Add a new Resource with this id.
protected  Entity readResource(String xml)
          Read one Resource from xml
 void removeResource(Edit edit)
          Remove this (locked) Resource.
protected  void setCaseInsensitivity(boolean setting)
          Enable / disable case insensitive ids.
protected  String updateSet(String[] fields)
          Form a string of n name=?
protected  String valuesParams(String[] fields)
          Form a string of n question marks with commas, for sql value statements, one for each item in the values array, or an empty string if null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_resourceTableName

protected String m_resourceTableName
Table name for resource records.


m_resourceTableIdField

protected String m_resourceTableIdField
The field in the resource table that holds the resource id.


m_resourceTableOtherFields

protected String[] m_resourceTableOtherFields
The additional field names in the resource table that go between the two ids and the xml


m_resourceEntryTagName

protected String m_resourceEntryTagName
The xml tag name for the element holding each actual resource entry.


m_locksAreInDb

protected boolean m_locksAreInDb
If true, we do our locks in the remote database.


m_locksAreInTable

protected boolean m_locksAreInTable
If true, we do our locks in the remove database using a separate locking table.


m_user

protected StorageUser m_user
The StorageUser to callback for new Resource and Edit objects.


m_locks

protected Hashtable m_locks
Locks, keyed by reference, holding Connections (or, if locks are done locally, holding an Edit).


m_caseInsensitive

protected boolean m_caseInsensitive
If set, we treat reasource ids as case insensitive.


m_sql

protected SqlService m_sql
Injected (by constructor) SqlService.

Constructor Detail

BaseDbSingleStorage

public BaseDbSingleStorage(String resourceTableName,
                           String resourceTableIdField,
                           String[] resourceTableOtherFields,
                           boolean locksInDb,
                           String resourceEntryName,
                           StorageUser user,
                           SqlService sqlService)
Construct.

Parameters:
resourceTableName - Table name for resources.
resourceTableIdField - The field in the resource table that holds the id.
resourceTableOtherFields - The other fields in the resource table (between the two id and the xml fields).
locksInDb - If true, we do our locks in the remote database, otherwise we do them here.
resourceEntryName - The xml tag name for the element holding each actual resource entry.
user - The StorageUser class to call back for creation of Resource and Edit objects.
sqlService - The SqlService.
Method Detail

open

public void open()
Open and be ready to read / write.


close

public void close()
Close.


readResource

protected Entity readResource(String xml)
Read one Resource from xml

Parameters:
xml - An string containing the xml which describes the resource.
Returns:
The Resource object created from the xml.

checkResource

public boolean checkResource(String id)
Check if a Resource by this id exists.

Parameters:
id - The id.
Returns:
true if a Resource by this id exists, false if not.

getResource

public Entity getResource(String id)
Get the Resource with this id, or null if not found.

Parameters:
id - The id.
Returns:
The Resource with this id, or null if not found.

isEmpty

public boolean isEmpty()

getAllResources

public List getAllResources()

getAllResources

public List getAllResources(int first,
                            int last)

countAllResources

public int countAllResources()

countSelectedResourcesWhere

public int countSelectedResourcesWhere(String sqlWhere)

getAllResourcesWhere

public List getAllResourcesWhere(String field,
                                 String value)
Get all Resources where the given field matches the given value.

Parameters:
field - The db field name for the selection.
value - The value to select.
Returns:
The list of all Resources that meet the criteria.

loadResources

protected List loadResources(String sql,
                             Object[] fields)

getAllResourcesWhereLike

public List getAllResourcesWhereLike(String field,
                                     String value)

getSelectedResources

public List getSelectedResources(Filter filter)
Get selected Resources, filtered by a test on the id field

Parameters:
filter - A filter to select what gets returned.
Returns:
The list of selected Resources.

getSelectedResourcesWhere

public List getSelectedResourcesWhere(String sqlWhere)
Get selected Resources, using a supplied where clause

Parameters:
sqlWhere - The SQL where clause.
Returns:
The list of selected Resources.

putResource

public Edit putResource(String id,
                        Object[] others)
Add a new Resource with this id.

Parameters:
id - The id.
others - Other fields for the newResource call
Returns:
The locked Resource object with this id, or null if the id is in use.

putDeleteResource

public Edit putDeleteResource(String id,
                              String uuid,
                              String userId,
                              Object[] others)
store the record in content_resource_delete table along with resource_uuid and date


insertDeleteFields

protected String insertDeleteFields(String before,
                                    String[] fields,
                                    String uuid,
                                    String date,
                                    String userId,
                                    String after)
Construct the SQL statement


commitDeleteResource

public void commitDeleteResource(Edit edit,
                                 String uuid)
update XML attribute on properties and remove locks


editResource

public Edit editResource(String id)
Get a lock on the Resource with this id, or null if a lock cannot be gotten.

Parameters:
id - The user id.
Returns:
The locked Resource with this id, or null if this records cannot be locked.

commitResource

public void commitResource(Edit edit)
Commit the changes and release the lock.


cancelResource

public void cancelResource(Edit edit)
Cancel the changes and release the lock.


removeResource

public void removeResource(Edit edit)
Remove this (locked) Resource.


valuesParams

protected String valuesParams(String[] fields)
Form a string of n question marks with commas, for sql value statements, one for each item in the values array, or an empty string if null.

Returns:
A sql statement fragment for the values part of an insert, one for each value in the array.

updateSet

protected String updateSet(String[] fields)
Form a string of n name=?, for sql update set statements, one for each item in the values array, or an empty string if null.

Returns:
A sql statement fragment for the values part of an insert, one for each value in the array.

insertFields

protected String insertFields(String before,
                              String[] fields,
                              String after)
Form a string of (field, field, field), for sql insert statements, one for each item in the fields array, plus one before, and one after.

Parameters:
before - The first field name.
after - The last field name.
Returns:
A sql statement fragment for the insert fields.

caseId

protected String caseId(String id)
Fix the case of resource ids to support case insensitive ids if enabled

Returns:
The id, case modified as needed.

setCaseInsensitivity

protected void setCaseInsensitivity(boolean setting)
Enable / disable case insensitive ids.

Parameters:
setting - true to set case insensitivity, false to set case sensitivity.