Search in sources :

Example 1 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method getTrackedEntityInstance.

public org.hisp.dhis.trackedentity.TrackedEntityInstance getTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions, ImportSummary importSummary) {
    if (StringUtils.isEmpty(trackedEntityInstance.getOrgUnit())) {
        importSummary.getConflicts().add(new ImportConflict(trackedEntityInstance.getTrackedEntityInstance(), "No org unit ID in tracked entity instance object."));
        return null;
    }
    org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = new org.hisp.dhis.trackedentity.TrackedEntityInstance();
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), trackedEntityInstance.getOrgUnit());
    if (organisationUnit == null) {
        importSummary.getConflicts().add(new ImportConflict(trackedEntityInstance.getTrackedEntityInstance(), "Invalid org unit ID: " + trackedEntityInstance.getOrgUnit()));
        return null;
    }
    entityInstance.setOrganisationUnit(organisationUnit);
    TrackedEntity trackedEntity = getTrackedEntity(importOptions.getIdSchemes(), trackedEntityInstance.getTrackedEntity());
    if (trackedEntity == null) {
        importSummary.getConflicts().add(new ImportConflict(trackedEntityInstance.getTrackedEntityInstance(), "Invalid tracked entity ID: " + trackedEntityInstance.getTrackedEntity()));
        return null;
    }
    entityInstance.setTrackedEntity(trackedEntity);
    entityInstance.setUid(CodeGenerator.isValidUid(trackedEntityInstance.getTrackedEntityInstance()) ? trackedEntityInstance.getTrackedEntityInstance() : CodeGenerator.generateUid());
    return entityInstance;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 2 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method checkRelationships.

private List<ImportConflict> checkRelationships(TrackedEntityInstance trackedEntityInstance) {
    List<ImportConflict> importConflicts = new ArrayList<>();
    for (org.hisp.dhis.dxf2.events.trackedentity.Relationship relationship : trackedEntityInstance.getRelationships()) {
        RelationshipType relationshipType = manager.get(RelationshipType.class, relationship.getRelationship());
        if (relationshipType == null) {
            importConflicts.add(new ImportConflict("Relationship.type", "Invalid type " + relationship.getRelationship()));
        }
        org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstanceA = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, relationship.getTrackedEntityInstanceA());
        if (entityInstanceA == null) {
            importConflicts.add(new ImportConflict("Relationship.trackedEntityInstance", "Invalid trackedEntityInstance " + relationship.getTrackedEntityInstanceA()));
        }
        org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstanceB = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, relationship.getTrackedEntityInstanceB());
        if (entityInstanceB == null) {
            importConflicts.add(new ImportConflict("Relationship.trackedEntityInstance", "Invalid trackedEntityInstance " + relationship.getTrackedEntityInstanceB()));
        }
    }
    return importConflicts;
}
Also used : ArrayList(java.util.ArrayList) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 3 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstance.

@Override
public ImportSummary updateTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummary importSummary = new ImportSummary(trackedEntityInstance.getTrackedEntityInstance());
    trackedEntityInstance.trimValuesToNull();
    Set<ImportConflict> importConflicts = new HashSet<>();
    importConflicts.addAll(checkRelationships(trackedEntityInstance));
    importConflicts.addAll(checkAttributes(trackedEntityInstance, importOptions));
    org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, trackedEntityInstance.getTrackedEntityInstance());
    if (entityInstance == null) {
        importConflicts.add(new ImportConflict("TrackedEntityInstance", "trackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " does not point to valid trackedEntityInstance"));
    }
    OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, trackedEntityInstance.getOrgUnit());
    if (organisationUnit == null) {
        importConflicts.add(new ImportConflict("OrganisationUnit", "orgUnit " + trackedEntityInstance.getOrgUnit() + " does not point to valid organisation unit"));
    }
    importSummary.setConflicts(importConflicts);
    if (!importConflicts.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.getImportCount().incrementIgnored();
        return importSummary;
    }
    entityInstance.setInactive(trackedEntityInstance.isInactive());
    removeRelationships(entityInstance);
    removeAttributeValues(entityInstance);
    teiService.updateTrackedEntityInstance(entityInstance);
    updateRelationships(trackedEntityInstance, entityInstance);
    updateAttributeValues(trackedEntityInstance, entityInstance);
    updateDateFields(trackedEntityInstance, entityInstance);
    teiService.updateTrackedEntityInstance(entityInstance);
    importSummary.setStatus(ImportStatus.SUCCESS);
    importSummary.setReference(entityInstance.getUid());
    importSummary.getImportCount().incrementUpdated();
    importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
    importSummary.setEnrollments(handleEnrollments(trackedEntityInstance, entityInstance, importOptions));
    return importSummary;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 4 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class JacksonTrackedEntityInstanceService method addTrackedEntityInstanceList.

private ImportSummaries addTrackedEntityInstanceList(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
    ImportSummaries importSummaries = new ImportSummaries();
    List<TrackedEntityInstance> create = new ArrayList<>();
    List<TrackedEntityInstance> update = new ArrayList<>();
    List<String> delete = new ArrayList<>();
    if (importOptions.getImportStrategy().isCreate()) {
        create.addAll(trackedEntityInstances);
    } else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
        for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
            if (StringUtils.isEmpty(trackedEntityInstance.getTrackedEntityInstance())) {
                create.add(trackedEntityInstance);
            } else {
                if (!teiService.trackedEntityInstanceExists(trackedEntityInstance.getTrackedEntityInstance())) {
                    create.add(trackedEntityInstance);
                } else {
                    update.add(trackedEntityInstance);
                }
            }
        }
    } else if (importOptions.getImportStrategy().isUpdate()) {
        update.addAll(trackedEntityInstances);
    } else if (importOptions.getImportStrategy().isDelete()) {
        delete.addAll(trackedEntityInstances.stream().map(TrackedEntityInstance::getTrackedEntityInstance).collect(Collectors.toList()));
    }
    importSummaries.addImportSummaries(addTrackedEntityInstances(create, importOptions));
    importSummaries.addImportSummaries(updateTrackedEntityInstances(update, importOptions));
    importSummaries.addImportSummaries(deleteTrackedEntityInstances(delete));
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 5 with TrackedEntityInstance

use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class AbstractEventService method getFromUrl.

@Override
public EventSearchParams getFromUrl(String program, String programStage, ProgramStatus programStatus, Boolean followUp, String orgUnit, OrganisationUnitSelectionMode orgUnitSelectionMode, String trackedEntityInstance, Date startDate, Date endDate, Date dueDateStart, Date dueDateEnd, Date lastUpdatedStartDate, Date lastUpdatedEndDate, EventStatus status, DataElementCategoryOptionCombo attributeOptionCombo, IdSchemes idSchemes, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging, List<Order> orders, List<String> gridOrders, boolean includeAttributes, Set<String> events, Set<String> filters, Set<String> dataElements, boolean includeDeleted) {
    UserCredentials userCredentials = currentUserService.getCurrentUser().getUserCredentials();
    EventSearchParams params = new EventSearchParams();
    Program pr = programService.getProgram(program);
    if (StringUtils.isNotEmpty(program) && pr == null) {
        throw new IllegalQueryException("Program is specified but does not exist: " + program);
    }
    ProgramStage ps = programStageService.getProgramStage(programStage);
    if (StringUtils.isNotEmpty(programStage) && ps == null) {
        throw new IllegalQueryException("Program stage is specified but does not exist: " + programStage);
    }
    OrganisationUnit ou = organisationUnitService.getOrganisationUnit(orgUnit);
    if (StringUtils.isNotEmpty(orgUnit) && ou == null) {
        throw new IllegalQueryException("Org unit is specified but does not exist: " + orgUnit);
    }
    if (ou != null && !organisationUnitService.isInUserHierarchy(ou)) {
        if (!userCredentials.isSuper() && !userCredentials.isAuthorized("F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS")) {
            throw new IllegalQueryException("User has no access to organisation unit: " + ou.getUid());
        }
    }
    if (pr != null && !userCredentials.isSuper() && !userCredentials.canAccessProgram(pr)) {
        throw new IllegalQueryException("User has no access to program: " + pr.getUid());
    }
    TrackedEntityInstance tei = entityInstanceService.getTrackedEntityInstance(trackedEntityInstance);
    if (StringUtils.isNotEmpty(trackedEntityInstance) && tei == null) {
        throw new IllegalQueryException("Tracked entity instance is specified but does not exist: " + trackedEntityInstance);
    }
    if (events != null && filters != null) {
        throw new IllegalQueryException("Event UIDs and filters can not be specified at the same time");
    }
    if (events == null) {
        events = new HashSet<>();
    }
    if (filters != null) {
        if (StringUtils.isNotEmpty(programStage) && ps == null) {
            throw new IllegalQueryException("ProgramStage needs to be specified for event filtering to work");
        }
        for (String filter : filters) {
            QueryItem item = getQueryItem(filter);
            params.getFilters().add(item);
        }
    }
    if (dataElements != null) {
        for (String de : dataElements) {
            QueryItem dataElement = getQueryItem(de);
            params.getDataElements().add(dataElement);
        }
    }
    params.setProgram(pr);
    params.setProgramStage(ps);
    params.setOrgUnit(ou);
    params.setTrackedEntityInstance(tei);
    params.setProgramStatus(programStatus);
    params.setFollowUp(followUp);
    params.setOrgUnitSelectionMode(orgUnitSelectionMode);
    params.setStartDate(startDate);
    params.setEndDate(endDate);
    params.setDueDateStart(dueDateStart);
    params.setDueDateEnd(dueDateEnd);
    params.setLastUpdatedStartDate(lastUpdatedStartDate);
    params.setLastUpdatedEndDate(lastUpdatedEndDate);
    params.setEventStatus(status);
    params.setCategoryOptionCombo(attributeOptionCombo);
    params.setIdSchemes(idSchemes);
    params.setPage(page);
    params.setPageSize(pageSize);
    params.setTotalPages(totalPages);
    params.setSkipPaging(skipPaging);
    params.setIncludeAttributes(includeAttributes);
    params.setOrders(orders);
    params.setGridOrders(gridOrders);
    params.setEvents(events);
    params.setIncludeDeleted(includeDeleted);
    return params;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) QueryItem(org.hisp.dhis.common.QueryItem) Program(org.hisp.dhis.program.Program) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) UserCredentials(org.hisp.dhis.user.UserCredentials) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Aggregations

TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)61 Test (org.junit.jupiter.api.Test)57 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)50 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)37 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)34 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)29 Date (java.util.Date)26 TrackerTest (org.hisp.dhis.dxf2.TrackerTest)26 ArrayList (java.util.ArrayList)25 List (java.util.List)25 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)25 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)24 Event (org.hisp.dhis.dxf2.events.event.Event)23 Program (org.hisp.dhis.program.Program)22 ProgramInstance (org.hisp.dhis.program.ProgramInstance)21 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)20 Collectors (java.util.stream.Collectors)19 User (org.hisp.dhis.user.User)17 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)16 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)16