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