|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface Criteria
Criteria is a simplified API for retrieving JCR Nodes by composing Criterion objects. This is a
very convenient approach for functionality like "search" screens where there is a variable number of conditions to be
placed upon the result set.
The JCRCriteriaFactory is a factory for Criteria. Criterion instances are usually obtained
via the factory methods on Restrictions. eg.
Calendar begin = Calendar.getInstance();
begin.set(1999, Calendar.JANUARY, 1);
Calendar end = Calendar.getInstance();
end.set(2001, Calendar.DECEMBER, 31);
Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE)
.setBasePath("/pets")
.add(Restrictions.contains("@title", "Lucky"))
.add(Restrictions.eq("@petType", "dog"))
.add(Restrictions.betweenDates("@birthDate", begin, end))
.addOrder(Order.desc("@title"));
will be translated into the following xpath statement
//pets//*[((jcr:contains(@title, 'Lucky')) and (@petType='dog')
and (@birthDate >=xs:dateTime('1999-01-01T00:00:00.000+00:00')
and @birthDate <=xs:dateTime('2001-12-31T23:59:59.999+00:00')))]
order by @title descending
Furthermore, you may want to have only a subset of the whole result set returned, much like in a MySQL limit clause.
In this case, you will use the setFirstResult and setMaxResults methods. Here is an
example.
Criteria criteria = JCRCriteriaFactory
.createCriteria()
.setWorkspace(ContentRepository.WEBSITE)
.setBasePath("/pets")
.add(Restrictions.betweenDates("@birthDate", begin, end))
.addOrder(Order.asc("@birthDate"))
.setFirstResult(5)
.setMaxResults(5);
Notice the setFirstResult(int) and setMaxResults(int) methods. Now executing the query will
return a subset of no more than five results, starting from the 6th item (counting starts from 0). If you dont
specify these two calls, the entire result set will be returned. If you only call setMaxResults(int),
the result set will be the subset of elements [0, maxResults] (firstResultValue is 0 by default).setPaging method:
Criteria criteria = JCRCriteriaFactory.createCriteria().setWorkspace(ContentRepository.WEBSITE)
.setBasePath("/pets")
.add(Restrictions.betweenDates("@birthDate", begin, end))
.addOrder(Order.asc("@birthDate"))
.setPaging(5, 2);
JCRCriteriaFactory. They are NOT
thread-safe, therefore client code wishing to use one of them as a shared global variable MUST
coordinate access to it. These objects are actually meant to be instantiated and used within a method scope (e.g. a
service method), where no concurrent issues arise.
JCRCriteriaFactory.createCriteria(),
Order| Method Summary | |
|---|---|
Criteria |
add(Criterion criterion)
Add a restriction to constrain the results to be retrieved. |
Criteria |
addOrder(Order order)
Add an ordering to the result set. |
Criteria |
setBasePath(String path)
Sets the base path of the query, i.e. |
Criteria |
setFirstResult(int firstResult)
Set the first result to be retrieved. |
Criteria |
setMaxResults(int maxResults)
Set a limit upon the number of objects to be retrieved. |
Criteria |
setPaging(int itemsPerPage,
int pageNumberStartingFromOne)
|
Criteria |
setSpellCheckString(String spellCheckString)
Sets the original input string for spell checking. |
Criteria |
setWorkspace(String workspace)
Sets the name of the workspace where the search will take place |
String |
toXpathExpression()
Returns the generated xpath expression |
| Methods inherited from interface net.sourceforge.openutils.mgnlcriteria.jcr.query.ExecutableQuery |
|---|
execute |
| Method Detail |
|---|
Criteria add(Criterion criterion)
restriction to constrain the results to be retrieved.
criterion - The criterion object representing the restriction to be applied.
Criteria addOrder(Order order)
ordering to the result set. Only one Order criterion per query can be
applied. Any Order added after the first one will be ignored.
order - The order object representing an ordering to be applied to the results.
Criteria setMaxResults(int maxResults)
maxResults - the maximum number of results
Criteria setFirstResult(int firstResult)
firstResult - the first result to retrieve, numbered from 0
Criteria setBasePath(String path)
path - the handle of a node, or a xpath query prefix in the form //handle/of/a/node//*
Criteria setPaging(int itemsPerPage,
int pageNumberStartingFromOne)
itemsPerPage - maximum number of results per page (i.e. page size)pageNumberStartingFromOne - page number to retrieve (1, 2, 3, ...)
Criteria setSpellCheckString(String spellCheckString)
spellCheckString - the actual input string for spell checking
Criteria setWorkspace(String workspace)
workspace - the name of a workspace
String toXpathExpression()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||