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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations