Ejemplo n.º 1
0
  static List<DisplayHeader> buildDisplayHeaders(Table table, RestRequest request) {
    List<DisplayHeader> display = new ArrayList<>();
    if (request.hasParam("h")) {
      Set<String> headers = expandHeadersFromRequest(table, request);

      for (String possibility : headers) {
        DisplayHeader dispHeader = null;

        if (table.getAsMap().containsKey(possibility)) {
          dispHeader = new DisplayHeader(possibility, possibility);
        } else {
          for (Table.Cell headerCell : table.getHeaders()) {
            String aliases = headerCell.attr.get("alias");
            if (aliases != null) {
              for (String alias : Strings.splitStringByCommaToArray(aliases)) {
                if (possibility.equals(alias)) {
                  dispHeader = new DisplayHeader(headerCell.value.toString(), alias);
                  break;
                }
              }
            }
          }
        }

        if (dispHeader != null && checkOutputTimestamp(dispHeader, request)) {
          // We know we need the header asked for:
          display.add(dispHeader);

          // Look for accompanying sibling column
          Table.Cell hcell = table.getHeaderMap().get(dispHeader.name);
          String siblingFlag = hcell.attr.get("sibling");
          if (siblingFlag != null) {
            // ...link the sibling and check that its flag is set
            String sibling = siblingFlag + "." + dispHeader.name;
            Table.Cell c = table.getHeaderMap().get(sibling);
            if (c != null && request.paramAsBoolean(siblingFlag, false)) {
              display.add(
                  new DisplayHeader(c.value.toString(), siblingFlag + "." + dispHeader.display));
            }
          }
        }
      }
    } else {
      for (Table.Cell cell : table.getHeaders()) {
        String d = cell.attr.get("default");
        if (Booleans.parseBoolean(d, true)
            && checkOutputTimestamp(cell.value.toString(), request)) {
          display.add(new DisplayHeader(cell.value.toString(), cell.value.toString()));
        }
      }
    }
    return display;
  }
Ejemplo n.º 2
0
  @Test
  public void testSimple() {
    Table table = this.getTableWithHeaders();
    table.startRow();
    table.addCell("foo1");
    table.addCell("bar1");
    table.endRow();
    table.startRow();
    table.addCell("foo2");
    table.addCell("bar2");
    table.endRow();

    // Check headers
    List<Table.Cell> headers = table.getHeaders();
    assertEquals(2, headers.size());
    assertEquals("foo", headers.get(0).value.toString());
    assertEquals(2, headers.get(0).attr.size());
    assertEquals("f", headers.get(0).attr.get("alias"));
    assertEquals("foo", headers.get(0).attr.get("desc"));
    assertEquals("bar", headers.get(1).value.toString());
    assertEquals(2, headers.get(1).attr.size());
    assertEquals("b", headers.get(1).attr.get("alias"));
    assertEquals("bar", headers.get(1).attr.get("desc"));

    // Check rows
    List<List<Table.Cell>> rows = table.getRows();
    assertEquals(2, rows.size());
    List<Table.Cell> row = rows.get(0);
    assertEquals("foo1", row.get(0).value.toString());
    assertEquals("bar1", row.get(1).value.toString());
    row = rows.get(1);
    assertEquals("foo2", row.get(0).value.toString());
    assertEquals("bar2", row.get(1).value.toString());

    // Check getAsMap
    Map<String, List<Table.Cell>> map = table.getAsMap();
    assertEquals(2, map.size());
    row = map.get("foo");
    assertEquals("foo1", row.get(0).value.toString());
    assertEquals("foo2", row.get(1).value.toString());
    row = map.get("bar");
    assertEquals("bar1", row.get(0).value.toString());
    assertEquals("bar2", row.get(1).value.toString());

    // Check getHeaderMap
    Map<String, Table.Cell> headerMap = table.getHeaderMap();
    assertEquals(2, headerMap.size());
    Table.Cell cell = headerMap.get("foo");
    assertEquals("foo", cell.value.toString());
    cell = headerMap.get("bar");
    assertEquals("bar", cell.value.toString());

    // Check findHeaderByName
    cell = table.findHeaderByName("foo");
    assertEquals("foo", cell.value.toString());
    cell = table.findHeaderByName("missing");
    assertNull(cell);
  }