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());
}
Aggregations