Search in sources :

Example 11 with ResourceDAO

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);
    }
}
Also used : ResourceCategoryDAO(fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO) StringAttributeComparator(fi.otavanopisto.pyramus.util.StringAttributeComparator) Resource(fi.otavanopisto.pyramus.domainmodel.resources.Resource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SmvcRuntimeException(fi.internetix.smvc.SmvcRuntimeException) ResourceDAO(fi.otavanopisto.pyramus.dao.resources.ResourceDAO) ResourceCategory(fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory)

Aggregations

ResourceDAO (fi.otavanopisto.pyramus.dao.resources.ResourceDAO)11 ResourceCategoryDAO (fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO)8 Tag (fi.otavanopisto.pyramus.domainmodel.base.Tag)8 ResourceCategory (fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory)8 Resource (fi.otavanopisto.pyramus.domainmodel.resources.Resource)7 TagDAO (fi.otavanopisto.pyramus.dao.base.TagDAO)6 HashSet (java.util.HashSet)6 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)5 HashMap (java.util.HashMap)3 Locale (java.util.Locale)3 Map (java.util.Map)3 CourseEducationSubtypeDAO (fi.otavanopisto.pyramus.dao.base.CourseEducationSubtypeDAO)2 CourseEducationTypeDAO (fi.otavanopisto.pyramus.dao.base.CourseEducationTypeDAO)2 CurriculumDAO (fi.otavanopisto.pyramus.dao.base.CurriculumDAO)2 DefaultsDAO (fi.otavanopisto.pyramus.dao.base.DefaultsDAO)2 EducationSubtypeDAO (fi.otavanopisto.pyramus.dao.base.EducationSubtypeDAO)2 EducationTypeDAO (fi.otavanopisto.pyramus.dao.base.EducationTypeDAO)2 EducationalTimeUnitDAO (fi.otavanopisto.pyramus.dao.base.EducationalTimeUnitDAO)2 OrganizationDAO (fi.otavanopisto.pyramus.dao.base.OrganizationDAO)2 SubjectDAO (fi.otavanopisto.pyramus.dao.base.SubjectDAO)2