Search in sources :

Example 1 with TrackerTypeReport

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

the class DefaultTrackerBundleService method commit.

@Override
@Transactional
public TrackerBundleReport commit(TrackerBundle bundle) {
    TrackerBundleReport bundleReport = new TrackerBundleReport();
    if (TrackerBundleMode.VALIDATE == bundle.getImportMode()) {
        return bundleReport;
    }
    Session session = sessionFactory.getCurrentSession();
    Map<TrackerType, TrackerTypeReport> report = bundleReport.getTypeReportMap();
    report.put(TrackerType.TRACKED_ENTITY, commitService.getTrackerPersister().persist(session, bundle));
    report.put(TrackerType.ENROLLMENT, commitService.getEnrollmentPersister().persist(session, bundle));
    report.put(TrackerType.EVENT, commitService.getEventPersister().persist(session, bundle));
    report.put(TrackerType.RELATIONSHIP, commitService.getRelationshipPersister().persist(session, bundle));
    return bundleReport;
}
Also used : TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundleReport(org.hisp.dhis.tracker.report.TrackerBundleReport) Session(org.hibernate.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with TrackerTypeReport

use of org.hisp.dhis.tracker.report.TrackerTypeReport 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 3 with TrackerTypeReport

use of org.hisp.dhis.tracker.report.TrackerTypeReport 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 4 with TrackerTypeReport

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

the class DefaultTrackerBundleService method delete.

@Override
@Transactional
public TrackerBundleReport delete(TrackerBundle bundle) {
    TrackerBundleReport bundleReport = new TrackerBundleReport();
    if (TrackerBundleMode.VALIDATE == bundle.getImportMode()) {
        return bundleReport;
    }
    Map<TrackerType, TrackerTypeReport> report = bundleReport.getTypeReportMap();
    report.put(TrackerType.RELATIONSHIP, deletionService.deleteRelationShips(bundle));
    report.put(TrackerType.EVENT, deletionService.deleteEvents(bundle));
    report.put(TrackerType.ENROLLMENT, deletionService.deleteEnrollments(bundle));
    report.put(TrackerType.TRACKED_ENTITY, deletionService.deleteTrackedEntityInstances(bundle));
    return bundleReport;
}
Also used : TrackerTypeReport(org.hisp.dhis.tracker.report.TrackerTypeReport) TrackerType(org.hisp.dhis.tracker.TrackerType) TrackerBundleReport(org.hisp.dhis.tracker.report.TrackerBundleReport) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with TrackerTypeReport

use of org.hisp.dhis.tracker.report.TrackerTypeReport 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)

Aggregations

TrackerTypeReport (org.hisp.dhis.tracker.report.TrackerTypeReport)7 TrackerObjectReport (org.hisp.dhis.tracker.report.TrackerObjectReport)5 TrackerType (org.hisp.dhis.tracker.TrackerType)4 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 TrackerBundle (org.hisp.dhis.tracker.bundle.TrackerBundle)2 EnrollmentTrackerConverterService (org.hisp.dhis.tracker.converter.EnrollmentTrackerConverterService)2 EventTrackerConverterService (org.hisp.dhis.tracker.converter.EventTrackerConverterService)2