Search in sources :

Example 1 with AvailabilityStatus

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

the class DefaultSynchronizationManager method executeEventPush.

@Override
public ImportSummaries executeEventPush() throws WebMessageParseException {
    AvailabilityStatus availability = isRemoteServerAvailable();
    if (!availability.isAvailable()) {
        log.info("Aborting synch, server not available");
        return null;
    }
    // ---------------------------------------------------------------------
    // 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 = getLastEventSynchSuccessFallback();
    int lastUpdatedEventsCount = eventService.getAnonymousEventValuesCountLastUpdatedAfter(lastSuccessTime);
    log.info("Events: " + lastUpdatedEventsCount + " since last synch success: " + lastSuccessTime);
    if (lastUpdatedEventsCount == 0) {
        log.info("Skipping synch, no new or updated data values for events");
        return null;
    }
    String url = systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_URL) + "/api/events";
    log.info("Remote server events POST URL: " + url);
    final String username = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_USERNAME);
    final String password = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_PASSWORD);
    final RequestCallback requestCallback = new RequestCallback() {

        @Override
        public void doWithRequest(ClientHttpRequest request) throws IOException {
            request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
            request.getHeaders().add(HEADER_AUTHORIZATION, CodecUtils.getBasicAuthString(username, password));
            Events result = eventService.getAnonymousEventValuesLastUpdatedAfter(lastSuccessTime);
            renderService.toJson(request.getBody(), result);
        }
    };
    ResponseExtractor<ImportSummaries> responseExtractor = new ImportSummariesResponseExtractor();
    ImportSummaries summaries = null;
    try {
        summaries = restTemplate.execute(url, HttpMethod.POST, requestCallback, responseExtractor);
    } catch (HttpClientErrorException ex) {
        String responseBody = ex.getResponseBodyAsString();
        summaries = WebMessageParseUtils.fromWebMessageResponse(responseBody, ImportSummaries.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("Event synch summary: " + summaries);
    boolean isError = false;
    if (summaries != null) {
        for (ImportSummary summary : summaries.getImportSummaries()) {
            if (ImportStatus.ERROR.equals(summary.getStatus()) || ImportStatus.WARNING.equals(summary.getStatus())) {
                isError = true;
                log.debug("Sync failed: " + summaries);
                break;
            }
        }
    }
    if (!isError) {
        setLastEventSynchSuccess(startTime);
        log.info("Synch successful, setting last success time: " + startTime);
    }
    return summaries;
}
Also used : ImportSummariesResponseExtractor(org.hisp.dhis.dxf2.common.ImportSummariesResponseExtractor) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) Date(java.util.Date) ResourceAccessException(org.springframework.web.client.ResourceAccessException) RequestCallback(org.springframework.web.client.RequestCallback) Events(org.hisp.dhis.dxf2.events.event.Events) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 2 with AvailabilityStatus

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

the class MetadataSyncPreProcessor method handleEventDataPush.

public ImportSummaries handleEventDataPush(MetadataRetryContext context) {
    log.debug("Entering event data push");
    ImportSummaries importSummary = null;
    AvailabilityStatus remoteServerAvailable = synchronizationManager.isRemoteServerAvailable();
    if (!remoteServerAvailable.isAvailable()) {
        String message = remoteServerAvailable.getMessage();
        log.error(message);
        context.updateRetryContext(MetadataSyncTask.EVENT_PUSH_SUMMARY, remoteServerAvailable.getMessage(), null, null);
        throw new MetadataSyncServiceException(message);
    }
    try {
        importSummary = synchronizationManager.executeEventPush();
        handleEventImportSummary(importSummary, context);
    } catch (Exception ex) {
        log.error("Exception happened while trying to do event data push " + ex.getMessage(), ex);
        if (ex instanceof MetadataSyncServiceException) {
            throw (MetadataSyncServiceException) ex;
        }
        context.updateRetryContext(MetadataSyncTask.EVENT_PUSH_SUMMARY, ex.getMessage(), null, null);
        throw new MetadataSyncServiceException(ex.getMessage(), ex);
    }
    log.debug("Exiting event data push");
    return importSummary;
}
Also used : MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)

Example 3 with AvailabilityStatus

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

the class MetadataSyncPreProcessorTest method testHandleEventDataPushShouldNotThrowExceptionWhenDataPushIsSuccessful.

@Test
public void testHandleEventDataPushShouldNotThrowExceptionWhenDataPushIsSuccessful() throws Exception {
    MetadataRetryContext mockRetryContext = mock(MetadataRetryContext.class);
    ImportSummaries expectedSummary = new ImportSummaries();
    ImportSummary summary = new ImportSummary();
    summary.setStatus(ImportStatus.SUCCESS);
    expectedSummary.addImportSummary(summary);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    when(metadataSyncPreProcessor.handleEventDataPush(mockRetryContext)).thenReturn(expectedSummary);
    ImportSummaries actualSummary = metadataSyncPreProcessor.handleEventDataPush(mockRetryContext);
    assertEquals(expectedSummary.getImportSummaries().get(0).getStatus(), actualSummary.getImportSummaries().get(0).getStatus());
}
Also used : AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) MetadataRetryContext(org.hisp.dhis.dxf2.metadata.tasks.MetadataRetryContext) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 4 with AvailabilityStatus

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

the class MetadataSyncPreProcessorTest method testHandleEventDataPushShouldCallEventDataPush.

@Test
public void testHandleEventDataPushShouldCallEventDataPush() throws Exception {
    MetadataRetryContext mockRetryContext = mock(MetadataRetryContext.class);
    AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
    when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
    metadataSyncPreProcessor.handleEventDataPush(mockRetryContext);
    verify(synchronizationManager, times(1)).executeEventPush();
}
Also used : AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) MetadataRetryContext(org.hisp.dhis.dxf2.metadata.tasks.MetadataRetryContext) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 5 with AvailabilityStatus

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

the class MetadataVersionDelegate method getDhisHttpResponse.

// ----------------------------------------------------------------------------------------
// Private Methods
// ----------------------------------------------------------------------------------------
private DhisHttpResponse getDhisHttpResponse(String url, int timeout) {
    AvailabilityStatus remoteServerAvailable = synchronizationManager.isRemoteServerAvailable();
    if (!(remoteServerAvailable.isAvailable())) {
        String message = remoteServerAvailable.getMessage();
        log.error(message);
        throw new RemoteServerUnavailableException(message);
    }
    String username = metadataSystemSettingService.getRemoteInstanceUserName();
    String password = metadataSystemSettingService.getRemoteInstancePassword();
    log.info("Remote server metadata version  URL: " + url + ", username: " + username);
    DhisHttpResponse dhisHttpResponse;
    try {
        dhisHttpResponse = HttpUtils.httpGET(url, true, username, password, null, timeout, true);
    } catch (Exception e) {
        String message = "Exception occurred while trying to make the GET call to URL: " + url;
        log.error(message, e);
        throw new MetadataVersionServiceException(message, e);
    }
    return dhisHttpResponse;
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) RemoteServerUnavailableException(org.hisp.dhis.dxf2.metadata.sync.exception.RemoteServerUnavailableException) RemoteServerUnavailableException(org.hisp.dhis.dxf2.metadata.sync.exception.RemoteServerUnavailableException) IOException(java.io.IOException) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)

Aggregations

AvailabilityStatus (org.hisp.dhis.dxf2.synch.AvailabilityStatus)28 Test (org.junit.jupiter.api.Test)19 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)14 HttpUtils (org.hisp.dhis.system.util.HttpUtils)12 DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)9 IOException (java.io.IOException)6 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)6 MetadataRetryContext (org.hisp.dhis.dxf2.metadata.jobs.MetadataRetryContext)6 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)5 Date (java.util.Date)4 DhisSpringTest (org.hisp.dhis.DhisSpringTest)4 IntegrationTest (org.hisp.dhis.IntegrationTest)4 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 HttpResponse (org.apache.http.HttpResponse)3 MetadataSyncServiceException (org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException)3 MetadataRetryContext (org.hisp.dhis.dxf2.metadata.tasks.MetadataRetryContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 RemoteServerUnavailableException (org.hisp.dhis.dxf2.metadata.sync.exception.RemoteServerUnavailableException)2