Search in sources :

Example 1 with MonthlyReportResult

use of org.activityinfo.legacy.shared.command.result.MonthlyReportResult in project activityinfo by bedatadriven.

the class GetMonthlyReportsHandlerAsync method execute.

@Override
public void execute(final GetMonthlyReports command, final ExecutionContext context, AsyncCallback<MonthlyReportResult> callback) {
    final Promise<SqlResultSet> indicators = queryIndicators(command, context);
    final Promise<SqlResultSet> periods = queryPeriods(command, context);
    Promise.waitAll(indicators, periods).then(new Function<Void, MonthlyReportResult>() {

        @Nullable
        @Override
        public MonthlyReportResult apply(@Nullable Void input) {
            Map<Integer, IndicatorRowDTO> indicatorMap = new HashMap<Integer, IndicatorRowDTO>();
            List<IndicatorRowDTO> rows = Lists.newArrayList();
            for (SqlResultSetRow indicatorRow : indicators.get().getRows()) {
                IndicatorRowDTO dto = new IndicatorRowDTO();
                dto.setIndicatorId(indicatorRow.getInt("indicatorId"));
                dto.setSiteId(command.getSiteId());
                dto.setIndicatorName(indicatorRow.getString("indicatorName"));
                dto.setCategory(indicatorRow.getString("category"));
                dto.setActivityName(indicatorRow.getString("activityName"));
                dto.setActivityId(indicatorRow.getInt("activityId"));
                indicatorMap.put(dto.getIndicatorId(), dto);
                rows.add(dto);
            }
            for (SqlResultSetRow period : periods.get().getRows()) {
                Date endDate = period.getDate("Date2");
                Month month = Month.of(endDate);
                if (month.compareTo(command.getStartMonth()) >= 0 && month.compareTo(command.getEndMonth()) <= 0) {
                    IndicatorRowDTO row = indicatorMap.get(period.getInt("indicatorId"));
                    if (row != null) {
                        row.setValue(month, period.getDouble("value"));
                    }
                }
            }
            return new MonthlyReportResult(rows);
        }
    }).then(callback);
}
Also used : HashMap(java.util.HashMap) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) MonthlyReportResult(org.activityinfo.legacy.shared.command.result.MonthlyReportResult) IndicatorRowDTO(org.activityinfo.legacy.shared.model.IndicatorRowDTO) Date(java.util.Date) Function(com.google.common.base.Function) Month(org.activityinfo.model.type.time.Month) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) Nullable(javax.annotation.Nullable)

Example 2 with MonthlyReportResult

use of org.activityinfo.legacy.shared.command.result.MonthlyReportResult in project activityinfo by bedatadriven.

the class GetMonthlyReportsHandler method execute.

@Override
public CommandResult execute(GetMonthlyReports cmd, User user) throws CommandException {
    Site site = em.find(Site.class, cmd.getSiteId());
    if (!permissionOracle.isViewAllowed(site, user)) {
        LOGGER.severe("User " + user.getEmail() + " has no view privs on site " + site.getId() + "," + "partner = " + site.getPartner().getName() + " " + site.getPartner().getId());
        throw new IllegalAccessCommandException();
    }
    List<ReportingPeriod> periods = em.createQuery("SELECT p from ReportingPeriod p WHERE p.site.id = :siteId", ReportingPeriod.class).setParameter("siteId", cmd.getSiteId()).getResultList();
    List<Indicator> indicators = em.createQuery("SELECT i from Indicator i " + "WHERE i.activity.id IN (SELECT s.activity.id FROM Site s WHERE s.id = :siteId) " + "AND i.dateDeleted IS NULL " + "ORDER BY i.sortOrder", Indicator.class).setParameter("siteId", cmd.getSiteId()).getResultList();
    List<IndicatorRowDTO> list = new ArrayList<IndicatorRowDTO>();
    for (Indicator indicator : indicators) {
        IndicatorRowDTO dto = new IndicatorRowDTO();
        dto.setIndicatorId(indicator.getId());
        dto.setSiteId(cmd.getSiteId());
        dto.setIndicatorName(indicator.getName());
        dto.setCategory(indicator.getCategory());
        dto.setActivityName(indicator.getActivity().getName());
        dto.setExpression(indicator.getExpression());
        for (ReportingPeriod period : periods) {
            Month month = HandlerUtil.monthFromRange(period.getDate1(), period.getDate2());
            if (month != null && month.compareTo(cmd.getStartMonth()) >= 0 && month.compareTo(cmd.getEndMonth()) <= 0) {
                for (IndicatorValue value : period.getIndicatorValues()) {
                    if (value.getIndicator().getId() == indicator.getId()) {
                        dto.setValue(month, value.getValue());
                    }
                }
            }
        }
        list.add(dto);
    }
    return new MonthlyReportResult(list);
}
Also used : Month(org.activityinfo.model.type.time.Month) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException) ArrayList(java.util.ArrayList) MonthlyReportResult(org.activityinfo.legacy.shared.command.result.MonthlyReportResult) IndicatorRowDTO(org.activityinfo.legacy.shared.model.IndicatorRowDTO)

Example 3 with MonthlyReportResult

use of org.activityinfo.legacy.shared.command.result.MonthlyReportResult 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 4 with MonthlyReportResult

use of org.activityinfo.legacy.shared.command.result.MonthlyReportResult 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)

Example 5 with MonthlyReportResult

use of org.activityinfo.legacy.shared.command.result.MonthlyReportResult in project activityinfo by bedatadriven.

the class MonthlyReportsTest method testGetReportsWhenEmpty.

@Test
public void testGetReportsWhenEmpty() throws Exception {
    GetMonthlyReports cmd = new GetMonthlyReports(7);
    cmd.setStartMonth(new Month(2009, 1));
    cmd.setEndMonth(new Month(2009, 2));
    MonthlyReportResult result = execute(cmd);
    Assert.assertEquals(1, result.getData().size());
}
Also used : Month(org.activityinfo.model.type.time.Month) GetMonthlyReports(org.activityinfo.legacy.shared.command.GetMonthlyReports) MonthlyReportResult(org.activityinfo.legacy.shared.command.result.MonthlyReportResult) Test(org.junit.Test)

Aggregations

MonthlyReportResult (org.activityinfo.legacy.shared.command.result.MonthlyReportResult)7 Month (org.activityinfo.model.type.time.Month)7 Test (org.junit.Test)4 GetMonthlyReports (org.activityinfo.legacy.shared.command.GetMonthlyReports)3 IndicatorRowDTO (org.activityinfo.legacy.shared.model.IndicatorRowDTO)3 ArrayList (java.util.ArrayList)2 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)1 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)1 Function (com.google.common.base.Function)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Nullable (javax.annotation.Nullable)1 UpdateMonthlyReports (org.activityinfo.legacy.shared.command.UpdateMonthlyReports)1 Change (org.activityinfo.legacy.shared.command.UpdateMonthlyReports.Change)1 IllegalAccessCommandException (org.activityinfo.legacy.shared.exception.IllegalAccessCommandException)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 Timed (org.activityinfo.server.util.monitoring.Timed)1 JsonGenerator (org.codehaus.jackson.JsonGenerator)1