Search in sources :

Example 1 with EmailerBean

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

the class TransactionEngine method sendFailureNotification.

/**
 * Constructs an email with error information, provided the application
 * rule 'jaffa.transaction.failureNotification.{type}.{subType}' is defined.
 */
public static void sendFailureNotification(Transaction transaction, Exception exception) throws MessagingException, FrameworkException {
    // Determine the recipients from an appropriate application-rule
    // The recipients string is expected to be a semicolon-separated list of email-addresses
    String type = transaction.getType();
    String subType = transaction.getSubType();
    String recipients = null;
    if (type != null && subType != null) {
        recipients = (String) ContextManagerFactory.instance().getProperty(RULE_FAILURE_NOTIFICATION_PREFIX + '.' + type + '.' + subType);
    }
    if ((recipients == null || recipients.length() == 0) && type != null) {
        recipients = (String) ContextManagerFactory.instance().getProperty(RULE_FAILURE_NOTIFICATION_PREFIX + '.' + type);
    }
    if (recipients == null || recipients.length() == 0) {
        recipients = (String) ContextManagerFactory.instance().getProperty(RULE_FAILURE_NOTIFICATION_PREFIX);
    }
    if (recipients != null && recipients.length() > 0) {
        String[] to = recipients.split(";");
        Object dataBean = transaction.getTransactionPayloadObject() != null ? transaction.getTransactionPayloadObject().moldInternalPayload() : null;
        // Obtain the template for the subject
        String subject = null;
        if (dataBean != null) {
            subject = MessageHelper.findMessage(EMAIL_SUBJECT_NOTIFICATION + '.' + dataBean.getClass().getName(), null);
        }
        if (subject == null || subject.length() == 0 || subject.startsWith("???")) {
            subject = MessageHelper.findMessage(EMAIL_SUBJECT_NOTIFICATION, null);
        }
        // Obtain the template for the body
        String body = null;
        if (dataBean != null) {
            body = MessageHelper.findMessage(EMAIL_BODY_NOTIFICATION + '.' + dataBean.getClass().getName(), null);
        }
        if (body == null || body.length() == 0 || body.startsWith("???")) {
            body = MessageHelper.findMessage(EMAIL_BODY_NOTIFICATION, null);
        }
        // Create the context for the template
        Map context = new HashMap();
        context.put("transaction", transaction);
        context.put("bean", dataBean);
        context.put("exception", exception);
        context.put("type", type);
        context.put("subType", subType);
        context.put("errorMessage", ExceptionHelper.extractErrorMessage(exception));
        context.put("context", ContextManagerFactory.instance().getThreadContext());
        context.put("appUrl", ContextManagerFactory.instance().getProperty("app.url"));
        // transform the stacktrace into an attachment
        BodyPart attachment = new MimeBodyPart();
        attachment.setContent(ExceptionHelper.getStackTrace(exception), "text/plain");
        attachment.setFileName("stacktrace.txt");
        // send the failure notification
        if (log.isDebugEnabled()) {
            log.debug("Sending failure notification to " + recipients);
        }
        EmailerBean eb = new EmailerBean();
        eb.sendMailTemplate(null, to, subject, body, context, new BodyPart[] { attachment });
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Failure Notification will not be sent since rule '" + RULE_FAILURE_NOTIFICATION_PREFIX + "' 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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with EmailerBean

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

the class TestConsumer method invoke.

public void invoke(UOW uow, TestMessageSingleton message) throws Exception {
    if (log.isDebugEnabled()) {
        log.debug("Handling message " + message);
    }
    // Send an Email
    if (message.getEmailAddress() != null) {
        new EmailerBean().sendMail(new String[] { message.getEmailAddress() }, SUBJECT, "Jaffa Transaction Tester message - Time: " + new DateTime());
    }
    // Pause
    Thread.sleep(message.getDelay());
    // Perform DBQuery
    if (message.getDbQuery() > 0) {
        long count = message.getDbQuery();
        Criteria c = new Criteria();
        c.setTable(SOAEventMeta.getName());
        for (int p = 0; p < count; p++) {
            uow.query(c);
        }
    }
    // Fail the transaction
    if (message.isFail())
        throw new ApplicationException("jaffa.tester.TestConsumer.fail");
}
Also used : ApplicationException(org.jaffa.exceptions.ApplicationException) EmailerBean(org.jaffa.util.EmailerBean) Criteria(org.jaffa.persistence.Criteria) DateTime(org.jaffa.datatypes.DateTime)

Example 3 with EmailerBean

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

the class TestConsumer method invoke.

public void invoke(UOW uow, TestMessage message) throws Exception {
    if (log.isDebugEnabled()) {
        log.debug("Handling message " + message);
    }
    // Send an Email
    if (message.getEmailAddress() != null) {
        new EmailerBean().sendMail(new String[] { message.getEmailAddress() }, SUBJECT, "Jaffa Transaction Tester message - Time: " + new DateTime());
    }
    // Pause
    Thread.sleep(message.getDelay());
    // Perform DBQuery
    if (message.getDbQuery() > 0) {
        long count = message.getDbQuery();
        Criteria c = new Criteria();
        c.setTable(SOAEventMeta.getName());
        for (int p = 0; p < count; p++) {
            uow.query(c);
        }
    }
    // Fail the transaction
    if (message.isFail())
        throw new ApplicationException("jaffa.tester.TestConsumer.fail");
}
Also used : ApplicationException(org.jaffa.exceptions.ApplicationException) EmailerBean(org.jaffa.util.EmailerBean) Criteria(org.jaffa.persistence.Criteria) DateTime(org.jaffa.datatypes.DateTime)

Example 4 with EmailerBean

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

the class RequestPasswordComponent method sendEmail.

public void sendEmail() {
    Criteria criteria = new Criteria();
    criteria.setTable(UserMeta.getName());
    if (this.getUserName() != null)
        criteria.addCriteria(UserMeta.USER_NAME, this.getUserName());
    if (this.getEmail() != null)
        criteria.addCriteria(UserMeta.E_MAIL_ADDRESS, this.getEmail());
    if (!this.getSecurityQuestion().equalsIgnoreCase("None"))
        criteria.addCriteria(UserMeta.SECURITY_QUESTION, this.getSecurityQuestion());
    if (this.getSecurityAnswer() != null)
        criteria.addCriteria(UserMeta.SECURITY_ANSWER, this.getSecurityAnswer());
    criteria.addCriteria(UserMeta.STATUS, "A");
    try {
        UOW uow = new UOW();
        if (!uow.query(criteria).isEmpty()) {
            StringBuffer body = new StringBuffer();
            if (uow.query(criteria).size() > 1) {
                body.append("You have " + uow.query(criteria).size() + " Active Accounts");
                int counter = 1;
                for (Iterator itr = uow.query(criteria).iterator(); itr.hasNext(); ) {
                    User ur = (User) itr.next();
                    body.append(", Account " + counter++ + ", User Name is " + ur.getUserName() + ", Security Question is '" + MessageHelper.replaceTokens("[label.Jaffa.User.UserRequest.SecurityQuestion." + ur.getSecurityQuestion() + "]") + "'");
                }
            } else {
                User ur = (User) uow.query(criteria).iterator().next();
                if (this.getUserName() != null) {
                    body.append("You have the following Active Account  ");
                    body.append(", User Name is " + ur.getUserName() + ", Password is " + ur.getPassword() + ".");
                } else if (this.getSecurityQuestion() != null && new Long(this.getSecurityQuestion()).equals(ur.getSecurityQuestion()) && (this.getSecurityAnswer().equals(ur.getSecurityAnswer()))) {
                    body.append("You have the following Active Account  ");
                    body.append(", User Name is " + ur.getUserName() + ", Password is " + ur.getPassword() + ".");
                } else {
                    body.append("You have the following Active Account");
                    body.append(", User Name is " + ur.getUserName() + ", Security Question is '" + MessageHelper.replaceTokens("[label.Jaffa.User.UserRequest.SecurityQuestion." + ur.getSecurityQuestion() + "]") + "'.");
                }
            }
            EmailerBean email = new EmailerBean();
            String[] to = new String[] { this.getEmail() };
            try {
                email.sendMail(to, "Account Information", body.toString());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    } catch (UOWException e) {
        e.printStackTrace();
    }
}
Also used : User(org.jaffa.applications.jaffa.modules.admin.domain.User) UOWException(org.jaffa.persistence.exceptions.UOWException) MessagingException(javax.mail.MessagingException) EmailerBean(org.jaffa.util.EmailerBean) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Example 5 with EmailerBean

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

the class UserRequestMaintenanceTx method postCreate.

// .//GEN-END:_createDomain_3_be
// .//GEN-BEGIN:_postCreate_1_be
/**
 * This method is invoked after the domain object has been created.
 */
private void postCreate(UOW uow, UserRequestMaintenanceCreateInDto input, UserRequest domain, boolean fromPrevalidate) throws FrameworkException, ApplicationExceptions {
    // .//GEN-END:_postCreate_1_be
    // Add custom code //GEN-FIRST:_postCreate_1
    EmailerBean email = new EmailerBean();
    UserRequest domainCreated = new UserRequest();
    Criteria criteria = new Criteria();
    criteria.setTable(UserRequestMeta.getName());
    log.debug("This is the dto's final value" + input.getUrl());
    criteria.addCriteria(UserRequestMeta.USER_NAME, domain.getUserName());
    criteria.addCriteria(UserRequestMeta.FIRST_NAME, domain.getFirstName());
    criteria.addCriteria(UserRequestMeta.LAST_NAME, domain.getLastName());
    criteria.addCriteria(UserRequestMeta.CREATED_ON, domain.getCreatedOn());
    Iterator itr = uow.query(criteria).iterator();
    if (itr.hasNext())
        domainCreated = (UserRequest) itr.next();
    if (domainCreated != null) {
        try {
            String url = input.getUrl() + domainCreated.getRequestId();
            url = replaceAll(url, "Jaffa.User", "Jaffa.Admin");
            String[] to = new String[] { "jonny.reid@mirotechnologies.com" };
            email.sendMail(to, "request for user account", url);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    input.setRequestId(domainCreated.getRequestId());
// .//GEN-LAST:_postCreate_1
// .//GEN-BEGIN:_postCreate_2_be
}
Also used : MessagingException(javax.mail.MessagingException) EmailerBean(org.jaffa.util.EmailerBean) Criteria(org.jaffa.persistence.Criteria) UserRequest(org.jaffa.applications.jaffa.modules.user.domain.UserRequest)

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