Search in sources :

Example 1 with ApplicationSignatures

use of fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures 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());
    }
}
Also used : ApplicationSignatures(fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember) ApplicationDAO(fi.otavanopisto.pyramus.dao.application.ApplicationDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) JSONObject(net.sf.json.JSONObject) ApplicationSignaturesDAO(fi.otavanopisto.pyramus.dao.application.ApplicationSignaturesDAO) Application(fi.otavanopisto.pyramus.domainmodel.application.Application)

Example 2 with ApplicationSignatures

use of fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures 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);
                }
            }
        }
    }
}
Also used : ApplicationSignaturesDAO(fi.otavanopisto.pyramus.dao.application.ApplicationSignaturesDAO) ApplicationSignatures(fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures) ApplicationDAO(fi.otavanopisto.pyramus.dao.application.ApplicationDAO) Application(fi.otavanopisto.pyramus.domainmodel.application.Application)

Example 3 with ApplicationSignatures

use of fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures 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);
    }
}
Also used : ApplicationLogDAO(fi.otavanopisto.pyramus.dao.application.ApplicationLogDAO) ApplicationState(fi.otavanopisto.pyramus.domainmodel.application.ApplicationState) ApplicationSignatures(fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember) ApplicationDAO(fi.otavanopisto.pyramus.dao.application.ApplicationDAO) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) StudentDAO(fi.otavanopisto.pyramus.dao.students.StudentDAO) PersonDAO(fi.otavanopisto.pyramus.dao.base.PersonDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) JSONObject(net.sf.json.JSONObject) ApplicationSignaturesDAO(fi.otavanopisto.pyramus.dao.application.ApplicationSignaturesDAO) Application(fi.otavanopisto.pyramus.domainmodel.application.Application)

Example 4 with ApplicationSignatures

use of fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures in project pyramus by otavanopisto.

the class SignStudentDocumentJSONRequestController method process.

public void process(JSONRequestContext requestContext) {
    // Validate request parameters
    String applicationId = requestContext.getString("id");
    if (StringUtils.isBlank(applicationId)) {
        logger.warning("Missing application id");
        fail(requestContext, "Puuttuva hakemustunnus");
        return;
    }
    String ssn = requestContext.getString("ssn");
    if (StringUtils.isBlank(ssn)) {
        logger.warning("Missing social security number");
        fail(requestContext, "Puuttuva henkilötunnus");
        return;
    }
    String authService = requestContext.getString("authService");
    if (StringUtils.isBlank(authService)) {
        logger.warning("Missing authService");
        fail(requestContext, "Puuttuva tunnistustapa");
        return;
    }
    // Ensure application state
    ApplicationDAO applicationDAO = DAOFactory.getInstance().getApplicationDAO();
    Application application = applicationDAO.findByApplicationIdAndArchived(applicationId, Boolean.FALSE);
    if (application == null) {
        logger.warning(String.format("Application with id %s not found", applicationId));
        fail(requestContext, String.format("Hakemusta tunnuksella %s ei löytynyt", applicationId));
        return;
    }
    if (application.getState() != ApplicationState.APPROVED_BY_SCHOOL) {
        logger.warning(String.format("Application with id %s in incorrect state (%s)", applicationId, application.getState()));
        fail(requestContext, "Hakemus ei ole allekirjoitettavassa tilassa");
        return;
    }
    if (!StringUtils.equals(ssn, ApplicationUtils.extractSSN(application))) {
        logger.warning("Social security number mismatch");
        fail(requestContext, "Virheellinen henkilötunnus");
        return;
    }
    // Signatures tracking
    ApplicationSignaturesDAO applicationSignaturesDAO = DAOFactory.getInstance().getApplicationSignaturesDAO();
    ApplicationSignatures signatures = applicationSignaturesDAO.findByApplication(application);
    if (signatures == null || signatures.getApplicantDocumentState() != ApplicationSignatureState.INVITATION_CREATED) {
        fail(requestContext, "Opiskelupaikan vastaanottoasiakirja ei ole allekirjoitettavassa tilassa");
        return;
    }
    // Return URL after signing
    StringBuilder returnUrl = new StringBuilder();
    returnUrl.append(requestContext.getRequest().getScheme());
    returnUrl.append("://");
    returnUrl.append(requestContext.getRequest().getServerName());
    returnUrl.append(":");
    returnUrl.append(requestContext.getRequest().getServerPort());
    returnUrl.append("/applications/done.page?invitationId=");
    returnUrl.append(signatures.getApplicantInvitationId());
    // Signing
    OnnistuuClient onnistuuClient = OnnistuuClient.getInstance();
    try {
        String completionUrl = onnistuuClient.getSignatureUrl(signatures.getApplicantInvitationId(), returnUrl.toString(), ssn, authService);
        // Respond with URL to complete the signature
        requestContext.addResponseParameter("status", "OK");
        requestContext.addResponseParameter("completionUrl", completionUrl);
    } catch (OnnistuuClientException e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
        fail(requestContext, e.getMessage());
    }
}
Also used : ApplicationSignaturesDAO(fi.otavanopisto.pyramus.dao.application.ApplicationSignaturesDAO) ApplicationSignatures(fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures) ApplicationDAO(fi.otavanopisto.pyramus.dao.application.ApplicationDAO) Application(fi.otavanopisto.pyramus.domainmodel.application.Application)

Example 5 with ApplicationSignatures

use of fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures in project pyramus by otavanopisto.

the class ApplicationSignaturesDAO method listByApplication.

public List<ApplicationSignatures> listByApplication(Application application) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ApplicationSignatures> criteria = criteriaBuilder.createQuery(ApplicationSignatures.class);
    Root<ApplicationSignatures> root = criteria.from(ApplicationSignatures.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(ApplicationSignatures_.application), application));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ApplicationSignatures(fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures)

Aggregations

ApplicationSignatures (fi.otavanopisto.pyramus.domainmodel.application.ApplicationSignatures)16 ApplicationDAO (fi.otavanopisto.pyramus.dao.application.ApplicationDAO)11 ApplicationSignaturesDAO (fi.otavanopisto.pyramus.dao.application.ApplicationSignaturesDAO)11 Application (fi.otavanopisto.pyramus.domainmodel.application.Application)10 StaffMemberDAO (fi.otavanopisto.pyramus.dao.users.StaffMemberDAO)6 StaffMember (fi.otavanopisto.pyramus.domainmodel.users.StaffMember)6 EntityManager (javax.persistence.EntityManager)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 JSONObject (net.sf.json.JSONObject)4 IOException (java.io.IOException)3 ApplicationLogDAO (fi.otavanopisto.pyramus.dao.application.ApplicationLogDAO)2 OnnistuuClient (fi.otavanopisto.pyramus.json.applications.OnnistuuClient)2 AlternativeLine (fi.otavanopisto.pyramus.applications.AlternativeLine)1 ApplicationAttachmentDAO (fi.otavanopisto.pyramus.dao.application.ApplicationAttachmentDAO)1 PersonDAO (fi.otavanopisto.pyramus.dao.base.PersonDAO)1 StudentDAO (fi.otavanopisto.pyramus.dao.students.StudentDAO)1 ApplicationAttachment (fi.otavanopisto.pyramus.domainmodel.application.ApplicationAttachment)1 ApplicationLog (fi.otavanopisto.pyramus.domainmodel.application.ApplicationLog)1 ApplicationState (fi.otavanopisto.pyramus.domainmodel.application.ApplicationState)1 School (fi.otavanopisto.pyramus.domainmodel.base.School)1