示例#1
0
  /**
   * 查询SNMP历史数据
   *
   * @param objId 对象ID
   * @param startTime 开始时间(单位是秒)
   * @param endTime 结束时间(单位是秒)
   * @param objAttrIds 对象属性id列表
   * @return 结果
   * @throws Exception 异常
   */
  public String querySNMPHistoryData(int objId, long startTime, long endTime, String objAttrIds)
      throws Exception {
    String result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><DataSources></DataSources>";

    if (!StringUtil.isNullOrEmpty(objAttrIds)) {
      String[] arr = objAttrIds.split(",");

      Map<Integer, List<QueryResult>> values =
          CollDataQuery.getInstance()
              .query(objId, startTime, endTime, ArrayUtils.stringToIntArray(arr));

      if (values != null) {
        // 查询要格式化值的对象属性
        List<Map<String, String>> lst =
            new SqlQueryUtil()
                .getLst(
                    "SELECT A.OBJATTR_ID,A.ATTRIB_TYPE,B.VALUE_TYPE FROM BMP_OBJATTRIB A LEFT JOIN BMP_ATTRIBUTE B ON A.ATTRIB_ID = B.ATTRIB_ID WHERE B.VALUE_TYPE > 0 AND B.VALUE_TYPE IS NOT NULL AND A.OBJATTR_ID IN ("
                        + objAttrIds
                        + ")");

        // 查询枚举信息
        List<String> valueObjAttrArr = ArrayUtils.stringToStringArrayList(arr);
        List<ValueTableEntity> valueTables = getValueTableByObjAttrIds(valueObjAttrArr);

        // 组装
        Document result_doc = DocumentHelper.parseText(result);

        String objattrId;
        String attribType = "";
        String valueType = "";
        List<ValueTableEntity> valueLst;
        for (Map.Entry<Integer, List<QueryResult>> entry : values.entrySet()) {
          objattrId = entry.getKey().toString();

          Element ele = result_doc.getRootElement().addElement("Data");
          ele.addAttribute("OBJATTR_ID", objattrId);

          valueLst = new ArrayList<ValueTableEntity>();
          if (lst != null && lst.size() > 0 && valueTables.size() > 0) {
            for (Map<String, String> map : lst) {
              if (objattrId.equals(map.get("OBJATTR_ID"))) {
                attribType = map.get("ATTRIB_TYPE");
                valueType = map.get("VALUE_TYPE");

                for (ValueTableEntity entity : valueTables) {
                  if (valueType.equals(String.valueOf(entity.getValueType()))) {
                    valueLst.add(entity);
                  }
                }

                break;
              }
            }
          }

          // 循环读取每个QueryResult对象,并转化为指定XML格式
          ele.appendContent(
              queryResultListToXml(
                      entry.getValue(), entry.getKey().toString(), "", "", attribType, valueLst)
                  .getRootElement());
        }

        result = result_doc.asXML();
      }
    }

    return result;
  }
示例#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();
  }