From 79292ec263b142291a0d07320fca3084629e875b Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 12 Apr 2017 14:55:45 +0200 Subject: [PATCH] CustomerDao mit gemeinsamen Query-Funktionen --- .../de/hsadmin/dao/customer/CustomerDao.java | 32 +++++++++++++++++++ .../service/customer/ContactService.java | 14 ++++---- .../service/customer/CustomerService.java | 16 ++++------ .../customer/SEPADirectDebitService.java | 17 ++++------ .../de/hsadmin/service/pac/PacService.java | 16 ++++------ 5 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 cust-services/src/main/java/de/hsadmin/dao/customer/CustomerDao.java diff --git a/cust-services/src/main/java/de/hsadmin/dao/customer/CustomerDao.java b/cust-services/src/main/java/de/hsadmin/dao/customer/CustomerDao.java new file mode 100644 index 0000000..e90c76b --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/dao/customer/CustomerDao.java @@ -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(); + } +} diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/ContactService.java b/cust-services/src/main/java/de/hsadmin/service/customer/ContactService.java index f4ab674..7bb414c 100644 --- a/cust-services/src/main/java/de/hsadmin/service/customer/ContactService.java +++ b/cust-services/src/main/java/de/hsadmin/service/customer/ContactService.java @@ -2,15 +2,16 @@ package de.hsadmin.service.customer; import java.util.List; +import javax.ejb.EJB; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import de.hsadmin.bo.customer.Contact; -import de.hsadmin.bo.customer.Customer; import de.hsadmin.common.error.TechnicalException; import de.hsadmin.common.error.UserException; +import de.hsadmin.dao.customer.CustomerDao; import de.hsadmin.login.RequestContext; import de.hsadmin.login.RequiredScope; import de.hsadmin.login.Role; @@ -23,6 +24,9 @@ public class ContactService extends AbstractModule implements Contact @PersistenceContext(name="hsar") private EntityManager entityManager; + + @EJB + private CustomerDao customerDao; @Override public ContactVO buildVO() throws TechnicalException { @@ -35,7 +39,7 @@ public class ContactService extends AbstractModule implements Contact throws UserException, TechnicalException { final ContactVO vo = super.create(requestContext, prototype); final Contact bo = new Contact(); - bo.setCustomer(findCustomerByName(prototype.getCustomer())); + bo.setCustomer(customerDao.findCustomerByName(prototype.getCustomer())); vo.copyPropertiesToPersistentObject(bo); entityManager.persist(bo); final Contact newBO = findContactByNames(vo); @@ -91,10 +95,4 @@ public class ContactService extends AbstractModule implements Contact 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(); - } - } diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/CustomerService.java b/cust-services/src/main/java/de/hsadmin/service/customer/CustomerService.java index dd33a02..f331453 100644 --- a/cust-services/src/main/java/de/hsadmin/service/customer/CustomerService.java +++ b/cust-services/src/main/java/de/hsadmin/service/customer/CustomerService.java @@ -4,10 +4,10 @@ import java.util.Date; import java.util.List; import java.util.Set; +import javax.ejb.EJB; 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.bo.pac.Pac; @@ -16,6 +16,7 @@ import de.hsadmin.common.error.UserError; import de.hsadmin.common.error.UserErrorList; import de.hsadmin.common.error.UserException; import de.hsadmin.common.util.DateUtil; +import de.hsadmin.dao.customer.CustomerDao; import de.hsadmin.login.RequestContext; import de.hsadmin.login.RequiredScope; import de.hsadmin.login.Role; @@ -29,6 +30,9 @@ public class CustomerService extends AbstractModule implements Custo @PersistenceContext(name="hsar") private EntityManager entityManager; + @EJB + private CustomerDao customerDao; + @Override public CustomerVO buildVO() throws TechnicalException { return new CustomerVO(); @@ -46,7 +50,7 @@ public class CustomerService extends AbstractModule implements Custo final Customer customerBO = new Customer(); customerVO.copyPropertiesToPersistentObject(customerBO); entityManager.persist(customerBO); - final Customer newBO = findCustomerByName(prototype); + final Customer newBO = customerDao.findCustomerByName(prototype); final CustomerVO newVO = new CustomerVO(); newVO.copyPropertiesFromPersistentObject(newBO); return newVO; @@ -71,7 +75,7 @@ public class CustomerService extends AbstractModule implements Custo throws UserException, TechnicalException { final List customersForUpdate = super.update(requestContext, criteria, prototype); for (CustomerVO vo : customersForUpdate) { - final Customer customer = findCustomerByName(vo); + final Customer customer = customerDao.findCustomerByName(vo); prototype.copyPropertiesToPersistentObject(customer); vo.copyPropertiesFromPersistentObject(customer); } @@ -110,11 +114,5 @@ public class CustomerService extends AbstractModule implements Custo } 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(); - } } diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/SEPADirectDebitService.java b/cust-services/src/main/java/de/hsadmin/service/customer/SEPADirectDebitService.java index 987635f..ae5525b 100644 --- a/cust-services/src/main/java/de/hsadmin/service/customer/SEPADirectDebitService.java +++ b/cust-services/src/main/java/de/hsadmin/service/customer/SEPADirectDebitService.java @@ -2,16 +2,17 @@ package de.hsadmin.service.customer; import java.util.List; +import javax.ejb.EJB; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import de.hsadmin.bo.customer.Contact; -import de.hsadmin.bo.customer.Customer; import de.hsadmin.bo.customer.SEPADirectDebit; import de.hsadmin.common.error.TechnicalException; import de.hsadmin.common.error.UserException; +import de.hsadmin.dao.customer.CustomerDao; import de.hsadmin.login.RequestContext; import de.hsadmin.login.RequiredScope; import de.hsadmin.login.Role; @@ -25,6 +26,9 @@ public class SEPADirectDebitService extends AbstractModule im @PersistenceContext(name="hsar") private EntityManager entityManager; + @EJB + private CustomerDao customerDao; + @Override public SEPADirectDebitVO buildVO() throws TechnicalException { return new SEPADirectDebitVO(); @@ -36,7 +40,7 @@ public class SEPADirectDebitService extends AbstractModule im throws UserException, TechnicalException { final SEPADirectDebitVO vo = super.create(requestContext, prototype); final SEPADirectDebit bo = new SEPADirectDebit(); - bo.setCustomer(findCustomerByName(prototype.getCustomer())); + bo.setCustomer(customerDao.findCustomerByName(prototype.getCustomer())); vo.copyPropertiesToPersistentObject(bo); entityManager.persist(bo); final SEPADirectDebit newBO = findMandatByValues(vo); @@ -90,12 +94,5 @@ public class SEPADirectDebitService extends AbstractModule im query.setParameter("customer", prototype.get("customer").getValue()); query.setParameter("mandatSigned", prototype.get("mandatSigned").getValue()); 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(); - } - + } } diff --git a/cust-services/src/main/java/de/hsadmin/service/pac/PacService.java b/cust-services/src/main/java/de/hsadmin/service/pac/PacService.java index d9994a5..a543c79 100644 --- a/cust-services/src/main/java/de/hsadmin/service/pac/PacService.java +++ b/cust-services/src/main/java/de/hsadmin/service/pac/PacService.java @@ -5,6 +5,7 @@ import java.net.UnknownHostException; import java.util.Date; import java.util.List; +import javax.ejb.EJB; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -26,12 +27,16 @@ import de.hsadmin.login.Role; import de.hsadmin.login.ScopePolicy; import de.hsadmin.module.impl.AbstractModule; import de.hsadmin.module.util.QueryBuilder; +import de.hsadmin.dao.customer.CustomerDao; @Stateless public class PacService extends AbstractModule implements PacServiceLocal { @PersistenceContext(name="hsar") private EntityManager entityManager; + + @EJB + private CustomerDao customerDao; @Override public PacVO buildVO() throws TechnicalException { @@ -64,7 +69,7 @@ public class PacService extends AbstractModule implements PacServiceLocal throw new UserException(new UserError(UserError.MSG_MISSING_AUTHORIZATION, "add")); } } - bo.setCustomer(findCustomerByName(customerProperty)); + bo.setCustomer(customerDao.findCustomerByName(customerProperty)); vo.copyPropertiesToPersistentObject(bo); if (bo.getCreated() == null) { bo.setCreated(new Date()); @@ -100,7 +105,7 @@ public class PacService extends AbstractModule implements PacServiceLocal final Pac bo = findPacByName(vo.getName()); final String customerName = prototype.getCustomer(); if (customerName != null && !customerName.isEmpty()) { - final Customer customer = findCustomerByName(customerName); + final Customer customer = customerDao.findCustomerByName(customerName); bo.setCustomer(customer); } prototype.copyPropertiesToPersistentObject(bo); @@ -162,11 +167,4 @@ public class PacService extends AbstractModule implements PacServiceLocal query.setParameter("name", value); 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(); - } - }