Ejemplo n.º 1
0
  public void checkValid(DomainModel domainModel, Map values, Action action)
      throws ValidateException {
    Assert.notNull(domainModel, "PropertyMap对象不能为空");
    Assert.isInstanceOf(PropertyMap.class, domainModel, "属性非空有效性校验失败,传入的类型必须为PropertyMap!");
    Assert.notNull(values, "values不允许为空,请确认!");
    Assert.notNull(action, "在进行唯一性检测时传入的操作类型不能为空");

    PropertyMap pm = (PropertyMap) domainModel;
    if (pm.isUnique() == false) return; // 允许重复则不进行唯一性检测

    Object value = values.get(Validate.VALUE_CHECK_OBJ);
    Object pkValue = values.get(UniqueValidate.VALUE_CHECK_PK_OBJ); // 获取 主键值
    if (value == null || StringHelper.isNullString(value.toString())) return; // 如果是空值则不进行唯一性检测

    if (action instanceof AddAction) {
      String HQL =
          "from " + pm.getEntityMap().getEntityName() + " eo  where  " + genWhereClause(pm, value);
      logger.debug("=====================" + HQL + "============================");
      if (Udf.dbOperator.isExist(HQL)) {
        throw new ValidateException(
            "以[" + value + "]为值的字段[" + pm.getChineseName() + "]在系统中已经存在,不能重复添加!");
      }
    } else if (action instanceof ModifyAction) {
      String HQL =
          "from "
              + pm.getEntityMap().getEntityName()
              + " eo  where  "
              + genWhereClause(
                  pm.getEntityMap()
                      .getPropertyMap(pm.getEntityMap().getPKPropertyMap().getPropertyName()),
                  pkValue);
      logger.debug("=====================" + HQL + "============================");
      Object obj = Udf.dbOperator.findUniqueResult(HQL);
      Object oldValue =
          ReflectHelper.invoke(
              obj, "get" + StringHelper.converseFirstCharToUpper(pm.getPropertyName()));
      if (this.isEquals(pm, oldValue, value) == true) return; // 如果新旧两个值相等则不必进行校验,说明值没有被改变

      // 如果新旧两个值不相等,则说明新值已经发生改变,则从数据库中检测是否有重复的记录
      HQL =
          "from " + pm.getEntityMap().getEntityName() + " eo  where  " + genWhereClause(pm, value);
      logger.debug("=====================" + HQL + "============================");
      if (Udf.dbOperator.isExist(HQL)) {
        throw new ValidateException(
            "以[" + value + "]为值的字段[" + pm.getChineseName() + "]在系统中已经存在,不能修改成[" + value + "]");
      }
    }
  }
Ejemplo n.º 2
0
  public void checkValid(PropertyMap pm, Object value, Object pkValue, AbstractAction action)
      throws ValidateException {
    if (pm.isUnique() == false) return; // 允许重复则不进行唯一性检测
    if (value == null || StringHelper.isNullString(value.toString())) return; // 如果是空值则不进行唯一性检测

    Assert.notNull(pm, "PropertyMap对象不能为空");
    Assert.notNull(action, "在进行唯一性检测时传入的操作类型不能为空");

    if (action instanceof AddAction) {
      String HQL =
          "from " + pm.getEntityMap().getEntityName() + " eo  where  " + genWhereClause(pm, value);
      logger.debug("=====================" + HQL + "============================");
      if (Udf.dbOperator.isExist(HQL)) {
        throw new ValidateException(
            "以[" + value + "]为值的字段[" + pm.getChineseName() + "]在系统中已经存在,不能重复添加!");
      }
    } else if (action instanceof ModifyAction) {
      String HQL =
          "from "
              + pm.getEntityMap().getEntityName()
              + " eo  where  "
              + genWhereClause(
                  pm.getEntityMap()
                      .getPropertyMap(pm.getEntityMap().getPKPropertyMap().getPropertyName()),
                  pkValue);
      logger.debug("=====================" + HQL + "============================");
      Object obj = Udf.dbOperator.findUniqueResult(HQL);
      Object oldValue =
          ReflectHelper.invoke(
              obj, "get" + StringHelper.converseFirstCharToUpper(pm.getPropertyName()));
      if (this.isEquals(pm, oldValue, value) == true) return; // 如果新旧两个值相等则不必进行校验,说明值没有被改变

      // 如果新旧两个值不相等,则说明新值已经发生改变,则从数据库中检测是否有重复的记录
      HQL =
          "from " + pm.getEntityMap().getEntityName() + " eo  where  " + genWhereClause(pm, value);
      logger.debug("=====================" + HQL + "============================");
      if (Udf.dbOperator.isExist(HQL)) {
        throw new ValidateException(
            "以[" + value + "]为值的字段[" + pm.getChineseName() + "]在系统中已经存在,不能修改成[" + value + "]");
      }
    }
  }