CustomerDao mit gemeinsamen Query-Funktionen

This commit is contained in:
Michael Hoennig 2017-04-12 14:55:45 +02:00
parent 5a9d2a0ba4
commit 79292ec263
5 changed files with 59 additions and 36 deletions

View File

@ -0,0 +1,32 @@
package de.hsadmin.dao.customer;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import de.hsadmin.bo.customer.Customer;
import de.hsadmin.common.error.TechnicalException;
import de.hsadmin.common.error.UserException;
import de.hsadmin.service.customer.CustomerVO;
@Stateless
public class CustomerDao {
@PersistenceContext(name="hsar")
private EntityManager entityManager;
public Customer findCustomerByName(final String customerName) throws UserException {
return findCustomerByNameImpl(customerName);
}
public Customer findCustomerByName(CustomerVO prototype) throws UserException, TechnicalException {
return findCustomerByNameImpl(prototype.get("name").getValue());
}
private Customer findCustomerByNameImpl(final Object customerName) throws UserException {
final Query query = entityManager.createQuery("SELECT c FROM Customer c WHERE c.name = :name");
query.setParameter("name", customerName);
return (Customer) query.getSingleResult();
}
}

View File

@ -2,15 +2,16 @@ package de.hsadmin.service.customer;
import java.util.List; import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Query; import javax.persistence.Query;
import de.hsadmin.bo.customer.Contact; import de.hsadmin.bo.customer.Contact;
import de.hsadmin.bo.customer.Customer;
import de.hsadmin.common.error.TechnicalException; import de.hsadmin.common.error.TechnicalException;
import de.hsadmin.common.error.UserException; import de.hsadmin.common.error.UserException;
import de.hsadmin.dao.customer.CustomerDao;
import de.hsadmin.login.RequestContext; import de.hsadmin.login.RequestContext;
import de.hsadmin.login.RequiredScope; import de.hsadmin.login.RequiredScope;
import de.hsadmin.login.Role; import de.hsadmin.login.Role;
@ -23,6 +24,9 @@ public class ContactService extends AbstractModule<ContactVO> implements Contact
@PersistenceContext(name="hsar") @PersistenceContext(name="hsar")
private EntityManager entityManager; private EntityManager entityManager;
@EJB
private CustomerDao customerDao;
@Override @Override
public ContactVO buildVO() throws TechnicalException { public ContactVO buildVO() throws TechnicalException {
@ -35,7 +39,7 @@ public class ContactService extends AbstractModule<ContactVO> implements Contact
throws UserException, TechnicalException { throws UserException, TechnicalException {
final ContactVO vo = super.create(requestContext, prototype); final ContactVO vo = super.create(requestContext, prototype);
final Contact bo = new Contact(); final Contact bo = new Contact();
bo.setCustomer(findCustomerByName(prototype.getCustomer())); bo.setCustomer(customerDao.findCustomerByName(prototype.getCustomer()));
vo.copyPropertiesToPersistentObject(bo); vo.copyPropertiesToPersistentObject(bo);
entityManager.persist(bo); entityManager.persist(bo);
final Contact newBO = findContactByNames(vo); final Contact newBO = findContactByNames(vo);
@ -91,10 +95,4 @@ public class ContactService extends AbstractModule<ContactVO> implements Contact
return (Contact) query.getSingleResult(); return (Contact) query.getSingleResult();
} }
private Customer findCustomerByName(final String customer) throws UserException {
final Query query = entityManager.createQuery("SELECT c FROM Customer c WHERE c.name = :name");
query.setParameter("name", customer);
return (Customer) query.getSingleResult();
}
} }

View File

@ -4,10 +4,10 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.ejb.EJB;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import de.hsadmin.bo.customer.Customer; import de.hsadmin.bo.customer.Customer;
import de.hsadmin.bo.pac.Pac; import de.hsadmin.bo.pac.Pac;
@ -16,6 +16,7 @@ import de.hsadmin.common.error.UserError;
import de.hsadmin.common.error.UserErrorList; import de.hsadmin.common.error.UserErrorList;
import de.hsadmin.common.error.UserException; import de.hsadmin.common.error.UserException;
import de.hsadmin.common.util.DateUtil; import de.hsadmin.common.util.DateUtil;
import de.hsadmin.dao.customer.CustomerDao;
import de.hsadmin.login.RequestContext; import de.hsadmin.login.RequestContext;
import de.hsadmin.login.RequiredScope; import de.hsadmin.login.RequiredScope;
import de.hsadmin.login.Role; import de.hsadmin.login.Role;
@ -29,6 +30,9 @@ public class CustomerService extends AbstractModule<CustomerVO> implements Custo
@PersistenceContext(name="hsar") @PersistenceContext(name="hsar")
private EntityManager entityManager; private EntityManager entityManager;
@EJB
private CustomerDao customerDao;
@Override @Override
public CustomerVO buildVO() throws TechnicalException { public CustomerVO buildVO() throws TechnicalException {
return new CustomerVO(); return new CustomerVO();
@ -46,7 +50,7 @@ public class CustomerService extends AbstractModule<CustomerVO> implements Custo
final Customer customerBO = new Customer(); final Customer customerBO = new Customer();
customerVO.copyPropertiesToPersistentObject(customerBO); customerVO.copyPropertiesToPersistentObject(customerBO);
entityManager.persist(customerBO); entityManager.persist(customerBO);
final Customer newBO = findCustomerByName(prototype); final Customer newBO = customerDao.findCustomerByName(prototype);
final CustomerVO newVO = new CustomerVO(); final CustomerVO newVO = new CustomerVO();
newVO.copyPropertiesFromPersistentObject(newBO); newVO.copyPropertiesFromPersistentObject(newBO);
return newVO; return newVO;
@ -71,7 +75,7 @@ public class CustomerService extends AbstractModule<CustomerVO> implements Custo
throws UserException, TechnicalException { throws UserException, TechnicalException {
final List<CustomerVO> customersForUpdate = super.update(requestContext, criteria, prototype); final List<CustomerVO> customersForUpdate = super.update(requestContext, criteria, prototype);
for (CustomerVO vo : customersForUpdate) { for (CustomerVO vo : customersForUpdate) {
final Customer customer = findCustomerByName(vo); final Customer customer = customerDao.findCustomerByName(vo);
prototype.copyPropertiesToPersistentObject(customer); prototype.copyPropertiesToPersistentObject(customer);
vo.copyPropertiesFromPersistentObject(customer); vo.copyPropertiesFromPersistentObject(customer);
} }
@ -110,11 +114,5 @@ public class CustomerService extends AbstractModule<CustomerVO> implements Custo
} }
errors.raiseException(); errors.raiseException();
} }
private Customer findCustomerByName(final CustomerVO prototype) throws UserException, TechnicalException {
final Query query = entityManager.createQuery("SELECT c FROM Customer c WHERE c.name = :name");
query.setParameter("name", prototype.get("name").getValue());
return (Customer) query.getSingleResult();
}
} }

View File

@ -2,16 +2,17 @@ package de.hsadmin.service.customer;
import java.util.List; import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Query; import javax.persistence.Query;
import de.hsadmin.bo.customer.Contact; import de.hsadmin.bo.customer.Contact;
import de.hsadmin.bo.customer.Customer;
import de.hsadmin.bo.customer.SEPADirectDebit; import de.hsadmin.bo.customer.SEPADirectDebit;
import de.hsadmin.common.error.TechnicalException; import de.hsadmin.common.error.TechnicalException;
import de.hsadmin.common.error.UserException; import de.hsadmin.common.error.UserException;
import de.hsadmin.dao.customer.CustomerDao;
import de.hsadmin.login.RequestContext; import de.hsadmin.login.RequestContext;
import de.hsadmin.login.RequiredScope; import de.hsadmin.login.RequiredScope;
import de.hsadmin.login.Role; import de.hsadmin.login.Role;
@ -25,6 +26,9 @@ public class SEPADirectDebitService extends AbstractModule<SEPADirectDebitVO> im
@PersistenceContext(name="hsar") @PersistenceContext(name="hsar")
private EntityManager entityManager; private EntityManager entityManager;
@EJB
private CustomerDao customerDao;
@Override @Override
public SEPADirectDebitVO buildVO() throws TechnicalException { public SEPADirectDebitVO buildVO() throws TechnicalException {
return new SEPADirectDebitVO(); return new SEPADirectDebitVO();
@ -36,7 +40,7 @@ public class SEPADirectDebitService extends AbstractModule<SEPADirectDebitVO> im
throws UserException, TechnicalException { throws UserException, TechnicalException {
final SEPADirectDebitVO vo = super.create(requestContext, prototype); final SEPADirectDebitVO vo = super.create(requestContext, prototype);
final SEPADirectDebit bo = new SEPADirectDebit(); final SEPADirectDebit bo = new SEPADirectDebit();
bo.setCustomer(findCustomerByName(prototype.getCustomer())); bo.setCustomer(customerDao.findCustomerByName(prototype.getCustomer()));
vo.copyPropertiesToPersistentObject(bo); vo.copyPropertiesToPersistentObject(bo);
entityManager.persist(bo); entityManager.persist(bo);
final SEPADirectDebit newBO = findMandatByValues(vo); final SEPADirectDebit newBO = findMandatByValues(vo);
@ -90,12 +94,5 @@ public class SEPADirectDebitService extends AbstractModule<SEPADirectDebitVO> im
query.setParameter("customer", prototype.get("customer").getValue()); query.setParameter("customer", prototype.get("customer").getValue());
query.setParameter("mandatSigned", prototype.get("mandatSigned").getValue()); query.setParameter("mandatSigned", prototype.get("mandatSigned").getValue());
return (SEPADirectDebit) query.getSingleResult(); return (SEPADirectDebit) query.getSingleResult();
} }
private Customer findCustomerByName(final String customer) throws UserException {
final Query query = entityManager.createQuery("SELECT c FROM Customer c WHERE c.name = :name");
query.setParameter("name", customer);
return (Customer) query.getSingleResult();
}
} }

View File

@ -5,6 +5,7 @@ import java.net.UnknownHostException;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
@ -26,12 +27,16 @@ import de.hsadmin.login.Role;
import de.hsadmin.login.ScopePolicy; import de.hsadmin.login.ScopePolicy;
import de.hsadmin.module.impl.AbstractModule; import de.hsadmin.module.impl.AbstractModule;
import de.hsadmin.module.util.QueryBuilder; import de.hsadmin.module.util.QueryBuilder;
import de.hsadmin.dao.customer.CustomerDao;
@Stateless @Stateless
public class PacService extends AbstractModule<PacVO> implements PacServiceLocal { public class PacService extends AbstractModule<PacVO> implements PacServiceLocal {
@PersistenceContext(name="hsar") @PersistenceContext(name="hsar")
private EntityManager entityManager; private EntityManager entityManager;
@EJB
private CustomerDao customerDao;
@Override @Override
public PacVO buildVO() throws TechnicalException { public PacVO buildVO() throws TechnicalException {
@ -64,7 +69,7 @@ public class PacService extends AbstractModule<PacVO> implements PacServiceLocal
throw new UserException(new UserError(UserError.MSG_MISSING_AUTHORIZATION, "add")); throw new UserException(new UserError(UserError.MSG_MISSING_AUTHORIZATION, "add"));
} }
} }
bo.setCustomer(findCustomerByName(customerProperty)); bo.setCustomer(customerDao.findCustomerByName(customerProperty));
vo.copyPropertiesToPersistentObject(bo); vo.copyPropertiesToPersistentObject(bo);
if (bo.getCreated() == null) { if (bo.getCreated() == null) {
bo.setCreated(new Date()); bo.setCreated(new Date());
@ -100,7 +105,7 @@ public class PacService extends AbstractModule<PacVO> implements PacServiceLocal
final Pac bo = findPacByName(vo.getName()); final Pac bo = findPacByName(vo.getName());
final String customerName = prototype.getCustomer(); final String customerName = prototype.getCustomer();
if (customerName != null && !customerName.isEmpty()) { if (customerName != null && !customerName.isEmpty()) {
final Customer customer = findCustomerByName(customerName); final Customer customer = customerDao.findCustomerByName(customerName);
bo.setCustomer(customer); bo.setCustomer(customer);
} }
prototype.copyPropertiesToPersistentObject(bo); prototype.copyPropertiesToPersistentObject(bo);
@ -162,11 +167,4 @@ public class PacService extends AbstractModule<PacVO> implements PacServiceLocal
query.setParameter("name", value); query.setParameter("name", value);
return (BasePac) query.getSingleResult(); return (BasePac) query.getSingleResult();
} }
private Customer findCustomerByName(final String value) {
final Query query = entityManager.createQuery("SELECT c FROM Customer c WHERE c.name = :name");
query.setParameter("name", value);
return (Customer) query.getSingleResult();
}
} }