Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package session;
- import entity.Contact;
- import entity.Customer;
- import entity.Field;
- import entity.CustomerGroup;
- import error.InvalidActionException;
- import error.NoResultException;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import javax.ejb.Stateless;
- import javax.persistence.EntityManager;
- import javax.persistence.PersistenceContext;
- import javax.persistence.Query;
- @Stateless
- public class CustomerSession implements CustomerSessionLocal {
- @PersistenceContext
- private EntityManager em;
- @Override
- public List<Customer> searchCustomers(String name) {
- Query q;
- if (name != null) {
- q = em.createQuery("SELECT c FROM Customer c WHERE "
- + "LOWER(c.name) LIKE :name");
- q.setParameter("name", "%" + name.toLowerCase() + "%");
- } else {
- q = em.createQuery("SELECT c FROM Customer c");
- }
- return q.getResultList();
- } //end searchCustomers
- @Override
- public Customer getCustomer(Long cId) throws NoResultException {
- Customer c = em.find(Customer.class, cId);
- if (c != null) {
- return c;
- } else {
- throw new NoResultException("Not found");
- }
- } //end getCustomer
- @Override
- public void createCustomer(Customer c) {
- em.persist(c);
- } //end createCustomer
- @Override
- public void updateCustomer(Customer c) throws NoResultException {
- Customer oldC = em.find(Customer.class, c.getId());
- if (oldC != null) {
- oldC.setDob(c.getDob());
- oldC.setGender(c.getGender());
- oldC.setName(c.getName());
- } else {
- throw new NoResultException("Not found");
- }
- } //end updateCustomer
- @Override
- public void addContact(Long cId, Contact c) throws NoResultException {
- Customer cust = em.find(Customer.class, cId);
- if (cust != null) {
- em.persist(c);
- cust.getContacts().add(c);
- } else {
- throw new NoResultException("Not found");
- }
- } //end addContact
- @Override
- public void addField(Long cId, Field f) throws NoResultException {
- Customer cust = em.find(Customer.class, cId);
- if (cust != null) {
- Query q = em.createQuery("SELECT f FROM Field f WHERE LOWER(f.name) = :name AND LOWER(f.value) = :value");
- q.setParameter("name", f.getName().toLowerCase());
- q.setParameter("value", f.getValue().toLowerCase());
- try {
- Field found = (Field) q.getSingleResult();
- f = found;
- } catch (Exception e) {
- //not found
- em.persist(f);
- }
- //only add if the field is not already there
- if (!cust.getFields().contains(f)){
- cust.getFields().add(f);
- }
- } else {
- throw new NoResultException("Not found");
- }
- } //end addField
- @Override
- public void deleteContact(Long cId) throws NoResultException {
- Contact c = em.find(Contact.class, cId);
- if (c != null) {
- Query q = em.createQuery("SELECT c FROM Customer c WHERE :contact MEMBER OF c.contacts");
- q.setParameter("contact", c);
- for (Object cust : q.getResultList()) {
- Customer cust1 = (Customer) cust;
- cust1.getContacts().remove(c);
- }
- em.remove(c);
- } else {
- throw new NoResultException("Not found");
- }
- } //end deleteContact
- @Override
- public void deleteField(Long cId, Long fId) throws NoResultException {
- Customer c = em.find(Customer.class, cId);
- Field f = em.find(Field.class, fId);
- if (c != null && f != null) {
- c.getFields().remove(f);
- //if no other association between field and customer, we are safe to delete this field
- Query q = em.createQuery("SELECT count(c) FROM Customer c WHERE :field MEMBER OF c.fields");
- q.setParameter("field", f);
- long count = (Long) q.getSingleResult();
- if (count == 0) {
- em.remove(f);
- }
- } else {
- throw new NoResultException("Not found");
- }
- } //end deleteField
- @Override
- public void deleteCustomer(Long cId) throws NoResultException {
- Customer c = em.find(Customer.class, cId);
- if (c == null) {
- throw new NoResultException("Not found");
- }
- List<Field> fields = c.getFields();
- c.setFields(null);
- for (Field f : fields) {
- //if no other association between field and customer, we are safe to delete this field
- Query q = em.createQuery("SELECT count(c) FROM Customer c WHERE :field MEMBER OF c.fields");
- q.setParameter("field", f);
- long count = (Long) q.getSingleResult();
- if (count == 0) {
- em.remove(f);
- }
- }
- em.remove(c);
- } //end deleteCustomer
- @Override
- public List<Customer> searchCustomersByContact(Contact c) {
- Query q = null;
- if (c.getPhone() != null) {
- q = em.createQuery("SELECT cust FROM Customer cust, Contact c WHERE c MEMBER OF cust.contacts AND LOWER(c.phone) LIKE :phone");
- q.setParameter("phone", "%" + c.getPhone().toLowerCase() + "%");
- } else if (c.getEmail() != null) {
- q = em.createQuery("SELECT cust FROM Customer cust, Contact c WHERE c MEMBER OF cust.contacts AND LOWER(c.email) LIKE :email");
- q.setParameter("email", "%" + c.getEmail().toLowerCase() + "%");
- } else {
- return new ArrayList<Customer>();
- }
- return q.getResultList();
- } //end searchCustomersByContact
- @Override
- public List<Customer> searchCustomersByField(Field f) {
- Query q = em.createQuery("SELECT cust FROM Customer cust, Field f WHERE f MEMBER OF cust.fields AND LOWER(f.name) = :name AND LOWER(f.value) LIKE :value");
- q.setParameter("name", f.getName().toLowerCase());
- q.setParameter("value", "%" + f.getValue().toLowerCase() + "%");
- return q.getResultList();
- } //end searchCustomersByField
- @Override
- public Set<String> getAllFieldNames() {
- Set<String> results = new HashSet<String>();
- Query q = em.createQuery("SELECT f FROM Field f");
- for (Object field : q.getResultList()) {
- Field field1 = (Field) field;
- results.add(field1.getName().toLowerCase());
- }
- return results;
- } //end listAllFieldNames
- @Override
- public void createGroup(CustomerGroup g) {
- em.persist(g);
- } //end createGroup
- @Override
- public CustomerGroup getGroup(Long gId) {
- return em.find(CustomerGroup.class, gId);
- } //end getGroup
- @Override
- public List<CustomerGroup> getAllGroups() {
- Query q = em.createQuery("SELECT g FROM CustomerGroup g");
- return q.getResultList();
- } //end getAllGroups
- @Override
- public void updateGroup(CustomerGroup g) throws NoResultException {
- CustomerGroup oldG = em.find(CustomerGroup.class, g.getId());
- if (oldG != null) {
- oldG.setName(g.getName());
- } else {
- throw new NoResultException("Not found");
- }
- } //end updateGroup
- @Override
- public void deleteGroup(Long gId) throws NoResultException, InvalidActionException {
- CustomerGroup g = em.find(CustomerGroup.class, gId);
- if (g != null) {
- if (g.getCustomers().size() > 0) {
- throw new InvalidActionException("Group still has customers");
- } else {
- em.remove(g);
- }
- } else {
- throw new NoResultException("Not found");
- }
- } //end deleteGroup
- @Override
- public void addCustomersToGroup(Long gId, ArrayList<Long> cIds) throws NoResultException, InvalidActionException {
- CustomerGroup g = em.find(CustomerGroup.class, gId);
- if (g != null) {
- List<Customer> existingList = g.getCustomers();
- ArrayList<Customer> toAddCustomers = new ArrayList<Customer>();
- for (Long cId : cIds) {
- Customer c = em.find(Customer.class, cId);
- if (c != null) {
- toAddCustomers.add(c);
- } else {
- throw new InvalidActionException("Invalid customer");
- }
- }
- for (Customer c : toAddCustomers) {
- if (!existingList.contains(c)) {
- existingList.add(c);
- //make sure update link on both side
- if (!c.getGroups().contains(g)){
- c.getGroups().add(g);
- }
- }
- }
- } else {
- throw new NoResultException("Not found");
- }
- } //end addCustomersToGroup
- @Override
- public void removeCustomersFromGroup(Long gId, ArrayList<Long> cIds) throws NoResultException, InvalidActionException {
- CustomerGroup g = em.find(CustomerGroup.class, gId);
- if (g != null) {
- List<Customer> existingList = g.getCustomers();
- ArrayList<Customer> toRemoveCustomers = new ArrayList<Customer>();
- if (existingList.size() > 0) {
- for (Long cId : cIds) {
- Customer c = em.find(Customer.class, cId);
- if (c != null) {
- toRemoveCustomers.add(c);
- } else {
- throw new InvalidActionException("Invalid customer");
- }
- }
- for (Customer c : toRemoveCustomers) {
- existingList.remove(c);
- //make sure update link on both side
- if (c.getGroups().contains(g)){
- c.getGroups().remove(g);
- }
- }
- }
- } else {
- throw new NoResultException("Not found");
- }
- } //end removeCustomersFromGroup
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement