initialize first contact with all customer-roles

This commit is contained in:
Peter Hormanns 2017-12-29 19:14:43 +01:00
parent c4f3025c80
commit c4856fccfb

View File

@ -1,6 +1,8 @@
package de.hsadmin.service.customer; package de.hsadmin.service.customer;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.ejb.Stateless; import javax.ejb.Stateless;
@ -9,6 +11,7 @@ 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.dao.customer.CustomerDao;
@ -39,7 +42,22 @@ 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(customerDao.findCustomerByName(prototype.getCustomer())); final Customer cust = customerDao.findCustomerByName(prototype.getCustomer());
final Set<Contact> existingContacts = cust.getContacts();
final String[] givenRoles = vo.getRoles();
if ((existingContacts == null || existingContacts.isEmpty()) && (givenRoles == null || givenRoles.length == 0)) {
final Query query = entityManager.createNativeQuery("SELECT role_name FROM role");
final List<?> rolesList = query.getResultList();
final List<String> roles = new ArrayList<>();
for (int idx = 0; idx < rolesList.size(); idx++) {
final Object role = rolesList.get(idx);
if (role instanceof String) {
roles.add((String) role);
}
}
bo.setRoles(roles);
}
bo.setCustomer(cust);
vo.copyPropertiesToPersistentObject(bo); vo.copyPropertiesToPersistentObject(bo);
entityManager.persist(bo); entityManager.persist(bo);
final Contact newBO = findContactByNames(vo); final Contact newBO = findContactByNames(vo);