Ejemplo n.º 1
0
  /**
   * Each service invocation might have different columns than other other invocations. This method
   * integrates all results into one table. For the invocations that don't have a specific column,
   * we put null values in corresponding column.
   */
  public static Table union(List<Table> srcTables) {

    if (srcTables == null) return null;

    Table resultTable = new Table();

    String attributeId = "";
    List<String> uniqueAttributeIDs = new ArrayList<String>();

    List<List<Attribute>> srcAttributes = new ArrayList<List<Attribute>>();
    List<List<List<String>>> srcValues = new ArrayList<List<List<String>>>();
    List<List<String>> srcRowIds = new ArrayList<List<String>>();

    List<Attribute> resultAttributes = new ArrayList<Attribute>();
    List<List<String>> resultValues = new ArrayList<List<String>>();
    List<String> resultRowIds = new ArrayList<String>();

    for (Table t : srcTables) {
      srcAttributes.add(t.getHeaders());
      srcRowIds.add(t.getRowIds());
      srcValues.add(t.getValues());
    }

    for (int i = 0; i < srcAttributes.size(); i++) {
      for (int j = 0; j < srcAttributes.get(i).size(); j++) {
        attributeId = srcAttributes.get(i).get(j).getId().toString();
        if (uniqueAttributeIDs.indexOf(attributeId) == -1) {
          uniqueAttributeIDs.add(attributeId);
          resultAttributes.add(srcAttributes.get(i).get(j));
        }
      }
    }

    List<Attribute> rawAttributes = null;
    List<String> rawAttributeIDs = new ArrayList<String>();
    List<String> rawValues = null;
    String singleValue = null;
    for (int i = 0; i < srcAttributes.size(); i++) {

      rawAttributes = srcAttributes.get(i);
      rawAttributeIDs.clear();
      for (Attribute p : rawAttributes) rawAttributeIDs.add(p.getId());

      //			logger.debug("table " + i);
      for (int j = 0; j < srcValues.get(i).size(); j++) {

        List<String> populatedValues = new ArrayList<String>();
        rawValues = srcValues.get(i).get(j);

        //				logger.debug("\t row " + j);
        for (int k = 0; k < resultAttributes.size(); k++) {
          //					logger.debug("\t\t column " + k);
          int index = rawAttributeIDs.indexOf(resultAttributes.get(k).getId());
          if (index == -1) singleValue = null;
          //						singleValue = "";
          else singleValue = rawValues.get(index);
          populatedValues.add(singleValue);
        }

        if (srcRowIds != null
            && srcRowIds.size() > 0
            && srcRowIds.get(i) != null
            && srcRowIds.get(i).size() > 0
            && srcRowIds.get(i).get(j) != null) resultRowIds.add(srcRowIds.get(i).get(j));
        resultValues.add(populatedValues);
      }
    }

    resultTable.setHeaders(resultAttributes);
    resultTable.setRowIds(resultRowIds);
    resultTable.setValues(resultValues);

    return resultTable;
  }