Design Patterns · java

strategy design pattern

public interface Sender {
  public boolean Send(NotificationBean notificationBean,List ... temp);
}

public abstract class sendEmailTemplate implements Sender {

	public abstract void preSend(NotificationBean notification,List ... temp);

	public abstract void PostSend();

	public abstract boolean TryToSend();

	public abstract boolean IsSent();

	public abstract boolean NotifyDB(boolean success);

	public boolean Send(NotificationBean notification,List ... temp) {
		preSend(notification,temp);
		if (TryToSend() && IsSent())
			PostSend();
		
		return IsSent();	
	}
}

public abstract class sendSmsTemplate implements Sender {

	public abstract void preSend(NotificationBean notification,List ... temp);

	public abstract void PostSend();

	public abstract boolean TryToSend();

	public abstract boolean IsSent();

	public abstract boolean NotifyDB(boolean success);

	public boolean Send(NotificationBean notification,List ... temp) {
		preSend(notification,temp);
		if (TryToSend() && IsSent())
			PostSend();
		
		return IsSent();	
	}
}

@Component
public class SMSSender extends sendSmsTemplate{

	@Override
	public void preSend(NotificationBean notification,List ... temp){
		
		System.out.println("presend action in send sms");
	}

	@Override
	public void PostSend() {
	
		System.out.println("postsend action in send sms");
	}

	@Override
	public boolean TryToSend() {
		
		return true;
	}

	@Override
	public boolean IsSent() {
		
		return true;
	}

	@Override
	public boolean NotifyDB(boolean success) {
		
		return false;
	}

}


@Component
public class EmailSender extends sendEmailTemplate {

	private String[] to, cc, bcc;
	boolean sent = false;
	private String subject, message;

	@Autowired
	public MailService mailService;
	@Autowired
	private NotificationBean NotificationBean;

	public EmailSender() {

	}

	@Override
	public void preSend(NotificationBean notification, List... temp) {

		// here i am within present of email sender
		// you are supposed to receive 3 lists
		// first one is to Destination list
		// first one is cc Destination list
		// first one is bcc Destination list

		/*
		 * for (List list : temp) { for (String string : list) {
		 * System.out.println(string); } }
		 */
		int counter = 0;

		for (List list : temp) {
			if (counter == 0)
				to = list.toArray(new String[0]);
			if (counter == 1)
				cc = list.toArray(new String[0]);
			if (counter == 2)
				bcc = list.toArray(new String[0]);
			counter++;
		}

		subject = notification.getNotification().getSubject();
		message = notification.getNotification().getMessage();

		setNotificationBean(notification);

	}

	@Override
	public void PostSend() {

		System.out.println("postsend action in send email");
	}

	@Override
	public boolean TryToSend() {
		SimpleMailMessage email = new SimpleMailMessage();

		email.setTo(to);
		email.setBcc(bcc);
		email.setCc(cc);
		email.setText(message);
		email.setSubject(subject);

		try {
			mailService.send("lorddisk@yahoo.com", to, cc, bcc, subject,
					message, getNotificationBean());
			System.out.println("Email has been sent ");
			sent = true;
		} catch (Exception e) {
			sent = false;
		}

		return sent;
	}

	@Override
	public boolean IsSent() {

		return sent;
	}

	@Override
	public boolean NotifyDB(boolean success) {

		return false;
	}

	public String[] getTo() {
		return to;
	}

	public void setTo(String[] to) {
		this.to = to;
	}

	public String[] getCc() {
		return cc;
	}

	public void setCc(String[] cc) {
		this.cc = cc;
	}

	public String[] getBcc() {
		return bcc;
	}

	public void setBcc(String[] bcc) {
		this.bcc = bcc;
	}

	public boolean isSent() {
		return sent;
	}

	public void setSent(boolean sent) {
		this.sent = sent;
	}

	public String getSubject() {
		return subject;
	}

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

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public MailService getMailService() {
		return mailService;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	public void setNotificationBean(NotificationBean notificationBean) {
		NotificationBean = notificationBean;
	}

	public NotificationBean getNotificationBean() {
		return NotificationBean;
	}

}


@Component
public  class senderFactory {
	@Autowired
	public EmailSender emailsender;
	@Autowired
	public SMSSender smsSender;

	public  Sender getSender(DestinationTypes destinationTypes) {
		Sender s = null;
		if (destinationTypes == destinationTypes.Email)
			s= emailsender;
		else if (destinationTypes == destinationTypes.MOBILENUMBER)
			s=  smsSender;
		return s;
	}

	public EmailSender getEmailsender() {
		return emailsender;
	}

	public void setEmailsender(EmailSender emailsender) {
		this.emailsender = emailsender;
	}

	public SMSSender getSmsSender() {
		return smsSender;
	}

	public void setSmsSender(SMSSender smsSender) {
		this.smsSender = smsSender;
	}
}


public enum DestinationTypes {
	Email(1),
	MOBILENUMBER(2),
	IP(3),
	URL(4);
	private int type;
	DestinationTypes(int type)
	{
		this.type=type;
	}
	
	public void setType(int type) {
		this.type = type;
	}

	public int getType() {
		return type;
	}
	
}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s