use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class UpdateApplicationStateJSONRequestController method process.
public void process(JSONRequestContext requestContext) {
try {
StaffMemberDAO staffMemberDAO = DAOFactory.getInstance().getStaffMemberDAO();
StaffMember staffMember = requestContext.getLoggedUserId() == null ? null : staffMemberDAO.findById(requestContext.getLoggedUserId());
if (staffMember == null) {
fail(requestContext, "Et ole kirjautunut sisään");
return;
}
Long id = requestContext.getLong("id");
if (id == null) {
fail(requestContext, "Puuttuva hakemustunnus");
return;
}
ApplicationState applicationState = ApplicationState.valueOf(requestContext.getString("state"));
Boolean lockApplication = requestContext.getBoolean("lockApplication");
Boolean setHandler = requestContext.getBoolean("setHandler");
Boolean removeHandler = requestContext.getBoolean("removeHandler");
// Application update
ApplicationDAO applicationDAO = DAOFactory.getInstance().getApplicationDAO();
Application application = applicationDAO.findById(id);
if (application == null) {
fail(requestContext, "Puuttuva hakemus");
return;
}
if (application.getState() != applicationState) {
if (applicationState == ApplicationState.APPROVED_BY_SCHOOL) {
// Gather required dynamic data from the application form
JSONObject formData = JSONObject.fromObject(application.getFormData());
String line = ApplicationUtils.applicationLineUiValue(application.getLine());
String applicantName = String.format("%s %s", getFormValue(formData, "field-first-names"), getFormValue(formData, "field-last-name"));
String ssn = ApplicationUtils.constructSSN(getFormValue(formData, "field-birthday"), getFormValue(formData, "field-ssn-end"));
String address = String.format("%s, %s %s, %s", getFormValue(formData, "field-street-address"), getFormValue(formData, "field-zip-code"), getFormValue(formData, "field-city"), getFormValue(formData, "field-country"));
String municipality = ApplicationUtils.municipalityUiValue(getFormValue(formData, "field-municipality"));
String nationality = ApplicationUtils.nationalityUiValue(getFormValue(formData, "field-nationality"));
String phone = getFormValue(formData, "field-phone");
String email = StringUtils.lowerCase(StringUtils.trim(getFormValue(formData, "field-email")));
String nickname = getFormValue(formData, "field-nickname");
String guardianMail = getFormValue(formData, "field-underage-email");
// Make sure we have application signatures and school approval
ApplicationSignaturesDAO applicationSignaturesDAO = DAOFactory.getInstance().getApplicationSignaturesDAO();
ApplicationSignatures applicationSignatures = applicationSignaturesDAO.findByApplication(application);
if (applicationSignatures == null || applicationSignatures.getStaffDocumentState() != ApplicationSignatureState.SIGNED) {
logger.log(Level.WARNING, String.format("Application %s not signed by staff", application.getApplicationId()));
fail(requestContext, "Oppilaitos ei ole vielä allekirjoittanut hyväksymisasiakirjaa");
return;
}
OnnistuuClient onnistuuClient = OnnistuuClient.getInstance();
// Create Onnistuu document (if not done before)
String documentId = null;
if (applicationSignatures.getApplicantDocumentId() == null) {
documentId = onnistuuClient.createDocument(String.format("Vastaanotto: %s", applicantName));
applicationSignatures = applicationSignaturesDAO.updateApplicantDocument(applicationSignatures, documentId, null, null, ApplicationSignatureState.DOCUMENT_CREATED);
} else {
documentId = applicationSignatures.getApplicantDocumentId();
}
if (applicationSignatures.getApplicantDocumentState() == ApplicationSignatureState.DOCUMENT_CREATED) {
byte[] pdf = onnistuuClient.generateApplicantSignatureDocument(requestContext, line, applicantName, ssn, address, municipality, nationality, phone, email);
onnistuuClient.addPdf(documentId, pdf);
applicationSignatures = applicationSignaturesDAO.updateApplicantDocument(applicationSignatures, documentId, null, null, ApplicationSignatureState.PDF_UPLOADED);
}
if (applicationSignatures.getApplicantDocumentState() == ApplicationSignatureState.PDF_UPLOADED) {
OnnistuuClient.Invitation invitation = onnistuuClient.createInvitation(documentId, email);
applicationSignatures = applicationSignaturesDAO.updateApplicantDocument(applicationSignatures, documentId, invitation.getUuid(), invitation.getPassphrase(), ApplicationSignatureState.INVITATION_CREATED);
}
// Construct accepted mail template
String staffDocUrl = String.format("https://www.onnistuu.fi/api/v1/invitation/%s/%s/files/0", applicationSignatures.getStaffInvitationId(), applicationSignatures.getStaffInvitationToken());
StringBuilder signUpUrl = new StringBuilder();
signUpUrl.append(requestContext.getRequest().getScheme());
signUpUrl.append("://");
signUpUrl.append(requestContext.getRequest().getServerName());
signUpUrl.append(":");
signUpUrl.append(requestContext.getRequest().getServerPort());
signUpUrl.append("/applications/accept.page?application=");
signUpUrl.append(application.getApplicationId());
String lineOrganization = ApplicationUtils.isOtaviaLine(application.getLine()) ? "Otavian" : "Otavan Opiston";
String signerOrganization = ApplicationUtils.isOtaviaLine(application.getLine()) ? "Otavia" : "Otavan Opisto";
String subject = String.format("Hyväksyminen %s opiskelijaksi", lineOrganization);
String content = IOUtils.toString(requestContext.getServletContext().getResourceAsStream("/templates/applications/mails/mail-accept-study-place.html"), "UTF-8");
content = String.format(content, nickname, lineOrganization, line.toLowerCase(), staffDocUrl, staffDocUrl, signUpUrl.toString(), signUpUrl.toString(), staffMember.getFullName(), signerOrganization);
if (StringUtils.isBlank(guardianMail)) {
Mailer.sendMail(Mailer.JNDI_APPLICATION, Mailer.HTML, null, email, subject, content);
} else {
Mailer.sendMail(Mailer.JNDI_APPLICATION, Mailer.HTML, null, email, guardianMail, subject, content);
}
// Add notification about sent mail
ApplicationLogDAO applicationLogDAO = DAOFactory.getInstance().getApplicationLogDAO();
applicationLogDAO.create(application, ApplicationLogType.HTML, String.format("<p>%s</p><p><b>%s</b></p>%s", "Hakijalle lähetetty ilmoitus opiskelijaksi hyväksymisestä", subject, content), staffMember);
} else // end of application has been approved logic
if (applicationState == ApplicationState.TRANSFERRED_AS_STUDENT) {
// Separate logic for transferring the applicant as student
// throws exception if multiple persons or is staff
Student student = ApplicationUtils.createPyramusStudent(application, staffMember);
PersonDAO personDAO = DAOFactory.getInstance().getPersonDAO();
personDAO.updateDefaultUser(student.getPerson(), student);
String credentialToken = RandomStringUtils.randomAlphanumeric(32).toLowerCase();
application = applicationDAO.updateApplicationStudentAndCredentialToken(application, student, credentialToken);
ApplicationUtils.mailCredentialsInfo(requestContext.getRequest(), student, application);
} else if (applicationState == ApplicationState.REJECTED) {
if (application.getState() == ApplicationState.REGISTERED_AS_STUDENT) {
Student student = application.getStudent();
StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
studentDAO.archive(student);
}
// #4226: Applications of rejected Internetix students are removed immediately
if (StringUtils.equals("aineopiskelu", application.getLine())) {
ApplicationUtils.deleteApplication(application);
requestContext.setRedirectURL(requestContext.getRequest().getContextPath() + "/applications/browse.page");
return;
}
} else if (applicationState == ApplicationState.PROCESSING) {
// #1216: If a signed application is returned to Processing state, remove the
// previous signatures so that the proper processing order can once again be followed
ApplicationSignaturesDAO applicationSignaturesDAO = DAOFactory.getInstance().getApplicationSignaturesDAO();
ApplicationSignatures applicationSignatures = applicationSignaturesDAO.findByApplication(application);
if (applicationSignatures != null) {
applicationSignaturesDAO.delete(applicationSignatures);
}
}
// Update the actual application state
application = applicationDAO.updateApplicationState(application, applicationState, staffMember);
if (Boolean.TRUE.equals(lockApplication) && application.getApplicantEditable()) {
application = applicationDAO.updateApplicantEditable(application, Boolean.FALSE, staffMember);
}
if (Boolean.TRUE.equals(setHandler)) {
application = applicationDAO.updateApplicationHandler(application, staffMember);
}
if (Boolean.TRUE.equals(removeHandler)) {
application = applicationDAO.updateApplicationHandler(application, null);
}
// Email notifications and log entries related to state change
ApplicationUtils.sendNotifications(application, requestContext.getRequest(), staffMember, false, null, true);
}
// Response parameters
requestContext.addResponseParameter("status", "OK");
requestContext.addResponseParameter("id", application.getId());
requestContext.addResponseParameter("state", application.getState());
requestContext.addResponseParameter("stateUi", ApplicationUtils.applicationStateUiValue(application.getState()));
requestContext.addResponseParameter("applicantEditable", application.getApplicantEditable());
requestContext.addResponseParameter("handler", application.getHandler() == null ? null : application.getHandler().getFullName());
requestContext.addResponseParameter("handlerId", application.getHandler() == null ? null : application.getHandler().getId());
requestContext.addResponseParameter("lastModified", application.getLastModified().getTime());
} catch (Exception e) {
requestContext.addResponseParameter("status", "FAIL");
requestContext.addResponseParameter("reason", e.getMessage());
logger.log(Level.SEVERE, "Error updating application state", e);
}
}
use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class UpdateLogEntryJSONRequestController 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 log entry due to staff member not found");
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
Long id = requestContext.getLong("id");
String text = requestContext.getRequest().getParameter("text");
if (id == null || text == null) {
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
ApplicationLogDAO applicationLogDAO = DAOFactory.getInstance().getApplicationLogDAO();
ApplicationLog applicationLog = applicationLogDAO.findById(id);
if (applicationLog != null) {
applicationLog = applicationLogDAO.update(applicationLog, text, staffMember);
requestContext.getResponse().setStatus(HttpServletResponse.SC_OK);
requestContext.addResponseParameter("id", applicationLog.getId());
requestContext.addResponseParameter("type", applicationLog.getType());
requestContext.addResponseParameter("text", applicationLog.getText());
requestContext.addResponseParameter("user", staffMember.getFullName());
requestContext.addResponseParameter("date", applicationLog.getDate().getTime());
} else {
requestContext.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Error saving log entry", e);
}
}
use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class SaveApplicationJSONRequestController 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 application due to staff member not found");
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String formDataStr = getFormData(requestContext.getRequest());
if (formDataStr == null) {
logger.log(Level.WARNING, "Refusing application due to missing form data");
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// Form validation
JSONObject formData = JSONObject.fromObject(formDataStr);
String applicationId = formData.getString("field-application-id");
if (applicationId == null) {
logger.log(Level.WARNING, "Refusing application due to missing applicationId");
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String line = formData.getString("field-line");
if (line == null) {
logger.log(Level.WARNING, "Refusing application due to missing line");
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String firstName = formData.getString("field-first-names");
if (firstName == null) {
logger.log(Level.WARNING, "Refusing application due to missing first name");
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String lastName = formData.getString("field-last-name");
if (lastName == null) {
logger.log(Level.WARNING, "Refusing application due to missing last name");
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String email = StringUtils.lowerCase(StringUtils.trim(formData.getString("field-email")));
if (StringUtils.isBlank(email)) {
logger.log(Level.WARNING, "Refusing application due to missing email");
requestContext.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (formData.has("attachment-name") && formData.has("attachment-description")) {
ApplicationAttachmentDAO applicationAttachmentDAO = DAOFactory.getInstance().getApplicationAttachmentDAO();
if (JSONUtils.isArray(formData.get("attachment-name"))) {
JSONArray attachmentNames = formData.getJSONArray("attachment-name");
JSONArray attachmentDescriptions = formData.getJSONArray("attachment-description");
for (int i = 0; i < attachmentNames.size(); i++) {
String name = attachmentNames.getString(i);
String description = attachmentDescriptions.getString(i);
ApplicationAttachment applicationAttachment = applicationAttachmentDAO.findByApplicationIdAndName(applicationId, name);
if (applicationAttachment == null) {
logger.warning(String.format("Attachment %s for application %s not found", name, applicationId));
} else {
applicationAttachmentDAO.updateDescription(applicationAttachment, description);
}
}
} else {
String name = formData.getString("attachment-name");
String description = formData.getString("attachment-description");
ApplicationAttachment applicationAttachment = applicationAttachmentDAO.findByApplicationIdAndName(applicationId, name);
if (applicationAttachment == null) {
logger.warning(String.format("Attachment %s for application %s not found", name, applicationId));
} else {
applicationAttachmentDAO.updateDescription(applicationAttachment, description);
}
}
}
// Save application
ApplicationDAO applicationDAO = DAOFactory.getInstance().getApplicationDAO();
Application application = applicationDAO.findByApplicationId(applicationId);
if (application == null) {
requestContext.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
boolean referenceCodeModified = !StringUtils.equalsIgnoreCase(application.getLastName(), lastName);
String oldSurname = referenceCodeModified ? application.getLastName() : lastName;
String referenceCode = referenceCodeModified ? ApplicationUtils.generateReferenceCode(lastName, application.getReferenceCode()) : application.getReferenceCode();
boolean lineChanged = !StringUtils.equals(line, application.getLine());
String oldLine = application.getLine();
application = applicationDAO.update(application, line, firstName, lastName, email, referenceCode, formDataStr, application.getState(), application.getApplicantEditable(), staffMember);
if (lineChanged) {
String notification = String.format("Hakemus vaihdettu linjalta <b>%s</b> linjalle <b>%s</b>", ApplicationUtils.applicationLineUiValue(oldLine), ApplicationUtils.applicationLineUiValue(line));
ApplicationLogDAO applicationLogDAO = DAOFactory.getInstance().getApplicationLogDAO();
applicationLogDAO.create(application, ApplicationLogType.HTML, notification, staffMember);
ApplicationUtils.sendNotifications(application, requestContext.getRequest(), staffMember, true, null, false);
}
if (referenceCodeModified) {
ApplicationUtils.sendApplicationModifiedMail(application, requestContext.getRequest(), oldSurname);
}
String redirecUrl = requestContext.getRequest().getContextPath() + "/applications/view.page?application=" + application.getId();
requestContext.setRedirectURL(redirecUrl);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error saving application", e);
}
}
use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class OnnistuuClient method generateStaffSignatureDocument.
public byte[] generateStaffSignatureDocument(RequestContext requestContext, String applicant, String line, StaffMember signer) throws OnnistuuClientException {
try {
HttpServletRequest httpRequest = requestContext.getRequest();
StringBuilder baseUrl = new StringBuilder();
baseUrl.append(httpRequest.getScheme());
baseUrl.append("://");
baseUrl.append(httpRequest.getServerName());
baseUrl.append(":");
baseUrl.append(httpRequest.getServerPort());
String documentPath = ApplicationUtils.isOtaviaLine(line) ? "/templates/applications/document-staff-signed-otavia.html" : "/templates/applications/document-staff-signed-otava.html";
// Staff signed document skeleton
String document = IOUtils.toString(requestContext.getServletContext().getResourceAsStream(documentPath), "UTF-8");
// Replace document date
document = StringUtils.replace(document, "[DOCUMENT-DATE]", new SimpleDateFormat("d.M.yyyy").format(new Date()));
// Replace applicant name
document = StringUtils.replace(document, "[DOCUMENT-APPLICANT]", applicant);
// Replace line specific welcome text
String welcomeText = IOUtils.toString(requestContext.getServletContext().getResourceAsStream(String.format("/templates/applications/document-acceptance-%s.html", line)), "UTF-8");
document = StringUtils.replace(document, "[DOCUMENT-TEXT]", welcomeText);
// Replace primary and (optional) secondary signers
Long primarySignerId = getPrimarySignerId(line);
if (primarySignerId == null) {
primarySignerId = signer.getId();
}
if (primarySignerId.equals(signer.getId())) {
document = StringUtils.replace(document, "[DOCUMENT-PRIMARY-SIGNER]", getSignature(signer, line));
document = StringUtils.replace(document, "[DOCUMENT-SECONDARY-SIGNER]", "");
} else {
StaffMemberDAO staffMemberDAO = DAOFactory.getInstance().getStaffMemberDAO();
StaffMember primarySigner = staffMemberDAO.findById(primarySignerId);
document = StringUtils.replace(document, "[DOCUMENT-PRIMARY-SIGNER]", getSignature(primarySigner, line));
document = StringUtils.replace(document, "[DOCUMENT-SECONDARY-SIGNER]", "<p>Puolesta</p>" + getSignature(signer, line));
}
// Convert to PDF
ByteArrayOutputStream out = new ByteArrayOutputStream();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(document, baseUrl.toString());
renderer.layout();
renderer.createPDF(out);
return out.toByteArray();
} catch (Exception e) {
logger.log(Level.SEVERE, "Unable to create staff document", e);
throw new OnnistuuClientException(e.getMessage(), e);
}
}
use of fi.otavanopisto.pyramus.domainmodel.users.StaffMember in project pyramus by otavanopisto.
the class CoursesService method createCourseUser.
public CourseUserEntity createCourseUser(@WebParam(name = "courseId") Long courseId, @WebParam(name = "userId") Long userId, @WebParam(name = "courseUserRoleId") Long courseUserRoleId) {
StaffMemberDAO staffMemberDAO = DAOFactory.getInstance().getStaffMemberDAO();
CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
CourseStaffMemberDAO courseStaffMemberDAO = DAOFactory.getInstance().getCourseStaffMemberDAO();
CourseStaffMemberRoleDAO courseStaffMemberRoleDAO = DAOFactory.getInstance().getCourseStaffMemberRoleDAO();
Course course = courseDAO.findById(courseId);
StaffMember staffMember = staffMemberDAO.findById(userId);
CourseStaffMemberRole role = courseStaffMemberRoleDAO.findById(courseUserRoleId);
CourseStaffMember courseUser = courseStaffMemberDAO.create(course, staffMember, role);
validateEntity(courseUser);
return EntityFactoryVault.buildFromDomainObject(courseUser);
}
Aggregations