Search in sources :

Example 1 with FeatureType

use of org.hisp.dhis.organisationunit.FeatureType 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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) FeatureType(org.hisp.dhis.organisationunit.FeatureType) Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) IOException(java.io.IOException)

Example 2 with FeatureType

use of org.hisp.dhis.organisationunit.FeatureType in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method getTei.

private TrackedEntityInstance getTei(org.hisp.dhis.trackedentity.TrackedEntityInstance daoTrackedEntityInstance, Set<TrackedEntityAttribute> readableAttributes, TrackedEntityInstanceParams params, User user) {
    if (daoTrackedEntityInstance == null) {
        return null;
    }
    TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
    trackedEntityInstance.setTrackedEntityInstance(daoTrackedEntityInstance.getUid());
    trackedEntityInstance.setOrgUnit(daoTrackedEntityInstance.getOrganisationUnit().getUid());
    trackedEntityInstance.setTrackedEntityType(daoTrackedEntityInstance.getTrackedEntityType().getUid());
    trackedEntityInstance.setCreated(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getCreated()));
    trackedEntityInstance.setCreatedAtClient(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getCreatedAtClient()));
    trackedEntityInstance.setLastUpdated(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getLastUpdated()));
    trackedEntityInstance.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(daoTrackedEntityInstance.getLastUpdatedAtClient()));
    trackedEntityInstance.setInactive(Optional.ofNullable(daoTrackedEntityInstance.isInactive()).orElse(false));
    trackedEntityInstance.setGeometry(daoTrackedEntityInstance.getGeometry());
    trackedEntityInstance.setDeleted(daoTrackedEntityInstance.isDeleted());
    trackedEntityInstance.setPotentialDuplicate(daoTrackedEntityInstance.isPotentialDuplicate());
    trackedEntityInstance.setStoredBy(daoTrackedEntityInstance.getStoredBy());
    trackedEntityInstance.setCreatedByUserInfo(daoTrackedEntityInstance.getCreatedByUserInfo());
    trackedEntityInstance.setLastUpdatedByUserInfo(daoTrackedEntityInstance.getLastUpdatedByUserInfo());
    if (daoTrackedEntityInstance.getGeometry() != null) {
        Geometry geometry = daoTrackedEntityInstance.getGeometry();
        FeatureType featureType = FeatureType.getTypeFromName(geometry.getGeometryType());
        trackedEntityInstance.setFeatureType(featureType);
        trackedEntityInstance.setCoordinates(GeoUtils.getCoordinatesFromGeometry(geometry));
    }
    if (params.isIncludeRelationships()) {
        for (RelationshipItem relationshipItem : daoTrackedEntityInstance.getRelationshipItems()) {
            org.hisp.dhis.relationship.Relationship daoRelationship = relationshipItem.getRelationship();
            if (trackerAccessManager.canRead(user, daoRelationship).isEmpty()) {
                Relationship relationship = relationshipService.getRelationship(relationshipItem.getRelationship(), RelationshipParams.FALSE, user);
                trackedEntityInstance.getRelationships().add(relationship);
            }
        }
    }
    if (params.isIncludeEnrollments()) {
        for (ProgramInstance programInstance : daoTrackedEntityInstance.getProgramInstances()) {
            if (trackerAccessManager.canRead(user, programInstance, false).isEmpty() && (params.isIncludeDeleted() || !programInstance.isDeleted())) {
                trackedEntityInstance.getEnrollments().add(enrollmentService.getEnrollment(user, programInstance, params, true));
            }
        }
    }
    if (params.isIncludeProgramOwners()) {
        for (TrackedEntityProgramOwner programOwner : daoTrackedEntityInstance.getProgramOwners()) {
            trackedEntityInstance.getProgramOwners().add(new ProgramOwner(programOwner));
        }
    }
    Set<TrackedEntityAttribute> readableAttributesCopy = filterOutSkipSyncAttributesIfApplies(params, trackedEntityInstance, readableAttributes);
    for (TrackedEntityAttributeValue attributeValue : daoTrackedEntityInstance.getTrackedEntityAttributeValues()) {
        if (readableAttributesCopy.contains(attributeValue.getAttribute())) {
            Attribute attribute = new Attribute();
            attribute.setCreated(DateUtils.getIso8601NoTz(attributeValue.getCreated()));
            attribute.setLastUpdated(DateUtils.getIso8601NoTz(attributeValue.getLastUpdated()));
            attribute.setDisplayName(attributeValue.getAttribute().getDisplayName());
            attribute.setAttribute(attributeValue.getAttribute().getUid());
            attribute.setValueType(attributeValue.getAttribute().getValueType());
            attribute.setCode(attributeValue.getAttribute().getCode());
            attribute.setValue(attributeValue.getValue());
            attribute.setStoredBy(attributeValue.getStoredBy());
            attribute.setSkipSynchronization(attributeValue.getAttribute().getSkipSynchronization());
            trackedEntityInstance.getAttributes().add(attribute);
        }
    }
    return trackedEntityInstance;
}
Also used : TrackedEntityProgramOwner(org.hisp.dhis.trackedentity.TrackedEntityProgramOwner) FeatureType(org.hisp.dhis.organisationunit.FeatureType) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Geometry(org.locationtech.jts.geom.Geometry) TrackedEntityProgramOwner(org.hisp.dhis.trackedentity.TrackedEntityProgramOwner) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Example 3 with FeatureType

use of org.hisp.dhis.organisationunit.FeatureType in project dhis2-core by dhis2.

the class ValidationUtils method validateGeometry.

static void validateGeometry(ValidationErrorReporter reporter, TrackerDto dto, Geometry geometry, FeatureType featureType) {
    checkNotNull(geometry, GEOMETRY_CANT_BE_NULL);
    if (featureType == null) {
        TrackerErrorReport error = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1074).build(reporter.getValidationContext().getBundle());
        reporter.addError(error);
        return;
    }
    FeatureType typeFromName = FeatureType.getTypeFromName(geometry.getGeometryType());
    if (FeatureType.NONE == featureType || featureType != typeFromName) {
        TrackerErrorReport error = TrackerErrorReport.builder().uid(dto.getUid()).trackerType(dto.getTrackerType()).errorCode(TrackerErrorCode.E1012).addArg(featureType.name()).build(reporter.getValidationContext().getBundle());
        reporter.addError(error);
    }
}
Also used : FeatureType(org.hisp.dhis.organisationunit.FeatureType) TrackerErrorReport(org.hisp.dhis.tracker.report.TrackerErrorReport)

Example 4 with FeatureType

use of org.hisp.dhis.organisationunit.FeatureType in project dhis2-core by dhis2.

the class OrganisationUnitPolygonCoveringCoordinateFilter method retain.

@Override
public boolean retain(OrganisationUnit unit) {
    FeatureType featureType = unit.getFeatureType();
    String coordinate = unit.getCoordinates();
    return featureType != null && coordinate != null && !coordinate.isEmpty() && featureType.isPolygon() && GeoUtils.checkPointWithMultiPolygon(longitude, latitude, unit.getCoordinates(), featureType);
}
Also used : FeatureType(org.hisp.dhis.organisationunit.FeatureType)

Example 5 with FeatureType

use of org.hisp.dhis.organisationunit.FeatureType in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method createDAOTrackedEntityInstance.

private org.hisp.dhis.trackedentity.TrackedEntityInstance createDAOTrackedEntityInstance(TrackedEntityInstance dtoEntityInstance, ImportOptions importOptions, ImportSummary importSummary) {
    if (StringUtils.isEmpty(dtoEntityInstance.getOrgUnit())) {
        importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "No org unit ID in tracked entity instance object");
        return null;
    }
    org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = new org.hisp.dhis.trackedentity.TrackedEntityInstance();
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), dtoEntityInstance.getOrgUnit());
    if (organisationUnit == null) {
        importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Invalid org unit ID: " + dtoEntityInstance.getOrgUnit());
        return null;
    }
    daoEntityInstance.setOrganisationUnit(organisationUnit);
    TrackedEntityType trackedEntityType = getTrackedEntityType(importOptions.getIdSchemes(), dtoEntityInstance.getTrackedEntityType());
    if (trackedEntityType == null) {
        importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Invalid tracked entity ID: " + dtoEntityInstance.getTrackedEntityType());
        return null;
    }
    if (dtoEntityInstance.getGeometry() != null) {
        FeatureType featureType = trackedEntityType.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.incrementIgnored();
            return null;
        } 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.incrementIgnored();
            return null;
        }
    } else {
        daoEntityInstance.setGeometry(null);
    }
    daoEntityInstance.setTrackedEntityType(trackedEntityType);
    daoEntityInstance.setUid(CodeGenerator.isValidUid(dtoEntityInstance.getTrackedEntityInstance()) ? dtoEntityInstance.getTrackedEntityInstance() : CodeGenerator.generateUid());
    String storedBy = !StringUtils.isEmpty(dtoEntityInstance.getStoredBy()) ? dtoEntityInstance.getStoredBy() : (importOptions.getUser() == null || StringUtils.isEmpty(importOptions.getUser().getUsername()) ? "system-process" : importOptions.getUser().getUsername());
    daoEntityInstance.setStoredBy(storedBy);
    daoEntityInstance.setPotentialDuplicate(dtoEntityInstance.isPotentialDuplicate());
    updateDateFields(dtoEntityInstance, daoEntityInstance);
    return daoEntityInstance;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) FeatureType(org.hisp.dhis.organisationunit.FeatureType) IOException(java.io.IOException)

Aggregations

FeatureType (org.hisp.dhis.organisationunit.FeatureType)6 IOException (java.io.IOException)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)1 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)1 Program (org.hisp.dhis.program.Program)1 ProgramInstance (org.hisp.dhis.program.ProgramInstance)1 RelationshipItem (org.hisp.dhis.relationship.RelationshipItem)1 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)1 TrackedEntityProgramOwner (org.hisp.dhis.trackedentity.TrackedEntityProgramOwner)1 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)1 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)1 TrackerErrorReport (org.hisp.dhis.tracker.report.TrackerErrorReport)1 Geometry (org.locationtech.jts.geom.Geometry)1 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)1 Point (org.locationtech.jts.geom.Point)1 Polygon (org.locationtech.jts.geom.Polygon)1