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();
}
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());
}
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) {
}
});
}
Aggregations