use of fi.otavanopisto.pyramus.dao.resources.ResourceDAO in project pyramus by otavanopisto.
the class SearchResourcesAutoCompleteBinaryRequestController method process.
/**
* Processes a binary request.
* The request should contain the following parameters:
* <dl>
* <dt><code>query</code></dt>
* <dd>Already typed characters.</dd>
* </dl>
*
* @param binaryRequestContext The context of the binary request.
*/
public void process(BinaryRequestContext binaryRequestContext) {
ResourceDAO resourceDAO = DAOFactory.getInstance().getResourceDAO();
ResourceCategoryDAO resourceCategoryDAO = DAOFactory.getInstance().getResourceCategoryDAO();
String query = binaryRequestContext.getString("query");
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append("<ul>");
List<ResourceCategory> resourceCategories = resourceCategoryDAO.listUnarchived();
Collections.sort(resourceCategories, new StringAttributeComparator("getName"));
for (ResourceCategory resourceCategory : resourceCategories) {
SearchResult<Resource> searchResult = resourceDAO.searchResources(5, 0, query + '*', null, null, resourceCategory, true);
if (searchResult.getTotalHitCount() > 0) {
addResourceCategory(resultBuilder, resourceCategory);
for (Resource resource : searchResult.getResults()) {
addResource(resultBuilder, resource);
}
}
}
resultBuilder.append("</ul>");
try {
binaryRequestContext.setResponseContent(resultBuilder.toString().getBytes("UTF-8"), "text/html;charset=UTF-8");
} catch (UnsupportedEncodingException e) {
throw new SmvcRuntimeException(e);
}
}
Aggregations