use of org.hisp.dhis.dxf2.importsummary.ImportSummary in project dhis2-core by dhis2.
the class AbstractEventService method updateEvent.
private ImportSummary updateEvent(Event event, User user, boolean singleValue, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(event.getEvent());
ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(event.getEvent());
if (programStageInstance == null) {
importSummary.getConflicts().add(new ImportConflict("Invalid Event ID.", event.getEvent()));
return importSummary.incrementIgnored();
}
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
if (organisationUnit == null) {
organisationUnit = programStageInstance.getOrganisationUnit();
}
Date executionDate = new Date();
if (event.getEventDate() != null) {
executionDate = DateUtils.parseDate(event.getEventDate());
programStageInstance.setExecutionDate(executionDate);
}
Date dueDate = new Date();
if (event.getDueDate() != null) {
dueDate = DateUtils.parseDate(event.getDueDate());
}
String storedBy = getStoredBy(event, null, user);
programStageInstance.setStoredBy(storedBy);
String completedBy = getCompletedBy(event, null, user);
if (event.getStatus() == EventStatus.ACTIVE) {
programStageInstance.setStatus(EventStatus.ACTIVE);
programStageInstance.setCompletedBy(null);
programStageInstance.setCompletedDate(null);
} else if (programStageInstance.getStatus() != event.getStatus() && event.getStatus() == EventStatus.COMPLETED) {
programStageInstance.setStatus(EventStatus.COMPLETED);
programStageInstance.setCompletedBy(completedBy);
programStageInstance.setCompletedDate(executionDate);
if (programStageInstance.isCompleted()) {
programStageInstanceService.completeProgramStageInstance(programStageInstance, importOptions.isSkipNotifications(), i18nManager.getI18nFormat());
}
} else if (event.getStatus() == EventStatus.SKIPPED) {
programStageInstance.setStatus(EventStatus.SKIPPED);
} else if (event.getStatus() == EventStatus.SCHEDULE) {
programStageInstance.setStatus(EventStatus.SCHEDULE);
}
programStageInstance.setDueDate(dueDate);
programStageInstance.setOrganisationUnit(organisationUnit);
if (!singleValue) {
if (programStageInstance.getProgramStage().getCaptureCoordinates()) {
if (event.getCoordinate() != null && event.getCoordinate().isValid()) {
programStageInstance.setLatitude(event.getCoordinate().getLatitude());
programStageInstance.setLongitude(event.getCoordinate().getLongitude());
} else {
programStageInstance.setLatitude(null);
programStageInstance.setLongitude(null);
}
}
}
Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
validateExpiryDays(event, program, programStageInstance);
if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
if (attributeOptionCombo == null) {
importSummary.getConflicts().add(new ImportConflict("Invalid attribute option combo identifier:", event.getAttributeCategoryOptions()));
return importSummary.incrementIgnored();
}
programStageInstance.setAttributeOptionCombo(attributeOptionCombo);
}
programStageInstance.setDeleted(event.isDeleted());
programStageInstanceService.updateProgramStageInstance(programStageInstance);
updateTrackedEntityInstance(programStageInstance);
saveTrackedEntityComment(programStageInstance, event, storedBy);
Set<TrackedEntityDataValue> dataValues = new HashSet<>(dataValueService.getTrackedEntityDataValues(programStageInstance));
Map<String, TrackedEntityDataValue> existingDataValues = getDataElementDataValueMap(dataValues);
for (DataValue value : event.getDataValues()) {
DataElement dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), value.getDataElement());
TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
if (!validateDataValue(dataElement, value.getValue(), importSummary)) {
continue;
}
if (dataValue != null) {
if (StringUtils.isEmpty(value.getValue()) && dataElement.isFileType() && !StringUtils.isEmpty(dataValue.getValue())) {
fileResourceService.deleteFileResource(dataValue.getValue());
}
dataValue.setValue(value.getValue());
dataValue.setProvidedElsewhere(value.getProvidedElsewhere());
dataValueService.updateTrackedEntityDataValue(dataValue);
dataValues.remove(dataValue);
} else {
TrackedEntityDataValue existingDataValue = existingDataValues.get(value.getDataElement());
saveDataValue(programStageInstance, event.getStoredBy(), dataElement, value.getValue(), value.getProvidedElsewhere(), existingDataValue, null);
}
}
if (!singleValue) {
dataValues.forEach(dataValueService::deleteTrackedEntityDataValue);
}
importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
return importSummary;
}
use of org.hisp.dhis.dxf2.importsummary.ImportSummary in project dhis2-core by dhis2.
the class AbstractEventService method saveEvent.
private ImportSummary saveEvent(Program program, ProgramInstance programInstance, ProgramStage programStage, ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit, Event event, User user, ImportOptions importOptions) {
Assert.notNull(program, "Program cannot be null");
Assert.notNull(programInstance, "Program instance cannot be null");
Assert.notNull(programStage, "Program stage cannot be null");
ImportSummary importSummary = new ImportSummary(event.getEvent());
if (importOptions == null) {
importOptions = new ImportOptions();
}
boolean existingEvent = programStageInstance != null;
boolean dryRun = importOptions.isDryRun();
// = new Date();
Date executionDate = null;
if (event.getEventDate() != null) {
executionDate = DateUtils.parseDate(event.getEventDate());
}
Date dueDate = new Date();
if (event.getDueDate() != null) {
dueDate = DateUtils.parseDate(event.getDueDate());
}
String storedBy = getStoredBy(event, importSummary, user);
String completedBy = getCompletedBy(event, importSummary, user);
DataElementCategoryOptionCombo aoc = null;
if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
try {
aoc = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
} catch (IllegalQueryException ex) {
importSummary.getConflicts().add(new ImportConflict(ex.getMessage(), event.getAttributeCategoryOptions()));
}
} else {
aoc = categoryService.getDefaultDataElementCategoryOptionCombo();
}
if (!dryRun) {
if (programStageInstance == null) {
programStageInstance = createProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, event.getEvent(), aoc, importOptions);
} else {
updateProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, programStageInstance, aoc, importOptions);
}
updateTrackedEntityInstance(programStageInstance);
saveTrackedEntityComment(programStageInstance, event, storedBy);
importSummary.setReference(programStageInstance.getUid());
}
Map<String, TrackedEntityDataValue> dataElementValueMap = Maps.newHashMap();
if (existingEvent) {
dataElementValueMap = getDataElementDataValueMap(dataValueService.getTrackedEntityDataValues(programStageInstance));
}
for (DataValue dataValue : event.getDataValues()) {
DataElement dataElement;
if (dataElementValueMap.containsKey(dataValue.getDataElement())) {
dataElement = dataElementValueMap.get(dataValue.getDataElement()).getDataElement();
} else {
dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), dataValue.getDataElement());
}
if (dataElement != null) {
if (validateDataValue(dataElement, dataValue.getValue(), importSummary)) {
String dataValueStoredBy = dataValue.getStoredBy() != null ? dataValue.getStoredBy() : storedBy;
if (!dryRun) {
TrackedEntityDataValue existingDataValue = dataElementValueMap.get(dataValue.getDataElement());
saveDataValue(programStageInstance, dataValueStoredBy, dataElement, dataValue.getValue(), dataValue.getProvidedElsewhere(), existingDataValue, importSummary);
}
}
} else {
importSummary.getConflicts().add(new ImportConflict("dataElement", dataValue.getDataElement() + " is not a valid data element"));
importSummary.getImportCount().incrementIgnored();
}
}
importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
return importSummary;
}
use of org.hisp.dhis.dxf2.importsummary.ImportSummary in project dhis2-core by dhis2.
the class AbstractEnrollmentService method updateEnrollment.
@Override
public ImportSummary updateEnrollment(Enrollment enrollment, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
if (enrollment == null || enrollment.getEnrollment() == null) {
return new ImportSummary(ImportStatus.ERROR, "No enrollment or enrollment ID was supplied").incrementIgnored();
}
ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
ProgramInstance programInstance = programInstanceService.getProgramInstance(enrollment.getEnrollment());
if (programInstance == null) {
return new ImportSummary(ImportStatus.ERROR, "Enrollment ID was not valid.").incrementIgnored();
}
Set<ImportConflict> importConflicts = new HashSet<>();
importConflicts.addAll(checkAttributes(enrollment, importOptions));
importSummary.setConflicts(importConflicts);
if (!importConflicts.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
programInstance.setProgram(program);
programInstance.setEntityInstance(entityInstance);
if (enrollment.getIncidentDate() != null) {
programInstance.setIncidentDate(enrollment.getIncidentDate());
}
if (enrollment.getEnrollmentDate() != null) {
programInstance.setEnrollmentDate(enrollment.getEnrollmentDate());
}
programInstance.setFollowup(enrollment.getFollowup());
if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("DisplayIncidentDate is true but IncidentDate is null ");
importSummary.incrementIgnored();
return importSummary;
}
if (program.getCaptureCoordinates()) {
if (enrollment.getCoordinate() != null && enrollment.getCoordinate().isValid()) {
programInstance.setLatitude(enrollment.getCoordinate().getLatitude());
programInstance.setLongitude(enrollment.getCoordinate().getLongitude());
} else {
programInstance.setLatitude(null);
programInstance.setLongitude(null);
}
}
if (EnrollmentStatus.fromProgramStatus(programInstance.getStatus()) != enrollment.getStatus()) {
if (EnrollmentStatus.CANCELLED == enrollment.getStatus()) {
programInstanceService.cancelProgramInstanceStatus(programInstance);
} else if (EnrollmentStatus.COMPLETED == enrollment.getStatus()) {
programInstanceService.completeProgramInstanceStatus(programInstance);
} else if (EnrollmentStatus.ACTIVE == enrollment.getStatus()) {
programInstanceService.incompleteProgramInstanceStatus(programInstance);
}
}
updateAttributeValues(enrollment, importOptions);
updateDateFields(enrollment, programInstance);
programInstanceService.updateProgramInstance(programInstance);
manager.update(programInstance.getEntityInstance());
saveTrackedEntityComment(programInstance, enrollment);
importSummary.setReference(enrollment.getEnrollment());
importSummary.getImportCount().incrementUpdated();
importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
return importSummary;
}
use of org.hisp.dhis.dxf2.importsummary.ImportSummary in project dhis2-core by dhis2.
the class AbstractEnrollmentService method deleteEnrollment.
// -------------------------------------------------------------------------
// DELETE
// -------------------------------------------------------------------------
@Override
public ImportSummary deleteEnrollment(String uid) {
ProgramInstance programInstance = programInstanceService.getProgramInstance(uid);
if (programInstance != null) {
programInstanceService.deleteProgramInstance(programInstance);
manager.update(programInstance.getEntityInstance());
return new ImportSummary(ImportStatus.SUCCESS, "Deletion of enrollment " + uid + " was successful.").incrementDeleted();
}
return new ImportSummary(ImportStatus.ERROR, "ID " + uid + " does not point to a valid enrollment").incrementIgnored();
}
use of org.hisp.dhis.dxf2.importsummary.ImportSummary in project dhis2-core by dhis2.
the class AbstractEnrollmentService method addEnrollment.
@Override
public ImportSummary addEnrollment(Enrollment enrollment, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(entityInstance);
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
params.setSkipPaging(true);
params.setProgram(program);
params.setTrackedEntityInstance(entityInstance);
params.setProgramStatus(ProgramStatus.ACTIVE);
List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
if (!enrollments.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have an active enrollment in program " + program.getUid());
importSummary.incrementIgnored();
return importSummary;
}
if (program.getOnlyEnrollOnce()) {
params.setProgramStatus(ProgramStatus.COMPLETED);
enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
if (!enrollments.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have a completed enrollment in program " + program.getUid() + ", and this program is" + " configured to only allow enrolling one time.");
importSummary.incrementIgnored();
return importSummary;
}
}
Set<ImportConflict> importConflicts = new HashSet<>();
importConflicts.addAll(checkAttributes(enrollment, importOptions));
importSummary.setConflicts(importConflicts);
if (!importConflicts.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.incrementIgnored();
return importSummary;
}
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, enrollment.getEnrollmentDate(), enrollment.getIncidentDate(), organisationUnit, enrollment.getEnrollment());
if (programInstance == null) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("Could not enroll TrackedEntityInstance " + enrollment.getTrackedEntityInstance() + " into program " + enrollment.getProgram());
importSummary.incrementIgnored();
return importSummary;
}
if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("DisplayIncidentDate is true but IncidentDate is null ");
importSummary.incrementIgnored();
return importSummary;
}
if (program.getCaptureCoordinates()) {
if (enrollment.getCoordinate() != null && enrollment.getCoordinate().isValid()) {
programInstance.setLatitude(enrollment.getCoordinate().getLatitude());
programInstance.setLongitude(enrollment.getCoordinate().getLongitude());
} else {
programInstance.setLatitude(null);
programInstance.setLongitude(null);
}
}
updateAttributeValues(enrollment, importOptions);
updateDateFields(enrollment, programInstance);
programInstance.setFollowup(enrollment.getFollowup());
programInstanceService.updateProgramInstance(programInstance);
manager.update(programInstance.getEntityInstance());
saveTrackedEntityComment(programInstance, enrollment);
importSummary.setReference(programInstance.getUid());
importSummary.getImportCount().incrementImported();
importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
return importSummary;
}
Aggregations