Beispiel #1
0
  public StorageDataSet getDataSetBySuperVos(SuperVO[] vos, int digit, Class cls)
      throws BusinessException {
    int iRowCount = (vos == null) ? 0 : vos.length;
    SuperVO vo = null;
    if (iRowCount == 0) { // 查询数据为空,必须构造column
      // System.out.println("DatasetUtil.getDatasetByCAVOs:行数为0");
      try {
        vo = (SuperVO) cls.newInstance();
      } catch (Exception e) {
        throw new BusinessException(e);
      }
    } else {
      vo = vos[0];
      cls = vo.getClass();
    }

    // 反射获得类属性信息
    QEDataSet sds = new QEDataSet();
    try {
      String names[] = vo.getAttributeNames();
      String realnames[] = null;
      if (vo instanceof IVarNameDefine) {
        realnames = ((IVarNameDefine) vo).getVaribleNames();
      }

      // 获得列数
      int iColCount = (names == null) ? 0 : names.length;

      Vector vecName = new Vector();
      Vector vecCol = new Vector();
      for (int i = 0; i < iColCount; i++) {
        try {
          // 创建列
          Column col = new Column();
          if (!CommonUtil.isNull(realnames) && !CommonUtil.isNull(realnames[i])) {
            col.setCaption(realnames[i]);

            col.setColumnName(names[i]);
            // 记录
            vecName.addElement(names[i]);
            vecCol.addElement(col);
            // 获得列类型
            Field fld = null;
            try {
              fld = cls.getField(names[i]);
            } catch (NoSuchFieldException e) {
              throw new BusinessException(e);
            }
            int iType = class2VariantType(fld.getType());
            /*
             * int iType = Variant.STRING; Object obj =
             * vos[0].getAttributeValue(names[i]); if (obj != null &&
             * obj.getClass().getSuperclass() == Number.class) iType =
             * Variant.DOUBLE;
             */
            col.setDataType(iType);
          }

        } catch (Exception e) {
          throw new BusinessException(e);
        }
      }
      // 获得有效列数
      iColCount = vecName.size();
      if (iColCount == 0) {
        // System.out.println("DatasetUtil.getDatasetByCAVOs:列数为0");
        return null;
      }
      // 重构列名数组和列数组
      names = new String[iColCount];
      vecName.copyInto(names);
      Column[] cols = new Column[iColCount];
      vecCol.copyInto(cols);

      // 转换
      sds = new QEDataSet();
      sds.setColumns(cols);
      sds.open();
      for (int i = 0; i < iRowCount; i++) {
        // 构造数据行
        DataRow row = new DataRow(sds);
        for (int j = 0; j < iColCount; j++) {
          Object obj = vos[i].getAttributeValue(names[j]);
          String str = (obj == null) ? null : obj.toString();
          int iColType = cols[j].getDataType();
          // 填充数据行
          DatasetUtil.makeDataRow(row, str, j, iColType);
          row = row;
        }
        // 加行
        sds.addRow(row);
      }
      sds.first();
    } catch (Exception e) {
      throw new BusinessException(e);
    }
    return sds;
  }