Search in sources :

Example 16 with ImportConflicts

use of org.hisp.dhis.dxf2.importsummary.ImportConflicts 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 17 with ImportConflicts

use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.

the class DefaultSynchronizationManager method executeDataValuePush.

/**
 * Executes a push of data values to the given remote instance.
 *
 * @param instance the remote system instance.
 * @return an ImportSummary.
 */
private ImportConflicts executeDataValuePush(SystemInstance instance) throws WebMessageParseException {
    // ---------------------------------------------------------------------
    // Set time for last success to start of process to make data saved
    // subsequently part of next synch process without being ignored
    // ---------------------------------------------------------------------
    final Date startTime = new Date();
    final Date lastSuccessTime = SyncUtils.getLastSyncSuccess(systemSettingManager, SettingKey.LAST_SUCCESSFUL_DATA_VALUE_SYNC);
    final Date skipChangedBefore = systemSettingManager.getDateSetting(SettingKey.SKIP_SYNCHRONIZATION_FOR_DATA_CHANGED_BEFORE);
    final Date lastUpdatedAfter = lastSuccessTime.after(skipChangedBefore) ? lastSuccessTime : skipChangedBefore;
    final int objectsToSynchronize = dataValueService.getDataValueCountLastUpdatedAfter(lastUpdatedAfter, true);
    log.info("DataValues last changed before " + skipChangedBefore + " will not be synchronized.");
    if (objectsToSynchronize == 0) {
        SyncUtils.setLastSyncSuccess(systemSettingManager, SettingKey.LAST_SUCCESSFUL_DATA_VALUE_SYNC, startTime);
        log.debug("Skipping data values push, no new or updated data values");
        ImportCount importCount = new ImportCount(0, 0, 0, 0);
        return new ImportSummary(ImportStatus.SUCCESS, "No new or updated data values to push.", importCount);
    }
    log.info("Data Values: " + objectsToSynchronize + " to push since last synchronization success: " + lastSuccessTime);
    log.info("Remote server POST URL: " + instance.getUrl());
    final RequestCallback requestCallback = request -> {
        request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        request.getHeaders().add(HEADER_AUTHORIZATION, CodecUtils.getBasicAuthString(instance.getUsername(), instance.getPassword()));
        dataValueSetService.exportDataValueSetJson(lastUpdatedAfter, request.getBody(), new IdSchemes());
    };
    final int maxSyncAttempts = systemSettingManager.getIntSetting(SettingKey.MAX_SYNC_ATTEMPTS);
    Optional<AbstractWebMessageResponse> responseSummary = SyncUtils.runSyncRequest(restTemplate, requestCallback, SyncEndpoint.DATA_VALUE_SETS.getKlass(), instance.getUrl(), maxSyncAttempts);
    ImportSummary summary = null;
    if (responseSummary.isPresent()) {
        summary = (ImportSummary) responseSummary.get();
        if (ImportStatus.SUCCESS.equals(summary.getStatus())) {
            log.info("Push successful: " + summary);
        } else {
            log.warn("Push failed: " + summary);
        }
    }
    return summary;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) WebMessageParseException(org.hisp.dhis.dxf2.webmessage.WebMessageParseException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Date(java.util.Date) ImportCount(org.hisp.dhis.dxf2.importsummary.ImportCount) SyncEndpoint(org.hisp.dhis.dxf2.sync.SyncEndpoint) DataValueService(org.hisp.dhis.datavalue.DataValueService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) MetadataImportService(org.hisp.dhis.dxf2.metadata.MetadataImportService) AbstractWebMessageResponse(org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse) User(org.hisp.dhis.user.User) SyncUtils(org.hisp.dhis.dxf2.sync.SyncUtils) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) RestTemplate(org.springframework.web.client.RestTemplate) IdSchemes(org.hisp.dhis.common.IdSchemes) DataValueSetService(org.hisp.dhis.dxf2.datavalueset.DataValueSetService) MediaType(org.springframework.http.MediaType) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) SchemaService(org.hisp.dhis.schema.SchemaService) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) RequestCallback(org.springframework.web.client.RequestCallback) CodecUtils(org.hisp.dhis.system.util.CodecUtils) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Optional(java.util.Optional) SettingKey(org.hisp.dhis.setting.SettingKey) RequestCallback(org.springframework.web.client.RequestCallback) IdSchemes(org.hisp.dhis.common.IdSchemes) AbstractWebMessageResponse(org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse) ImportCount(org.hisp.dhis.dxf2.importsummary.ImportCount) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Date(java.util.Date) SyncEndpoint(org.hisp.dhis.dxf2.sync.SyncEndpoint)

Aggregations

ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)13 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)9 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)7 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)6 ImportConflicts (org.hisp.dhis.dxf2.importsummary.ImportConflicts)5 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)5 Program (org.hisp.dhis.program.Program)5 ProgramInstance (org.hisp.dhis.program.ProgramInstance)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Date (java.util.Date)4 Map (java.util.Map)4 Set (java.util.Set)4 IdSchemes (org.hisp.dhis.common.IdSchemes)4 Lists (com.google.common.collect.Lists)3 Collections (java.util.Collections)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3