예제 #1
0
  public static boolean deleteRecordByCode(String code, int objectType) {
    // TODO by the candidate
    /*
     * This method is called when you click delete button on an edit view
     * the code parameter is the code of (Customer - PRoduct ) or order
     * number of Sales Order and the type is identifier of the object type
     * and may be TYPE_PRODUCT , TYPE_CUSTOMER or TYPE_SALESORDER
     */

    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    try {
      switch (objectType) {
        case TYPE_CUSTOMER:
          Customer c = new Customer();
          c.setCode(code);
          session.delete(c);
          break;
        case TYPE_PRODUCT:
          Product p = new Product();
          p.setCode(code);
          session.delete(p);
          break;
        case TYPE_SALESORDER:
          SalesOrder s = (SalesOrder) session.get(SalesOrder.class, code);
          if (s != null) {
            for (OrderLines ol : s.getOrderLinesList()) {
              session.delete(ol);
            }
            session.delete(s);
          }
          break;
        default:
          break;
      }
      session.flush();
      session.getTransaction().commit();
      return true;
    } catch (HibernateException e) {
      logger.error("Error While Deleting", e);
      session.getTransaction().rollback();
    }
    return false;
  }