Search in sources :

Example 1 with SystemInstance

use of org.hisp.dhis.dxf2.synch.SystemInstance in project dhis2-core by dhis2.

the class DefaultSynchronizationManager method executeDataPush.

/**
     * Executes a push of data values to the given remote instance.
     *
     * @param instance the remote system instance.
     * @return an ImportSummary.
     */
private ImportSummary executeDataPush(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 = getLastDataSynchSuccessFallback();
    final int lastUpdatedCount = dataValueService.getDataValueCountLastUpdatedAfter(lastSuccessTime, true);
    log.info("Values: " + lastUpdatedCount + " since last synch success: " + lastSuccessTime);
    if (lastUpdatedCount == 0) {
        log.debug("Skipping synch, no new or updated data values");
        return null;
    }
    log.info("Values: " + lastUpdatedCount + " since last synch 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.writeDataValueSetJson(lastSuccessTime, request.getBody(), new IdSchemes());
    };
    ResponseExtractor<ImportSummary> responseExtractor = new ImportSummaryResponseExtractor();
    ImportSummary summary = null;
    try {
        summary = restTemplate.execute(instance.getUrl(), HttpMethod.POST, requestCallback, responseExtractor);
    } catch (HttpClientErrorException ex) {
        String responseBody = ex.getResponseBodyAsString();
        summary = WebMessageParseUtils.fromWebMessageResponse(responseBody, ImportSummary.class);
    } catch (HttpServerErrorException ex) {
        String responseBody = ex.getResponseBodyAsString();
        log.error("Internal error happened during event data push: " + responseBody, ex);
        throw ex;
    } catch (ResourceAccessException ex) {
        log.error("Exception during event data push: " + ex.getMessage(), ex);
        throw ex;
    }
    log.info("Synch summary: " + summary);
    if (summary != null && ImportStatus.SUCCESS.equals(summary.getStatus())) {
        setLastDataSynchSuccess(startTime);
        log.info("Synch successful, setting last success time: " + startTime);
    } else {
        log.warn("Sync failed: " + summary);
    }
    return summary;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) WebMessageParseException(org.hisp.dhis.dxf2.webmessage.WebMessageParseException) EventService(org.hisp.dhis.dxf2.events.event.EventService) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Date(java.util.Date) RenderService(org.hisp.dhis.render.RenderService) Autowired(org.springframework.beans.factory.annotation.Autowired) Configuration(org.hisp.dhis.configuration.Configuration) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) ImportSummaryResponseExtractor(org.hisp.dhis.dxf2.common.ImportSummaryResponseExtractor) DataValueService(org.hisp.dhis.datavalue.DataValueService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) MetadataImportService(org.hisp.dhis.dxf2.metadata.MetadataImportService) User(org.hisp.dhis.user.User) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) WebMessageParseUtils(org.hisp.dhis.dxf2.webmessage.utils.WebMessageParseUtils) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) RestTemplate(org.springframework.web.client.RestTemplate) DefaultRenderService(org.hisp.dhis.render.DefaultRenderService) ResponseExtractor(org.springframework.web.client.ResponseExtractor) HttpHeaders(org.springframework.http.HttpHeaders) IdSchemes(org.hisp.dhis.common.IdSchemes) DataValueSetService(org.hisp.dhis.dxf2.datavalueset.DataValueSetService) MediaType(org.springframework.http.MediaType) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) DateTime(org.joda.time.DateTime) HttpMethod(org.springframework.http.HttpMethod) SchemaService(org.hisp.dhis.schema.SchemaService) ResourceAccessException(org.springframework.web.client.ResourceAccessException) IOException(java.io.IOException) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) RequestCallback(org.springframework.web.client.RequestCallback) CodecUtils(org.hisp.dhis.system.util.CodecUtils) HttpStatus(org.springframework.http.HttpStatus) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ImportSummariesResponseExtractor(org.hisp.dhis.dxf2.common.ImportSummariesResponseExtractor) HttpEntity(org.springframework.http.HttpEntity) Events(org.hisp.dhis.dxf2.events.event.Events) CurrentUserService(org.hisp.dhis.user.CurrentUserService) ConfigurationService(org.hisp.dhis.configuration.ConfigurationService) Log(org.apache.commons.logging.Log) ResponseEntity(org.springframework.http.ResponseEntity) LogFactory(org.apache.commons.logging.LogFactory) SettingKey(org.hisp.dhis.setting.SettingKey) ImportSummaryResponseExtractor(org.hisp.dhis.dxf2.common.ImportSummaryResponseExtractor) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) RequestCallback(org.springframework.web.client.RequestCallback) IdSchemes(org.hisp.dhis.common.IdSchemes) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) Date(java.util.Date) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

Example 2 with SystemInstance

use of org.hisp.dhis.dxf2.synch.SystemInstance in project dhis2-core by dhis2.

the class SyncUtils method getRemoteInstanceWithSyncImportStrategy.

static SystemInstance getRemoteInstanceWithSyncImportStrategy(SystemSettingManager systemSettingManager, SyncEndpoint syncEndpoint) {
    SystemInstance systemInstance = getRemoteInstance(systemSettingManager, syncEndpoint);
    systemInstance.setUrl(systemInstance.getUrl() + SyncUtils.IMPORT_STRATEGY_SYNC_SUFFIX);
    return systemInstance;
}
Also used : SystemInstance(org.hisp.dhis.dxf2.synch.SystemInstance)

Example 3 with SystemInstance

use of org.hisp.dhis.dxf2.synch.SystemInstance 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)

Example 4 with SystemInstance

use of org.hisp.dhis.dxf2.synch.SystemInstance in project dhis2-core by dhis2.

the class SyncUtilsTest method getRemoteInstanceWithSyncImportStrategyTest.

@Test
void getRemoteInstanceWithSyncImportStrategyTest() {
    systemSettingManager.saveSystemSetting(SettingKey.REMOTE_INSTANCE_URL, URL);
    SystemInstance systemInstance = SyncUtils.getRemoteInstanceWithSyncImportStrategy(systemSettingManager, SyncEndpoint.EVENTS);
    assertThat(systemInstance.getUrl(), is(EVENTS_URL_WITH_SYNC_STRATEGY));
}
Also used : SystemInstance(org.hisp.dhis.dxf2.synch.SystemInstance) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 5 with SystemInstance

use of org.hisp.dhis.dxf2.synch.SystemInstance in project dhis2-core by dhis2.

the class SyncUtilsTest method getRemoteInstanceTest.

@Test
void getRemoteInstanceTest() {
    systemSettingManager.saveSystemSetting(SettingKey.REMOTE_INSTANCE_USERNAME, USERNAME);
    systemSettingManager.saveSystemSetting(SettingKey.REMOTE_INSTANCE_PASSWORD, PASSWORD);
    systemSettingManager.saveSystemSetting(SettingKey.REMOTE_INSTANCE_URL, URL);
    SystemInstance systemInstance = SyncUtils.getRemoteInstance(systemSettingManager, SyncEndpoint.EVENTS);
    assertThat(systemInstance.getUsername(), is(USERNAME));
    assertThat(systemInstance.getPassword(), is(PASSWORD));
    assertThat(systemInstance.getUrl(), is(EVENTS_URL));
}
Also used : SystemInstance(org.hisp.dhis.dxf2.synch.SystemInstance) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

SystemInstance (org.hisp.dhis.dxf2.synch.SystemInstance)3 IOException (java.io.IOException)2 Date (java.util.Date)2 DhisSpringTest (org.hisp.dhis.DhisSpringTest)2 IdSchemes (org.hisp.dhis.common.IdSchemes)2 DataValueService (org.hisp.dhis.datavalue.DataValueService)2 DataValueSetService (org.hisp.dhis.dxf2.datavalueset.DataValueSetService)2 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)2 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)2 AtomicMode (org.hisp.dhis.dxf2.metadata.AtomicMode)2 Metadata (org.hisp.dhis.dxf2.metadata.Metadata)2 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)2 MetadataImportService (org.hisp.dhis.dxf2.metadata.MetadataImportService)2 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)2 WebMessageParseException (org.hisp.dhis.dxf2.webmessage.WebMessageParseException)2 SchemaService (org.hisp.dhis.schema.SchemaService)2 SettingKey (org.hisp.dhis.setting.SettingKey)2 SystemSettingManager (org.hisp.dhis.setting.SystemSettingManager)2 CodecUtils (org.hisp.dhis.system.util.CodecUtils)2 CurrentUserService (org.hisp.dhis.user.CurrentUserService)2