use of org.activityinfo.model.type.time.Month 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.model.type.time.Month in project activityinfo by bedatadriven.
the class SitesResources method formatMonth.
private String formatMonth(String propertyName) {
Month month = Month.parseMonth(propertyName.substring(1));
String monthName = month.getYear() + "-";
if (month.getMonth() < 10) {
monthName += "0";
}
monthName += month.getMonth();
return monthName;
}
use of org.activityinfo.model.type.time.Month in project activityinfo by bedatadriven.
the class CubeResource method pivot.
@GET
@Timed(name = "api.rest.sites.pivot")
@Produces("application/json")
public List<Bucket> pivot(@QueryParam("dimension") List<String> dimensions, @QueryParam("form") List<Integer> forms, @QueryParam("month") String monthName) {
Filter filter = new Filter();
if (forms.size() == 0) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Must specify at least one ?form={formId}").build());
}
filter.addRestriction(DimensionType.Activity, forms);
if (monthName != null) {
Month month = Month.parseMonth(monthName);
filter.setEndDateRange(new DateUtilCalendarImpl().monthRange(month));
}
Set<Dimension> pivotDimensions = Sets.newHashSet();
if (forms.size() == 0) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Must specify at least one ?dimension={indicator|form|database|...}").build());
}
for (String dimension : dimensions) {
switch(dimension) {
case "indicator":
pivotDimensions.add(new Dimension(DimensionType.Indicator));
break;
case "site":
pivotDimensions.add(new Dimension(DimensionType.Site));
break;
case "month":
pivotDimensions.add(new DateDimension(DateUnit.MONTH));
break;
case "partner":
pivotDimensions.add(new Dimension(DimensionType.Partner));
break;
default:
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Invalid dimension '" + dimension + "'").build());
}
}
PivotSites query = new PivotSites();
query.setFilter(filter);
query.setDimensions(pivotDimensions);
if (query.isTooBroad()) {
return Lists.newArrayList();
}
PivotSites.PivotResult result = dispatcherSync.execute(query);
return result.getBuckets();
}
use of org.activityinfo.model.type.time.Month in project activityinfo by bedatadriven.
the class FormModelTest method subformInstancesPersistence.
@Test
public void subformInstancesPersistence() {
setupForms();
FormInstance rootInstance = new FormInstance(ResourceId.generateSubmissionId(masterFormClass), masterFormClass.getId());
rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.START_DATE_FIELD), new LocalDate(2016, 1, 1));
rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.END_DATE_FIELD), new LocalDate(2016, 1, 1));
rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.PARTNER_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.partnerFormId(1), CuidAdapter.partnerRecordId(1))));
rootInstance.set(CuidAdapter.field(masterFormClass.getId(), CuidAdapter.LOCATION_FIELD), new ReferenceValue(new RecordRef(CuidAdapter.locationFormClass(1), CuidAdapter.locationInstanceId(1))));
FormModel formModel = newFormModel();
formModel.setWorkingRootInstance(rootInstance);
String tab1 = new Month(2015, 3).toString();
String tab2 = new Month(2015, 8).toString();
// Tab1
FormInstance valueInstance1 = formModel.getWorkingInstance(subFormChildField.getId(), tab1).get();
valueInstance1.set(subFormChildField.getId(), TextValue.valueOf("tab1"));
// Tab2
FormInstance valueInstance2 = formModel.getWorkingInstance(subFormChildField.getId(), tab2).get();
valueInstance2.set(subFormChildField.getId(), TextValue.valueOf("tab2"));
formModel.getChangedInstances().add(valueInstance1);
formModel.getChangedInstances().add(valueInstance2);
// persist all value and tab/key instances
FormActions actions = new FormActions(locator, formModel);
assertResolves(actions.save());
// make sure instances are persisted
FormInstance fetchedInstance1 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance1.getId()));
FormInstance fetchedInstance2 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance2.getId()));
assertEquals(fetchedInstance1.get(subFormChildField.getId()), TextValue.valueOf("tab1"));
assertEquals(fetchedInstance2.get(subFormChildField.getId()), TextValue.valueOf("tab2"));
// Update value instances
// Tab1
valueInstance1 = formModel.getWorkingInstance(subFormChildField.getId(), tab1).get();
valueInstance1.set(subFormChildField.getId(), TextValue.valueOf("tab11"));
// Tab2
valueInstance2 = formModel.getWorkingInstance(subFormChildField.getId(), tab2).get();
valueInstance2.set(subFormChildField.getId(), TextValue.valueOf("tab22"));
formModel.getChangedInstances().add(valueInstance1);
formModel.getChangedInstances().add(valueInstance2);
// persist updates
assertResolves(actions.save());
// make sure instances are persisted
fetchedInstance1 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance1.getId()));
fetchedInstance2 = assertResolves(locator.getFormInstance(subFormClass.getId(), valueInstance2.getId()));
assertEquals(fetchedInstance1.get(subFormChildField.getId()), TextValue.valueOf("tab11"));
assertEquals(fetchedInstance2.get(subFormChildField.getId()), TextValue.valueOf("tab22"));
// check subform loader
FormModel emptyModel = new FormModel(locator, new GxtStateProvider());
emptyModel.setWorkingRootInstance(rootInstance);
// load subform instances into empty model
assertResolves(new SubFormInstanceLoader(emptyModel).load(subFormClass));
Map<FormModel.SubformValueKey, Set<FormInstance>> loadedInstances = emptyModel.getSubFormInstances();
assertEquals(1, loadedInstances.size());
assertEquals(emptyModel.getSubformValueInstance(subFormClass, rootInstance, tab1).get(), valueInstance1);
assertEquals(emptyModel.getSubformValueInstance(subFormClass, rootInstance, tab2).get(), valueInstance2);
}
use of org.activityinfo.model.type.time.Month 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());
}
Aggregations