use of org.hisp.dhis.trackedentity.TrackedEntityAttributeService.TEA_VALUE_MAX_LENGTH 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()));
}
}
}
}
Aggregations