Search in sources :

Example 36 with WorkContext

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();
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Pair(org.apache.commons.lang3.tuple.Pair) Transactional(org.springframework.transaction.annotation.Transactional)

Example 37 with WorkContext

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);
    }
}
Also used : EventImporterUserService(org.hisp.dhis.dxf2.events.importer.EventImporterUserService) DataValue(org.hisp.dhis.dxf2.events.event.DataValue) ServiceDelegator(org.hisp.dhis.dxf2.events.importer.ServiceDelegator) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) Set(java.util.Set) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Collectors(java.util.stream.Collectors) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Event(org.hisp.dhis.dxf2.events.event.Event) Processor(org.hisp.dhis.dxf2.events.importer.Processor) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) Optional(java.util.Optional) User(org.hisp.dhis.user.User) Collections(java.util.Collections) User(org.hisp.dhis.user.User) UserInfoSnapshot(org.hisp.dhis.program.UserInfoSnapshot) Event(org.hisp.dhis.dxf2.events.event.Event) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue)

Example 38 with WorkContext

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");
        }
    }
}
Also used : ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataValue(org.hisp.dhis.dxf2.events.event.DataValue) Authorities(org.hisp.dhis.security.Authorities) ValueType(org.hisp.dhis.common.ValueType) ValidationUtils(org.hisp.dhis.system.util.ValidationUtils) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) IdentifiableObjectUtils.getIdentifierBasedOnIdScheme(org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifierBasedOnIdScheme) StringUtils(org.apache.commons.lang3.StringUtils) DataElement(org.hisp.dhis.dataelement.DataElement) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) SQLException(java.sql.SQLException) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) Map(java.util.Map) Checker(org.hisp.dhis.dxf2.events.importer.Checker) ImageIO(javax.imageio.ImageIO) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) User(org.hisp.dhis.user.User) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) EventUtils.eventDataValuesToJson(org.hisp.dhis.dxf2.events.event.EventUtils.eventDataValuesToJson) ImmutableSet(com.google.common.collect.ImmutableSet) FileResource(org.hisp.dhis.fileresource.FileResource) Set(java.util.Set) EventStatus(org.hisp.dhis.event.EventStatus) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) ProgramStage(org.hisp.dhis.program.ProgramStage) Component(org.springframework.stereotype.Component) OptionSet(org.hisp.dhis.option.OptionSet) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent) Optional(java.util.Optional) ValidationStrategy(org.hisp.dhis.program.ValidationStrategy) Collections(java.util.Collections) IdScheme(org.hisp.dhis.common.IdScheme) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) OptionSet(org.hisp.dhis.option.OptionSet) IdentifiableObjectUtils.getIdentifierBasedOnIdScheme(org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifierBasedOnIdScheme) IdScheme(org.hisp.dhis.common.IdScheme) ProgramStage(org.hisp.dhis.program.ProgramStage) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 39 with WorkContext

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());
}
Also used : EventImporterUserService(org.hisp.dhis.dxf2.events.importer.EventImporterUserService) AuditableEntity(org.hisp.dhis.artemis.audit.AuditableEntity) AuditManager(org.hisp.dhis.artemis.audit.AuditManager) ProgramStageInstanceMapper(org.hisp.dhis.dxf2.events.importer.mapper.ProgramStageInstanceMapper) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 40 with WorkContext

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);
        }
    }
}
Also used : ProgramStageInstanceMapper(org.hisp.dhis.dxf2.events.importer.mapper.ProgramStageInstanceMapper) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)47 ImmutableEvent (org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent)35 Test (org.junit.jupiter.api.Test)35 BaseValidationTest (org.hisp.dhis.dxf2.events.importer.validation.BaseValidationTest)26 Program (org.hisp.dhis.program.Program)23 HashMap (java.util.HashMap)20 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)16 DhisConvenienceTest.createProgram (org.hisp.dhis.DhisConvenienceTest.createProgram)14 ProgramInstance (org.hisp.dhis.program.ProgramInstance)14 WorkContext (org.hisp.dhis.dxf2.events.importer.context.WorkContext)13 Event (org.hisp.dhis.dxf2.events.event.Event)12 ProgramStage (org.hisp.dhis.program.ProgramStage)11 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)9 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)9 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)9 Set (java.util.Set)7 DhisConvenienceTest.createTrackedEntityInstance (org.hisp.dhis.DhisConvenienceTest.createTrackedEntityInstance)7 IdScheme (org.hisp.dhis.common.IdScheme)6 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)6 User (org.hisp.dhis.user.User)5