Search in sources :

Example 26 with ImmutableEvent

use of org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent in project dhis2-core by dhis2.

the class ExpirationDaysCheckTest method failWhenProgramStageInstanceHasNoCompletedDateAndProgramHasExpiryDays.

@Test
void failWhenProgramStageInstanceHasNoCompletedDateAndProgramHasExpiryDays() {
    // Given
    // Prepare program
    Program program = createProgram('P');
    program.setCompleteEventsExpiryDays(3);
    Map<String, Program> programMap = new HashMap<>();
    programMap.put(program.getUid(), program);
    when(workContext.getProgramsMap()).thenReturn(programMap);
    // Prepare program stage instance
    Map<String, ProgramStageInstance> psiMap = new HashMap<>();
    ProgramStageInstance psi = new ProgramStageInstance();
    psi.setStatus(EventStatus.COMPLETED);
    psi.setUid(event.getUid());
    psiMap.put(event.getUid(), psi);
    when(workContext.getProgramStageInstanceMap()).thenReturn(psiMap);
    // Prepare event
    event.setProgram(program.getUid());
    event.setStatus(EventStatus.COMPLETED);
    // When
    ImportSummary importSummary = rule.check(new ImmutableEvent(event), workContext);
    // Then
    assertHasError(importSummary, event, "Event needs to have completed date");
}
Also used : Program(org.hisp.dhis.program.Program) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) HashMap(java.util.HashMap) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Test(org.junit.jupiter.api.Test) BaseValidationTest(org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)

Example 27 with ImmutableEvent

use of org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent in project dhis2-core by dhis2.

the class ExpirationDaysCheckTest method failWhenProgramStageInstanceHasExecutionDateBeforeAllowedProgramExpiryDaysBasedOnPeriod.

@Test
void failWhenProgramStageInstanceHasExecutionDateBeforeAllowedProgramExpiryDaysBasedOnPeriod() {
    // Given
    final String monthlyPeriodType = new SimpleDateFormat("yyyyMM").format(new Date());
    // Prepare program
    Program program = createProgram('P');
    program.setExpiryPeriodType(PeriodType.getPeriodTypeFromIsoString(monthlyPeriodType));
    program.setExpiryDays(3);
    Map<String, Program> programMap = new HashMap<>();
    programMap.put(program.getUid(), program);
    when(workContext.getProgramsMap()).thenReturn(programMap);
    // Prepare program stage instance
    Map<String, ProgramStageInstance> psiMap = new HashMap<>();
    ProgramStageInstance psi = new ProgramStageInstance();
    // month length + 5
    psi.setExecutionDate(getTodayMinusDays(35));
    // days
    psi.setUid(event.getUid());
    psiMap.put(event.getUid(), psi);
    when(workContext.getProgramStageInstanceMap()).thenReturn(psiMap);
    // Prepare event
    event.setProgram(program.getUid());
    // When
    ImportSummary importSummary = rule.check(new ImmutableEvent(event), workContext);
    // Then
    assertHasError(importSummary, event, "The program's expiry date has passed. It is not possible to make changes to this event");
}
Also used : Program(org.hisp.dhis.program.Program) DhisConvenienceTest.createProgram(org.hisp.dhis.DhisConvenienceTest.createProgram) HashMap(java.util.HashMap) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Test(org.junit.jupiter.api.Test) BaseValidationTest(org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)

Example 28 with ImmutableEvent

use of org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent in project dhis2-core by dhis2.

the class DataValueCheck method validateMandatoryAttributes.

public void validateMandatoryAttributes(ImportConflicts importConflicts, WorkContext ctx, ImmutableEvent event) {
    if (StringUtils.isEmpty(event.getProgramStage()))
        return;
    final IdScheme programStageIdScheme = ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme();
    final IdScheme dataElementIdScheme = ctx.getImportOptions().getIdSchemes().getDataElementIdScheme();
    final Map<String, Set<EventDataValue>> eventDataValueMap = ctx.getEventDataValueMap();
    final boolean allowSingleUpdates = ctx.getImportOptions().isMergeDataValues();
    ProgramStage programStage = ctx.getProgramStage(programStageIdScheme, event.getProgramStage());
    final Set<ProgramStageDataElement> mandatoryDataElements = getMandatoryProgramStageDataElements(programStage);
    // Data Element IDs associated to the current event
    Set<String> dataValues = eventDataValueMap.get(event.getUid()).stream().map(EventDataValue::getDataElement).collect(Collectors.toSet());
    if (allowSingleUpdates) {
        final ProgramStageInstance programStageInstance = ctx.getProgramStageInstanceMap().get(event.getUid());
        dataValues.addAll(programStageInstance.getEventDataValues().stream().filter(dv -> !StringUtils.isEmpty(dv.getValue().trim())).map(EventDataValue::getDataElement).collect(Collectors.toSet()));
    }
    for (ProgramStageDataElement mandatoryDataElement : mandatoryDataElements) {
        String resolvedDataElementId = getIdentifierBasedOnIdScheme(mandatoryDataElement.getDataElement(), dataElementIdScheme);
        if (!dataValues.contains(resolvedDataElementId)) {
            importConflicts.addConflict(resolvedDataElementId, "value_required_but_not_provided");
        }
    }
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataValue(org.hisp.dhis.dxf2.events.event.DataValue) Authorities(org.hisp.dhis.security.Authorities) ValueType(org.hisp.dhis.common.ValueType) ValidationUtils(org.hisp.dhis.system.util.ValidationUtils) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) IdentifiableObjectUtils.getIdentifierBasedOnIdScheme(org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifierBasedOnIdScheme) StringUtils(org.apache.commons.lang3.StringUtils) DataElement(org.hisp.dhis.dataelement.DataElement) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) SQLException(java.sql.SQLException) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) Map(java.util.Map) Checker(org.hisp.dhis.dxf2.events.importer.Checker) ImageIO(javax.imageio.ImageIO) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) User(org.hisp.dhis.user.User) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) EventUtils.eventDataValuesToJson(org.hisp.dhis.dxf2.events.event.EventUtils.eventDataValuesToJson) ImmutableSet(com.google.common.collect.ImmutableSet) FileResource(org.hisp.dhis.fileresource.FileResource) Set(java.util.Set) EventStatus(org.hisp.dhis.event.EventStatus) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) ProgramStage(org.hisp.dhis.program.ProgramStage) Component(org.springframework.stereotype.Component) OptionSet(org.hisp.dhis.option.OptionSet) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Optional(java.util.Optional) ValidationStrategy(org.hisp.dhis.program.ValidationStrategy) Collections(java.util.Collections) IdScheme(org.hisp.dhis.common.IdScheme) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) OptionSet(org.hisp.dhis.option.OptionSet) IdentifiableObjectUtils.getIdentifierBasedOnIdScheme(org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifierBasedOnIdScheme) IdScheme(org.hisp.dhis.common.IdScheme) ProgramStage(org.hisp.dhis.program.ProgramStage) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 29 with ImmutableEvent

use of org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent in project dhis2-core by dhis2.

the class EventGeometryCheck method check.

@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
    IdScheme scheme = ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme();
    ProgramStage programStage = ctx.getProgramStage(scheme, event.getProgramStage());
    if (Objects.nonNull(event.getGeometry()) && programStageFeatureCompatibleWithEventGeometry(event, programStage)) {
        return new ImportSummary(ImportStatus.ERROR, "Geometry (" + event.getGeometry().getGeometryType() + ") does not conform to the feature type (" + programStage.getFeatureType().value() + ") specified for the program stage: " + programStage.getUid()).setReference(event.getEvent()).incrementIgnored();
    }
    return success();
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) IdScheme(org.hisp.dhis.common.IdScheme) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 30 with ImmutableEvent

use of org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent in project dhis2-core by dhis2.

the class ExpirationDaysCheck method check.

@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
    final ImportOptions importOptions = ctx.getImportOptions();
    final Program program = ctx.getProgramsMap().get(event.getProgram());
    final ProgramStageInstance programStageInstance = ctx.getProgramStageInstanceMap().get(event.getEvent());
    if (importOptions == null || importOptions.getUser() == null || importOptions.getUser().isAuthorized(Authorities.F_EDIT_EXPIRED.getAuthority())) {
        return success();
    }
    if (program != null) {
        ImportSummary importSummary = checkEventOrPsiCompletedDate(program, event, programStageInstance);
        if (importSummary.isStatus(ImportStatus.ERROR)) {
            return importSummary;
        }
        return checkEventOrPsiExpirationDate(program, event, programStageInstance);
    }
    return success();
}
Also used : Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)49 ImmutableEvent (org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent)38 Test (org.junit.jupiter.api.Test)35 BaseValidationTest (org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)34 Program (org.hisp.dhis.program.Program)18 HashMap (java.util.HashMap)17 DhisConvenienceTest.createProgram (org.hisp.dhis.DhisConvenienceTest.createProgram)13 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)11 ProgramInstance (org.hisp.dhis.program.ProgramInstance)10 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)9 ProgramStage (org.hisp.dhis.program.ProgramStage)8 DataElement (org.hisp.dhis.dataelement.DataElement)7 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)7 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)7 DhisConvenienceTest.createDataElement (org.hisp.dhis.DhisConvenienceTest.createDataElement)5 DhisConvenienceTest.createProgramStageDataElement (org.hisp.dhis.DhisConvenienceTest.createProgramStageDataElement)5 DhisConvenienceTest.createTrackedEntityInstance (org.hisp.dhis.DhisConvenienceTest.createTrackedEntityInstance)5 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)5 EventTestUtils.createDataValue (org.hisp.dhis.dxf2.events.importer.EventTestUtils.createDataValue)5 EventTestUtils.createEventDataValue (org.hisp.dhis.dxf2.events.importer.EventTestUtils.createEventDataValue)5