Search in sources :

Example 81 with TrackedEntityInstance

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

the class AbstractTrackedEntityInstanceService method updateAttributeValues.

private void updateAttributeValues(TrackedEntityInstance dtoEntityInstance, org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance, Program program, User user) {
    Set<String> incomingAttributes = new HashSet<>();
    Map<String, TrackedEntityAttributeValue> teiAttributeToValueMap = getTeiAttributeValueMap(trackedEntityAttributeValueService.getTrackedEntityAttributeValues(daoEntityInstance));
    for (Attribute dtoAttribute : dtoEntityInstance.getAttributes()) {
        String storedBy = getStoredBy(dtoAttribute, new ImportSummary(), User.username(user, Constants.UNKNOWN));
        TrackedEntityAttributeValue existingAttributeValue = teiAttributeToValueMap.get(dtoAttribute.getAttribute());
        incomingAttributes.add(dtoAttribute.getAttribute());
        if (// value exists
        existingAttributeValue != null) {
            if (// value
            !existingAttributeValue.getValue().equals(dtoAttribute.getValue())) // is
            // changed,
            // do
            // update
            {
                existingAttributeValue.setStoredBy(storedBy);
                existingAttributeValue.setValue(dtoAttribute.getValue());
                trackedEntityAttributeValueService.updateTrackedEntityAttributeValue(existingAttributeValue, user);
            }
        } else // value is new, do add
        {
            TrackedEntityAttribute daoEntityAttribute = trackedEntityAttributeService.getTrackedEntityAttribute(dtoAttribute.getAttribute());
            TrackedEntityAttributeValue newAttributeValue = new TrackedEntityAttributeValue();
            newAttributeValue.setStoredBy(storedBy);
            newAttributeValue.setEntityInstance(daoEntityInstance);
            newAttributeValue.setValue(dtoAttribute.getValue());
            newAttributeValue.setAttribute(daoEntityAttribute);
            daoEntityInstance.getTrackedEntityAttributeValues().add(newAttributeValue);
            trackedEntityAttributeValueService.addTrackedEntityAttributeValue(newAttributeValue);
        }
    }
    if (program != null) {
        for (TrackedEntityAttribute att : program.getTrackedEntityAttributes()) {
            TrackedEntityAttributeValue attVal = teiAttributeToValueMap.get(att.getUid());
            if (attVal != null && !incomingAttributes.contains(att.getUid())) {
                trackedEntityAttributeValueService.deleteTrackedEntityAttributeValue(attVal);
            }
        }
    }
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) HashSet(java.util.HashSet)

Example 82 with TrackedEntityInstance

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

the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstances.

// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
private ImportSummaries updateTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
    List<List<TrackedEntityInstance>> partitions = Lists.partition(trackedEntityInstances, FLUSH_FREQUENCY);
    importOptions = updateImportOptions(importOptions);
    ImportSummaries importSummaries = new ImportSummaries();
    List<Enrollment> enrollments = new ArrayList<>();
    for (List<TrackedEntityInstance> _trackedEntityInstances : partitions) {
        reloadUser(importOptions);
        prepareCaches(_trackedEntityInstances, importOptions.getUser());
        for (TrackedEntityInstance trackedEntityInstance : _trackedEntityInstances) {
            ImportSummary importSummary = updateTrackedEntityInstance(trackedEntityInstance, null, importOptions, false, false);
            importSummaries.addImportSummary(importSummary);
            if (importSummary.isStatus(ImportStatus.SUCCESS)) {
                enrollments.addAll(trackedEntityInstance.getEnrollments());
            }
        }
        clearSession();
    }
    ImportSummaries enrollmentImportSummaries = enrollmentService.addEnrollmentList(enrollments, importOptions);
    linkEnrollmentSummaries(importSummaries, enrollmentImportSummaries, enrollments);
    return importSummaries;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) List(java.util.List) ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 83 with TrackedEntityInstance

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

the class AbstractTrackedEntityInstanceService method deleteTrackedEntityInstances.

@Override
@Transactional
public ImportSummaries deleteTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
    ImportSummaries importSummaries = new ImportSummaries();
    importOptions = updateImportOptions(importOptions);
    int counter = 0;
    for (TrackedEntityInstance tei : trackedEntityInstances) {
        importSummaries.addImportSummary(deleteTrackedEntityInstance(tei.getTrackedEntityInstance(), tei, importOptions));
        if (counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    clearSession();
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Transactional(org.springframework.transaction.annotation.Transactional)

Example 84 with TrackedEntityInstance

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

the class AbstractTrackedEntityInstanceService method addTrackedEntityInstance.

private ImportSummary addTrackedEntityInstance(TrackedEntityInstance dtoEntityInstance, ImportOptions importOptions, boolean handleEnrollments, boolean bulkImport) {
    if (!bulkImport && teiService.trackedEntityInstanceExistsIncludingDeleted(dtoEntityInstance.getTrackedEntityInstance())) {
        return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance " + dtoEntityInstance.getTrackedEntityInstance() + " already exists or was deleted earlier").setReference(dtoEntityInstance.getTrackedEntityInstance()).incrementIgnored();
    }
    importOptions = updateImportOptions(importOptions);
    dtoEntityInstance.trimValuesToNull();
    ImportSummary importSummary = new ImportSummary(dtoEntityInstance.getTrackedEntityInstance());
    checkTrackedEntityType(dtoEntityInstance, importOptions, importSummary);
    checkAttributes(dtoEntityInstance, importOptions, importSummary, false);
    if (importSummary.hasConflicts()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.getImportCount().incrementIgnored();
        return importSummary;
    }
    org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = createDAOTrackedEntityInstance(dtoEntityInstance, importOptions, importSummary);
    if (importSummary.hasConflicts()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.getImportCount().incrementIgnored();
        return importSummary;
    }
    if (daoEntityInstance == null) {
        return importSummary;
    }
    List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoEntityInstance);
    if (!errors.isEmpty()) {
        return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
    }
    teiService.addTrackedEntityInstance(daoEntityInstance);
    addAttributeValues(dtoEntityInstance, daoEntityInstance, importOptions.getUser());
    importSummary.setReference(daoEntityInstance.getUid());
    importSummary.getImportCount().incrementImported();
    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 : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment)

Example 85 with TrackedEntityInstance

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

the class RelationshipRowCallbackHandler method createItem.

/**
 * The SQL query that generates the ResultSet used by this
 * {@see RowCallbackHandler} fetches both sides of a relationship: since
 * each side can be a Tracked Entity Instance, a Program Instance or a
 * Program Stage Instance, the query adds an "hint" to the final result to
 * help this Handler to correctly associate the type to the left or right
 * side of the relationship. The "typeWithUid" variable contains the UID of
 * the object and a string representing the type. E.g.
 *
 * tei|dj3382832 psi|332983893
 *
 * This function parses the string and extract the type and the uid, in
 * order to instantiate the appropriate object and assign it to the
 * {@see RelationshipItem}
 *
 * @param typeWithUid a String containing the object type and the UID of the
 *        object, separated by | (pipe)
 * @return a {@see RelationshipItem}
 */
private RelationshipItem createItem(String typeWithUid) {
    if (StringUtils.isEmpty(typeWithUid)) {
        return new RelationshipItem();
    }
    RelationshipItem ri = new RelationshipItem();
    final String type = typeWithUid.split("\\|")[0];
    final String uid = typeWithUid.split("\\|")[1];
    switch(type) {
        case "tei":
            TrackedEntityInstance tei = new TrackedEntityInstance();
            tei.clear();
            tei.setTrackedEntityInstance(uid);
            ri.setTrackedEntityInstance(tei);
            break;
        case "pi":
            Enrollment pi = new Enrollment();
            pi.setEnrollment(uid);
            ri.setEnrollment(pi);
            break;
        case "psi":
            Event psi = new Event();
            psi.setEvent(uid);
            ri.setEvent(psi);
            break;
        default:
            log.warn("Expecting tei|psi|pi as type when fetching a relationship, got: " + type);
    }
    return ri;
}
Also used : Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) Event(org.hisp.dhis.dxf2.events.event.Event) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) RelationshipItem(org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem)

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