Advertisement
hsianghui

IS3106 Lab1 CustomerSession.java

Aug 27th, 2017 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. package session;
  2.  
  3. import entity.Contact;
  4. import entity.Customer;
  5. import entity.Field;
  6. import error.NoResultException;
  7. import java.util.ArrayList;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Set;
  11. import javax.ejb.Stateless;
  12. import javax.persistence.EntityManager;
  13. import javax.persistence.PersistenceContext;
  14. import javax.persistence.Query;
  15.  
  16. @Stateless
  17. public class CustomerSession implements CustomerSessionLocal {
  18.  
  19.     @PersistenceContext
  20.     private EntityManager em;
  21.  
  22.     @Override
  23.     public List<Customer> searchCustomers(String name) {
  24.         Query q;
  25.         if (name != null) {
  26.             q = em.createQuery("SELECT c FROM Customer c WHERE "
  27.                     + "LOWER(c.name) LIKE :name");
  28.             q.setParameter("name", "%" + name.toLowerCase() + "%");
  29.         } else {
  30.             q = em.createQuery("SELECT c FROM Customer c");
  31.         }
  32.  
  33.         return q.getResultList();
  34.     } //end searchCustomers
  35.  
  36.     @Override
  37.     public Customer getCustomer(Long cId) throws NoResultException {
  38.         Customer cust = em.find(Customer.class, cId);
  39.  
  40.         if (cust != null) {
  41.             return cust;
  42.         } else {
  43.             throw new NoResultException("Not found");
  44.         }
  45.     } //end getCustomer
  46.  
  47.     @Override
  48.     public void createCustomer(Customer c) {
  49.         em.persist(c);
  50.     } //end createCustomer
  51.  
  52.     @Override
  53.     public void updateCustomer(Customer c) throws NoResultException {
  54.         Customer oldC = em.find(Customer.class, c.getId());
  55.  
  56.         if (oldC != null) {
  57.             oldC.setDob(c.getDob());
  58.             oldC.setGender(c.getGender());
  59.             oldC.setName(c.getName());
  60.         } else {
  61.             throw new NoResultException("Not found");
  62.         }
  63.     } //end updateCustomer
  64.  
  65.     @Override
  66.     public void addContact(Long cId, Contact c) throws NoResultException {
  67.         Customer cust = em.find(Customer.class, cId);
  68.  
  69.         if (cust != null) {
  70.             em.persist(c);
  71.  
  72.             if (c.getPhone() != null) {
  73.                 c.setPhone(c.getPhone().trim());
  74.             }
  75.  
  76.             if (c.getEmail() != null) {
  77.                 c.setEmail(c.getEmail().trim());
  78.             }
  79.  
  80.             cust.getContacts().add(c);
  81.         } else {
  82.             throw new NoResultException("Not found");
  83.         }
  84.     } //end addContact
  85.  
  86.     @Override
  87.     public void addField(Long cId, Field f) throws NoResultException {
  88.         Customer cust = em.find(Customer.class, cId);
  89.  
  90.         if (cust != null) {
  91.             f.setName(f.getName().trim());
  92.             f.setFieldValue(f.getFieldValue().trim());
  93.  
  94.             Query q = em.createQuery("SELECT f FROM Field f WHERE LOWER(f.name) = :name AND LOWER(f.fieldValue) = :fieldValue");
  95.             q.setParameter("name", f.getName().toLowerCase());
  96.             q.setParameter("fieldValue", f.getFieldValue().toLowerCase());
  97.  
  98.             try {
  99.                 Field found = (Field) q.getSingleResult();
  100.                 f = found;
  101.             } catch (Exception e) {
  102.                 //not found
  103.                 em.persist(f);
  104.             }
  105.  
  106.             //should check that the field is not found
  107.             if (!cust.getFields().contains(f)) {
  108.                 cust.getFields().add(f);
  109.             }
  110.         } else {
  111.             throw new NoResultException("Not found");
  112.         }
  113.     } //end addField
  114.  
  115.     @Override
  116.     public void deleteContact(Long cId) throws NoResultException {
  117.         Contact contact = em.find(Contact.class, cId);
  118.  
  119.         if (contact != null) {
  120.             Query q = em.createQuery("SELECT c FROM Customer c WHERE :contact MEMBER OF c.contacts");
  121.             q.setParameter("contact", contact);
  122.  
  123.             for (Object cust : q.getResultList()) {
  124.                 Customer cust1 = (Customer) cust;
  125.                 cust1.getContacts().remove(contact);
  126.             }
  127.  
  128.             em.remove(contact);
  129.         } else {
  130.             throw new NoResultException("Not found");
  131.         }
  132.     } //end deleteContact
  133.  
  134.     @Override
  135.     public void deleteField(Long cId, Long fId) throws NoResultException {
  136.         Customer cust = em.find(Customer.class, cId);
  137.         Field f = em.find(Field.class, fId);
  138.  
  139.         if (cust != null && f != null) {
  140.             cust.getFields().remove(f);
  141.  
  142.             //if no other association between field and customer, we are safe to delete this field
  143.             Query q = em.createQuery("SELECT count(c) FROM Customer c WHERE :field MEMBER OF c.fields");
  144.             q.setParameter("field", f);
  145.  
  146.             long count = (Long) q.getSingleResult();
  147.  
  148.             if (count == 0) {
  149.                 em.remove(f);
  150.             }
  151.         } else {
  152.             throw new NoResultException("Not found");
  153.         }
  154.     } //end deleteField
  155.  
  156.     @Override
  157.     public void deleteCustomer(Long cId) throws NoResultException {
  158.         Customer cust = em.find(Customer.class, cId);
  159.  
  160.         if (cust == null) {
  161.             throw new NoResultException("Not found");
  162.         }
  163.  
  164.         List<Field> fields = cust.getFields();
  165.         cust.setFields(null);
  166.  
  167.         for (Field f : fields) {
  168.             //if no other association between field and customer, we are safe to delete this field
  169.             Query q = em.createQuery("SELECT count(c) FROM Customer c WHERE :field MEMBER OF c.fields");
  170.             q.setParameter("field", f);
  171.  
  172.             long count = (Long) q.getSingleResult();
  173.  
  174.             if (count == 0) {
  175.                 em.remove(f);
  176.             }
  177.         }
  178.  
  179.         em.remove(cust);
  180.     } //end deleteCustomer
  181.  
  182.     @Override
  183.     public List<Customer> searchCustomersByContact(Contact c) {
  184.         Query q;
  185.         if (c.getPhone() != null) {
  186.             q = em.createQuery("SELECT DISTINCT cust FROM Customer cust, Contact c  WHERE c MEMBER OF cust.contacts AND LOWER(c.phone) LIKE :phone");
  187.             q.setParameter("phone", "%" + c.getPhone().toLowerCase() + "%");
  188.         } else if (c.getEmail() != null) {
  189.             q = em.createQuery("SELECT DISTINCT cust FROM Customer cust, Contact c  WHERE c MEMBER OF cust.contacts AND LOWER(c.email) LIKE :email");
  190.             q.setParameter("email", "%" + c.getEmail().toLowerCase() + "%");
  191.         } else {
  192.             return new ArrayList<Customer>();
  193.         }
  194.  
  195.         return q.getResultList();
  196.     } //end searchCustomersByContact
  197.  
  198.     @Override
  199.     public List<Customer> searchCustomersByField(Field f) {
  200.         Query q = em.createQuery("SELECT DISTINCT cust FROM Customer cust, Field f  WHERE f MEMBER OF cust.fields AND LOWER(f.name) = :name AND LOWER(f.fieldValue) LIKE :fieldValue");
  201.  
  202.         q.setParameter("name", f.getName().toLowerCase());
  203.         q.setParameter("fieldValue", "%" + f.getFieldValue().toLowerCase() + "%");
  204.  
  205.         return q.getResultList();
  206.     } //end searchCustomersByField
  207.  
  208.     @Override
  209.     public Set<String> getAllFieldNames() {
  210.         Set<String> results = new HashSet<String>();
  211.         Query q = em.createQuery("SELECT f FROM Field f");
  212.  
  213.         for (Object field : q.getResultList()) {
  214.             Field field1 = (Field) field;
  215.             results.add(field1.getName().toLowerCase());
  216.         }
  217.  
  218.         return results;
  219.     } //end getAllFieldNames
  220. }
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement