com.scooterframework.orm.activerecord
Class Relation

java.lang.Object
  extended by com.scooterframework.orm.activerecord.Relation
Direct Known Subclasses:
BelongsToRelation, HasManyRelation, HasManyThroughRelation, HasOneRelation

public abstract class Relation
extends java.lang.Object

Relation class has information about relationship between objects. There are three types of binary relations supported:

 1. has-one:      A has one B.
 2. has-many:     A has many B.
 3. belongs-to:   A belongs to B.
 
For has-many-through relation, the HasManyThroughRelation subclass should be used.


Field Summary
protected  java.lang.String associationId
           
static java.lang.String BELONGS_TO_TYPE
           
static java.lang.String CASCADE_DELETE
          Cascade key to indicate the delete of child record(s).
static java.lang.String CASCADE_NONE
          Cascade key to indicate no cascade effect.
static java.lang.String CASCADE_NULLIFY
          Cascade key to indicate the nullifying of the foreign key field in its child record(s).
static java.lang.String CASCADE_SIMPLY_DELETE
          Simply delete the children without triggering any actions caused by the removal of the child record such as counter decrement in its parent record.
static java.lang.String HAS_MANY_THROUGH_TYPE
           
static java.lang.String HAS_MANY_TYPE
           
static java.lang.String HAS_ONE_TYPE
           
protected  java.lang.String key
           
protected  java.lang.String mapping
           
protected  java.lang.Class<? extends ActiveRecord> ownerClass
           
protected  java.lang.String ownerModel
           
protected  java.util.Map<java.lang.String,java.lang.String> properties
           
static java.lang.String REVERSE_RELATION
          Specifies reverse relation name.
protected  java.lang.Class<? extends ActiveRecord> targetClass
           
protected  java.lang.String targetModel
           
protected  java.lang.String type
           
 
Constructor Summary
Relation(java.lang.Class<? extends ActiveRecord> ownerClass, java.lang.String type, java.lang.String associationId, java.lang.String targetModel)
           
 
Method Summary
 boolean allowCascade(java.lang.String cascadeType)
          Checks if a cascade type is allowed.
 boolean allowCascadeDelete()
          Checks if the value of cascade property is delete.
 boolean allowCascadeNone()
          Checks if the value of cascade property is none.
 boolean allowCascadeNullify()
          Checks if the value of cascade property is nullify.
 boolean allowCascadeSimplyDelete()
          Checks if the value of cascade property is simply_delete.
 java.lang.String getAssociation()
          Returns the association target.
 java.lang.String getConditionsString()
          Returns the conditions_sql in the property map.
 java.lang.String getConditionsString(java.lang.String tableName, java.lang.String aliasTableName)
          Returns the conditions_sql in the property map.
 java.lang.String[] getLeftSideMappingItems()
          Returns an array of all strings on the left-side of the mapping.
 java.lang.String getMapping()
          Returns the mapping string.
 java.util.Map<java.lang.String,java.lang.String> getMappingMap()
          Returns a Map of mapping string For belongs-to relation, the key is the foreign-key column of Class A, while the corresponding value is the primary-key field of Class B.
 java.lang.Class<? extends ActiveRecord> getOwnerClass()
          Returns owner class.
 java.lang.String getOwnerModel()
          Returns owner model name.
 java.util.Map<java.lang.String,java.lang.String> getProperties()
          Returns the property map.
 java.lang.String getRelationKey()
          Returns a representation of the relation.
 java.lang.String getRelationType()
          Returns the relation type.
 java.lang.String getReverseMapping()
          Returns the reverse mapping.
 java.util.Map<java.lang.String,java.lang.String> getReverseMappingMap()
           
 Relation getReverseRelation()
          Returns reverse relation.
 java.lang.String getReverseRelationName()
          Returns reverse relation association id.
 java.lang.String getReverseRelationType()
          Returns the type of the reverse relation.
 java.lang.String[] getRightSideMappingItems()
          Returns an array of all strings on the right-side of the mapping.
 java.lang.Class<? extends ActiveRecord> getTargetClass()
          Returns target class.
 java.lang.String getTargetModel()
          Returns target model name.
 void setProperties(java.util.Map<java.lang.String,java.lang.String> properties)
          Sets the property map.
 void setRelationKey(java.lang.String key)
          Sets relation key.
 void setTargetClass(java.lang.Class<? extends ActiveRecord> targetClass)
          Sets target class.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

BELONGS_TO_TYPE

public static final java.lang.String BELONGS_TO_TYPE
See Also:
Constant Field Values

HAS_ONE_TYPE

public static final java.lang.String HAS_ONE_TYPE
See Also:
Constant Field Values

HAS_MANY_TYPE

public static final java.lang.String HAS_MANY_TYPE
See Also:
Constant Field Values

HAS_MANY_THROUGH_TYPE

public static final java.lang.String HAS_MANY_THROUGH_TYPE
See Also:
Constant Field Values

CASCADE_NONE

public static final java.lang.String CASCADE_NONE
Cascade key to indicate no cascade effect. This is the default case.

See Also:
Constant Field Values

CASCADE_NULLIFY

public static final java.lang.String CASCADE_NULLIFY
Cascade key to indicate the nullifying of the foreign key field in its child record(s).

See Also:
Constant Field Values

CASCADE_DELETE

public static final java.lang.String CASCADE_DELETE
Cascade key to indicate the delete of child record(s). This cascade type will trigger actions caused by the removal of the child record such as counter decrement in its parent record.

See Also:
Constant Field Values

CASCADE_SIMPLY_DELETE

public static final java.lang.String CASCADE_SIMPLY_DELETE
Simply delete the children without triggering any actions caused by the removal of the child record such as counter decrement in its parent record.

See Also:
Constant Field Values

REVERSE_RELATION

public static final java.lang.String REVERSE_RELATION
Specifies reverse relation name.

See Also:
Constant Field Values

ownerClass

protected java.lang.Class<? extends ActiveRecord> ownerClass

type

protected java.lang.String type

associationId

protected java.lang.String associationId

targetClass

protected java.lang.Class<? extends ActiveRecord> targetClass

ownerModel

protected java.lang.String ownerModel

targetModel

protected java.lang.String targetModel

mapping

protected java.lang.String mapping

properties

protected java.util.Map<java.lang.String,java.lang.String> properties

key

protected java.lang.String key
Constructor Detail

Relation

public Relation(java.lang.Class<? extends ActiveRecord> ownerClass,
                java.lang.String type,
                java.lang.String associationId,
                java.lang.String targetModel)
Method Detail

getRelationType

public java.lang.String getRelationType()
Returns the relation type.

Returns:
the relation type

getAssociation

public java.lang.String getAssociation()
Returns the association target.

Returns:
the association target

getReverseRelationType

public java.lang.String getReverseRelationType()

Returns the type of the reverse relation. For has-one and has-many relations, the reverse relation type must always be of belongs-to.

To ease any confusion, it is better to state reverse relation by using the reverse key word in the properties attribute when declaring a relation.

This is mostly used by a belongs-to class to figure out if the reverse relation type is has-one or has-many.

Returns:
the reverse relation type

getReverseRelation

public Relation getReverseRelation()

Returns reverse relation.

To ease any confusion, it is better to state reverse relation by using the reverse key word in the properties attribute when declaring a relation.

The reverse relation for a has-one or has-many relation is always of belongs-to type.

Returns:
reverse relation

getReverseRelationName

public java.lang.String getReverseRelationName()

Returns reverse relation association id.

To ease any confusion, it is better to state reverse relation by using the reverse key word in the properties attribute when declaring a relation.

Returns:
reverse relation name

getOwnerClass

public java.lang.Class<? extends ActiveRecord> getOwnerClass()
Returns owner class.


getTargetClass

public java.lang.Class<? extends ActiveRecord> getTargetClass()
Returns target class.


setTargetClass

public void setTargetClass(java.lang.Class<? extends ActiveRecord> targetClass)
Sets target class.

Parameters:
targetClass - target class

getOwnerModel

public java.lang.String getOwnerModel()
Returns owner model name.


getTargetModel

public java.lang.String getTargetModel()
Returns target model name.


getMapping

public java.lang.String getMapping()
Returns the mapping string. The mapping string is like: "id=order_id" where id is primary key of Order record and order_id is foreign-key of Invoice record.


getReverseMapping

public java.lang.String getReverseMapping()
Returns the reverse mapping.


getLeftSideMappingItems

public java.lang.String[] getLeftSideMappingItems()
Returns an array of all strings on the left-side of the mapping.

Returns:
an array of all strings on the left-side of the mapping.

getRightSideMappingItems

public java.lang.String[] getRightSideMappingItems()
Returns an array of all strings on the right-side of the mapping.

Returns:
an array of all strings on the right-side of the mapping.

getMappingMap

public java.util.Map<java.lang.String,java.lang.String> getMappingMap()
Returns a Map of mapping string For belongs-to relation, the key is the foreign-key column of Class A, while the corresponding value is the primary-key field of Class B. For has-one and has-many relations, the key is the primary-key field of Class A, while the corresponding value is the foreign-key field of Class B.

Returns:
a Map of mapping string

getReverseMappingMap

public java.util.Map<java.lang.String,java.lang.String> getReverseMappingMap()

getProperties

public java.util.Map<java.lang.String,java.lang.String> getProperties()
Returns the property map.


getConditionsString

public java.lang.String getConditionsString()
Returns the conditions_sql in the property map.


getConditionsString

public java.lang.String getConditionsString(java.lang.String tableName,
                                            java.lang.String aliasTableName)
Returns the conditions_sql in the property map. Replaces table name with mapping alias table name.


setProperties

public void setProperties(java.util.Map<java.lang.String,java.lang.String> properties)
Sets the property map.


getRelationKey

public java.lang.String getRelationKey()
Returns a representation of the relation.

Returns:
relation key string

setRelationKey

public void setRelationKey(java.lang.String key)
Sets relation key.

Parameters:
key - relation key string

allowCascadeNone

public boolean allowCascadeNone()
Checks if the value of cascade property is none.

Returns:
true if the value is none

allowCascadeNullify

public boolean allowCascadeNullify()
Checks if the value of cascade property is nullify.

Returns:
true if the value is nullify

allowCascadeDelete

public boolean allowCascadeDelete()
Checks if the value of cascade property is delete.

Returns:
true if the value is delete

allowCascadeSimplyDelete

public boolean allowCascadeSimplyDelete()
Checks if the value of cascade property is simply_delete.

Returns:
true if the value is delete

allowCascade

public boolean allowCascade(java.lang.String cascadeType)
Checks if a cascade type is allowed.

Parameters:
cascadeType - a cascade type string constant
Returns:
true if the cascade type is allowed.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object