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

View File

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