public void validateUserAccountOnly(Users human) {

    if (human.getPassword() == null || human.getPassword().trim().compareTo("") == 0) {
      throw new ValidateInputException(ResourceMessages.getResource("user_not_enter_password"));
    }
    if (human.getRetypePassword() == null || human.getRetypePassword().trim().compareTo("") == 0) {
      throw new ValidateInputException(
          ResourceMessages.getResource("user_not_enter_retype_password"));
    }
    if (human.getRetypePassword().toLowerCase().compareTo(human.getPassword().toLowerCase()) != 0) {
      throw new ValidateInputException(ResourceMessages.getResource("user_password_not_march"));
    }
    if (human.getUserId() == null) {
      DataValidator.validateUsernameAsEmail(human.getUsername());
      Users u = this.userService.getUserByUsername(human.getUsername());
      if (u != null) {
        //                if (human.getUserId() == null) { // them moi user name da ton tai
        throw new ValidateInputException(ResourceMessages.getResource("user_username_exist"));
        //                }

        //                if (u.getUserId().longValue() != human.getUserId().longValue()) {
        //                    throw new
        // ValidateInputException(ResourceMessages.getResource("user_username_exist"));
        //                }
      }
    }

    human.setPassword(HashData.hashDocument(human.getPassword()));
  }
  @Override
  public void saveHuman(HumanDto human, Organization organization, HumanType humanType) {

    validateHuman(human, organization, humanType);

    AuthenticationController authenticationController =
        ControllerUtils.getBean(ControllerName.AUTHENTICATION);
    boolean isupdate = true;
    if (human.getHumanId() == null) {
      isupdate = false;
      human.setHumanId(humanService.getSequence(SystemDefine.SEQUENCE_STAFF_ID).longValue());
    }
    if (human.getUsername() != null
        && human.getUsername().trim().compareTo("") != 0
        && human.getUserId() == null) {
      human.setUserId(getSequence(SystemDefine.SEQUENCE_USER_ID).longValue());
      human.setCreateDate(new Date());
      human.setLoginFailCounter(0);
    }
    if (humanType.toInteger() == HumanType.RETAIL_CUSTOMER.toInteger()) {
      human.setOrganizationId(authenticationController.getOrganization().getOrganizationId());
      human.setOrgRootId(authenticationController.getOrganization().getRootId());
    } else {
      human.setOrganizationId(organization.getOrganizationId());
      human.setOrgRootId(organization.getRootId());
    }
    Human saveHuman = human.cloneHuman();
    Date today = new Date();
    Human modifiedHuman = AuthenticationController.getCurrentHuman();
    Long modifiedHumanId = modifiedHuman == null ? null : modifiedHuman.getHumanId();
    if (!isupdate) {
      saveHuman.setCreateDate(today);
      if (modifiedHuman != null) {
        saveHuman.setCreateStaffId(modifiedHumanId);
      }
    }

    saveHuman.setModifiedDate(today);
    if (modifiedHuman != null) {
      saveHuman.setCreateStaffId(modifiedHumanId);
    }

    this.humanService.saveEntity(saveHuman);

    if (human.getUserId() != null) {
      this.userService.saveEntity(human.cloneUser());
    }
    if (!isupdate) {
      ControllerUtils.addSuccessMessage(ResourceMessages.getResource("create_user_success"));
    } else {
      ControllerUtils.addSuccessMessage(ResourceMessages.getResource("update_user_success"));
    }
    UserController userController = ControllerUtils.getBean(ControllerName.USER_CONTROLLER);
    userController.setHuman4InitPermission(human);
  }
  public void validateHumanOnly(HumanDto human, Organization organization, HumanType humanType) {
    if (((organization == null || organization.getOrganizationId() == null)
        && humanType.toInteger()
            == HumanType.STAFF.toInteger()) // chi check truong hop khong quan ly mo hinh to chuc
    ) {
      throw new ValidateInputException(
          ResourceMessages.getResource("user_not_choice_organization"));
    }
    if (human.getFullName() == null || human.getFullName().trim().compareTo("") == 0) {
      throw new ValidateInputException(ResourceMessages.getResource("user_not_enter_fullname"));
    }
    if (human.getGender() == -1) {
      throw new ValidateInputException(ResourceMessages.getResource("user_choice_gender"));
    }
    if (human.getBirthday() == null) {
      throw new ValidateInputException(ResourceMessages.getResource("user_enter_brithday"));
    } else if (!human.getBirthday().before(new Date())) {
      throw new ValidateInputException(
          ResourceMessages.getResource("user_enter_brithday_invalide"));
    }

    if (human.getEmailAddress() != null || human.getEmailAddress().trim().compareTo("") != 0) {
      human.setEmailAddress(human.getEmailAddress().trim());
      DataValidator.validateEmailAdress(human.getEmailAddress());
      if (humanType.toInteger() == HumanType.RETAIL_CUSTOMER.toInteger()
          && humanService.validateCustomerByEmail(
              human.getEmailAddress(),
              AuthenticationController.getCurrentRootOrg(),
              human.getHumanId())) {
        MessagesExceptionUtils.addErrorMessages(
            "Địa chỉ email "
                + human.getEmailAddress()
                + " đã tồn tại. Vui lòng sử dụng địa chỉ email khác.");
      } else if (humanService.validateHumanByEmail(
          human.getEmailAddress(),
          AuthenticationController.getCurrentRootOrg(),
          human.getHumanId())) {
        MessagesExceptionUtils.addErrorMessages(
            "Địa chỉ email "
                + human.getEmailAddress()
                + " đã tồn tại. Vui lòng sử dụng địa chỉ email khác.");
      }
    }

    human.setFullName(DataValidator.deleteSpace(DataValidator.standardName(human.getFullName())));
    human.setAddress(DataValidator.deleteSpace(DataValidator.standardName(human.getAddress())));
  }
  public void saveUserOnly(HumanDto human) {

    boolean isupdate = true;
    Users user = human.cloneUser();
    if (user.getUserId() == null) {
      validateUserAccountOnly(user);
      user.setUserId(getSequence(SystemDefine.SEQUENCE_USER_ID).longValue());
      user.setCreateDate(new Date());
      user.setLoginFailCounter(0);
      isupdate = false;
    }

    if (!isupdate) {
      ControllerUtils.addSuccessMessage(ResourceMessages.getResource("create_account_success"));
      this.userService.persistEntity(user);
    } else {
      this.userService.updateUser(user);
      ControllerUtils.addSuccessMessage(ResourceMessages.getResource("update_account_success"));
    }
    UserController userController = ControllerUtils.getBean(ControllerName.USER_CONTROLLER);
    userController.setHuman4InitPermission(human);
  }
  @Override
  public void updateUserPassword(Users user) {
    validateUserAccountOnly(user);
    StringBuilder sql =
        new StringBuilder(
            "update Users user set "
                + "user.password = :password"
                + ", user.mandatoryResetPass = :mandatoryResetPass"
                + " where user.userId = :userId");

    Query query = em.createQuery(sql.toString());
    query.setParameter("password", user.getPassword());
    query.setParameter("mandatoryResetPass", user.getMandatoryResetPass());
    query.setParameter("userId", user.getUserId());

    query.executeUpdate();
    ControllerUtils.addSuccessMessage(
        ResourceMessages.getResource("update_account_password_success"));
  }
  public void validateHuman(HumanDto human, Organization organization, HumanType humanType) {
    if (((organization == null || organization.getOrganizationId() == null)
        && humanType.toInteger()
            == HumanType.STAFF.toInteger()) // chi check truong hop khong quan ly mo hinh to chuc
    ) {
      throw new ValidateInputException(
          ResourceMessages.getResource("user_not_choice_organization"));
    }
    if (human.getFullName() == null || human.getFullName().trim().compareTo("") == 0) {
      throw new ValidateInputException(ResourceMessages.getResource("user_not_enter_fullname"));
    }
    if (human.getGender() == -1) {
      throw new ValidateInputException(ResourceMessages.getResource("user_choice_gender"));
    }
    if (human.getBirthday() == null) {
      throw new ValidateInputException(ResourceMessages.getResource("user_enter_brithday"));
    } else if (!human.getBirthday().before(new Date())) {
      throw new ValidateInputException(
          ResourceMessages.getResource("user_enter_brithday_invalide"));
    }

    if (human.getUsername() != null && human.getUsername().trim().compareTo("") != 0) {
      if (human.getUserId() == null
          || (human.getUserId() != null
              && ((human.getPassword() != null && human.getPassword().compareTo("") != 0)
                  || (human.getRetypePassword() != null)
                      && human.getRetypePassword().compareTo("") != 0))) {
        if (human.getUserId() == null) {
          DataValidator.validateUsername(human.getUsername());
        }

        if (human.getPassword() == null || human.getPassword().trim().compareTo("") == 0) {
          throw new ValidateInputException(ResourceMessages.getResource("user_not_enter_password"));
        }
        if (human.getRetypePassword() == null
            || human.getRetypePassword().trim().compareTo("") == 0) {
          throw new ValidateInputException(
              ResourceMessages.getResource("user_not_enter_retype_password"));
        }
        if (human.getRetypePassword().toLowerCase().compareTo(human.getPassword().toLowerCase())
            != 0) {
          throw new ValidateInputException(ResourceMessages.getResource("user_password_not_march"));
        }
      }
    }

    //        if (human.getAddress() == null || human.getAddress().trim().compareTo("") == 0) {
    //            throw new
    // ValidateInputException(ResourceMessages.getResource("not_enter_address"));
    //        }
    if (human.getEmailAddress() != null || human.getEmailAddress().trim().compareTo("") != 0) {
      human.setEmailAddress(human.getEmailAddress().trim());
      DataValidator.validateEmailAdress(human.getEmailAddress());
      if (humanType.toInteger() == HumanType.RETAIL_CUSTOMER.toInteger()
          && humanService.validateCustomerByEmail(
              human.getEmailAddress(),
              AuthenticationController.getCurrentRootOrg(),
              human.getHumanId())) {
        MessagesExceptionUtils.addErrorMessages(
            "Địa chỉ email "
                + human.getEmailAddress()
                + " đã tồn tại. Vui lòng sử dụng địa chỉ email khác.");
      } else if (humanService.validateHumanByEmail(
          human.getEmailAddress(),
          AuthenticationController.getCurrentRootOrg(),
          human.getHumanId())) {
        MessagesExceptionUtils.addErrorMessages(
            "Địa chỉ email "
                + human.getEmailAddress()
                + " đã tồn tại. Vui lòng sử dụng địa chỉ email khác.");
      }
    }

    human.setFullName(DataValidator.deleteSpace(DataValidator.standardName(human.getFullName())));
    human.setAddress(DataValidator.deleteSpace(DataValidator.standardName(human.getAddress())));

    if (human.getUsername() != null && human.getUsername().trim().compareTo("") != 0) {
      Users u = this.userService.getUserByUsername(human.getUsername());
      if (u != null) {
        if (human.getUserId() == null) { // them moi user name da ton tai
          throw new ValidateInputException(ResourceMessages.getResource("user_username_exist"));
        }

        if (u.getUserId().longValue() != human.getUserId().longValue()) {
          throw new ValidateInputException(ResourceMessages.getResource("user_username_exist"));
        }
      }
      if (u == null) { // them moi thi ma hoa password luon
        human.setPassword(HashData.hashDocument(human.getPassword()));
      } else if (human.getPassword() != null
          && human.getPassword().compareTo("")
              != 0) { // ko phai them moi thi phai kiem tra xem password co khac nhau ko
        human.setPassword(HashData.hashDocument(human.getPassword()));
      } else {
        human.setPassword(u.getPassword());
      }
    }
  }