Search in sources :

Example 6 with Month

use of org.activityinfo.model.type.time.Month in project activityinfo by bedatadriven.

the class SitesResources method queryMonthlyReports.

@GET
@Path("{id}/monthlyReports")
@Produces("application/json")
@Timed(name = "api.rest.sites.monthly_reports")
public String queryMonthlyReports(@PathParam("id") int siteId) throws IOException {
    GetMonthlyReports command = new GetMonthlyReports(siteId, new Month(0, 1), new Month(Integer.MAX_VALUE, 12));
    MonthlyReportResult result = dispatcher.execute(command);
    // list all months
    Set<String> monthNames = Sets.newHashSet();
    for (IndicatorRowDTO row : result.getData()) {
        for (String propertyName : row.getPropertyNames()) {
            if (propertyName.startsWith("M")) {
                monthNames.add(propertyName);
            }
        }
    }
    // write out results per month
    StringWriter writer = new StringWriter();
    JsonGenerator json = Jackson.createJsonFactory(writer);
    json.writeStartObject();
    for (String monthName : monthNames) {
        json.writeArrayFieldStart(formatMonth(monthName));
        for (IndicatorRowDTO row : result.getData()) {
            if (row.get(monthName) instanceof Number) {
                json.writeStartObject();
                Number value = row.get(monthName);
                json.writeNumberField("indicatorId", row.getIndicatorId());
                json.writeStringField("indicatorName", row.getIndicatorName());
                json.writeNumberField("value", value.doubleValue());
                json.writeEndObject();
            }
        }
        json.writeEndArray();
    }
    json.writeEndObject();
    json.close();
    return writer.toString();
}
Also used : Month(org.activityinfo.model.type.time.Month) StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) MonthlyReportResult(org.activityinfo.legacy.shared.command.result.MonthlyReportResult) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 7 with Month

use of org.activityinfo.model.type.time.Month in project activityinfo by bedatadriven.

the class SitesResources method formatMonth.

private String formatMonth(String propertyName) {
    Month month = Month.parseMonth(propertyName.substring(1));
    String monthName = month.getYear() + "-";
    if (month.getMonth() < 10) {
        monthName += "0";
    }
    monthName += month.getMonth();
    return monthName;
}
Also used : Month(org.activityinfo.model.type.time.Month)

Example 8 with Month

use of org.activityinfo.model.type.time.Month in project activityinfo by bedatadriven.

the class CubeResource method pivot.

@GET
@Timed(name = "api.rest.sites.pivot")
@Produces("application/json")
public List<Bucket> pivot(@QueryParam("dimension") List<String> dimensions, @QueryParam("form") List<Integer> forms, @QueryParam("month") String monthName) {
    Filter filter = new Filter();
    if (forms.size() == 0) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Must specify at least one ?form={formId}").build());
    }
    filter.addRestriction(DimensionType.Activity, forms);
    if (monthName != null) {
        Month month = Month.parseMonth(monthName);
        filter.setEndDateRange(new DateUtilCalendarImpl().monthRange(month));
    }
    Set<Dimension> pivotDimensions = Sets.newHashSet();
    if (forms.size() == 0) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Must specify at least one ?dimension={indicator|form|database|...}").build());
    }
    for (String dimension : dimensions) {
        switch(dimension) {
            case "indicator":
                pivotDimensions.add(new Dimension(DimensionType.Indicator));
                break;
            case "site":
                pivotDimensions.add(new Dimension(DimensionType.Site));
                break;
            case "month":
                pivotDimensions.add(new DateDimension(DateUnit.MONTH));
                break;
            case "partner":
                pivotDimensions.add(new Dimension(DimensionType.Partner));
                break;
            default:
                throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Invalid dimension '" + dimension + "'").build());
        }
    }
    PivotSites query = new PivotSites();
    query.setFilter(filter);
    query.setDimensions(pivotDimensions);
    if (query.isTooBroad()) {
        return Lists.newArrayList();
    }
    PivotSites.PivotResult result = dispatcherSync.execute(query);
    return result.getBuckets();
}
Also used : Month(org.activityinfo.model.type.time.Month) PivotSites(org.activityinfo.legacy.shared.command.PivotSites) WebApplicationException(javax.ws.rs.WebApplicationException) Filter(org.activityinfo.legacy.shared.command.Filter) DateUtilCalendarImpl(org.activityinfo.server.report.util.DateUtilCalendarImpl) Dimension(org.activityinfo.legacy.shared.reports.model.Dimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) DateDimension(org.activityinfo.legacy.shared.reports.model.DateDimension) Produces(javax.ws.rs.Produces) Timed(org.activityinfo.server.util.monitoring.Timed) GET(javax.ws.rs.GET)

Example 9 with Month

use of org.activityinfo.model.type.time.Month in project activityinfo by bedatadriven.

the class FormModelTest method subformInstancesPersistence.

@Test
public void subformInstancesPersistence() {
    setupForms();
    FormInstance rootInstance = new FormInstance(ResourceId.generateSubmissionId(masterFormClass), masterFormClass.getId());
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.START_DATE_FIELD), new LocalDate(2016, 1, 1));
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.END_DATE_FIELD), new LocalDate(2016, 1, 1));
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.PARTNER_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.partnerFormId(1), CuidAdapter.partnerRecordId(1))));
    rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.LOCATION_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(1), CuidAdapter.locationInstanceId(1))));
    FormModel formModel = newFormModel();
    formModel.setWorkingRootInstance(rootInstance);
    String tab1 = new Month(2015, 3).toString();
    String tab2 = new Month(2015, 8).toString();
    // Tab1
    FormInstance valueInstance1 = formModel.getWorkingInstance(subFormChildField.getId(), tab1).get();
    valueInstance1.set(subFormChildField.getId(), TextValue.valueOf("tab1"));
    // Tab2
    FormInstance valueInstance2 = formModel.getWorkingInstance(subFormChildField.getId(), tab2).get();
    valueInstance2.set(subFormChildField.getId(), TextValue.valueOf("tab2"));
    formModel.getChangedInstances().add(valueInstance1);
    formModel.getChangedInstances().add(valueInstance2);
    // persist all value and tab/key instances
    FormActions actions = new FormActions(locator, formModel);
    assertResolves(actions.save());
    // make sure instances are persisted
    FormInstance fetchedInstance1 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance1.getId()));
    FormInstance fetchedInstance2 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance2.getId()));
    assertEquals(fetchedInstance1.get(subFormChildField.getId()), TextValue.valueOf("tab1"));
    assertEquals(fetchedInstance2.get(subFormChildField.getId()), TextValue.valueOf("tab2"));
    // Update value instances
    // Tab1
    valueInstance1 = formModel.getWorkingInstance(subFormChildField.getId(), tab1).get();
    valueInstance1.set(subFormChildField.getId(), TextValue.valueOf("tab11"));
    // Tab2
    valueInstance2 = formModel.getWorkingInstance(subFormChildField.getId(), tab2).get();
    valueInstance2.set(subFormChildField.getId(), TextValue.valueOf("tab22"));
    formModel.getChangedInstances().add(valueInstance1);
    formModel.getChangedInstances().add(valueInstance2);
    // persist updates
    assertResolves(actions.save());
    // make sure instances are persisted
    fetchedInstance1 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance1.getId()));
    fetchedInstance2 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance2.getId()));
    assertEquals(fetchedInstance1.get(subFormChildField.getId()), TextValue.valueOf("tab11"));
    assertEquals(fetchedInstance2.get(subFormChildField.getId()), TextValue.valueOf("tab22"));
    // check subform loader
    FormModel emptyModel = new FormModel(locator, new GxtStateProvider());
    emptyModel.setWorkingRootInstance(rootInstance);
    // load subform instances into empty model
    assertResolves(new SubFormInstanceLoader(emptyModel).load(subFormClass));
    Map<FormModel.SubformValueKey, Set<FormInstance>> loadedInstances = emptyModel.getSubFormInstances();
    assertEquals(1, loadedInstances.size());
    assertEquals(emptyModel.getSubformValueInstance(subFormClass, rootInstance, tab1).get(), valueInstance1);
    assertEquals(emptyModel.getSubformValueInstance(subFormClass, rootInstance, tab2).get(), valueInstance2);
}
Also used : SubFormInstanceLoader(org.activityinfo.ui.client.component.form.subform.SubFormInstanceLoader) Set(java.util.Set) OnDataSet(org.activityinfo.server.database.OnDataSet) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) GxtStateProvider(org.activityinfo.ui.client.dispatch.state.GxtStateProvider) LocalDate(org.activityinfo.model.type.time.LocalDate) Month(org.activityinfo.model.type.time.Month) FormInstance(org.activityinfo.model.form.FormInstance) Test(org.junit.Test)

Example 10 with Month

use of org.activityinfo.model.type.time.Month in project activityinfo by bedatadriven.

the class MonthlyReportsTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    ArrayList<UpdateMonthlyReports.Change> changes = new ArrayList<UpdateMonthlyReports.Change>();
    changes.add(new UpdateMonthlyReports.Change(6, new Month(2009, 1), 45.0));
    changes.add(new UpdateMonthlyReports.Change(6, new Month(2009, 3), 22.0));
    execute(new UpdateMonthlyReports(6, changes));
    // verify that that changes have been made
    GetMonthlyReports cmd = new GetMonthlyReports(6);
    cmd.setStartMonth(new Month(2009, 1));
    cmd.setEndMonth(new Month(2009, 3));
    MonthlyReportResult result = execute(cmd);
    Assert.assertEquals(1, result.getData().size());
    Assert.assertEquals(45, result.getData().get(0).getValue(2009, 1).intValue());
    Assert.assertEquals(70, result.getData().get(0).getValue(2009, 2).intValue());
    Assert.assertEquals(22, result.getData().get(0).getValue(2009, 3).intValue());
}
Also used : Month(org.activityinfo.model.type.time.Month) GetMonthlyReports(org.activityinfo.legacy.shared.command.GetMonthlyReports) UpdateMonthlyReports(org.activityinfo.legacy.shared.command.UpdateMonthlyReports) ArrayList(java.util.ArrayList) MonthlyReportResult(org.activityinfo.legacy.shared.command.result.MonthlyReportResult) Test(org.junit.Test)

Aggregations

Month (org.activityinfo.model.type.time.Month)23 Test (org.junit.Test)9 MonthlyReportResult (org.activityinfo.legacy.shared.command.result.MonthlyReportResult)7 ArrayList (java.util.ArrayList)5 UpdateMonthlyReports (org.activityinfo.legacy.shared.command.UpdateMonthlyReports)5 GetMonthlyReports (org.activityinfo.legacy.shared.command.GetMonthlyReports)3 IndicatorRowDTO (org.activityinfo.legacy.shared.model.IndicatorRowDTO)3 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)2 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)2 DateWrapper (com.extjs.gxt.ui.client.util.DateWrapper)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 Change (org.activityinfo.legacy.shared.command.UpdateMonthlyReports.Change)2 IllegalAccessCommandException (org.activityinfo.legacy.shared.exception.IllegalAccessCommandException)2 OnDataSet (org.activityinfo.server.database.OnDataSet)2 Timed (org.activityinfo.server.util.monitoring.Timed)2 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)1 FieldEvent (com.extjs.gxt.ui.client.event.FieldEvent)1 LabelToolItem (com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem)1