alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (SendOrderConfirmationEmailAdvice.java)

This example Spring Framework source code file (SendOrderConfirmationEmailAdvice.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

dear, default_mail_from, default_subject, jpetstore, object, object, order, order, reflection, sendorderconfirmationemailadvice, simplemailmessage, string, string, stringbuffer, stringbuffer

The Spring Framework SendOrderConfirmationEmailAdvice.java source code

package org.springframework.samples.jpetstore.domain.logic;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.samples.jpetstore.domain.Account;
import org.springframework.samples.jpetstore.domain.Order;

/**
 * AOP advice that sends confirmation email after order has been submitted
 * @author Dmitriy Kopylenko
 */
public class SendOrderConfirmationEmailAdvice implements AfterReturningAdvice, InitializingBean {

	private static final String DEFAULT_MAIL_FROM = "jpetstore@springframework.org";

	private static final String DEFAULT_SUBJECT = "Thank you for your order!";

	private final Log logger = LogFactory.getLog(getClass());

	private MailSender mailSender;

	private String mailFrom = DEFAULT_MAIL_FROM;

	private String subject = DEFAULT_SUBJECT;

	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void setMailFrom(String mailFrom) {
		this.mailFrom = mailFrom;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void afterPropertiesSet() throws Exception {
		if (this.mailSender == null) {
			throw new IllegalStateException("mailSender is required");
		}
	}

	public void afterReturning(Object returnValue, Method m, Object[] args, Object target) throws Throwable {
		Order order = (Order) args[0];
		Account account = ((PetStoreFacade) target).getAccount(order.getUsername());

		// don't do anything if email address is not set
		if (account.getEmail() == null || account.getEmail().length() == 0) {
			return;
		}

		StringBuffer text = new StringBuffer();
		text.append("Dear ").append(account.getFirstName()).append(' ').append(account.getLastName());
		text.append(", thank your for your order from JPetStore. Please note that your order number is ");
		text.append(order.getOrderId());

		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setTo(account.getEmail());
		mailMessage.setFrom(this.mailFrom);
		mailMessage.setSubject(this.subject);
		mailMessage.setText(text.toString());
		try {
			this.mailSender.send(mailMessage);
		}
		catch (MailException ex) {
			// just log it and go on
			logger.warn("An exception occured when trying to send email", ex);
		}
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework SendOrderConfirmationEmailAdvice.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.