use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.
the class AbstractEnrollmentService method validateAttributeType.
private List<ImportConflict> validateAttributeType(Attribute attribute, ImportOptions importOptions) {
List<ImportConflict> importConflicts = Lists.newArrayList();
TrackedEntityAttribute teAttribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), attribute.getAttribute());
if (teAttribute == null) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Does not point to a valid attribute."));
return importConflicts;
}
String errorMessage = trackedEntityAttributeService.validateValueType(teAttribute, attribute.getValue());
if (errorMessage != null) {
importConflicts.add(new ImportConflict("Attribute.value", errorMessage));
}
return importConflicts;
}
use of org.hisp.dhis.dxf2.importsummary.ImportConflicts 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.importsummary.ImportConflicts 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.importsummary.ImportConflicts in project dhis2-core by dhis2.
the class DataValueSetServiceTest method testImportDataValuesBooleanCsv.
@Test
void testImportDataValuesBooleanCsv() throws Exception {
in = new ClassPathResource("datavalueset/dataValueSetBooleanTest.csv").getInputStream();
ImportConflicts summary = dataValueSetService.importDataValueSetCsv(in, null, null);
String description = summary.getConflictsDescription();
assertEquals(4, summary.getTotalConflictOccurrenceCount(), description);
assertEquals(4, summary.getConflictOccurrenceCount(ErrorCode.E7619), description);
assertEquals(2, summary.getConflictCount(), description);
Iterator<ImportConflict> conflicts = summary.getConflicts().iterator();
assertArrayEquals(new int[] { 10, 11 }, conflicts.next().getIndexes());
assertArrayEquals(new int[] { 16, 17 }, conflicts.next().getIndexes());
List<String> expectedBools = Lists.newArrayList("true", "false");
List<DataValue> resultBools = mockDataValueBatchHandler.getInserts();
for (DataValue dataValue : resultBools) {
assertTrue(expectedBools.contains(dataValue.getValue()));
}
}
use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.
the class AbstractEnrollmentService method checkAttributes.
private List<ImportConflict> checkAttributes(Enrollment enrollment, ImportOptions importOptions) {
List<ImportConflict> importConflicts = new ArrayList<>();
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = teiService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
Map<String, String> attributeValueMap = Maps.newHashMap();
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());
importConflicts.addAll(validateAttributeType(attribute, importOptions));
}
TrackedEntityInstance instance = trackedEntityInstanceService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
for (TrackedEntityAttribute trackedEntityAttribute : mandatoryMap.keySet()) {
Boolean mandatory = mandatoryMap.get(trackedEntityAttribute);
if (mandatory && !attributeValueMap.containsKey(trackedEntityAttribute.getUid())) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Missing mandatory attribute " + trackedEntityAttribute.getUid()));
continue;
}
if (trackedEntityAttribute.isUnique()) {
OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, instance.getOrgUnit());
importConflicts.addAll(checkScope(trackedEntityInstance, trackedEntityAttribute, attributeValueMap.get(trackedEntityAttribute.getUid()), organisationUnit, program));
}
attributeValueMap.remove(trackedEntityAttribute.getUid());
}
if (!attributeValueMap.isEmpty()) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Only program attributes is allowed for enrollment " + attributeValueMap));
}
return importConflicts;
}
Aggregations