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