/*
 * Copyright 2009-2010 The MICE Project Team.
 * Licensed under the MICE s.r.l.
 * End User License Agreement
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.micegroup.it/voila/license.html
 */
package it.mice.voila.runtime.mail.beans;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

/**
 * Definizione di una mail.
 */
public final class MailTemplate extends MailBaseBean {
	/**
	 * Serial version UID.
	 */
	private static final long serialVersionUID = 1280718653L;
	/**
	 * Default logger.
	 */
	@SuppressWarnings("unused")
	private static Log logger = LogFactory.getLog(MailTemplate.class);
	//
	// Declare entity attributes with accessor methods
	//
	/**
	 * String data type.
	 */
	private String mailTemplateId;
	/**
	 * String data type.
	 */
	private String mailTemplateDescription;
	/**
	 * String data type.
	 */
	private String mailSubject;
	/**
	 * String data type.
	 */
	private String mailBodyHtml;
	/**
	 * String data type.
	 */
	private String mailBodyText;
	/**
	 * String data type.
	 */
	private String mailFrom;
	/**
	 * String data type.
	 */
	private String mailTo;
	/**
	 * String data type.
	 */
	private String mailCc;
	/**
	 * String data type.
	 */
	private String mailBcc;
	/**
	 * String data type.
	 */
	private String mailReplyTo;
	/**
	 * String data type.
	 */
	private boolean skipTitle = false;
	/**
	 * is mail template enabled?.
	 */
	private boolean enabled = true;
	/**
	 * String data type.
	 */
	private String attachments;
	//
	// entity parent attribute declaration
	//
	/**
	 * Riferimento all'entity: MailConfig. Server di posta definiti.
	 */
	private MailConfig theMailConfig;
	/**
	 * Riferimento all'entity: MailStyle. Stili di mail possibili
	 */
	private MailStyle theMailStyle;
	//
	// entity child attribute declarations
	//
	/**
	 * Riferimento all'entity list: SelectorPerTemplate. Associazione tra
	 * template e mail selectors
	 */
	private Collection<SelectorPerTemplate> theSelectorPerTemplate = new ArrayList<SelectorPerTemplate>();

	//
	// Constructor declaration
	//
	/**
	 * Costruttore vuoto per MailTemplate. Istanzia un nuovo MailTemplate
	 * completamente vuoto.
	 */
	public MailTemplate() {
		super();
	}

	//
	// getters and setters
	//
	/**
	 * @return the mailTemplateId
	 */
	public String getMailTemplateId() {
		return mailTemplateId;
	}

	/**
	 * @param mailTemplateId
	 *            the mailTemplateId to set
	 */
	public void setMailTemplateId(String mailTemplateId) {
		this.mailTemplateId = mailTemplateId;
	}

	/**
	 * @return the mailTemplateDescription
	 */
	public String getMailTemplateDescription() {
		return mailTemplateDescription;
	}

	/**
	 * @param mailTemplateDescription
	 *            the mailTemplateDescription to set
	 */
	public void setMailTemplateDescription(String mailTemplateDescription) {
		this.mailTemplateDescription = mailTemplateDescription;
	}

	/**
	 * @return the mailSubject
	 */
	public String getMailSubject() {
		return mailSubject;
	}

	/**
	 * @param mailSubject
	 *            the mailSubject to set
	 */
	public void setMailSubject(String mailSubject) {
		this.mailSubject = mailSubject;
	}

	/**
	 * @return the mailBodyHtml
	 */
	public String getMailBodyHtml() {
		return mailBodyHtml;
	}

	/**
	 * @param mailBodyHtml the mailBodyHtml to set
	 */
	public void setMailBodyHtml(String mailBodyHtml) {
		this.mailBodyHtml = mailBodyHtml;
	}

	/**
	 * @return the mailBodyText
	 */
	public String getMailBodyText() {
		return mailBodyText;
	}

	/**
	 * @param mailBodyText the mailBodyText to set
	 */
	public void setMailBodyText(String mailBodyText) {
		this.mailBodyText = mailBodyText;
	}

	/**
	 * @return the mailFrom
	 */
	public String getMailFrom() {
		return mailFrom;
	}

	/**
	 * @param mailFrom
	 *            the mailFrom to set
	 */
	public void setMailFrom(String mailFrom) {
		this.mailFrom = mailFrom;
	}

	/**
	 * @return the mailTo
	 */
	public String getMailTo() {
		return mailTo;
	}

	/**
	 * @param mailTo
	 *            the mailTo to set
	 */
	public void setMailTo(String mailTo) {
		this.mailTo = mailTo;
	}

	/**
	 * @return the mailCc
	 */
	public String getMailCc() {
		return mailCc;
	}

	/**
	 * @param mailCc
	 *            the mailCc to set
	 */
	public void setMailCc(String mailCc) {
		this.mailCc = mailCc;
	}

	/**
	 * @return the mailBcc
	 */
	public String getMailBcc() {
		return mailBcc;
	}

	/**
	 * @param mailBcc
	 *            the mailBcc to set
	 */
	public void setMailBcc(String mailBcc) {
		this.mailBcc = mailBcc;
	}

	/**
	 * @return the mailReplyTo
	 */
	public String getMailReplyTo() {
		return mailReplyTo;
	}

	/**
	 * @param mailReplyTo
	 *            the mailReplyTo to set
	 */
	public void setMailReplyTo(String mailReplyTo) {
		this.mailReplyTo = mailReplyTo;
	}

	/**
	 * @return the skipTitle
	 */
	public boolean isSkipTitle() {
		return skipTitle;
	}

	/**
	 * @param skipTitle the skipTitle to set
	 */
	public void setSkipTitle(boolean skipTitle) {
		this.skipTitle = skipTitle;
	}

	/**
	 * @return the attachments
	 */
	public String getAttachments() {
		return attachments;
	}

	/**
	 * @param attachments the attachments to set
	 */
	public void setAttachments(String attachments) {
		this.attachments = attachments;
	}

	/**
	 * @return the theMailConfig
	 */
	public MailConfig getTheMailConfig() {
		return theMailConfig;
	}

	/**
	 * @param theMailConfig
	 *            the theMailConfig to set
	 */
	public void setTheMailConfig(MailConfig theMailConfig) {
		this.theMailConfig = theMailConfig;
	}

	/**
	 * @return the theMailStyle
	 */
	public MailStyle getTheMailStyle() {
		return theMailStyle;
	}

	/**
	 * @param theMailStyle
	 *            the theMailStyle to set
	 */
	public void setTheMailStyle(MailStyle theMailStyle) {
		this.theMailStyle = theMailStyle;
	}

	/**
	 * @return the theSelectorPerTemplate
	 */
	public Collection<SelectorPerTemplate> getTheSelectorPerTemplate() {
		return theSelectorPerTemplate;
	}

	/**
	 * @param theSelectorPerTemplate
	 *            the theSelectorPerTemplate to set
	 */
	public void setTheSelectorPerTemplate(
			Collection<SelectorPerTemplate> theSelectorPerTemplate) {
		this.theSelectorPerTemplate = theSelectorPerTemplate;
	}

	/**
	 * @return the enabled
	 */
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * @param enabled the enabled to set
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	//
	// other methods
	//

	public void afterPropertiesSet() throws Exception {
		setMailTemplateId(getBeanName());
		if (getTheMailConfig() == null) {
			try {
				setTheMailConfig(getBeanFactory().getBean(MailConfig.class));
			} catch (NoSuchBeanDefinitionException e) {
				logger.fatal("No mail config defined or more than only one mail config found. If more mail config are defined please ensure that each mailTemplate have corresponding mail config defined thru the property theMailConfig.");
				throw e;
			}
		}
		if (getTheMailStyle() == null) {
			try {
				setTheMailStyle(getBeanFactory().getBean(MailStyle.class));
			} catch (NoSuchBeanDefinitionException e) {
				logger.fatal("No mail style defined or more than only one mail style found. If more mail style are defined please ensure that each mailTemplate have corresponding mail style defined thru the property theMailStyle.");
				throw e;
			}
		}
		setMailBodyHtml(StringUtils.trimToEmpty(getMailBodyHtml()));
		setMailBodyText(StringUtils.trimToEmpty(getMailBodyText()));
		setAttachments(StringUtils.trimToEmpty(getAttachments()));
		
		if (getMailFrom() == null) {
			setMailFrom(getTheMailConfig().getMailFromAddress());
		}
		if (getMailCc() == null) {
			setMailCc(getTheMailConfig().getMailCcAddress());
		}
		if (getMailBcc() == null) {
			setMailBcc(getTheMailConfig().getMailBccAddress());
		}
		if (getMailSubject() == null) {
			setMailSubject(getTheMailConfig().getMailDefaultSubject());
		}
		
		if (logger.isInfoEnabled()) {
			logger.info("Defined mail template: " + this);
		}
	}

	public Collection<MailSelector> getMailToSelectors() {
		return getMailSelectorFor(SelectorPerTemplate.SELECTOR_TO);
	}

	public Collection<MailSelector> getMailCcSelectors() {
		return getMailSelectorFor(SelectorPerTemplate.SELECTOR_CC);
	}

	public Collection<MailSelector> getMailBccSelectors() {
		return getMailSelectorFor(SelectorPerTemplate.SELECTOR_BCC);
	}

	public Collection<MailSelector> getMailSelectorFor(String type) {
		Collection<MailSelector> result = new ArrayList<MailSelector>();
		for (SelectorPerTemplate selector : getTheSelectorPerTemplate()) {
			if (type.equalsIgnoreCase(selector.getTipoSelector())) {
				result.add(selector.getTheMailSelector());
			}
		}
		return result;
	}

	public Collection<SelectorPerTemplate> getSelectorPerTemplateFor(String type) {
		Collection<SelectorPerTemplate> result = new ArrayList<SelectorPerTemplate>();
		for (SelectorPerTemplate selector : getTheSelectorPerTemplate()) {
			if (type.equalsIgnoreCase(selector.getTipoSelector())) {
				result.add(selector);
			}
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "MailTemplate [mailTemplateId=" + mailTemplateId
				+ ", mailTemplateDescription=" + mailTemplateDescription
				+ ", mailSubject=" + mailSubject + "]";
	}

	public void addTheSelectorPerTemplate(
			SelectorPerTemplate selectorPerTemplate) {
		selectorPerTemplate.setTheMailTemplate(this);
		getTheSelectorPerTemplate().add(selectorPerTemplate);
	}
}
