Package com.google.visualization.datasource.datatable

Examples of com.google.visualization.datasource.datatable.DataTable


      newColumnDescriptions.add(new ColumnDescription(column.getId(),
          column.getValueType(table),
          ScalarFunctionColumnTitle.getColumnDescriptionLabel(table, column)));
    }

    DataTable tempTable = new DataTable();
    tempTable.addColumns(newColumnDescriptions);

    // Calculate the values of the added scalar function columns in each row.
    DataTableColumnLookup lookup = new DataTableColumnLookup(table);
    for (TableRow sourceRow : table.getRows()) {
      TableRow newRow = new TableRow();
      for (TableCell sourceCell : sourceRow.getCells()) {
        newRow.addCell(sourceCell);
      }
      for (ScalarFunctionColumn column : groupAndPivotScalarFunctionColumns) {
        newRow.addCell(new TableCell(column.getValue(lookup, sourceRow)));
      }
      try {
        tempTable.addRow(newRow);
      } catch (TypeMismatchException e) {
        // Should not happen, given that the original table is OK.
      }
    }
    table = tempTable;

    // Calculate the aggregations.
    TableAggregator aggregator = new TableAggregator(groupAndPivotIds,
        Sets.newHashSet(aggregationIds), table);
    Set<AggregationPath> paths = aggregator.getPathsToLeaves();

    // These variables will hold the "titles" of the rows and columns.
    // They are TreeSets because their order matters.
    SortedSet<RowTitle> rowTitles =
        Sets.newTreeSet(GroupingComparators.ROW_TITLE_COMPARATOR);
    SortedSet<ColumnTitle> columnTitles = Sets.newTreeSet(
        GroupingComparators.getColumnTitleDynamicComparator(columnAggregations));

    // A tree set containing all pivot value lists (the set is for the
    // uniqueness and the tree for the order).
    TreeSet<List<Value>> pivotValuesSet =
        Sets.newTreeSet(GroupingComparators.VALUE_LIST_COMPARATOR);
    // This MetaTable holds all the data in the table, this data is then
    // dumped into the real table.
    MetaTable metaTable = new MetaTable();
    for (AggregationColumn columnAggregation : columnAggregations) {
      for (AggregationPath path : paths) {

        // A ColumnTitle is composed of all the values for the pivot-by
        // columns, and a ColumnAggregation. That is why it is necessary to iterate over all
        // ColumnAggregations and create a ColumnTitle for each one.
        List<Value> originalValues = path.getValues();

        // Separate originalValues into the rowValues and columnValues. The
        // rowValues are the values of the group-by columns and the columnValues
        // are the values of the pivot-by columns.
        List<Value> rowValues = originalValues.subList(0, groupByIds.size());
        RowTitle rowTitle = new RowTitle(rowValues);
        rowTitles.add(rowTitle);

        List<Value> columnValues = originalValues.subList(groupByIds.size(), originalValues.size());
        pivotValuesSet.add(columnValues);

        ColumnTitle columnTitle = new ColumnTitle(columnValues,
            columnAggregation, (columnAggregations.size() > 1));
        columnTitles.add(columnTitle);
        metaTable.put(rowTitle, columnTitle, new TableCell(aggregator.getAggregationValue(path,
            columnAggregation.getAggregatedColumn().getId(),
            columnAggregation.getAggregationType())));
      }
    }

    // Create the scalar function column titles for the scalar function columns
    // that contain aggregations.
    List<ScalarFunctionColumnTitle> scalarFunctionColumnTitles =
        Lists.newArrayList();
    for (ScalarFunctionColumn scalarFunctionColumn :
        selectedScalarFunctionColumns) {
      if (scalarFunctionColumn.getAllAggregationColumns().size() != 0) {
        for (List<Value> columnValues : pivotValuesSet) {
          scalarFunctionColumnTitles.add(new ScalarFunctionColumnTitle(columnValues,
              scalarFunctionColumn));
        }
      }
    }

    // Create the new table description.
    DataTable result = createDataTable(groupByIds, columnTitles, table, scalarFunctionColumnTitles);
    List<ColumnDescription> colDescs = result.getColumnDescriptions();

    // Fill the columnIndices and columnLookups parameters for the group-by
    // columns and the aggregation columns.
    columnIndices.clear();
    int columnIndex = 0;
    if (group != null) {
      List<Value> empytListOfValues = Lists.newArrayList();
      columnLookups.put(empytListOfValues, new GenericColumnLookup());
      for (AbstractColumn column : group.getColumns()) {
        columnIndices.put(column, columnIndex);
        if (!(column instanceof ScalarFunctionColumn)) {
          ((GenericColumnLookup) columnLookups.get(empytListOfValues)).put(column, columnIndex);
          for (List<Value> columnValues : pivotValuesSet) {
            if (!columnLookups.containsKey(columnValues)) {
              columnLookups.put(columnValues, new GenericColumnLookup());
            }
            ((GenericColumnLookup) columnLookups.get(columnValues)).put(column, columnIndex);
          }
        }
        columnIndex++;
      }
    }

    for (ColumnTitle title : columnTitles) {
      columnIndices.put(title.aggregation, columnIndex);
      List<Value> values = title.getValues();
      if (!columnLookups.containsKey(values)) {
        columnLookups.put(values, new GenericColumnLookup());
      }
      ((GenericColumnLookup) columnLookups.get(values)).put(title.aggregation, columnIndex);
      columnIndex++;
    }

    // Dump the data from the metaTable to the result DataTable.
    for (RowTitle rowTitle : rowTitles) {
      TableRow curRow = new TableRow();
      // Add the group-by columns cells.
      for (Value v : rowTitle.values) {
        curRow.addCell(new TableCell(v));
      }
      Map<ColumnTitle, TableCell> rowData = metaTable.getRow(rowTitle);
      int i = 0;
      // Add the aggregation columns cells.
      for (ColumnTitle colTitle : columnTitles) {
        TableCell cell = rowData.get(colTitle);
        curRow.addCell((cell != null) ? cell : new TableCell(
            Value.getNullValueFromValueType(colDescs.get(i + rowTitle.values.size()).getType())));
        i++;
      }
      // Add the scalar function columns cells.
      for (ScalarFunctionColumnTitle columnTitle : scalarFunctionColumnTitles) {
        curRow.addCell(new TableCell(columnTitle.scalarFunctionColumn.
            getValue(columnLookups.get(columnTitle.getValues()), curRow)));
      }
      result.addRow(curRow);
    }

    // Fill the columnIndices and columnLookups parameters for the scalar
    // function column titles. This must be done after the calculation of the values
    // in the scalar function column cells, or else the scalar function columns
View Full Code Here


  public void setUp() throws Exception {
    super.setUp();

    colIds = Lists.newArrayList();

    testData = new DataTable();
    ColumnDescription c0 = new ColumnDescription("col0", ValueType.TEXT, "label0");
    ColumnDescription c1 = new ColumnDescription("col1", ValueType.NUMBER, "label1");
    ColumnDescription c2 = new ColumnDescription("col2", ValueType.BOOLEAN, "label2");
    ColumnDescription c3 = new ColumnDescription("col3", ValueType.DATE, "label3");
    ColumnDescription c4 = new ColumnDescription("col4", ValueType.TIMEOFDAY, "label4");
View Full Code Here

    testData = null;
    rows = null;
  }

  public void testEmptyDataTableToCsv() {
    DataTable dataTable = new DataTable();
    assertEquals("", CsvRenderer.renderDataTable(dataTable, null, null));
    assertEquals("", CsvRenderer.renderDataTable(dataTable, null, ","));
    assertEquals("", CsvRenderer.renderDataTable(dataTable, null, "\t"));
  }
View Full Code Here

  }

  public void testSimpleDataTableToCsv() throws DataSourceException {
    colIds = Lists.newArrayList();

    testData = new DataTable();
    ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
    ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
    ColumnDescription c2 = new ColumnDescription("C", ValueType.BOOLEAN, "col2");

    testData.addColumn(c0);
View Full Code Here

  }

  public void testCustomPropertiesToCsv() throws DataSourceException {
    colIds = Lists.newArrayList();

    testData = new DataTable();
    ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
    ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
    c1.setCustomProperty("arak", "elit");

    testData.addColumn(c0);
View Full Code Here

        "\"Error: Operation not supported. Cannot \"\"do\"\" that, too late!\"",
        CsvRenderer.renderCsvError(responseStatus));
  }
 
  public void testRenderDataTableWithCommas() throws DataSourceException {
    testData = new DataTable();
    ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
    ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
    ColumnDescription c2 = new ColumnDescription("C", ValueType.BOOLEAN, "col2");
    ColumnDescription c3 = new ColumnDescription("D", ValueType.DATE, "col3");
    ColumnDescription c4 = new ColumnDescription("E", ValueType.DATETIME, "col4");
View Full Code Here

  private static final Logger log = Logger.getLogger(SimpleExampleServlet.class.getName());
 
  @Override
  public DataTable generateDataTable(Query query, HttpServletRequest request) {
    // Create a data table,
    DataTable data = new DataTable();
    ArrayList<ColumnDescription> cd = new ArrayList<ColumnDescription>();
    cd.add(new ColumnDescription("metricDate", ValueType.TEXT, "Date"));
    cd.add(new ColumnDescription("metricValue", ValueType.NUMBER, "Metric Value"));
    cd.add(new ColumnDescription("goalName", ValueType.TEXT, "Goal Name"));
    cd.add(new ColumnDescription("isGoalMet", ValueType.TEXT, "Goal Met?"));
    cd.add(new ColumnDescription("percentToGoal", ValueType.TEXT, "Percent to Goal"));

    data.addColumns(cd);

    MetricDao metricDao = new MetricDao();
    String metricName = null;
    metricName = (String) request.getParameter("metricName");
    metricName = "foo";
    Metric metric = metricDao.getMetric(metricName);
   
    MetricValueDao valueDao = new MetricValueDao();
    MetricGoalDao goalDao = new MetricGoalDao();
    List<MetricGoal> metricGoals = goalDao.listByProperty("metricKey", metric.getKey());
    Iterator<MetricValue> values =  valueDao.listByProperty("metricKey", metric.getKey()).iterator();
    int j=0;
    while (values.hasNext()) {

      j++;
      log.log(Level.WARNING, "value #" + j);
     
      MetricValue value = values.next();
      for (int i=0; i< metricGoals.size(); i++) {
        String isGoalMet = "N";
        MetricGoal goal = metricGoals.get(i);
        if (value.getTimeFrame().after(goal.getEffectivityDate()) &&
            value.getTimeFrame().before(goal.getExpirationDate())) {
          if (goal.getGoalType().equals(MetricGoal.HIGHISBETTER) && goal.getGoal() <= value.getValue()) {
            isGoalMet = "Y";
          }
          else if (goal.getGoalType().equals(MetricGoal.LOWISBETTER) && goal.getGoal() >= value.getValue()) {
            isGoalMet = "Y";
          }
        }
       
       
        try {
         
          log.log(Level.WARNING, "timeframe: " + value.getTimeFrame());
          log.log(Level.WARNING, "value: " + value.getValue());
          log.log(Level.WARNING, "goal name: " + goal.getName());
          log.log(Level.WARNING, "isGoalMet: " + isGoalMet);
          log.log(Level.WARNING, "percent to goal: " + MessageFormat.format("{0,number,#.##%}", value.getValue() / goal.getGoal()));
         
          TableRow row = new TableRow();
          row.addCell(new TableCell(new TextValue(value.getTimeFrame().toString())));
          row.addCell(new TableCell(new NumberValue(value.getValue())));
          row.addCell(new TableCell(new TextValue(goal.getName())));
          row.addCell(new TableCell(new TextValue(isGoalMet)));
          row.addCell(new TableCell(new TextValue(MessageFormat.format("{0,number,#.##%}", value.getValue() / goal.getGoal()))));
         
          data.addRow(row);
          } catch (TypeMismatchException e) {
          ExceptionManager.logException(log, e);
          return null;
        }
       
View Full Code Here

*/
public class DataSourceServiceTest extends TestCase {

    public void testApplyQuery() throws DataSourceException,
            TypeMismatchException {
        DataTable data = createData();

        DataSourceService helper = new DataSourceService();
        // Test select.
        DataTable result = helper.applyQuery(DataSourceRequest.parseQuery("select population"),
                data, ULocale.US);
        assertEquals(1, result.getNumberOfColumns());
        assertEquals(4, result.getNumberOfRows());
        assertEquals(new NumberValue(300), result.getRow(1).getCell(0).getValue());

        data = createData();

        // Test where.
        result = helper.applyQuery(DataSourceRequest.parseQuery(
                "select name,vegeterian where population > 100"), data, ULocale.US);
        assertEquals(2, result.getNumberOfColumns());
        assertEquals(1, result.getNumberOfRows());
        assertEquals(new TextValue("Sloth"), result.getRow(0).getCell(0).getValue());
        assertEquals(BooleanValue.TRUE, result.getRow(0).getCell(1).getValue());

        data = createData();

        // Test group by.
        result = helper.applyQuery(DataSourceRequest.parseQuery(
                "select vegeterian,sum(population) group by vegeterian"), data, ULocale.US);
        assertEquals(2, result.getNumberOfColumns());
        assertEquals(2, result.getNumberOfRows());
        assertEquals(BooleanValue.FALSE, result.getRow(0).getCell(0).getValue());
        assertEquals(new NumberValue(130), result.getRow(0).getCell(1).getValue());

        data = createData();

        // Test pivot.
        result = helper.applyQuery(DataSourceRequest.parseQuery(
                "select sum(population) pivot vegeterian"), data, ULocale.US);
        assertEquals(2, result.getNumberOfColumns());
        assertEquals(1, result.getNumberOfRows());
        assertEquals("false", result.getColumnDescription(0).getLabel());
        assertEquals(new NumberValue(130), result.getRow(0).getCell(0).getValue());

        data = createData();

        // Test order by.
        result = helper.applyQuery(DataSourceRequest.parseQuery(
                "select name order by population"), data, ULocale.US);
        assertEquals(1, result.getNumberOfColumns());
        assertEquals(4, result.getNumberOfRows());
        assertEquals(new TextValue("Leopard"), result.getRow(0).getCell(0).getValue());
        assertEquals(new TextValue("Tiger"), result.getRow(1).getCell(0).getValue());
        assertEquals(new TextValue("Aye-aye"), result.getRow(2).getCell(0).getValue());
        assertEquals(new TextValue("Sloth"), result.getRow(3).getCell(0).getValue());

        data = createData();

        // Test limit and offset.
        result = helper.applyQuery(DataSourceRequest.parseQuery("limit 1 offset 1"), data,
                ULocale.US);
        assertEquals(4, result.getNumberOfColumns());
        assertEquals(1, result.getNumberOfRows());
        assertEquals(new TextValue("Sloth"), result.getRow(0).getCell(0).getValue());

        data = createData();

        // Test label and format.
        result = helper.applyQuery(DataSourceRequest.parseQuery(
                "label population 'Population size (thousands)' format population \"'$'#'k'\""), data,
                ULocale.US);
        assertEquals(4, result.getNumberOfColumns());
        assertEquals(4, result.getNumberOfRows(), 4);
        assertEquals("Population size (thousands)",
                result.getColumnDescription("population").getLabel());
        int populationIndex = result.getColumnIndex("population");
        assertEquals("$100k", result.getRow(0).getCell(populationIndex).getFormattedValue());

        // Test that validation can fail
        try {
            DataSourceRequest.parseQuery("select min(min(a))");
            fail();
View Full Code Here

        r.setHeaders(new DataSourceRequest(parameters), mockHttpServletResponse);
        verify(mockHttpServletResponse);
    }

    public void testEmptyDataTableToCsv() throws DataSourceException {
        DataTable dataTable = new DataTable();
        CsvRenderer r = new CsvRenderer();
        DataSourceRequest request = new DataSourceRequest();
        assertEquals("", r.render(request, dataTable));
        assertEquals("", r.render(request, dataTable));
    }
View Full Code Here

        assertEquals("", r.render(request, dataTable));
        assertEquals("", r.render(request, dataTable));
    }

    public void testSimpleDataTableToCsv() throws DataSourceException {
        DataTable testData;
        List<TableRow> rows;

        testData = new DataTable();
        ColumnDescription c0 = new ColumnDescription("A", ValueType.TEXT, "col0");
        ColumnDescription c1 = new ColumnDescription("B", ValueType.NUMBER, "col1");
        ColumnDescription c2 = new ColumnDescription("C", ValueType.BOOLEAN, "col2");

        testData.addColumn(c0);
        testData.addColumn(c1);
        testData.addColumn(c2);

        rows = Lists.newArrayList();

        TableRow row = new TableRow();
        row.addCell(new TableCell("aaa"));
        row.addCell(new TableCell(new NumberValue(222), "222"));
        row.addCell(new TableCell(false));
        rows.add(row);

        row = new TableRow();
        row.addCell(new TableCell(""));
        row.addCell(new TableCell(NumberValue.getNullValue()));
        row.addCell(new TableCell(true));
        rows.add(row);

        row = new TableRow();
        row.addCell(new TableCell(new TextValue("bbb"), "bb@@b"));
        row.addCell(new TableCell(333));
        row.addCell(new TableCell(true));
        rows.add(row);

        row = new TableRow();
        row.addCell(new TableCell("ddd"));
        row.addCell(new TableCell(222));
        row.addCell(new TableCell(false));
        rows.add(row);

        testData.addRows(rows);

        CsvRenderer r = new CsvRenderer();
        DataSourceRequest request = new DataSourceRequest();
        assertEquals(
                "\"col0\",\"col1\",\"col2\"\n" +
View Full Code Here

TOP

Related Classes of com.google.visualization.datasource.datatable.DataTable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.