net.toften.jlips.persist
Class EntityFactory

java.lang.Object
  extended by net.toften.jlips.persist.EntityFactory

public final class EntityFactory
extends Object

Java Lightweight Interface Persistence System (jlips) Factory. This factory is the main interface towards the application. It is responsible for creating and finding objects specified by the interfaces. To use the factory an interface needs to be created, specifing the accessor methods for the fields in the database table. All methods in this factory returns an object of the type Object. However this object can (and must) be typecasted to the type of the interface specifying the accessor methods.

Version:
$Revision: 1.6 $
Author:
thomas

Field Summary
static String EF_DB_CON_HAND
          String used to specify the name of the database connection handler in the properties passed to jlips when it is initialised
static String EF_DB_PROP_HAND
          String used to specify the name of the database connection properties in the properties passed to jlips when it is initialised
static String EF_DB_STM_HAND
          String used to specify the name of the SQL statement creater class.
 
Method Summary
static Object create(Class theEntity)
          Creates a new row in the database.
static Object find(Class theEntity, Object key)
          Finds a row in the database given a primary key.
static Collection findAll(Class theEntity)
          This method will return all the records in the table mapped to the persist entity provided
static String getEntityName(Class theEntity)
          Returns the name of the interface class given.
static void init(DBProperties dbProps, Map tableMap)
           
static void init(Properties dbProps, Map tableMap)
          Initializes the persistence system.
static Collection search(Class theEntity, String query, Object[] args)
          Runs a predefined query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EF_DB_CON_HAND

public static final String EF_DB_CON_HAND
String used to specify the name of the database connection handler in the properties passed to jlips when it is initialised

See Also:
DBConnectionHandler, Constant Field Values

EF_DB_PROP_HAND

public static final String EF_DB_PROP_HAND
String used to specify the name of the database connection properties in the properties passed to jlips when it is initialised

See Also:
DBProperties, Constant Field Values

EF_DB_STM_HAND

public static final String EF_DB_STM_HAND
String used to specify the name of the SQL statement creater class. If this is not specified, the DBDefaultStatement class will be used.

See Also:
DBStatement, Constant Field Values
Method Detail

init

public static void init(Properties dbProps,
                        Map tableMap)
Initializes the persistence system. This must be called before any of the methods are used. To initialize the system you will need to supply information regardng the connection to the database and the mapping of interfaces and database tables. This is done in the two parameters that the init method takes.

Database properties

The database connection properties dbProps takes two properties. These properties are the full class path to the classes that implements the connection information retrieval and the database connection handler. The dbProps parameter is a Properties Object containing two elements. Example:
  connection.handler=net.toften.jlips.persist.db.DBPooledConnection
  properties.handler=net.toften.jlips.persist.db.DBHardProperties
 
The properties describe which classes jLips should use to manage database connections and to retreive connection information. When jLips initialise the database connection factory, it will instantiate one object of each of these classes. The connection.handler must implement the interface DBConnectionHandler and the properties.handler must implement the interface DBProperties interface.

Table map

DOCUMENT ME!

Parameters:
dbProps - Properties that describe the database connection details
tableMap - Map describing the mapping between the persist entities and the database tables

init

public static void init(DBProperties dbProps,
                        Map tableMap)

create

public static Object create(Class theEntity)
Creates a new row in the database. No values are inserted into the database except the generated primary key. This method is invoked after jLips has been initialised.
 Person p = (Person)EntityFactory.create(Person.class);
 p.setName("Thomas");
 p.commit();
 
The code above will create a new record using the Person persist entity and set the field name to "Thomas". The last line commits the changes to the database.

Parameters:
theEntity - The class of the interface specifying the accessor methods
Returns:
The created object

find

public static Object find(Class theEntity,
                          Object key)
Finds a row in the database given a primary key.

Parameters:
theEntity - The class of the interface specifying the accessor methods
key - The primary key of the record to be found
Returns:
The found object. If the object could not be found null is returned

getEntityName

public static String getEntityName(Class theEntity)
Returns the name of the interface class given. Note the returned name is in lower case! As defined for jLips, this must be the same name as the name of the table in the database.

Parameters:
theEntity - The class of the interface specifying the accessor methods
Returns:
String containing the name of the interface in lower case

search

public static Collection search(Class theEntity,
                                String query,
                                Object[] args)
Runs a predefined query. Queries are defined in the mapping interface in the array queries. This array is a two dimentional array in the following format:
  public static final String queries[][] =
      {
          { "findByFirstName", "firstname = ?" },
          { "findByLastName", "lastname = ?" }
      };
 
Where the first element is the name of the query passed to this method in the query parameter. The second element is the WHERE part of the SQL query. In this part every ? will be replaces with the corosponding element in the args array, i.e. the first ? will be replaced with args[0] and so forth. The query will return a Collection that contains all the records that matches the search. If no matches were found an empty Collection is returned.

Parameters:
theEntity - The persist entity containing the search
query -
args - Array of arguments to be passed into the query
Returns:
Collection of the found records
See Also:
SearchCollectionEntityHandler

findAll

public static Collection findAll(Class theEntity)
This method will return all the records in the table mapped to the persist entity provided

Parameters:
theEntity - The entity for which all records is requested
Returns:
Collection of all records