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