org.salespointframework.core.data.database
Class PersistenceManager

java.lang.Object
  extended by org.salespointframework.core.data.database.PersistenceManager
All Implemented Interfaces:
java.beans.PropertyChangeListener, java.util.EventListener

public class PersistenceManager
extends java.lang.Object
implements java.beans.PropertyChangeListener

The PersistenceManager is the core of SalesPoints Persistence layer Its job is to manage the database connections and persist and recover objects

Since:
4.0
Author:
Thomas Kissinger

Nested Class Summary
static class PersistenceManager.LogonInformation
          Logon information of users in local Salespoints should work with remote logons in future version
static class PersistenceManager.PerformanceTimer
          Simple Performance timer with nano seconds precision
 
Field Summary
 java.util.logging.Logger logger
          The logger
static int MODIFICATION_TRIGGER_WORKER_ENTRY_REMOVE_MULTIPLIER
          defines after how much cycles a ModificationEntry should be removed from modification table
static long MODIFICATION_TRIGGER_WORKER_INTERVAL
          interval in which ModificationTriggerWorker checks for changes in database
 
Constructor Summary
PersistenceManager()
          Constructor
 
Method Summary
 boolean addDatasourceChangedListener(DatasourceChangeListener dscl)
          Add a Datasource changed listener
 boolean addDatasourceOnChangeListener(DatasourceOnChangeListener docl)
          Add a Datasource on change listener
 boolean addDBCListener(DatabaseConnectionListener dbcl)
          Add as database connection listener
 boolean addPersistenceStateListener(PersistenceStateListener psl)
          Add a Persistence state Listener
 java.lang.Object addToList(java.lang.Class c, java.lang.String id, java.lang.Class oc, java.lang.Class kc, java.lang.Object o, java.lang.Object k, boolean uniqueKey, java.lang.Object host, int index, boolean newIndex)
          Core function for persisting lists. this method persists items to the list.
 boolean attachExternalModificationListener(java.lang.String tableid, ExternalModificationListener eml)
          add an ExternalModificationListener
 void clearDatabase()
          Clears the Database
 void close()
          closes the JDBC connection
 int countListItems(java.lang.Class c, java.lang.String id, java.lang.Class oc, java.lang.Class kc, java.lang.Object k, boolean uniqueKey, java.lang.Object host, java.lang.Object o)
          Core function for persisting lists. this method counts the items of a list.
 void dettachExternalModificationListener(java.lang.String tableid, ExternalModificationListener eml)
          remove ExternalModificationListener
protected  java.lang.Object doTypeChecking(java.lang.Object odata, java.lang.Class c)
           
 void fireDBCUpdate()
          notifies connection listeners about an update
 boolean forceOpen()
          Enforces the connection opening
 java.util.List<DatabaseConnection> getConnections()
          returns a read only list of regsitered database connections
 java.util.List<DatabaseConnectionTemplate> getConnectionTemplates()
          returns the list of Database connection templates
 DatabaseConnection getDatabaseConnection()
          returns the current database connection
 java.util.List<java.lang.Object> getEntireList(java.lang.Class c, java.lang.String id, java.lang.Class oc, java.lang.Class kc, boolean uniqueKey, boolean returnKeys, java.lang.Object RecoveryContext, java.lang.Object host, java.lang.Object k, boolean isMap)
          Core function for persisting lists. this method recovers the entire list.
 java.util.List<java.lang.Object> getFromList(java.lang.Class c, java.lang.String id, java.lang.Class oc, java.lang.Class kc, java.lang.Object k, boolean uniqueKey, java.lang.Object recoveryContext, java.lang.Object host, java.lang.Object ident)
          Core function for persisting lists. this method recovers a set of entry objects from the list can be used for Maps as well as Lists
static PersistenceManager getInstance()
          get the singleton instance
 java.util.Map<java.lang.Object,java.lang.Object> getLargeList(java.lang.Class c, java.lang.String id, java.lang.Class oc, java.lang.Class kc, boolean uniqueKey, java.lang.Object RecoveryContext, java.lang.Object host, java.lang.Object k, boolean isMap)
          Core function for persisting lists. this method recovers the entire list.
 java.lang.String getListId(java.lang.Class c, java.lang.String id, java.lang.Object host)
          build table name for list
 java.lang.String getListId(java.lang.Class c, java.lang.String id, java.lang.Object host, boolean raw)
           
 java.util.Map<java.lang.String,java.lang.Object> getObjectCache()
          get the object cache, which caches all recovered objects of current session
 java.util.Set<java.lang.String> getTableCache()
          get the table cache, which contains all verified tables of current session
 java.util.List<PersistenceManager.LogonInformation> getUserSessions()
          returns a list of all PersistenceManager.LogonInformation
 java.util.List<PersistenceManager.LogonInformation> getUserSessions(User user)
          returns a list of all PersistenceManager.LogonInformation for given user
 void initalizeData()
          fill database data
 boolean isOpen()
          determines whether the JDBC connection is open
 boolean isPerformanceMode()
          Sets Performance mode.
 void loadConnections()
          Loads connections from PreferenceStorage
 void logoffUser(SalesPoint sp)
          logoff user form given SalesPoint and remove PersistenceManager.LogonInformation record
 boolean logonUser(User user, SalesPoint sp)
           
 void markTableAsDirty(java.lang.String tname)
          writes a ModificationEntry that marks given table as modified
 boolean open()
          tries to open the JDBC connection, with help of the dbConnection
 java.lang.Object persist(java.lang.Object o)
          Persist an object to DataSource
protected  void prepareListTable(java.lang.String tname, java.lang.Class o, java.lang.Class k, boolean uniqueKey)
          Prepares a table for a list, if table does not exist
protected  void prepareTable(java.lang.Class c, java.lang.Object o)
          Create a table for a Class, if table doesn't exist
 void propertyChange(java.beans.PropertyChangeEvent evt)
          react on property change
 boolean realOpen()
          this method really tries to open the connection
<T> T
recover(java.lang.Class<T> c, java.lang.Object ident, java.lang.Object recoveryContext)
          Recovers an object identified by ident.
 void refireCSChanged()
          fire the current connection state again
 boolean registerConnection(DatabaseConnection dbc)
          registers an new Database connection
 void registerShop(Shop shop)
          register shop instance
 void removeDatasourceChangedListener(DatasourceChangeListener dscl)
          Remove a Datasource changed listener
 void removeDatasourceOnChangeListener(DatasourceOnChangeListener docl)
          Remove a datasource on change listener
 void removeDBCListener(DatabaseConnectionListener dbcl)
          Removes a database connection listener
 void removeListItems(java.lang.Class c, java.lang.String id, java.lang.Class oc, java.lang.Class kc, java.lang.Object k, boolean uniqueKey, java.lang.Object host, java.lang.Object o, java.lang.String ctr, java.lang.Object ctro, boolean removeItem, int index)
          Core function for persisting lists. this method removes items from a list can be used for Maps as well as Lists
 void removePersistenceStateListener(PersistenceStateListener psl)
          Remove a persistence state listener
 void saveConnections()
          Saves Connections to PrferenceStorage
 void setBroadcastStateChanged(boolean val)
          set broadcastStateChanged
 void setClassFieldMapper(ClassFieldMapper cfm)
          sets the current ClassFieldMapper
 void setClassNameEncoder(ClassNameEncoder cne)
          Sets the current ClassNameEncoder
 void setConsoleLogLevel(java.util.logging.Level level)
          Set the log level for Console output
 void setDatabaseConnection(DatabaseConnection dbc)
          set the current database connection
 void setLogStream(java.io.PrintWriter log)
          Set the LogStream for DriverManager
 void setPerformanceMode(boolean value)
          Set performance mode
 void unpersist(java.lang.Class c, java.lang.Object ident)
          Removes an Object from DataSource
 void unregisterConnection(DatabaseConnection dbc)
          removes a database connection
 void unregisterShop()
          unregister shop from datasource
protected  java.lang.Object upsert(java.lang.Object o, java.lang.Class c)
          insert or update an object in persistent storage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MODIFICATION_TRIGGER_WORKER_INTERVAL

public static long MODIFICATION_TRIGGER_WORKER_INTERVAL
interval in which ModificationTriggerWorker checks for changes in database


MODIFICATION_TRIGGER_WORKER_ENTRY_REMOVE_MULTIPLIER

public static int MODIFICATION_TRIGGER_WORKER_ENTRY_REMOVE_MULTIPLIER
defines after how much cycles a ModificationEntry should be removed from modification table


logger

public final java.util.logging.Logger logger
The logger

Constructor Detail

PersistenceManager

public PersistenceManager()
Constructor

Method Detail

setConsoleLogLevel

public void setConsoleLogLevel(java.util.logging.Level level)
Set the log level for Console output


getInstance

public static PersistenceManager getInstance()
get the singleton instance

Returns:
the PersistenceManager

setClassFieldMapper

public void setClassFieldMapper(ClassFieldMapper cfm)
sets the current ClassFieldMapper

Parameters:
cfm -

setClassNameEncoder

public void setClassNameEncoder(ClassNameEncoder cne)
Sets the current ClassNameEncoder

Parameters:
cne -

setLogStream

public void setLogStream(java.io.PrintWriter log)
Set the LogStream for DriverManager

Parameters:
log -

isPerformanceMode

public boolean isPerformanceMode()
Sets Performance mode. In performance mode, persistence manager tries to load all object at once.


setPerformanceMode

public void setPerformanceMode(boolean value)
Set performance mode

Parameters:
value - true to activate

isOpen

public boolean isOpen()
determines whether the JDBC connection is open

Returns:

close

public void close()
closes the JDBC connection


addDatasourceOnChangeListener

public boolean addDatasourceOnChangeListener(DatasourceOnChangeListener docl)
Add a Datasource on change listener


removeDatasourceOnChangeListener

public void removeDatasourceOnChangeListener(DatasourceOnChangeListener docl)
Remove a datasource on change listener


addDatasourceChangedListener

public boolean addDatasourceChangedListener(DatasourceChangeListener dscl)
Add a Datasource changed listener


removeDatasourceChangedListener

public void removeDatasourceChangedListener(DatasourceChangeListener dscl)
Remove a Datasource changed listener


addPersistenceStateListener

public boolean addPersistenceStateListener(PersistenceStateListener psl)
Add a Persistence state Listener

Parameters:
psl - the listener

removePersistenceStateListener

public void removePersistenceStateListener(PersistenceStateListener psl)
Remove a persistence state listener

Parameters:
the - listener

setBroadcastStateChanged

public void setBroadcastStateChanged(boolean val)
set broadcastStateChanged

Parameters:
val -

refireCSChanged

public void refireCSChanged()
fire the current connection state again


loadConnections

public void loadConnections()
Loads connections from PreferenceStorage


saveConnections

public void saveConnections()
Saves Connections to PrferenceStorage


getTableCache

public java.util.Set<java.lang.String> getTableCache()
get the table cache, which contains all verified tables of current session

Returns:

getObjectCache

public java.util.Map<java.lang.String,java.lang.Object> getObjectCache()
get the object cache, which caches all recovered objects of current session

Returns:

getConnectionTemplates

public java.util.List<DatabaseConnectionTemplate> getConnectionTemplates()
returns the list of Database connection templates

Returns:

getConnections

public java.util.List<DatabaseConnection> getConnections()
returns a read only list of regsitered database connections

Returns:

addDBCListener

public boolean addDBCListener(DatabaseConnectionListener dbcl)
Add as database connection listener

Parameters:
dbcl -
Returns:
true on success

removeDBCListener

public void removeDBCListener(DatabaseConnectionListener dbcl)
Removes a database connection listener

Parameters:
dbcl -

fireDBCUpdate

public void fireDBCUpdate()
notifies connection listeners about an update


registerConnection

public boolean registerConnection(DatabaseConnection dbc)
registers an new Database connection

Parameters:
dbc - the connection to be registered
Returns:
true on success

unregisterConnection

public void unregisterConnection(DatabaseConnection dbc)
removes a database connection

Parameters:
dbc - connection to be removed

forceOpen

public boolean forceOpen()
Enforces the connection opening

Returns:

open

public boolean open()
tries to open the JDBC connection, with help of the dbConnection

Returns:
true on success

realOpen

public boolean realOpen()
this method really tries to open the connection

Returns:
true on success

clearDatabase

public void clearDatabase()
Clears the Database


initalizeData

public void initalizeData()
fill database data


doTypeChecking

protected java.lang.Object doTypeChecking(java.lang.Object odata,
                                          java.lang.Class c)

recover

public <T> T recover(java.lang.Class<T> c,
                     java.lang.Object ident,
                     java.lang.Object recoveryContext)
Recovers an object identified by ident.

Type Parameters:
T - expected class type
Parameters:
c - Class of object
ident - Identification of the object
recoveryContext - parameter which will be passed to post-recovering operations
Returns:
the fully recovered object

persist

public java.lang.Object persist(java.lang.Object o)
Persist an object to DataSource

Parameters:
o - Object to persist
Returns:
identification for recovery

unpersist

public void unpersist(java.lang.Class c,
                      java.lang.Object ident)
Removes an Object from DataSource

Parameters:
c - Class of Object to remove
ident - objects identification

upsert

protected java.lang.Object upsert(java.lang.Object o,
                                  java.lang.Class c)
insert or update an object in persistent storage

Parameters:
o - object to persist
c - class of object
Returns:
identification object

markTableAsDirty

public void markTableAsDirty(java.lang.String tname)
writes a ModificationEntry that marks given table as modified

Parameters:
tname - affected table

getLargeList

public java.util.Map<java.lang.Object,java.lang.Object> getLargeList(java.lang.Class c,
                                                                     java.lang.String id,
                                                                     java.lang.Class oc,
                                                                     java.lang.Class kc,
                                                                     boolean uniqueKey,
                                                                     java.lang.Object RecoveryContext,
                                                                     java.lang.Object host,
                                                                     java.lang.Object k,
                                                                     boolean isMap)
Core function for persisting lists. this method recovers the entire list. can be used for Maps as well as Lists used in performance mode

Parameters:
c - Class of calling List
id - Lists unique id
oc - Class of entry object
kc - class of key object
uniqueKey - guaranty max. one occurrence of a key
RecoveryContext -
host - host object
ident -
isMap -
Returns:
List of keys or entry objects

getEntireList

public java.util.List<java.lang.Object> getEntireList(java.lang.Class c,
                                                      java.lang.String id,
                                                      java.lang.Class oc,
                                                      java.lang.Class kc,
                                                      boolean uniqueKey,
                                                      boolean returnKeys,
                                                      java.lang.Object RecoveryContext,
                                                      java.lang.Object host,
                                                      java.lang.Object k,
                                                      boolean isMap)
Core function for persisting lists. this method recovers the entire list. can be used for Maps as well as Lists

Parameters:
c - Class of calling List
id - Lists unique id
oc - Class of entry object
kc - class of key object
uniqueKey - guaranty max. one occurrence of a key
returnKeys - true, if all keys should be returned. otherwise only entry objects will be returned
RecoveryContext -
host - host object
ident -
isMap -
Returns:
List of keys or entry objects

removeListItems

public void removeListItems(java.lang.Class c,
                            java.lang.String id,
                            java.lang.Class oc,
                            java.lang.Class kc,
                            java.lang.Object k,
                            boolean uniqueKey,
                            java.lang.Object host,
                            java.lang.Object o,
                            java.lang.String ctr,
                            java.lang.Object ctro,
                            boolean removeItem,
                            int index)
Core function for persisting lists. this method removes items from a list can be used for Maps as well as Lists

Parameters:
c - Class of calling list
id - unique id of calling list
oc - entry objects class
kc - key objects class
k - key to remove. if null: list will be cleared
uniqueKey - guaranty unique occurrence of key
host - host object
ident -

countListItems

public int countListItems(java.lang.Class c,
                          java.lang.String id,
                          java.lang.Class oc,
                          java.lang.Class kc,
                          java.lang.Object k,
                          boolean uniqueKey,
                          java.lang.Object host,
                          java.lang.Object o)
Core function for persisting lists. this method counts the items of a list. can be used for Maps as well as Lists

Parameters:
c - calling class's type
id - id of calling class
oc - entry objects class
kc - key objects class
k - keys to count. if null: all keys will be counted
uniqueKey - guaranty single occurrence of key
host - host object
ident -
Returns:
count of items

getFromList

public java.util.List<java.lang.Object> getFromList(java.lang.Class c,
                                                    java.lang.String id,
                                                    java.lang.Class oc,
                                                    java.lang.Class kc,
                                                    java.lang.Object k,
                                                    boolean uniqueKey,
                                                    java.lang.Object recoveryContext,
                                                    java.lang.Object host,
                                                    java.lang.Object ident)
Core function for persisting lists. this method recovers a set of entry objects from the list can be used for Maps as well as Lists

Parameters:
c - lists class
id - lists unique id
oc - objects class
kc - keys class
k - keys to recover
uniqueKey -
recoveryContext -
host - host object
ident -
Returns:
List of recovered objects

addToList

public java.lang.Object addToList(java.lang.Class c,
                                  java.lang.String id,
                                  java.lang.Class oc,
                                  java.lang.Class kc,
                                  java.lang.Object o,
                                  java.lang.Object k,
                                  boolean uniqueKey,
                                  java.lang.Object host,
                                  int index,
                                  boolean newIndex)
Core function for persisting lists. this method persists items to the list. can be used for Maps as well as Lists

Parameters:
c - lists class
id - lists unique id
oc - objects class
kc - keys class
o - object to persist
k - objects key. can be null, is autoKeys
uniqueKey - guaranty single occurrence of key
host - host element
Returns:
objects id

getListId

public java.lang.String getListId(java.lang.Class c,
                                  java.lang.String id,
                                  java.lang.Object host)
build table name for list

Parameters:
c - class of list
id - unique id of list
Returns:
table name

getListId

public java.lang.String getListId(java.lang.Class c,
                                  java.lang.String id,
                                  java.lang.Object host,
                                  boolean raw)

prepareListTable

protected void prepareListTable(java.lang.String tname,
                                java.lang.Class o,
                                java.lang.Class k,
                                boolean uniqueKey)
Prepares a table for a list, if table does not exist

Parameters:
tname - desired table name
o - class of entry object
k - class of key object
uniqueKey -

prepareTable

protected void prepareTable(java.lang.Class c,
                            java.lang.Object o)
Create a table for a Class, if table doesn't exist

Parameters:
c - objects class
o - object to persist

setDatabaseConnection

public void setDatabaseConnection(DatabaseConnection dbc)
set the current database connection

Parameters:
dbc -

getDatabaseConnection

public DatabaseConnection getDatabaseConnection()
returns the current database connection

Returns:

logonUser

public boolean logonUser(User user,
                         SalesPoint sp)

logoffUser

public void logoffUser(SalesPoint sp)
logoff user form given SalesPoint and remove PersistenceManager.LogonInformation record

Parameters:
sp -

getUserSessions

public java.util.List<PersistenceManager.LogonInformation> getUserSessions(User user)
returns a list of all PersistenceManager.LogonInformation for given user

Parameters:
user - user
Returns:

getUserSessions

public java.util.List<PersistenceManager.LogonInformation> getUserSessions()
returns a list of all PersistenceManager.LogonInformation

Returns:

registerShop

public void registerShop(Shop shop)
register shop instance

Parameters:
shop - shop to register

unregisterShop

public void unregisterShop()
unregister shop from datasource


attachExternalModificationListener

public boolean attachExternalModificationListener(java.lang.String tableid,
                                                  ExternalModificationListener eml)
add an ExternalModificationListener

Parameters:
tableid - table it is interested in
eml - the listener
Returns:
true on success

dettachExternalModificationListener

public void dettachExternalModificationListener(java.lang.String tableid,
                                                ExternalModificationListener eml)
remove ExternalModificationListener

Parameters:
tableid - table name
eml - the listener

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent evt)
react on property change

Specified by:
propertyChange in interface java.beans.PropertyChangeListener