Use LimitFilter, SortParameter, CPAssembler, CPFactory
The following sample queries the Content Delivery database for Components in a given Publication by using the PublicationCriteria and ItemTypeCriteria.
It also uses a ResultFilter in the form of a LimitFilter and a SortParameter to only retrieve a maximum set of Components in the given sort order (descending according to their last-publish-date).
For each of the identified Components (if any), we retrieve the Dynamic Component Presentation using the CT with the highest priority. Finally, we feed these DCPs into the ComponentPresentationAssembler in order to retrieve their actual content.
<%
The following sample queries the Content Delivery database for Components in a given Publication by using the PublicationCriteria and ItemTypeCriteria.
It also uses a ResultFilter in the form of a LimitFilter and a SortParameter to only retrieve a maximum set of Components in the given sort order (descending according to their last-publish-date).
For each of the identified Components (if any), we retrieve the Dynamic Component Presentation using the CT with the highest priority. Finally, we feed these DCPs into the ComponentPresentationAssembler in order to retrieve their actual content.
<%
JSPPage jspPage = new JSPPage(pageContext, "tcm:83-3246-64");
cpAssembler = new ComponentPresentationAssembler(jspPage);
Query query = new Query();
PublicationCriteria publicationCriteria = new
PublicationCriteria(83);
ItemTypeCriteria itemTypeCriteria = new
ItemTypeCriteria(ItemTypes.COMPONENT);
AndCriteria andCriteria = new AndCriteria(publicationCriteria, itemTypeCriteria);
query.setCriteria(andCriteria);
ResultFilter resultFilter = new LimitFilter(2);
query.setResultFilter(resultFilter);
SortParameter sortParameter = new SortParameter(
new ItemLastPublishColumn(), SortDirection.DESCENDING);
query.addSorting(sortParameter);
String[] items = query.executeQuery();
out.println("<p><b>Publication=83 AND ItemType=16 Query:
</b>" + list(items) + "</p>");
out.println("<p><b>DCPs:</b>");
writeCP(out, items);
out.println("</p>");
%>
<%!ComponentPresentationAssembler cpAssembler = null;
public String list(String[] items) {
StringBuilder sb = new StringBuilder();
for (String item : items) {
sb.append(item + " ");
}
return sb.toString();
}
public void writeCP(JspWriter out, String[] items) throws IOException {
try {
if (items == null || items.length == 0) {
return;
}
TCMURI itemUri = new TCMURI(items[0]);
ComponentPresentationFactory cpFactory = new ComponentPresentationFactory(
itemUri.getPublicationId());
for (String compUri : items) {
ComponentPresentation cp = cpFactory
.getComponentPresentationWithHighestPriority(compUri);
if (cp == null) {
out.println("Found
null CP for compUri=" + compUri + "<hr/>");
} else {
out.println(cpAssembler.getContent(cp.getComponentId(),
cp.getComponentTemplateId()) + "<hr/>");
}
}
} catch (ParseException pe) {
out.println("ParseException occurred: " + pe);
}
}%>
Comments