Search in sources :

Example 6 with EmailerBean

use of org.jaffa.util.EmailerBean in project jaffa-framework by jaffa-projects.

the class UserMaintenanceTx method updateDomain.

// .//GEN-END:_loadUpdate_3_be
// .//GEN-BEGIN:_updateDomain_1_be
/**
 * Update the domain object and add it to the UOW.
 */
private void updateDomain(UOW uow, UserMaintenanceUpdateInDto input, User domain, boolean fromPrevalidate) throws FrameworkException, ApplicationExceptions {
    ApplicationExceptions appExps = null;
    // .//GEN-BEGIN:_updateDomain_2_be
    try {
        domain.updateFirstName(input.getFirstName());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateLastName(input.getLastName());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateStatus(input.getStatus());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateEMailAddress(input.getEMailAddress());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateSecurityQuestion(input.getSecurityQuestion());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateSecurityAnswer(input.getSecurityAnswer());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        if (!fromPrevalidate)
            domain.updateLastUpdatedOn(new DateTime());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        if (!fromPrevalidate && input.getHeaderDto() != null && input.getHeaderDto().getUserId() != null)
            domain.updateLastUpdatedBy(input.getHeaderDto().getUserId());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    // Add custom code //GEN-FIRST:_updateDomain_2
    try {
        if (input.getPassword1() != null)
            domain.updatePassword(input.getPassword1());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    String[] userRole = null;
    if (appExps == null || appExps.size() == 0) {
        // Build an array of roles
        userRole = new String[input.getUserRoleCount()];
        UserRoleDto[] userRoleDtos = input.getUserRole();
        for (int i = 0; i < userRoleDtos.length; i++) userRole[i] = userRoleDtos[i].getRoleName();
        Arrays.sort(userRole);
        performUserRoleValidations(userRole, domain);
    }
    if (!fromPrevalidate) {
        // These keep track of whats added and removed
        Collection rolesAdded = new ArrayList();
        Collection rolesRemoved = new ArrayList();
        Collection processed = new ArrayList();
        if (appExps == null || appExps.size() == 0) {
            // Roles can be updated only if the user has access to the maintenance function
            if (hasMaintenanceAccess()) {
                // Get the current roles
                Criteria crit = new Criteria();
                crit.setTable(UserRoleMeta.getName());
                crit.addCriteria(UserRoleMeta.USER_NAME, input.getUserName());
                Collection currRoles = uow.query(crit);
                // if in database, add to the processed list
                for (Iterator it = currRoles.iterator(); it.hasNext(); ) {
                    UserRole ur = (UserRole) it.next();
                    if (Arrays.binarySearch(userRole, ur.getRoleName()) >= 0) {
                        processed.add(ur.getRoleName());
                        if (log.isDebugEnabled())
                            log.debug("Keeping Role - " + ur.getRoleName());
                    } else {
                        rolesRemoved.add(ur.getRoleName());
                        uow.delete(ur);
                        if (log.isDebugEnabled())
                            log.debug("Deleting Role - " + ur.getRoleName());
                    }
                }
                // Now add the new ones
                for (int i = 0; i < userRole.length; i++) {
                    String rolename = userRole[i];
                    if (!processed.contains(rolename)) {
                        try {
                            UserRole ur = new UserRole();
                            ur.updateRoleName(rolename);
                            ur.updateUserName(input.getUserName());
                            rolesAdded.add(ur.getRoleName());
                            uow.add(ur);
                            if (log.isDebugEnabled())
                                log.debug("Adding Role - " + ur.getRoleName());
                        } catch (ValidationException e) {
                            if (appExps == null)
                                appExps = new ApplicationExceptions();
                            appExps.add(e);
                        }
                    }
                }
            }
        }
    }
    if (input.getNotifyUser().booleanValue()) {
        try {
            EmailerBean email = new EmailerBean();
            String[] to = new String[] { domain.getEMailAddress() };
            StringBuffer body = new StringBuffer();
            if ((oldPassword != null && !oldPassword.equals(domain.getPassword())) || (oldStatus != null && !oldStatus.equals(domain.getStatus()))) {
                body.append("Your UserName is " + domain.getUserName());
                if (oldPassword != null && !oldPassword.equals(domain.getPassword()))
                    body.append(" and your password is " + domain.getPassword() + ".");
                if (oldStatus != null && !oldStatus.equals(domain.getStatus()))
                    body.append("Your account is currently " + domain.getStatus() + ".");
                email.sendMail(to, "Account Information", body.toString());
            }
        } catch (javax.mail.MessagingException e) {
            e.printStackTrace();
        }
    }
    /*     try {
            UserRequest userRequest = UserRequest.findByPK(uow , new Long(input.getRequestId()));
            userRequest.setStatus("S");
            uow.update(userRequest);


        } catch (ValidationException e) {
            if (appExps == null)
                appExps = new ApplicationExceptions();
            appExps.add(e);
        }
*/
    if (appExps != null && appExps.size() > 0)
        throw appExps;
    // .//GEN-BEGIN:_updateDomain_3_be
    if (appExps != null && appExps.size() > 0)
        throw appExps;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ValidationException(org.jaffa.datatypes.ValidationException) EmailerBean(org.jaffa.util.EmailerBean) Criteria(org.jaffa.persistence.Criteria) DateTime(org.jaffa.datatypes.DateTime) UserRole(org.jaffa.applications.jaffa.modules.admin.domain.UserRole)

Example 7 with EmailerBean

use of org.jaffa.util.EmailerBean in project jaffa-framework by jaffa-projects.

the class UserMaintenanceTx method postCreate.

// .//GEN-END:_createDomain_3_be
// .//GEN-BEGIN:_postCreate_1_be
/**
 * This method is invoked after the domain object has been created.
 */
private User postCreate(UOW uow, UserMaintenanceCreateInDto input, User domain, boolean fromPrevalidate) throws FrameworkException, ApplicationExceptions {
    // .//GEN-END:_postCreate_1_be
    // Add custom code //GEN-FIRST:_postCreate_1
    ApplicationExceptions appExps = null;
    String[] userRole = null;
    // Build an array of roles
    userRole = new String[input.getUserRoleCount()];
    UserRoleDto[] userRoleDtos = input.getUserRole();
    for (int i = 0; i < userRoleDtos.length; i++) userRole[i] = userRoleDtos[i].getRoleName();
    Arrays.sort(userRole);
    performUserRoleValidations(userRole, domain);
    if (!fromPrevalidate) {
        // add the the roles
        for (int i = 0; i < userRole.length; i++) {
            try {
                UserRole ur = new UserRole();
                ur.updateRoleName(userRole[i]);
                ur.updateUserName(input.getUserName());
                uow.add(ur);
            } catch (ValidationException e) {
                if (appExps == null)
                    appExps = new ApplicationExceptions();
                appExps.add(e);
            }
        }
    }
    if (input.getNotifyUser().booleanValue()) {
        try {
            EmailerBean email = new EmailerBean();
            String[] to = new String[] { domain.getEMailAddress() };
            String body = "Your UserName is " + domain.getUserName() + " and your password is " + domain.getPassword() + ". Your account is currently " + domain.getStatus() + ".";
            email.sendMail(to, "Account Information", body);
        } catch (javax.mail.MessagingException e) {
            e.printStackTrace();
        }
    }
    if (input.getRequestId() != null && input.getRequestId().length() > 0) {
        try {
            UserRequest userRequest = UserRequest.findByPK(uow, new Long(input.getRequestId()));
            userRequest.setStatus("S");
            uow.update(userRequest);
        } catch (ValidationException e) {
            if (appExps == null)
                appExps = new ApplicationExceptions();
            appExps.add(e);
        }
    }
    if (appExps != null && appExps.size() > 0)
        throw appExps;
    // .//GEN-BEGIN:_postCreate_2_be
    return domain;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ValidationException(org.jaffa.datatypes.ValidationException) EmailerBean(org.jaffa.util.EmailerBean) UserRole(org.jaffa.applications.jaffa.modules.admin.domain.UserRole) UserRequest(org.jaffa.applications.jaffa.modules.user.domain.UserRequest)

Example 8 with EmailerBean

use of org.jaffa.util.EmailerBean in project jaffa-framework by jaffa-projects.

the class FormDelivery method sendEmail.

/**
 * Email out the generated document as an attachment
 * @param request Print Request containing possible email info
 * @param document document to put as the email attachment
 * @param dom A context object that can be used to extract values when generating e-mail
 * subject and body information. This object should be a java bean or a map
 * @throws ApplicationExceptions Thrown if any funtional issue ocurred when executing
 * @throws FrameworkException Thrown if any framework issue ocurred when executing
 */
public static void sendEmail(PrintRequest request, File document, Object dom) throws FrameworkException, ApplicationExceptions {
    String documentName = null;
    try {
        // Set up parameters
        Object[] params = null;
        if (request instanceof FormPrintRequest) {
            FormPrintRequest fpr = (FormPrintRequest) request;
            params = new Object[] { fpr.getFormName(), fpr.getFormAlternateName(), fpr.getVariation() };
            documentName = fpr.getFormName();
        } else {
            // Just use the short class name
            documentName = StringHelper.getShortClassName(request.getClass());
        }
        String fromAddress = request.getEmailFromAddress();
        // Use the resource bundle to get subject and body of this message
        String subject = request.getEmailSubject();
        if (subject == null) {
            String token = "label.Jaffa.Printing.Email." + documentName + ".subject";
            subject = MessageHelper.findMessage(token, params);
        }
        String body = request.getEmailMessage();
        if (body == null) {
            String token = "label.Jaffa.Printing.Email." + documentName + ".body";
            body = MessageHelper.findMessage(token, params);
        }
        // @TODO Pass the databean DOM in and use velocity templating to to token replacement of
        // the subject and body for better messages. Expose request and dom to the template
        EmailerBean eb = new EmailerBean();
        eb.sendMail(fromAddress, StringHelper.parseString(request.getEmailToAddresses(), ";"), subject, body, new File[] { document });
    } catch (MessagingException e) {
        log.error("Failed to send email with attachment " + document.getAbsolutePath(), e);
        throw new ApplicationExceptions(new EmailFailedException(documentName));
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) MessagingException(javax.mail.MessagingException) EmailerBean(org.jaffa.util.EmailerBean) EmailFailedException(org.jaffa.modules.printing.services.exceptions.EmailFailedException)

Example 9 with EmailerBean

use of org.jaffa.util.EmailerBean in project jaffa-framework by jaffa-projects.

the class JaffaMessageConsumer method sendFailureNotification.

/**
 * Constructs an email with error information, provided the application rule
 * 'jaffa.messaging.failureNotification.{queuename}' is defined.
 */
private void sendFailureNotification(Message aMessage, Object payload, Exception e) throws JMSException, MessagingException {
    String queueName = aMessage.getStringProperty(JmsBrowser.HEADER_ORIGINAL_QUEUE_NAME);
    String ruleName = RULE_FAILURE_NOTIFICATION_PREFIX + queueName;
    String ruleValue = (String) ContextManagerFactory.instance().getProperty(ruleName);
    if (ruleValue != null && ruleValue.length() > 0) {
        // the rule value is expected to be a semicolon-separated list of
        // email-addresses
        String[] to = ruleValue.split(";");
        String token = EMAIL_SUBJECT_NOTIFICATION + '.' + payload.getClass().getName();
        // obtain the template for the subject
        String subject = MessageHelper.findMessage(token, null);
        if (subject == null || subject.length() == 0 || subject.startsWith("???")) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Entry '" + token + "' not found in the resource bundle for generating the subject of the failure notification. Will use the generic subject.");
            subject = MessageHelper.findMessage(EMAIL_SUBJECT_NOTIFICATION, null);
        }
        // obtain the template for the body
        token = EMAIL_BODY_NOTIFICATION + '.' + payload.getClass().getName();
        String body = MessageHelper.findMessage(token, null);
        if (body == null || body.length() == 0 || body.startsWith("???")) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Entry '" + token + "' not found in the resource bundle for generating the body of the failure notification. Will use the generic body.");
            body = MessageHelper.findMessage(EMAIL_BODY_NOTIFICATION, null);
        }
        // create the context for the template
        final Map<String, Object> context = new HashMap<String, Object>();
        context.put("message", aMessage);
        context.put("bean", payload);
        context.put("exception", e);
        context.put("queue", queueName);
        context.put("errorMessage", aMessage.getStringProperty(JmsBrowser.HEADER_ERROR_DETAILS));
        context.put("context", ContextManagerFactory.instance().getThreadContext());
        context.put("appUrl", ContextManagerFactory.instance().getProperty("app.url"));
        // transform the stacktrace into an attachment
        final BodyPart attachment = new MimeBodyPart();
        attachment.setContent(ExceptionHelper.getStackTrace(e), "text/plain");
        attachment.setFileName("stacktrace.txt");
        // send the failure notification
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Sending failure notification to " + ruleValue);
        final EmailerBean eb = new EmailerBean();
        eb.sendMailTemplate(null, to, subject, body, context, new BodyPart[] { attachment });
    } else {
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Failure Notification will not be sent since rule " + ruleName + " is undefined");
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) HashMap(java.util.HashMap) EmailerBean(org.jaffa.util.EmailerBean) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

EmailerBean (org.jaffa.util.EmailerBean)9 Criteria (org.jaffa.persistence.Criteria)5 MessagingException (javax.mail.MessagingException)3 DateTime (org.jaffa.datatypes.DateTime)3 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)3 HashMap (java.util.HashMap)2 BodyPart (javax.mail.BodyPart)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 UserRole (org.jaffa.applications.jaffa.modules.admin.domain.UserRole)2 UserRequest (org.jaffa.applications.jaffa.modules.user.domain.UserRequest)2 ValidationException (org.jaffa.datatypes.ValidationException)2 ApplicationException (org.jaffa.exceptions.ApplicationException)2 Map (java.util.Map)1 User (org.jaffa.applications.jaffa.modules.admin.domain.User)1 EmailFailedException (org.jaffa.modules.printing.services.exceptions.EmailFailedException)1 UOW (org.jaffa.persistence.UOW)1 UOWException (org.jaffa.persistence.exceptions.UOWException)1