Search in sources :

Example 11 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class DatePeriodResourceTable method getPopulateTempTableContent.

@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
    List<PeriodType> periodTypes = PeriodType.getAvailablePeriodTypes();
    List<Object[]> batchArgs = new ArrayList<>();
    //TODO
    Date startDate = new Cal(1975, 1, 1, true).time();
    Date endDate = new Cal(2025, 1, 1, true).time();
    List<Period> days = new UniqueArrayList<>(new DailyPeriodType().generatePeriods(startDate, endDate));
    Calendar calendar = PeriodType.getCalendar();
    for (Period day : days) {
        List<Object> values = new ArrayList<>();
        values.add(day.getStartDate());
        for (PeriodType periodType : periodTypes) {
            values.add(periodType.createPeriod(day.getStartDate(), calendar).getIsoDate());
        }
        batchArgs.add(values.toArray());
    }
    return Optional.of(batchArgs);
}
Also used : DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) DailyPeriodType(org.hisp.dhis.period.DailyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) Calendar(org.hisp.dhis.calendar.Calendar) UniqueArrayList(org.hisp.dhis.commons.collection.UniqueArrayList) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) UniqueArrayList(org.hisp.dhis.commons.collection.UniqueArrayList) Date(java.util.Date) Cal(org.hisp.dhis.period.Cal)

Example 12 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class IdentifiableObjectUtilsTest method testGetPeriodByPeriodType.

@Test
public void testGetPeriodByPeriodType() {
    Calendar calendar = Iso8601Calendar.getInstance();
    WeeklyPeriodType weekly = new WeeklyPeriodType();
    MonthlyPeriodType monthly = new MonthlyPeriodType();
    QuarterlyPeriodType quarterly = new QuarterlyPeriodType();
    YearlyPeriodType yearly = new YearlyPeriodType();
    assertEquals(PeriodType.getPeriodFromIsoString("2017W10"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("20170308"), weekly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("2017W9"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("20170301"), weekly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017W8"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017W9"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201705"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017W21"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201706"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017W22"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017WedW8"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017WedW9"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017ThuW8"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017ThuW9"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SatW7"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SatW8"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW7"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW8"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201702"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW7"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("201703"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017SunW8"), monthly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("2017Q1"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("201703"), quarterly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("2017Q2"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("201704"), quarterly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("2016"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2016Q4"), yearly, calendar));
    assertEquals(PeriodType.getPeriodFromIsoString("2017"), IdentifiableObjectUtils.getPeriodByPeriodType(PeriodType.getPeriodFromIsoString("2017Q1"), yearly, calendar));
}
Also used : WeeklyPeriodType(org.hisp.dhis.period.WeeklyPeriodType) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) QuarterlyPeriodType(org.hisp.dhis.period.QuarterlyPeriodType) Iso8601Calendar(org.hisp.dhis.calendar.impl.Iso8601Calendar) Calendar(org.hisp.dhis.calendar.Calendar) YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) Test(org.junit.Test)

Example 13 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class GetReportParamsAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() {
    if (mode == null || uid == null) {
        return SUCCESS;
    }
    RelativePeriods relatives = null;
    if (MODE_REPORT_TABLE.equals(mode)) {
        ReportTable reportTable = reportTableService.getReportTable(uid);
        if (reportTable != null) {
            reportParams = reportTable.getReportParams();
            relatives = reportTable.getRelatives();
        }
    } else if (MODE_REPORT.equals(mode)) {
        Report report = reportService.getReport(uid);
        if (report != null && report.isTypeReportTable()) {
            reportParams = report.getReportTable().getReportParams();
            relatives = report.getReportTable().getRelatives();
        } else if (report != null && (report.isTypeJdbc() || report.isTypeHtml())) {
            reportParams = report.getReportParams();
            relatives = report.getRelatives();
        }
        if (type == null && report != null) {
            // Set type based on report
            type = report.getType();
        }
    }
    if (reportParams != null && reportParams.isParamReportingMonth() && relatives != null) {
        CalendarPeriodType periodType = (CalendarPeriodType) relatives.getPeriodType();
        List<Period> periods = periodType.generateLast5Years(new Date());
        Collections.reverse(periods);
        FilterUtils.filter(periods, new PastAndCurrentPeriodFilter());
        Calendar calendar = PeriodType.getCalendar();
        for (Period period_ : periods) {
            BaseIdentifiableObject period = new BaseIdentifiableObject();
            if (calendar.isIso8601()) {
                period.setUid(period_.getIsoDate());
                period.setDisplayName(format.formatPeriod(period_));
            } else {
                DateTimeUnit dateTimeUnit = calendar.fromIso(period_.getStartDate());
                period.setUid(period_.getPeriodType().getIsoDate(dateTimeUnit));
                period.setDisplayName(format.formatPeriod(period_));
            }
            this.periods.add(period);
        }
    }
    return SUCCESS;
}
Also used : RelativePeriods(org.hisp.dhis.period.RelativePeriods) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Report(org.hisp.dhis.report.Report) Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) ReportTable(org.hisp.dhis.reporttable.ReportTable) Period(org.hisp.dhis.period.Period) CalendarPeriodType(org.hisp.dhis.period.CalendarPeriodType) PastAndCurrentPeriodFilter(org.hisp.dhis.system.filter.PastAndCurrentPeriodFilter) Date(java.util.Date)

Example 14 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class DefaultAnalyticsService method addMetaData.

/**
     * Adds meta data values to the given grid based on the given data query
     * parameters.
     *
     * @param params the {@link DataQueryParams}.
     * @param grid the grid.
     */
private void addMetaData(DataQueryParams params, Grid grid) {
    if (!params.isSkipMeta()) {
        Map<String, Object> metaData = new HashMap<>();
        Map<String, Object> internalMetaData = new HashMap<>();
        // -----------------------------------------------------------------
        // Items / names element
        // -----------------------------------------------------------------
        Map<String, String> cocNameMap = AnalyticsUtils.getCocNameMap(params);
        if (params.getApiVersion().ge(DhisApiVersion.V26)) {
            metaData.put(AnalyticsMetaDataKey.ITEMS.getKey(), AnalyticsUtils.getDimensionMetadataItemMap(params));
        } else {
            Map<String, String> uidNameMap = AnalyticsUtils.getDimensionItemNameMap(params);
            uidNameMap.putAll(cocNameMap);
            uidNameMap.put(DATA_X_DIM_ID, DISPLAY_NAME_DATA_X);
            metaData.put(AnalyticsMetaDataKey.NAMES.getKey(), uidNameMap);
        }
        // -----------------------------------------------------------------
        // Item order elements
        // -----------------------------------------------------------------
        Map<String, Object> dimensionItems = new HashMap<>();
        Calendar calendar = PeriodType.getCalendar();
        List<String> periodUids = calendar.isIso8601() ? getDimensionalItemIds(params.getDimensionOrFilterItems(PERIOD_DIM_ID)) : getLocalPeriodIdentifiers(params.getDimensionOrFilterItems(PERIOD_DIM_ID), calendar);
        dimensionItems.put(PERIOD_DIM_ID, periodUids);
        dimensionItems.put(CATEGORYOPTIONCOMBO_DIM_ID, cocNameMap.keySet());
        for (DimensionalObject dim : params.getDimensionsAndFilters()) {
            if (!dimensionItems.keySet().contains(dim.getDimension())) {
                dimensionItems.put(dim.getDimension(), getDimensionalItemIds(dim.getItems()));
            }
        }
        if (params.getApiVersion().ge(DhisApiVersion.V26)) {
            metaData.put(AnalyticsMetaDataKey.DIMENSIONS.getKey(), dimensionItems);
        } else {
            metaData.putAll(dimensionItems);
        }
        // -----------------------------------------------------------------
        // Organisation unit hierarchy
        // -----------------------------------------------------------------
        User user = securityManager.getCurrentUser(params);
        List<OrganisationUnit> organisationUnits = asTypedList(params.getDimensionOrFilterItems(ORGUNIT_DIM_ID));
        Collection<OrganisationUnit> roots = user != null ? user.getOrganisationUnits() : null;
        if (params.isHierarchyMeta()) {
            metaData.put(AnalyticsMetaDataKey.ORG_UNIT_HIERARCHY.getKey(), getParentGraphMap(organisationUnits, roots));
        }
        if (params.isShowHierarchy()) {
            Map<Object, List<?>> ancestorMap = organisationUnits.stream().collect(Collectors.toMap(OrganisationUnit::getUid, ou -> ou.getAncestorNames(roots, true)));
            internalMetaData.put(AnalyticsMetaDataKey.ORG_UNIT_ANCESTORS.getKey(), ancestorMap);
            metaData.put(AnalyticsMetaDataKey.ORG_UNIT_NAME_HIERARCHY.getKey(), getParentNameGraphMap(organisationUnits, roots, true));
        }
        grid.setMetaData(ImmutableMap.copyOf(metaData));
        grid.setInternalMetaData(ImmutableMap.copyOf(internalMetaData));
    }
}
Also used : ListGrid(org.hisp.dhis.system.grid.ListGrid) EventAnalyticsService(org.hisp.dhis.analytics.event.EventAnalyticsService) ValueType(org.hisp.dhis.common.ValueType) Autowired(org.springframework.beans.factory.annotation.Autowired) AnalyticsSecurityManager(org.hisp.dhis.analytics.AnalyticsSecurityManager) CombinationGenerator(org.hisp.dhis.common.CombinationGenerator) Future(java.util.concurrent.Future) Calendar(org.hisp.dhis.calendar.Calendar) Map(java.util.Map) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) ReportingRateMetric(org.hisp.dhis.common.ReportingRateMetric) Period(org.hisp.dhis.period.Period) ImmutableMap(com.google.common.collect.ImmutableMap) EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) ListUtils(org.hisp.dhis.commons.collection.ListUtils) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Collection(java.util.Collection) IRT2D(org.hisp.dhis.reporttable.ReportTable.IRT2D) Set(java.util.Set) AnalyticsTableType(org.hisp.dhis.analytics.table.AnalyticsTableType) RawAnalyticsManager(org.hisp.dhis.analytics.RawAnalyticsManager) DimensionType(org.hisp.dhis.common.DimensionType) Collectors(java.util.stream.Collectors) DataValueSet(org.hisp.dhis.dxf2.datavalueset.DataValueSet) OrganisationUnit.getParentGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap) List(java.util.List) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) ProcessingHint(org.hisp.dhis.analytics.ProcessingHint) LogFactory(org.apache.commons.logging.LogFactory) DimensionItem(org.hisp.dhis.analytics.DimensionItem) MathUtils(org.hisp.dhis.system.util.MathUtils) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ReportTable.addListIfEmpty(org.hisp.dhis.reporttable.ReportTable.addListIfEmpty) AnalyticsMetaDataKey(org.hisp.dhis.analytics.AnalyticsMetaDataKey) DataQueryService(org.hisp.dhis.analytics.DataQueryService) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ExpressionService(org.hisp.dhis.expression.ExpressionService) Lists(com.google.common.collect.Lists) ReportTable(org.hisp.dhis.reporttable.ReportTable) GridHeader(org.hisp.dhis.common.GridHeader) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) Indicator(org.hisp.dhis.indicator.Indicator) User(org.hisp.dhis.user.User) DimensionalObjectUtils.getDimensionalItemIds(org.hisp.dhis.common.DimensionalObjectUtils.getDimensionalItemIds) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) OrganisationUnit.getParentNameGraphMap(org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap) DataDimensionItemType(org.hisp.dhis.common.DataDimensionItemType) DimensionalObjectUtils(org.hisp.dhis.common.DimensionalObjectUtils) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Timer(org.hisp.dhis.util.Timer) QueryPlanner(org.hisp.dhis.analytics.QueryPlanner) AggregationType(org.hisp.dhis.analytics.AggregationType) ConstantService(org.hisp.dhis.constant.ConstantService) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) Grid(org.hisp.dhis.common.Grid) OutputFormat(org.hisp.dhis.analytics.OutputFormat) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) SystemUtils(org.hisp.dhis.commons.util.SystemUtils) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) AnalyticsUtils(org.hisp.dhis.analytics.AnalyticsUtils) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) IdentifiableObjectUtils.getLocalPeriodIdentifiers(org.hisp.dhis.common.IdentifiableObjectUtils.getLocalPeriodIdentifiers) SettingKey(org.hisp.dhis.setting.SettingKey) AnalyticsManager(org.hisp.dhis.analytics.AnalyticsManager) AnalyticsService(org.hisp.dhis.analytics.AnalyticsService) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) Calendar(org.hisp.dhis.calendar.Calendar) PeriodType.getPeriodTypeFromIsoString(org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString) DimensionalObject(org.hisp.dhis.common.DimensionalObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) DimensionalObject(org.hisp.dhis.common.DimensionalObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) List(java.util.List) DimensionalObjectUtils.asTypedList(org.hisp.dhis.common.DimensionalObjectUtils.asTypedList) ArrayList(java.util.ArrayList)

Example 15 with Calendar

use of org.hisp.dhis.calendar.Calendar in project dhis2-core by dhis2.

the class JdbcEventAnalyticsTableManager method getTables.

@Override
@Transactional
public List<AnalyticsTable> getTables(Date earliest) {
    log.info(String.format("Get tables using earliest: %s, spatial support: %b", earliest, databaseInfo.isSpatialSupport()));
    List<AnalyticsTable> tables = new UniqueArrayList<>();
    Calendar calendar = PeriodType.getCalendar();
    String baseName = getTableName();
    List<Program> programs = idObjectManager.getAllNoAcl(Program.class);
    for (Program program : programs) {
        List<Integer> dataYears = getDataYears(program, earliest);
        Collections.sort(dataYears);
        for (Integer year : dataYears) {
            Period period = PartitionUtils.getPeriod(calendar, year);
            AnalyticsTable table = new AnalyticsTable(baseName, null, period, program);
            List<AnalyticsTableColumn> dimensionColumns = getDimensionColumns(table);
            table.setDimensionColumns(dimensionColumns);
            tables.add(table);
        }
    }
    return tables;
}
Also used : Program(org.hisp.dhis.program.Program) AnalyticsTable(org.hisp.dhis.analytics.AnalyticsTable) Calendar(org.hisp.dhis.calendar.Calendar) Period(org.hisp.dhis.period.Period) UniqueArrayList(org.hisp.dhis.commons.collection.UniqueArrayList) AnalyticsTableColumn(org.hisp.dhis.analytics.AnalyticsTableColumn) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Calendar (org.hisp.dhis.calendar.Calendar)36 DateTimeUnit (org.hisp.dhis.calendar.DateTimeUnit)14 Date (java.util.Date)10 Period (org.hisp.dhis.period.Period)10 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 ArrayList (java.util.ArrayList)5 DimensionalObject (org.hisp.dhis.common.DimensionalObject)4 PeriodType (org.hisp.dhis.period.PeriodType)4 UniqueArrayList (org.hisp.dhis.commons.collection.UniqueArrayList)3 AnalyticsTable (org.hisp.dhis.analytics.AnalyticsTable)2 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)2 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)2 ReportTable (org.hisp.dhis.reporttable.ReportTable)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1