Advertisement
hsianghui

IS3106 Lab3 (CustomerSession.java)

Oct 8th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.43 KB | None | 0 0
  1. package session;
  2.  
  3. import entity.Contact;
  4. import entity.Customer;
  5. import entity.Field;
  6. import entity.CustomerGroup;
  7. import error.InvalidActionException;
  8. import error.NoResultException;
  9. import java.util.ArrayList;
  10. import java.util.HashSet;
  11. import java.util.List;
  12. import java.util.Set;
  13. import javax.ejb.Stateless;
  14. import javax.persistence.EntityManager;
  15. import javax.persistence.PersistenceContext;
  16. import javax.persistence.Query;
  17.  
  18. @Stateless
  19. public class CustomerSession implements CustomerSessionLocal {
  20.  
  21.     @PersistenceContext
  22.     private EntityManager em;
  23.  
  24.     @Override
  25.     public List<Customer> searchCustomers(String name) {
  26.         Query q;
  27.         if (name != null) {
  28.             q = em.createQuery("SELECT c FROM Customer c WHERE "
  29.                     + "LOWER(c.name) LIKE :name");
  30.             q.setParameter("name", "%" + name.toLowerCase() + "%");
  31.         } else {
  32.             q = em.createQuery("SELECT c FROM Customer c");
  33.         }
  34.  
  35.         return q.getResultList();
  36.     } //end searchCustomers
  37.  
  38.     @Override
  39.     public Customer getCustomer(Long cId) throws NoResultException {
  40.         Customer c = em.find(Customer.class, cId);
  41.  
  42.         if (c != null) {
  43.             return c;
  44.         } else {
  45.             throw new NoResultException("Not found");
  46.         }
  47.     } //end getCustomer
  48.  
  49.     @Override
  50.     public void createCustomer(Customer c) {
  51.         em.persist(c);
  52.     } //end createCustomer
  53.  
  54.     @Override
  55.     public void updateCustomer(Customer c) throws NoResultException {
  56.         Customer oldC = em.find(Customer.class, c.getId());
  57.  
  58.         if (oldC != null) {
  59.             oldC.setDob(c.getDob());
  60.             oldC.setGender(c.getGender());
  61.             oldC.setName(c.getName());
  62.         } else {
  63.             throw new NoResultException("Not found");
  64.         }
  65.     } //end updateCustomer
  66.  
  67.     @Override
  68.     public void addContact(Long cId, Contact c) throws NoResultException {
  69.         Customer cust = em.find(Customer.class, cId);
  70.  
  71.         if (cust != null) {
  72.             em.persist(c);
  73.             cust.getContacts().add(c);
  74.         } else {
  75.             throw new NoResultException("Not found");
  76.         }
  77.     } //end addContact
  78.  
  79.     @Override
  80.     public void addField(Long cId, Field f) throws NoResultException {
  81.         Customer cust = em.find(Customer.class, cId);
  82.  
  83.         if (cust != null) {
  84.             Query q = em.createQuery("SELECT f FROM Field f WHERE LOWER(f.name) = :name AND LOWER(f.value) = :value");
  85.             q.setParameter("name", f.getName().toLowerCase());
  86.             q.setParameter("value", f.getValue().toLowerCase());
  87.  
  88.             try {
  89.                 Field found = (Field) q.getSingleResult();
  90.                 f = found;
  91.             } catch (Exception e) {
  92.                 //not found
  93.                 em.persist(f);
  94.             }
  95.            
  96.             //only add if the field is not already there
  97.             if (!cust.getFields().contains(f)){
  98.                 cust.getFields().add(f);
  99.             }
  100.         } else {
  101.             throw new NoResultException("Not found");
  102.         }
  103.     } //end addField
  104.  
  105.     @Override
  106.     public void deleteContact(Long cId) throws NoResultException {
  107.         Contact c = em.find(Contact.class, cId);
  108.  
  109.         if (c != null) {
  110.             Query q = em.createQuery("SELECT c FROM Customer c WHERE :contact MEMBER OF c.contacts");
  111.             q.setParameter("contact", c);
  112.  
  113.             for (Object cust : q.getResultList()) {
  114.                 Customer cust1 = (Customer) cust;
  115.                 cust1.getContacts().remove(c);
  116.             }
  117.  
  118.             em.remove(c);
  119.         } else {
  120.             throw new NoResultException("Not found");
  121.         }
  122.     } //end deleteContact
  123.  
  124.     @Override
  125.     public void deleteField(Long cId, Long fId) throws NoResultException {
  126.         Customer c = em.find(Customer.class, cId);
  127.         Field f = em.find(Field.class, fId);
  128.  
  129.         if (c != null && f != null) {
  130.             c.getFields().remove(f);
  131.  
  132.             //if no other association between field and customer, we are safe to delete this field
  133.             Query q = em.createQuery("SELECT count(c) FROM Customer c WHERE :field MEMBER OF c.fields");
  134.             q.setParameter("field", f);
  135.  
  136.             long count = (Long) q.getSingleResult();
  137.  
  138.             if (count == 0) {
  139.                 em.remove(f);
  140.             }
  141.         } else {
  142.             throw new NoResultException("Not found");
  143.         }
  144.     } //end deleteField
  145.  
  146.     @Override
  147.     public void deleteCustomer(Long cId) throws NoResultException {
  148.         Customer c = em.find(Customer.class, cId);
  149.  
  150.         if (c == null) {
  151.             throw new NoResultException("Not found");
  152.         }
  153.  
  154.         List<Field> fields = c.getFields();
  155.         c.setFields(null);
  156.  
  157.         for (Field f : fields) {
  158.             //if no other association between field and customer, we are safe to delete this field
  159.             Query q = em.createQuery("SELECT count(c) FROM Customer c WHERE :field MEMBER OF c.fields");
  160.             q.setParameter("field", f);
  161.  
  162.             long count = (Long) q.getSingleResult();
  163.  
  164.             if (count == 0) {
  165.                 em.remove(f);
  166.             }
  167.         }
  168.  
  169.         em.remove(c);
  170.     } //end deleteCustomer
  171.  
  172.     @Override
  173.     public List<Customer> searchCustomersByContact(Contact c) {
  174.         Query q = null;
  175.         if (c.getPhone() != null) {
  176.             q = em.createQuery("SELECT cust FROM Customer cust, Contact c  WHERE c MEMBER OF cust.contacts AND LOWER(c.phone) LIKE :phone");
  177.             q.setParameter("phone", "%" + c.getPhone().toLowerCase() + "%");
  178.         } else if (c.getEmail() != null) {
  179.             q = em.createQuery("SELECT cust FROM Customer cust, Contact c  WHERE c MEMBER OF cust.contacts AND LOWER(c.email) LIKE :email");
  180.             q.setParameter("email", "%" + c.getEmail().toLowerCase() + "%");
  181.         } else {
  182.             return new ArrayList<Customer>();
  183.         }
  184.  
  185.         return q.getResultList();
  186.     } //end searchCustomersByContact
  187.  
  188.     @Override
  189.     public List<Customer> searchCustomersByField(Field f) {
  190.         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");
  191.  
  192.         q.setParameter("name", f.getName().toLowerCase());
  193.         q.setParameter("value", "%" + f.getValue().toLowerCase() + "%");
  194.  
  195.         return q.getResultList();
  196.     } //end searchCustomersByField
  197.  
  198.     @Override
  199.     public Set<String> getAllFieldNames() {
  200.         Set<String> results = new HashSet<String>();
  201.         Query q = em.createQuery("SELECT f FROM Field f");
  202.  
  203.         for (Object field : q.getResultList()) {
  204.             Field field1 = (Field) field;
  205.             results.add(field1.getName().toLowerCase());
  206.         }
  207.  
  208.         return results;
  209.     } //end listAllFieldNames
  210.  
  211.     @Override
  212.     public void createGroup(CustomerGroup g) {
  213.         em.persist(g);
  214.     } //end createGroup
  215.    
  216.     @Override
  217.     public CustomerGroup getGroup(Long gId) {
  218.         return em.find(CustomerGroup.class, gId);
  219.     } //end getGroup
  220.  
  221.     @Override
  222.     public List<CustomerGroup> getAllGroups() {
  223.         Query q = em.createQuery("SELECT g FROM CustomerGroup g");
  224.         return q.getResultList();
  225.     } //end getAllGroups
  226.  
  227.     @Override
  228.     public void updateGroup(CustomerGroup g) throws NoResultException {
  229.         CustomerGroup oldG = em.find(CustomerGroup.class, g.getId());
  230.  
  231.         if (oldG != null) {
  232.             oldG.setName(g.getName());
  233.         } else {
  234.             throw new NoResultException("Not found");
  235.         }
  236.     } //end updateGroup
  237.  
  238.     @Override
  239.     public void deleteGroup(Long gId) throws NoResultException, InvalidActionException {
  240.         CustomerGroup g = em.find(CustomerGroup.class, gId);
  241.  
  242.         if (g != null) {
  243.             if (g.getCustomers().size() > 0) {
  244.                 throw new InvalidActionException("Group still has customers");
  245.             } else {
  246.                 em.remove(g);
  247.             }
  248.         } else {
  249.             throw new NoResultException("Not found");
  250.         }
  251.     } //end deleteGroup
  252.  
  253.     @Override
  254.     public void addCustomersToGroup(Long gId, ArrayList<Long> cIds) throws NoResultException, InvalidActionException {
  255.         CustomerGroup g = em.find(CustomerGroup.class, gId);
  256.  
  257.         if (g != null) {
  258.             List<Customer> existingList = g.getCustomers();
  259.  
  260.             ArrayList<Customer> toAddCustomers = new ArrayList<Customer>();
  261.  
  262.             for (Long cId : cIds) {
  263.                 Customer c = em.find(Customer.class, cId);
  264.                 if (c != null) {
  265.                     toAddCustomers.add(c);
  266.                 } else {
  267.                     throw new InvalidActionException("Invalid customer");
  268.                 }
  269.             }
  270.  
  271.             for (Customer c : toAddCustomers) {
  272.                 if (!existingList.contains(c)) {
  273.                     existingList.add(c);
  274.                    
  275.                     //make sure update link on both side
  276.                     if (!c.getGroups().contains(g)){
  277.                         c.getGroups().add(g);
  278.                     }
  279.                 }
  280.             }
  281.         } else {
  282.             throw new NoResultException("Not found");
  283.         }
  284.     } //end addCustomersToGroup
  285.  
  286.     @Override
  287.     public void removeCustomersFromGroup(Long gId, ArrayList<Long> cIds) throws NoResultException, InvalidActionException {
  288.         CustomerGroup g = em.find(CustomerGroup.class, gId);
  289.  
  290.         if (g != null) {
  291.             List<Customer> existingList = g.getCustomers();
  292.  
  293.             ArrayList<Customer> toRemoveCustomers = new ArrayList<Customer>();
  294.  
  295.             if (existingList.size() > 0) {
  296.                 for (Long cId : cIds) {
  297.                     Customer c = em.find(Customer.class, cId);
  298.                     if (c != null) {
  299.                         toRemoveCustomers.add(c);
  300.                     } else {
  301.                         throw new InvalidActionException("Invalid customer");
  302.                     }
  303.                 }
  304.                
  305.                 for (Customer c : toRemoveCustomers) {
  306.                     existingList.remove(c);
  307.                    
  308.                     //make sure update link on both side
  309.                     if (c.getGroups().contains(g)){
  310.                         c.getGroups().remove(g);
  311.                     }
  312.                 }
  313.             }
  314.         } else {
  315.             throw new NoResultException("Not found");
  316.         }
  317.     } //end removeCustomersFromGroup
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement