package fi.otavanopisto.pyramus.views.students; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; 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.ContactTypeDAO; import fi.otavanopisto.pyramus.dao.base.ContactURLTypeDAO; import fi.otavanopisto.pyramus.dao.base.CurriculumDAO; import fi.otavanopisto.pyramus.dao.base.LanguageDAO; import fi.otavanopisto.pyramus.dao.base.MunicipalityDAO; import fi.otavanopisto.pyramus.dao.base.NationalityDAO; import fi.otavanopisto.pyramus.dao.base.PersonDAO; import fi.otavanopisto.pyramus.dao.base.SchoolDAO; import fi.otavanopisto.pyramus.dao.base.StudyProgrammeDAO; import fi.otavanopisto.pyramus.dao.grading.CourseAssessmentDAO; import fi.otavanopisto.pyramus.dao.grading.CreditLinkDAO; import fi.otavanopisto.pyramus.dao.grading.TransferCreditDAO; import fi.otavanopisto.pyramus.dao.students.StudentActivityTypeDAO; import fi.otavanopisto.pyramus.dao.students.StudentDAO; import fi.otavanopisto.pyramus.dao.students.StudentEducationalLevelDAO; import fi.otavanopisto.pyramus.dao.students.StudentExaminationTypeDAO; import fi.otavanopisto.pyramus.dao.students.StudentStudyEndReasonDAO; import fi.otavanopisto.pyramus.dao.users.UserDAO; import fi.otavanopisto.pyramus.dao.users.UserIdentificationDAO; import fi.otavanopisto.pyramus.dao.users.UserVariableDAO; import fi.otavanopisto.pyramus.dao.users.UserVariableKeyDAO; import fi.otavanopisto.pyramus.domainmodel.base.ContactType; import fi.otavanopisto.pyramus.domainmodel.base.ContactURLType; import fi.otavanopisto.pyramus.domainmodel.base.Language; import fi.otavanopisto.pyramus.domainmodel.base.Municipality; import fi.otavanopisto.pyramus.domainmodel.base.Nationality; import fi.otavanopisto.pyramus.domainmodel.base.Person; import fi.otavanopisto.pyramus.domainmodel.base.School; import fi.otavanopisto.pyramus.domainmodel.base.Tag; import fi.otavanopisto.pyramus.domainmodel.students.Student; import fi.otavanopisto.pyramus.domainmodel.users.User; import fi.otavanopisto.pyramus.domainmodel.users.UserIdentification; import fi.otavanopisto.pyramus.domainmodel.users.UserVariable; import fi.otavanopisto.pyramus.domainmodel.users.UserVariableKey; import fi.otavanopisto.pyramus.framework.PyramusViewController; import fi.otavanopisto.pyramus.framework.UserRole; import fi.otavanopisto.pyramus.framework.UserUtils; import fi.otavanopisto.pyramus.plugin.auth.AuthenticationProviderVault; import fi.otavanopisto.pyramus.plugin.auth.InternalAuthenticationProvider; import fi.otavanopisto.pyramus.util.StringAttributeComparator; public class EditStudentViewController extends PyramusViewController implements Breadcrumbable { public void process(PageRequestContext pageRequestContext) { StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO(); PersonDAO personDAO = DAOFactory.getInstance().getPersonDAO(); StudentActivityTypeDAO studentActivityTypeDAO = DAOFactory.getInstance().getStudentActivityTypeDAO(); StudentEducationalLevelDAO studentEducationalLevelDAO = DAOFactory.getInstance().getStudentEducationalLevelDAO(); StudentExaminationTypeDAO studentExaminationTypeDAO = DAOFactory.getInstance().getStudentExaminationTypeDAO(); StudentStudyEndReasonDAO studyEndReasonDAO = DAOFactory.getInstance().getStudentStudyEndReasonDAO(); UserVariableKeyDAO userVariableKeyDAO = DAOFactory.getInstance().getUserVariableKeyDAO(); UserVariableDAO userVariableDAO = DAOFactory.getInstance().getUserVariableDAO(); StudyProgrammeDAO studyProgrammeDAO = DAOFactory.getInstance().getStudyProgrammeDAO(); MunicipalityDAO municipalityDAO = DAOFactory.getInstance().getMunicipalityDAO(); NationalityDAO nationalityDAO = DAOFactory.getInstance().getNationalityDAO(); SchoolDAO schoolDAO = DAOFactory.getInstance().getSchoolDAO(); LanguageDAO languageDAO = DAOFactory.getInstance().getLanguageDAO(); ContactTypeDAO contactTypeDAO = DAOFactory.getInstance().getContactTypeDAO(); ContactURLTypeDAO contactURLTypeDAO = DAOFactory.getInstance().getContactURLTypeDAO(); CreditLinkDAO creditLinkDAO = DAOFactory.getInstance().getCreditLinkDAO(); CourseAssessmentDAO courseAssessmentDAO = DAOFactory.getInstance().getCourseAssessmentDAO(); TransferCreditDAO transferCreditDAO = DAOFactory.getInstance().getTransferCreditDAO(); UserIdentificationDAO userIdentificationDAO = DAOFactory.getInstance().getUserIdentificationDAO(); UserDAO userDAO = DAOFactory.getInstance().getUserDAO(); CurriculumDAO curriculumDAO = DAOFactory.getInstance().getCurriculumDAO(); User loggedUser = userDAO.findById(pageRequestContext.getLoggedUserId()); Long personId = pageRequestContext.getLong("person"); Person person = personDAO.findById(personId); List<Student> students = studentDAO.listByPerson(person); Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { /** * Ordering study programmes as follows * 1. studies that have start date but no end date (ongoing) * 2. studies that have no start nor end date * 3. studies that have ended * 4. studies that are archived * 5. other */ int o1class = (o1.getArchived()) ? 4: (o1.getStudyStartDate() != null && o1.getStudyEndDate() == null) ? 1: (o1.getStudyStartDate() == null && o1.getStudyEndDate() == null) ? 2: (o1.getStudyEndDate() != null) ? 3: 5; int o2class = (o2.getArchived()) ? 4: (o2.getStudyStartDate() != null && o2.getStudyEndDate() == null) ? 1: (o2.getStudyStartDate() == null && o2.getStudyEndDate() == null) ? 2: (o2.getStudyEndDate() != null) ? 3: 5; if (o1class == o2class) { // classes are the same, we try to do last comparison from the start dates return ((o1.getStudyStartDate() != null) && (o2.getStudyStartDate() != null)) ? o2.getStudyStartDate().compareTo(o1.getStudyStartDate()) : 0; } else return o1class < o2class ? -1 : o1class == o2class ? 0 : 1; } }); Map<Long, String> studentTags = new HashMap<>(); Map<Long, Boolean> studentHasCredits = new HashMap<>(); List<UserVariableKey> userVariableKeys = userVariableKeyDAO.listByUserEditable(Boolean.TRUE); Collections.sort(userVariableKeys, new StringAttributeComparator("getVariableName")); for (Student student : students) { StringBuilder tagsBuilder = new StringBuilder(); Iterator<Tag> tagIterator = student.getTags().iterator(); while (tagIterator.hasNext()) { Tag tag = tagIterator.next(); tagsBuilder.append(tag.getText()); if (tagIterator.hasNext()) tagsBuilder.append(' '); } studentTags.put(student.getId(), tagsBuilder.toString()); studentHasCredits.put(student.getId(), creditLinkDAO.countByStudent(student) + courseAssessmentDAO.countByStudent(student) + transferCreditDAO.countByStudent(student) > 0); JSONArray variables = new JSONArray(); for (UserVariableKey userVariableKey : userVariableKeys) { UserVariable userVariable = userVariableDAO.findByUserAndVariableKey(student, userVariableKey); JSONObject variable = new JSONObject(); variable.put("type", userVariableKey.getVariableType()); variable.put("name", userVariableKey.getVariableName()); variable.put("key", userVariableKey.getVariableKey()); variable.put("value", userVariable != null ? userVariable.getValue() : ""); variables.add(variable); } setJsDataVariable(pageRequestContext, "variables." + student.getId(), variables.toString()); } List<Nationality> nationalities = nationalityDAO.listUnarchived(); Collections.sort(nationalities, new StringAttributeComparator("getName")); List<Municipality> municipalities = municipalityDAO.listUnarchived(); Collections.sort(municipalities, new StringAttributeComparator("getName")); List<Language> languages = languageDAO.listUnarchived(); Collections.sort(languages, new StringAttributeComparator("getName")); List<School> schools = schoolDAO.listUnarchived(); Collections.sort(schools, new StringAttributeComparator("getName")); List<ContactURLType> contactURLTypes = contactURLTypeDAO.listUnarchived(); Collections.sort(contactURLTypes, new StringAttributeComparator("getName")); List<ContactType> contactTypes = contactTypeDAO.listUnarchived(); Collections.sort(contactTypes, new StringAttributeComparator("getName")); String username = ""; boolean hasInternalAuthenticationStrategies = AuthenticationProviderVault.getInstance().hasInternalStrategies(); if (UserUtils.allowEditCredentials(loggedUser, person)) { if (hasInternalAuthenticationStrategies) { // TODO: Support for multiple internal authentication providers List<InternalAuthenticationProvider> internalAuthenticationProviders = AuthenticationProviderVault.getInstance().getInternalAuthenticationProviders(); if (internalAuthenticationProviders.size() == 1) { InternalAuthenticationProvider internalAuthenticationProvider = internalAuthenticationProviders.get(0); if (internalAuthenticationProvider != null) { UserIdentification userIdentification = userIdentificationDAO.findByAuthSourceAndPerson(internalAuthenticationProvider.getName(), person); if (internalAuthenticationProvider.canUpdateCredentials()) { if (userIdentification != null) { username = internalAuthenticationProvider.getUsername(userIdentification.getExternalId()); } } } } } } pageRequestContext.getRequest().setAttribute("tags", studentTags); pageRequestContext.getRequest().setAttribute("person", person); pageRequestContext.getRequest().setAttribute("students", students); pageRequestContext.getRequest().setAttribute("activityTypes", studentActivityTypeDAO.listUnarchived()); pageRequestContext.getRequest().setAttribute("contactURLTypes", contactURLTypes); pageRequestContext.getRequest().setAttribute("contactTypes", contactTypes); pageRequestContext.getRequest().setAttribute("examinationTypes", studentExaminationTypeDAO.listUnarchived()); pageRequestContext.getRequest().setAttribute("educationalLevels", studentEducationalLevelDAO.listUnarchived()); pageRequestContext.getRequest().setAttribute("nationalities", nationalities); pageRequestContext.getRequest().setAttribute("municipalities", municipalities); pageRequestContext.getRequest().setAttribute("languages", languages); pageRequestContext.getRequest().setAttribute("schools", schools); pageRequestContext.getRequest().setAttribute("studyProgrammes", studyProgrammeDAO.listUnarchived()); pageRequestContext.getRequest().setAttribute("curriculums", curriculumDAO.listUnarchived()); pageRequestContext.getRequest().setAttribute("studyEndReasons", studyEndReasonDAO.listByParentReason(null)); pageRequestContext.getRequest().setAttribute("variableKeys", userVariableKeys); pageRequestContext.getRequest().setAttribute("studentHasCredits", studentHasCredits); pageRequestContext.getRequest().setAttribute("hasInternalAuthenticationStrategies", hasInternalAuthenticationStrategies); pageRequestContext.getRequest().setAttribute("username", username); pageRequestContext.getRequest().setAttribute("allowEditCredentials", UserUtils.allowEditCredentials(loggedUser, person)); pageRequestContext.setIncludeJSP("/templates/students/editstudent.jsp"); } 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, "students.editStudent.breadcrumb"); } }