package de.hsadmin.mods.email;

import javax.persistence.EntityManager;

import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.qserv.EntityProcessorFactory;
import de.hsadmin.core.qserv.Processor;
import de.hsadmin.core.qserv.ShellProcessor;

/**
 * System level implementation for EMailAlias module.
 */
public class EMailAliasProcessorFactory implements EntityProcessorFactory {
	
	// TODO: execution of any of these processors needs to be synchronized

	/**
	 * @return a Processor which creates an email alias
	 */
	public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em,
			T entity) {
		EMailAlias alias = (EMailAlias) entity;
		return new ShellProcessor("postalias -r -i /etc/postfix/aliases", 
			alias.getName() + ": " + alias.getTarget());
	}

	/**
	 * @return a Processor which updates an email alias
	 */
	public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em,
			T entity) {
		EMailAlias alias = (EMailAlias) entity;
		return new ShellProcessor("postalias -r -i /etc/postfix/aliases", 
			alias.getName() + ": " + alias.getTarget());
	}

	/**
	 * @return a Processor which deletes an email alias
	 */
	public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em,
			T entity) {
		EMailAlias alias = (EMailAlias) entity;
		return new ShellProcessor(
			"postalias -d '" + alias.getName() + "' /etc/postfix/aliases");
	}
}
