Search in sources :

Example 61 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment 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 62 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment 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 63 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.

the class EnrollmentRowCallbackHandler method getEnrollment.

private Enrollment getEnrollment(ResultSet rs) throws SQLException {
    Enrollment enrollment = new Enrollment();
    enrollment.setEnrollment(rs.getString(getColumnName(UID)));
    MapperGeoUtils.resolveGeometry(rs.getBytes(getColumnName(GEOMETRY))).ifPresent(enrollment::setGeometry);
    enrollment.setTrackedEntityType(rs.getString(getColumnName(TEI_TYPE_UID)));
    enrollment.setTrackedEntityInstance(rs.getString(getColumnName(TEI_UID)));
    enrollment.setOrgUnit(rs.getString(getColumnName(ORGUNIT_UID)));
    enrollment.setOrgUnitName(rs.getString(getColumnName(ORGUNIT_NAME)));
    enrollment.setCreated(DateUtils.getIso8601NoTz(rs.getTimestamp(getColumnName(CREATED))));
    enrollment.setCreatedAtClient(DateUtils.getIso8601NoTz(rs.getTimestamp(getColumnName(CREATEDCLIENT))));
    setUserInfoSnapshot(rs, getColumnName(CREATED_BY), enrollment::setCreatedByUserInfo);
    enrollment.setLastUpdated(DateUtils.getIso8601NoTz(rs.getTimestamp(getColumnName(UPDATED))));
    enrollment.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(rs.getTimestamp(getColumnName(UPDATEDCLIENT))));
    setUserInfoSnapshot(rs, getColumnName(LAST_UPDATED_BY), enrollment::setLastUpdatedByUserInfo);
    enrollment.setProgram(rs.getString(getColumnName(PROGRAM_UID)));
    enrollment.setStatus(EnrollmentStatus.fromStatusString(rs.getString(getColumnName(STATUS))));
    enrollment.setEnrollmentDate(rs.getTimestamp(getColumnName(ENROLLMENTDATE)));
    enrollment.setIncidentDate(rs.getTimestamp(getColumnName(INCIDENTDATE)));
    final boolean followup = rs.getBoolean(getColumnName(FOLLOWUP));
    enrollment.setFollowup(rs.wasNull() ? null : followup);
    enrollment.setCompletedDate(rs.getTimestamp(getColumnName(COMPLETED)));
    enrollment.setCompletedBy(rs.getString(getColumnName(COMPLETEDBY)));
    enrollment.setStoredBy(rs.getString(getColumnName(STOREDBY)));
    enrollment.setDeleted(rs.getBoolean(getColumnName(DELETED)));
    enrollment.setId(rs.getLong(getColumnName(ID)));
    return enrollment;
}
Also used : Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment)

Example 64 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment 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)

Example 65 with Enrollment

use of org.hisp.dhis.dxf2.events.enrollment.Enrollment in project dhis2-core by dhis2.

the class DefaultEnrollmentStore method getEnrollmentsByTrackedEntityInstanceIdsPartitioned.

private Multimap<String, Enrollment> getEnrollmentsByTrackedEntityInstanceIdsPartitioned(List<Long> ids, AggregateContext ctx) {
    EnrollmentRowCallbackHandler handler = new EnrollmentRowCallbackHandler();
    jdbcTemplate.query(getQuery(GET_ENROLLMENT_SQL_BY_TEI, ctx, " pi.programid IN (:programIds)", FILTER_OUT_DELETED_ENROLLMENTS), createIdsParam(ids).addValue("programIds", ctx.getPrograms()), handler);
    return handler.getItems();
}
Also used : EnrollmentRowCallbackHandler(org.hisp.dhis.dxf2.events.trackedentity.store.mapper.EnrollmentRowCallbackHandler)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)46 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)44 Test (org.junit.jupiter.api.Test)35 Event (org.hisp.dhis.dxf2.events.event.Event)31 Date (java.util.Date)28 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)23 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)20 ArrayList (java.util.ArrayList)18 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)18 ProgramInstance (org.hisp.dhis.program.ProgramInstance)18 List (java.util.List)17 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)17 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)14 Program (org.hisp.dhis.program.Program)14 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)13 Map (java.util.Map)12 Set (java.util.Set)12 Collectors (java.util.stream.Collectors)12 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)12 Lists (com.google.common.collect.Lists)11