com.scooterframework.orm.activerecord
Class RelationManager

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

public class RelationManager
extends java.lang.Object

RelationManager class manages relations.


Method Summary
 RecordRelation createRecordRelation(ActiveRecord record, java.lang.String associationId)
          Creates a RecordRelation between owner (record instance) and its associated model.
 boolean existsBelongsToRelationBetween(java.lang.Class<? extends ActiveRecord> endA, java.lang.Class<? extends ActiveRecord> endB)
          Checks if there is a belongs-to relation between the two class types.
 boolean existsHasManyRelationBetween(java.lang.Class<? extends ActiveRecord> endA, java.lang.Class<? extends ActiveRecord> endB)
          Checks if there is a has-many relation between the two class types.
 boolean existsHasManyThroughRelationBetween(java.lang.Class<? extends ActiveRecord> endA, java.lang.Class<? extends ActiveRecord> endB)
          Checks if there is a has-many-through relation between the two class types.
 boolean existsHasOneRelationBetween(java.lang.Class<? extends ActiveRecord> endA, java.lang.Class<? extends ActiveRecord> endB)
          Checks if there is a has-one relation between the two class types.
 java.util.List<java.lang.String> getAllRelationNameTypes(java.lang.Class<? extends ActiveRecord> clz)
          Return a list of name type combination for a model class.
 Relation getBelongsToRelationBetween(java.lang.Class<? extends ActiveRecord> endA, java.lang.Class<? extends ActiveRecord> endB)
          Returns a belongs-to relation which exists between the two class types.
 Category getCategory(java.lang.String category)
          Returns a declared Category instance.
 java.lang.String getCategoryEntity(java.lang.String category, java.lang.String type)
          Returns model name of a type in a category.
 java.lang.String getDefaultMapping(java.lang.Class<? extends ActiveRecord> a, java.lang.String type, java.lang.String target, java.lang.Class<? extends ActiveRecord> b)
          returns default FK mapping.
 Relation getHasManyRelationBetween(java.lang.Class<? extends ActiveRecord> endA, java.lang.Class<? extends ActiveRecord> endB)
          Returns a has-many relation which exists between the two class types.
 Relation getHasManyThroughRelationBetween(java.lang.Class<? extends ActiveRecord> endA, java.lang.Class<? extends ActiveRecord> endB)
          Returns a has-many-through relation which exists between the two class types.
 Relation getHasOneRelationBetween(java.lang.Class<? extends ActiveRecord> endA, java.lang.Class<? extends ActiveRecord> endB)
          Returns a has-one relation which exists between the two class types.
static RelationManager getInstance()
           
 java.util.List<Relation> getOwnedRelations(java.lang.Class<? extends ActiveRecord> owner)
          Returns a list of relation instances owned by an owner class type.
 java.util.List<Category> getRegisteredCategory(java.lang.Class<? extends ActiveRecord> center)
          Gets a category list that all have the same center class.
 Relation getRelation(java.lang.Class<? extends ActiveRecord> owner, java.lang.String associationId)
          Returns relation between owner and target.
 java.util.List<Relation> getRelations(java.lang.Class<? extends ActiveRecord> owner, java.lang.Class<? extends ActiveRecord> target)
          Returns a list of relation instances owned by an owner class type with the specific target class type.
 java.lang.String getRelationType(java.lang.Class<? extends ActiveRecord> owner, java.lang.Class<? extends ActiveRecord> target)
          Returns relation type from owner class to target class.
 void registerCategory(java.lang.Class<? extends ActiveRecord> center, java.lang.String category, java.lang.String idField, java.lang.String typeField)
          Registers a Category.
 void registerRelations(java.lang.Class<? extends ActiveRecord> recordClass)
          Loads relations for a class type.
 void removeRelationsFor(java.lang.String model)
          Removes all cached relations owned by a model.
 void setupHasManyThroughRelation(java.lang.Class<? extends ActiveRecord> ownerClass, java.lang.String targets, java.lang.String throughAssociation, java.lang.String properties, java.util.Map<java.lang.String,java.lang.Object> joinInputs)
          Sets up has-many-through relation.
 void setupRelation(java.lang.Class<? extends ActiveRecord> ownerClass, java.lang.String type, java.lang.String associationId, java.lang.Class<? extends ActiveRecord> targetClass, java.lang.String properties)
          Sets up relation with other model(s).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static RelationManager getInstance()

registerRelations

public void registerRelations(java.lang.Class<? extends ActiveRecord> recordClass)
Loads relations for a class type.

Parameters:
recordClass - class type

setupRelation

public void setupRelation(java.lang.Class<? extends ActiveRecord> ownerClass,
                          java.lang.String type,
                          java.lang.String associationId,
                          java.lang.Class<? extends ActiveRecord> targetClass,
                          java.lang.String properties)
Sets up relation with other model(s). target parameter can be either the model name of the target or a descriptive string of the target. In the latter case, either the properties must contain key model to indicate the model name of the target or the targetClass input parameter is not null. In a property string, each name-value pair is separated by ';' character, while within each name-value pair, name and value strings are separated by ':' character. For example, a property string like the following
      conditions_sql: id in (1, 2, 3); include: category, user; 
      order_by: first_name, salary desc; cascade: delete
 
will be converted to a HashMap with the following entries:
      key             =>  value
      conditions_sql  =>  id in (1, 2, 3)
      include         =>  category, user
      order_by        =>  first_name, salary desc
      cascade         =>  delete
 

Parameters:
ownerClass - owner class
type - type of relation
associationId - association name
targetClass - target class
properties - string of properties

setupHasManyThroughRelation

public void setupHasManyThroughRelation(java.lang.Class<? extends ActiveRecord> ownerClass,
                                        java.lang.String targets,
                                        java.lang.String throughAssociation,
                                        java.lang.String properties,
                                        java.util.Map<java.lang.String,java.lang.Object> joinInputs)
Sets up has-many-through relation.

Parameters:
ownerClass - relation owner class
targets - target association name
throughAssociation - the through association name
properties - string of properties
joinInputs - map of input key/value pairs for the join model

createRecordRelation

public RecordRelation createRecordRelation(ActiveRecord record,
                                           java.lang.String associationId)
Creates a RecordRelation between owner (record instance) and its associated model.

Parameters:
record - ActiveRecord instance of the owner
associationId - association name for the target
Returns:
RecordRelation a specific RecordRelation

getAllRelationNameTypes

public java.util.List<java.lang.String> getAllRelationNameTypes(java.lang.Class<? extends ActiveRecord> clz)
Return a list of name type combination for a model class. An item in the list is like: order:invoice = has-one order:item = has-many

Parameters:
clz - ActiveRecord class type
Returns:
List

getOwnedRelations

public java.util.List<Relation> getOwnedRelations(java.lang.Class<? extends ActiveRecord> owner)
Returns a list of relation instances owned by an owner class type.

Parameters:
owner - ActiveRecord class type
Returns:
List of relation instances

getRelations

public java.util.List<Relation> getRelations(java.lang.Class<? extends ActiveRecord> owner,
                                             java.lang.Class<? extends ActiveRecord> target)
Returns a list of relation instances owned by an owner class type with the specific target class type.

Parameters:
owner - owner ActiveRecord class type
target - target ActiveRecord class type
Returns:
List of relation instances

removeRelationsFor

public void removeRelationsFor(java.lang.String model)
Removes all cached relations owned by a model.

Parameters:
model - the owner of the relation

getRelation

public Relation getRelation(java.lang.Class<? extends ActiveRecord> owner,
                            java.lang.String associationId)
Returns relation between owner and target.

Parameters:
owner - relation owner class
associationId - association id for target model in lower case
Returns:
relation

getRelationType

public java.lang.String getRelationType(java.lang.Class<? extends ActiveRecord> owner,
                                        java.lang.Class<? extends ActiveRecord> target)
Returns relation type from owner class to target class.

Parameters:
owner - class type for relation owner
target - class type for relation target
Returns:
relation type

existsBelongsToRelationBetween

public boolean existsBelongsToRelationBetween(java.lang.Class<? extends ActiveRecord> endA,
                                              java.lang.Class<? extends ActiveRecord> endB)
Checks if there is a belongs-to relation between the two class types.

Parameters:
endA - class type for owner class
endB - class type for target class
Returns:
true if endA class belongs-to endB class

existsHasOneRelationBetween

public boolean existsHasOneRelationBetween(java.lang.Class<? extends ActiveRecord> endA,
                                           java.lang.Class<? extends ActiveRecord> endB)
Checks if there is a has-one relation between the two class types.

Parameters:
endA - class type for owner class
endB - class type for target class
Returns:
true if endA class has-one endB class

existsHasManyRelationBetween

public boolean existsHasManyRelationBetween(java.lang.Class<? extends ActiveRecord> endA,
                                            java.lang.Class<? extends ActiveRecord> endB)
Checks if there is a has-many relation between the two class types.

Parameters:
endA - class type for owner class
endB - class type for target class
Returns:
true if endA class has-many endB class

existsHasManyThroughRelationBetween

public boolean existsHasManyThroughRelationBetween(java.lang.Class<? extends ActiveRecord> endA,
                                                   java.lang.Class<? extends ActiveRecord> endB)
Checks if there is a has-many-through relation between the two class types.

Parameters:
endA - class type for owner class
endB - class type for target class
Returns:
true if endA class has-many-through endB class

getBelongsToRelationBetween

public Relation getBelongsToRelationBetween(java.lang.Class<? extends ActiveRecord> endA,
                                            java.lang.Class<? extends ActiveRecord> endB)
Returns a belongs-to relation which exists between the two class types.

Parameters:
endA - class type for owner class
endB - class type for target class
Returns:
a belongs-to relation

getHasOneRelationBetween

public Relation getHasOneRelationBetween(java.lang.Class<? extends ActiveRecord> endA,
                                         java.lang.Class<? extends ActiveRecord> endB)
Returns a has-one relation which exists between the two class types.

Parameters:
endA - class type for owner class
endB - class type for target class
Returns:
a has-one relation

getHasManyRelationBetween

public Relation getHasManyRelationBetween(java.lang.Class<? extends ActiveRecord> endA,
                                          java.lang.Class<? extends ActiveRecord> endB)
Returns a has-many relation which exists between the two class types.

Parameters:
endA - class type for owner class
endB - class type for target class
Returns:
a has-many relation

getHasManyThroughRelationBetween

public Relation getHasManyThroughRelationBetween(java.lang.Class<? extends ActiveRecord> endA,
                                                 java.lang.Class<? extends ActiveRecord> endB)
Returns a has-many-through relation which exists between the two class types.

Parameters:
endA - class type for owner class
endB - class type for target class
Returns:
a has-many-through relation

registerCategory

public void registerCategory(java.lang.Class<? extends ActiveRecord> center,
                             java.lang.String category,
                             java.lang.String idField,
                             java.lang.String typeField)
Registers a Category.

Parameters:
center - center class of the category
category - category name
idField - id field name for the category
typeField - type field name for the category

getRegisteredCategory

public java.util.List<Category> getRegisteredCategory(java.lang.Class<? extends ActiveRecord> center)
Gets a category list that all have the same center class.

Parameters:
center - center class of the category
Returns:
a category list

getCategory

public Category getCategory(java.lang.String category)
Returns a declared Category instance. If the category is not declared, null will be returned.

Parameters:
category - category name
Returns:
category a Category instance if there is one

getCategoryEntity

public java.lang.String getCategoryEntity(java.lang.String category,
                                          java.lang.String type)
Returns model name of a type in a category.

Parameters:
category - category name
type - type name of the model
Returns:
model model name for the type

getDefaultMapping

public java.lang.String getDefaultMapping(java.lang.Class<? extends ActiveRecord> a,
                                          java.lang.String type,
                                          java.lang.String target,
                                          java.lang.Class<? extends ActiveRecord> b)
returns default FK mapping. The default FK mapping follows these rules: For belongsTo relation, class A is the owner of the relation and class A holds the foreign key FK:
 1. If id is not class B's primary key, the mapping is 
    {Class B's primary key}={Class B's primary key}. For 
    example, if class B's PK is order_id, then the mapping is 
    order_id=order_id. Here we assume that order_id is a 
    foreign key column of Class A.
 2. If id is class B's primary key, the mapping is 
    {Class B's model name in lower case}_id=id
    
    Example: Lines belongsTo Order. The default mapping is "order_id=id".
 
For hasMany or hasOne relation, class A is the owner of the relation and class B holds the FK:
 1. If id is not class A's primary key, the mapping is 
    {Class A's primary key}={Class A's primary key}. For 
    example, if class A's PK is order_id, then the mapping is 
    order_id=order_id. Here we assume that order_id is a 
    foreign key column of Class B.
 2. If id is class A's primary key, the mapping is 
    id={Class A's model name in lower case}_id
    
    Example: Order hasMany Lines. The default mapping is "id=order_id".
 

Parameters:
a - The end a class
type - String of type of relation
target - target name of the associated class.
b - The end b class