Search in sources :

Example 96 with PeriodType

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

the class HibernateDataSetStore method update.

@Override
public void update(DataSet dataSet) {
    PeriodType periodType = periodService.reloadPeriodType(dataSet.getPeriodType());
    dataSet.setPeriodType(periodType);
    super.update(dataSet);
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType)

Example 97 with PeriodType

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

the class HibernateDataSetStore method save.

// -------------------------------------------------------------------------
// DataSet
// -------------------------------------------------------------------------
@Override
public void save(DataSet dataSet) {
    PeriodType periodType = periodService.reloadPeriodType(dataSet.getPeriodType());
    dataSet.setPeriodType(periodType);
    super.save(dataSet);
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType)

Example 98 with PeriodType

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

the class HibernatePeriodStore method reloadPeriodType.

@Override
public PeriodType reloadPeriodType(PeriodType periodType) {
    Session session = sessionFactory.getCurrentSession();
    if (periodType == null || session.contains(periodType)) {
        return periodType;
    }
    PeriodType reloadedPeriodType = getPeriodType(periodType.getClass());
    if (reloadedPeriodType == null) {
        throw new InvalidIdentifierReferenceException("The PeriodType referenced by the Period is not in database: " + periodType.getName());
    }
    return reloadedPeriodType;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) StatelessSession(org.hibernate.StatelessSession) Session(org.hibernate.Session)

Example 99 with PeriodType

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

the class EventPredictionServiceTest method setUpTest.

@Override
public void setUpTest() {
    final String DATA_ELEMENT_A_UID = "DataElemenA";
    final String DATA_ELEMENT_D_UID = "DataElemenD";
    final String DATA_ELEMENT_I_UID = "DataElemenI";
    final String DATA_ELEMENT_E_UID = "DataElemenE";
    final String DATA_ELEMENT_T_UID = "DataElemenT";
    final String DATA_ELEMENT_X_UID = "DataElemenX";
    final String TRACKED_ENTITY_ATTRIBUTE_UID = "TEAttribute";
    final String PROGRAM_UID = "ProgramUidA";
    final String PROGRAM_INDICATOR_A_UID = "ProgramIndA";
    final String PROGRAM_INDICATOR_B_UID = "ProgramIndB";
    final String PROGRAM_TRACKED_ENTITY_ATTRIBUTE_DIMENSION_ITEM = PROGRAM_UID + SEPARATOR + TRACKED_ENTITY_ATTRIBUTE_UID;
    final String PROGRAM_DATA_ELEMENT_DIMENSION_ITEM = PROGRAM_UID + SEPARATOR + DATA_ELEMENT_X_UID;
    // A
    final String EXPRESSION_A = "sum( A{" + PROGRAM_TRACKED_ENTITY_ATTRIBUTE_DIMENSION_ITEM + "} )";
    // -
    // ProgramTrackedEntityAttribute
    // D
    final String EXPRESSION_D = "sum( D{" + PROGRAM_DATA_ELEMENT_DIMENSION_ITEM + "} )";
    // -
    // ProgramDataElement
    // I
    final String EXPRESSION_I = "sum( I{" + PROGRAM_INDICATOR_A_UID + "} + I{" + PROGRAM_INDICATOR_B_UID + "} )";
    // -
    // ProgramIndicators
    // E
    final String EXPRESSION_E = "sum( #{" + DATA_ELEMENT_E_UID + "} )";
    // -
    // Data
    // element
    // T -
    final String EXPRESSION_T = EXPRESSION_A + " + " + EXPRESSION_E;
    // Two
    // things,
    // event
    // and
    // data
    // elemeent
    // Program
    final String EX_INDICATOR_A = "#{" + PROGRAM_DATA_ELEMENT_DIMENSION_ITEM + "} + 4";
    // Indicator
    // A
    // expression
    // Program
    final String EX_INDICATOR_B = "V{enrollment_count}";
    // Indicator B
    // expression
    defaultCombo = categoryService.getDefaultCategoryOptionCombo();
    orgUnitA = createOrganisationUnit('A');
    organisationUnitService.addOrganisationUnit(orgUnitA);
    Set<OrganisationUnit> orgUnitASet = Sets.newHashSet(orgUnitA);
    PeriodType periodTypeMonthly = new MonthlyPeriodType();
    testYear = Calendar.getInstance().get(Calendar.YEAR) - 1;
    periodMar = createPeriod(periodTypeMonthly, getDate(testYear, 3, 1), getDate(testYear, 3, 31));
    periodApr = createPeriod(periodTypeMonthly, getDate(testYear, 4, 1), getDate(testYear, 4, 30));
    periodMay = createPeriod(periodTypeMonthly, getDate(testYear, 5, 1), getDate(testYear, 5, 31));
    periodService.addPeriod(periodMar);
    periodService.addPeriod(periodApr);
    periodService.addPeriod(periodMay);
    Date dateMar20 = getDate(testYear, 3, 20);
    Date dateApr10 = getDate(testYear, 4, 10);
    predictorOutputA = createDataElement('A');
    predictorOutputD = createDataElement('D');
    predictorOutputI = createDataElement('I');
    predictorOutputT = createDataElement('T');
    DataElement dataElementE = createDataElement('E');
    DataElement dataElementX = createDataElement('P', ValueType.INTEGER, AggregationType.SUM, DataElementDomain.TRACKER);
    predictorOutputA.setUid(DATA_ELEMENT_A_UID);
    predictorOutputD.setUid(DATA_ELEMENT_D_UID);
    predictorOutputI.setUid(DATA_ELEMENT_I_UID);
    predictorOutputT.setUid(DATA_ELEMENT_T_UID);
    dataElementE.setUid(DATA_ELEMENT_E_UID);
    dataElementX.setUid(DATA_ELEMENT_X_UID);
    dataElementService.addDataElement(predictorOutputA);
    dataElementService.addDataElement(predictorOutputD);
    dataElementService.addDataElement(predictorOutputI);
    dataElementService.addDataElement(predictorOutputT);
    dataElementService.addDataElement(dataElementE);
    dataElementService.addDataElement(dataElementX);
    TrackedEntityAttribute entityAttribute = createTrackedEntityAttribute('A');
    entityAttribute.setAggregationType(AggregationType.COUNT);
    entityAttribute.setUid(TRACKED_ENTITY_ATTRIBUTE_UID);
    entityAttributeService.addTrackedEntityAttribute(entityAttribute);
    TrackedEntityInstance entityInstance = createTrackedEntityInstance('A', orgUnitA, entityAttribute);
    entityInstanceService.addTrackedEntityInstance(entityInstance);
    TrackedEntityAttributeValue trackedEntityAttributeValue = new TrackedEntityAttributeValue(entityAttribute, entityInstance);
    trackedEntityAttributeValue.setValue("123");
    entityAttributeValueService.addTrackedEntityAttributeValue(trackedEntityAttributeValue);
    entityInstance.setTrackedEntityAttributeValues(Sets.newHashSet(trackedEntityAttributeValue));
    entityInstanceService.updateTrackedEntityInstance(entityInstance);
    Program program = createProgram('A', null, Sets.newHashSet(entityAttribute), orgUnitASet, null);
    program.setUid(PROGRAM_UID);
    programService.addProgram(program);
    ProgramStage stageA = createProgramStage('A', 0);
    stageA.setProgram(program);
    stageA.addDataElement(dataElementX, 1);
    programStageService.saveProgramStage(stageA);
    ProgramIndicator programIndicatorA = createProgramIndicator('A', program, EX_INDICATOR_A, null);
    programIndicatorA.setAggregationType(AggregationType.SUM);
    programIndicatorA.setUid(PROGRAM_INDICATOR_A_UID);
    programIndicatorService.addProgramIndicator(programIndicatorA);
    ProgramIndicator programIndicatorB = createProgramIndicator('B', program, EX_INDICATOR_B, null);
    programIndicatorB.setAnalyticsType(AnalyticsType.ENROLLMENT);
    programIndicatorB.setAggregationType(AggregationType.COUNT);
    programIndicatorB.setUid(PROGRAM_INDICATOR_B_UID);
    programIndicatorService.addProgramIndicator(programIndicatorB);
    program.setProgramStages(Sets.newHashSet(stageA));
    program.getProgramIndicators().add(programIndicatorA);
    program.getProgramIndicators().add(programIndicatorB);
    programService.updateProgram(program);
    ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, dateMar20, dateMar20, orgUnitA);
    programInstanceService.addProgramInstance(programInstance);
    ProgramStageInstance stageInstanceA = programStageInstanceService.createProgramStageInstance(programInstance, stageA, dateMar20, dateMar20, orgUnitA);
    ProgramStageInstance stageInstanceB = programStageInstanceService.createProgramStageInstance(programInstance, stageA, dateApr10, dateApr10, orgUnitA);
    stageInstanceA.setExecutionDate(dateMar20);
    stageInstanceB.setExecutionDate(dateApr10);
    stageInstanceA.setAttributeOptionCombo(defaultCombo);
    stageInstanceB.setAttributeOptionCombo(defaultCombo);
    programStageInstanceService.addProgramStageInstance(stageInstanceA);
    programStageInstanceService.addProgramStageInstance(stageInstanceB);
    categoryManager.addAndPruneAllOptionCombos();
    Expression expressionA = new Expression(EXPRESSION_A, "ProgramTrackedEntityAttribute");
    Expression expressionD = new Expression(EXPRESSION_D, "ProgramDataElement");
    Expression expressionI = new Expression(EXPRESSION_I, "ProgramIndicators");
    Expression expressionT = new Expression(EXPRESSION_T, "Two things");
    OrganisationUnitLevel orgUnitLevel1 = new OrganisationUnitLevel(1, "Level1");
    organisationUnitService.addOrganisationUnitLevel(orgUnitLevel1);
    predictorA = createPredictor(predictorOutputA, defaultCombo, "A", expressionA, null, periodTypeMonthly, orgUnitLevel1, 2, 0, 0);
    predictorD = createPredictor(predictorOutputD, defaultCombo, "D", expressionD, null, periodTypeMonthly, orgUnitLevel1, 2, 0, 0);
    predictorI = createPredictor(predictorOutputI, defaultCombo, "I", expressionI, null, periodTypeMonthly, orgUnitLevel1, 2, 0, 0);
    predictorT = createPredictor(predictorOutputT, defaultCombo, "T", expressionT, null, periodTypeMonthly, orgUnitLevel1, 2, 0, 0);
    predictorService.addPredictor(predictorA);
    predictorService.addPredictor(predictorD);
    predictorService.addPredictor(predictorI);
    Map<String, Grid> itemGridMap = new HashMap<>();
    itemGridMap.put(PROGRAM_TRACKED_ENTITY_ATTRIBUTE_DIMENSION_ITEM, newGrid(PROGRAM_TRACKED_ENTITY_ATTRIBUTE_DIMENSION_ITEM, 1.0, 1.0));
    itemGridMap.put(PROGRAM_DATA_ELEMENT_DIMENSION_ITEM, newGrid(PROGRAM_DATA_ELEMENT_DIMENSION_ITEM, 4.0, 5.0));
    itemGridMap.put(PROGRAM_INDICATOR_A_UID, newGrid(PROGRAM_INDICATOR_A_UID, 8.0, 9.0));
    itemGridMap.put(PROGRAM_INDICATOR_B_UID, newGrid(PROGRAM_INDICATOR_B_UID, 10.0, 11.0));
    MockAnalyticsService mockAnalyticsSerivce = new MockAnalyticsService();
    mockAnalyticsSerivce.setItemGridMap(itemGridMap);
    setDependency(AnalyticsServiceTarget.class, AnalyticsServiceTarget::setAnalyticsService, mockAnalyticsSerivce, predictionService);
    CurrentUserService mockCurrentUserService = new MockCurrentUserService(true, orgUnitASet, orgUnitASet);
    setDependency(CurrentUserServiceTarget.class, CurrentUserServiceTarget::setCurrentUserService, mockCurrentUserService, predictionService);
    dataValueService.addDataValue(createDataValue(dataElementE, periodMar, orgUnitA, defaultCombo, defaultCombo, "100"));
    dataValueService.addDataValue(createDataValue(dataElementE, periodApr, orgUnitA, defaultCombo, defaultCombo, "200"));
    dataValueService.addDataValue(createDataValue(dataElementE, periodMay, orgUnitA, defaultCombo, defaultCombo, "300"));
}
Also used : MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) PeriodType(org.hisp.dhis.period.PeriodType) HashMap(java.util.HashMap) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) CurrentUserServiceTarget(org.hisp.dhis.user.CurrentUserServiceTarget) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) AnalyticsServiceTarget(org.hisp.dhis.analytics.AnalyticsServiceTarget) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) MockAnalyticsService(org.hisp.dhis.mock.MockAnalyticsService) MonthlyPeriodType(org.hisp.dhis.period.MonthlyPeriodType) Expression(org.hisp.dhis.expression.Expression) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramIndicator(org.hisp.dhis.program.ProgramIndicator)

Example 100 with PeriodType

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

the class EventDateValidationHook method validatePeriodType.

private void validatePeriodType(ValidationErrorReporter reporter, Event event, Program program) {
    checkNotNull(event, TrackerImporterAssertErrors.EVENT_CANT_BE_NULL);
    checkNotNull(program, TrackerImporterAssertErrors.PROGRAM_CANT_BE_NULL);
    PeriodType periodType = program.getExpiryPeriodType();
    if (periodType == null || program.getExpiryDays() == 0) {
        // Nothing more to check here, return out
        return;
    }
    Instant referenceDate = Optional.of(event).map(Event::getOccurredAt).orElseGet(event::getScheduledAt);
    if (referenceDate == null) {
        reporter.addError(event, E1046, event);
        return;
    }
    Period period = periodType.createPeriod(new Date());
    if (referenceDate.isBefore(period.getStartDate().toInstant())) {
        reporter.addError(event, E1047, event);
    }
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Instant(java.time.Instant) Period(org.hisp.dhis.period.Period) Date(java.util.Date)

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