use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.
the class WorkContextLoader method load.
@Transactional(readOnly = true)
public WorkContext load(ImportOptions importOptions, List<Event> events) {
sessionFactory.getCurrentSession().flush();
ImportOptions localImportOptions = importOptions;
// API allows a null Import Options
if (localImportOptions == null) {
localImportOptions = ImportOptions.getDefaultImportOptions();
}
initializeUser(localImportOptions);
// Make sure all events have the 'uid' field populated
events = uidGen.assignUidToEvents(events);
final Map<String, ProgramStageInstance> programStageInstanceMap = programStageInstanceSupplier.get(localImportOptions, events);
final Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap = trackedEntityInstanceSupplier.get(localImportOptions, events);
final Map<String, OrganisationUnit> orgUniMap = organisationUnitSupplier.get(localImportOptions, events);
return WorkContext.builder().importOptions(localImportOptions).programsMap(programSupplier.get(localImportOptions, events)).programStageInstanceMap(programStageInstanceMap).organisationUnitMap(orgUniMap).trackedEntityInstanceMap(teiMap).programInstanceMap(programInstanceSupplier.get(localImportOptions, teiMap, events)).categoryOptionComboMap(categoryOptionComboSupplier.get(localImportOptions, events)).dataElementMap(dataElementSupplier.get(localImportOptions, events)).notesMap(noteSupplier.get(localImportOptions, events)).assignedUserMap(assignedUserSupplier.get(localImportOptions, events)).eventDataValueMap(new EventDataValueAggregator().aggregateDataValues(events, programStageInstanceMap, localImportOptions)).programWithOrgUnitsMap(programOrgUnitSupplier.get(localImportOptions, events, orgUniMap)).serviceDelegator(serviceDelegatorSupplier.get()).build();
}
use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.
the class AbstractUserInfoPreProcessor method process.
@Override
public void process(Event event, WorkContext workContext) {
User user = findUserFromImportOptions(workContext.getImportOptions()).orElseGet(() -> getUser(workContext));
if (user != null) {
UserInfoSnapshot userInfo = UserInfoSnapshot.from(user);
updateEventUserInfo(event, userInfo);
Set<String> updatableDataValues = Optional.ofNullable(event).map(Event::getDataValues).orElse(Collections.emptySet()).stream().map(DataValue::getDataElement).collect(Collectors.toSet());
Set<EventDataValue> eventDataValuesToUpdate = getWorkContextDataValueMapEntry(workContext, event.getUid()).stream().filter(eventDataValue -> updatableDataValues.contains(eventDataValue.getDataElement())).collect(Collectors.toSet());
updateDataValuesUserInfo(getExistingPsi(workContext, event.getUid()), eventDataValuesToUpdate, userInfo);
}
}
use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.
the class DataValueCheck method validateMandatoryAttributes.
public void validateMandatoryAttributes(ImportConflicts importConflicts, WorkContext ctx, ImmutableEvent event) {
if (StringUtils.isEmpty(event.getProgramStage()))
return;
final IdScheme programStageIdScheme = ctx.getImportOptions().getIdSchemes().getProgramStageIdScheme();
final IdScheme dataElementIdScheme = ctx.getImportOptions().getIdSchemes().getDataElementIdScheme();
final Map<String, Set<EventDataValue>> eventDataValueMap = ctx.getEventDataValueMap();
final boolean allowSingleUpdates = ctx.getImportOptions().isMergeDataValues();
ProgramStage programStage = ctx.getProgramStage(programStageIdScheme, event.getProgramStage());
final Set<ProgramStageDataElement> mandatoryDataElements = getMandatoryProgramStageDataElements(programStage);
// Data Element IDs associated to the current event
Set<String> dataValues = eventDataValueMap.get(event.getUid()).stream().map(EventDataValue::getDataElement).collect(Collectors.toSet());
if (allowSingleUpdates) {
final ProgramStageInstance programStageInstance = ctx.getProgramStageInstanceMap().get(event.getUid());
dataValues.addAll(programStageInstance.getEventDataValues().stream().filter(dv -> !StringUtils.isEmpty(dv.getValue().trim())).map(EventDataValue::getDataElement).collect(Collectors.toSet()));
}
for (ProgramStageDataElement mandatoryDataElement : mandatoryDataElements) {
String resolvedDataElementId = getIdentifierBasedOnIdScheme(mandatoryDataElement.getDataElement(), dataElementIdScheme);
if (!dataValues.contains(resolvedDataElementId)) {
importConflicts.addConflict(resolvedDataElementId, "value_required_but_not_provided");
}
}
}
use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.
the class AbstractEventAuditPostProcessor method process.
@Override
public void process(final Event event, final WorkContext ctx) {
final AuditManager auditManager = ctx.getServiceDelegator().getAuditManager();
final EventImporterUserService eventImporterUserService = ctx.getServiceDelegator().getEventImporterUserService();
final ProgramStageInstanceMapper programStageInstanceMapper = new ProgramStageInstanceMapper(ctx);
final ProgramStageInstance programStageInstance = programStageInstanceMapper.map(event);
auditManager.send(Audit.builder().auditType(getAuditType()).auditScope(AuditScope.TRACKER).createdAt(LocalDateTime.now()).createdBy(eventImporterUserService.getAuditUsername()).object(programStageInstance).auditableEntity(new AuditableEntity(ProgramStageInstance.class, programStageInstance)).build());
}
use of org.hisp.dhis.dxf2.events.importer.context.WorkContext in project dhis2-core by dhis2.
the class DefaultEventPersistenceService method save.
@Override
@Transactional
public void save(WorkContext context, List<Event> events) {
if (isNotEmpty(events)) {
ProgramStageInstanceMapper mapper = new ProgramStageInstanceMapper(context);
List<ProgramStageInstance> programStageInstances = jdbcEventStore.saveEvents(events.stream().map(mapper::map).collect(Collectors.toList()));
jdbcEventCommentStore.saveAllComments(programStageInstances);
if (!context.getImportOptions().isSkipLastUpdated()) {
updateTeis(context, events);
}
}
}
Aggregations