DBWeb
Class Entity

java.lang.Object
  |
  +--DBWeb.Entity
All Implemented Interfaces:
java.lang.Comparable

public class Entity
extends java.lang.Object
implements java.lang.Comparable

An Entity represents a set of property/value pairs. Properties are always Strings. Values can be either singular or plural, but in either case are normally strings. A plural value may be retrieved in various ways. Entities also contain an ErrRecord, which remembers any errors or warnings associated with the Entity. Created on September 16, 2003, 3:37 PM


Field Summary
static java.lang.String[] fieldOrder
           
 
Constructor Summary
Entity()
          Creates a new instance of Entity
Entity(javax.servlet.http.HttpServletRequest rq)
          Create an Entity from an HttpServletRequest.
Entity(java.sql.ResultSet rs)
          Creates a new instance of Entity from a SQL ResultSet.
 
Method Summary
 void add(java.lang.String key, java.lang.String val)
          Add an association between the key and val.
 void addError(java.lang.String key, java.lang.String err)
          Adds an error message associated with the key to the Entity.
 void addWarning(java.lang.String key, java.lang.String err)
          Adds a warning message associated with the key to the Entity.
 int compareTo(java.lang.Object other)
          Entities should be compared by lexicographic ordering according to some order among their fields.
 void defaultFrom(DBWeb.Entity other)
          Copies over any properties that aren't in this Entity from other.
 java.lang.String get(java.lang.String key)
          Retrieves the value associated with the String key.
 java.lang.String getDmdy(java.lang.String key)
           
 java.lang.String getDmy(java.lang.String key)
           
 java.lang.String getDSql(java.lang.String key)
          Retrieves the value associated with the String key as a date appropriate for storing in a SQL database, e.g., 1995-07-14.
 java.lang.String getDy(java.lang.String key)
           
 java.lang.String getH(java.lang.String key)
          Retrieves value(s) associated with the String key.
 java.util.ArrayList getP(java.lang.String key)
          Retrieves an ArrayList of values associated with the key.
 java.lang.String getQ(java.lang.String key)
          Retrieves value(s) associated with the String key.
 java.lang.String getS(java.lang.String key)
          Retrives the value(s) associated with the String key.
 boolean isOK()
          Is true if there are no errors associated with this Entity.
 void put(java.lang.String key, java.lang.String val)
          Create an association between the key and val, both Strings.
 void putP(java.lang.String key, java.util.Collection c)
          Create an association between the key and all elements of the Collection c, where all are Strings.
 void putP(java.lang.String key, java.lang.String[] vals)
          Create an association between the key and all elements of vals[], where all are Strings.
 void reconcileEntities(EntityModel em, DBWeb.Entity other)
          Compares the field values of this and the OTHER Entity and adds warnings for any fields that differ.
 java.lang.String toErrHtml()
          Returns HTML needed to display any errors or warnings.
 java.lang.String toHtml()
          Returns a String suitable for displaying the content of the Entity as an HTML table.
 java.lang.String toString()
          Returns a compact String representation of an Entity.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

fieldOrder

public static java.lang.String[] fieldOrder
Constructor Detail

Entity

public Entity()
Creates a new instance of Entity


Entity

public Entity(java.sql.ResultSet rs)
Creates a new instance of Entity from a SQL ResultSet. Every field in the ResultSet becomes a property of the Entity.

Parameters:
rs - ResultSet

Entity

public Entity(javax.servlet.http.HttpServletRequest rq)
Create an Entity from an HttpServletRequest. The Entity stores every Parameter name and value as retrieved by the request. Both keys and values are Strings. When the same parameter name appears multiple times (e.g., from a multiple-choice HTML options field), these values are assigned as multiple values.

Parameters:
rq - HttpServletRequest
Method Detail

put

public void put(java.lang.String key,
                java.lang.String val)
Create an association between the key and val, both Strings. Any former value/values associated with the key are lost.

Parameters:
key - A String holding the key.
val - A String holding the value.

putP

public void putP(java.lang.String key,
                 java.lang.String[] vals)
Create an association between the key and all elements of vals[], where all are Strings. Any former value/values associated with the key are lost.

Parameters:
key - A String holding the key.
vals - A String[] holding the values.

putP

public void putP(java.lang.String key,
                 java.util.Collection c)
Create an association between the key and all elements of the Collection c, where all are Strings.

Parameters:
key - A String holding the key.

add

public void add(java.lang.String key,
                java.lang.String val)
Add an association between the key and val. Any previous associations, whether added by put or add are retained and added to.

Parameters:
key - A String holding the key.
val - A String holding the new key to add.

addError

public void addError(java.lang.String key,
                     java.lang.String err)
Adds an error message associated with the key to the Entity.

Parameters:
key - A String holding the key.
err - A String holding the error message.

addWarning

public void addWarning(java.lang.String key,
                       java.lang.String err)
Adds a warning message associated with the key to the Entity.

Parameters:
key - A String holding the key.
err - A String holding the warning message.

get

public java.lang.String get(java.lang.String key)
Retrieves the value associated with the String key. If there is no such associated value, this returns null. If a value has been associated by put, it is simply returned. (It is a String.) If multiple values have been associated by putP or add, then these Strings are concatenated by commas and returned as a single String. To retrieve multiple values separately, see (@link DBWeb.Entity#getP(String)).

Parameters:
key - A String holding the key.
Returns:
A String holding the associated value, or the associated values concatenated by commas. If there are no associated values, null is returned.

getS

public java.lang.String getS(java.lang.String key)
Retrives the value(s) associated with the String key. This is exactly like get, except that instead of ever returning null, it returns the empty string ("").

Parameters:
key - A String holding the key.
Returns:
A String holding the associated value, or the associated values concatenated by commas. If there are no associated values, the empty string ("") is returned.

getH

public java.lang.String getH(java.lang.String key)
Retrieves value(s) associated with the String key. This is like getS, except that the String result is encoded for HTML. I.e., any characters <, >, & and " are replaced by their spelled-out HTML codes, &lt;, ... .

Parameters:
key - A String holding the key.
Returns:
A String holding the associated value, or the associated values concatenated by commas, encoded for SQL. If there are no associated values, the empty string ("") is returned.

getQ

public java.lang.String getQ(java.lang.String key)
Retrieves value(s) associated with the String key. This is like getS, except that the String result is encoded for SQL. I.e., the value is enclosed by single-quotes and any single-quotes in the value are doubled.

Parameters:
key - A String holding the key.
Returns:
A String holding the associated value, or the associated values concatenated by commas, encoded for SQL. If there are no associated values, the empty string ("") is returned.

getDSql

public java.lang.String getDSql(java.lang.String key)
Retrieves the value associated with the String key as a date appropriate for storing in a SQL database, e.g., 1995-07-14. If the associated value is not interpretable as a date, the empty string is returned.

Parameters:
key - A String holding the key.
Returns:
A String holding a date in a format appropriate to store in a SQL data base. If it cannot be interpreted in that form, yields the empty string.

getDmdy

public java.lang.String getDmdy(java.lang.String key)

getDmy

public java.lang.String getDmy(java.lang.String key)

getDy

public java.lang.String getDy(java.lang.String key)

getP

public java.util.ArrayList getP(java.lang.String key)
Retrieves an ArrayList of values associated with the key. Like get, it returns null if there are no such values.

Parameters:
key - A String holding the key.
Returns:
An ArrayList of Strings holding the values, or null if none.

isOK

public boolean isOK()
Is true if there are no errors associated with this Entity.

Returns:
True if there are no errors associated with the Entity.

toString

public java.lang.String toString()
Returns a compact String representation of an Entity.

Overrides:
toString in class java.lang.Object

toHtml

public java.lang.String toHtml()
Returns a String suitable for displaying the content of the Entity as an HTML table.

Returns:
String holding HTML text describing the Entity.

toErrHtml

public java.lang.String toErrHtml()
Returns HTML needed to display any errors or warnings.

Returns:
String holding the HTML. "" if nothing to display.

compareTo

public int compareTo(java.lang.Object other)
Entities should be compared by lexicographic ordering according to some order among their fields. We assume that the static variable fieldOrder holds the appropriate order of fields. Because it defaults to null, usually Entities will need to be subclassed in order to give an appropriate fieldOrder for this type of Entity. By default, we throw an exception if the fieldOrder has not been defined and an attempt is made to invoke compareTo.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
other - The other object to which this should be compared.

reconcileEntities

public void reconcileEntities(EntityModel em,
                              DBWeb.Entity other)
Compares the field values of this and the OTHER Entity and adds warnings for any fields that differ. This is used to compare the Entity that was updated in the database and what is then retrieved from the database, to see whether all changes got properly recorded. We assume that dates have been canonicalized to sql format, so they can be compared via string equality.


defaultFrom

public void defaultFrom(DBWeb.Entity other)
Copies over any properties that aren't in this Entity from other.

Parameters:
other - The Entity from which to default properties