Class AbstractBasicDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>

java.lang.Object
rs.data.impl.dao.AbstractBasicDAO<K,​C>
Type Parameters:
K - type of primary key
C - type of Business Object Interface
All Implemented Interfaces:
IConfigurable, IGeneralDAO<K,​C>
Direct Known Subclasses:
AbstractGeneralDAO

public abstract class AbstractBasicDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
extends java.lang.Object
implements IGeneralDAO<K,​C>, IConfigurable
Abstract Implementation for Data Access Objects. This implementation makes no restriction on the type of BO implementation.
Author:
ralph
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected org.slf4j.Logger log  
  • Constructor Summary

    Constructors 
    Constructor Description
    AbstractBasicDAO()
    Constructor.
  • Method Summary

    Modifier and Type Method Description
    protected abstract void _create​(C object)
    Creates the object.
    protected abstract void _delete​(C object)
    Deletes the object.
    protected abstract C _newInstance()
    Override here and return a plain, unintitialized BO implementation.
    protected abstract void _save​(C object)
    Saves the object.
    protected void addCached​(C object)
    Add the given object to the cache.
    void addDaoListener​(IDaoListener listener)
    Adds a DAO event listener.
    protected void afterCreate​(C object)
    Called immediately after creation.
    protected void afterDelete​(C object)
    Called immediately after deletion.
    protected void afterNewInstance​(C object, boolean persisted)
    Called after a new instance was created.
    protected void afterSave​(C object)
    Called immediately after saving.
    protected void beforeCreate​(C object)
    Called immediately before creation.
    protected void beforeDelete​(C object)
    Called immediately before deletion.
    protected void beforeSave​(C object)
    Called immediately before saving.
    void clearCache()
    Clears the cache.
    void configure​(org.apache.commons.configuration.Configuration cfg)
    void create​(C object)
    Inserts the object into the database if it does not exist.
    void create​(C object, boolean setCreationDate)
    Inserts the object into the database if it does not exist.
    protected ICache<CID,​C> createCache()
    Returns a WeakMapCache.
    void createObject​(java.lang.Object object)
    Inserts the object into the database if it does not exist.
    void createObject​(java.lang.Object object, boolean setCreationDate)
    Creates the object.
    void delete​(C object)
    Deletes the object.
    void delete​(java.util.Collection<C> objects)
    Deletes the objects.
    void deleteByKey​(K id)
    Deletes the object of given key.
    void deleteByKeys​(java.util.Collection<K> ids)
    Deletes the objects.
    void deleteObject​(java.lang.Object object)
    DELETING
    java.util.List<C> findAll()
    Returns all domain objects.
    java.util.List<C> findAll​(int firstResult, int maxResults)
    Returns a subset of all objects.
    java.util.List<C> findAll​(java.lang.String sortBy)
    Returns all domain objects.
    java.util.List<C> findBy​(java.util.Collection<K> ids)
    Find the given objects in model.
    java.util.List<C> findBy​(java.util.Collection<K> ids, java.lang.String sortBy)
    Find the given objects in model.
    C findById​(java.lang.Object id)
    Find the given object in model.
    java.util.List<C> findDefaultAll()
    Returns domain objects with default criteria.
    java.util.List<C> findDefaultAll​(int firstResult, int maxResults)
    Returns domain objects with default criteria.
    java.util.List<C> findDefaultAll​(java.lang.String sortBy)
    Returns domain objects with default criteria.
    protected void fireAllDefaultDeleted()
    Fires a DAO all-delete event.
    protected void fireAllDeleted()
    Fires a DAO all-delete event.
    protected void fireDaoEvent​(DaoEvent event)
    Fires a DAO delete event.
    protected void fireObjectCreated​(C object)
    Fires a DAO create event.
    protected void fireObjectDeleted​(C object)
    Fires a DAO delete event.
    protected void fireObjectUpdated​(C object)
    Fires a DAO update event.
    java.lang.Class<C> getBoInterfaceClass()
    Returns the boInterfaceClass.
    protected ICache<CID,​C> getCache()
    Returns the cache.
    protected C getCached​(CID cid)
    Returns an object from the cache.
    protected CID getCID​(C object)
    Returns the CID for this object.
    CID getCID​(K id)
    Computes the CID from the id.
    IDaoMaster getDaoMaster()
    Returns the DAO master.
    IDaoFactory getFactory()
    Returns the DAO factory.
    java.lang.Class<K> getKeyClass()
    Returns the keyClass.
    protected org.slf4j.Logger getLog()
    Returns the log.
    protected void init()
    Initializes / called from constructor.
    boolean isCacheEnabled()
    Returns status of caching mechanism.
    IDaoIterator<C> iterateAll()
    Returns all domain objects.
    IDaoIterator<C> iterateAll​(int firstResult, int maxResults)
    Returns a subset of all objects
    IDaoIterator<C> iterateAll​(java.lang.String sortBy)
    Returns all domain objects.
    IDaoIterator<C> iterateDefaultAll()
    Returns domain objects with default criteria.
    IDaoIterator<C> iterateDefaultAll​(int firstResult, int maxResults)
    Returns a subset of domain objects with default criteria.
    IDaoIterator<C> iterateDefaultAll​(java.lang.String sortBy)
    Returns domain objects with default criteria.
    C newInstance()
    Create a new domain class instance.
    protected void removeCached​(C object)
    Add the given object to the cache.
    void removeDaoListener​(IDaoListener listener)
    Removes a DAO event listener.
    void save​(C object)
    Saves the object.
    void save​(C object, boolean setChangeDate)
    Saves the object.
    void saveObject​(java.lang.Object object)
    Saves the object.
    void saveObject​(java.lang.Object object, boolean setChangeDate)
    Saves the object.
    void setCacheEnabled​(boolean enabled)
    Enables or disables the caching mechanism.
    void setDaoMaster​(IDaoMaster daoMaster)
    Sets the DAO master.
    void setFactory​(IDaoFactory factory)
    Sets the factory.
    void shutdown()
    Shutdown the DAO.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • log

      protected final org.slf4j.Logger log
  • Constructor Details

  • Method Details

    • init

      protected void init()
      Initializes / called from constructor.
    • createCache

      protected ICache<CID,​C> createCache()
      Returns a WeakMapCache.
      Returns:
      the cache implementation.
    • configure

      public void configure​(org.apache.commons.configuration.Configuration cfg) throws org.apache.commons.configuration.ConfigurationException
      Specified by:
      configure in interface IConfigurable
      Throws:
      org.apache.commons.configuration.ConfigurationException
    • shutdown

      public void shutdown()
      Shutdown the DAO.
      Specified by:
      shutdown in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
    • getLog

      protected org.slf4j.Logger getLog()
      Returns the log.
      Returns:
      the log
    • getKeyClass

      public java.lang.Class<K> getKeyClass()
      Returns the keyClass.
      Returns:
      the keyClass
    • getBoInterfaceClass

      public java.lang.Class<C> getBoInterfaceClass()
      Returns the boInterfaceClass.
      Returns:
      the boInterfaceClass
    • getFactory

      public IDaoFactory getFactory()
      Returns the DAO factory.
      Specified by:
      getFactory in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Returns:
      the DAO factory this DAO is assigned to
    • setFactory

      public void setFactory​(IDaoFactory factory)
      Sets the factory.
      Specified by:
      setFactory in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      factory - - the factory this DAO belongs to
    • getDaoMaster

      public IDaoMaster getDaoMaster()
      Returns the DAO master.
      Specified by:
      getDaoMaster in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Returns:
      the DAO master
    • setDaoMaster

      public void setDaoMaster​(IDaoMaster daoMaster)
      Sets the DAO master.
      Specified by:
      setDaoMaster in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      daoMaster - the master
    • getCID

      protected CID getCID​(C object)
      Returns the CID for this object.
      Parameters:
      object - object
      Returns:
      CID
    • newInstance

      public C newInstance()
      Create a new domain class instance. This method will set the creation date..

      This implementation forwards the call to _newInstance() which must be implemented by subclasses. It then calls afterNewInstance(IGeneralBO, boolean).

      Specified by:
      newInstance in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Returns:
      new instance
    • _newInstance

      protected abstract C _newInstance()
      Override here and return a plain, unintitialized BO implementation.
      Returns:
      a new instance
    • afterNewInstance

      protected void afterNewInstance​(C object, boolean persisted)
      Called after a new instance was created.

      This method sets creationDate and changeDate when the object was not created from a a persisted object.

      Parameters:
      object - object being created
      persisted - true when the object was created from a persisted object
    • addCached

      protected void addCached​(C object)
      Add the given object to the cache.
      Parameters:
      object - object to add
    • removeCached

      protected void removeCached​(C object)
      Add the given object to the cache.
      Parameters:
      object - object to add
    • getCID

      public CID getCID​(K id)
      Computes the CID from the id.
      Parameters:
      id - id
      Returns:
      computed CID.
    • getCached

      protected C getCached​(CID cid)
      Returns an object from the cache.
      Parameters:
      cid - CID of object
      Returns:
      the cached object if found
    • clearCache

      public void clearCache()
      Clears the cache.
      Specified by:
      clearCache in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
    • getCache

      protected ICache<CID,​C> getCache()
      Returns the cache.
      Returns:
      the cache
    • setCacheEnabled

      public void setCacheEnabled​(boolean enabled)
      Enables or disables the caching mechanism.
      Specified by:
      setCacheEnabled in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      enabled - true when cache shall be enabled.
    • isCacheEnabled

      public boolean isCacheEnabled()
      Returns status of caching mechanism.
      Specified by:
      isCacheEnabled in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Returns:
      true when cache shall be enabled.
    • createObject

      public void createObject​(java.lang.Object object)
      Inserts the object into the database if it does not exist. This method will set the creation date.
      Specified by:
      createObject in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - domain class instance
    • createObject

      public void createObject​(java.lang.Object object, boolean setCreationDate)
      Creates the object.
      Specified by:
      createObject in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - object to be saved
      setCreationDate - whether creationDate will be set
      See Also:
      IGeneralDAO.create(IGeneralBO, boolean)
    • create

      public void create​(C object)
      Inserts the object into the database if it does not exist. This method will set the creation date.
      Specified by:
      create in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - domain class instance
    • create

      public void create​(C object, boolean setCreationDate)
      Inserts the object into the database if it does not exist.
      Specified by:
      create in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - domain class instance
      setCreationDate - whether creation date shall be set
    • _create

      protected abstract void _create​(C object)
      Creates the object. This method assumes that the object does not exist.
      Parameters:
      object - BO to be saved
      See Also:
      createObject(Object)
    • beforeCreate

      protected void beforeCreate​(C object)
      Called immediately before creation. This method does nothing.
      Parameters:
      object - object to be created
    • afterCreate

      protected void afterCreate​(C object)
      Called immediately after creation. This method does nothing.
      Parameters:
      object - object created
    • findBy

      public java.util.List<C> findBy​(java.util.Collection<K> ids)
      Find the given objects in model.

      This implementation calls IGeneralDAO.findBy(Serializable) for each key. Descendants shall override when there are more efficient ways for finding multiple objects via their ID.

      Specified by:
      findBy in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      ids - ids of objects
      Returns:
      object
    • findBy

      public java.util.List<C> findBy​(java.util.Collection<K> ids, java.lang.String sortBy)
      Find the given objects in model.

      This implementation calls IGeneralDAO.findBy(Serializable) for each key. Descendants shall override when there are more efficient ways for finding multiple objects via their ID.

      Specified by:
      findBy in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      ids - - ids of objects
      sortBy - - the order clauses, e.g. column1 ASC, column2, column3 DESC
      Returns:
      object
    • findById

      public C findById​(java.lang.Object id)
      Find the given object in model. This method is an alternative in case you cannot specify the type of ID at compile time.
      Specified by:
      findById in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      id - id of object
      Returns:
      object
    • findAll

      public java.util.List<C> findAll()
      Returns all domain objects.
      Specified by:
      findAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Returns:
      list of all objects
    • findAll

      public java.util.List<C> findAll​(java.lang.String sortBy)
      Returns all domain objects.
      Specified by:
      findAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      sortBy - - the order clauses, e.g. column1 ASC, column2, column3 DESC
      Returns:
      list of all objects
    • findAll

      public java.util.List<C> findAll​(int firstResult, int maxResults)
      Returns a subset of all objects.
      Specified by:
      findAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      firstResult - index of first result
      maxResults - maximum number of results to return
      Returns:
      list of objects
    • findDefaultAll

      public java.util.List<C> findDefaultAll()
      Returns domain objects with default criteria. A default criteria could hide objects not needed regulary (e.g. deleted objects)
      Specified by:
      findDefaultAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Returns:
      list of all objects
    • findDefaultAll

      public java.util.List<C> findDefaultAll​(java.lang.String sortBy)
      Returns domain objects with default criteria. A default criteria could hide objects not needed regulary (e.g. deleted objects)
      Specified by:
      findDefaultAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      sortBy - - the order clauses, e.g. column1 ASC, column2, column3 DESC
      Returns:
      list of all objects
    • findDefaultAll

      public java.util.List<C> findDefaultAll​(int firstResult, int maxResults)
      Returns domain objects with default criteria. A default criteria could hide objects not needed regulary (e.g. deleted objects).
      Specified by:
      findDefaultAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      firstResult - - the first result to be returned
      maxResults - - the number of results returned at most
      Returns:
      list of all objects
    • iterateAll

      public IDaoIterator<C> iterateAll()
      Returns all domain objects.
      Specified by:
      iterateAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Returns:
      list of all objects
    • iterateAll

      public IDaoIterator<C> iterateAll​(int firstResult, int maxResults)
      Returns a subset of all objects
      Specified by:
      iterateAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      firstResult - index of first result
      maxResults - maximum number of results to return
      Returns:
      iterator of BO
    • iterateAll

      public IDaoIterator<C> iterateAll​(java.lang.String sortBy)
      Returns all domain objects.
      Specified by:
      iterateAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      sortBy - - the order clauses, e.g. column1 ASC, column2, column3 DESC
      Returns:
      list of all objects
    • iterateDefaultAll

      public IDaoIterator<C> iterateDefaultAll()
      Returns domain objects with default criteria. A default criteria could hide objects not needed regulary (e.g. deleted objects)
      Specified by:
      iterateDefaultAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Returns:
      list of all objects
    • iterateDefaultAll

      public IDaoIterator<C> iterateDefaultAll​(java.lang.String sortBy)
      Returns domain objects with default criteria. A default criteria could hide objects not needed regulary (e.g. deleted objects)
      Specified by:
      iterateDefaultAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      sortBy - - the order clauses, e.g. column1 ASC, column2, column3 DESC
      Returns:
      list of all objects
    • iterateDefaultAll

      public IDaoIterator<C> iterateDefaultAll​(int firstResult, int maxResults)
      Returns a subset of domain objects with default criteria. A default criteria could hide objects not needed regulary (e.g. deleted objects)
      Specified by:
      iterateDefaultAll in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      firstResult - index of first result
      maxResults - maximum number of results to return
      Returns:
      list of all objects
    • saveObject

      public void saveObject​(java.lang.Object object)
      Saves the object. This method assumes that the object already exists. Also, this method will set the change date.
      Specified by:
      saveObject in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - object to be saved
    • saveObject

      public void saveObject​(java.lang.Object object, boolean setChangeDate)
      Saves the object. This method assumes that the object already exists.
      Specified by:
      saveObject in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - object to be saved
      setChangeDate - whether changeDate will be set
    • save

      public void save​(C object)
      Saves the object. This method assumes that the object already exists. Also, this method will set the change date.
      Specified by:
      save in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - object to be saved
    • save

      public void save​(C object, boolean setChangeDate)
      Saves the object. This method assumes that the object already exists.
      Specified by:
      save in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - object to be saved
      setChangeDate - whether changeDate will be set
    • beforeSave

      protected void beforeSave​(C object)
      Called immediately before saving. This method does nothing.
      Parameters:
      object - object to be saved
    • afterSave

      protected void afterSave​(C object)
      Called immediately after saving. This method does nothing.
      Parameters:
      object - object saved
    • _save

      protected abstract void _save​(C object)
      Saves the object. This method assumes that the object already exists.
      Parameters:
      object - BO to be saved
      See Also:
      create(IGeneralBO, boolean)
    • deleteObject

      public void deleteObject​(java.lang.Object object)
      DELETING
      Specified by:
      deleteObject in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - object to be saved
    • delete

      public void delete​(C object)
      Deletes the object. This method assumes that the object existed before.
      Specified by:
      delete in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      object - object to be deleted.
    • delete

      public void delete​(java.util.Collection<C> objects)
      Deletes the objects. This method assumes that the objects existed before.
      Specified by:
      delete in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      objects - objects to be deleted.
    • deleteByKeys

      public void deleteByKeys​(java.util.Collection<K> ids)
      Deletes the objects. This method assumes that the object existed before.
      Specified by:
      deleteByKeys in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      ids - id of objects to be deleted.
    • deleteByKey

      public void deleteByKey​(K id)
      Deletes the object of given key. This method assumes that the object existed before.
      Specified by:
      deleteByKey in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      id - id of object to be deleted.
    • beforeDelete

      protected void beforeDelete​(C object)
      Called immediately before deletion. This method does nothing.
      Parameters:
      object - object to be deleted
    • afterDelete

      protected void afterDelete​(C object)
      Called immediately after deletion. This method does nothing.
      Parameters:
      object - object deleted
    • _delete

      protected abstract void _delete​(C object)
      Deletes the object. This method assumes that the object existed before.
      Parameters:
      object - DTO to be deleted.
    • addDaoListener

      public void addDaoListener​(IDaoListener listener)
      Adds a DAO event listener.
      Specified by:
      addDaoListener in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      listener - listener
    • removeDaoListener

      public void removeDaoListener​(IDaoListener listener)
      Removes a DAO event listener.
      Specified by:
      removeDaoListener in interface IGeneralDAO<K extends java.io.Serializable,​C extends IGeneralBO<K>>
      Parameters:
      listener - listener
    • fireObjectCreated

      protected void fireObjectCreated​(C object)
      Fires a DAO create event.
      Parameters:
      object - object created
    • fireObjectUpdated

      protected void fireObjectUpdated​(C object)
      Fires a DAO update event.
      Parameters:
      object - object created
    • fireObjectDeleted

      protected void fireObjectDeleted​(C object)
      Fires a DAO delete event.
      Parameters:
      object - object created
    • fireAllDeleted

      protected void fireAllDeleted()
      Fires a DAO all-delete event.
    • fireAllDefaultDeleted

      protected void fireAllDefaultDeleted()
      Fires a DAO all-delete event.
    • fireDaoEvent

      protected void fireDaoEvent​(DaoEvent event)
      Fires a DAO delete event.
      Parameters:
      event - event to be fired