org.fosstrak.llrp.client.repository.sql
Class AbstractSQLRepository

java.lang.Object
  extended by org.fosstrak.llrp.client.repository.sql.AbstractSQLRepository
All Implemented Interfaces:
Repository
Direct Known Subclasses:
DerbyRepository, MySQLRepository, PostgreSQLRepository

public abstract class AbstractSQLRepository
extends java.lang.Object
implements Repository

The AbstractSQLRepository represents a common super class for all SQL based Repositories. The class implements the different methods like put(LLRPMessageItem) or get(String) in an abstract manner, using the strategy pattern. We explain the idea below:
Two examples:

  1. Create the table to store the LLRP messages:
    Statement sCreateTable = conn.createStatement();
    sCreateTable.execute(sqlCreateTable());
    sCreateTable.close();
    As you can see, the AbstractSQLRepository runs on a prepared statement (in this case a normal SQL statement would do as well), but this statement is not hard-coded. The create SQL is obtained via the method sqlCreateTable(). Depending on the implementing data base, different SQL statements for the create instruction can be used. Example: a derby implementation DerbyRepository uses a different create SQL than a MySQLRepository (as the data types differ).
  2. Clear all the messages received by an adapter:
    PreparedStatement psRemove = conn.prepareStatement(
         sqlRemoveAllAdapterMessages());
    psRemove.setString(1, adapter);
    psRemove.executeUpdate();
    psRemove.close();
    Here the AbstractSQLRepository executes a prepared statement. The name of the adapter to be cleared, is injected from the code in the AbstractSQLRepository. Again, the actual query is delivered from the implementing strategy (example: MySQLRepository).

NOTICE:

By default, the AbstractSQLRepository uses the SQL commands tailored to the Derby database (as this is the default internal database). So, if you do not override certain SQL "queries-getter", please be aware that this might cause trouble with a database differing from Derby.

Author:
sawielan

Field Summary
protected  java.util.Map<java.lang.String,java.lang.String> args
          map with additional arguments to be passed to the initializer.
protected  java.sql.Connection conn
          the JDBC connection.
protected  java.lang.String connectURL
          the connection URL.
static java.lang.String DB_DRIVER
          the database driver to use.
static java.lang.String DB_NAME
          the name of the database in the database server.
protected  boolean isHealth
          whether the repository is healthy or not.
protected  boolean logROAccess
          whether to log RO_ACCESS_REPORT.
protected static int NUM_TABLE_COLUMNS
          the number of table columns.
protected  java.lang.String password
          the database password.
static int SELECTOR_ADAPTOR
          column index of the adapter name.
static int SELECTOR_COMMENT
          column index of the comment field.
static int SELECTOR_CONTENT
          column index of the comment column.
static int SELECTOR_ID
          column index of the ID.
static int SELECTOR_MARK
          column index of the mark.
static int SELECTOR_MESSAGE_TYPE
          column index of the message type.
static int SELECTOR_READER
          column index of the reader name.
static int SELECTOR_STATUS
          column index of the status flag.
static int SELECTOR_TIMESTAMP
          column index of the time-stamp column.
static java.lang.String TABLE_LLRP_REPOSITORY
          the name of the LLRP message repository table.
protected  java.lang.String username
          the database user name.
protected  boolean wipe
          whether to wipe the database at startup or not.
protected  boolean wipeROAccess
          whether to wipe the RO_ACCESS_REPORTS database at startup or not.
 
Fields inherited from interface org.fosstrak.llrp.client.Repository
RETRIEVE_ALL
 
Constructor Summary
AbstractSQLRepository()
           
 
Method Summary
 void clearAdapter(java.lang.String adapter)
          clear the repository from entries to a given adapter.
 void clearAll()
          remove all the messages from the repository.
 void clearReader(java.lang.String adapter, java.lang.String reader)
          clear the repository from entries to a given adapter and a given reader.
 void close()
          Close the database connection.
 int count(java.lang.String adaptor, java.lang.String reader)
          the method computes the number of messages stored in the repository depending on the input parameters: (adaptor == null) then compute all messages in the repository.
protected  void createTable()
          generates the necessary tables.
protected  void dropTable()
          drops the table.
protected  boolean existsTable()
          checks whether the required tables exist or not.
 LLRPMessageItem get(java.lang.String aMsgSysId)
          Get the LLRP Message Item from repository according to the unique Message ID.
 java.util.ArrayList<LLRPMessageItem> get(java.lang.String adaptorName, java.lang.String readerName, int num, boolean content)
          returns all the messages from the specified adaptor and the reader limited by num. if you set num to RETRIEVE_ALL all messages get returned.
 java.util.Map<java.lang.String,java.lang.String> getArgs()
           
 java.sql.Connection getDBConnection()
           
protected  java.lang.String getDBDriver()
          Returns the class name of the JDBC driver to be used.
 void initialize(java.util.Map<java.lang.String,java.lang.String> args)
          Initializer method for the repository.
 boolean isHealth()
           
protected  boolean loadDriver()
          Loads the appropriate JDBC driver for this environment/framework.
protected abstract  java.sql.Connection openConnection()
          Opens the JDBC connection to the database.
 void put(LLRPMessageItem aMessage)
          store an LLRP message into the repository.
protected  java.lang.String sqlCreateTable()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlDropTable()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlInsertMessage()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlRemoveAllAdapterMessages()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlRemoveAllMessages()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlRemoveAllReaderMessages()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlSelectByAdapterAndReaderWithContent()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlSelectByAdapterAndReaderWithoutContent()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlSelectByAdapterWithContent()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlSelectByAdapterWithoutContent()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlSelectMessageByID()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlSelectMessagesWithContent()
          NOTICE: this SQL command corresponds to derby SQL!.
protected  java.lang.String sqlSelectMessagesWithoutContent()
          NOTICE: this SQL command corresponds to derby SQL!.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.fosstrak.llrp.client.Repository
getROAccessRepository
 

Field Detail

SELECTOR_ID

public static final int SELECTOR_ID
column index of the ID.

See Also:
Constant Field Values

SELECTOR_MESSAGE_TYPE

public static final int SELECTOR_MESSAGE_TYPE
column index of the message type.

See Also:
Constant Field Values

SELECTOR_READER

public static final int SELECTOR_READER
column index of the reader name.

See Also:
Constant Field Values

SELECTOR_ADAPTOR

public static final int SELECTOR_ADAPTOR
column index of the adapter name.

See Also:
Constant Field Values

SELECTOR_TIMESTAMP

public static final int SELECTOR_TIMESTAMP
column index of the time-stamp column.

See Also:
Constant Field Values

SELECTOR_STATUS

public static final int SELECTOR_STATUS
column index of the status flag.

See Also:
Constant Field Values

SELECTOR_COMMENT

public static final int SELECTOR_COMMENT
column index of the comment field.

See Also:
Constant Field Values

SELECTOR_MARK

public static final int SELECTOR_MARK
column index of the mark.

See Also:
Constant Field Values

SELECTOR_CONTENT

public static final int SELECTOR_CONTENT
column index of the comment column.

See Also:
Constant Field Values

DB_NAME

public static final java.lang.String DB_NAME
the name of the database in the database server.

See Also:
Constant Field Values

TABLE_LLRP_REPOSITORY

public static final java.lang.String TABLE_LLRP_REPOSITORY
the name of the LLRP message repository table.

See Also:
Constant Field Values

isHealth

protected boolean isHealth
whether the repository is healthy or not.


NUM_TABLE_COLUMNS

protected static final int NUM_TABLE_COLUMNS
the number of table columns.

See Also:
Constant Field Values

conn

protected java.sql.Connection conn
the JDBC connection.


DB_DRIVER

public static final java.lang.String DB_DRIVER
the database driver to use. NOTICE: the default is derby!.

See Also:
Constant Field Values

username

protected java.lang.String username
the database user name.


password

protected java.lang.String password
the database password.


connectURL

protected java.lang.String connectURL
the connection URL.


wipe

protected boolean wipe
whether to wipe the database at startup or not.


wipeROAccess

protected boolean wipeROAccess
whether to wipe the RO_ACCESS_REPORTS database at startup or not.


logROAccess

protected boolean logROAccess
whether to log RO_ACCESS_REPORT.


args

protected java.util.Map<java.lang.String,java.lang.String> args
map with additional arguments to be passed to the initializer.

Constructor Detail

AbstractSQLRepository

public AbstractSQLRepository()
Method Detail

sqlCreateTable

protected java.lang.String sqlCreateTable()
NOTICE: this SQL command corresponds to derby SQL!.

Returns:
a SQL command that creates the table.

sqlRemoveAllMessages

protected java.lang.String sqlRemoveAllMessages()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that erases all the LLRP messages.

sqlDropTable

protected java.lang.String sqlDropTable()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that drops the LLRP message table.

sqlRemoveAllAdapterMessages

protected java.lang.String sqlRemoveAllAdapterMessages()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that removes all the messages that belong to a given adapter.

sqlRemoveAllReaderMessages

protected java.lang.String sqlRemoveAllReaderMessages()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that removes all the messages that belong to a given reader.

sqlInsertMessage

protected java.lang.String sqlInsertMessage()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that inserts a new item into the database.

sqlSelectMessageByID

protected java.lang.String sqlSelectMessageByID()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that selects an item by its ID.

sqlSelectMessagesWithContent

protected java.lang.String sqlSelectMessagesWithContent()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that selects all the messages with the content.

sqlSelectMessagesWithoutContent

protected java.lang.String sqlSelectMessagesWithoutContent()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that selects all the messages without the content.

sqlSelectByAdapterWithContent

protected java.lang.String sqlSelectByAdapterWithContent()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that selects all the messages to a given adapter with the content.

sqlSelectByAdapterWithoutContent

protected java.lang.String sqlSelectByAdapterWithoutContent()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that selects all the messages to a given adapter without the content.

sqlSelectByAdapterAndReaderWithContent

protected java.lang.String sqlSelectByAdapterAndReaderWithContent()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that selects all the messages to a given adapter and a given reader with the content.

sqlSelectByAdapterAndReaderWithoutContent

protected java.lang.String sqlSelectByAdapterAndReaderWithoutContent()
NOTICE: this SQL command corresponds to derby SQL!. So override, if your database uses different SQL instructions.

Returns:
a SQL command that selects all the messages to a given adapter and a given reader without the content.

loadDriver

protected boolean loadDriver()
Loads the appropriate JDBC driver for this environment/framework.

Returns:
true if the loading went fine, false otherwise.

getDBDriver

protected java.lang.String getDBDriver()
Returns the class name of the JDBC driver to be used. NOTICE : you should override this method if you use a database other than derby.

Returns:
a class name of the JDBC driver to be used.

openConnection

protected abstract java.sql.Connection openConnection()
                                               throws java.lang.Exception
Opens the JDBC connection to the database.

Returns:
a handle to the Connection item.
Throws:
java.lang.Exception - whenever the connection could not be established.

initialize

public void initialize(java.util.Map<java.lang.String,java.lang.String> args)
                throws LLRPRuntimeException
Description copied from interface: Repository
Initializer method for the repository. The method will be called directly after instantiation.

Specified by:
initialize in interface Repository
Parameters:
args - hash-map with the parameters.
Throws:
LLRPRuntimeException

getArgs

public java.util.Map<java.lang.String,java.lang.String> getArgs()
Specified by:
getArgs in interface Repository
Returns:
a hash map with the properties passed by the initializer.

existsTable

protected boolean existsTable()
checks whether the required tables exist or not.

Returns:
true if everything is ok, false otherwise.

dropTable

protected void dropTable()
drops the table.


createTable

protected void createTable()
generates the necessary tables.


put

public void put(LLRPMessageItem aMessage)
store an LLRP message into the repository.

Specified by:
put in interface Repository
Parameters:
aMessage - the message to be stored.

clearAll

public void clearAll()
remove all the messages from the repository.

Specified by:
clearAll in interface Repository

isHealth

public boolean isHealth()
Specified by:
isHealth in interface Repository
Returns:
true if the repository is ok, false otherwise.

count

public int count(java.lang.String adaptor,
                 java.lang.String reader)
the method computes the number of messages stored in the repository depending on the input parameters:
  1. (adaptor == null) then compute all messages in the repository.
  2. (adaptor != null) && (reader == null) then compute all the messages for the adapter ignoring the name of the reader.
  3. (adaptor != null) && (reader != null) then compute all the messages for the adapter where the reader name is equal to reader.

Specified by:
count in interface Repository
Parameters:
adaptor - the name of the adapter.
reader - the name of the reader.
Returns:
the number of messages stored in the repository.

clearAdapter

public void clearAdapter(java.lang.String adapter)
clear the repository from entries to a given adapter.

Specified by:
clearAdapter in interface Repository
Parameters:
adapter - the name of the adapter to clean out.

clearReader

public void clearReader(java.lang.String adapter,
                        java.lang.String reader)
clear the repository from entries to a given adapter and a given reader.

Specified by:
clearReader in interface Repository
Parameters:
adapter - the name of the adapter.
reader - the name of the reader.

close

public void close()
Close the database connection.

Specified by:
close in interface Repository

get

public java.util.ArrayList<LLRPMessageItem> get(java.lang.String adaptorName,
                                                java.lang.String readerName,
                                                int num,
                                                boolean content)
returns all the messages from the specified adaptor and the reader limited by num. if you set num to RETRIEVE_ALL all messages get returned. if you set readerName to null, all the messages of all the readers with adaptor adaptorName will be returned.

Specified by:
get in interface Repository
Parameters:
adaptorName - the name of the adaptor.
readerName - the name of the reader.
num - how many messages to retrieve.
content - if true retrieve the message content, false no content.
Returns:
a list of messages.

get

public LLRPMessageItem get(java.lang.String aMsgSysId)
Description copied from interface: Repository
Get the LLRP Message Item from repository according to the unique Message ID.

Specified by:
get in interface Repository
Parameters:
aMsgSysId - the message id of the item to be retrieved.
Returns:
the LLRP message to the given message id.

getDBConnection

public java.sql.Connection getDBConnection()
Specified by:
getDBConnection in interface Repository
Returns:
a handle to the database connection. users of the repository are allowed to use the database for their own purposes.


Copyright © 2009. All Rights Reserved.