Holger's Java API

## com.antelmann.sql
Class SQLNumberGenerator

    java.lang.Object
      com.antelmann.sql.SQLNumberGenerator

    public class SQLNumberGenerator

extends Object

this class is a helper class that provides new IDs or document/product numbers as counters based on flexible intervals. An example would be to generate counters based on years, hence using a SimpleDateFormat formatter of the form "yyyy". Note that these numbers are only save to be used when created and used within a properly synchronized transaction.

Author:
  Holger Antelmann
See Also:
  `TransactionalNumberGenerator`

Field Summary
`protected Connection` `con`
`protected String` `countField`
`protected DateFormat` `dateFormat`
`protected String` `intervalField`
`protected String` `itemField`
`protected NumberFormat` `numberFormat`
`protected String` `separator`
`protected String` `table`

Constructor Summary
`SQLNumberGenerator(Connection con)` uses default values for the protected data members
`SQLNumberGenerator(Connection con, String table, String itemField, String intervalField, String countField, String separator, DateFormat dateFormat)`
`SQLNumberGenerator(Connection con, String table, String itemField, String intervalField, String countField, String separator, DateFormat dateFormat, NumberFormat numberFormat)` constructs a generator by initializing all configuration data.

Method Summary
`TransactionalNumberGenerator` `createTransactionalNumberGenerator(String item, String interval, DBTransaction tx)`
`int` `deleteCount(String item, Date interval)`
`int` `deleteCount(String item, String interval)`
`int` `generateNewCountFor(String item)`
`int` `generateNewCountFor(String item, Date date)`
`int` `generateNewCountFor(String item, String interval)` returns the counter for the given item and interval after updating the database.
`String` `generateNewNumberFor(String item)`
`String` `generateNewNumberFor(String item, Date date)`
`String` `generateNewNumberFor(String item, Date date, DateFormat format)`
`String` `generateNewNumberFor(String item, String interval)` returns a String constructed from the interval, the separator and the calculated counter using the embedded NumberFormat
`String` `generateNewNumberFor(String item, String interval, NumberFormat nf)`
`Connection` `getConnection()`
`int` `getCount(String item, Date date)`
`int` `getCount(String item, Date date, DateFormat format)`
`int` `getCount(String item, String interval)`
`DateFormat` `getDateFormat()`
`NumberFormat` `getNumberFormat()`
`void` `setCount(String item, Date date, DateFormat format, int count)`
`void` `setCount(String item, Date date, int count)`
`void` `setCount(String item, String interval, int count)`
`void` `setDateFormat(DateFormat dateFormat)`
`void` `setNumberFormat(NumberFormat numberFormat)`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### con

    protected Connection con

### table

    protected String table

### itemField

    protected String itemField

### intervalField

    protected String intervalField

### countField

    protected String countField

### separator

    protected String separator

### dateFormat

    protected DateFormat dateFormat

### numberFormat

    protected NumberFormat numberFormat

Constructor Detail

### SQLNumberGenerator

    public SQLNumberGenerator(Connection con)

  uses default values for the protected data members

### SQLNumberGenerator

    public SQLNumberGenerator(Connection con,
                              String table,
                              String itemField,
                              String intervalField,
                              String countField,
                              String separator,
                              DateFormat dateFormat)

### SQLNumberGenerator

    public SQLNumberGenerator(Connection con,
                              String table,
                              String itemField,
                              String intervalField,
                              String countField,
                              String separator,
                              DateFormat dateFormat,
                              NumberFormat numberFormat)

  constructs a generator by initializing all configuration data.

  Parameters:
    `con` - the database connection to be used
    `table` - the name of the table containing the counting data
    `itemField` - the name of the column to be queried for the item to be counted (as String)
    `intervalField` - the name of the column holding the time interval as a String (for most flexibility)
    `countField` - the name of the column holding the counter as an integer
    `separator` - the characters to be used to separate the intervalField from the counter for returning the id
    `dateFormat` - the default format to be used when only a date is given for an interval

Method Detail

### getConnection

    public Connection getConnection()

### getDateFormat

    public DateFormat getDateFormat()

### setDateFormat

    public void setDateFormat(DateFormat dateFormat)

### getNumberFormat

    public NumberFormat getNumberFormat()

### setNumberFormat

    public void setNumberFormat(NumberFormat numberFormat)

### getCount

    public int getCount(String item,
                        Date date)
                 throws SQLException

  Throws:
    `SQLException`

### getCount

    public int getCount(String item,
                        Date date,
                        DateFormat format)
                 throws SQLException

  Throws:
    `SQLException`

### getCount

    public int getCount(String item,
                        String interval)
                 throws SQLException

  Throws:
    `SQLException`

### setCount

    public void setCount(String item,
                         Date date,
                         int count)
                  throws SQLException

  Throws:
    `SQLException`

### setCount

    public void setCount(String item,
                         Date date,
                         DateFormat format,
                         int count)
                  throws SQLException

  Throws:
    `SQLException`

### setCount

    public void setCount(String item,
                         String interval,
                         int count)
                  throws SQLException

  Throws:
    `SQLException`

### deleteCount

    public int deleteCount(String item,
                           Date interval)
                    throws SQLException

  Throws:
    `SQLException`

### deleteCount

    public int deleteCount(String item,
                           String interval)
                    throws SQLException

  Throws:
    `SQLException`

### createTransactionalNumberGenerator

    public TransactionalNumberGenerator createTransactionalNumberGenerator(String item,
                                                                           String interval,
                                                                           DBTransaction tx)

### generateNewNumberFor

    public String generateNewNumberFor(String item)
                                throws SQLException

  Throws:
    `SQLException`

### generateNewNumberFor

    public String generateNewNumberFor(String item,
                                       Date date)
                                throws SQLException

  Throws:
    `SQLException`

### generateNewNumberFor

    public String generateNewNumberFor(String item,
                                       Date date,
                                       DateFormat format)
                                throws SQLException

  Throws:
    `SQLException`

### generateNewNumberFor

    public String generateNewNumberFor(String item,
                                       String interval)
                                throws SQLException

  returns a String constructed from the interval, the separator and the calculated counter using the embedded NumberFormat

  Throws:
    `SQLException`

### generateNewNumberFor

    public String generateNewNumberFor(String item,
                                       String interval,
                                       NumberFormat nf)
                                throws SQLException

  Throws:
    `SQLException`

### generateNewCountFor

    public int generateNewCountFor(String item)
                            throws SQLException

  Throws:
    `SQLException`

### generateNewCountFor

    public int generateNewCountFor(String item,
                                   Date date)
                            throws SQLException

  Throws:
    `SQLException`

### generateNewCountFor

    public int generateNewCountFor(String item,
                                   String interval)
                            throws SQLException

  returns the counter for the given item and interval after updating the database. If the configured table doesn't contain an entry for the given parameters, 1 is stored along with the given parameters and returned. If an entry is found, the counter will be increased by 1 and returned.

  Throws:
    `SQLException`

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads