@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())));
  }
  private void buildQuery(StringBuffer sql, Map<String, Object> filters) {
    String path = (String) filters.get(IHumanDtoService.USER_PARENT_PATH);
    String orgId = (String) filters.get(IHumanDtoService.USER_ORGNAZATION_ID);
    String humanType = (String) filters.get(IHumanDtoService.STAFF_TYPE);
    String orgRootId = (String) filters.get(IHumanDtoService.USER_ORGNAZATION_ROOT_ID);
    String fullName = (String) filters.get(IHumanDtoService.USER_FULLNAME);
    String email = (String) filters.get(IHumanDtoService.USER_EMAIL);
    String birthday = (String) filters.get(IHumanDtoService.USER_BITHDAY);
    String username = (String) filters.get(IHumanDtoService.USER_USERNAME);
    String tel = (String) filters.get(IHumanDtoService.USER_TEL);
    String gender = (String) filters.get(IHumanDtoService.USER_GENDER);
    String objectType = (String) filters.get(IHumanDtoService.ORG_TYPE);
    AuthenticationController controller = ControllerUtils.getBean(ControllerName.AUTHENTICATION);

    if (fullName != null && fullName.trim().compareTo("") != 0) {
      sql.append(" and s.full_name like '%").append(fullName.trim()).append("%'");
    }
    if (gender != null && gender.trim().compareTo("-1") != 0) {
      sql.append(" and s.gender = '").append(gender).append("'");
    }
    if (tel != null && tel.trim().compareTo("") != 0) {
      sql.append(" and s.tel = '").append(tel.trim()).append("'");
    }
    if (email != null && email.trim().compareTo("") != 0) {
      sql.append(" and s.email_address = '").append(email.trim()).append("'");
    }
    if (username != null && username.trim().compareTo("") != 0) {
      sql.append(" and u.username = '******'");
    }

    if (birthday != null) {
      sql.append(" and s.birthday = ?1");
    }

    if (orgId == null) {
      //            orgId = controller.getOrganization().getOrganizationId().toString();
      path = controller.getOrganization().getPath();
    }
    // lay customer ra
    if (humanType != null && Integer.valueOf(humanType) == HumanType.RETAIL_CUSTOMER.toInteger()) {
      sql.append(
          ", organization org  where s.org_root_id = org.root_id and "
              + " (( org.org_type = 2 and (s.user_type = 1 or s.user_type =2)) "
              + " OR (org.org_type != 2 and s.user_type = 3)) ");
      sql.append(" AND org.path LIKE '").append(path).append("%' ");
    } else {

      sql.append(" , organization org  where org.organization_id = s.organization_id");
      if (path != null) {
        sql.append(" and org.path like '").append(path).append("%' ");
      }
      sql.append(" and s.org_root_id = ").append(orgRootId);

      //            int objectTypeInt = Integer.valueOf(objectType).intValue();
      //            switch (objectTypeInt) {
      //                case 1://ObjectType.TYPE_STAFF
      //                    sql.append(" and org.org_type =
      // ").append(OrgType.ORG_H2J_CENTER.toInteger());
      //                    break;
      //                case 2://ObjectType.ORG_CUSTOMER neu la kieu khach hang, se gom ca khach lẻ,
      // khach hang thuoc dai ly; nha cung cap vao
      //                    sql.append(" and org.org_type =
      // ").append(OrgType.ORG_CUSTOMER.toInteger());
      //                    break;
      //                case 3://ObjectType.SUPPLIER
      //                    sql.append(" and org.org_type = ").append(OrgType.SUPPLIER.toInteger());
      //                    break;
      //                case 4://ObjectType.AGENCY
      //                    sql.append(" and org.org_type = ").append(OrgType.AGENCY.toInteger());
      //                    break;
      //                default:
      //                    sql.append(" and org.org_type =
      // ").append(controller.getOrganization().getOrgType());
      //                    break;
      //            }
      if (humanType != null) {
        if (humanType.compareTo(HumanType.STAFF.toString()) == 0) {
          sql.append(" and (s.user_type = ")
              .append(HumanType.STAFF.toString())
              .append(" or s.user_type = ")
              .append(HumanType.DEPUTY_STAFF.toString())
              .append(")");
        } else {
          sql.append(" and s.user_type = ").append(humanType);
        }
      }
      //            // role admin thi tim trong to chuc cha
      //            if (orgRootId != null) {
      //                sql.append(" and s.org_root_id = ").append(orgRootId);
      //            } else if (controller.getOrganization().getOrgType() != null ||
      // controller.getOrganization().getOrgType() != OrgType.ORG_H2J_CENTER.toInteger()) {
      //                sql.append(" and s.org_root_id =
      // ").append(controller.getOrganization().getRootId());
      //            }
    }
  }
  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());
      }
    }
  }