de.esprit.appsuite.database.dbase

## Class PrimaryKeyFactory

- java.lang.Object
- - de.esprit.common.util.DumpableObject
  - - de.esprit.appsuite.database.dbase.PrimaryKeyFactory

- All Implemented Interfaces:
    Dumpable

      public class PrimaryKeyFactory
      extends DumpableObject

  This class serves as a primary key generator for DBObjects. It caches the last given primary key per table. If a new key is requested the key for the according table is incremented. This ensures that the primary key is an ever growing number and a key will never be reused even if records are deleted.

  This class also increases INSERT performance because is avoids to perform the `'select max(column) from table'` to find out a new primary key value. It also has the advantage of being database-independent (not relying on particular features of the RDBMS system).

  Note that primary keys can be requested only for DBObjects that are told to be managed by the factory. It also works only for tables which do NOT have a composed primary key. Note also that there should only exist one single Primary key factory at a time. If there were two managing the same tables they would confuse each other.

  Author:
    Rainer Buesch
  See Also:
    `DBObject`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `PrimaryKeyFactory(javax.sql.DataSource connPool, DBObject... tables)` Constructs the PrimaryKeyFactory by fetching the current maximum primary key values of all managed tables from the database.
    `PrimaryKeyFactory(DBConnectionPool connPool)` Constructs an empty PrimaryKeyFactory.
    `PrimaryKeyFactory(DBObject... tables)` Same as `PrimaryKeyFactory(DataSource, DBObject[])` but tries to get the ConnectionPool from the DBObject class.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addKeyManagementFor(DBObject dbo)` Adds primary key management for the given DBObject.
    `int` | `getKeyFor(java.lang.String tableName)` Returns a new primary key value for the given table.
    `boolean` | `isKeyAvailableFor(java.lang.String tableName)` Tells whether the given table is managed by this factory.
    `DumpString` | `toDumpString(DumpString s)` Returns a string that displays the current content of this factory for debugging purposes.
    `ToString` | `toString(ToString s)` This method allows subclasses to add values to the existing ToString of the superclass.

    - ### Methods inherited from class de.esprit.common.util.DumpableObject

      `configureDumpHeadlineLength, configureDumpMaxElementsPrinted, copyDumpStringValues, copyToStringValues, createDumpHeader, toDumpString, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### PrimaryKeyFactory

          public PrimaryKeyFactory(javax.sql.DataSource connPool,
                           DBObject... tables)
                            throws java.lang.Exception

      Constructs the PrimaryKeyFactory by fetching the current maximum primary key values of all managed tables from the database.
      Parameters:
        `connPool` - The DBConnectionPool instance
        `tables` - Instances of DBObjects to be managed
      Throws:
        `java.lang.Exception`

    - #### PrimaryKeyFactory

          public PrimaryKeyFactory(DBObject... tables)
                            throws java.lang.Exception

      Same as `PrimaryKeyFactory(DataSource, DBObject[])` but tries to get the ConnectionPool from the DBObject class.
      Parameters:
        `tables` - Instances of DBObjects to be managed
      Throws:
        `java.lang.Exception`

    - #### PrimaryKeyFactory

          public PrimaryKeyFactory(DBConnectionPool connPool)
                            throws java.lang.Exception

      Constructs an empty PrimaryKeyFactory. You need to add managed objects using the addKeyManagmentFor(DBOBject) method.
      Parameters:
        `connPool` - The DBConnectionPool instance
      Throws:
        `java.lang.Exception`
      See Also:
        `addKeyManagementFor(DBObject)`

  - ### Method Detail

    - #### getKeyFor

          public int getKeyFor(java.lang.String tableName)

      Returns a new primary key value for the given table. Note that you should call isKeyAvailableFor(table) before actually requesting one.
      Throws:
        `java.lang.IllegalArgumentException` - if the table is not managed by the factory.

    - #### isKeyAvailableFor

          public boolean isKeyAvailableFor(java.lang.String tableName)

      Tells whether the given table is managed by this factory.

    - #### addKeyManagementFor

          public void addKeyManagementFor(DBObject dbo)
                                   throws java.lang.Exception

      Adds primary key management for the given DBObject. When adding a DBO a query is executed to find out the current highest value of the primary key from there on the new keys are determined.
      Throws:
        `java.lang.Exception`

    - #### toString

          public ToString toString(ToString s)

      Description copied from interface: `Dumpable`
      This method allows subclasses to add values to the existing ToString of the superclass. Usually subclasses only need to override this method.
      Specified by:
        `toString` in interface `Dumpable`
      Overrides:
        `toString` in class `DumpableObject`

    - #### toDumpString

          public DumpString toDumpString(DumpString s)

      Returns a string that displays the current content of this factory for debugging purposes.
      Specified by:
        `toDumpString` in interface `Dumpable`
      Overrides:
        `toDumpString` in class `DumpableObject`

Copyright © 2014 EsprIT-Systems. All Rights Reserved.