Search in sources :

Example 1 with IndicatorRowDTO

use of org.activityinfo.legacy.shared.model.IndicatorRowDTO 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 IndicatorRowDTO

use of org.activityinfo.legacy.shared.model.IndicatorRowDTO 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 IndicatorRowDTO

use of org.activityinfo.legacy.shared.model.IndicatorRowDTO in project activityinfo by bedatadriven.

the class SyncIntegrationTest method updateMonthlyReports.

@Test
@OnDataSet("/dbunit/monthly-calc-indicators.db.xml")
public void updateMonthlyReports() throws SQLException, InterruptedException {
    synchronize();
    int siteId = 1;
    MonthlyReportResult result = executeLocally(new GetMonthlyReports(siteId, new Month(2009, 1), 5));
    IndicatorRowDTO women = result.getData().get(0);
    IndicatorRowDTO men = result.getData().get(1);
    assertThat(women.getIndicatorName(), equalTo("women"));
    assertThat(women.getIndicatorId(), equalTo(7002));
    assertThat(men.getIndicatorName(), equalTo("men"));
    assertThat(men.getActivityName(), equalTo("NFI"));
    assertThat(men.getActivityId(), equalTo(901));
    assertThat(men.getIndicatorId(), equalTo(7001));
    assertThat(men.getValue(2009, 1), CoreMatchers.equalTo(200d));
    assertThat(women.getValue(2009, 1), equalTo(300d));
    assertThat(men.getValue(2009, 2), equalTo(150d));
    assertThat(women.getValue(2009, 2), equalTo(330d));
    // Update locally
    executeLocally(new UpdateMonthlyReports(siteId, Lists.newArrayList(new Change(men.getIndicatorId(), new Month(2009, 1), 221d), new Change(men.getIndicatorId(), new Month(2009, 3), 444d), new Change(women.getIndicatorId(), new Month(2009, 5), 200d), new Change(men.getIndicatorId(), new Month(2009, 5), 522d))));
    result = executeLocally(new GetMonthlyReports(siteId, new Month(2009, 1), 12));
    women = result.getData().get(0);
    men = result.getData().get(1);
    assertThat(men.getValue(2009, 1), equalTo(221d));
    assertThat(women.getValue(2009, 1), equalTo(300d));
    // same - no change
    assertThat(men.getValue(2009, 2), equalTo(150d));
    assertThat(women.getValue(2009, 2), equalTo(330d));
    // new periods
    assertThat(men.getValue(2009, 3), equalTo(444d));
    assertThat(women.getValue(2009, 5), equalTo(200d));
    assertThat(men.getValue(2009, 5), equalTo(522d));
    // Synchronize
    synchronize();
    newRequest();
    MonthlyReportResult remoteResult = executeRemotely(new GetMonthlyReports(siteId, new Month(2009, 1), 12));
    women = remoteResult.getData().get(0);
    men = remoteResult.getData().get(1);
    assertThat(men.getValue(2009, 1), equalTo(221d));
    assertThat(women.getValue(2009, 1), equalTo(300d));
    // same - no change
    assertThat(men.getValue(2009, 2), equalTo(150d));
    assertThat(women.getValue(2009, 2), equalTo(330d));
    // new periods
    assertThat(men.getValue(2009, 3), equalTo(444d));
    assertThat(women.getValue(2009, 5), equalTo(200d));
    assertThat(men.getValue(2009, 5), equalTo(522d));
    newRequest();
    // REmote update
    executeRemotely(new UpdateMonthlyReports(siteId, Lists.newArrayList(new Change(men.getIndicatorId(), new Month(2009, 1), 40d), new Change(women.getIndicatorId(), new Month(2009, 3), 6000d))));
    newRequest();
    synchronize();
    result = executeLocally(new GetMonthlyReports(siteId, new Month(2009, 1), 5));
    women = result.getData().get(0);
    men = result.getData().get(1);
    assertThat(men.getValue(2009, 1), CoreMatchers.equalTo(40d));
    // unchanged
    assertThat(women.getValue(2009, 1), CoreMatchers.equalTo(300d));
    assertThat(women.getValue(2009, 3), equalTo(6000d));
}
Also used : Month(org.activityinfo.model.type.time.Month) Change(org.activityinfo.legacy.shared.command.UpdateMonthlyReports.Change) MonthlyReportResult(org.activityinfo.legacy.shared.command.result.MonthlyReportResult) IndicatorRowDTO(org.activityinfo.legacy.shared.model.IndicatorRowDTO) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Aggregations

MonthlyReportResult (org.activityinfo.legacy.shared.command.result.MonthlyReportResult)3 IndicatorRowDTO (org.activityinfo.legacy.shared.model.IndicatorRowDTO)3 Month (org.activityinfo.model.type.time.Month)3 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)1 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)1 Function (com.google.common.base.Function)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Nullable (javax.annotation.Nullable)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 Test (org.junit.Test)1