Search in sources :

Example 1 with ImportSummariesResponseExtractor

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

Aggregations

Date (java.util.Date)1 ImportSummariesResponseExtractor (org.hisp.dhis.dxf2.common.ImportSummariesResponseExtractor)1 Events (org.hisp.dhis.dxf2.events.event.Events)1 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)1 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)1 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1 RequestCallback (org.springframework.web.client.RequestCallback)1 ResourceAccessException (org.springframework.web.client.ResourceAccessException)1