Пример #1
0
  private String aggregateSql(Dataset ds, FieldRelation rel) {
    if (ds.getCurrentKey() == null) return null;
    Dataset refDs = ds.getWidget().getViewModels().getDataset(rel.getRefDataset());
    if (refDs == null) {
      Logger.error("ref ds can not be fount:" + rel.getRefDataset());
      return null;
    }
    if (rel.getWhereField() == null) return null;
    MatchField[] mfs = rel.getMatchFields();
    WhereField where = rel.getWhereField();
    StringBuffer sql = new StringBuffer();
    sql.append("select ");
    for (int i = 0; i < mfs.length; i++) {
      Field f = refDs.getFieldSet().getField(mfs[i].getReadField());
      if (f == null)
        throw new LfwRuntimeException(
            "can not find field in ds:"
                + rel.getRefDataset()
                + ",field:"
                + mfs[i].getReadField()
                + ", in fieldrelation process");
      sql.append("a.").append(f.getField());
      if (i != mfs.length - 1) sql.append(",");
    }
    sql.append(",a.").append(refDs.getFieldSet().getField(where.getKey()).getField());
    sql.append(" from ");
    sql.append(getRefTableName(refDs));
    sql.append(" a where a.")
        .append(refDs.getFieldSet().getField(where.getKey()).getField())
        .append(" in (");

    RowData rowSet = ds.getCurrentRowSet().getCurrentRowData();
    Set<String> inSet = new HashSet<String>();
    if (rowSet != null) {
      Row[] rows = rowSet.getRows();
      int whereIndex = ds.nameToIndex(where.getValue());
      if (whereIndex == -1) return null;
      for (int i = 0; i < rows.length; i++) {
        if (rows[i] instanceof EmptyRow) continue;
        Object wValue = rows[i].getValue(whereIndex);
        String wValueStr = "";
        if (wValue != null) {
          wValueStr = wValue.toString();
          String[] strs = wValueStr.split(",");
          for (int j = 0; j < strs.length; j++) {
            inSet.add(strs[j]);
          }
        }
      }
    }

    if (inSet.size() == 0) return null;

    Iterator<String> it = inSet.iterator();
    while (it.hasNext()) {
      String value = it.next();
      sql.append("'");
      sql.append(value);
      sql.append("'");
      if (it.hasNext()) sql.append(",");
    }
    sql.append(")");
    return sql.toString();
  }
Пример #2
0
  /**
   * 字段关联处理
   *
   * @param ds
   * @param rels
   */
  public void dealFieldRelation(Dataset ds, FieldRelation[] rels) {
    if (rels != null && rels.length != 0) {
      for (int i = 0; i < rels.length; i++) {
        FieldRelation rel = rels[i];
        try {
          if (!needDoRel(ds, rel)) continue;
          String sql = aggregateSql(ds, rel);
          List<Object[]> result = null;
          if (sql != null) {
            result =
                (List<Object[]>) CRUDHelper.getCRUDService().query(sql, new ArrayListProcessor());
          } else {
            result = new ArrayList<Object[]>();
          }
          // 如果查出数据为空,则增加一行,使后面置空逻辑可继续运行
          if (result.size() == 0) result.add(null);
          Iterator<Object[]> it = result.iterator();

          WhereField whereField = rel.getWhereField();
          // 外键字段
          String key = ds.getFieldSet().getField(whereField.getValue()).getId();
          // 关联字段
          MatchField[] mfs = rel.getMatchFields();
          int[] mfIndices = new int[mfs.length];
          for (int j = 0; j < mfIndices.length; j++) {
            mfIndices[j] = ds.nameToIndex(mfs[j].getWriteField());
          }

          // 记录是否关联过的标识,以便于处理多选关联
          Row[] rows = ds.getCurrentRowData().getRows();
          boolean[][] notFirst = new boolean[rows.length][];
          for (int j = 0; j < notFirst.length; j++) {
            notFirst[j] = new boolean[mfIndices.length];
          }

          while (it.hasNext()) {

            Object[] values = it.next();
            int keyIndex = ds.nameToIndex(key);
            for (int j = 0; j < rows.length; j++) {
              if (rows[j] instanceof EmptyRow) continue;
              String keyValue = (String) rows[j].getValue(keyIndex);
              if (keyValue != null && values != null) {
                // 将主键值分隔
                String[] keyValues = keyValue.split(",");
                for (int m = 0; m < keyValues.length; m++) {
                  // 找出匹配的主键值,将对应的值字段写入
                  if (values[values.length - 1] != null
                      && values[values.length - 1].toString().trim().equals(keyValues[m].trim())) {
                    for (int k = 0; k < mfIndices.length; k++) {
                      Object value = values[k];
                      int mfIndex = mfIndices[k];
                      Object oldValue = rows[j].getValue(mfIndex);

                      // 原来的值不为空,有2种情况。一种是残留值,这种情况应该去掉。另一种是上一循环设置的,这种情况应该拼上 ","
                      if (oldValue != null) {
                        if (notFirst[j][k]) {
                          // SuperVO vo = (SuperVO) cavos[j].getCavo();
                          if (!mfs[k].getReadField().equals(whereField.getKey())) {
                            value = oldValue.toString() + "," + value;
                            rows[j].setValue(mfIndex, value);
                          }
                        } else {
                          notFirst[j][k] = true;
                          rows[j].setValue(mfIndex, value);
                        }
                      } else {
                        notFirst[j][k] = true;
                        rows[j].setValue(mfIndex, value);
                      }
                    }
                    break;
                  }
                }
              }
              // 将数据置空
              else {
                for (int k = 0; k < mfIndices.length; k++) {
                  Object value = null;
                  int mfIndex = mfIndices[k];
                  rows[j].setValue(mfIndex, value);
                }
              }
            }
          }
          if (rel.getChildRelationList() != null) {
            dealFieldRelation(ds, rel.getChildRelationList().toArray(new FieldRelation[0]));
          }
        } catch (Exception e) {
          Logger.error(e.getMessage(), e);
        }
      }
    }
  }