use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class EnvironmentPermissionResolver method hasCourseAccess.
private boolean hasCourseAccess(Course course, fi.otavanopisto.pyramus.domainmodel.users.User userEntity, Permission permission) {
PyramusPermissionCollection permissionCollection = findCollection(permission.getName());
if (permissionCollection != null) {
try {
String[] defaultRoles = permissionCollection.getDefaultRoles(permission.getName());
// Is EnvironmentRole in the environment roles of the permission
if ((userEntity.getRole() != null) && ArrayUtils.contains(defaultRoles, userEntity.getRole().toString())) {
return true;
}
CourseRoleArchetype[] defaultCourseRoles = permissionCollection.getDefaultCourseRoles(permission.getName());
if (userEntity instanceof Student) {
CourseStudent courseStudent = courseStudentDAO.findByCourseAndStudent(course, (Student) userEntity);
if (courseStudent != null) {
return ArrayUtils.contains(defaultCourseRoles, CourseRoleArchetype.STUDENT);
} else {
return false;
}
} else if (userEntity instanceof StaffMember) {
CourseStaffMember courseStaffMember = courseStaffMemberDAO.findByCourseAndStaffMember(course, (StaffMember) userEntity);
if (courseStaffMember != null) {
return ArrayUtils.contains(defaultCourseRoles, CourseRoleArchetype.TEACHER);
} else {
return false;
}
} else {
logger.severe(String.format("UserEntity could not be casted to a student nor staffmember."));
}
} catch (NoSuchFieldException e) {
}
}
return false;
}
use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class EditStudentProjectJSONRequestController method process.
public void process(JSONRequestContext jsonRequestContext) {
StaffMemberDAO staffMemberDAO = DAOFactory.getInstance().getStaffMemberDAO();
ModuleDAO moduleDAO = DAOFactory.getInstance().getModuleDAO();
CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
CourseStudentDAO courseStudentDAO = DAOFactory.getInstance().getCourseStudentDAO();
StudentProjectDAO studentProjectDAO = DAOFactory.getInstance().getStudentProjectDAO();
StudentProjectModuleDAO studentProjectModuleDAO = DAOFactory.getInstance().getStudentProjectModuleDAO();
GradeDAO gradeDAO = DAOFactory.getInstance().getGradeDAO();
ProjectAssessmentDAO projectAssessmentDAO = DAOFactory.getInstance().getProjectAssessmentDAO();
EducationalTimeUnitDAO educationalTimeUnitDAO = DAOFactory.getInstance().getEducationalTimeUnitDAO();
AcademicTermDAO academicTermDAO = DAOFactory.getInstance().getAcademicTermDAO();
TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
DefaultsDAO defaultsDAO = DAOFactory.getInstance().getDefaultsDAO();
Defaults defaults = defaultsDAO.getDefaults();
// Project
Long studentProjectId = jsonRequestContext.getLong("studentProject");
StudentProject studentProject = studentProjectDAO.findById(studentProjectId);
// Version check
Long version = jsonRequestContext.getLong("version");
if (!studentProject.getVersion().equals(version))
throw new StaleObjectStateException(StudentProject.class.getName(), studentProject.getId());
String name = jsonRequestContext.getString("name");
String description = jsonRequestContext.getString("description");
StaffMember staffMember = staffMemberDAO.findById(jsonRequestContext.getLoggedUserId());
Long optionalStudiesLengthTimeUnitId = jsonRequestContext.getLong("optionalStudiesLengthTimeUnit");
EducationalTimeUnit optionalStudiesLengthTimeUnit = educationalTimeUnitDAO.findById(optionalStudiesLengthTimeUnitId);
Double optionalStudiesLength = jsonRequestContext.getDouble("optionalStudiesLength");
String tagsText = jsonRequestContext.getString("tags");
Long studentId = jsonRequestContext.getLong("student");
CourseOptionality projectOptionality = (CourseOptionality) jsonRequestContext.getEnum("projectOptionality", CourseOptionality.class);
Set<Tag> tagEntities = new HashSet<>();
if (!StringUtils.isBlank(tagsText)) {
List<String> tags = Arrays.asList(tagsText.split("[\\ ,]"));
for (String tag : tags) {
if (!StringUtils.isBlank(tag)) {
Tag tagEntity = tagDAO.findByText(tag.trim());
if (tagEntity == null)
tagEntity = tagDAO.create(tag);
tagEntities.add(tagEntity);
}
}
}
Student student = studentDAO.findById(studentId);
if (!studentProject.getStudent().equals(student)) {
studentProjectDAO.updateStudent(studentProject, student, staffMember);
}
studentProjectDAO.update(studentProject, name, description, optionalStudiesLength, optionalStudiesLengthTimeUnit, projectOptionality, staffMember);
// Tags
studentProjectDAO.updateTags(studentProject, tagEntities);
// ProjectAssessments
int rowCount = jsonRequestContext.getInteger("assessmentsTable.rowCount").intValue();
for (int i = 0; i < rowCount; i++) {
String colPrefix = "assessmentsTable." + i;
Long assessmentModified = jsonRequestContext.getLong(colPrefix + ".modified");
if ((assessmentModified != null) && (assessmentModified.intValue() == 1)) {
Long assessmentId = jsonRequestContext.getLong(colPrefix + ".assessmentId");
ProjectAssessment projectAssessment = ((assessmentId != null) && (assessmentId.intValue() != -1)) ? projectAssessmentDAO.findById(assessmentId) : null;
Long assessmentArchived = jsonRequestContext.getLong(colPrefix + ".deleted");
if ((assessmentArchived != null) && (assessmentArchived.intValue() == 1)) {
if (projectAssessment != null)
projectAssessmentDAO.archive(projectAssessment);
else
throw new SmvcRuntimeException(PyramusStatusCode.OK, "Assessment marked for delete does not exist.");
} else {
Date assessmentDate = jsonRequestContext.getDate(colPrefix + ".date");
Long assessmentGradeId = jsonRequestContext.getLong(colPrefix + ".grade");
Grade grade = assessmentGradeId != null ? gradeDAO.findById(assessmentGradeId) : null;
String verbalAssessment = projectAssessment != null ? projectAssessment.getVerbalAssessment() : null;
Long verbalAssessmentModified = jsonRequestContext.getLong(colPrefix + ".verbalModified");
if ((verbalAssessmentModified != null) && (verbalAssessmentModified.intValue() == 1))
verbalAssessment = jsonRequestContext.getString(colPrefix + ".verbalAssessment");
if (projectAssessment == null) {
projectAssessmentDAO.create(studentProject, staffMember, grade, assessmentDate, verbalAssessment);
} else {
projectAssessmentDAO.update(projectAssessment, staffMember, grade, assessmentDate, verbalAssessment);
}
}
}
}
// Student project modules
Set<Long> existingModuleIds = new HashSet<>();
rowCount = jsonRequestContext.getInteger("modulesTable.rowCount").intValue();
for (int i = 0; i < rowCount; i++) {
String colPrefix = "modulesTable." + i;
Long studentProjectModuleId = jsonRequestContext.getLong(colPrefix + ".studentProjectModuleId");
CourseOptionality optionality = (CourseOptionality) jsonRequestContext.getEnum(colPrefix + ".optionality", CourseOptionality.class);
Long studyTermId = jsonRequestContext.getLong(colPrefix + ".academicTerm");
AcademicTerm academicTerm = studyTermId == null ? null : academicTermDAO.findById(studyTermId);
if (studentProjectModuleId == -1) {
Long moduleId = jsonRequestContext.getLong(colPrefix + ".moduleId");
Module module = moduleDAO.findById(moduleId);
studentProjectModuleId = studentProjectModuleDAO.create(studentProject, module, academicTerm, optionality).getId();
} else {
studentProjectModuleDAO.update(studentProjectModuleDAO.findById(studentProjectModuleId), academicTerm, optionality);
}
existingModuleIds.add(studentProjectModuleId);
}
// Removed Student project modules
List<StudentProjectModule> studentProjectModules = studentProjectModuleDAO.listByStudentProject(studentProject);
for (StudentProjectModule studentProjectModule : studentProjectModules) {
if (!existingModuleIds.contains(studentProjectModule.getId())) {
studentProjectModuleDAO.delete(studentProjectModule);
}
}
// Student project courses
rowCount = jsonRequestContext.getInteger("coursesTable.rowCount").intValue();
for (int i = 0; i < rowCount; i++) {
String colPrefix = "coursesTable." + i;
Long courseId = jsonRequestContext.getLong(colPrefix + ".courseId");
CourseOptionality optionality = (CourseOptionality) jsonRequestContext.getEnum(colPrefix + ".optionality", CourseOptionality.class);
Course course = courseId == -1 ? null : courseDAO.findById(courseId);
CourseStudent courseStudent = courseStudentDAO.findByCourseAndStudent(course, studentProject.getStudent());
if (courseStudent == null) {
CourseEnrolmentType courseEnrolmentType = defaults.getInitialCourseEnrolmentType();
CourseParticipationType participationType = defaults.getInitialCourseParticipationType();
Date enrolmentDate = new Date(System.currentTimeMillis());
Boolean lodging = Boolean.FALSE;
String organization = null;
String additionalInfo = null;
Room room = null;
BigDecimal lodgingFee = null;
Currency lodgingFeeCurrency = null;
BigDecimal reservationFee = null;
Currency reservationFeeCurrency = null;
try {
courseStudent = courseStudentDAO.create(course, studentProject.getStudent(), courseEnrolmentType, participationType, enrolmentDate, lodging, optionality, null, organization, additionalInfo, room, lodgingFee, lodgingFeeCurrency, reservationFee, reservationFeeCurrency, Boolean.FALSE);
} catch (DuplicateCourseStudentException dcse) {
Locale locale = jsonRequestContext.getRequest().getLocale();
throw new SmvcRuntimeException(PyramusStatusCode.UNDEFINED, Messages.getInstance().getText(locale, "generic.errors.duplicateCourseStudent", new Object[] { student.getFullName() }));
}
} else {
courseStudentDAO.updateOptionality(courseStudent, optionality);
}
}
jsonRequestContext.setRedirectURL(jsonRequestContext.getReferer(true));
}
use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class EditCourseJSONRequestController method processSignupStudentGroups.
private void processSignupStudentGroups(JSONRequestContext requestContext, Course course, StaffMember loggedUser) {
CourseSignupStudentGroupDAO courseSignupStudentGroupDAO = DAOFactory.getInstance().getCourseSignupStudentGroupDAO();
StudentGroupDAO studentGroupDAO = DAOFactory.getInstance().getStudentGroupDAO();
List<CourseSignupStudentGroup> signupStudentGroups = courseSignupStudentGroupDAO.listByCourse(course);
Integer studentGroupsRowCount = requestContext.getInteger("signupStudentGroupsTable.rowCount");
if (studentGroupsRowCount != null) {
Set<Long> studentGroupIdsPresent = new HashSet<>();
for (int i = 0; i < studentGroupsRowCount; i++) {
Long studentGroupId = requestContext.getLong(String.format("signupStudentGroupsTable.%d.studentGroupId", i));
if (studentGroupId != null) {
studentGroupIdsPresent.add(studentGroupId);
}
}
// Create missing groups
studentGroupIdsPresent.forEach(studentGroupId -> {
if (signupStudentGroups.stream().noneMatch(signupStudentGroup -> Objects.equals(signupStudentGroup.getStudentGroup().getId(), studentGroupId))) {
StudentGroup studentGroup = studentGroupDAO.findById(studentGroupId);
if ((studentGroup != null) && UserUtils.canAccessOrganization(loggedUser, studentGroup.getOrganization())) {
courseSignupStudentGroupDAO.create(course, studentGroup);
} else {
throw new SmvcRuntimeException(PyramusStatusCode.UNAUTHORIZED, "Invalid organization.");
}
}
});
// Remove groups that don't exist anymore
signupStudentGroups.stream().filter(signupStudentGroup -> !studentGroupIdsPresent.contains(signupStudentGroup.getStudentGroup().getId())).forEach(signupStudentGroup -> {
if (UserUtils.canAccessOrganization(loggedUser, signupStudentGroup.getStudentGroup().getOrganization())) {
courseSignupStudentGroupDAO.delete(signupStudentGroup);
} else {
throw new SmvcRuntimeException(PyramusStatusCode.UNAUTHORIZED, "Invalid organization.");
}
});
}
}
use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class CreateMailTemplateJSONRequestController method process.
public void process(JSONRequestContext requestContext) {
try {
StaffMemberDAO staffMemberDAO = DAOFactory.getInstance().getStaffMemberDAO();
StaffMember staffMember = staffMemberDAO.findById(requestContext.getLoggedUserId());
if (staffMember == null) {
logger.log(Level.WARNING, "Refusing mail template due to staff member not found");
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String line = requestContext.getString("line");
String name = requestContext.getString("name");
String subject = requestContext.getString("subject");
String content = requestContext.getString("content");
if (StringUtils.isEmpty(name) || StringUtils.isEmpty(subject) || StringUtils.isEmpty(content)) {
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
ApplicationMailTemplateDAO applicationMailTemplateDAO = DAOFactory.getInstance().getApplicationMailTemplateDAO();
ApplicationMailTemplate applicationMailTemplate = applicationMailTemplateDAO.create(line, name, subject, content, staffMember);
String redirectURL = requestContext.getRequest().getContextPath() + "/applications/editmailtemplate.page?template=" + applicationMailTemplate.getId();
requestContext.setRedirectURL(redirectURL);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error saving mail template", e);
}
}
use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class GenerateAcceptanceDocumentJSONRequestController method process.
public void process(JSONRequestContext requestContext) {
// Ensure user has SSN to be able to eventually sign the generated document
StaffMemberDAO staffMemberDAO = DAOFactory.getInstance().getStaffMemberDAO();
StaffMember staffMember = staffMemberDAO.findById(requestContext.getLoggedUserId());
if (staffMember == null) {
logger.warning("Current user cannot be resolved");
fail(requestContext, "Et ole kirjautunut sisään");
return;
}
if (StringUtils.isBlank(staffMember.getPerson().getSocialSecurityNumber())) {
logger.warning("Current user lacks social security number");
fail(requestContext, "Allekirjoittamiseen vaadittua henkilötunnusta ei ole asetettu");
return;
}
// Find application and ensure its state
Long id = requestContext.getLong("id");
if (id == null) {
logger.warning("Missing application id");
fail(requestContext, "Puuttuva hakemustunnus");
return;
}
ApplicationDAO applicationDAO = DAOFactory.getInstance().getApplicationDAO();
Application application = applicationDAO.findById(id);
if (application == null) {
logger.warning(String.format("Application with id %d not found", id));
fail(requestContext, String.format("Hakemusta tunnuksella %d ei löytynyt", id));
return;
}
if (application.getState() != ApplicationState.WAITING_STAFF_SIGNATURE) {
logger.warning(String.format("Application with id %d in incorrect state (%s)", id, application.getState()));
fail(requestContext, "Hakemus ei ole allekirjoitettavassa tilassa");
return;
}
// Signatures tracking
ApplicationSignaturesDAO applicationSignaturesDAO = DAOFactory.getInstance().getApplicationSignaturesDAO();
ApplicationSignatures signatures = applicationSignaturesDAO.findByApplication(application);
if (signatures == null) {
signatures = applicationSignaturesDAO.create(application);
}
if (signatures.getStaffDocumentState() == ApplicationSignatureState.SIGNED) {
fail(requestContext, "Hyväksymisasiakirja on jo allekirjoitettu");
return;
}
// Gather required dynamic data for the PDF document
JSONObject formData = JSONObject.fromObject(application.getFormData());
String applicantName = String.format("%s %s", getFormValue(formData, "field-first-names"), getFormValue(formData, "field-last-name"));
String line = application.getLine();
String documentName = String.format("Hyväksyntä: %s", applicantName);
OnnistuuClient onnistuuClient = OnnistuuClient.getInstance();
try {
// Generate Onnistuu document (if not done before)
String documentId = null;
if (signatures.getStaffDocumentId() == null) {
documentId = onnistuuClient.createDocument(documentName);
signatures = applicationSignaturesDAO.updateStaffDocument(signatures, documentId, null, null, ApplicationSignatureState.DOCUMENT_CREATED);
} else {
documentId = signatures.getStaffDocumentId();
}
if (signatures.getStaffDocumentState() == ApplicationSignatureState.DOCUMENT_CREATED) {
byte[] pdf = onnistuuClient.generateStaffSignatureDocument(requestContext, applicantName, line, staffMember);
onnistuuClient.addPdf(documentId, pdf);
signatures = applicationSignaturesDAO.updateStaffDocument(signatures, documentId, null, null, ApplicationSignatureState.PDF_UPLOADED);
}
if (signatures.getStaffDocumentState() == ApplicationSignatureState.PDF_UPLOADED) {
OnnistuuClient.Invitation invitation = onnistuuClient.createInvitation(documentId, staffMember.getPrimaryEmail().getAddress());
signatures = applicationSignaturesDAO.updateStaffDocument(signatures, documentId, invitation.getUuid(), invitation.getPassphrase(), ApplicationSignatureState.INVITATION_CREATED);
}
// Respond with URL to view the PDF
requestContext.addResponseParameter("status", "OK");
requestContext.addResponseParameter("documentUrl", String.format("https://www.onnistuu.fi/api/v1/invitation/%s/%s/files/0", signatures.getStaffInvitationId(), signatures.getStaffInvitationToken()));
} catch (OnnistuuClientException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
fail(requestContext, e.getMessage());
}
}
Aggregations