Пример #1
0
  /** 序列化多个dataset */
  public String[] serialize(Dataset[] dss) {
    if (dss != null && dss.length > 0) {
      String[] rss = new String[dss.length];
      for (int i = 0; i < dss.length; i++) {
        Dataset ds = dss[i];
        // 做数据校正,将“编码树”结构改造成为“PK树”,此改造在本页面会话中只进行一次
        if (ds.getExtendAttribute(Dataset.CODE_LEVEL_CLAZZ) != null) {
          ICodeRuleDsUtil dsUtil =
              (ICodeRuleDsUtil)
                  LfwClassUtil.newInstance(
                      (String) ds.getExtendAttributeValue(Dataset.CODE_LEVEL_CLAZZ));
          String codeRule = (String) ds.getExtendAttributeValue(Dataset.CODE_LEVEL_RULE);
          String codeField = (String) ds.getExtendAttributeValue(Dataset.CODE_LEVEL_CODEFIELD);
          String codePk = (String) ds.getExtendAttributeValue(Dataset.CODE_LEVEL_PK);
          String codePPk = (String) ds.getExtendAttributeValue(Dataset.CODE_LEVEL_PPK);
          int codeIndex = ds.getFieldSet().nameToIndex(codeField);
          int pkIndex = ds.getFieldSet().nameToIndex(codePk);
          int ppkIndex = ds.getFieldSet().nameToIndex(codePPk);
          dsUtil.buildCodeRuleDataset(ds, codeRule, codeIndex, pkIndex, ppkIndex);
        }

        // 处理字段关联
        processFieldRelation(ds);
        processFormular(ds);
        StringBuffer xmlSb = new StringBuffer(BUFFER_SIZE);
        xmlSb.append(
            "<dataset id=\""
                + ds.getId()
                + "\" focusIndex=\""
                + ds.getFocusIndex()
                + "\" currentkey=\""
                + (ds.getCurrentKey() == null ? EventContextConstant.NULL : ds.getCurrentKey())
                + "\" editable=\""
                + ds.isEnabled()
                + "\" randomRowIndex=\""
                + ds.getRandomRowIndex()
                + "\">\n");
        xmlSb.append("    <" + EventContextConstant.res_parameters + ">\n");
        ParameterSet resParamSet = ds.getResParameters();
        int count = resParamSet.size();
        for (int j = 0; j < count; j++) {
          Parameter param = resParamSet.getParameter(j);
          String name = param.getName();
          String value = param.getValue();
          xmlSb.append("      <" + EventContextConstant.parameter + " name=\"" + name + "\">");
          xmlSb
              .append(value == null ? "" : value)
              .append("</" + EventContextConstant.parameter + ">\n");
        }
        xmlSb.append("    </" + EventContextConstant.res_parameters + ">\n");
        xmlSb.append("    <rowsets>\n");
        RowSet[] rowsets = ds.getRowSets();
        for (int j = 0; j < rowsets.length; j++) {
          RowSet rowSet = rowsets[j];
          int pagecount = rowSet.getPaginationInfo().getPageCount();
          int pagesize = rowSet.getPaginationInfo().getPageSize();
          int recordscount = rowSet.getPaginationInfo().getRecordsCount();
          int pageindex = rowSet.getPaginationInfo().getPageIndex();
          xmlSb
              .append("    <rowset pagecount=\"")
              .append(pagecount)
              .append("\" pagesize=\"")
              .append(pagesize)
              .append("\" recordcount=\"")
              .append(recordscount)
              .append("\" pageindex=\"")
              .append(pageindex)
              .append("\" keyvalue=\"")
              .append(rowSet.getKeyvalue())
              .append("\"");
          if (rowSet.getOldKeyValue() != null) {
            xmlSb.append(" oldkeyvalue=\"").append(rowSet.getOldKeyValue()).append("\"");
          }
          xmlSb.append(">\n");

          RowData[] rds = rowSet.getRowDatas();
          for (int k = 0; k < rds.length; k++) {
            RowData rdata = rds[k];
            if (rdata.isRowDataChanged()) {
              xmlSb.append(
                  "    <"
                      + EventContextConstant.records
                      + " pageindex=\""
                      + rdata.getPageindex()
                      + "\" changed=\""
                      + rdata.isRowDataSelfChanged()
                      + "\">\n");
              Integer[] indices = rdata.getSelectIndices();
              if (indices != null && indices.length > 0) {
                xmlSb.append("<selected>");
                for (int l = 0; l < indices.length; l++) {
                  xmlSb.append(indices[l]);
                  if (l != indices.length - 1) {
                    xmlSb.append(",");
                  }
                }
                xmlSb.append("</selected>\n");
              }
              processRecord(xmlSb, rdata);
              xmlSb.append("</" + EventContextConstant.records + ">\n");
            }
          }
          xmlSb.append("    </rowset>\n");
        }
        xmlSb.append("    </rowsets>\n");
        xmlSb.append("</dataset>\n");
        rss[i] = xmlSb.toString();
      }
      return rss;
    }
    return null;
  }
Пример #2
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();
  }
Пример #3
0
  /**
   * 根据状态转换成相应地记录应答格式,注意,因为如果是删除记录不会返回ajax应答,所以 此处不处理删除状态。
   *
   * @param xmlSb
   * @param pm
   * @param rows
   * @param state
   */
  protected void processRecord(StringBuffer xmlSb, RowData data) {

    StringBuffer rowCountsb = null;
    int size = data.getRowCount();
    for (int i = 0; i < size; i++) {
      Row row = data.getRow(i);
      int fieldCount = row.size();
      rowCountsb = new StringBuffer();

      boolean isEmptyRow = (row instanceof EmptyRow);
      String recordSign = "<" + EventContextConstant.record + "";
      String recordESign = "</" + EventContextConstant.record + ">";
      if (isEmptyRow) {
        recordSign = "<" + EventContextConstant.erecord;
        recordESign = "</" + EventContextConstant.erecord + ">";
      }
      xmlSb.append(recordSign + " id=\"" + row.getRowId() + "\" ");
      if (!isNull(row.getParentId())) xmlSb.append(" parentid=\"" + row.getParentId() + ">");
      else xmlSb.append(">");

      if (!isEmptyRow) {
        String[] startend = getStartEndByState(row.getState());
        xmlSb.append(startend[0]);

        Object value = null;
        for (int j = 0; j < fieldCount; j++) {
          value = row.getValue(j);
          if (value != null) {
            // 处理日期时间类型 dingrf
            if (row.getContent()[j] instanceof UFDate) {
              value = ((UFDate) row.getContent()[j]).getMillis();
            } else if (row.getContent()[j] instanceof UFDateTime) {
              value = ((UFDateTime) row.getContent()[j]).getMillis();
            } else if (row.getContent()[j] instanceof UFLiteralDate) {
              value = ((UFLiteralDate) row.getContent()[j]).getMillis();
            }
            rowCountsb.append(JsURLEncoder.encode(value.toString(), "UTF-8"));
          } else rowCountsb.append(EventContextConstant.NULL);
          if (j < fieldCount - 1) rowCountsb.append(",");
        }
        xmlSb.append(rowCountsb);
        xmlSb.append(startend[1]);

        // dingrf记录本行中发生修改的列索引
        String changedSign = "<" + EventContextConstant.changed + ">";
        String changedESign = "</" + EventContextConstant.changed + ">";
        xmlSb.append(changedSign);
        for (int k = 0; k < row.getChangedIndices().size(); k++) {
          xmlSb.append(row.getChangedIndices().get(k));
          if (k < row.getChangedIndices().size() - 1) xmlSb.append(",");
        }
        xmlSb.append(changedESign);
      }
      xmlSb.append(recordESign + "\n");
    }

    try {
      Method m = RowData.class.getDeclaredMethod("getDeleteRows", new Class[] {});
      m.setAccessible(true);
      Row[] delRows = (Row[]) m.invoke(data, new Object[] {});
      if (delRows != null && delRows.length > 0) {
        for (int i = 0; i < delRows.length; i++) {
          xmlSb.append(
              "<" + EventContextConstant.drecord + " id=\"" + delRows[i].getRowId() + "\"/>");
        }
      }
    } catch (Exception e) {
      LfwLogger.error(e);
    }
  }
Пример #4
0
  private void processFormular(Dataset ds) {
    RowData rd = ds.getCurrentRowData();
    if (rd == null) return;
    int rcount = rd.getRowCount();
    List<String> executedFpList = new ArrayList<String>();
    int fieldCount = ds.getFieldSet().getFieldCount();
    for (int i = 0; i < fieldCount; i++) {
      try {
        Field field = ds.getFieldSet().getField(i);
        List<String> fieldFormulars = new ArrayList<String>();
        String formular = field.getEditFormular();
        if (formular != null) fieldFormulars.add(formular);

        formular = field.getLoadFormular();
        if (formular != null) fieldFormulars.add(formular);

        Iterator<String> fit = fieldFormulars.iterator();
        while (fit.hasNext()) {
          formular = fit.next();
          String exp = null;
          if (formular != null) exp = JsURLDecoder.decode(formular, "UTF-8");
          // 如果没有加载公式,继续下一个字段
          if (exp == null) continue;

          if (executedFpList.contains(exp)) continue;
          executedFpList.add(exp);
          String[] expArr = exp.split(";");

          fp.setExpressArray(expArr);
          VarryVO[] varryVOs = fp.getVarryArray();
          if (varryVOs == null || varryVOs.length == 0) continue;
          String[] formularNames = new String[varryVOs.length];

          Map<String, Integer> indexMap = getIndexMap(ds);
          for (int j = 0; j < varryVOs.length; j++) {
            String[] keys = varryVOs[j].getVarry();
            if (keys != null) {
              for (String key : keys) {
                List<Object> valueList = new ArrayList<Object>();
                for (int k = 0; k < rcount; k++) {
                  Row r = rd.getRow(k);
                  //
                  Object value = r.getValue(indexMap.get(key));
                  Field f = ds.getFieldSet().getField(key);
                  if (f != null && value != null) {
                    //										//如果是Double类型,进行一下转换
                    if (StringDataTypeConst.UFDOUBLE.equals(f.getDataType())
                        || StringDataTypeConst.DOUBLE.equals(f.getDataType())
                        || StringDataTypeConst.Decimal.equals(f.getDataType())) {
                      if (!(value instanceof UFDouble)) value = new UFDouble(value.toString());
                    } else if (StringDataTypeConst.INTEGER.equals(f.getDataType())) {
                      if (!(value instanceof Integer)) value = new Integer((String) value);
                    }
                  }
                  valueList.add(value);
                }
                fp.addVariable(key, valueList);
              }
              formularNames[j] = varryVOs[j].getFormulaName();
            }
          }

          Object[][] result = fp.getValueOArray();
          if (result != null) {
            for (int l = 0; l < formularNames.length; l++) {
              int index = ds.nameToIndex(formularNames[l]);
              if (index == -1) {
                LfwLogger.error(
                    "can not find column:" + formularNames[l] + ", ds id:" + ds.getId());
                continue;
              }
              for (int k = 0; k < rcount; k++) {
                Row r = rd.getRow(k);
                r.setValue(index, result[l][k]);
              }
            }
          }
        }
      } catch (Throwable e) {
        Logger.error(e.getMessage(), e);
      }
    }
  }