com.objectwave.persist.broker
Class ObjectQuerySupport

java.lang.Object
  |
  +--com.objectwave.persist.broker.ObjectQuerySupport
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
FileQuery, ObjectPoolQuery

public abstract class ObjectQuerySupport
extends java.lang.Object
implements java.lang.Cloneable

A generic class that can assist with the query of objects in a way similar to relational databases.

Version:
$Id: ObjectQuerySupport.java,v 2.5 2002/03/09 17:13:47 dave_hoag Exp $
Author:
dhoag

Nested Class Summary
static class ObjectQuerySupport.Test
           
 
Field Summary
protected  ObjectFormatter defaultFormatter
           
 
Constructor Summary
ObjectQuerySupport()
           
 
Method Summary
 boolean checkAttributeValues(boolean wildCard, RDBPersistence subject, RDBPersistence pObj)
          If the subject is retrieved from the database, only check the primary key in the query.
protected  boolean checkConstrainedColumns(Persistence poolElement, RDBPersistence pObj)
           
protected  boolean checkForeignValues(RDBPersistence subject, RDBPersistence listElementAdapter, java.util.ArrayList visited)
           
protected  boolean checkInstanceValues(RDBPersistence subject, RDBPersistence listElementAdapter, java.util.ArrayList visited)
           
 java.lang.Object clone()
          Create an almost copy.
 java.lang.Object find()
          Execute the query.
 java.lang.Object[] getAttributeData(RDBPersistence adapter)
          Get the AttributeData from the persistent adapter
protected  CollectionAdapter getCollectionAdapter(SQLQuery query)
          Get the collection adapter specified in the query.
protected  AttributeTypeColumn getFieldForName(Persistence persistentObject, java.lang.String fieldName)
          Find the Field object represented by the provided field name
protected abstract  java.util.Iterator getListOfObjects(Persistence expectedType)
          Query the object pool for all objects that are of the expected type.
protected  RDBPersistence getReferenceAdapter(Persistence listElement, AttributeTypeColumn col)
          Get the col value from the list element.
protected  Persistence[] initDataElements(AttributeTypeColumn[] subjCols, Persistence subject)
          Use the AttributeTypeColumns to extract the data from the persistent subject.
 void initialize()
           
protected  Persistence initializeSubject(SQLQuery query)
          Set the subject of this ObjectPoolQuery to be the subject found in the query parameter.
protected  boolean isConsideredEqual(boolean wildCardMatch, java.lang.Object poolObject, java.lang.Object queryObject)
          If the query object is null or a default value, then this will return true.
protected  boolean isMatch(boolean wildCard, RDBPersistence subject, Persistence poolElement, java.util.ArrayList visited)
          To prevent problems with cyclical references we keep a list of every object visited.
protected  boolean isMatch(RDBPersistence searchObject, RDBPersistence listElement, java.util.ArrayList visited)
          Create a new ObjectPoolQuery using the subject of searchObject and check to see if it matches listElement.
protected  boolean isPersistentReferenceValid(RDBPersistence subject, RDBPersistence pQueryReference, RDBPersistence pObjFkData, RDBPersistence listElementAdapter, java.util.ArrayList visited)
           
protected  boolean isPrimaryKeyMatch(boolean wildCardMatch, RDBPersistence querySubject, RDBPersistence poolElement)
          Determine if the primary keys from querySubject and the poolElement have the same values.
protected  boolean satisfyingConstraint(Persistence poolElement, java.lang.String columnName, java.lang.Object value, java.lang.Object queryValue)
          Check to see if any constraints have been established for this query.
 void setSqlQuery(SQLQuery q)
          Without this instance being initialized with a query, nothing good can be done with this class.
protected  void sortOnField(java.util.Vector collection, java.lang.String fieldName)
          Modify the vector parameter to be sorted based upon the field.
protected  java.lang.Object sortResult(java.lang.Object originalResult, java.util.Vector orderByList)
          If there the query specifies a sort order, apply that now.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultFormatter

protected ObjectFormatter defaultFormatter
Constructor Detail

ObjectQuerySupport

public ObjectQuerySupport()
Method Detail

setSqlQuery

public void setSqlQuery(SQLQuery q)
Without this instance being initialized with a query, nothing good can be done with this class.

Parameters:
q - The new SqlQuery value

getAttributeData

public java.lang.Object[] getAttributeData(RDBPersistence adapter)
Get the AttributeData from the persistent adapter

Parameters:
adapter -
Returns:
The data from the persistent object.

getCollectionAdapter

protected CollectionAdapter getCollectionAdapter(SQLQuery query)
Get the collection adapter specified in the query. If none is specified default to a VectorCollectionAdapter.

Parameters:
query -
Returns:
The CollectionAdapter value

getListOfObjects

protected abstract java.util.Iterator getListOfObjects(Persistence expectedType)
Query the object pool for all objects that are of the expected type.

Parameters:
expectedType - The type of object specified in the query.
Returns:
The ListOfObjects value

isMatch

protected boolean isMatch(boolean wildCard,
                          RDBPersistence subject,
                          Persistence poolElement,
                          java.util.ArrayList visited)
To prevent problems with cyclical references we keep a list of every object visited.

Parameters:
visited - ArrayList The list of visited objects.
poolElement -
wildCard -
subject -
Returns:
The Match value

isMatch

protected boolean isMatch(RDBPersistence searchObject,
                          RDBPersistence listElement,
                          java.util.ArrayList visited)
Create a new ObjectPoolQuery using the subject of searchObject and check to see if it matches listElement.

Parameters:
searchObject - Foreign key or instance link data that was found in the original query object.
listElement - The fk or instance link data from an object found in the object pool.
visited - ArrayList The list of persistent objects to which we have visited
Returns:
true if query matches the data, otherwise false.

isPrimaryKeyMatch

protected boolean isPrimaryKeyMatch(boolean wildCardMatch,
                                    RDBPersistence querySubject,
                                    RDBPersistence poolElement)
                             throws java.io.IOException
Determine if the primary keys from querySubject and the poolElement have the same values.

Parameters:
wildCardMatch - true if the querySubject specified a primaryKey as a wild card
querySubject -
poolElement -
Returns:
The PrimaryKeyMatch value
Throws:
java.io.IOException

isConsideredEqual

protected boolean isConsideredEqual(boolean wildCardMatch,
                                    java.lang.Object poolObject,
                                    java.lang.Object queryObject)
                             throws java.io.IOException
If the query object is null or a default value, then this will return true.

Parameters:
poolObject - Real data from a persistent object.
queryObject - User specified data found in a query. May contain wild cards.
wildCardMatch -
Returns:
The ConsideredEqual value
Throws:
java.io.IOException

getReferenceAdapter

protected RDBPersistence getReferenceAdapter(Persistence listElement,
                                             AttributeTypeColumn col)
Get the col value from the list element. If it exists, check to see if it is a proxy. If so, realize the proxy. Once the final list element reference value exists, get the adapter and return that result.

Parameters:
col - AttributeTypeColumn The 'field' that should reference a persistent object.
listElement - The persistent object having the refence to another persistent object.
Returns:
null if element does not exist

isPersistentReferenceValid

protected boolean isPersistentReferenceValid(RDBPersistence subject,
                                             RDBPersistence pQueryReference,
                                             RDBPersistence pObjFkData,
                                             RDBPersistence listElementAdapter,
                                             java.util.ArrayList visited)
Parameters:
subject -
visited -
pObjFkData -
listElementAdapter -
pQueryReference -
Returns:
The PersistentReferenceValid value

initialize

public void initialize()

find

public java.lang.Object find()
Execute the query.

Returns:

clone

public java.lang.Object clone()
Create an almost copy. The transient data is not copied.

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

checkAttributeValues

public boolean checkAttributeValues(boolean wildCard,
                                    RDBPersistence subject,
                                    RDBPersistence pObj)
                             throws java.io.IOException
If the subject is retrieved from the database, only check the primary key in the query. Assumes queryAttributeData has been setup via the setSubject method call.

Parameters:
pObj - An element from the object pool
wildCard -
subject -
Returns:
Throws:
java.io.IOException

initializeSubject

protected Persistence initializeSubject(SQLQuery query)
Set the subject of this ObjectPoolQuery to be the subject found in the query parameter.

Parameters:
query - The original query that resulted in this object being created.
Returns:
The query subject

sortResult

protected java.lang.Object sortResult(java.lang.Object originalResult,
                                      java.util.Vector orderByList)
If there the query specifies a sort order, apply that now.

Parameters:
originalResult -
orderByList -
Returns:

checkConstrainedColumns

protected boolean checkConstrainedColumns(Persistence poolElement,
                                          RDBPersistence pObj)
Parameters:
poolElement -
pObj -
Returns:

satisfyingConstraint

protected boolean satisfyingConstraint(Persistence poolElement,
                                       java.lang.String columnName,
                                       java.lang.Object value,
                                       java.lang.Object queryValue)
Check to see if any constraints have been established for this query. If constraints exist, make sure that the value satisfies the constraint parameters.

Parameters:
columnName -
value - The value from the found object in the list of candidates for query matches.
queryValue -
poolElement -
Returns:
true If the contraint is satisfied or does not exist

checkForeignValues

protected boolean checkForeignValues(RDBPersistence subject,
                                     RDBPersistence listElementAdapter,
                                     java.util.ArrayList visited)
Parameters:
visited - ArrayList The list of visited persistent objects.
subject -
listElementAdapter -
Returns:

initDataElements

protected Persistence[] initDataElements(AttributeTypeColumn[] subjCols,
                                         Persistence subject)
Use the AttributeTypeColumns to extract the data from the persistent subject.

Parameters:
subjCols - AttributeTypeColumn [] The attribute type columns that identify the instance relations.
subject - Persistence The persistent object
Returns:

checkInstanceValues

protected boolean checkInstanceValues(RDBPersistence subject,
                                      RDBPersistence listElementAdapter,
                                      java.util.ArrayList visited)
Parameters:
visited - The list objects that have been visited by this query.
subject -
listElementAdapter -
Returns:

sortOnField

protected void sortOnField(java.util.Vector collection,
                           java.lang.String fieldName)
Modify the vector parameter to be sorted based upon the field.

Parameters:
collection - Vector A result from a query.
fieldName - The name of a field on each object in the collection.

getFieldForName

protected AttributeTypeColumn getFieldForName(Persistence persistentObject,
                                              java.lang.String fieldName)
                                       throws java.lang.NoSuchFieldException
Find the Field object represented by the provided field name

Parameters:
persistentObject - The object that should contain the provided field
fieldName - The name of a persistent field on the provided object
java.lang.NoSuchFieldException