use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractEnrollmentService method checkAttributes.
private void checkAttributes(org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance, Enrollment enrollment, ImportOptions importOptions, ImportConflicts importConflicts) {
Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
Map<String, String> attributeValueMap = Maps.newHashMap();
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
for (ProgramTrackedEntityAttribute programTrackedEntityAttribute : program.getProgramAttributes()) {
mandatoryMap.put(programTrackedEntityAttribute.getAttribute(), programTrackedEntityAttribute.isMandatory());
}
// ignore attributes which do not belong to this program
trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> mandatoryMap.containsKey(value.getAttribute())).forEach(value -> attributeValueMap.put(value.getAttribute().getUid(), value.getValue()));
for (Attribute attribute : enrollment.getAttributes()) {
attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
validateAttributeType(attribute, importOptions, importConflicts);
}
List<String> errors = trackerAccessManager.canRead(importOptions.getUser(), trackedEntityInstance);
if (!errors.isEmpty()) {
throw new IllegalQueryException(errors.toString());
}
checkAttributeForMandatoryMaxLengthAndUniqueness(trackedEntityInstance, importOptions, importConflicts, mandatoryMap, attributeValueMap);
if (!attributeValueMap.isEmpty()) {
importConflicts.addConflict(ATTRIBUTE_ATTRIBUTE, "Only program attributes is allowed for enrollment " + attributeValueMap);
}
if (!program.getSelectEnrollmentDatesInFuture()) {
if (Objects.nonNull(enrollment.getEnrollmentDate()) && enrollment.getEnrollmentDate().after(new Date())) {
importConflicts.addConflict("Enrollment.date", "Enrollment Date can't be future date :" + enrollment.getEnrollmentDate());
}
}
if (!program.getSelectIncidentDatesInFuture()) {
if (Objects.nonNull(enrollment.getIncidentDate()) && enrollment.getIncidentDate().after(new Date())) {
importConflicts.addConflict("Enrollment.incidentDate", "Incident Date can't be future date :" + enrollment.getIncidentDate());
}
}
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class ProgramInstanceSupplier method get.
public Map<String, ProgramInstance> get(ImportOptions importOptions, Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap, List<Event> events) {
if (events == null) {
return new HashMap<>();
}
// Collect all the program instance UIDs to pass as SQL query argument
Set<String> programInstanceUids = events.stream().filter(e -> StringUtils.isNotEmpty(e.getEnrollment())).map(Event::getEnrollment).collect(Collectors.toSet());
Map<String, ProgramInstance> programInstances = new HashMap<>();
if (!programInstanceUids.isEmpty()) {
// Create a bi-directional map enrollment uid -> event uid
Multimap<String, String> programInstanceToEvent = HashMultimap.create();
for (Event event : events) {
programInstanceToEvent.put(event.getEnrollment(), event.getUid());
}
// Collect all the Program Stage Instances specified in the Events
// (enrollment
// property)
programInstances = getProgramInstancesByUid(importOptions, events, programInstanceToEvent, programInstanceUids);
}
mapExistingEventsToProgramInstances(importOptions, events, programInstances);
mapEventsToProgramInstanceByTei(importOptions, events, programInstances, teiMap);
return programInstances;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class JacksonEnrollmentService method addEnrollmentList.
@Override
public ImportSummaries addEnrollmentList(List<Enrollment> enrollments, ImportOptions importOptions) {
ImportSummaries importSummaries = new ImportSummaries();
importOptions = updateImportOptions(importOptions);
List<Enrollment> create = new ArrayList<>();
List<Enrollment> update = new ArrayList<>();
List<Enrollment> delete = new ArrayList<>();
if (importOptions.getImportStrategy().isCreate()) {
create.addAll(enrollments);
} else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
sortCreatesAndUpdates(enrollments, create, update);
} else if (importOptions.getImportStrategy().isUpdate()) {
update.addAll(enrollments);
} else if (importOptions.getImportStrategy().isDelete()) {
delete.addAll(enrollments);
} else if (importOptions.getImportStrategy().isSync()) {
for (Enrollment enrollment : enrollments) {
if (enrollment.isDeleted()) {
delete.add(enrollment);
} else {
sortCreatesAndUpdates(enrollment, create, update);
}
}
}
importSummaries.addImportSummaries(addEnrollments(create, importOptions, null, true));
importSummaries.addImportSummaries(updateEnrollments(update, importOptions, true));
importSummaries.addImportSummaries(deleteEnrollments(delete, importOptions, true));
if (ImportReportMode.ERRORS == importOptions.getReportMode()) {
importSummaries.getImportSummaries().removeIf(is -> !is.hasConflicts());
}
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstance.
private ImportSummary updateTrackedEntityInstance(TrackedEntityInstance dtoEntityInstance, String programId, ImportOptions importOptions, boolean singleUpdate, boolean handleEnrollments) {
ImportSummary importSummary = new ImportSummary(dtoEntityInstance.getTrackedEntityInstance());
importOptions = updateImportOptions(importOptions);
dtoEntityInstance.trimValuesToNull();
checkAttributes(dtoEntityInstance, importOptions, importSummary, true);
org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = teiService.getTrackedEntityInstance(dtoEntityInstance.getTrackedEntityInstance(), importOptions.getUser());
List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoEntityInstance);
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), dtoEntityInstance.getOrgUnit());
Program program = getProgram(importOptions.getIdSchemes(), programId);
if (daoEntityInstance == null || !errors.isEmpty() || organisationUnit == null || importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
if (daoEntityInstance == null) {
String message = "You are trying to add or update tracked entity instance " + dtoEntityInstance.getTrackedEntityInstance() + " that has already been deleted";
importSummary.addConflict("TrackedEntityInstance", message);
} else if (!errors.isEmpty()) {
importSummary.setDescription(errors.toString());
} else if (organisationUnit == null) {
String message = "Org unit " + dtoEntityInstance.getOrgUnit() + " does not exist";
importSummary.addConflict("OrganisationUnit", message);
}
return importSummary;
}
daoEntityInstance.setOrganisationUnit(organisationUnit);
daoEntityInstance.setInactive(dtoEntityInstance.isInactive());
daoEntityInstance.setPotentialDuplicate(dtoEntityInstance.isPotentialDuplicate());
if (dtoEntityInstance.getGeometry() != null) {
FeatureType featureType = daoEntityInstance.getTrackedEntityType().getFeatureType();
if (featureType.equals(FeatureType.NONE) || !featureType.equals(FeatureType.getTypeFromName(dtoEntityInstance.getGeometry().getGeometryType()))) {
importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Geometry does not conform to feature type '" + featureType + "'");
importSummary.getImportCount().incrementIgnored();
return importSummary;
} else {
daoEntityInstance.setGeometry(dtoEntityInstance.getGeometry());
}
} else if (!FeatureType.NONE.equals(dtoEntityInstance.getFeatureType()) && dtoEntityInstance.getCoordinates() != null) {
try {
daoEntityInstance.setGeometry(GeoUtils.getGeometryFromCoordinatesAndType(dtoEntityInstance.getFeatureType(), dtoEntityInstance.getCoordinates()));
} catch (IOException e) {
importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Could not parse coordinates");
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
} else {
daoEntityInstance.setGeometry(null);
}
if (!importOptions.isIgnoreEmptyCollection() || !dtoEntityInstance.getAttributes().isEmpty()) {
updateAttributeValues(dtoEntityInstance, daoEntityInstance, program, importOptions.getUser());
}
updateDateFields(dtoEntityInstance, daoEntityInstance);
teiService.updateTrackedEntityInstance(daoEntityInstance);
importSummary.setReference(daoEntityInstance.getUid());
importSummary.getImportCount().incrementUpdated();
if (singleUpdate && (!importOptions.isIgnoreEmptyCollection() || !dtoEntityInstance.getRelationships().isEmpty())) {
importSummary.setRelationships(handleRelationships(dtoEntityInstance, daoEntityInstance, importOptions));
}
if (handleEnrollments) {
importSummary.setEnrollments(handleEnrollments(dtoEntityInstance, daoEntityInstance, importOptions));
} else {
for (Enrollment enrollment : dtoEntityInstance.getEnrollments()) {
enrollment.setTrackedEntityType(dtoEntityInstance.getTrackedEntityType());
enrollment.setTrackedEntityInstance(daoEntityInstance.getUid());
}
}
return importSummary;
}
use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method handleEnrollments.
private ImportSummaries handleEnrollments(TrackedEntityInstance dtoEntityInstance, org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance, ImportOptions importOptions) {
List<Enrollment> create = new ArrayList<>();
List<Enrollment> update = new ArrayList<>();
List<Enrollment> delete = new ArrayList<>();
for (Enrollment enrollment : dtoEntityInstance.getEnrollments()) {
enrollment.setTrackedEntityType(dtoEntityInstance.getTrackedEntityType());
enrollment.setTrackedEntityInstance(daoEntityInstance.getUid());
if (importOptions.getImportStrategy().isSync() && enrollment.isDeleted()) {
delete.add(enrollment);
} else if (!programInstanceService.programInstanceExists(enrollment.getEnrollment())) {
create.add(enrollment);
} else {
update.add(enrollment);
}
}
ImportSummaries importSummaries = new ImportSummaries();
importSummaries.addImportSummaries(enrollmentService.deleteEnrollments(delete, importOptions, false));
importSummaries.addImportSummaries(enrollmentService.updateEnrollments(update, importOptions, false));
importSummaries.addImportSummaries(enrollmentService.addEnrollments(create, importOptions, daoEntityInstance, false));
return importSummaries;
}
Aggregations