/*
   * Add a new record to the Organization table,
   * creates an admin account when SA creates a new consulting company
   * @param OrganizationCode (based on Company Name)
   * @param OrganizationName (based on Company Description)
   * @param FKCompanyID
   * @param NameSequence
   * @param PKUser
   * @param nomRater
   * @throws SQLException
   * @throws Exception
   * @author: Mark Oei
   * @since v.1.3.12.63 09 Mar 2010
   */
  public boolean addOrganisationByCons(
      String OrganizationCode,
      String OrganizationName,
      int FKCompanyID,
      int NameSequence,
      int PKUser,
      String nomRater)
      throws SQLException, Exception {
    Connection con = null;
    Statement st = null;

    boolean bIsAdded = false;

    String sql =
        "INSERT INTO tblOrganization (OrganizationCode, OrganizationName, FKCompanyID, NameSequence, NominationModule)";
    sql =
        sql
            + " VALUES ('"
            + OrganizationCode
            + "', '"
            + OrganizationName
            + "', "
            + FKCompanyID
            + ", "
            + NameSequence
            + ", '"
            + Boolean.parseBoolean(nomRater)
            + "')";
    try {
      con = ConnectionBean.getConnection();
      st = con.createStatement();
      int iSuccess = st.executeUpdate(sql);
      System.out.println(iSuccess);
      if (iSuccess != 0) bIsAdded = true;
    } catch (Exception E) {
      System.err.println("Organization.java - AddRecord - " + E);
    } finally {
      ConnectionBean.closeStmt(st); // Close statement
      ConnectionBean.close(con); // Close connection
    }

    System.out.println("1. Add Organization");

    // add default under the organization.
    String defaultName = "NA";
    int FKOrganization = checkOrgExist(OrganizationCode, OrganizationName, FKCompanyID);
    // Change to disable print statement. Used for debugging only
    // Mark Oei 19 Mar 2010
    // System.out.println("testing " + FKOrganization);
    System.out.println("2. Check Organization Exist");
    if (FKOrganization != 0) {
      // Add Division
      div.addRecord(defaultName, FKOrganization, PKUser);
      System.out.println("3. Add Division");
      // Add Department
      dept.addRecord(defaultName, FKOrganization, PKUser);
      System.out.println("4. Add Department");
      // Add Group
      G.addRecord(defaultName, FKOrganization, PKUser);
      System.out.println("5. Add Group");
      // Check whether exists
      int FKDivision = div.checkDivExist(defaultName, FKOrganization);
      int FKDepartment = dept.checkDeptExist(defaultName, FKOrganization);
      int FKGroup = G.checkGroupExist(defaultName, FKOrganization);
      // Create links
      dept.linkDepartment(FKDivision, FKDepartment);
      G.linkGroup(FKDepartment, FKGroup);
      // Establish new admin account and password
      Date timeStamp = new java.util.Date();
      SimpleDateFormat dFormat = new SimpleDateFormat("ddMMyyHHmmss");
      String temp = dFormat.format(timeStamp);
      String loginName = OrganizationCode + "admin";
      String password = OrganizationCode + temp;
      int userType = 6;
      // Insert record into database
      U.addRecord(
          FKDepartment,
          FKDivision,
          userType,
          "Admin",
          "Admin",
          loginName,
          "NA",
          "NA",
          FKGroup,
          password,
          1,
          FKCompanyID,
          FKOrganization,
          "NA",
          PKUser);

      System.out.println("6. Add User");
      int userExist =
          U.checkUserExist(
              FKDepartment,
              FKDivision,
              userType,
              "Admin",
              "Admin",
              loginName,
              "NA",
              "NA",
              FKGroup,
              password,
              1,
              FKCompanyID,
              FKOrganization);

      System.out.println(
          "FKDivision = "
              + FKDivision
              + ", FKDepartment = "
              + FKDepartment
              + ", FKGroup = "
              + FKGroup
              + " and User Exist = "
              + userExist);

      if (userExist != 0) {
        try {
          U.insertRelation(userExist, userExist, 0);
        } catch (SQLException SE) {
          System.out.println(SE.getMessage());
        }
        // Send email notification
        String content = template.ForgotPass_temp(loginName, password);
        String email = "*****@*****.**";
        // Edited By Roger 13 June 2008
        Email.sendMail(
            server.getAdminEmail(),
            email,
            "New Admin Assignment for " + OrganizationName,
            content,
            FKOrganization);
      }

      System.out.println("8. Add User Relation");
    }

    sDetail = detail.getUserDetail(PKUser);
    ev.addRecord("Insert", itemName, OrganizationName, sDetail[2], sDetail[11], sDetail[10]);

    return bIsAdded;
  } // End Method for addOrganisationByCons
  // Changed by DeZ, 18/06/08, to add function to enable/disable Nominate Rater
  public boolean editRecord(
      int PKOrganization,
      String OrganizationCode,
      String OrganizationName,
      int FKCompanyID,
      int NameSequence,
      int PKUser,
      String nomRater)
      throws SQLException, Exception {
    String OldName = "";
    String command = "SELECT * FROM tblOrganization WHERE PKOrganization = " + PKOrganization;

    Connection con = null;
    Statement st = null;
    ResultSet rs = null;

    try {
      con = ConnectionBean.getConnection();
      st = con.createStatement();
      rs = st.executeQuery(command);

      if (rs.next()) {
        OldName = rs.getString("OrganizationName");
      }

      rs.close();
      rs = null;

    } catch (Exception E) {
      System.err.println("Organization.java - editRecord - " + E);
    } finally {
      ConnectionBean.closeRset(rs); // Close ResultSet
      ConnectionBean.closeStmt(st); // Close statement
      ConnectionBean.close(con); // Close connection
    }

    // Changed by DeZ, 18/06/08, to add function to enable/disable Nominate Rater
    String sql =
        "UPDATE tblOrganization SET OrganizationCode = '"
            + OrganizationCode
            + "', OrganizationName = '"
            + OrganizationName
            + "', FKCompanyID = "
            + FKCompanyID
            + ", NameSequence = "
            + NameSequence
            + ", NominationModule = '"
            + Boolean.parseBoolean(nomRater)
            + "'";
    sql = sql + " WHERE PKOrganization = " + PKOrganization;

    boolean bIsUpdated = false;

    try {

      con = ConnectionBean.getConnection();
      st = con.createStatement();
      int iSuccess = st.executeUpdate(sql);
      if (iSuccess != 0) bIsUpdated = true;

    } catch (Exception E) {
      System.err.println("Organization.java - editRecord - " + E);
    } finally {

      ConnectionBean.closeStmt(st); // Close statement
      ConnectionBean.close(con); // Close connection
    }

    sDetail = detail.getUserDetail(PKUser);
    try {
      ev.addRecord(
          "Update",
          itemName,
          "(" + OldName + ") - (" + OrganizationName + ")",
          sDetail[2],
          sDetail[11],
          sDetail[10]);
    } catch (SQLException SE) {
    }
    return bIsUpdated;
  }
  // Changed by DeZ, 18/06/08, to add function to enable/disable Nominate Rater
  public boolean addRecord(
      String OrganizationCode,
      String OrganizationName,
      int FKCompanyID,
      int NameSequence,
      int PKUser,
      String nomRater)
      throws SQLException, Exception {
    Connection con = null;
    Statement st = null;

    boolean bIsAdded = false;
    // Changed by DeZ, 18/06/08, to add function to enable/disable Nominate Rater
    String sql =
        "INSERT INTO tblOrganization (OrganizationCode, OrganizationName, FKCompanyID, NameSequence, NominationModule)";
    sql =
        sql
            + " VALUES ('"
            + OrganizationCode
            + "', '"
            + OrganizationName
            + "', "
            + FKCompanyID
            + ", "
            + NameSequence
            + ", '"
            + Boolean.parseBoolean(nomRater)
            + "')";
    try {
      con = ConnectionBean.getConnection();
      st = con.createStatement();
      int iSuccess = st.executeUpdate(sql);
      System.out.println(iSuccess);
      if (iSuccess != 0) bIsAdded = true;

    } catch (Exception E) {
      System.err.println("Organization.java - AddRecord - " + E);
    } finally {
      ConnectionBean.closeStmt(st); // Close statement
      ConnectionBean.close(con); // Close connection
    }

    /*
     * Codes commented to prevent addition of Admin account when a new
     * organisation is added
     * Mark Oei 09 Mar 2010
     */

    //		System.out.println("1. Add Organization");
    //
    //		// add default under the organization.
    //		String defaultName = "NA";
    //		int FKOrganization = checkOrgExist(OrganizationCode, OrganizationName, FKCompanyID);
    //		System.out.println("testing " + FKOrganization);
    //		System.out.println("2. Check Organization Exist");
    //		if(FKOrganization != 0) {
    //			div.addRecord(defaultName, FKOrganization, PKUser);
    //			System.out.println("3. Add Division");
    //
    //			dept.addRecord(defaultName, FKOrganization, PKUser);
    //			System.out.println("4. Add Department");
    //
    //			G.addRecord(defaultName, FKOrganization, PKUser);
    //			System.out.println("5. Add Group");
    //
    //			int FKDivision = div.checkDivExist(defaultName, FKOrganization);
    //			int FKDepartment = dept.checkDeptExist(defaultName, FKOrganization);
    //			int FKGroup = G.checkGroupExist(defaultName, FKOrganization);
    //
    //			/*System.out.println("FKDivision = " + FKDivision);
    //			System.out.println("FKDepartment = " + FKDepartment);
    //			System.out.println("FKGroup = " + FKGroup);*/
    //
    //			// Link newly created Department with Division
    //			dept.linkDepartment(FKDivision, FKDepartment);
    //			// Link newly created Group with Department
    //			G.linkGroup(FKDepartment, FKGroup);
    //
    //			Date timeStamp = new java.util.Date();
    //			SimpleDateFormat dFormat = new SimpleDateFormat("ddMMyyHHmmss");
    //			String temp  =  dFormat.format(timeStamp);
    //			String loginName = OrganizationCode + "admin";
    //			String password = OrganizationCode + temp;
    //			int userType = 6;
    //
    //			U.addRecord(FKDepartment, FKDivision, userType, "Admin", "Admin",
    //							loginName, "NA", "NA", FKGroup, password, 1, FKCompanyID, FKOrganization, "NA",
    // PKUser);
    //
    //			System.out.println("6. Add User");
    //			int userExist = U.checkUserExist(FKDepartment, FKDivision, userType, "Admin", "Admin",
    // loginName, "NA", "NA", FKGroup, password, 1, FKCompanyID, FKOrganization);
    //
    //			System.out.println("FKDivision = " + FKDivision + ", FKDepartment = " + FKDepartment + ",
    // FKGroup = " + FKGroup + " and User Exist = " + userExist);
    //
    //			if(userExist != 0) {
    //				try {
    //					U.insertRelation(userExist, userExist, 0);
    //				}catch(SQLException SE) {System.out.println(SE.getMessage());}
    //
    //				String content = template.ForgotPass_temp(loginName, password);
    //				String email = "*****@*****.**";
    //				//Edited By Roger 13 June 2008
    //				Email.sendMail(server.getAdminEmail(), email, "New Admin Assignment for " +
    // OrganizationName, content, FKOrganization);
    //			}
    //
    //			System.out.println("8. Add User Relation");
    //		}
    //
    //		sDetail = detail.getUserDetail(PKUser);
    //		ev.addRecord("Insert", itemName, OrganizationName, sDetail[2], sDetail[11], sDetail[10]);

    return bIsAdded;
  } // End Method for addRecord