package fi.otavanopisto.pyramus.views.modules;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import fi.internetix.smvc.controllers.PageRequestContext;
import fi.otavanopisto.pyramus.I18N.Messages;
import fi.otavanopisto.pyramus.breadcrumbs.Breadcrumbable;
import fi.otavanopisto.pyramus.dao.DAOFactory;
import fi.otavanopisto.pyramus.dao.base.CurriculumDAO;
import fi.otavanopisto.pyramus.dao.base.EducationSubtypeDAO;
import fi.otavanopisto.pyramus.dao.base.EducationTypeDAO;
import fi.otavanopisto.pyramus.dao.base.EducationalTimeUnitDAO;
import fi.otavanopisto.pyramus.dao.base.SubjectDAO;
import fi.otavanopisto.pyramus.dao.courses.CourseDescriptionCategoryDAO;
import fi.otavanopisto.pyramus.domainmodel.base.EducationSubtype;
import fi.otavanopisto.pyramus.domainmodel.base.EducationType;
import fi.otavanopisto.pyramus.domainmodel.base.EducationalTimeUnit;
import fi.otavanopisto.pyramus.domainmodel.base.Subject;
import fi.otavanopisto.pyramus.domainmodel.users.Role;
import fi.otavanopisto.pyramus.framework.PyramusViewController;
import fi.otavanopisto.pyramus.framework.UserRole;
import fi.otavanopisto.pyramus.util.StringAttributeComparator;
/**
* The controller responsible of the Create Module view of the application.
*
* @see fi.otavanopisto.pyramus.json.users.CreateGradingScaleJSONRequestController
*/
public class CreateModuleViewController extends PyramusViewController implements Breadcrumbable {
/**
* Processes the page request by including the corresponding JSP page to the response.
* <p/>
* In order for the JSP page to build the module creation view, a list of all education types and
* subjects are added as request attributes.
*
* @param pageRequestContext Page request context
*/
public void process(PageRequestContext pageRequestContext) {
CourseDescriptionCategoryDAO descriptionCategoryDAO = DAOFactory.getInstance().getCourseDescriptionCategoryDAO();
SubjectDAO subjectDAO = DAOFactory.getInstance().getSubjectDAO();
EducationTypeDAO educationTypeDAO = DAOFactory.getInstance().getEducationTypeDAO();
EducationSubtypeDAO educationSubtypeDAO = DAOFactory.getInstance().getEducationSubtypeDAO();
EducationalTimeUnitDAO educationalTimeUnitDAO = DAOFactory.getInstance().getEducationalTimeUnitDAO();
CurriculumDAO curriculumDAO = DAOFactory.getInstance().getCurriculumDAO();
List<EducationType> educationTypes = educationTypeDAO.listUnarchived();
Collections.sort(educationTypes, new StringAttributeComparator("getName"));
// Subjects
Map<Long, List<Subject>> subjectsByEducationType = new HashMap<>();
List<Subject> subjectsByNoEducationType = subjectDAO.listByEducationType(null);
Collections.sort(subjectsByNoEducationType, new StringAttributeComparator("getName"));
for (EducationType educationType : educationTypes) {
List<Subject> subjectsOfType = subjectDAO.listByEducationType(educationType);
if (subjectsOfType != null && !subjectsOfType.isEmpty()) {
Collections.sort(subjectsOfType, new StringAttributeComparator("getName"));
subjectsByEducationType.put(educationType.getId(), subjectsOfType);
}
}
List<EducationalTimeUnit> educationalTimeUnits = educationalTimeUnitDAO.listUnarchived();
Collections.sort(educationalTimeUnits, new StringAttributeComparator("getName"));
Map<Long, List<EducationSubtype>> educationSubtypes = new HashMap<>();
for (EducationType educationType : educationTypes) {
List<EducationSubtype> subtypes = educationSubtypeDAO.listByEducationType(educationType);
Collections.sort(subtypes, new StringAttributeComparator("getName"));
educationSubtypes.put(educationType.getId(), subtypes);
}
pageRequestContext.getRequest().setAttribute("educationTypes", educationTypes);
pageRequestContext.getRequest().setAttribute("educationSubtypes", educationSubtypes);
pageRequestContext.getRequest().setAttribute("subjectsByNoEducationType", subjectsByNoEducationType);
pageRequestContext.getRequest().setAttribute("subjectsByEducationType", subjectsByEducationType);
pageRequestContext.getRequest().setAttribute("moduleLengthTimeUnits", educationalTimeUnits);
pageRequestContext.getRequest().setAttribute("courseDescriptionCategories", descriptionCategoryDAO.listUnarchived());
pageRequestContext.getRequest().setAttribute("curriculums", curriculumDAO.listUnarchived());
pageRequestContext.setIncludeJSP("/templates/modules/createmodule.jsp");
}
/**
* Returns the roles allowed to access this page. Creating modules is available for users with
* {@link Role#MANAGER} or {@link Role#ADMINISTRATOR} privileges.
*
* @return The roles allowed to access this page
*/
public UserRole[] getAllowedRoles() {
return new UserRole[] { UserRole.MANAGER, UserRole.STUDY_PROGRAMME_LEADER, UserRole.ADMINISTRATOR };
}
/**
* Returns the localized name of this page. Used e.g. for breadcrumb navigation.
*
* @param locale The locale to be used for the name
*
* @return The localized name of this page
*/
public String getName(Locale locale) {
return Messages.getInstance().getText(locale, "modules.createModule.pageTitle");
}
}