use of org.activityinfo.model.type.time.Month in project activityinfo by bedatadriven.
the class CommandQueue method deserializeMonthlyReports.
private Command deserializeMonthlyReports(JsonObject root) {
int siteId = root.get("siteId").getAsInt();
JsonArray changeArray = root.get("changes").getAsJsonArray();
ArrayList<Change> changes = Lists.newArrayList();
for (int i = 0; i != changeArray.size(); ++i) {
JsonObject changeObject = changeArray.get(i).getAsJsonObject();
int indicatorId = changeObject.get("indicatorId").getAsInt();
Month month = new Month(changeObject.get("year").getAsInt(), changeObject.get("month").getAsInt());
Double value = null;
if (changeObject.get("value").isJsonPrimitive()) {
value = changeObject.get("value").getAsDouble();
}
changes.add(new Change(indicatorId, month, value));
}
return new UpdateMonthlyReports(siteId, changes);
}
use of org.activityinfo.model.type.time.Month in project activityinfo by bedatadriven.
the class MonthlyReportsPanel method addToolBar.
private void addToolBar() {
toolBar = new ActionToolBar();
toolBar.setListener(this);
toolBar.addSaveSplitButton();
toolBar.add(new LabelToolItem(I18N.CONSTANTS.month() + ": "));
monthCombo = new MappingComboBox<>();
monthCombo.setEditable(false);
monthCombo.addListener(Events.Select, new Listener<FieldEvent>() {
@Override
public void handleEvent(FieldEvent be) {
selectStartMonth(monthCombo.getMappedValue());
}
});
DateWrapper today = new DateWrapper();
DateTimeFormat monthFormat = DateTimeFormat.getFormat("MMM yyyy");
for (int year = today.getFullYear() + 2; year != today.getFullYear() - 3; --year) {
for (int month = 12; month != 0; --month) {
DateWrapper d = new DateWrapper(year, month, 1);
Month m = new Month(year, month);
monthCombo.add(m, monthFormat.format(d.asDate()));
}
}
toolBar.add(monthCombo);
toolBar.setDirty(false);
setTopComponent(toolBar);
}
use of org.activityinfo.model.type.time.Month 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.model.type.time.Month in project activityinfo by bedatadriven.
the class UpdateMonthlyReportsHandler method execute.
@Override
public CommandResult execute(UpdateMonthlyReports cmd, User user) throws CommandException {
// Phantom Row issue occurs when attempting to update Monthly ReportingPeriods concurrently.
// To prevent this, we introduce a locking mechanism to prevent simultaneous insertions into table which result
// in duplicate reporting periods on the given site.
// Once we have acquired a lock, we can then safely execute the command
acquireLock(cmd.getSiteId());
try {
Site site = em.find(Site.class, cmd.getSiteId());
if (site == null) {
throw new CommandException(cmd, "site " + cmd.getSiteId() + " not found for user " + user.getEmail());
}
if (!permissionOracle.isEditAllowed(site, user)) {
throw new IllegalAccessCommandException("Not authorized to modify sites");
}
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()) {
if (!periods.containsKey(change.getMonth())) {
ReportingPeriod 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);
}
}
for (UpdateMonthlyReports.Change change : cmd.getChanges()) {
updateIndicatorValue(em, periods.get(change.getMonth()), change.getIndicatorId(), change.getValue(), false);
siteHistoryChangeMap.put(getPropertyName(change.getIndicatorId(), change.getMonth()), change.getValue());
}
// update the timestamp on the site entity so changes get picked up
// by the synchro mechanism
site.setVersion(site.getActivity().incrementSiteVersion());
siteHistoryProcessor.persistHistory(site, user, ChangeType.UPDATE, siteHistoryChangeMap);
} finally {
releaseLock(cmd.getSiteId());
}
return new VoidResult();
}
use of org.activityinfo.model.type.time.Month 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);
}
Aggregations