Search in sources :

Example 1 with TrackerSideEffectDataBundle

use of org.hisp.dhis.tracker.job.TrackerSideEffectDataBundle in project dhis2-core by dhis2.

the class DefaultTrackerImportService method commitBundle.

protected TrackerBundleReport commitBundle(TrackerBundle trackerBundle) {
    TrackerBundleReport bundleReport = trackerBundleService.commit(trackerBundle);
    if (!trackerBundle.isSkipSideEffects()) {
        List<TrackerSideEffectDataBundle> sideEffectDataBundles = Stream.of(TrackerType.ENROLLMENT, TrackerType.EVENT).map(trackerType -> safelyGetSideEffectsDataBundles(bundleReport, trackerType)).flatMap(Collection::stream).collect(Collectors.toList());
        trackerBundleService.handleTrackerSideEffects(sideEffectDataBundles);
    }
    return bundleReport;
}
Also used : TrackerSideEffectDataBundle(org.hisp.dhis.tracker.job.TrackerSideEffectDataBundle) TrackerBundleReport(org.hisp.dhis.tracker.report.TrackerBundleReport)

Example 2 with TrackerSideEffectDataBundle

use of org.hisp.dhis.tracker.job.TrackerSideEffectDataBundle 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

TrackerSideEffectDataBundle (org.hisp.dhis.tracker.job.TrackerSideEffectDataBundle)2 ArrayList (java.util.ArrayList)1 TrackerBundleReport (org.hisp.dhis.tracker.report.TrackerBundleReport)1 TrackerObjectReport (org.hisp.dhis.tracker.report.TrackerObjectReport)1 TrackerTypeReport (org.hisp.dhis.tracker.report.TrackerTypeReport)1