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);
}
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);
}
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;
}
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"));
}
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);
}
}
Aggregations