Search in sources :

Example 71 with Program

use of org.hisp.dhis.program.Program 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 72 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method filterOutSkipSyncAttributesIfApplies.

private Set<TrackedEntityAttribute> filterOutSkipSyncAttributesIfApplies(TrackedEntityInstanceParams params, TrackedEntityInstance trackedEntityInstance, Set<TrackedEntityAttribute> readableAttributes) {
    Set<TrackedEntityAttribute> readableAttributesCopy;
    if (params.isDataSynchronizationQuery()) {
        List<String> programs = trackedEntityInstance.getEnrollments().stream().map(Enrollment::getProgram).collect(Collectors.toList());
        readableAttributesCopy = readableAttributes.stream().filter(att -> !att.getSkipSynchronization()).collect(Collectors.toSet());
        IdSchemes idSchemes = new IdSchemes();
        for (String programUid : programs) {
            Program program = getProgram(idSchemes, programUid);
            if (program != null) {
                readableAttributesCopy.addAll(program.getTrackedEntityAttributes().stream().filter(att -> !att.getSkipSynchronization()).collect(Collectors.toSet()));
            }
        }
    } else {
        readableAttributesCopy = new HashSet<>(readableAttributes);
    }
    return readableAttributesCopy;
}
Also used : Program(org.hisp.dhis.program.Program) IdSchemes(org.hisp.dhis.common.IdSchemes) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute)

Example 73 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class AttributeOptionComboDateCheck method check.

@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
    CategoryOptionCombo attributeOptionCombo = ctx.getCategoryOptionComboMap().get(event.getUid());
    Date executionDate = null;
    if (event.getEventDate() != null) {
        executionDate = DateUtils.parseDate(event.getEventDate());
    }
    Date dueDate = new Date();
    if (event.getDueDate() != null) {
        dueDate = DateUtils.parseDate(event.getDueDate());
    }
    Date eventDate = executionDate != null ? executionDate : dueDate;
    if (eventDate == null) {
        return error("Event date can not be empty", event.getEvent());
    }
    Program program = ctx.getProgramsMap().get(event.getProgram());
    for (CategoryOption categoryOption : attributeOptionCombo.getCategoryOptions()) {
        if (categoryOption.getStartDate() != null && eventDate.compareTo(categoryOption.getStartDate()) < 0) {
            return error("Event date " + getMediumDateString(eventDate) + " is before start date " + getMediumDateString(categoryOption.getStartDate()) + " for attributeOption '" + categoryOption.getName() + "'", event.getEvent());
        }
        if (categoryOption.getEndDate() != null && eventDate.compareTo(categoryOption.getAdjustedEndDate(program)) > 0) {
            return error("Event date " + getMediumDateString(eventDate) + " is after end date " + getMediumDateString(categoryOption.getAdjustedEndDate(program)) + " for attributeOption '" + categoryOption.getName() + "' in program '" + program.getName() + "'", event.getEvent());
        }
    }
    return success();
}
Also used : Program(org.hisp.dhis.program.Program) CategoryOption(org.hisp.dhis.category.CategoryOption) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Date(java.util.Date)

Example 74 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class ProgramOrgUnitCheck method check.

@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
    ProgramInstance programInstance = ctx.getProgramInstanceMap().get(event.getUid());
    final Map<Long, List<Long>> programWithOrgUnitsMap = ctx.getProgramWithOrgUnitsMap();
    final Map<String, OrganisationUnit> organisationUnitMap = ctx.getOrganisationUnitMap();
    if (programInstance != null) {
        final OrganisationUnit organisationUnit = organisationUnitMap.get(event.getUid());
        final Program program = programInstance.getProgram();
        if (organisationUnit != null && program != null) {
            List<Long> ouList = programWithOrgUnitsMap.get(program.getId());
            if (ouList == null || !ouList.contains(organisationUnit.getId())) {
                return error("Program is not assigned to this Organisation Unit: " + event.getOrgUnit(), event.getEvent());
            }
        }
    }
    return success();
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) List(java.util.List)

Example 75 with Program

use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.

the class AttributeOptionComboCheck method check.

@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
    Program program = ctx.getProgramsMap().get(event.getProgram());
    CategoryOptionCombo coc = ctx.getCategoryOptionComboMap().get(event.getUid());
    if (coc != null && coc.isDefault() && program.getCategoryCombo() != null && !program.getCategoryCombo().isDefault()) {
        ImportSummary importSummary = new ImportSummary(event.getEvent());
        importSummary.addConflict("attributeOptionCombo", "Valid attribute option combo must be specified since program does not have the default category combo");
        importSummary.setStatus(ImportStatus.ERROR);
        return importSummary.incrementIgnored();
    }
    return success();
}
Also used : Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Aggregations

Program (org.hisp.dhis.program.Program)344 Test (org.junit.jupiter.api.Test)160 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)123 ProgramStage (org.hisp.dhis.program.ProgramStage)109 ProgramInstance (org.hisp.dhis.program.ProgramInstance)79 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)68 Date (java.util.Date)47 Collectors (java.util.stream.Collectors)44 User (org.hisp.dhis.user.User)44 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)42 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)42 BeforeEach (org.junit.jupiter.api.BeforeEach)42 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)41 ValidationErrorReporter (org.hisp.dhis.tracker.report.ValidationErrorReporter)41 DhisConvenienceTest (org.hisp.dhis.DhisConvenienceTest)40 DhisConvenienceTest.createProgram (org.hisp.dhis.DhisConvenienceTest.createProgram)39 CategoryCombo (org.hisp.dhis.category.CategoryCombo)39 DataElement (org.hisp.dhis.dataelement.DataElement)39 Event (org.hisp.dhis.tracker.domain.Event)38 Enrollment (org.hisp.dhis.tracker.domain.Enrollment)37