com.scooterframework.orm.activerecord
Class AssociationHelper

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

public class AssociationHelper
extends java.lang.Object

Helper class has helper methods for ActiveRecord.


Constructor Summary
AssociationHelper()
           
 
Method Summary
static void hasManyInCategoryThrough(java.lang.Class<? extends ActiveRecord> owner, java.lang.Class<? extends ActiveRecord>[] targets, java.lang.String category, java.lang.Class<? extends ActiveRecord> through)
          This method adds a bunch of methods in many classes.
static void hasManyInCategoryThrough(java.lang.Class<? extends ActiveRecord> owner, java.lang.Class<? extends ActiveRecord>[] targets, java.lang.String category, java.lang.Class<? extends ActiveRecord> through, java.util.Map<java.lang.String,java.lang.Object>[] acJoinInputs, java.lang.String[] abProperties, java.lang.String[] types, java.lang.String relationType, java.lang.String[] bcProperties, java.util.Map<java.lang.String,java.lang.Object>[] bcJoinInputs, java.lang.String[] cbProperties, java.lang.String[] baProperties)
          This method adds a bunch of methods in many classes.
static void populateFKInBelongsTo(ActiveRecord owner, java.util.Map<java.lang.String,java.lang.String> mappingMap, ActiveRecord target)
          Populates foreign key value in a belongs-to relation.
static void populateFKInHasMany(ActiveRecord owner, java.util.Map<java.lang.String,java.lang.String> mappingMap, ActiveRecord target)
          Populates foreign key value in a has-many relation.
static void populateFKInHasOne(ActiveRecord owner, java.util.Map<java.lang.String,java.lang.String> mappingMap, ActiveRecord target)
          Populates foreign key value in a has-one relation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AssociationHelper

public AssociationHelper()
Method Detail

hasManyInCategoryThrough

public static void hasManyInCategoryThrough(java.lang.Class<? extends ActiveRecord> owner,
                                            java.lang.Class<? extends ActiveRecord>[] targets,
                                            java.lang.String category,
                                            java.lang.Class<? extends ActiveRecord> through)
This method adds a bunch of methods in many classes.
  1. A has-many-through association from owner to each target class.
  2. A has-many association from each target to through class.
  3. A has-many-through association from each target to owner class.
  4. A belongs-to association from through to each target class.
In order to establish the associations, the method assumes the following:
  1. The type value of the category type column is the model name of each corresponding target class.
  2. The primary key of each target class is "id".
  3. The mapping string between each target class and through class is "id= category's id column".
  4. The association property from each target to through contains "cascade: delete".

If any of the above assumptions are not satisfied, you need to use the other hasManyInCategoryThrough method which gives you more control on specifying the associations.

Example usage:

Assuming there are image files and text files in a folder. We create three models: images, texts, folders. We also use linkings model to link folders with images and texts files. We will create the following classes:

  CREATE TABLE linkings (
      id INTEGER AUTO_INCREMENT,
      folder_id INTEGER,
      linkable_id INTEGER,
      linkable_type VARCHAR(20),
      PRIMARY KEY(id)
  )
  
  class Linking extends ActiveRecord {
      public void registerRelations() {
          belongsTo(Folder.class);
          belongsToCategory("linkable");
      }
  }
  
  class Folder extends ActiveRecord {
      public void registerRelations() {
          hasMany(Linking.class);
          AssociationHelper.hasManyInCategoryThrough(Folder.class, 
                                  new Class[]{Image.class, Text.class}, 
                                  "linkable", Linking.class);
      }
  }
  
  class Image extends ActiveRecord {
  }
  
  class Text extends ActiveRecord {
  }
 
The following codes show how to get total of ownership for a customer:
      //Find all ownerships of a customer:
      ActiveRecord customerHome = ActiveRecordUtil.getHomeInstance(Customer.class);
      ActiveRecord customer = customerHome.find("id=1");
      int total = customer.allAssociatedInCategory("ownerable").size();
 
It is also easy to add a dvd to the ownership of the customer:
      Assign a dvd to a customer:
      ActiveRecord dvdHome = ActiveRecordUtil.getHomeInstance(Dvd.class);
      ActiveRecord dvd = dvdHome.find("id=4");
      List dvds = customer.allAssociatedInCategory("ownerable").add(dvd).getRecords();
 

Parameters:
owner - owner class
targets - array of target classes
category - the category which the targets act as
through - the middle join class between owner and targets

hasManyInCategoryThrough

public static void hasManyInCategoryThrough(java.lang.Class<? extends ActiveRecord> owner,
                                            java.lang.Class<? extends ActiveRecord>[] targets,
                                            java.lang.String category,
                                            java.lang.Class<? extends ActiveRecord> through,
                                            java.util.Map<java.lang.String,java.lang.Object>[] acJoinInputs,
                                            java.lang.String[] abProperties,
                                            java.lang.String[] types,
                                            java.lang.String relationType,
                                            java.lang.String[] bcProperties,
                                            java.util.Map<java.lang.String,java.lang.Object>[] bcJoinInputs,
                                            java.lang.String[] cbProperties,
                                            java.lang.String[] baProperties)
This method adds a bunch of methods in many classes.
 
  • A has-many-through association from owner to each target class.
  • A has-many association from each target to through class.
  • A has-many-through association from each target to owner class.
  • A belongs-to association from through to each target class.
  • Assuming owner class is A, target class is B, through class is C,
     abProperties is join properties from A to B, 
     bcProperties is join properties from B to C, 
     cbProperties is join properties from C to B, 
     baProperties is join properties from B to A.
     

    Parameters:
    owner - owner class
    targets - array of target classes
    category - the category which the targets act as
    through - the middle join class between owner and targets
    acJoinInputs - array of data map for the join through table.
    abProperties - properties from owner to target class
    types - array of join types in the category, default to model name
    relationType - either has-many or has-one
    bcProperties - array of properties from each target to through class
    bcJoinInputs - array of data map for the join through table.
    cbProperties - array of properties from through to each target class
    baProperties - array of properties from each target to owner class

    populateFKInBelongsTo

    public static void populateFKInBelongsTo(ActiveRecord owner,
                                             java.util.Map<java.lang.String,java.lang.String> mappingMap,
                                             ActiveRecord target)
    Populates foreign key value in a belongs-to relation. In a belongs-to relation, the owner record should hold the foreign key value. Therefore, the foreign-key fields in the owner record is going to be set with data of the corresponding fields from the target record.

    Parameters:
    owner - the owner record of the relation
    mappingMap - relation mapping from owner to target
    target - the target record in the relation

    populateFKInHasMany

    public static void populateFKInHasMany(ActiveRecord owner,
                                           java.util.Map<java.lang.String,java.lang.String> mappingMap,
                                           ActiveRecord target)
    Populates foreign key value in a has-many relation. In a has-many relation, the target record should hold the foreign key value. Therefore, the foreign-key fields in the target record is going to be set with data of the corresponding fields from the owner record.

    Parameters:
    owner - the owner record of the relation
    mappingMap - relation mapping from owner to target
    target - the target record in the relation

    populateFKInHasOne

    public static void populateFKInHasOne(ActiveRecord owner,
                                          java.util.Map<java.lang.String,java.lang.String> mappingMap,
                                          ActiveRecord target)
    Populates foreign key value in a has-one relation. In a has-one relation, the target record should hold the foreign key value. Therefore, the foreign-key fields in the target record is going to be set with data of the corresponding fields from the owner record.

    Parameters:
    owner - the owner record of the relation
    mappingMap - relation mapping from owner to target
    target - the target record in the relation