use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance 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.trackedentity.TrackedEntityInstance 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.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class TrackedEntityInstanceSupplier method get.
@Override
public Map<String, Pair<TrackedEntityInstance, Boolean>> get(ImportOptions importOptions, List<Event> events) {
if (events == null) {
return new HashMap<>();
}
// @formatter:off
// Collect all the org unit uids to pass as SQL query argument
Set<String> teiUids = events.stream().filter(e -> e.getTrackedEntityInstance() != null).map(Event::getTrackedEntityInstance).collect(Collectors.toSet());
if (isEmpty(teiUids)) {
return new HashMap<>();
}
// Create a map: tei uid -> List [event uid]
Multimap<String, String> teiToEvent = HashMultimap.create();
for (Event event : events) {
teiToEvent.put(event.getTrackedEntityInstance(), event.getUid());
}
//
// Get all TEI associated to the events
//
Map<String, TrackedEntityInstance> teiMap = getTrackedEntityInstances(teiUids, teiToEvent);
Map<String, Pair<TrackedEntityInstance, Boolean>> result = new HashMap<>();
//
for (String event : teiMap.keySet()) {
TrackedEntityInstance tei = teiMap.get(event);
result.put(event, Pair.of(tei, !importOptions.isSkipLastUpdated() ? aclService.canUpdate(importOptions.getUser(), tei) : null));
}
return result;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method checkForExistingTeisIncludingDeleted.
private List<String> checkForExistingTeisIncludingDeleted(List<TrackedEntityInstance> teis, ImportSummaries importSummaries) {
List<String> foundTeis = teiService.getTrackedEntityInstancesUidsIncludingDeleted(teis.stream().map(TrackedEntityInstance::getTrackedEntityInstance).collect(Collectors.toList()));
for (String foundTeiUid : foundTeis) {
ImportSummary is = new ImportSummary(ImportStatus.ERROR, "Tracked entity instance " + foundTeiUid + " already exists or was deleted earlier").setReference(foundTeiUid).incrementIgnored();
importSummaries.addImportSummary(is);
}
return foundTeis;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance 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;
}
Aggregations