com.arsdigita.persistence
Class DataQueryDataCollectionAdapter

java.lang.Object
  extended bycom.arsdigita.persistence.DataQueryDecorator
      extended bycom.arsdigita.persistence.DataQueryDataCollectionAdapter
All Implemented Interfaces:
DataCollection, DataQuery

public class DataQueryDataCollectionAdapter
extends DataQueryDecorator
implements DataCollection

Adapter to make a data query look like a data collection. If your data query looks something like the following in PDL:

 
 query ItemsInFolder {
   ContentItem item;
   ContentType type;
   do {
     select i.item_id, i.name, i.version,
            t.type_id, t.label
     from cms_items i, content_types t
     where i.type_id = t.type_id
   } map {
     item.id = i.item_id;
     item.name = i.name;
     ...
     type.id = t.type_id;
     type.label = t.label;
   }
 
and dq is a DataQuery constructed from that PDL description, then you can get a dat collection of items through

    new DataQueryDataCollectionAdapter(dq, "item");
 
and a new data collection of content types through

    new DataQueryDataCollectionAdapter(dq, "type");
 
Warning: Note that all manipulations of the data collection also change the underlying data query. The constructed object is not a cursor, it just wraps the data query that was passed in.

Version:
$Id: //core-platform/dev/src/com/arsdigita/persistence/DataQueryDataCollectionAdapter.java#9 $
Author:
David Lutterkort

Field Summary
 
Fields inherited from interface com.arsdigita.persistence.DataCollection
versionId
 
Constructor Summary
DataQueryDataCollectionAdapter(DataQuery dq, String dataObjectProperty)
          Create a data collection that uses the objects with name dataObjectProperty from the data query as its data objects.
DataQueryDataCollectionAdapter(String queryName, String dataObjectProperty)
          Create a data collection that uses the objects with name dataObjectProperty from the data query as its data objects.
 
Method Summary
 Filter addEqualsFilter(String attribute, Object value)
          This creates the appropriate SQL for the given attribute and passed in value.
 Filter addFilter(Filter filter)
          This adds the passed in filter to this query and ANDs it with an existing filters.
 Filter addFilter(String conditions)
          Adds the conditions to the filter that will be used on this query.
 Filter addInSubqueryFilter(String propertyName, String subqueryName)
          Add an 'in' subquery to a query.
 Filter addNotEqualsFilter(String attribute, Object value)
          This creates the appropriate SQL for the given attribute and passed in value.
 void addOrder(String order)
          Set the order in which the result of this query will be returned.
 boolean contains(DataObject dobj)
          Tests whether the current collection contains an object.
 boolean contains(OID oid)
          Tests whether the current collection contains an object.
 Object get(String propertyName)
          Returns the value of the propertyName property associated with the current position in the sequence.
 DataObject getDataObject()
          Returns a data object for the current position in the collection.
 Object getLinkAttribute(String propertyName)
          Retrieve an attribute of the underlying query.
 ObjectType getObjectType()
          Returns the object type of the data collection.
 Filter setFilter(String conditions)
          Sets a filter for this query.
 void setOrder(String order)
          Set the order in which the result of this query will be returned.
 String toString()
           
 
Methods inherited from class com.arsdigita.persistence.DataQueryDecorator
addInSubqueryFilter, addNotInSubqueryFilter, addOrderWithNull, addPath, alias, clearFilter, clearOrder, close, first, getDataQuery, getFilterFactory, getParameter, getPosition, getPropertyValues, getType, hasProperty, isAfterLast, isBeforeFirst, isEmpty, isFirst, isLast, last, next, previous, removeFilter, reset, rewind, setParameter, setRange, setRange, setReturnsLowerBound, setReturnsUpperBound, size
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.arsdigita.persistence.DataCollection
getParameter, setParameter
 
Methods inherited from interface com.arsdigita.persistence.DataQuery
addInSubqueryFilter, addNotInSubqueryFilter, addOrderWithNull, addPath, alias, clearFilter, clearOrder, close, first, getFilterFactory, getPosition, getPropertyValues, getType, hasProperty, isAfterLast, isBeforeFirst, isEmpty, isFirst, isLast, last, next, previous, removeFilter, reset, rewind, setRange, setRange, setReturnsLowerBound, setReturnsUpperBound, size
 

Constructor Detail

DataQueryDataCollectionAdapter

public DataQueryDataCollectionAdapter(DataQuery dq,
                                      String dataObjectProperty)
Create a data collection that uses the objects with name dataObjectProperty from the data query as its data objects.

Parameters:
dq - the data query from which data objects are taken
dataObjectProperty - the name of the data objects in the query

DataQueryDataCollectionAdapter

public DataQueryDataCollectionAdapter(String queryName,
                                      String dataObjectProperty)
Create a data collection that uses the objects with name dataObjectProperty from the data query as its data objects. The data query with name queryName is retrieved and used as the source for data objects.

Parameters:
queryName - the name of the data query from which data objects are taken
dataObjectProperty - the name of the data objects in the query
Method Detail

getDataObject

public DataObject getDataObject()
Description copied from interface: DataCollection
Returns a data object for the current position in the collection.

Specified by:
getDataObject in interface DataCollection
Returns:
A DataObject.

getObjectType

public ObjectType getObjectType()
Description copied from interface: DataCollection
Returns the object type of the data collection.

Specified by:
getObjectType in interface DataCollection
Returns:
The object type of the data collection.

getLinkAttribute

public Object getLinkAttribute(String propertyName)
Retrieve an attribute of the underlying query. Typically, this method will be used to retrieve link attributes.

Parameters:
propertyName - the name of the link attribute to retrieve
Returns:
the valur of the link attribute (may be null)

get

public Object get(String propertyName)
Description copied from interface: DataQuery
Returns the value of the propertyName property associated with the current position in the sequence.

Specified by:
get in interface DataQuery
Overrides:
get in class DataQueryDecorator

setFilter

public Filter setFilter(String conditions)
Description copied from interface: DataQuery
Sets a filter for this query. The filter consists of a set of SQL condition specified in terms of the properties of this query. The conditions may be combined with "and" and "or". Bind variables may be used in the body of the filter. The values are set by using the set method on the Filter object that is returned.
 Filter f = query.setFilter("id < :maxId and id > :minId");
 f.set("maxId", 10);
 f.set("minId", 1);
 

Specified by:
setFilter in interface DataQuery
Overrides:
setFilter in class DataQueryDecorator

addFilter

public Filter addFilter(String conditions)
Description copied from interface: DataQuery
Adds the conditions to the filter that will be used on this query. If a filter already exists, this alters the filter object and returns the altered object. If one does not already exist, it creates a new filter. When adding filters the user should be aware that their query is wrapped and the filter is appended to the wrapped query. That is, your query will look like the following:

        select * from (<data query here>) results
        where <conditions here>
        

When adding filters, the user should not use the same parameter name in multiple filters. That is, the following will not work

 
 Filter filter = query.addFilter("priority > :bound");
 filter.set("bound", new Integer(3));
 filter = query.addFilter("priority < :bound");
 filter.set("bound", new Integer(8));
 
 
The above actually evaluates to "priority < 8 and priority > 8" which is clearly not what the developer wants.

The following will work.

 
 Filter filter = query.addFilter("priority > :lowerBound");
 filter.set("lowerBound", new Integer(3));
 filter = query.addFilter("priority < :upperBound");
 filter.set("upperBound", new Integer(8));
 
 
It is actually the same as
 
 Filter filter = query.addFilter("priority > :lowerBound
                                  and priority < :uperBound");
 filter.set("upperBound", new Integer(8));
 filter.set("lowerBound", new Integer(3));
 
 

Specified by:
addFilter in interface DataQuery
Overrides:
addFilter in class DataQueryDecorator

addFilter

public Filter addFilter(Filter filter)
Description copied from interface: DataQuery
This adds the passed in filter to this query and ANDs it with an existing filters. It returns the filter for this query.

Specified by:
addFilter in interface DataQuery
Overrides:
addFilter in class DataQueryDecorator

addInSubqueryFilter

public Filter addInSubqueryFilter(String propertyName,
                                  String subqueryName)
Description copied from interface: DataQuery
Add an 'in' subquery to a query.

Specified by:
addInSubqueryFilter in interface DataQuery
Overrides:
addInSubqueryFilter in class DataQueryDecorator

addEqualsFilter

public Filter addEqualsFilter(String attribute,
                              Object value)
Description copied from interface: DataQuery
This creates the appropriate SQL for the given attribute and passed in value. It creates a filter for "attribute = 'value.toString()'" unless the value is an integer (in which case it creates "attribute = value.toString()") or the developer is using oracle and the value is null. In this case, it would create "attribute is null".

This is simply a convenience method for addFilter(getFilterFactory().equals(attribute, value));

Specified by:
addEqualsFilter in interface DataQuery
Overrides:
addEqualsFilter in class DataQueryDecorator

addNotEqualsFilter

public Filter addNotEqualsFilter(String attribute,
                                 Object value)
Description copied from interface: DataQuery
This creates the appropriate SQL for the given attribute and passed in value. It creates a filter for "attribute = 'value.toString()'" unless the value is an integer (in which case it creates "attribute != value.toString()") or the developer is using oracle and the value is null. In this case, it would create "attribute is not null".

This is simply a convenience method for addFilter(getFilterFactory().notEquals(attribute, value));

Specified by:
addNotEqualsFilter in interface DataQuery
Overrides:
addNotEqualsFilter in class DataQueryDecorator

setOrder

public void setOrder(String order)
              throws PersistenceException
Description copied from interface: DataQuery
Set the order in which the result of this query will be returned. The string passed is a standard SQL order by clause specified in terms of the properties. For example:
 query.setOrder("creationDate desc, id");
 

Specified by:
setOrder in interface DataQuery
Overrides:
setOrder in class DataQueryDecorator
Throws:
PersistenceException

addOrder

public void addOrder(String order)
              throws PersistenceException
Description copied from interface: DataQuery
Set the order in which the result of this query will be returned. The string passed is a standard SQL order by clause specified in terms of the properties. For example:
 query.addOrder("creationDate desc, id");
 

Specified by:
addOrder in interface DataQuery
Overrides:
addOrder in class DataQueryDecorator
Throws:
PersistenceException

toString

public String toString()
Overrides:
toString in class DataQueryDecorator

contains

public boolean contains(OID oid)
Description copied from interface: DataCollection
Tests whether the current collection contains an object.

Specified by:
contains in interface DataCollection
Parameters:
oid - The oid of the object.
Returns:
True if the collection contains the object, false otherwise.

contains

public boolean contains(DataObject dobj)
Description copied from interface: DataCollection
Tests whether the current collection contains an object.

Specified by:
contains in interface DataCollection
Parameters:
dobj - The dataobject.
Returns:
True if the collection contains the object, false otherwise.


Copyright (c) 2004 Red Hat, Inc. Corporation. All Rights Reserved. Generated at July 20 2004:2337 UTC