org.sormula.active
Class ActiveRecord<R extends ActiveRecord<R>>

java.lang.Object
  extended by org.sormula.active.ActiveRecord<R>
Type Parameters:
R - record type
All Implemented Interfaces:
java.io.Serializable, LazySelectable

public abstract class ActiveRecord<R extends ActiveRecord<R>>
extends java.lang.Object
implements LazySelectable, java.io.Serializable

Base class for active records. See table(Class) for an example how to add a static member named table to the sublcass that extends ActiveRecord.

Since:
1.7 and 2.1
Author:
Jeff Miller
See Also:
Serialized Form

Constructor Summary
ActiveRecord()
          Constructs.
 
Method Summary
 void attach(ActiveDatabase activeDatabase)
          Associates an active database with this active record.
 void checkLazySelects(java.lang.String fieldName)
          Selects record(s) from database for field based upon definitions in select annotations of field where SelectCascade.lazy() is true.
protected  ActiveTable<R> createTable()
          Creates an ActiveTable for record class returned by getRecordClass().
 int delete()
          Deletes record from database.
 void detach()
          Sets active database to null.
 ActiveDatabase getActiveDatabase()
          Gets database set by attach(ActiveDatabase).
 java.lang.Class<R> getRecordClass()
          Class definition of this record.
 int insert()
          Inserts record into database.
 void pendingLazySelects(Database database)
          Informs a lazy selector that there are lazy selects to be performed.
 int save()
          Saves record into database.
static
<R extends ActiveRecord<R>>
ActiveTable<R>
table(java.lang.Class<R> recordClass)
          Creates a table that can be used to for records of type recordClass for the default active database.
 int update()
          Updates record in database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActiveRecord

public ActiveRecord()
Constructs.

Method Detail

getActiveDatabase

public ActiveDatabase getActiveDatabase()
Gets database set by attach(ActiveDatabase).

Returns:
active database for this active record

attach

public void attach(ActiveDatabase activeDatabase)
Associates an active database with this active record. insert(), save(), update(), and delete() methods use active database to know what database to act upon. The method is invoked by ActiveTable methods insert or modify table or when an active record is selected as result of a cascade. This method must be invoked on an active record that was created with new operator prior to using insert(), save(), update(), and delete().

Parameters:
activeDatabase - the database for this record

detach

public void detach()
Sets active database to null.


table

public static <R extends ActiveRecord<R>> ActiveTable<R> table(java.lang.Class<R> recordClass)
                                                    throws ActiveException
Creates a table that can be used to for records of type recordClass for the default active database. Use to initialize active record static table member like the following:
 public class SomeRecord extends ActiveRecord≶SomeRecord>
 {
     private static final long serialVersionUID = 1L;
     public static final ActiveTable<SomeRecord> table = table(SomeRecord.class);
     ...
 }
 
Use like:
 List<SomeRecord> records = SomeRecord.table.selectAll();
 

Parameters:
recordClass - type of records of table
Returns:
ActiveTable instance for records of type R
Throws:
ActiveException - if error

save

public int save()
         throws ActiveException
Saves record into database. Delegates to ActiveTable.save(ActiveRecord). The database used is getActiveDatabase(). If no active database is set for this record, then the default active database is used, ActiveDatabase.getDefault().

Returns:
number of records affected; typically 1 if record was saved or 0 if not saved
Throws:
ActiveException - if error

insert

public int insert()
           throws ActiveException
Inserts record into database. Delegates to ActiveTable.insert(ActiveRecord). The database used is getActiveDatabase(). If no active database is set for this record, then the default active database is used, ActiveDatabase.getDefault().

Returns:
number of records affected; typically 1 if record was inserted or 0 if not inserted
Throws:
ActiveException - if error

update

public int update()
           throws ActiveException
Updates record in database. Delegates to ActiveTable.update(ActiveRecord). The database used is getActiveDatabase(). If no active database is set for this record, then the default active database is used, ActiveDatabase.getDefault().

Returns:
number of records affected; typically 1 if record was updated or 0 if not updated
Throws:
ActiveException - if error

delete

public int delete()
           throws ActiveException
Deletes record from database. Delegates to ActiveTable.delete(ActiveRecord). The database used is getActiveDatabase(). If no active database is set for this record, then the default active database is used, ActiveDatabase.getDefault().

Returns:
number of records affected; typically 1 if record was deleted or 0 if not deleted
Throws:
ActiveException - if error

getRecordClass

public java.lang.Class<R> getRecordClass()
Class definition of this record.

Returns:
Object.getClass()

checkLazySelects

public void checkLazySelects(java.lang.String fieldName)
                      throws ActiveException
Selects record(s) from database for field based upon definitions in select annotations of field where SelectCascade.lazy() is true. Typically this method is invoked by the "get" method associated with field. For example:
    public List<SomeChild> getChildList()
    {
        lazySelectCascade("childList");
        return childList;
    }
    

This method may be invoked more than once per field but the field will only be selected upon the first invocation. Each time that an active record is selected, all lazy select cascaded fields are initialized by pendingLazySelects(Database). Invoking this method on an active record that has not been selected (created with new operator) has no affect.

Specified by:
checkLazySelects in interface LazySelectable
Parameters:
fieldName - field to select
Throws:
ActiveException - if error
Since:
1.8 and 2.2

pendingLazySelects

public void pendingLazySelects(Database database)
Informs a lazy selector that there are lazy selects to be performed. Invoked by ScalarSelectOperation.readNext() when a least one field has a SelectCascade.lazy() set to true. Use Table.getLazySelectCascadeFields() to know which fields are to be lazily selected.

Specified by:
pendingLazySelects in interface LazySelectable
Parameters:
database - the database where lazy select fields are to be read

createTable

protected ActiveTable<R> createTable()
                                                      throws ActiveException
Creates an ActiveTable for record class returned by getRecordClass(). Default active database is used if none is available for this record.

Returns:
active table for performing database operations on this record
Throws:
ActiveException - if error