Search in sources :

Example 1 with UpdateMonthlyReports

use of org.activityinfo.shared.command.UpdateMonthlyReports in project activityinfo by bedatadriven.

the class UpdateMonthlyReportsHandler method execute.

@Override
public CommandResult execute(UpdateMonthlyReports cmd, User user) throws CommandException {
    Site site = em.find(Site.class, cmd.getSiteId());
    if (site == null) {
        throw new CommandException(cmd, "site " + cmd.getSiteId() + " not found for user " + user.getEmail());
    }
    Map<Month, ReportingPeriod> periods = Maps.newHashMap();
    Map<String, Object> siteHistoryChangeMap = createChangeMap();
    for (ReportingPeriod period : site.getReportingPeriods()) {
        periods.put(HandlerUtil.monthFromRange(period.getDate1(), period.getDate2()), period);
    }
    for (UpdateMonthlyReports.Change change : cmd.getChanges()) {
        ReportingPeriod period = periods.get(change.getMonth());
        if (period == null) {
            period = new ReportingPeriod(site);
            period.setId(keyGenerator.generateInt());
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, change.getMonth().getYear());
            calendar.set(Calendar.MONTH, change.getMonth().getMonth() - 1);
            calendar.set(Calendar.DATE, 1);
            period.setDate1(calendar.getTime());
            calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
            period.setDate2(calendar.getTime());
            em.persist(period);
            periods.put(change.getMonth(), period);
        }
        updateIndicatorValue(em, period, change.getIndicatorId(), change.getValue(), false);
        siteHistoryChangeMap.put(IndicatorDTO.getPropertyName(change.getIndicatorId(), change.getMonth()), change.getValue());
    }
    siteHistoryProcessor.persistHistory(site, user, ChangeType.UPDATE, siteHistoryChangeMap);
    return new VoidResult();
}
Also used : Site(org.activityinfo.server.database.hibernate.entity.Site) Month(org.activityinfo.shared.command.Month) ReportingPeriod(org.activityinfo.server.database.hibernate.entity.ReportingPeriod) UpdateMonthlyReports(org.activityinfo.shared.command.UpdateMonthlyReports) VoidResult(org.activityinfo.shared.command.result.VoidResult) Calendar(java.util.Calendar) CommandException(org.activityinfo.shared.exception.CommandException)

Example 2 with UpdateMonthlyReports

use of org.activityinfo.shared.command.UpdateMonthlyReports 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.shared.command.Month) GetMonthlyReports(org.activityinfo.shared.command.GetMonthlyReports) UpdateMonthlyReports(org.activityinfo.shared.command.UpdateMonthlyReports) ArrayList(java.util.ArrayList) MonthlyReportResult(org.activityinfo.shared.command.result.MonthlyReportResult) Test(org.junit.Test)

Example 3 with UpdateMonthlyReports

use of org.activityinfo.shared.command.UpdateMonthlyReports in project activityinfo by bedatadriven.

the class MonthlyReportsPanel method save.

private void save() {
    ArrayList<UpdateMonthlyReports.Change> changes = new ArrayList<UpdateMonthlyReports.Change>();
    for (Record record : store.getModifiedRecords()) {
        IndicatorRowDTO report = (IndicatorRowDTO) record.getModel();
        for (String property : record.getChanges().keySet()) {
            UpdateMonthlyReports.Change change = new UpdateMonthlyReports.Change();
            change.setIndicatorId(report.getIndicatorId());
            change.setMonth(IndicatorRowDTO.monthForProperty(property));
            change.setValue((Double) report.get(property));
            changes.add(change);
        }
    }
    service.execute(new UpdateMonthlyReports(currentSiteId, changes), new MaskingAsyncMonitor(this, I18N.CONSTANTS.save()), new AsyncCallback<VoidResult>() {

        @Override
        public void onFailure(Throwable caught) {
        // handled by monitor
        }

        @Override
        public void onSuccess(VoidResult result) {
        }
    });
}
Also used : UpdateMonthlyReports(org.activityinfo.shared.command.UpdateMonthlyReports) VoidResult(org.activityinfo.shared.command.result.VoidResult) ArrayList(java.util.ArrayList) IndicatorRowDTO(org.activityinfo.shared.dto.IndicatorRowDTO) MaskingAsyncMonitor(org.activityinfo.client.dispatch.monitor.MaskingAsyncMonitor) Record(com.extjs.gxt.ui.client.store.Record)

Aggregations

UpdateMonthlyReports (org.activityinfo.shared.command.UpdateMonthlyReports)3 ArrayList (java.util.ArrayList)2 Month (org.activityinfo.shared.command.Month)2 VoidResult (org.activityinfo.shared.command.result.VoidResult)2 Record (com.extjs.gxt.ui.client.store.Record)1 Calendar (java.util.Calendar)1 MaskingAsyncMonitor (org.activityinfo.client.dispatch.monitor.MaskingAsyncMonitor)1 ReportingPeriod (org.activityinfo.server.database.hibernate.entity.ReportingPeriod)1 Site (org.activityinfo.server.database.hibernate.entity.Site)1 GetMonthlyReports (org.activityinfo.shared.command.GetMonthlyReports)1 MonthlyReportResult (org.activityinfo.shared.command.result.MonthlyReportResult)1 IndicatorRowDTO (org.activityinfo.shared.dto.IndicatorRowDTO)1 CommandException (org.activityinfo.shared.exception.CommandException)1 Test (org.junit.Test)1