Search in sources :

Example 86 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class SectionUtilsTest method testDataSetSection.

@Test
void testDataSetSection() {
    CategoryOption categoryOptionA = new CategoryOption("OptionA");
    CategoryOption categoryOptionB = new CategoryOption("OptionB");
    CategoryOption categoryOptionC = new CategoryOption("OptionC");
    CategoryOption categoryOptionD = new CategoryOption("OptionD");
    CategoryOption categoryOptionE = new CategoryOption("OptionE");
    CategoryOption categoryOptionF = new CategoryOption("OptionF");
    CategoryOption categoryOptionG = new CategoryOption("OptionG");
    categoryService.addCategoryOption(categoryOptionA);
    categoryService.addCategoryOption(categoryOptionB);
    categoryService.addCategoryOption(categoryOptionC);
    categoryService.addCategoryOption(categoryOptionD);
    categoryService.addCategoryOption(categoryOptionE);
    categoryService.addCategoryOption(categoryOptionF);
    categoryService.addCategoryOption(categoryOptionG);
    Category categoryA = createCategory('A', categoryOptionA, categoryOptionB);
    Category categoryB = createCategory('B', categoryOptionC, categoryOptionD);
    Category categoryC = createCategory('C', categoryOptionE, categoryOptionF);
    categoryService.addCategory(categoryA);
    categoryService.addCategory(categoryB);
    categoryService.addCategory(categoryC);
    List<Category> categoriesAB = new ArrayList<>();
    List<Category> categoriesABC = new ArrayList<>();
    categoriesAB.add(categoryA);
    categoriesAB.add(categoryB);
    categoriesABC.add(categoryA);
    categoriesABC.add(categoryB);
    categoriesABC.add(categoryC);
    CategoryCombo categoryComboAB = new CategoryCombo("CategoryComboA", DataDimensionType.DISAGGREGATION, categoriesAB);
    CategoryCombo categoryComboABC = new CategoryCombo("CategoryComboB", DataDimensionType.DISAGGREGATION, categoriesABC);
    long catAB = categoryService.addCategoryCombo(categoryComboAB);
    long catABC = categoryService.addCategoryCombo(categoryComboABC);
    long catDefault = categoryService.getDefaultCategoryCombo().getId();
    DataElement dataElementA = createDataElement('A');
    DataElement dataElementB = createDataElement('B');
    DataElement dataElementC = createDataElement('C', categoryComboAB);
    DataElement dataElementD = createDataElement('D', categoryComboAB);
    DataElement dataElementE = createDataElement('E');
    DataElement dataElementF = createDataElement('F', categoryComboABC);
    DataElement dataElementG = createDataElement('G', categoryComboABC);
    DataElement dataElementH = createDataElement('H', categoryComboAB);
    DataElement dataElementI = createDataElement('I', categoryComboABC);
    dataElementService.addDataElement(dataElementA);
    dataElementService.addDataElement(dataElementB);
    dataElementService.addDataElement(dataElementC);
    dataElementService.addDataElement(dataElementD);
    dataElementService.addDataElement(dataElementE);
    dataElementService.addDataElement(dataElementF);
    dataElementService.addDataElement(dataElementG);
    dataElementService.addDataElement(dataElementH);
    dataElementService.addDataElement(dataElementI);
    PeriodType periodType = new MonthlyPeriodType();
    DataSet dataSetA = createDataSet('A', periodType);
    dataSetA.addDataSetElement(dataElementA);
    dataSetA.addDataSetElement(dataElementB);
    dataSetA.addDataSetElement(dataElementC);
    dataSetA.addDataSetElement(dataElementD);
    dataSetA.addDataSetElement(dataElementE);
    dataSetA.addDataSetElement(dataElementF);
    dataSetA.addDataSetElement(dataElementG);
    dataSetA.addDataSetElement(dataElementH);
    dataSetA.addDataSetElement(dataElementI);
    Section section = new Section();
    List<DataElement> dataElements = new ArrayList<>();
    dataElements.add(dataElementA);
    dataElements.add(dataElementB);
    dataElements.add(dataElementC);
    dataElements.add(dataElementD);
    dataElements.add(dataElementE);
    dataElements.add(dataElementF);
    dataElements.add(dataElementG);
    dataElements.add(dataElementH);
    dataElements.add(dataElementI);
    section.setDataElements(dataElements);
    dataSetA.getSections().add(section);
    long idA = dataSetService.addDataSet(dataSetA);
    DataSet ds = dataSetService.getDataSet(idA);
    assertEquals(idA, ds.getId());
    assertEquals(9, ds.getDataSetElements().size());
    assertEquals(1, ds.getSections().size());
    Section sec = ds.getSections().iterator().next();
    assertEquals(9, sec.getDataElements().size());
    Map<String, Collection<DataElement>> orderedDataElements = sectionUtils.getOrderedDataElementsMap(sec);
    List<String> keys = new ArrayList<>(orderedDataElements.keySet());
    assertEquals(6, orderedDataElements.keySet().size());
    String key1 = sec.getId() + "-" + catDefault + "-" + "0";
    String key2 = sec.getId() + "-" + catAB + "-" + "1";
    String key3 = sec.getId() + "-" + catDefault + "-" + "2";
    String key4 = sec.getId() + "-" + catABC + "-" + "3";
    String key5 = sec.getId() + "-" + catAB + "-" + "4";
    String key6 = sec.getId() + "-" + catABC + "-" + "5";
    assertEquals(key1, keys.get(0));
    assertEquals(key2, keys.get(1));
    assertEquals(key3, keys.get(2));
    assertEquals(key4, keys.get(3));
    assertEquals(key5, keys.get(4));
    assertEquals(key6, keys.get(5));
    assertEquals(2, orderedDataElements.get(key1).size());
    assertEquals(2, orderedDataElements.get(key2).size());
    assertEquals(1, orderedDataElements.get(key3).size());
    assertEquals(2, orderedDataElements.get(key4).size());
    assertEquals(1, orderedDataElements.get(key5).size());
    assertEquals(1, orderedDataElements.get(key6).size());
}
Also used : MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) Category(org.hisp.dhis.category.Category) DataSet(org.hisp.dhis.dataset.DataSet) ArrayList(java.util.ArrayList) Section(org.hisp.dhis.dataset.Section) DataElement(org.hisp.dhis.dataelement.DataElement) CategoryCombo(org.hisp.dhis.category.CategoryCombo) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) CategoryOption(org.hisp.dhis.category.CategoryOption) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 87 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class DefaultPredictionService method getSamplePeriodsMap.

/**
 * Creates a map relating each output period to a list of sample periods
 * from which the sample data is to be drawn. Sample periods returned for
 * each output period are in order from older to newer, so any prediction
 * results can be brought forward if they are to be used in later period
 * predictions.
 */
private ListMap<Period, Period> getSamplePeriodsMap(List<Period> outputPeriods, Predictor predictor) {
    int sequentialCount = predictor.getSequentialSampleCount();
    int annualCount = predictor.getAnnualSampleCount();
    int skipCount = firstNonNull(predictor.getSequentialSkipCount(), 0);
    PeriodType periodType = predictor.getPeriodType();
    ListMap<Period, Period> samplePeriodsMap = new ListMap<>();
    for (Period outputPeriod : outputPeriods) {
        samplePeriodsMap.put(outputPeriod, new ArrayList<>());
        Period p = periodType.getPreviousPeriod(outputPeriod, skipCount);
        for (int i = skipCount; i < sequentialCount; i++) {
            p = periodType.getPreviousPeriod(p);
            samplePeriodsMap.putValue(outputPeriod, p);
        }
        for (int year = 1; year <= annualCount; year++) {
            Period pPrev = periodType.getPreviousYearsPeriod(outputPeriod, year);
            Period pNext = pPrev;
            samplePeriodsMap.putValue(outputPeriod, pPrev);
            for (int i = 0; i < sequentialCount; i++) {
                pPrev = periodType.getPreviousPeriod(pPrev);
                pNext = periodType.getNextPeriod(pNext);
                samplePeriodsMap.putValue(outputPeriod, pPrev);
                samplePeriodsMap.putValue(outputPeriod, pNext);
            }
        }
    }
    return samplePeriodsMap;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Period(org.hisp.dhis.period.Period) ListMap(org.hisp.dhis.common.ListMap)

Example 88 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class AscendingPeriodComparator method compare.

@Override
public int compare(Period period1, Period period2) {
    PeriodType a = period1.getPeriodType();
    PeriodType b = period2.getPeriodType();
    int freqCompare = Integer.compare(a.getFrequencyOrder(), b.getFrequencyOrder());
    int nameCompare = a.getName().compareTo(b.getName());
    return freqCompare == 0 ? (nameCompare == 0 ? period1.getStartDate().compareTo(period2.getStartDate()) : nameCompare) : freqCompare;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType)

Example 89 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class DefaultQueryPlanner method groupByDataPeriodType.

/**
 * Groups the given query in sub queries based on the period type of its
 * data elements. Sets the data period type on each query. This only applies
 * if the aggregation type of the query involves disaggregation.
 *
 * @param params the {@link DataQueryParams}.
 * @return a list of {@link DataQueryParams}.
 */
private List<DataQueryParams> groupByDataPeriodType(DataQueryParams params) {
    List<DataQueryParams> queries = new ArrayList<>();
    if (params.isDisaggregation() && !params.getDataElements().isEmpty()) {
        ListMap<PeriodType, DimensionalItemObject> periodTypeDataElementMap = QueryPlannerUtils.getPeriodTypeDataElementMap(params.getDataElements());
        for (PeriodType periodType : periodTypeDataElementMap.keySet()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withDataElements(periodTypeDataElementMap.get(periodType)).withDataPeriodType(periodType).build();
            queries.add(query);
        }
    } else if (params.isDisaggregation() && !params.getDataElementGroupSets().isEmpty()) {
        DataElementGroup deg = params.getFirstDataElementGroup();
        PeriodType periodType = deg != null ? deg.getPeriodType() : null;
        queries.add(DataQueryParams.newBuilder(params).withDataPeriodType(periodType).build());
    } else {
        queries.add(DataQueryParams.newBuilder(params).build());
    }
    logQuerySplit(queries, "data period type");
    return queries;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) PeriodType(org.hisp.dhis.period.PeriodType) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup)

Example 90 with PeriodType

use of org.hisp.dhis.period.PeriodType in project dhis2-core by dhis2.

the class DataElement method getLatestOpenFuturePeriod.

/**
 * Returns the latest period which is open for data input. Returns null if
 * data element is not associated with any data sets.
 *
 * @return the latest period which is open for data input.
 */
public Period getLatestOpenFuturePeriod() {
    int periods = getOpenFuturePeriods();
    PeriodType periodType = getPeriodType();
    if (periodType != null) {
        Period period = periodType.createPeriod();
        // Rewind one as 0 open periods implies current period is locked
        period = periodType.getPreviousPeriod(period);
        return periodType.getNextPeriod(period, periods);
    }
    return null;
}
Also used : YearlyPeriodType(org.hisp.dhis.period.YearlyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) Period(org.hisp.dhis.period.Period)

Aggregations

PeriodType (org.hisp.dhis.period.PeriodType)104 Period (org.hisp.dhis.period.Period)27 MonthlyPeriodType (org.hisp.dhis.period.MonthlyPeriodType)24 DataElement (org.hisp.dhis.dataelement.DataElement)17 DataSet (org.hisp.dhis.dataset.DataSet)17 ArrayList (java.util.ArrayList)15 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)13 Test (org.junit.jupiter.api.Test)12 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)11 Date (java.util.Date)10 DailyPeriodType (org.hisp.dhis.period.DailyPeriodType)8 ProgramStage (org.hisp.dhis.program.ProgramStage)8 HashMap (java.util.HashMap)7 UniqueArrayList (org.hisp.dhis.commons.collection.UniqueArrayList)7 Expression (org.hisp.dhis.expression.Expression)7 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)7 OrganisationUnitGroupSet (org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)6 Program (org.hisp.dhis.program.Program)6 Property (org.hisp.dhis.schema.Property)6 Schema (org.hisp.dhis.schema.Schema)6