DBWeb
Class EntityModel

java.lang.Object
  |
  +--DBWeb.EntityModel

public class EntityModel
extends java.lang.Object

An EntityModel describes the relationship between an Entity and a table or tables in a database. The baseTable should identify the table each of whose rows corresponds to a single Entity. In addition, DetailModels can identify additional tables that hold 1-n relationships to these Entities. When an Entity is retrieved from the database, such details are used to populate multi-valued associations in the Entity with data from these details. EntityModels are mostly used when retrieving information for update. For display only, it may be more convenient simply to construct a complex SQL expression that retrieves all the needed data, and to code explicitly further retrieval expressions to get the details.


Field Summary
 boolean[] asDate
           
 boolean asName
           
 java.lang.String baseTable
           
 java.lang.String condition
           
 java.util.ArrayList details
           
 java.lang.String fieldNames
           
 java.util.ArrayList fieldNamesA
           
 java.lang.String keyName
           
 
Constructor Summary
EntityModel(java.lang.String mainSpec)
           
EntityModel(java.lang.String mainSpec, java.lang.String[] detailSpecs)
          Creates a new instance of EntityModel, from a compact form.
EntityModel(java.lang.String table, java.lang.String key, java.lang.String fields, java.lang.String cond)
          Creates a new instance of EntityModel
 
Method Summary
 void addDetail(DetailModel dm)
           
 void addDetail(java.lang.String table, java.lang.String key, java.lang.String field)
           
 void addDetail(java.lang.String table, java.lang.String key, java.lang.String field, java.lang.String name)
          Adds a detail relationship to an EntityModel.
 java.lang.String toSql()
           
 java.lang.String toSql(java.lang.String id)
          Creates a SQL query that retrieves the modeled Entity identified by key id.
 java.lang.String toSqlDelete(Entity b)
           
 java.lang.String toSqlInsert(Entity b)
          Creates a SQL query that inserts the modeled Entity.
 java.lang.String toSqlUpdate(Entity b)
          Creates a SQL command that updates the modeled Entity.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

baseTable

public java.lang.String baseTable

keyName

public java.lang.String keyName

fieldNames

public java.lang.String fieldNames

fieldNamesA

public java.util.ArrayList fieldNamesA

condition

public java.lang.String condition

asName

public boolean asName

asDate

public boolean[] asDate

details

public java.util.ArrayList details
Constructor Detail

EntityModel

public EntityModel(java.lang.String mainSpec,
                   java.lang.String[] detailSpecs)
            throws java.text.ParseException
Creates a new instance of EntityModel, from a compact form.

Parameters:
mainSpec - String specifying the table name and list of fields that hold data about the entity. The format is table:key:f1,f2,...fn:cond table is the sql table holding the base information key is the primary key field f1,f2,...fn are the other fields (should not include key) The cond, if any, must be proper SQL for the conditon. Omit : if none. If the table is followed by =n, the Entity retrieved is treated as an EnglishName and fields fullname and fullFNF are added, for the last-name-first full name, and first-name-first. If any field name is followed by =d, it is treated as a date, and converted to the required SQL form of date before being stored or updated.
detailSpecs - String[] specifying the detail models. Each has form: table: key: field: name: c1=v1,c2=v2,...ck=vk where table holds the editable 1-n relationship, key is its key field, field is the field whose values are to be retrieved, name is the property of the main object having field as its multiple values Unlike for the baseTable, detailSpecs normally specify indices into other tables, not names or dates; hence no =n, =d support.

EntityModel

public EntityModel(java.lang.String mainSpec)
            throws java.text.ParseException

EntityModel

public EntityModel(java.lang.String table,
                   java.lang.String key,
                   java.lang.String fields,
                   java.lang.String cond)
            throws java.text.ParseException
Creates a new instance of EntityModel

Parameters:
table - String naming the base table.
key - String naming the field in table that holds the primary key.
fields - String naming the fields of the table to retrieve as single-valued associations with the field name. These are separated by commas. This should NOT include the key.
cond - String holding a WHERE condition that is added to the SQL.
Method Detail

addDetail

public void addDetail(java.lang.String table,
                      java.lang.String key,
                      java.lang.String field,
                      java.lang.String name)
Adds a detail relationship to an EntityModel.

Parameters:
table - String holding the name of the 1-n relationship table.
key - String holding field name in table that is the foreign key identifying the base Entity.
field - String holding the field name whose values are to become associated with this field name as multiple values in the Entity.
name - String holding the name with which to associate these retrived values, if field is not appropriate.

addDetail

public void addDetail(java.lang.String table,
                      java.lang.String key,
                      java.lang.String field)

addDetail

public void addDetail(DetailModel dm)

toString

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

toSql

public java.lang.String toSql(java.lang.String id)
Creates a SQL query that retrieves the modeled Entity identified by key id.

Parameters:
id - String holding the key.
Returns:
SQL query as a String.

toSql

public java.lang.String toSql()

toSqlInsert

public java.lang.String toSqlInsert(Entity b)
Creates a SQL query that inserts the modeled Entity. Note that, in addition to executing this Update, one also needs to determine the auto_increment'ed new id and to create and execute commands to insert the related data specified by DetailModels. Furthermore, any condition specified in the EntityModel is not automatically made to be satisfied by the insertion. (This differs from the treatment of conditions for DetailModels.) In addition, any items in teh Entity marked by the EntityModel as dates are converted into SQL-style dates.

Parameters:
b - Entity to be inserted.
Returns:
String holding the SQL statement.

toSqlDelete

public java.lang.String toSqlDelete(Entity b)

toSqlUpdate

public java.lang.String toSqlUpdate(Entity b)
Creates a SQL command that updates the modeled Entity. The primary key given by the keyName entry must be non-null. Note that this command will only update the baseTable of the Entity. In addition, one must execute commands to update tables specifying the DetailModels associated with the EntityModel.

Parameters:
b - Entity to be updated.
Returns:
String holding the SQL statement.