diff --git a/cust-services/src/main/java/de/hsadmin/service/customer/MemberAssetService.java b/cust-services/src/main/java/de/hsadmin/service/customer/MemberAssetService.java new file mode 100644 index 0000000..825fb26 --- /dev/null +++ b/cust-services/src/main/java/de/hsadmin/service/customer/MemberAssetService.java @@ -0,0 +1,96 @@ +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 org.apache.commons.lang3.NotImplementedException; + +import de.hsadmin.bo.customer.MemberAsset; +import de.hsadmin.common.error.TechnicalException; +import de.hsadmin.common.error.UserException; +import de.hsadmin.dao.customer.CustomerDao; +import de.hsadmin.dao.customer.MemberAssetDao; +import de.hsadmin.login.RequestContext; +import de.hsadmin.login.RequiredScope; +import de.hsadmin.login.Role; +import de.hsadmin.login.ScopePolicy; +import de.hsadmin.module.impl.AbstractModule; +import de.hsadmin.module.util.QueryBuilder; + +@Stateless +public class MemberAssetService extends AbstractModule implements MemberAssetServiceLocal { + + @PersistenceContext(name="hsar") + private EntityManager entityManager; + + @EJB + private CustomerDao customerDao; + + @EJB + private MemberAssetDao memberAssetDao; + + public MemberAssetService() { + } + + public MemberAssetService(final EntityManager em, final CustomerDao customerDao, final MemberAssetDao memberAssetDao) { + this.entityManager = em; + this.customerDao = customerDao; + this.memberAssetDao = memberAssetDao; + } + + @Override + public MemberAssetVO buildVO() throws TechnicalException { + return new MemberAssetVO(); + } + + @Override + @RequiredScope({@ScopePolicy(Role.SYSTEM)}) + public MemberAssetVO create(final RequestContext requestContext, final MemberAssetVO prototype) + throws UserException, TechnicalException { + final MemberAssetVO vo = super.create(requestContext, prototype); + final MemberAsset bo = new MemberAsset(); + bo.setCustomer(customerDao.findCustomerByName(prototype.getCustomer())); + vo.copyPropertiesToPersistentObject(bo); + entityManager.persist(bo); + final MemberAssetVO newVO = new MemberAssetVO(); + newVO.copyPropertiesFromPersistentObject(bo); + return newVO; + } + + @Override + @RequiredScope({@ScopePolicy(Role.SYSTEM), @ScopePolicy(value=Role.CUSTOMER, property="customer")}) + public List read(final RequestContext requestContext, final MemberAssetVO criteria) + throws UserException, TechnicalException { + final List emptyList = super.read(requestContext, criteria); + final List list = runCriteriaQuery(criteria); + for (MemberAsset c : list) { + final MemberAssetVO vo = new MemberAssetVO(); + vo.copyPropertiesFromPersistentObject(c); + emptyList.add(vo); + } + return emptyList; + } + + @Override + @RequiredScope({@ScopePolicy(Role.SYSTEM)}) + public List update(final RequestContext requestContext, final MemberAssetVO criteria, final MemberAssetVO prototype) + throws UserException, TechnicalException { + throw new TechnicalException(new NotImplementedException("MemberAsset are immutable")); + } + + @Override + @RequiredScope({@ScopePolicy(Role.SYSTEM)}) + public void delete(final RequestContext requestContext, final MemberAssetVO criteria) + throws UserException, TechnicalException { + throw new TechnicalException(new NotImplementedException("MemberAsset are immutable")); + } + + // macht diese Klasse Unit-testbar + List runCriteriaQuery(final MemberAssetVO criteria) throws UserException, TechnicalException { + return QueryBuilder.newBuilder(entityManager, MemberAsset.class).getResultList(criteria); + } +}