Search in sources :

Example 1 with Month

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);
}
Also used : JsonArray(com.google.gson.JsonArray) Month(org.activityinfo.model.type.time.Month) JsonObject(com.google.gson.JsonObject) Change(org.activityinfo.legacy.shared.command.UpdateMonthlyReports.Change)

Example 2 with Month

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);
}
Also used : LabelToolItem(com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem) Month(org.activityinfo.model.type.time.Month) FieldEvent(com.extjs.gxt.ui.client.event.FieldEvent) DateWrapper(com.extjs.gxt.ui.client.util.DateWrapper) ActionToolBar(org.activityinfo.ui.client.page.common.toolbar.ActionToolBar) DateTimeFormat(com.google.gwt.i18n.client.DateTimeFormat)

Example 3 with Month

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);
}
Also used : HashMap(java.util.HashMap) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) MonthlyReportResult(org.activityinfo.legacy.shared.command.result.MonthlyReportResult) IndicatorRowDTO(org.activityinfo.legacy.shared.model.IndicatorRowDTO) Date(java.util.Date) Function(com.google.common.base.Function) Month(org.activityinfo.model.type.time.Month) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) Nullable(javax.annotation.Nullable)

Example 4 with Month

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();
}
Also used : UpdateMonthlyReports(org.activityinfo.legacy.shared.command.UpdateMonthlyReports) VoidResult(org.activityinfo.legacy.shared.command.result.VoidResult) Calendar(java.util.Calendar) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException) CommandException(org.activityinfo.legacy.shared.exception.CommandException) Month(org.activityinfo.model.type.time.Month) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException)

Example 5 with Month

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);
}
Also used : Month(org.activityinfo.model.type.time.Month) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException) ArrayList(java.util.ArrayList) MonthlyReportResult(org.activityinfo.legacy.shared.command.result.MonthlyReportResult) IndicatorRowDTO(org.activityinfo.legacy.shared.model.IndicatorRowDTO)

Aggregations

Month (org.activityinfo.model.type.time.Month)23 Test (org.junit.Test)9 MonthlyReportResult (org.activityinfo.legacy.shared.command.result.MonthlyReportResult)7 ArrayList (java.util.ArrayList)5 UpdateMonthlyReports (org.activityinfo.legacy.shared.command.UpdateMonthlyReports)5 GetMonthlyReports (org.activityinfo.legacy.shared.command.GetMonthlyReports)3 IndicatorRowDTO (org.activityinfo.legacy.shared.model.IndicatorRowDTO)3 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)2 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)2 DateWrapper (com.extjs.gxt.ui.client.util.DateWrapper)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 Change (org.activityinfo.legacy.shared.command.UpdateMonthlyReports.Change)2 IllegalAccessCommandException (org.activityinfo.legacy.shared.exception.IllegalAccessCommandException)2 OnDataSet (org.activityinfo.server.database.OnDataSet)2 Timed (org.activityinfo.server.util.monitoring.Timed)2 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)1 FieldEvent (com.extjs.gxt.ui.client.event.FieldEvent)1 LabelToolItem (com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem)1