Exemplo n.º 1
0
  public Object calculate() {

    Object tempObject = this.entityObject;
    boolean isAggregate = false;
    String sumcSplitStr = "";
    for (int i = 0; i < sumcEls.length; i++) {
      if (sumcEls[i] instanceof SumcElObject) { // 如果当前是一个SumcElObject,则上一个取得的对象肯定是一个集合对象,
        if (tempObject instanceof java.util.Collection) {
          Object[] objs = ((java.util.Collection) tempObject).toArray();
          StringBuffer buf = new StringBuffer();
          for (int j = 0; j < objs.length; j++) {
            SumcElObject tempSumcElObject = ((SumcElObject) sumcEls[i]);
            tempSumcElObject.setEntityObject(
                objs[j]); // 将当前集合取得的每个对象做为递归的SumcElObject的enitityObject以备计算之用
            buf.append(tempSumcElObject.calculate());
            if (j < objs.length - 1) buf.append(sumcSplitStr);
          }
          tempObject = buf.toString();
        } else {
          SumcElObject tempSumcElObject = ((SumcElObject) sumcEls[i]);
          tempSumcElObject.setEntityObject(
              tempObject); // 将当前集合取得的每个对象做为递归的SumcElObject的enitityObject以备计算之用
          tempObject = tempSumcElObject.calculate();
        }

      } else if (SumcElObject.isSumcAggregateExpression((String) sumcEls[i]) == true) {
        sumcSplitStr = getSumcSplitStr((String) sumcEls[i]);
        tempObject =
            ReflectHelper.invoke(
                tempObject,
                "get"
                    + StringHelper.converseFirstCharToUpper(
                        getAggregateMethod((String) sumcEls[i])));
        isAggregate = true;
      } else { // 普通的简单elobject对象调用
        if (isAggregate == true) {
          tempObject =
              sumcInvoke(
                  tempObject,
                  "get" + StringHelper.converseFirstCharToUpper((String) sumcEls[i]),
                  sumcSplitStr);
          break;
        } else {
          tempObject =
              ReflectHelper.invoke(
                  tempObject, "get" + StringHelper.converseFirstCharToUpper((String) sumcEls[i]));
          if (tempObject == null) {
            tempObject = "";
            break;
          }
        }
      }
    }

    return tempObject;
  }
  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 + "]");
      }
    }
  }
  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 + "]");
      }
    }
  }