Search in sources :

Example 1 with CourseTemplateFilter

use of fi.otavanopisto.pyramus.dao.courses.CourseDAO.CourseTemplateFilter in project pyramus by otavanopisto.

the class SearchCoursesJSONRequestController method process.

/**
 * Processes the request to search courses.
 * The request should contain the either following parameters (for simple search):
 * <dl>
 *   <dt><code>text</code></dt>
 *   <dd>The text to search for</dd>
 * </dl>
 * or the following parameters (for advanced search):
 * <dl>
 *   <dt><code>name</code></dt>
 *   <dd>Course name to find.</dd>
 *   <dt><code>tags</code></dt>
 *   <dd>Tags to find.</dd>
 *   <dt><code>nameExtension</code></dt>
 *   <dd>The name extension to find.</dd>
 *   <dt><code>description</code></dt>
 *   <dd>The description to find.</dd>
 *   <dt><code>state</code></dt>
 *   <dd>The ID of the course state to find.</dd>
 *   <dt><code>subject</code></dt>
 *   <dd>The ID of the subject to find.</dd>
 *   <dt><code>timeframeStart</code></dt>
 *   <dd>The start of the timeframe to find.</dd>
 *   <dt><code>timeframeEnd</code></dt>
 *   <dd>The end of the timeframe to find.</dd>
 *   <dt><code>educationType</code></dt>
 *   <dd>The education type to find.</dd>
 *   <dt><code>educationSubtype</code></dt>
 *   <dd>The education subtype to find.</dd>
 *   <dt><code>timeframeMode</code></dt>
 *   <dd>The mode of the timeframe. Can be <code>INCLUSIVE</code>
 *   or <code>EXCLUSIVE</code>.</dd>
 * </dl>
 *
 * @param jsonRequestContext The JSON request context
 */
public void process(JSONRequestContext requestContext) {
    CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
    CourseStateDAO courseStateDAO = DAOFactory.getInstance().getCourseStateDAO();
    EducationTypeDAO educationTypeDAO = DAOFactory.getInstance().getEducationTypeDAO();
    SubjectDAO subjectDAO = DAOFactory.getInstance().getSubjectDAO();
    EducationSubtypeDAO educationSubtypeDAO = DAOFactory.getInstance().getEducationSubtypeDAO();
    // Determine the number of results shown per page. If not defined, default to ten results per page
    Integer resultsPerPage = NumberUtils.createInteger(requestContext.getRequest().getParameter("maxResults"));
    if (resultsPerPage == null)
        resultsPerPage = 10;
    // Determine the result page to be shown. If not defined, default to the first page
    Integer page = NumberUtils.createInteger(requestContext.getRequest().getParameter("page"));
    if (page == null) {
        page = 0;
    }
    SearchResult<Course> searchResult;
    if ("advanced".equals(requestContext.getRequest().getParameter("activeTab"))) {
        String name = requestContext.getString("name");
        String tags = requestContext.getString("tags");
        if (!StringUtils.isBlank(tags))
            tags = tags.replace(',', ' ');
        String nameExtension = requestContext.getString("nameExtension");
        String description = requestContext.getString("description");
        CourseState courseState = null;
        Long courseStateId = requestContext.getLong("state");
        if (courseStateId != null) {
            courseState = courseStateDAO.findById(courseStateId);
        }
        Subject subject = null;
        Long subjectId = requestContext.getLong("subject");
        if (subjectId != null) {
            subject = subjectDAO.findById(subjectId);
        }
        Date timeframeStart = null;
        String value = requestContext.getString("timeframeStart");
        if (NumberUtils.isNumber(value)) {
            timeframeStart = new Date(NumberUtils.createLong(value));
        }
        Date timeframeEnd = null;
        value = requestContext.getString("timeframeEnd");
        if (NumberUtils.isNumber(value)) {
            timeframeEnd = new Date(NumberUtils.createLong(value));
        }
        EducationType educationType = null;
        Long educationTypeId = requestContext.getLong("educationType");
        if (educationTypeId != null) {
            educationType = educationTypeDAO.findById(educationTypeId);
        }
        EducationSubtype educationSubtype = null;
        Long educationSubtypeId = requestContext.getLong("educationSubtype");
        if (educationSubtypeId != null) {
            educationSubtype = educationSubtypeDAO.findById(educationSubtypeId);
        }
        CourseTemplateFilter courseTemplateFilter = (CourseTemplateFilter) requestContext.getEnum("courseTemplateFilter", CourseTemplateFilter.class);
        SearchTimeFilterMode timeFilterMode = (SearchTimeFilterMode) requestContext.getEnum("timeframeMode", SearchTimeFilterMode.class);
        searchResult = courseDAO.searchCourses(resultsPerPage, page, name, tags, nameExtension, description, courseState, subject, timeFilterMode, timeframeStart, timeframeEnd, educationType, educationSubtype, true, courseTemplateFilter);
    } else {
        String text = requestContext.getRequest().getParameter("text");
        searchResult = courseDAO.searchCoursesBasic(resultsPerPage, page, text, true);
    }
    List<Map<String, Object>> results = new ArrayList<>();
    List<Course> courses = searchResult.getResults();
    for (Course course : courses) {
        Map<String, Object> courseInfo = new HashMap<>();
        courseInfo.put("id", course.getId());
        courseInfo.put("name", course.getName());
        courseInfo.put("nameExtension", course.getNameExtension());
        if (course.getBeginDate() != null) {
            courseInfo.put("beginDate", course.getBeginDate().getTime());
        }
        if (course.getEndDate() != null) {
            courseInfo.put("endDate", course.getEndDate().getTime());
        }
        results.add(courseInfo);
    }
    String statusMessage;
    Locale locale = requestContext.getRequest().getLocale();
    if (searchResult.getTotalHitCount() > 0) {
        statusMessage = Messages.getInstance().getText(locale, "courses.searchCourses.searchStatus", new Object[] { searchResult.getFirstResult() + 1, searchResult.getLastResult() + 1, searchResult.getTotalHitCount() });
    } else {
        statusMessage = Messages.getInstance().getText(locale, "courses.searchCourses.searchStatusNoMatches");
    }
    requestContext.addResponseParameter("results", results);
    requestContext.addResponseParameter("statusMessage", statusMessage);
    requestContext.addResponseParameter("pages", searchResult.getPages());
    requestContext.addResponseParameter("page", searchResult.getPage());
}
Also used : Locale(java.util.Locale) EducationType(fi.otavanopisto.pyramus.domainmodel.base.EducationType) HashMap(java.util.HashMap) SubjectDAO(fi.otavanopisto.pyramus.dao.base.SubjectDAO) CourseDAO(fi.otavanopisto.pyramus.dao.courses.CourseDAO) ArrayList(java.util.ArrayList) CourseStateDAO(fi.otavanopisto.pyramus.dao.courses.CourseStateDAO) SearchTimeFilterMode(fi.otavanopisto.pyramus.persistence.search.SearchTimeFilterMode) CourseState(fi.otavanopisto.pyramus.domainmodel.courses.CourseState) Course(fi.otavanopisto.pyramus.domainmodel.courses.Course) EducationSubtype(fi.otavanopisto.pyramus.domainmodel.base.EducationSubtype) Subject(fi.otavanopisto.pyramus.domainmodel.base.Subject) Date(java.util.Date) EducationSubtypeDAO(fi.otavanopisto.pyramus.dao.base.EducationSubtypeDAO) CourseTemplateFilter(fi.otavanopisto.pyramus.dao.courses.CourseDAO.CourseTemplateFilter) EducationTypeDAO(fi.otavanopisto.pyramus.dao.base.EducationTypeDAO) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

EducationSubtypeDAO (fi.otavanopisto.pyramus.dao.base.EducationSubtypeDAO)1 EducationTypeDAO (fi.otavanopisto.pyramus.dao.base.EducationTypeDAO)1 SubjectDAO (fi.otavanopisto.pyramus.dao.base.SubjectDAO)1 CourseDAO (fi.otavanopisto.pyramus.dao.courses.CourseDAO)1 CourseTemplateFilter (fi.otavanopisto.pyramus.dao.courses.CourseDAO.CourseTemplateFilter)1 CourseStateDAO (fi.otavanopisto.pyramus.dao.courses.CourseStateDAO)1 EducationSubtype (fi.otavanopisto.pyramus.domainmodel.base.EducationSubtype)1 EducationType (fi.otavanopisto.pyramus.domainmodel.base.EducationType)1 Subject (fi.otavanopisto.pyramus.domainmodel.base.Subject)1 Course (fi.otavanopisto.pyramus.domainmodel.courses.Course)1 CourseState (fi.otavanopisto.pyramus.domainmodel.courses.CourseState)1 SearchTimeFilterMode (fi.otavanopisto.pyramus.persistence.search.SearchTimeFilterMode)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1