Search in sources :

Example 6 with Resource

use of fi.otavanopisto.pyramus.domainmodel.resources.Resource in project pyramus by otavanopisto.

the class SearchResourcesJSONRequestController method process.

public void process(JSONRequestContext requestContext) {
    ResourceDAO resourceDAO = DAOFactory.getInstance().getResourceDAO();
    ResourceCategoryDAO resourceCategoryDAO = DAOFactory.getInstance().getResourceCategoryDAO();
    Integer resultsPerPage = NumberUtils.createInteger(requestContext.getRequest().getParameter("maxResults"));
    if (resultsPerPage == null) {
        resultsPerPage = 10;
    }
    Integer page = NumberUtils.createInteger(requestContext.getRequest().getParameter("page"));
    if (page == null) {
        page = 0;
    }
    SearchResult<Resource> searchResult;
    if ("advanced".equals(requestContext.getString("activeTab"))) {
        String name = requestContext.getRequest().getParameter("name");
        String tags = requestContext.getRequest().getParameter("tags");
        ResourceType resourceType = null;
        String resourceTypeParam = requestContext.getRequest().getParameter("resourceType");
        if (!StringUtils.isBlank(resourceTypeParam)) {
            resourceType = Enum.valueOf(ResourceType.class, resourceTypeParam);
        }
        ResourceCategory resourceCategory = null;
        String resourceCategoryParam = requestContext.getRequest().getParameter("resourceCategory");
        if (!StringUtils.isBlank(resourceCategoryParam)) {
            resourceCategory = resourceCategoryDAO.findById(NumberUtils.createLong(resourceCategoryParam));
        }
        searchResult = resourceDAO.searchResources(resultsPerPage, page, name, tags, resourceType, resourceCategory, true);
    } else {
        String simpleQuery = requestContext.getString("simpleQuery");
        searchResult = resourceDAO.searchResourcesBasic(resultsPerPage, page, simpleQuery);
    }
    List<Map<String, Object>> results = new ArrayList<>();
    List<Resource> resources = searchResult.getResults();
    for (Resource resource : resources) {
        Double unitCost = 0.0;
        if (resource instanceof MaterialResource) {
            unitCost = ((MaterialResource) resource).getUnitCost().getAmount();
        } else if (resource instanceof WorkResource) {
            unitCost = ((WorkResource) resource).getCostPerUse().getAmount();
        }
        Double hourlyCost = 0.0;
        if (resource instanceof WorkResource) {
            hourlyCost = ((WorkResource) resource).getHourlyCost().getAmount();
        }
        Map<String, Object> resourceInfo = new HashMap<>();
        resourceInfo.put("id", resource.getId());
        resourceInfo.put("name", resource.getName());
        resourceInfo.put("resourceType", resource.getResourceType());
        resourceInfo.put("resourceCategoryId", resource.getCategory().getId());
        resourceInfo.put("resourceCategoryName", resource.getCategory().getName());
        resourceInfo.put("unitCost", unitCost);
        resourceInfo.put("hourlyCost", hourlyCost);
        results.add(resourceInfo);
    }
    String statusMessage;
    Locale locale = requestContext.getRequest().getLocale();
    if (searchResult.getTotalHitCount() > 0) {
        statusMessage = Messages.getInstance().getText(locale, "resources.searchResources.searchStatus", new Object[] { searchResult.getFirstResult() + 1, searchResult.getLastResult() + 1, searchResult.getTotalHitCount() });
    } else {
        statusMessage = Messages.getInstance().getText(locale, "resources.searchResources.searchStatusNoMatches");
    }
    requestContext.addResponseParameter("results", results);
    requestContext.addResponseParameter("statusMessage", statusMessage);
    requestContext.addResponseParameter("pages", searchResult.getPages());
    requestContext.addResponseParameter("page", searchResult.getPage());
}
Also used : MaterialResource(fi.otavanopisto.pyramus.domainmodel.resources.MaterialResource) Locale(java.util.Locale) WorkResource(fi.otavanopisto.pyramus.domainmodel.resources.WorkResource) HashMap(java.util.HashMap) ResourceCategoryDAO(fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO) Resource(fi.otavanopisto.pyramus.domainmodel.resources.Resource) MaterialResource(fi.otavanopisto.pyramus.domainmodel.resources.MaterialResource) WorkResource(fi.otavanopisto.pyramus.domainmodel.resources.WorkResource) ArrayList(java.util.ArrayList) ResourceType(fi.otavanopisto.pyramus.domainmodel.resources.ResourceType) ResourceCategory(fi.otavanopisto.pyramus.domainmodel.resources.ResourceCategory) ResourceDAO(fi.otavanopisto.pyramus.dao.resources.ResourceDAO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with Resource

use of fi.otavanopisto.pyramus.domainmodel.resources.Resource 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)7 Resource (fi.otavanopisto.pyramus.domainmodel.resources.Resource)7 ResourceCategoryDAO (fi.otavanopisto.pyramus.dao.resources.ResourceCategoryDAO)4 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)3 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 TagDAO (fi.otavanopisto.pyramus.dao.base.TagDAO)2 BasicCourseResourceDAO (fi.otavanopisto.pyramus.dao.courses.BasicCourseResourceDAO)2 CourseComponentDAO (fi.otavanopisto.pyramus.dao.courses.CourseComponentDAO)2 CourseComponentResourceDAO (fi.otavanopisto.pyramus.dao.courses.CourseComponentResourceDAO)2