Search in sources :

Example 1 with Month

use of org.activityinfo.shared.command.Month in project activityinfo by bedatadriven.

the class ItemDetail method create.

static ItemDetail create(RenderContext ctx, Map.Entry<String, Object> entry) {
    ItemDetail d = new ItemDetail();
    Map<String, Object> state = ctx.getState();
    SchemaDTO schema = ctx.getSchema();
    String key = entry.getKey();
    final Object oldValue = state.get(key);
    final Object newValue = entry.getValue();
    state.put(key, newValue);
    final StringBuilder sb = new StringBuilder();
    // basic
    if (key.equals("date1")) {
        addValues(sb, I18N.CONSTANTS.startDate(), oldValue, newValue);
    } else if (key.equals("date2")) {
        addValues(sb, I18N.CONSTANTS.endDate(), oldValue, newValue);
    } else if (key.equals("comments")) {
        addValues(sb, I18N.CONSTANTS.comments(), oldValue, newValue);
    } else if (key.equals("locationId")) {
        // schema loookups
        String oldName = null;
        if (oldValue != null) {
            oldName = ctx.getLocation(toInt(oldValue)).getName();
        }
        String newName = ctx.getLocation(toInt(newValue)).getName();
        addValues(sb, I18N.CONSTANTS.location(), oldName, newName);
    } else if (key.equals("projectId")) {
        String oldName = null;
        if (oldValue != null) {
            oldName = schema.getProjectById(toInt(oldValue)).getName();
        }
        String newName = schema.getProjectById(toInt(newValue)).getName();
        addValues(sb, I18N.CONSTANTS.project(), oldName, newName);
    } else if (key.equals("partnerId")) {
        String oldName = null;
        if (oldValue != null) {
            oldName = schema.getPartnerById(toInt(oldValue)).getName();
        }
        String newName = schema.getPartnerById(toInt(newValue)).getName();
        addValues(sb, I18N.CONSTANTS.partner(), oldName, newName);
    } else if (key.startsWith(IndicatorDTO.PROPERTY_PREFIX)) {
        // custom
        int id = IndicatorDTO.indicatorIdForPropertyName(key);
        IndicatorDTO dto = schema.getIndicatorById(id);
        String name = dto.getName();
        Month m = IndicatorDTO.monthForPropertyName(key);
        if (m != null) {
            name = I18N.MESSAGES.siteHistoryIndicatorName(name, m.toLocalDate().atMidnightInMyTimezone());
        }
        addValues(sb, name, oldValue, newValue, dto.getUnits());
    } else if (key.startsWith(AttributeDTO.PROPERTY_PREFIX)) {
        int id = AttributeDTO.idForPropertyName(key);
        AttributeDTO dto = schema.getAttributeById(id);
        if (Boolean.parseBoolean(newValue.toString())) {
            sb.append(I18N.MESSAGES.siteHistoryAttrAdd(dto.getName()));
        } else {
            sb.append(I18N.MESSAGES.siteHistoryAttrRemove(dto.getName()));
        }
    } else {
        // fallback
        addValues(sb, key, oldValue, newValue);
    }
    d.stringValue = sb.toString();
    return d;
}
Also used : AttributeDTO(org.activityinfo.shared.dto.AttributeDTO) Month(org.activityinfo.shared.command.Month) IndicatorDTO(org.activityinfo.shared.dto.IndicatorDTO) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO)

Example 2 with Month

use of org.activityinfo.shared.command.Month in project activityinfo by bedatadriven.

the class GetMonthlyReportsHandler method execute.

@Override
public CommandResult execute(GetMonthlyReports cmd, User user) throws CommandException {
    List<ReportingPeriod> periods = em.createQuery("select p from ReportingPeriod p where p.site.id = ?1").setParameter(1, cmd.getSiteId()).getResultList();
    List<Indicator> indicators = em.createQuery("select i from Indicator i where i.activity.id =" + "(select s.activity.id from Site s where s.id = ?1)").setParameter(1, 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());
        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 : ReportingPeriod(org.activityinfo.server.database.hibernate.entity.ReportingPeriod) Month(org.activityinfo.shared.command.Month) IndicatorValue(org.activityinfo.server.database.hibernate.entity.IndicatorValue) ArrayList(java.util.ArrayList) MonthlyReportResult(org.activityinfo.shared.command.result.MonthlyReportResult) Indicator(org.activityinfo.server.database.hibernate.entity.Indicator) IndicatorRowDTO(org.activityinfo.shared.dto.IndicatorRowDTO)

Example 3 with Month

use of org.activityinfo.shared.command.Month 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();
}
Also used : Site(org.activityinfo.server.database.hibernate.entity.Site) Month(org.activityinfo.shared.command.Month) ReportingPeriod(org.activityinfo.server.database.hibernate.entity.ReportingPeriod) UpdateMonthlyReports(org.activityinfo.shared.command.UpdateMonthlyReports) VoidResult(org.activityinfo.shared.command.result.VoidResult) Calendar(java.util.Calendar) CommandException(org.activityinfo.shared.exception.CommandException)

Example 4 with Month

use of org.activityinfo.shared.command.Month 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());
}
Also used : Month(org.activityinfo.shared.command.Month) GetMonthlyReports(org.activityinfo.shared.command.GetMonthlyReports) MonthlyReportResult(org.activityinfo.shared.command.result.MonthlyReportResult) Test(org.junit.Test)

Example 5 with Month

use of org.activityinfo.shared.command.Month in project activityinfo by bedatadriven.

the class MonthlyReportsTest method testMonthCompare.

@Test
public void testMonthCompare() throws Exception {
    Month feb = new Month(2009, 2);
    Month maxMonth = new Month(2009, 2);
    Assert.assertEquals(0, maxMonth.compareTo(feb));
}
Also used : Month(org.activityinfo.shared.command.Month) Test(org.junit.Test)

Aggregations

Month (org.activityinfo.shared.command.Month)13 MonthlyReportResult (org.activityinfo.shared.command.result.MonthlyReportResult)4 Test (org.junit.Test)4 DateWrapper (com.extjs.gxt.ui.client.util.DateWrapper)3 GetMonthlyReports (org.activityinfo.shared.command.GetMonthlyReports)3 DateTimeFormat (com.google.gwt.i18n.client.DateTimeFormat)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 ReportingPeriod (org.activityinfo.server.database.hibernate.entity.ReportingPeriod)2 UpdateMonthlyReports (org.activityinfo.shared.command.UpdateMonthlyReports)2 FieldEvent (com.extjs.gxt.ui.client.event.FieldEvent)1 LabelToolItem (com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem)1 ActionToolBar (org.activityinfo.client.page.common.toolbar.ActionToolBar)1 Indicator (org.activityinfo.server.database.hibernate.entity.Indicator)1 IndicatorValue (org.activityinfo.server.database.hibernate.entity.IndicatorValue)1 Site (org.activityinfo.server.database.hibernate.entity.Site)1 VoidResult (org.activityinfo.shared.command.result.VoidResult)1 AttributeDTO (org.activityinfo.shared.dto.AttributeDTO)1 IndicatorDTO (org.activityinfo.shared.dto.IndicatorDTO)1 IndicatorRowDTO (org.activityinfo.shared.dto.IndicatorRowDTO)1