|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.edustructures.sifworks.Query
public class Query
Encapsulates a SIF Query.
An instance of this class is passed to the Zone.query and
Topic.query methods when issuing a SIF Request. A Query object
defines the following parameters to the request:
To construct a simple Query to query for all objects with no conditions or field restrictions, call the constructor that accepts an ElementDef constant from the SIFDTD class:
Query myQuery = new Query( SIFDTD.STUDENTPERSONAL );
More complex queries can be constructed by specifying conditions and field
restrictions.Conditions
A Query may optionally specify one or more conditions to restrict the number of objects returned by the responder. (Refer to the SIF Specification for a detailed description of how query conditions may be constructed.) When no conditions are specified, the responder interprets the query to mean "all objects". Note SIF 1.0r2 and earlier limit queries such that only root-level attributes may be included in query conditions, and only the equals ("EQ") comparison operator may be used. SIF 1.1 and later allow agents to query for elements within an object, but responders may return an error if they do not support that functionality.
Query conditions are encapsulated by the ADK's ConditionGroup class, which is
used to build SIF_ConditionGroup, SIF_Conditions, and SIF_Condition elements
when the class framework sends a SIF_Request message to a zone. Every Query
with conditions has a root ConditionGroup with one or more child ConditionGroups.
Unless you construct these groups manually, the Query class will automatically
establish a root ConditionGroup and a single child when the addCondition
method is called. Use the addCondition method to add conditions
to a Query. Note the form of Query constructor you call determines how the
addCondition method works. If you call the default constructor,
the ADK automatically establishes a root SIF_ConditionGroup with a Type
attribute of "None", and a single SIF_Conditions child with a Type attribute
of "And". ("None" will be used if the query has only one condition.)
SIF_Condition elements are then added to this element whenever the
addCondition method is called.
For example,
// Query for a single student by RefId
Query query = new Query( SIFDTD.STUDENTPERSONAL );
query.addCondition(
SIFDTD.STUDENTPERSONAL_REFID, Condition.EQ,
"4A37969803F0D00322AF0EB969038483" );
If you want to specify the "Or" comparision operator instead of the default
of "And", call the constructor that accepts a constant from the Condition
class.For example,
// Query for student where the RefId is A, B, or C
Query query = new Query( SIFDTD.STUDENTPERSONAL, Condition.OR );
query.addCondition(
SIFDTD.STUDENTPERSONAL_REFID, Condition.EQ,
"4A37969803F0D00322AF0EB969038483" );
query.addCondition(
SIFDTD.STUDENTPERSONAL_REFID, Condition.EQ,
"5A37969803F0D00322AF0EB969038484" );
query.addCondition(
SIFDTD.STUDENTPERSONAL_REFID, Condition.EQ,
"6A37969803F0D00322AF0EB969038485" );
The above examples show how to add simple conditions to a Query. To construct
complex queries with nested groups of conditions, create your own root
SIF_ConditionGroup object by calling the form of constructor that
accepts a ConditionGroup instance. You can specify nested ConditionGroup
children of this root object.
For example,
// Query for student where the Last Name is Jones and the First Name is
// Bob, and the graduation year is 2004, 2005, or 2006
ConditionGroup root = new ConditionGroup( Condition.AND );
ConditionGroup grp1 = new ConditionGroup( Condition.AND );
ConditionGroup grp2 = new ConditionGroup( Condition.OR );
// For nested elements, you cannot reference a SIFDTD constant. Instead, use
// the lookupElementDefBySQL function to lookup an ElementDef constant
// given a SIF Query Pattern (SQP)
ElementDef lname = ADK.DTD().lookupElementDefBySQP(
SIFDTD.STUDENTPERSONAL, "Name/LastName" );
ElementDef fname = ADK.DTD().lookupElementDefBySQP(
SIFDTD.STUDENTPERSONAL, "Name/FirstName" );
grp1.addCondition( lname, Condition.EQ, "Jones" );
grp1.addCondition( fname, Condition.EQ, "Bob" );
grp2.addCondition( SIFDTD.STUDENTPERSONAL_GRADYEAR, Condition.EQ, "2004" );
grp2.addCondition( SIFDTD.STUDENTPERSONAL_GRADYEAR, Condition.EQ, "2005" );
grp2.addCondition( SIFDTD.STUDENTPERSONAL_GRADYEAR, Condition.EQ, "2006" );
// Add condition groups to the root group
root.addGroup( grp1 );
root.addGroup( grp2 );
// Query for student with the conditions prepared above by passing the
// root ConditionGroup to the constructor
Query query = new Query( SIFDTD.STUDENTPERSONAL, root );
Field Restrictions
If only a subset of elements and attributes are requested, use the
setFieldRestrictions method to indicate which elements and
attributes should be returned to your agent by the responder. For example,
to request the <StudentPersonal> object with RefId "4A37969803F0D00322AF0EB969038483"
but to only include the RefId attribute and Name
and PhoneNumber elements in the response,
// Query for a single student by RefId
Query query = new Query( SIFDTD.STUDENTPERSONAL );
query.addCondition(
SIFDTD.STUDENTPERSONAL_REFID, Condition.EQ,
"4A37969803F0D00322AF0EB969038483" );
query.setFieldRestrictions(
new ElementDef[] {
SIFDTD.STUDENTPERSONAL_REFID,
SIFDTD.STUDENTPERSONAL_NAME,
SIFDTD.STUDENTPERSONAL_PHONENUMBER
}
);
| Constructor Summary | |
|---|---|
Query(ElementDef objectType)
Constructs a Query object with no initial conditions or field restrictions. |
|
Query(ElementDef objectType,
ConditionGroup conditions)
Constructs a Query object with a ConditionGroup. |
|
Query(ElementDef objectType,
GroupOperators logicalOp)
Constructs a Query object with one ConditionGroup where all conditions in the group are evaluated using the supplied boolean operator (either Condition.AND or Condition.OR). |
|
Query(SIF_Query query)
Constructs a Query object from a SIF_QueryObject. |
|
| Method Summary | |
|---|---|
void |
addCondition(Condition condition)
Add a condition to this query. |
void |
addCondition(ElementDef field,
ComparisonOperators ops,
java.lang.String value)
Add a condition to this query. |
void |
addCondition(java.lang.String xPath,
ComparisonOperators ops,
java.lang.String value)
Add a condition to this query using a deeply nested path. |
void |
addCondition(java.lang.String field,
java.lang.String ops,
java.lang.String value)
Add a condition to this query. |
void |
addFieldRestriction(ElementDef field)
Restricts the query to a specific field (i.e. |
boolean |
evaluate(SIFDataObject obj)
Evaluate the given the SIFDataObject against the conditions provided in the Query. |
boolean |
evaluate(SIFDataObject obj,
java.util.Comparator comparer)
Evaluate the given the SIFDataObject against the conditions provided in the Query. |
ConditionGroup[] |
getConditions()
Gets the conditions placed on this query. |
SIFVersion |
getEffectiveVersion()
From the list of SIFVersions associated with this Query, returns the latest SIFVersion supported by the current ADK instance. |
ElementDef[] |
getFieldRestrictions()
Gets the fields to include in the result of the query. |
java.lang.String |
getObjectTag()
Gets the tag name of the object type being queried |
ElementDef |
getObjectType()
Gets the object type being queried |
ConditionGroup |
getRootConditionGroup()
Gets the root ConditionGroup. |
SIFContext |
getSIFContext()
Gets the SIF Context that this Query applies to |
SIFVersion[] |
getSIFVersions()
Sets the value of the SIF_Request/SIF_Version element. |
java.io.Serializable |
getUserData()
Gets the custom state object associated with this query |
Condition |
hasCondition(ElementDef elementOrAttr)
Tests if this Query has a specific element or attribute condition |
Condition |
hasCondition(java.lang.String xPath)
Tests if this Query has a condition referencing a specific xPath |
boolean |
hasConditions()
Determines if this Query has any conditions |
boolean |
hasFieldRestrictions()
Determines if this Query has any field restrictions |
void |
setConditionGroup(ConditionGroup root)
Sets the root ConditionGroup. |
void |
setFieldRestrictions(ElementDef[] fields)
Restricts the query to the specified elements and attributes |
void |
setSIFContext(SIFContext context)
Sets the SIFContext that this query should apply to |
void |
setSIFVersions(SIFVersion... versions)
Sets the value of the SIF_Request/SIF_Version element. |
void |
setUserData(java.io.Serializable userData)
Allows a custom state object to be associated with a query |
SIF_Query |
toSIF_Query()
Returns the SIF_Query representation of this Query in the format required by SIF |
SIF_Query |
toSIF_Query(SIFVersion version)
Returns the SIF_Query representation of this Query in the format required by SIF for the specified version |
java.lang.String |
toXML()
Returns the XML representation of this Query in the format required by SIF |
java.lang.String |
toXML(SIFVersion version)
Returns the XML representation of this Query in the format required by SIF for the specified version |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Query(ElementDef objectType)
objectType - An ElementDef describing the object type to query (e.g.
ADK.DTD.STUDENTPERSONAL)
public Query(ElementDef objectType,
GroupOperators logicalOp)
Condition.AND or Condition.OR). All Conditions
subsequently added to this Query will be placed into the ConditionGroup
created by the constructor.This constructor is provided as a convenience so that callers do not have to explicitly create a ConditionGroup for simple queries.
objectType - An ElementDef describing the object type to query (e.g.
ADK.DTD.STUDENTPERSONAL)logicalOp - The logical operator that defines how to compare this group
with other condition groups that comprise the query (e.g. GroupOperators.OR)
public Query(ElementDef objectType,
ConditionGroup conditions)
objectType - An ElementDef describing the object type to query (e.g.
ADK.DTD.STUDENTPERSONAL)conditions - A ConditionGroup comprised of one or more query Conditions
public Query(SIF_Query query)
throws ADKUnknownOperatorException,
ADKSchemaException
This constructor is not typically called by agents but is used internally
by the class framework. The other constructors can be used to safely
create Query instances to request a specific SIF Data Object. Use the
addCondition and setFieldRestrictions methods
to further define the conditions and SIF elements specified by the query.
query - A SIF_Query object received in a SIF_Request message
ADKUnknownOperatorException - If one of the operators in the SIF_Query is
unrecognized by the ADK
ADKSchemaException - If the object or elements defined in the query or
not recognized by the ADK| Method Detail |
|---|
public java.lang.String toXML()
public java.lang.String toXML(SIFVersion version)
version - The SIF Version to render the Query in. The ADK will attempt to render
the query path using the proper element or attribute names for the version of SIF
public SIF_Query toSIF_Query()
public SIF_Query toSIF_Query(SIFVersion version)
version - The SIF Version to render the Query in. The ADK will attempt to render
the query path using the proper element or attribute names for the version of SIF
public ElementDef getObjectType()
public java.lang.String getObjectTag()
public java.io.Serializable getUserData()
setUserData(Serializable)public void setUserData(java.io.Serializable userData)
userData - A custom state object that implements the Serializable interface
public void addCondition(ElementDef field,
ComparisonOperators ops,
java.lang.String value)
This method of adding conditions is convenient for adding conditions involving
root attributes or elements to a query. If you need to add conditions on deeply
nested elements, use addCondition(String, ComparisonOperators, String)
field - A constant from the SIFDTD class that identifies an element
or attribute of the data object (e.g. SIFDTD.STUDENTPERSONAL_REFID).ops - The comparison operator. Comparison operator constants are
defined by the ComparisionOperators enumvalue - The data that is used to compare to the element or attribute
java.lang.IllegalArgumentException - if the ElementDef does not represent an immediate
child of the object being queried.public void addCondition(Condition condition)
condition - The condition to add. This condition is added to the root
condition group.getRootConditionGroup(),
getConditions()
public void addCondition(java.lang.String xPath,
ComparisonOperators ops,
java.lang.String value)
To ensure your code works with all versions of SIF, you should use
addCondition(ElementDef, ComparisonOperators, String) whenever possible.
xPath - The Simple XPath to use for this query condition. E.g.
SIF_ExendedElements/SIF_ExtendedElement[@Name='eyecolor']ops - The comparison operator. Comparison operator value from the
ComparisonOperators enumvalue - The data that is used to compare to the element or attribute
public void addCondition(java.lang.String field,
java.lang.String ops,
java.lang.String value)
addCondition
method is intended to be called internally by the ADK when parsing an
incoming SIF_Query element. To ensure your code works with all versions
of SIF, you should use the other form of this method that accepts an
ElementDef constant for the field parameter whenever possible.
field - Identifies an element or attribute of the data object in
SIF Query Pattern form as described by the SIF 1.0r2 Specification
(e.g. "@RefId"). With SIF 1.0r2 and earlier, only root-level
attributes may be specified in a query. Note this string is specific
to the version of SIF associated with the Query as element and
attribute names may vary from one version of SIF to the next. The
version defaults to the version of SIF in effect for the agent or the
version of SIF associated with the SIF_Query object
passed to the constructor.ops - A value from the ComparisonOperators enumvalue - The data that is used to compare to the element or attributepublic void addFieldRestriction(ElementDef field)
field - A ElementDef object defined by the static
constants of the SIFDTD class. For example, to restrict
a query for the StudentPersonal topic to include only the StatePr
element of the student address, pass SIFDTD.ADDRESS_STATEPR.
This would cause the query results to include only
StudentPersonal/Address/StatePr elements.public void setFieldRestrictions(ElementDef[] fields)
fields - An array of ElementDef objects identifying one or more
elements and/or attributespublic ElementDef[] getFieldRestrictions()
public ConditionGroup[] getConditions()
public ConditionGroup getRootConditionGroup()
public boolean hasConditions()
getConditions(),
addCondition(ElementDef, ComparisonOperators, String)public boolean hasFieldRestrictions()
getFieldRestrictions(),
addFieldRestriction(com.edustructures.sifworks.ElementDef),
setFieldRestrictions(com.edustructures.sifworks.ElementDef[])public Condition hasCondition(ElementDef elementOrAttr)
elementOrAttr - The ElementDef constant from the SIFDTD class that
identifies the specific attribute or element to search for
public Condition hasCondition(java.lang.String xPath)
xPath - The ElementDef constant from the SIFDTD class that
identifies the specific attribute or element to search for
public void setSIFVersions(SIFVersion... versions)
versions - The version(s) of SIF the responding agent should use when
returning SIF_Response messages for this querypublic SIFVersion[] getSIFVersions()
public SIFVersion getEffectiveVersion()
ADK.getLatestSupportedVersion(SIFVersion[])public void setConditionGroup(ConditionGroup root)
By default a Query is constructed with a ConditionGroup to which
individual conditions will be added by the addCondition
methods. You can call this method to prepare a ConditionGroup ahead of
time and replace the default with your own.
Note calling this method after addCondition will replace
any conditions previously added to the Query with the conditions in the
supplied ConditionGroup.
root - The root ConditionGroup to use for this query
public boolean evaluate(SIFDataObject obj)
throws ADKSchemaException
obj - The SIFDataObject to evalaute against this query
ADKSchemaException - If the condition contains references to invalid elements
public boolean evaluate(SIFDataObject obj,
java.util.Comparator comparer)
throws ADKSchemaException
obj - The SIFDataObject to evalaute against this querycomparer - The comparer used to do comparisons
ADKSchemaException - If the condition contains references to invalid elementspublic void setSIFContext(SIFContext context)
context - The SIF Context that this query applies topublic SIFContext getSIFContext()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||