use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method checkAttributes.
private void checkAttributes(TrackedEntityInstance dtoEntityInstance, ImportOptions importOptions, ImportConflicts importConflicts, boolean teiExistsInDatabase) {
if (dtoEntityInstance.getAttributes().isEmpty()) {
return;
}
List<String> fileValues = new ArrayList<>();
org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = null;
if (teiExistsInDatabase) {
daoEntityInstance = teiService.getTrackedEntityInstance(dtoEntityInstance.getTrackedEntityInstance(), importOptions.getUser());
if (daoEntityInstance == null) {
return;
}
daoEntityInstance.getTrackedEntityAttributeValues().stream().filter(attrVal -> attrVal.getAttribute().getValueType().isFile()).forEach(attrVal -> fileValues.add(attrVal.getValue()));
}
for (Attribute attribute : dtoEntityInstance.getAttributes()) {
if (StringUtils.isNotEmpty(attribute.getValue())) {
// Cache was populated in prepareCaches, so I should hit the
// cache
TrackedEntityAttribute daoEntityAttribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), attribute.getAttribute());
if (daoEntityAttribute == null) {
importConflicts.addConflict("Attribute.attribute", "Invalid attribute " + attribute.getAttribute());
continue;
}
if (attribute.getValue() != null && attribute.getValue().length() > TEA_VALUE_MAX_LENGTH) {
// We shorten the value to first 25 characters, since we
// dont want to post a 1200+ string back.
importConflicts.addConflict("Attribute.value", String.format("Value exceeds the character limit of %s characters: '%s...'", TEA_VALUE_MAX_LENGTH, attribute.getValue().substring(0, 25)));
}
if (daoEntityAttribute.isUnique()) {
// Cache was populated in prepareCaches, so I should hit the
// cache
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), dtoEntityInstance.getOrgUnit());
checkAttributeUniquenessWithinScope(daoEntityInstance, daoEntityAttribute, attribute.getValue(), organisationUnit, importConflicts);
}
validateAttributeType(attribute, importOptions, importConflicts);
if (daoEntityAttribute.getValueType().isFile() && checkAssigned(attribute, fileValues)) {
importConflicts.addConflict("Attribute.value", String.format("File resource with uid '%s' has already been assigned to a different object", attribute.getValue()));
}
}
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance 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;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method deleteTrackedEntityInstance.
private ImportSummary deleteTrackedEntityInstance(String uid, TrackedEntityInstance dtoEntityInstance, ImportOptions importOptions) {
ImportSummary importSummary = new ImportSummary();
importOptions = updateImportOptions(importOptions);
boolean teiExists = teiService.trackedEntityInstanceExists(uid);
if (teiExists) {
org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = teiService.getTrackedEntityInstance(uid);
if (dtoEntityInstance != null) {
importSummary.setReference(uid);
importSummary.setEnrollments(handleEnrollments(dtoEntityInstance, daoEntityInstance, importOptions));
}
if (importOptions.getUser() != null) {
isAllowedToDelete(importOptions.getUser(), daoEntityInstance, importSummary);
if (importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setReference(daoEntityInstance.getUid());
importSummary.incrementIgnored();
return importSummary;
}
}
teiService.deleteTrackedEntityInstance(daoEntityInstance);
importSummary.setStatus(ImportStatus.SUCCESS);
importSummary.setDescription("Deletion of tracked entity instance " + uid + " was successful");
return importSummary.incrementDeleted();
} else {
importSummary.setStatus(ImportStatus.SUCCESS);
importSummary.setDescription("Tracked entity instance " + uid + " cannot be deleted as it is not present in the system");
return importSummary.incrementIgnored();
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method addTrackedEntityInstances.
@Override
@Transactional
public ImportSummaries addTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
importOptions = updateImportOptions(importOptions);
ImportSummaries importSummaries = new ImportSummaries();
List<Enrollment> enrollments = new ArrayList<>();
List<TrackedEntityInstance> validTeis = resolveImportableTeis(trackedEntityInstances, importSummaries);
List<List<TrackedEntityInstance>> partitions = Lists.partition(validTeis, FLUSH_FREQUENCY);
for (List<TrackedEntityInstance> _trackedEntityInstances : partitions) {
reloadUser(importOptions);
prepareCaches(_trackedEntityInstances, importOptions.getUser());
for (TrackedEntityInstance trackedEntityInstance : _trackedEntityInstances) {
ImportSummary importSummary = addTrackedEntityInstance(trackedEntityInstance, importOptions, false, true);
importSummaries.addImportSummary(importSummary);
if (importSummary.isStatus(ImportStatus.SUCCESS)) {
enrollments.addAll(trackedEntityInstance.getEnrollments());
}
}
clearSession();
}
ImportSummaries enrollmentImportSummaries = enrollmentService.addEnrollmentList(enrollments, importOptions);
linkEnrollmentSummaries(importSummaries, enrollmentImportSummaries, enrollments);
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class ProgramInstanceRepeatableStageCheck method check.
@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
IdScheme scheme = ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme();
ProgramStage programStage = ctx.getProgramStage(scheme, event.getProgramStage());
ProgramInstance programInstance = ctx.getProgramInstanceMap().get(event.getUid());
Program program = ctx.getProgramsMap().get(event.getProgram());
TrackedEntityInstance tei = null;
if (program.isRegistration()) {
tei = ctx.getTrackedEntityInstanceMap().get(event.getUid()).getLeft();
}
// @formatter:off
if (programInstance != null && tei != null && program.isRegistration() && !programStage.getRepeatable() && hasProgramStageInstance(ctx.getServiceDelegator().getJdbcTemplate(), programInstance.getId(), programStage.getId(), tei.getId())) {
return new ImportSummary(ImportStatus.ERROR, "Program stage is not repeatable and an event already exists").setReference(event.getEvent()).incrementIgnored();
}
return success();
}
Aggregations