/**
  * Callback method called when the Form mode is changed.
  *
  * @param currentMode current Form mode
  */
 public void modeChanged(int currentMode) {
   if (currentMode == Consts.INSERT) {
     frame.getGrid().clearData();
     frame.setEnableGridButtons(false);
   } else {
     frame.setEnableGridButtons(true);
   }
 }
  /**
   * Callback method called when the data loading is completed.
   *
   * @param error <code>true</code> if an error occours during data loading, <code>false</code> if
   *     data loading is successfully completed
   */
  public void loadDataCompleted(boolean error) {
    frame.getControlCurrency().setCurrencySymbol("$");
    frame.getControlCurrency().setDecimals(2);
    frame.getControlCurrency().setDecimalSymbol('.');
    frame.getControlCurrency().setGroupingSymbol(',');

    EmpVO vo = (EmpVO) frame.getMainPanel().getVOModel().getValueObject();
    frame.getGrid().getOtherGridParams().put("empCode", vo.getEmpCode());
    frame.getGrid().reloadData();
  }
  public EmpDetailFrameController(EmpGridFrame gridFrame, String pk, Connection conn) {
    this.gridFrame = gridFrame;
    this.pk = pk;
    this.conn = conn;
    frame = new EmpDetailFrame(conn, this);
    MDIFrame.add(frame);

    if (pk != null) {
      frame.getMainPanel().setMode(Consts.READONLY);
      frame.getMainPanel().reload();
    } else {
      frame.getMainPanel().setMode(Consts.INSERT);
    }
  }
  /**
   * Method called by the Form panel to delete existing data.
   *
   * @param persistentObject value object to delete
   * @return an ErrorResponse value object in case of errors, VOResponse if the operation is
   *     successfully completed
   */
  public Response deleteRecord(ValueObject persistentObject) throws Exception {
    PreparedStatement stmt = null;
    try {
      EmpVO vo = (EmpVO) persistentObject;

      // delete from WORKING_DAYS...
      stmt = conn.prepareStatement("delete from WORKING_DAYS where EMP_CODE=?");
      stmt.setString(1, vo.getEmpCode());
      stmt.execute();
      stmt.close();

      // delete from EMP...
      stmt = conn.prepareStatement("delete from EMP where EMP_CODE=?");
      stmt.setString(1, vo.getEmpCode());
      stmt.execute();
      gridFrame.reloadData();

      frame.getGrid().clearData();

      return new VOResponse(vo);
    } catch (SQLException ex) {
      ex.printStackTrace();
      return new ErrorResponse(ex.getMessage());
    } finally {
      try {
        stmt.close();
        conn.commit();
      } catch (SQLException ex1) {
      }
    }
  }
  /**
   * Method called by the Form panel to insert new data.
   *
   * @param newValueObject value object to save
   * @return an ErrorResponse value object in case of errors, VOResponse if the operation is
   *     successfully completed
   */
  public Response insertRecord(ValueObject newPersistentObject) throws Exception {
    // mapping between attributes and database fields...
    Map attribute2dbField = new HashMap();
    attribute2dbField.put("empCode", "EMP_CODE");
    attribute2dbField.put("firstName", "FIRST_NAME");
    attribute2dbField.put("lastName", "LAST_NAME");
    attribute2dbField.put("deptCode", "DEPT_CODE");
    attribute2dbField.put("taskCode", "TASK_CODE");
    attribute2dbField.put("sex", "SEX");
    attribute2dbField.put("hireDate", "HIRE_DATE");
    attribute2dbField.put("salary", "SALARY");
    attribute2dbField.put("note", "NOTE");

    Response res =
        QueryUtil.insertTable(conn, newPersistentObject, "EMP", attribute2dbField, "Y", "N", true);
    if (res.isError()) {
      conn.rollback();
      return res;
    } else {
      // insert 7 records in WORKING_DAYS, one for each day of week...
      PreparedStatement pstmt = null;
      try {
        pstmt = conn.prepareStatement("insert into WORKING_DAYS(EMP_CODE,DAY) values(?,?)");

        pstmt.setString(1, ((EmpVO) newPersistentObject).getEmpCode());
        pstmt.setInt(2, Calendar.SUNDAY);
        pstmt.execute();

        pstmt.setString(1, ((EmpVO) newPersistentObject).getEmpCode());
        pstmt.setInt(2, Calendar.MONDAY);
        pstmt.execute();

        pstmt.setString(1, ((EmpVO) newPersistentObject).getEmpCode());
        pstmt.setInt(2, Calendar.TUESDAY);
        pstmt.execute();

        pstmt.setString(1, ((EmpVO) newPersistentObject).getEmpCode());
        pstmt.setInt(2, Calendar.WEDNESDAY);
        pstmt.execute();

        pstmt.setString(1, ((EmpVO) newPersistentObject).getEmpCode());
        pstmt.setInt(2, Calendar.THURSDAY);
        pstmt.execute();

        pstmt.setString(1, ((EmpVO) newPersistentObject).getEmpCode());
        pstmt.setInt(2, Calendar.FRIDAY);
        pstmt.execute();

        pstmt.setString(1, ((EmpVO) newPersistentObject).getEmpCode());
        pstmt.setInt(2, Calendar.SATURDAY);
        pstmt.execute();

        conn.commit();

        frame
            .getGrid()
            .getOtherGridParams()
            .put("empCode", ((EmpVO) newPersistentObject).getEmpCode());
        frame.getGrid().reloadData();

        return res;
      } catch (Exception ex) {
        conn.rollback();
        return new ErrorResponse(ex.getMessage());
      } finally {
        try {
          pstmt.close();
        } catch (Exception ex1) {
        }
      }
    }

    /*
        // an alternative way: you can define your own business logic to store data at hand...
        PreparedStatement stmt = null;
        try {
          stmt = conn.prepareStatement("insert into EMP(EMP_CODE,FIRST_NAME,LAST_NAME,DEPT_CODE,TASK_CODE,SEX,HIRE_DATE,SALARY,NOTE) values(?,?,?,?,?,?,?,?,?)");
          EmpVO vo = (EmpVO)newPersistentObject;
          stmt.setString(1,vo.getEmpCode());
          stmt.setString(2,vo.getFirstName());
          stmt.setString(3,vo.getLastName());
          stmt.setString(4,vo.getDeptCode());
          stmt.setString(5,vo.getTaskCode());
          stmt.setString(6,vo.getSex());
          stmt.setDate(7,vo.getHireDate());
          stmt.setBigDecimal(8,vo.getSalary());
          stmt.setString(9,vo.getNote());

          stmt.execute();
          pk = vo.getEmpCode();
          gridFrame.reloadData();
          return new VOResponse(vo);
        }
        catch (SQLException ex) {
          ex.printStackTrace();
          return new ErrorResponse(ex.getMessage());
        }
        finally {
          try {
            stmt.close();
            conn.commit();
          }
          catch (SQLException ex1) {
          }
        }
    */
  }