/*
 * 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;

/**
 * Evento email configurabile. Un evento può essere scatenato direttamente
 * dall'esecuzione di una determinaa transazione, oppure essere ativato
 * direttamente da codice. Ad un evento possono essere associati uno o più mail
 * template, ognuno dei quali spedisce una mail.
 */
public final class MailEvent extends MailBaseBean {
	/**
	 * Serial version UID.
	 */
	private static final long serialVersionUID = 1280718653L;
	/**
	 * Default logger.
	 */
	@SuppressWarnings("unused")
	private static Log logger = LogFactory.getLog(MailEvent.class);
	//
	// Declare entity attributes with accessor methods
	//
	/**
	 * mail event id.
	 */
	private String mailEventId;
	/**
	 * mail event description.
	 */
	private String mailEventDescription;
	/**
	 * mail event enabld.
	 */
	private boolean enabled = true;
	/**
	 * mail event script.
	 */
	private String script;
	//
	// entity child attribute declarations
	//
	/**
	 * Riferimento all'entity list: SelectorPerTemplate. Associazione tra
	 * template e mail selectors
	 */
	private Collection<MailTemplate> theMailTemplate = new ArrayList<MailTemplate>();

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

	//
	// getters and setters
	//

	/**
	 * @return the mailEventId
	 */
	public String getMailEventId() {
		return mailEventId;
	}

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

	/**
	 * @return the mailEventDescription
	 */
	public String getMailEventDescription() {
		return mailEventDescription;
	}

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

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

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

	/**
	 * @return the script
	 */
	public String getScript() {
		return script;
	}

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

	/**
	 * @return the theMailTemplate
	 */
	public Collection<MailTemplate> getTheMailTemplate() {
		return theMailTemplate;
	}

	/**
	 * @param theMailTemplate the theMailTemplate to set
	 */
	public void setTheMailTemplate(Collection<MailTemplate> theMailTemplate) {
		this.theMailTemplate = theMailTemplate;
	}

	//
	// other methods
	//

	public void afterPropertiesSet() throws Exception {
		setMailEventId(getBeanName());
		setScript(StringUtils.trimToEmpty(getScript()));
		if (logger.isInfoEnabled()) {
			logger.info("Defined mail event: " + this);
		}
	}

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

}
