Search in sources :

Example 1 with TEA_VALUE_MAX_LENGTH

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()));
            }
        }
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) Authorities(org.hisp.dhis.security.Authorities) Date(java.util.Date) Restrictions(org.hisp.dhis.query.Restrictions) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) TrackedEntityInstanceAuditService(org.hisp.dhis.trackedentity.TrackedEntityInstanceAuditService) ReservedValueService(org.hisp.dhis.reservedvalue.ReservedValueService) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) RelationshipService(org.hisp.dhis.relationship.RelationshipService) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) TrackedEntityAttributeStore(org.hisp.dhis.trackedentity.TrackedEntityAttributeStore) EnrollmentService(org.hisp.dhis.dxf2.events.enrollment.EnrollmentService) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) TrackedEntityProgramOwner(org.hisp.dhis.trackedentity.TrackedEntityProgramOwner) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) Objects(java.util.Objects) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) TEA_VALUE_MAX_LENGTH(org.hisp.dhis.trackedentity.TrackedEntityAttributeService.TEA_VALUE_MAX_LENGTH) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Geometry(org.locationtech.jts.geom.Geometry) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityInstanceAggregate(org.hisp.dhis.dxf2.events.aggregates.TrackedEntityInstanceAggregate) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) Constants(org.hisp.dhis.dxf2.Constants) AuditType(org.hisp.dhis.common.AuditType) GeoUtils(org.hisp.dhis.system.util.GeoUtils) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Notifier(org.hisp.dhis.system.notification.Notifier) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) User(org.hisp.dhis.user.User) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) TrackedEntityInstanceAudit(org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit) IdSchemes(org.hisp.dhis.common.IdSchemes) FileResource(org.hisp.dhis.fileresource.FileResource) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) FeatureType(org.hisp.dhis.organisationunit.FeatureType) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) Transactional(org.springframework.transaction.annotation.Transactional) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ArrayList(java.util.ArrayList)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1 TrackedEntityInstanceAudit (org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit)1 AuditType (org.hisp.dhis.common.AuditType)1 CodeGenerator (org.hisp.dhis.common.CodeGenerator)1 IdSchemes (org.hisp.dhis.common.IdSchemes)1