use of fi.otavanopisto.pyramus.dao.application.ApplicationDAO in project pyramus by otavanopisto.
the class CreateCredentialsJSONRequestController method process.
public void process(JSONRequestContext requestContext) {
// Validation of request data and basic necessities
String applicationId = StringUtils.trim(requestContext.getString("applicationId"));
String credentialToken = StringUtils.trim(requestContext.getString("token"));
String username = StringUtils.trim(requestContext.getString("username"));
String password = StringUtils.trim(requestContext.getString("password"));
List<InternalAuthenticationProvider> providers = AuthenticationProviderVault.getInstance().getInternalAuthenticationProviders();
InternalAuthenticationProvider provider = providers.size() == 1 ? providers.get(0) : null;
if (provider == null || !provider.canUpdateCredentials() || StringUtils.isAnyBlank(applicationId, username, password, credentialToken)) {
fail(requestContext, "Sisäinen virhe");
return;
}
// Validate application
ApplicationDAO applicationDAO = DAOFactory.getInstance().getApplicationDAO();
Application application = applicationDAO.findByApplicationId(applicationId);
if (application == null || application.getStudent() == null || !StringUtils.equals(credentialToken, application.getCredentialToken())) {
fail(requestContext, "Hakemus ei mahdollista tunnusten luontia");
return;
}
// Validate student
Person person = application.getStudent().getPerson();
InternalAuthDAO internalAuthDAO = DAOFactory.getInstance().getInternalAuthDAO();
UserIdentificationDAO userIdentificationDAO = DAOFactory.getInstance().getUserIdentificationDAO();
UserIdentification userIdentification = userIdentificationDAO.findByAuthSourceAndPerson(provider.getName(), person);
if (userIdentification != null) {
fail(requestContext, "Käyttäjätilillä on jo tunnukset");
return;
}
InternalAuth internalAuth = internalAuthDAO.findByUsername(username);
if (internalAuth != null) {
fail(requestContext, "Valittu käyttäjätunnus on jo varattu");
return;
}
String externalId = provider.createCredentials(username, password);
userIdentificationDAO.create(person, provider.getName(), externalId);
requestContext.addResponseParameter("status", "OK");
}
use of fi.otavanopisto.pyramus.dao.application.ApplicationDAO 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());
}
}
use of fi.otavanopisto.pyramus.dao.application.ApplicationDAO in project pyramus by otavanopisto.
the class GetDocumentUrlsJSONRequestController method process.
public void process(JSONRequestContext requestContext) {
Long id = requestContext.getLong("id");
if (id != null) {
ApplicationDAO applicationDAO = DAOFactory.getInstance().getApplicationDAO();
Application application = applicationDAO.findById(id);
if (application != null) {
ApplicationSignaturesDAO applicationSignaturesDAO = DAOFactory.getInstance().getApplicationSignaturesDAO();
ApplicationSignatures applicationSignatures = applicationSignaturesDAO.findByApplication(application);
if (applicationSignatures != null) {
if (StringUtils.isNotBlank(applicationSignatures.getStaffInvitationId())) {
String staffDocumentUrl = String.format("https://www.onnistuu.fi/api/v1/invitation/%s/%s/files/0", applicationSignatures.getStaffInvitationId(), applicationSignatures.getStaffInvitationToken());
requestContext.addResponseParameter("staffDocumentUrl", staffDocumentUrl);
}
if (StringUtils.isNotBlank(applicationSignatures.getApplicantInvitationId())) {
String applicantDocumentUrl = String.format("https://www.onnistuu.fi/api/v1/invitation/%s/%s/files/0", applicationSignatures.getApplicantInvitationId(), applicationSignatures.getApplicantInvitationToken());
requestContext.addResponseParameter("applicantDocumentUrl", applicantDocumentUrl);
}
}
}
}
}
use of fi.otavanopisto.pyramus.dao.application.ApplicationDAO 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.dao.application.ApplicationDAO 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);
}
}
Aggregations