@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 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"));
  }
  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());
      //            }
    }
  }