Search in sources :

Example 1 with TrackerObjectReport

use of org.hisp.dhis.tracker.report.TrackerObjectReport in project dhis2-core by dhis2.

the class DefaultTrackerObjectsDeletionService method deleteTrackedEntityInstances.

@Override
public TrackerTypeReport deleteTrackedEntityInstances(TrackerBundle bundle) {
    TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.TRACKED_ENTITY);
    List<TrackedEntity> trackedEntities = bundle.getTrackedEntities();
    for (int idx = 0; idx < trackedEntities.size(); idx++) {
        String uid = trackedEntities.get(idx).getTrackedEntity();
        TrackerObjectReport objectReport = new TrackerObjectReport(TrackerType.TRACKED_ENTITY, uid, idx);
        org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = teiService.getTrackedEntityInstance(uid);
        Set<ProgramInstance> programInstances = daoEntityInstance.getProgramInstances();
        List<Enrollment> enrollments = enrollmentTrackerConverterService.to(Lists.newArrayList(programInstances.stream().filter(pi -> !pi.isDeleted()).collect(Collectors.toList())));
        TrackerBundle trackerBundle = TrackerBundle.builder().enrollments(enrollments).user(bundle.getUser()).build();
        deleteEnrollments(trackerBundle);
        teiService.deleteTrackedEntityInstance(daoEntityInstance);
        typeReport.getStats().incDeleted();
        typeReport.addObjectReport(objectReport);
    }
    return typeReport;
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) EnrollmentTrackerConverterService(org.hisp.dhis.tracker.converter.EnrollmentTrackerConverterService) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Relationship(org.hisp.dhis.tracker.domain.Relationship) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport) Collectors(java.util.stream.Collectors) TrackedEntityInstanceService(org.hisp.dhis.trackedentity.TrackedEntityInstanceService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) List(java.util.List) Lists(com.google.common.collect.Lists) RelationshipService(org.hisp.dhis.relationship.RelationshipService) Service(org.springframework.stereotype.Service) EventTrackerConverterService(org.hisp.dhis.tracker.converter.EventTrackerConverterService) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) AllArgsConstructor(lombok.AllArgsConstructor) TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle)

Example 2 with TrackerObjectReport

use of org.hisp.dhis.tracker.report.TrackerObjectReport in project dhis2-core by dhis2.

the class DefaultTrackerObjectsDeletionService method deleteEnrollments.

@Override
public TrackerTypeReport deleteEnrollments(TrackerBundle bundle) {
    TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.ENROLLMENT);
    List<Enrollment> enrollments = bundle.getEnrollments();
    for (int idx = 0; idx < enrollments.size(); idx++) {
        String uid = enrollments.get(idx).getEnrollment();
        TrackerObjectReport objectReport = new TrackerObjectReport(TrackerType.ENROLLMENT, uid, idx);
        ProgramInstance programInstance = programInstanceService.getProgramInstance(uid);
        List<Event> events = eventTrackerConverterService.to(Lists.newArrayList(programInstance.getProgramStageInstances().stream().filter(psi -> !psi.isDeleted()).collect(Collectors.toList())));
        TrackerBundle trackerBundle = TrackerBundle.builder().events(events).user(bundle.getUser()).build();
        deleteEvents(trackerBundle);
        TrackedEntityInstance tei = programInstance.getEntityInstance();
        tei.getProgramInstances().remove(programInstance);
        programInstanceService.deleteProgramInstance(programInstance);
        teiService.updateTrackedEntityInstance(tei);
        typeReport.getStats().incDeleted();
        typeReport.addObjectReport(objectReport);
    }
    return typeReport;
}
Also used : Event(org.hisp.dhis.tracker.domain.Event) EnrollmentTrackerConverterService(org.hisp.dhis.tracker.converter.EnrollmentTrackerConverterService) TrackedEntity(org.hisp.dhis.tracker.domain.TrackedEntity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) Relationship(org.hisp.dhis.tracker.domain.Relationship) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport) Collectors(java.util.stream.Collectors) TrackedEntityInstanceService(org.hisp.dhis.trackedentity.TrackedEntityInstanceService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) List(java.util.List) Lists(com.google.common.collect.Lists) RelationshipService(org.hisp.dhis.relationship.RelationshipService) Service(org.springframework.stereotype.Service) EventTrackerConverterService(org.hisp.dhis.tracker.converter.EventTrackerConverterService) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) AllArgsConstructor(lombok.AllArgsConstructor) TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) Event(org.hisp.dhis.tracker.domain.Event) TrackerBundle(org.hisp.dhis.tracker.bundle.TrackerBundle) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 3 with TrackerObjectReport

use of org.hisp.dhis.tracker.report.TrackerObjectReport in project dhis2-core by dhis2.

the class AbstractTrackerPersister method persist.

/**
 * Template method that can be used by classes extending this class to
 * execute the persistence flow of Tracker entities
 *
 * @param session a valid Hibernate Session
 * @param bundle the Bundle to persist
 * @return a {@link TrackerTypeReport}
 */
@Override
public TrackerTypeReport persist(Session session, TrackerBundle bundle) {
    // 
    // Init the report that will hold the results of the persist operation
    // 
    TrackerTypeReport typeReport = new TrackerTypeReport(getType());
    List<TrackerSideEffectDataBundle> sideEffectDataBundles = new ArrayList<>();
    // 
    // Extract the entities to persist from the Bundle
    // 
    List<T> dtos = getByType(getType(), bundle);
    Set<String> updatedTeiList = bundle.getUpdatedTeis();
    for (int idx = 0; idx < dtos.size(); idx++) {
        // 
        // Create the Report for the entity being persisted
        // 
        final T trackerDto = dtos.get(idx);
        TrackerObjectReport objectReport = new TrackerObjectReport(getType(), trackerDto.getUid(), idx);
        try {
            // 
            // Convert the TrackerDto into an Hibernate-managed entity
            // 
            V convertedDto = convert(bundle, trackerDto);
            // 
            // Handle comments persistence, if required
            // 
            persistComments(bundle.getPreheat(), convertedDto);
            // 
            // Handle ownership records, if required
            // 
            persistOwnership(bundle.getPreheat(), convertedDto);
            updateDataValues(session, bundle.getPreheat(), trackerDto, convertedDto);
            // 
            if (isNew(bundle.getPreheat(), trackerDto)) {
                session.persist(convertedDto);
                typeReport.getStats().incCreated();
                typeReport.addObjectReport(objectReport);
                updateAttributes(session, bundle.getPreheat(), trackerDto, convertedDto);
            } else {
                if (isUpdatable()) {
                    updateAttributes(session, bundle.getPreheat(), trackerDto, convertedDto);
                    session.merge(convertedDto);
                    typeReport.getStats().incUpdated();
                    typeReport.addObjectReport(objectReport);
                    Optional.ofNullable(getUpdatedTrackedEntity(convertedDto)).ifPresent(updatedTeiList::add);
                } else {
                    typeReport.getStats().incIgnored();
                }
            }
            // 
            // Add the entity to the Preheat
            // 
            updatePreheat(bundle.getPreheat(), convertedDto);
            if (FlushMode.OBJECT == bundle.getFlushMode()) {
                session.flush();
            }
            if (!bundle.isSkipSideEffects()) {
                sideEffectDataBundles.add(handleSideEffects(bundle, convertedDto));
            }
            bundle.setUpdatedTeis(updatedTeiList);
        } catch (Exception e) {
            final String msg = "A Tracker Entity of type '" + getType().getName() + "' (" + trackerDto.getUid() + ") failed to persist.";
            if (bundle.getAtomicMode().equals(AtomicMode.ALL)) {
                throw new PersistenceException(msg, e);
            } else {
                // TODO currently we do not keep track of the failed entity
                // in the TrackerObjectReport
                log.warn(msg + "\nThe Import process will process remaining entities.", e);
                typeReport.getStats().incIgnored();
            }
        }
    }
    typeReport.getSideEffectDataBundles().addAll(sideEffectDataBundles);
    return typeReport;
}
Also used : TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) ArrayList(java.util.ArrayList) TrackerSideEffectDataBundle(org.hisp.dhis.tracker.job.TrackerSideEffectDataBundle) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport)

Example 4 with TrackerObjectReport

use of org.hisp.dhis.tracker.report.TrackerObjectReport in project dhis2-core by dhis2.

the class DefaultTrackerObjectsDeletionService method deleteEvents.

@Override
public TrackerTypeReport deleteEvents(TrackerBundle bundle) {
    TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.EVENT);
    List<Event> events = bundle.getEvents();
    for (int idx = 0; idx < events.size(); idx++) {
        String uid = events.get(idx).getEvent();
        TrackerObjectReport objectReport = new TrackerObjectReport(TrackerType.EVENT, uid, idx);
        ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(uid);
        ProgramInstance programInstance = programStageInstance.getProgramInstance();
        programStageInstanceService.deleteProgramStageInstance(programStageInstance);
        if (programStageInstance.getProgramStage().getProgram().isRegistration()) {
            teiService.updateTrackedEntityInstance(programStageInstance.getProgramInstance().getEntityInstance());
            programInstance.getProgramStageInstances().remove(programStageInstance);
            programInstanceService.updateProgramInstance(programInstance);
        }
        typeReport.getStats().incDeleted();
        typeReport.addObjectReport(objectReport);
    }
    return typeReport;
}
Also used : TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport) Event(org.hisp.dhis.tracker.domain.Event) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 5 with TrackerObjectReport

use of org.hisp.dhis.tracker.report.TrackerObjectReport in project dhis2-core by dhis2.

the class DefaultTrackerObjectsDeletionService method deleteRelationShips.

@Override
public TrackerTypeReport deleteRelationShips(TrackerBundle bundle) {
    TrackerTypeReport typeReport = new TrackerTypeReport(TrackerType.RELATIONSHIP);
    List<Relationship> relationships = bundle.getRelationships();
    for (int idx = 0; idx < relationships.size(); idx++) {
        String uid = relationships.get(idx).getRelationship();
        TrackerObjectReport objectReport = new TrackerObjectReport(TrackerType.RELATIONSHIP, uid, idx);
        org.hisp.dhis.relationship.Relationship relationship = relationshipService.getRelationship(uid);
        relationshipService.deleteRelationship(relationship);
        typeReport.getStats().incDeleted();
        typeReport.addObjectReport(objectReport);
    }
    return typeReport;
}
Also used : TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) Relationship(org.hisp.dhis.tracker.domain.Relationship) TrackerObjectReport(org.hisp.dhis.tracker.report.TrackerObjectReport)

Aggregations

TrackerObjectReport (org.hisp.dhis.tracker.report.TrackerObjectReport)5 TrackerTypeReport (org.hisp.dhis.tracker.report.TrackerTypeReport)5 ProgramInstance (org.hisp.dhis.program.ProgramInstance)3 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)3 Event (org.hisp.dhis.tracker.domain.Event)3 Relationship (org.hisp.dhis.tracker.domain.Relationship)3 Lists (com.google.common.collect.Lists)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 AllArgsConstructor (lombok.AllArgsConstructor)2 ProgramInstanceService (org.hisp.dhis.program.ProgramInstanceService)2 ProgramStageInstanceService (org.hisp.dhis.program.ProgramStageInstanceService)2 RelationshipService (org.hisp.dhis.relationship.RelationshipService)2 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)2 TrackedEntityInstanceService (org.hisp.dhis.trackedentity.TrackedEntityInstanceService)2 TrackerType (org.hisp.dhis.tracker.TrackerType)2 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)2 EnrollmentTrackerConverterService (org.hisp.dhis.tracker.converter.EnrollmentTrackerConverterService)2 EventTrackerConverterService (org.hisp.dhis.tracker.converter.EventTrackerConverterService)2