Search in sources :

Example 1 with TableModel

use of org.activityinfo.model.analysis.TableModel in project activityinfo by bedatadriven.

the class TableViewModelTest method test.

@Test
public void test() {
    TableModel tableModel = ImmutableTableModel.builder().formId(setup.getSurveyForm().getFormId()).build();
    TableViewModel viewModel = new TableViewModel(setup.getFormStore(), tableModel);
    Connection<EffectiveTableModel> view = setup.connect(viewModel.getEffectiveTable());
    EffectiveTableModel effectiveTableModel = view.assertLoaded();
    EffectiveTableColumn nameColumn = effectiveTableModel.getColumns().get(0);
    assertThat(nameColumn.getLabel(), equalTo("Respondent Name"));
    // Now verify that we can update the column label
    view.resetChangeCounter();
    TableColumn updatedColumn = ImmutableTableColumn.builder().from(nameColumn.getModel()).label("MY column").build();
    viewModel.update(ImmutableTableModel.builder().from(tableModel).columns(Arrays.asList(updatedColumn)).build());
    setup.runScheduled();
    // Should receive a change event...
    view.assertChanged();
    EffectiveTableModel updatedModel = view.assertLoaded();
    assertThat(updatedModel.getColumns().get(0).getLabel(), equalTo("MY column"));
}
Also used : TableColumn(org.activityinfo.model.analysis.TableColumn) ImmutableTableColumn(org.activityinfo.model.analysis.ImmutableTableColumn) TableModel(org.activityinfo.model.analysis.TableModel) ImmutableTableModel(org.activityinfo.model.analysis.ImmutableTableModel) Test(org.junit.Test)

Example 2 with TableModel

use of org.activityinfo.model.analysis.TableModel in project activityinfo by bedatadriven.

the class TableViewModelTest method testSubFormExport.

@Test
public void testSubFormExport() {
    IncidentForm incidentForm = setup.getCatalog().getIncidentForm();
    TableModel tableModel = ImmutableTableModel.builder().formId(incidentForm.getFormId()).addColumns(ImmutableTableColumn.builder().label("My PCODE").formula(IncidentForm.PROTECTION_CODE_FIELD_ID.asString()).build()).build();
    TableViewModel viewModel = new TableViewModel(setup.getFormStore(), tableModel);
    Connection<TableModel> exportModel = setup.connect(viewModel.computeExportModel(Observable.just(ReferralSubForm.FORM_ID), Observable.just(ExportScope.SELECTED)));
    System.out.println(Json.stringify(exportModel.assertLoaded().toJson(), 2));
    assertThat(exportModel.assertLoaded().getFormId(), equalTo(ReferralSubForm.FORM_ID));
    assertThat(exportModel.assertLoaded().getColumns(), hasSize(3));
    TableColumn firstColumn = exportModel.assertLoaded().getColumns().get(0);
    assertThat(firstColumn.getLabel(), equalTo(Optional.of("My PCODE")));
    assertThat(firstColumn.getFormula(), equalTo(new CompoundExpr(new SymbolNode(ColumnModel.PARENT_SYMBOL), IncidentForm.PROTECTION_CODE_FIELD_ID).asExpression()));
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) TableColumn(org.activityinfo.model.analysis.TableColumn) ImmutableTableColumn(org.activityinfo.model.analysis.ImmutableTableColumn) TableModel(org.activityinfo.model.analysis.TableModel) ImmutableTableModel(org.activityinfo.model.analysis.ImmutableTableModel) IncidentForm(org.activityinfo.store.testing.IncidentForm) Test(org.junit.Test)

Example 3 with TableModel

use of org.activityinfo.model.analysis.TableModel in project activityinfo by bedatadriven.

the class TableViewModel method updateColumnWidth.

@Override
public void updateColumnWidth(String columnId, int newWidth) {
    TableModel model = this.tableModel.get();
    List<TableColumn> updatedColumns = new ArrayList<>();
    for (TableColumn column : model.getColumns()) {
        if (column.getId().equals(columnId)) {
            updatedColumns.add(ImmutableTableColumn.builder().from(column).width(newWidth).build());
        } else {
            updatedColumns.add(column);
        }
    }
    tableModel.updateIfNotSame(ImmutableTableModel.builder().from(model).columns(updatedColumns).build());
}
Also used : ArrayList(java.util.ArrayList) TableColumn(org.activityinfo.model.analysis.TableColumn) ImmutableTableColumn(org.activityinfo.model.analysis.ImmutableTableColumn) TableModel(org.activityinfo.model.analysis.TableModel) ImmutableTableModel(org.activityinfo.model.analysis.ImmutableTableModel)

Example 4 with TableModel

use of org.activityinfo.model.analysis.TableModel in project activityinfo by bedatadriven.

the class XlsTableWriterTest method surveyForm.

@Test
public void surveyForm() throws IOException {
    TableModel tableModel = ImmutableTableModel.builder().formId(catalog.getSurvey().getFormId()).build();
    assertThat(export(tableModel), sameWorkbook(getWorkBook("survey-expected.xls")));
}
Also used : ImmutableTableModel(org.activityinfo.model.analysis.ImmutableTableModel) EffectiveTableModel(org.activityinfo.analysis.table.EffectiveTableModel) TableModel(org.activityinfo.model.analysis.TableModel) Test(org.junit.Test)

Example 5 with TableModel

use of org.activityinfo.model.analysis.TableModel in project activityinfo by bedatadriven.

the class ExportFormExecutor method execute.

@Override
public ExportResult execute(ExportFormJob descriptor) throws IOException {
    TableModel tableModel = descriptor.getTableModel();
    GeneratedResource export = storageProvider.create(XlsTableWriter.EXCEL_MIME_TYPE, "Export.xls");
    TableViewModel viewModel = new TableViewModel(formSource, tableModel);
    EffectiveTableModel effectiveTableModel = viewModel.getEffectiveTable().waitFor();
    ColumnSet columnSet = effectiveTableModel.getColumnSet().waitFor();
    XlsTableWriter writer = new XlsTableWriter();
    writer.addSheet(effectiveTableModel, columnSet);
    try (OutputStream out = export.openOutputStream()) {
        writer.write(out);
    }
    return new ExportResult(export.getDownloadUri());
}
Also used : EffectiveTableModel(org.activityinfo.analysis.table.EffectiveTableModel) XlsTableWriter(org.activityinfo.io.xls.XlsTableWriter) TableViewModel(org.activityinfo.analysis.table.TableViewModel) OutputStream(java.io.OutputStream) ColumnSet(org.activityinfo.model.query.ColumnSet) GeneratedResource(org.activityinfo.server.generated.GeneratedResource) EffectiveTableModel(org.activityinfo.analysis.table.EffectiveTableModel) TableModel(org.activityinfo.model.analysis.TableModel) ExportResult(org.activityinfo.model.job.ExportResult)

Aggregations

TableModel (org.activityinfo.model.analysis.TableModel)12 ImmutableTableModel (org.activityinfo.model.analysis.ImmutableTableModel)11 Test (org.junit.Test)9 ImmutableTableColumn (org.activityinfo.model.analysis.ImmutableTableColumn)4 TableColumn (org.activityinfo.model.analysis.TableColumn)4 ColumnSet (org.activityinfo.model.query.ColumnSet)3 Optional (com.google.common.base.Optional)2 ArrayList (java.util.ArrayList)2 EffectiveTableModel (org.activityinfo.analysis.table.EffectiveTableModel)2 JsonValue (org.activityinfo.json.JsonValue)2 CompoundExpr (org.activityinfo.model.formula.CompoundExpr)2 SymbolNode (org.activityinfo.model.formula.SymbolNode)2 RecordRef (org.activityinfo.model.type.RecordRef)2 IncidentForm (org.activityinfo.store.testing.IncidentForm)2 Queue (com.google.appengine.api.taskqueue.Queue)1 Function (com.google.common.base.Function)1 VoidWork (com.googlecode.objectify.VoidWork)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 List (java.util.List)1