Ejemplo n.º 1
0
  /**
   * 将调用性能接口返回的结果转化为XML字符串
   *
   * @param list 结果
   * @param objAttrId 对象属性ID
   * @param objAttrName 对象属性名
   * @param attribType 对象属性的类型
   * @param valueLst 格式化枚举值
   * @return 返回指定格式的XML字符串
   * @throws Exception
   */
  private Document queryResultListToXml(
      List<QueryResult> list,
      String objAttrId,
      String objAttrName,
      String fetchSize,
      String attribType,
      List<ValueTableEntity> valueLst)
      throws Exception {
    Document result_doc = DocumentHelper.parseText("<DataSource></DataSource>");

    if (list != null && list.size() > 0) {
      // 初始化截取条数,默认为所有结果,若fetchSize不为空,则截取fetchSize条
      int fs = list.size();
      if (!StringUtil.isNullOrEmpty(fetchSize)) {
        fs = Integer.valueOf(fetchSize);
      }

      // 组装
      // 如果只取一条
      if (fs == 1) {
        QueryResult entity = list.get(list.size() - 1); // 最后一条

        Element ele = result_doc.getRootElement().addElement("DataTable");
        if (!StringUtil.isNullOrEmpty(objAttrId)) {
          Element eleObjAttrId = ele.addElement("OBJATTR_ID");
          eleObjAttrId.setText(objAttrId);
        }
        if (!StringUtil.isNullOrEmpty(objAttrName)) {
          Element eleObjAttrName = ele.addElement("OBJATTR_NAME");
          eleObjAttrName.setText(objAttrName);
        }

        if (list.size() == 1) {
          // 如果只取一条,则若结果也只有一条,则截取该结果;
          Element eleCollTime = ele.addElement("COLL_TIME");
          eleCollTime.setText(String.valueOf(entity.getCollTime()));
          Element eleVlue = ele.addElement("VALUE");
          eleVlue.setText(
              formatObjAttribValue(String.valueOf(entity.getValue()), attribType, valueLst));
        } else {
          // 若结果大于一条,则先取最后一条,若最后一条的VALUE为空,则取倒数第二条,不管倒数第二条的VALUE是否为空
          if (StringUtil.isNullOrEmpty(entity.getValue())) {
            QueryResult entity2 = list.get(list.size() - 2);

            Element eleCollTime = ele.addElement("COLL_TIME");
            eleCollTime.setText(String.valueOf(entity2.getCollTime()));
            Element eleVlue = ele.addElement("VALUE");
            eleVlue.setText(
                formatObjAttribValue(String.valueOf(entity2.getValue()), attribType, valueLst));
          } else {
            Element eleCollTime = ele.addElement("COLL_TIME");
            eleCollTime.setText(String.valueOf(entity.getCollTime()));
            Element eleVlue = ele.addElement("VALUE");
            eleVlue.setText(
                formatObjAttribValue(String.valueOf(entity.getValue()), attribType, valueLst));
          }
        }
      } else {
        // 如果取多条,则取结果的最后fs条
        QueryResult entity;
        int count = 0;
        for (int i = list.size() - 1; i >= 0; i--) {
          entity = list.get(i);

          Element ele = result_doc.getRootElement().addElement("DataTable");
          if (!StringUtil.isNullOrEmpty(objAttrId)) {
            Element eleObjAttrId = ele.addElement("OBJATTR_ID");
            eleObjAttrId.setText(objAttrId);
          }
          if (!StringUtil.isNullOrEmpty(objAttrName)) {
            Element eleObjAttrName = ele.addElement("OBJATTR_NAME");
            eleObjAttrName.setText(objAttrName);
          }
          Element eleCollTime = ele.addElement("COLL_TIME");
          eleCollTime.setText(String.valueOf(entity.getCollTime()));
          Element eleVlue = ele.addElement("VALUE");
          eleVlue.setText(
              formatObjAttribValue(String.valueOf(entity.getValue()), attribType, valueLst));

          count++;

          if (count >= fs) {
            break;
          }
        }
      }
    }

    return result_doc;
  }
Ejemplo n.º 2
0
  /**
   * 获取某对象的某类别的自定义属性或者配置信息、表格数据的值
   *
   * @param objId 对象ID
   * @param classId 分类ID
   * @param isFresh 是否刷新
   * @return 自定义属性或者配置信息、表格数据的值XML
   * @throws Exception 异常
   */
  public String getObjAttrValueByObjAndClass(int objId, int classId, boolean isFresh)
      throws Exception {
    // 定义一个Document作为返回结果
    Document result_doc =
        DocumentHelper.parseText(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?><DataSource></DataSource>");

    // 查询该对象该分类下的所有对象属性
    List<Map<String, String>> lst =
        new SqlQueryUtil()
            .getLst(
                "SELECT A.OBJATTR_ID,A.OBJATTR_NAME,A.ATTRIB_TYPE,B.VALUE_TYPE,B.DATA_UNIT FROM BMP_OBJATTRIB A "
                    + "LEFT JOIN BMP_ATTRIBUTE B ON A.ATTRIB_ID=B.ATTRIB_ID LEFT JOIN BMP_ATTRIB2CLASS C ON B.ATTRIB_ID=C.ATTRIB_ID "
                    + "LEFT JOIN BMP_ATTRIBCLASS D ON C.CLASS_ID=D.CLASS_ID WHERE A.IS_VISIBLE=1 AND A.OBJ_ID="
                    + objId
                    + " AND D.CLASS_ID="
                    + classId);

    if (lst != null && lst.size() > 0) {
      List<String> valueArr = new ArrayList<String>();
      List<Integer> objattrIdLst = new ArrayList<Integer>();
      for (Map<String, String> map : lst) {
        objattrIdLst.add(Integer.valueOf(map.get("OBJATTR_ID")));
        if (!StringUtil.isNullOrEmpty(map.get("VALUE_TYPE"))
            && !"0".equals(map.get("VALUE_TYPE"))) {
          valueArr.add(map.get("VALUE_TYPE"));
        }
      }

      // 查询枚举信息
      List<ValueTableEntity> valueTables = new ArrayList<ValueTableEntity>();
      if (valueArr != null && valueArr.size() > 0) {
        String valueTypes = StringUtils.join(valueArr, ',');
        DefaultDal<ValueTableEntity> dal = new DefaultDal<ValueTableEntity>(ValueTableEntity.class);

        valueTables =
            dal.getLst(
                new SqlCondition(
                    "VALUE_TYPE",
                    valueTypes,
                    SqlLogicType.And,
                    SqlRelationType.In,
                    SqlParamType.Numeric));
      }

      // 调用接口
      int[] arr = ArrayUtils.listToIntArray(objattrIdLst);
      Map<Integer, QueryResult> values = CollDataQuery.getInstance().query(objId, arr, isFresh);

      // 根据结果组装XML
      if (values != null && values.size() > 0) {
        for (Map<String, String> map : lst) {
          String objattrId = map.get("OBJATTR_ID");
          String objattrName = map.get("OBJATTR_NAME");
          String valueType = map.get("VALUE_TYPE");
          String dataUnit = map.get("DATA_UNIT");
          String attribType = map.get("ATTRIB_TYPE");

          Element ele = result_doc.getRootElement().addElement("DataTable");
          Element eleId = ele.addElement("OBJATTR_ID");
          eleId.setText(objattrId);
          Element eleName = ele.addElement("OBJATTR_NAME");
          eleName.setText(objattrName);
          Element eleType = ele.addElement("VALUE_TYPE");
          eleType.setText(valueType);
          Element eleUnit = ele.addElement("DATA_UNIT");
          eleUnit.setText(dataUnit);

          Element eleValue = ele.addElement("STR_VALUE");
          QueryResult resultLst = values.get(Integer.valueOf(objattrId));
          String value = resultLst.getValue();

          // 如果存在格式化信息,则格式化
          if (!StringUtil.isNullOrEmpty(valueType)
              && !"0".equals(valueType)
              && valueTables.size() > 0) {
            List<ValueTableEntity> valueLst = new ArrayList<ValueTableEntity>();
            for (ValueTableEntity entity : valueTables) {
              if (valueType.equals(String.valueOf(entity.getValueType()))) {
                valueLst.add(entity);
              }
            }

            eleValue.setText(formatObjAttribValue(value, attribType, valueLst));
          } else {
            if (StringUtil.isNullOrEmpty(value)) {
              eleValue.setText("");
            } else {
              eleValue.setText(value);
            }
          }
        }
      }
    }

    return result_doc.asXML();
  }