com.objectwave.persist.constraints
Class Constraint

java.lang.Object
  |
  +--com.objectwave.persist.constraints.Constraint
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable
Direct Known Subclasses:
ConstraintAnyOf, ConstraintBetween, ConstraintBoolean, ConstraintCompare, ConstraintIsNull, ConstraintSubSelect, CrossField

public abstract class Constraint
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

Abstract class for all query constraints. Remember, after creating one of these guys, you should add a line to the ConstraintFactory's static{} block.

Version:
$Id: Constraint.java,v 2.1 2002/02/08 22:41:28 dave_hoag Exp $
Author:
Steven Sinclair
See Also:
Serialized Form

Field Summary
protected  java.lang.String columnName
           
protected static ObjectFormatter defaultObjectFormatter
           
protected  ObjectFormatter objectFormatter
           
 
Constructor Summary
Constraint()
           
 
Method Summary
abstract  boolean checkConstraint(java.lang.Object dataField, java.lang.Object queryField)
          Check to see if a given object passes this constraint.
 boolean checkConstraint(SQLQuery query, Persistence poolElement, java.lang.Object dataField, java.lang.Object queryField)
           
protected static java.lang.Class classOfField(java.lang.String field, java.lang.Object baseObj)
          This is a handy method for formatting output and validating input for specific fields.
 java.lang.Object clone()
          Publicize the clone method.
abstract  java.lang.String constructQueryString()
          Return the string which will appear in the query string.
 boolean equals(java.lang.Object obj)
          A rudimentary but effective equals() method for subclasses of Constraint.
 boolean findField(java.lang.String field)
           
protected  java.lang.String formatString(java.lang.String input)
          This is to abstract the need to get input in the correct format for a given field object, based on the class of the field indicated by getField() and getPersistence().
abstract  void fromString(java.lang.String str)
          Parse the string.
 java.lang.String getColumnName()
           
 java.lang.String getField()
          A given constraint instance will be associated with a field.
 boolean getNot()
          Gets the Not attribute of the Constraint object
 ObjectFormatter getObjectFormatter()
           
 Persistence getPersistence()
          The field is specified as a field on a given persistent object.
abstract  java.util.Enumeration getStaticList()
          Gets the StaticList attribute of the Constraint object
abstract  java.lang.String getType()
          This is a unique, readable string identifying the constraint type (ex, anyof, null)
 boolean isComparisonValueFromPersistentObject()
           
 boolean isUsingColumnName()
           
 void setColumnName(java.lang.String colName)
           
 void setField(java.lang.String field)
          Sets the Field attribute of the Constraint object
 void setNot(boolean not)
          Sets the Not attribute of the Constraint object
 void setObjectFormatter(ObjectFormatter value)
           
 void setPersistence(Persistence p)
          Sets the Persistence attribute of the Constraint object
protected static java.lang.String sqlString(java.lang.String str)
          Provide a generic method for converting a string to a SQL-type string:
abstract  void staticListInsert(java.lang.String field)
          Each type of constraint will have a specific set of fields associated with it.
abstract  java.lang.String stringify()
          Stringify the object.
protected  java.lang.Object stringToObject(java.lang.String str, java.lang.Class classOfStr)
           
 java.lang.String toString()
          toString returns the stringified object.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultObjectFormatter

protected static ObjectFormatter defaultObjectFormatter

columnName

protected java.lang.String columnName

objectFormatter

protected transient ObjectFormatter objectFormatter
Constructor Detail

Constraint

public Constraint()
Method Detail

classOfField

protected static java.lang.Class classOfField(java.lang.String field,
                                              java.lang.Object baseObj)
                                       throws java.lang.NoSuchFieldException
This is a handy method for formatting output and validating input for specific fields. baseObj would presumably be getPersistence().

Parameters:
field -
baseObj -
Returns:
Throws:
java.lang.NoSuchFieldException

sqlString

protected static java.lang.String sqlString(java.lang.String str)
Provide a generic method for converting a string to a SQL-type string:

Parameters:
str -
Returns:

setObjectFormatter

public void setObjectFormatter(ObjectFormatter value)
Parameters:
value - The new ObjectFormatter value

setField

public void setField(java.lang.String field)
Sets the Field attribute of the Constraint object

Parameters:
field - The new Field value

setNot

public void setNot(boolean not)
Sets the Not attribute of the Constraint object

Parameters:
not - The new Not value

setPersistence

public void setPersistence(Persistence p)
Sets the Persistence attribute of the Constraint object

Parameters:
p - The new Persistence value

setColumnName

public void setColumnName(java.lang.String colName)
Parameters:
colName - The new ColumnName value

getObjectFormatter

public ObjectFormatter getObjectFormatter()
Returns:
The ObjectFormatter value

getField

public java.lang.String getField()
A given constraint instance will be associated with a field. Since most constraints can be preceeded by "not", it has been defined here, in the base class.

Returns:
The Field value

getNot

public boolean getNot()
Gets the Not attribute of the Constraint object

Returns:
The Not value

getPersistence

public Persistence getPersistence()
The field is specified as a field on a given persistent object. This is the persistent object with that field.

Returns:
The Persistence value

getStaticList

public abstract java.util.Enumeration getStaticList()
Gets the StaticList attribute of the Constraint object

Returns:
The StaticList value

getType

public abstract java.lang.String getType()
This is a unique, readable string identifying the constraint type (ex, anyof, null)

Returns:
The Type value

isComparisonValueFromPersistentObject

public boolean isComparisonValueFromPersistentObject()
Returns:
true If the constraint object should use the comparison value from the persistent object.

isUsingColumnName

public boolean isUsingColumnName()
Returns:
The UsingColumnName value

getColumnName

public java.lang.String getColumnName()
Returns:
The ColumnName value

checkConstraint

public abstract boolean checkConstraint(java.lang.Object dataField,
                                        java.lang.Object queryField)
Check to see if a given object passes this constraint.

Parameters:
dataField -
queryField -
Returns:
boolean: true iif the object dataField is acceptable by the rules of this constraint instance.

checkConstraint

public boolean checkConstraint(SQLQuery query,
                               Persistence poolElement,
                               java.lang.Object dataField,
                               java.lang.Object queryField)
Parameters:
query -
poolElement -
dataField -
queryField -
Returns:

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Publicize the clone method.

Overrides:
clone in class java.lang.Object
Returns:
Throws:
java.lang.CloneNotSupportedException

constructQueryString

public abstract java.lang.String constructQueryString()
Return the string which will appear in the query string. That is, what should follow the field specification? (ex, "NOT IN ('cat','dog')"). If this constraint is simply an operator, and the original comparison value should go on the RHS, then the last character should be a '*'. Ex, If the field is "quote.status", and the field' value has been specified to be "Q%", then returning "MATCHES*" will produce "quote.status MATCHES Q%". Ex, Using the above example, returning "NOT IN ('cat','dog')" will produce "quote.status NOT IN ('cat','dog')". Note that the returned string is never padded with spaces. Note that this method will return null if the string cannot be constructed for some reason (an messafe will already have been presented to the user. Perhaps I should delegate that responsability to the calling method... hmmm....)

Returns:

equals

public boolean equals(java.lang.Object obj)
A rudimentary but effective equals() method for subclasses of Constraint.

Overrides:
equals in class java.lang.Object
Parameters:
obj -
Returns:

findField

public boolean findField(java.lang.String field)
Parameters:
field -
Returns:

fromString

public abstract void fromString(java.lang.String str)
                         throws java.text.ParseException
Parse the string. Probably a string encoded via the toString() call, this method attempts to build the object's state from the information in string "str".

Parameters:
str -
Throws:
java.text.ParseException

staticListInsert

public abstract void staticListInsert(java.lang.String field)
Each type of constraint will have a specific set of fields associated with it. This is to allow the user to select them via Gui.

Parameters:
field -

stringify

public abstract java.lang.String stringify()
Stringify the object. Object serialization may be a better approach. Oh well. Encode the class's type and state in a readable string.

Returns:

toString

public java.lang.String toString()
toString returns the stringified object.

Overrides:
toString in class java.lang.Object
Returns:

formatString

protected java.lang.String formatString(java.lang.String input)
                                 throws java.lang.NoSuchFieldException,
                                        java.text.ParseException,
                                        java.lang.NumberFormatException
This is to abstract the need to get input in the correct format for a given field object, based on the class of the field indicated by getField() and getPersistence(). If either are null, or if the string getField() doesn't indicate a path to a real field, starting at the class of object getPersistence(), then a NoSuchFieldException is thrown. Otherwise, the method returns if 'input' is a recognizable format for the class indicated by getField(). If the input string is a bad format, then a ParseException is thrown. If the class indicated by getField() is not one of int, long, float, double, boolean, Integer, Long, Float, Double, Boolean, String, Date, or DateWithoutTime, then a just the input is returned. The returned is as a SQL-argument style version of the input.

Parameters:
input -
Returns:
Throws:
java.lang.NoSuchFieldException
java.text.ParseException
java.lang.NumberFormatException

stringToObject

protected java.lang.Object stringToObject(java.lang.String str,
                                          java.lang.Class classOfStr)
Parameters:
str - java.lang.String
classOfStr - java.lang.Class
Returns:
java.lang.Object