Use module implementation to remove entities.

This commit is contained in:
Michael Hierweck 2017-09-20 21:05:59 +02:00
parent ced5cc9827
commit 1968b61fa9
2 changed files with 20 additions and 11 deletions

View File

@ -12,6 +12,7 @@ import javax.persistence.Query;
import de.hsadmin.core.model.AbstractEntity; import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.AbstractModuleImpl; import de.hsadmin.core.model.AbstractModuleImpl;
import de.hsadmin.core.model.AuthorisationException; import de.hsadmin.core.model.AuthorisationException;
import de.hsadmin.core.model.GenericModuleImpl;
import de.hsadmin.core.model.HSAdminException; import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.hostsharing.BasePacType; import de.hsadmin.hostsharing.BasePacType;
import de.hsadmin.mods.email.EMailAddress; import de.hsadmin.mods.email.EMailAddress;
@ -247,16 +248,24 @@ public class DomainModuleImpl extends AbstractModuleImpl {
@Override @Override
public void delete(AbstractEntity existingEntity) throws HSAdminException { public void delete(AbstractEntity existingEntity) throws HSAdminException {
EntityManager em = getTransaction().getEntityManager();
needsWriteAccessOn(existingEntity, "delete"); needsWriteAccessOn(existingEntity, "delete");
Domain dom = (Domain) existingEntity; Domain dom = (Domain) existingEntity;
EntityManager em = getTransaction().getEntityManager(); GenericModuleImpl helperModule = new GenericModuleImpl(getTransaction());
Query query = em.createQuery("SELECT adr FROM " + EMailAddress.class.getAnnotation(javax.persistence.Entity.class).name()
+ " adr WHERE adr.domain.name='" + dom.getName() + "'"); String[] dependQueries = new String[] { "SELECT adr FROM EMailAdresses adr WHERE domain = :dom", };
List<?> resultList = query.getResultList();
for (Object obj : resultList) { for (String queryString : dependQueries) {
EMailAddress eMailAddress = (EMailAddress) obj; Query qEntities = em.createQuery(queryString);
em.remove(eMailAddress); qEntities.setParameter("dom", dom.getName());
List<?> entitiesList = qEntities.getResultList();
for (Object o : entitiesList) {
if (o instanceof AbstractEntity) {
helperModule.delete((AbstractEntity) o);
} }
}
}
super.delete(existingEntity); super.delete(existingEntity);
} }

View File

@ -214,10 +214,10 @@ public class PacModuleImpl extends AbstractModuleImpl {
}; };
for (String queryString : dependQueries) { for (String queryString : dependQueries) {
Query qDoms = em.createQuery(queryString); Query qEntities = em.createQuery(queryString);
qDoms.setParameter("pac", pacName); qEntities.setParameter("pac", pacName);
List<?> domsList = qDoms.getResultList(); List<?> entitiesList = qEntities.getResultList();
for (Object o : domsList) { for (Object o : entitiesList) {
if (o instanceof AbstractEntity) { if (o instanceof AbstractEntity) {
helperModule.delete((AbstractEntity) o); helperModule.delete((AbstractEntity) o);
} }