com.scooterframework.orm.activerecord
Class ActiveRecordUtil

java.lang.Object
  extended by com.scooterframework.orm.activerecord.ActiveRecordUtil

public class ActiveRecordUtil
extends java.lang.Object

ActiveRecordUtil class has helper methods.


Field Summary
static java.lang.String DEFAULT_RECORD_CLASS
           
 
Constructor Summary
ActiveRecordUtil()
           
 
Method Summary
static ActiveRecord generateActiveRecordInstance(java.lang.String className, java.lang.String model)
          Generates an instance of ActiveRecord.
static ActiveRecord generateActiveRecordInstance(java.lang.String className, java.lang.String connName, java.lang.String model)
          Generates an instance of ActiveRecord.
static ActiveRecord generateActiveRecordInstance(java.lang.String className, java.lang.String connName, java.lang.String model, java.lang.String table)
          Generates an instance of ActiveRecord.
static Calculator getCalculator(java.lang.Class<? extends ActiveRecord> modelClass)
          Returns a Calculator instance for the model class.
static Calculator getCalculator(java.lang.String fullModelClassName)
          Returns a Calculator instance for the model class.
static java.lang.String getFullClassName(java.lang.Class<? extends ActiveRecord> c)
          Returns the full class name
static TableGateway getGateway(ActiveRecord home)
           
static TableGateway getGateway(java.lang.Class<? extends ActiveRecord> modelClass)
           
static TableGateway getGateway(java.lang.String fullModelClassName)
           
static ActiveRecord getHomeInstance(java.lang.Class<? extends ActiveRecord> clz)
          Returns a home instance of a class type.
static ActiveRecord getHomeInstance(java.lang.String fullModelClassName)
          Returns a home instance of a class type.
static ActiveRecord getHomeInstance(java.lang.String fullModelClassName, java.lang.String modelName, java.lang.String defaultModelClassName)
          Returns an ActiveRecord home instance.
static java.lang.String getModelName(ActiveRecord record)
          Returns model name of an active record instance.
static java.lang.String getModelName(java.lang.Class<? extends ActiveRecord> modelClass)
          Returns model name of an active record class.
static java.lang.String getModelName(java.lang.String tableName)
          Returns model name for a table name.
static java.lang.String getSimpleTableName(java.lang.Class<? extends ActiveRecord> c)
          Returns the simple table name of an active record class.
static java.lang.String getTableName(java.lang.Class<? extends ActiveRecord> c)
          Returns full table name of an active record class.
static boolean isSameRecord(ActiveRecord r1, ActiveRecord r2)
          Checks if the two records are actually the same record.
static java.lang.Object newInstance(java.lang.Class<?> c, java.lang.Class<?>[] parameterTypes, java.lang.Object[] initargs)
          Creates a new instance.
static java.util.List<? extends ActiveRecord> remains(java.util.List<? extends ActiveRecord> one, java.util.List<? extends ActiveRecord> two)
          Removes duplicated items from list one.
static void setCalculatorInstance(java.lang.String fullModelClassName, Calculator cal)
          Sets Calculator instance.
static void setGateInstance(java.lang.String fullModelClassName, TableGateway gate)
          Sets gateway instance.
static void setHomeInstance(ActiveRecord record)
          Sets home instance.
static void validateRecordType(java.lang.Class<? extends ActiveRecord> expected, ActiveRecord record)
          Validates a record is of the expected type.
static void verifyExistenceOfColumn(java.lang.Class<? extends ActiveRecord> clazz, java.lang.String fieldName)
          Checks if a field name exists in a database table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_RECORD_CLASS

public static final java.lang.String DEFAULT_RECORD_CLASS
See Also:
Constant Field Values
Constructor Detail

ActiveRecordUtil

public ActiveRecordUtil()
Method Detail

isSameRecord

public static boolean isSameRecord(ActiveRecord r1,
                                   ActiveRecord r2)
Checks if the two records are actually the same record. If the two records are the same, they are in the same table and their primary keys are the same.

Parameters:
r1 - record 1
r2 - record 2
Returns:
true if the two records have the same primary key

validateRecordType

public static void validateRecordType(java.lang.Class<? extends ActiveRecord> expected,
                                      ActiveRecord record)
Validates a record is of the expected type.

Parameters:
expected -
record -
Throws:
WrongRecordTypeException - exception if unexpected.

remains

public static java.util.List<? extends ActiveRecord> remains(java.util.List<? extends ActiveRecord> one,
                                                             java.util.List<? extends ActiveRecord> two)
Removes duplicated items from list one.

Parameters:
one - the original list
two - the list to be subtracted
Returns:
new list

getFullClassName

public static java.lang.String getFullClassName(java.lang.Class<? extends ActiveRecord> c)
Returns the full class name

Returns:
String

getModelName

public static java.lang.String getModelName(java.lang.Class<? extends ActiveRecord> modelClass)
Returns model name of an active record class. Model name is actually the short class name of the model class in lowercase unless the class is ActiveRecord class itself. In that case, it is the slim table name of the class. Model name should always be in singularized form.

Parameters:
modelClass - an ActiveRecord class type
Returns:
model name

getModelName

public static java.lang.String getModelName(ActiveRecord record)
Returns model name of an active record instance.

Parameters:
record - ActiveRecord instance
Returns:
model name

getModelName

public static java.lang.String getModelName(java.lang.String tableName)
Returns model name for a table name. Model name should be a singularized form of a table name without any prefix or suffix element.

Parameters:
tableName - table name for the model
Returns:
String

getTableName

public static java.lang.String getTableName(java.lang.Class<? extends ActiveRecord> c)
Returns full table name of an active record class. A full table name includes table prefix and suffix elements.

Parameters:
c - an ActiveRecord class type
Returns:
String

getSimpleTableName

public static java.lang.String getSimpleTableName(java.lang.Class<? extends ActiveRecord> c)
Returns the simple table name of an active record class. A simple table name is a name without any prefix or suffix element.

Parameters:
c - an ActiveRecord class type
Returns:
String

generateActiveRecordInstance

public static ActiveRecord generateActiveRecordInstance(java.lang.String className,
                                                        java.lang.String model)

Generates an instance of ActiveRecord. model value is used to deduce the table name related to the ActiveRecord instance. The meta data of the model is obtained from the default database connection name as specified by the database.properties file or from the getConnectionName() method of the underlying model class.

Parameters:
className - class name of the ActiveRecord instance to be created.
model - model name of the ActiveRecord class.
Returns:
an ActiveRecord instance

generateActiveRecordInstance

public static ActiveRecord generateActiveRecordInstance(java.lang.String className,
                                                        java.lang.String connName,
                                                        java.lang.String model)

Generates an instance of ActiveRecord. connName is the database connection name from where the meta data of the model is obtained. model value is used to deduce the table name related to the ActiveRecord instance.

The table name related to this model is going to be derived from the model name.

Parameters:
className - class name of the ActiveRecord instance to be created
connName - db connection name
model - model name of the ActiveRecord class
Returns:
an ActiveRecord instance

generateActiveRecordInstance

public static ActiveRecord generateActiveRecordInstance(java.lang.String className,
                                                        java.lang.String connName,
                                                        java.lang.String model,
                                                        java.lang.String table)

Generates an instance of ActiveRecord. connName is the database connection name from where the meta data of the model is obtained. model value is used to deduce the table name related to the ActiveRecord instance.

Parameters:
className - class name of the ActiveRecord instance to be created
connName - db connection name
model - model name of the ActiveRecord class
table - table name related to the ActiveRecord class
Returns:
an ActiveRecord instance

newInstance

public static java.lang.Object newInstance(java.lang.Class<?> c,
                                           java.lang.Class<?>[] parameterTypes,
                                           java.lang.Object[] initargs)
                                    throws java.lang.Exception
Creates a new instance.

Parameters:
c - class type of the new instance
parameterTypes - constructor parameter types
initargs - constructor parameter values
Returns:
a new instance
Throws:
java.lang.Exception

getHomeInstance

public static ActiveRecord getHomeInstance(java.lang.String fullModelClassName,
                                           java.lang.String modelName,
                                           java.lang.String defaultModelClassName)
Returns an ActiveRecord home instance. This method first tries to retrieve an ActiveRecord home instance of type fullModelClassName. If a null value is received, it then generates the home instance. If this fails too, a default home instance of type defaultModelClassName will be created and returned.

See description of getHomeInstance(java.lang.String) method for more details.

Parameters:
fullModelClassName - class name of the model
modelName - model name
defaultModelClassName - default model class name
Returns:
an ActiveRecord home instance

getHomeInstance

public static ActiveRecord getHomeInstance(java.lang.String fullModelClassName)

Returns a home instance of a class type.

A home instance of a record is a read-only instance for a model type. Its main function is to provide meta information of the model and some finder methods.

In this method, the value of fullModelClassName cannot be DEFAULT_RECORD_CLASS, because each home instance of a record class type is cached for performance.

This method creates a home instance the first time it is called.

Parameters:
fullModelClassName - class type name of the model
Returns:
a home instance of a model

getHomeInstance

public static ActiveRecord getHomeInstance(java.lang.Class<? extends ActiveRecord> clz)

Returns a home instance of a class type. See description of getHomeInstance(java.lang.String) method for more details.

Parameters:
clz - class of the model
Returns:
a home instance of a model

setHomeInstance

public static void setHomeInstance(ActiveRecord record)
Sets home instance.

Parameters:
record - a home instance

getGateway

public static TableGateway getGateway(java.lang.Class<? extends ActiveRecord> modelClass)

getGateway

public static TableGateway getGateway(java.lang.String fullModelClassName)

getGateway

public static TableGateway getGateway(ActiveRecord home)

setGateInstance

public static void setGateInstance(java.lang.String fullModelClassName,
                                   TableGateway gate)
Sets gateway instance.

Parameters:
fullModelClassName - model class name
gate - a gateway instance

getCalculator

public static Calculator getCalculator(java.lang.Class<? extends ActiveRecord> modelClass)
Returns a Calculator instance for the model class.

Parameters:
modelClass - model class type
Returns:
a Calculator instance for the model class

getCalculator

public static Calculator getCalculator(java.lang.String fullModelClassName)
Returns a Calculator instance for the model class.

Parameters:
fullModelClassName -
Returns:
a Calculator instance for the model class

setCalculatorInstance

public static void setCalculatorInstance(java.lang.String fullModelClassName,
                                         Calculator cal)
Sets Calculator instance.

Parameters:
fullModelClassName - model class name
cal - a Calculator instance

verifyExistenceOfColumn

public static void verifyExistenceOfColumn(java.lang.Class<? extends ActiveRecord> clazz,
                                           java.lang.String fieldName)
Checks if a field name exists in a database table.

Parameters:
clazz - the class type of an ActiveRecord record
fieldName - a field name