hs.hsadmin/hsarback/src/de/hsadmin/mods/email/EMailAddressProcessorFactory.java

96 lines
3.8 KiB
Java
Raw Normal View History

2010-10-05 21:42:07 +02:00
package de.hsadmin.mods.email;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.qserv.CompoundProcessor;
import de.hsadmin.core.qserv.EntityProcessorFactory;
import de.hsadmin.core.qserv.Processor;
import de.hsadmin.core.qserv.ShellProcessor;
2011-08-29 16:09:20 +02:00
import de.hsadmin.core.qserv.WaitingTasksProcessor;
2010-10-05 21:42:07 +02:00
public class EMailAddressProcessorFactory implements EntityProcessorFactory {
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) {
// TODO: combine both keys in a single call (optimization)
EMailAddress email = (EMailAddress) entity;
CompoundProcessor cp = new CompoundProcessor();
cp.appendProcessor(new ShellProcessor( "postmap -r -i /etc/postfix/virtual",
email.getFullDomain() + " -" ) );
cp.appendProcessor(new ShellProcessor( "postmap -r -i /etc/postfix/virtual",
email.getEMailAddress() + " " + email.getTarget() ) );
2011-08-29 16:09:20 +02:00
if (emailAddressCount(em, email) < 2) {
WaitingTasksProcessor waitingTasksProcessor = new WaitingTasksProcessor(cp);
String pacName = email.getDomain().getUser().getPac().getName();
String domName = email.getDomain().getName();
for (String queueName : new String[] { "mail1", "mail2", "mail3" }) {
waitingTasksProcessor.appendProcessor(queueName, createMailinSetupProcessor(domName, pacName), queueName + ".hostsharing.net");
}
return waitingTasksProcessor;
} else {
return cp;
}
2010-10-05 21:42:07 +02:00
}
public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) {
// TODO: if update is specified by primary-key or DB query instead of OID,
// a postmap -d might be neccessary
return createCreateProcessor(em, entity);
}
public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) {
CompoundProcessor cp = new CompoundProcessor();
EMailAddress email = (EMailAddress) entity;
cp.appendProcessor(
new ShellProcessor( "postmap -d '" + email.getEMailAddress() + "' /etc/postfix/virtual" ) );
2011-08-29 16:09:20 +02:00
int emailAddressCount = emailAddressCount(em, email);
if (emailAddressCount == 0) {
// remove the domain from virtual.db
cp.appendProcessor(
new ShellProcessor( "postmap -d '" + email.getFullDomain() + "' /etc/postfix/virtual" ) );
WaitingTasksProcessor waitingTasksProcessor = new WaitingTasksProcessor(cp);
String domName = email.getDomain().getName();
for (String queueName : new String[] { "mail1", "mail2", "mail3" }) {
waitingTasksProcessor.appendProcessor(queueName, createMailinDeleteProcessor(domName), queueName + ".hostsharing.net");
}
return waitingTasksProcessor;
} else {
return cp;
}
}
private int emailAddressCount(EntityManager em, EMailAddress email) {
2010-10-05 21:42:07 +02:00
Query query;
if ( email.getSubdomain() != null ) {
query = em.createQuery("SELECT e FROM EMailAddresses e WHERE e.subdomain=:subdomain AND e.domain=:domain");
query.setParameter("subdomain", email.getSubdomain());
}
else {
2011-08-30 12:50:57 +02:00
query = em.createQuery("SELECT e FROM EMailAddresses e WHERE ( e.subdomain IS NULL OR e.subdomain = '' ) AND e.domain=:domain");
2010-10-05 21:42:07 +02:00
}
query.setParameter("domain", email.getDomain());
List<?> result = query.getResultList();
2011-08-29 16:09:20 +02:00
if (result == null) {
return 0;
2010-10-05 21:42:07 +02:00
}
2011-08-29 16:09:20 +02:00
return result.size();
}
private Processor createMailinSetupProcessor(String domName, String pacName) {
return
new ShellProcessor("postmap -r -i /etc/postfix-mailin/transport",
domName + " smtp:[" + pacName + ".hostsharing.net]\n" +
"." + domName + " smtp:[" + pacName + ".hostsharing.net]\n");
}
private Processor createMailinDeleteProcessor(String domName) {
return new ShellProcessor(
"postmap -d '" + domName + "' /etc/postfix-mailin/transport && " +
"postmap -d '." + domName + "' /etc/postfix-mailin/transport");
2010-10-05 21:42:07 +02:00
}
2011-08-29 16:09:20 +02:00
2010-10-05 21:42:07 +02:00
}