com.compomics.util.db
Class ObjectsDB

java.lang.Object
  extended by com.compomics.util.db.ObjectsDB
All Implemented Interfaces:
Serializable

public class ObjectsDB
extends Object
implements Serializable

A database which can easily be used to store objects.

Author:
Marc Vaudel
See Also:
Serialized Form

Constructor Summary
ObjectsDB(String folder, String dbName, boolean deleteOldDatabase, ObjectsCache objectsCache)
          Constructor.
 
Method Summary
 void addTable(String tableName)
          Adds the desired table in the database.
 void close()
          Closes the db connection.
 String correctTableName(String tableName)
          Surrounds the table name with quotation marks such that spaces etc are allowed.
 void deleteObject(String tableName, String objectKey)
          Deletes an object from the desired table.
 void establishConnection(String aDbFolder, boolean deleteOldDatabase, ObjectsCache objectsCache)
          Establishes connection to the database.
 String getName()
          Returns the database name.
 ObjectsCache getObjectsCache()
          Returns the cache used by this database.
 boolean inDB(String tableName, String objectKey, boolean cache)
          Indicates whether an object is loaded in the given table.
 void insertObject(String tableName, String objectKey, Object object, boolean inCache)
          Stores an object in the desired table.
 void insertObjects(String tableName, HashMap<String,Object> objects, WaitingHandler waitingHandler)
          Inserts a set of objects in the given table.
 void loadObjects(String tableName, ProgressDialogX progressDialog)
          Loads all objects from a table in the cache.
 Object retrieveObject(String tableName, String objectKey)
          Retrieves an object from the desired table.
 void setObjectCache(ObjectsCache objectCache)
          Sets the object cache to be used by this database.
 ArrayList<String> tableContent(String tableName)
          Returns an arraylist with the content in the given table.
 void updateObject(String tableName, String objectKey, Object object)
          Updates an object in the cache or in the tables if not in cache.
 void updateObject(String tableName, String objectKey, Object object, boolean cache)
          Updates an object in the cache or in the tables if not in cache or if cache is wrong.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectsDB

public ObjectsDB(String folder,
                 String dbName,
                 boolean deleteOldDatabase,
                 ObjectsCache objectsCache)
          throws SQLException
Constructor.

Parameters:
folder - absolute path of the folder where to establish the database
dbName - name of the database
deleteOldDatabase - if true, tries to delete the old database
objectsCache -
Throws:
SQLException
Method Detail

getName

public String getName()
Returns the database name.

Returns:
the database name

getObjectsCache

public ObjectsCache getObjectsCache()
Returns the cache used by this database.

Returns:
the cache used by this database

setObjectCache

public void setObjectCache(ObjectsCache objectCache)
Sets the object cache to be used by this database.

Parameters:
objectCache - the object cache to be used by this database

addTable

public void addTable(String tableName)
              throws SQLException
Adds the desired table in the database.

Parameters:
tableName - the name of the table
Throws:
SQLException - exception thrown whenever a problem occurred while working with the database

insertObject

public void insertObject(String tableName,
                         String objectKey,
                         Object object,
                         boolean inCache)
                  throws SQLException,
                         IOException
Stores an object in the desired table. When multiple objects are to be inserted, use insertObjects instead.

Parameters:
tableName - the name of the table
objectKey - the key of the object
object - the object to store
inCache - boolean indicating whether the method shall try to put the object in cache or not
Throws:
SQLException - exception thrown whenever an error occurred while storing the object
IOException - exception thrown whenever an error occurred while writing in the database

insertObjects

public void insertObjects(String tableName,
                          HashMap<String,Object> objects,
                          WaitingHandler waitingHandler)
                   throws SQLException,
                          IOException
Inserts a set of objects in the given table.

Parameters:
tableName - the name of the table
objects - map of the objects (object key -> object)
waitingHandler - a waiting handler displaying the progress (can be null). The progress will be displayed on the secondary progress bar.
Throws:
SQLException
IOException

loadObjects

public void loadObjects(String tableName,
                        ProgressDialogX progressDialog)
                 throws SQLException,
                        IOException,
                        ClassNotFoundException
Loads all objects from a table in the cache.

Parameters:
tableName - the table name
progressDialog - the progress dialog
Throws:
SQLException - exception thrown whenever an error occurred while interrogating the database
IOException - exception thrown whenever an error occurred while reading the database
ClassNotFoundException - exception thrown whenever the class of the object is not found when deserializing it.

retrieveObject

public Object retrieveObject(String tableName,
                             String objectKey)
                      throws SQLException,
                             IOException,
                             ClassNotFoundException
Retrieves an object from the desired table. The key should be unique otherwise the first object will be returned. Returns null if the key is not found.

Parameters:
tableName - the name of the table
objectKey - the object key
Returns:
the object stored in the table.
Throws:
SQLException - exception thrown whenever an error occurred while interrogating the database
IOException - exception thrown whenever an error occurred while reading the database
ClassNotFoundException - exception thrown whenever the class of the object is not found when deserializing it.

inDB

public boolean inDB(String tableName,
                    String objectKey,
                    boolean cache)
             throws SQLException
Indicates whether an object is loaded in the given table.

Parameters:
tableName - the table name
objectKey - the object key
cache - a boolean indicating whether the cache should be searched as well
Returns:
a boolean indicating whether an object is loaded in the given table
Throws:
SQLException - exception thrown whenever an exception occurred while interrogating the database

tableContent

public ArrayList<String> tableContent(String tableName)
                               throws SQLException
Returns an arraylist with the content in the given table.

Parameters:
tableName - the table to get the content for
Returns:
an arraylist with the content in the given tabl
Throws:
SQLException

deleteObject

public void deleteObject(String tableName,
                         String objectKey)
                  throws SQLException,
                         IOException
Deletes an object from the desired table.

Parameters:
tableName - the name of the table
objectKey - the object key
Throws:
SQLException - exception thrown whenever an error occurred while interrogating the database
IOException - exception thrown whenever an error occurred while interrogating the database

updateObject

public void updateObject(String tableName,
                         String objectKey,
                         Object object)
                  throws SQLException,
                         IOException
Updates an object in the cache or in the tables if not in cache.

Parameters:
tableName - the name of the table
objectKey - the key of the object
object - the object to store
Throws:
SQLException - exception thrown whenever an error occurred while storing the object
IOException - exception thrown whenever an error occurred while writing in the database

updateObject

public void updateObject(String tableName,
                         String objectKey,
                         Object object,
                         boolean cache)
                  throws SQLException,
                         IOException
Updates an object in the cache or in the tables if not in cache or if cache is wrong.

Parameters:
tableName - the name of the table
objectKey - the key of the object
object - the object to store
cache - a boolean indicating whether the method should look in the cache
Throws:
SQLException - exception thrown whenever an error occurred while storing the object
IOException - exception thrown whenever an error occurred while writing in the database

close

public void close()
           throws SQLException
Closes the db connection.

Throws:
SQLException - exception thrown whenever an error occurred while closing the database connection

establishConnection

public void establishConnection(String aDbFolder,
                                boolean deleteOldDatabase,
                                ObjectsCache objectsCache)
                         throws SQLException
Establishes connection to the database.

Parameters:
aDbFolder - the folder where the database is located
deleteOldDatabase - if true, tries to delete the old database
objectsCache -
Throws:
SQLException - exception thrown whenever an error occurred while establishing the connection

correctTableName

public String correctTableName(String tableName)
Surrounds the table name with quotation marks such that spaces etc are allowed.

Parameters:
tableName - the table name
Returns:
the corrected table name


Copyright © 2012. All Rights Reserved.