use of fi.otavanopisto.pyramus.dao.courses.CourseSignupStudyProgrammeDAO in project pyramus by otavanopisto.
the class EditCourseJSONRequestController method processSignupStudyProgrammes.
private void processSignupStudyProgrammes(JSONRequestContext requestContext, Course course, StaffMember loggedUser) {
CourseSignupStudyProgrammeDAO courseSignupStudyProgrammeDAO = DAOFactory.getInstance().getCourseSignupStudyProgrammeDAO();
StudyProgrammeDAO studyProgrammeDAO = DAOFactory.getInstance().getStudyProgrammeDAO();
List<CourseSignupStudyProgramme> signupStudyProgrammes = courseSignupStudyProgrammeDAO.listByCourse(course);
Integer studyProgrammesRowCount = requestContext.getInteger("signupStudyProgrammesTable.rowCount");
if (studyProgrammesRowCount != null) {
Set<Long> studyProgrammeIdsPresent = new HashSet<>();
for (int i = 0; i < studyProgrammesRowCount; i++) {
Long studyProgrammeId = requestContext.getLong(String.format("signupStudyProgrammesTable.%d.studyProgrammeId", i));
if (studyProgrammeId != null) {
studyProgrammeIdsPresent.add(studyProgrammeId);
}
}
// Create missing groups
studyProgrammeIdsPresent.forEach(studentGroupId -> {
if (signupStudyProgrammes.stream().noneMatch(signupStudentGroup -> Objects.equals(signupStudentGroup.getStudyProgramme().getId(), studentGroupId))) {
StudyProgramme studyProgramme = studyProgrammeDAO.findById(studentGroupId);
if ((studyProgramme != null) && UserUtils.canAccessOrganization(loggedUser, studyProgramme.getOrganization())) {
courseSignupStudyProgrammeDAO.create(course, studyProgramme);
} else {
throw new SmvcRuntimeException(PyramusStatusCode.UNAUTHORIZED, "Invalid organization.");
}
}
});
// Remove groups that don't exist anymore
signupStudyProgrammes.stream().filter(signupStudyProgramme -> !studyProgrammeIdsPresent.contains(signupStudyProgramme.getStudyProgramme().getId())).forEach(signupStudyProgramme -> {
if (UserUtils.canAccessOrganization(loggedUser, signupStudyProgramme.getStudyProgramme().getOrganization())) {
courseSignupStudyProgrammeDAO.delete(signupStudyProgramme);
} else {
throw new SmvcRuntimeException(PyramusStatusCode.UNAUTHORIZED, "Invalid organization.");
}
});
}
}
Aggregations