Search in sources :

Example 1 with Event

use of org.hisp.dhis.dxf2.events.event.Event 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 Event

use of org.hisp.dhis.dxf2.events.event.Event 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 3 with Event

use of org.hisp.dhis.dxf2.events.event.Event 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 4 with Event

use of org.hisp.dhis.dxf2.events.event.Event in project dhis2-core by dhis2.

the class RegistrationSingleEventServiceTest method createEvent.

private Event createEvent(String program, String programStage, String orgUnit, String person) {
    Event event = new Event();
    event.setProgram(program);
    event.setProgramStage(programStage);
    event.setOrgUnit(orgUnit);
    event.setTrackedEntityInstance(person);
    event.setEventDate("2013-01-01");
    event.getDataValues().add(new DataValue(dataElementA.getUid(), "10"));
    return event;
}
Also used : DataValue(org.hisp.dhis.dxf2.events.event.DataValue) Event(org.hisp.dhis.dxf2.events.event.Event)

Example 5 with Event

use of org.hisp.dhis.dxf2.events.event.Event in project dhis2-core by dhis2.

the class NoRegistrationSingleEventServiceTest method testUpdateEvent.

@Test
public void testUpdateEvent() {
    Event event = createEvent(programA.getUid(), organisationUnitA.getUid());
    ImportSummary importSummary = eventService.addEvent(event, null);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    assertNotNull(importSummary.getReference());
    assertEquals("10", event.getDataValues().get(0).getValue());
    event = eventService.getEvent(importSummary.getReference());
    event.getDataValues().get(0).setValue("254");
    eventService.updateEvent(event, false);
    event = eventService.getEvent(importSummary.getReference());
    assertEquals("254", event.getDataValues().get(0).getValue());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Event(org.hisp.dhis.dxf2.events.event.Event) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

Event (org.hisp.dhis.dxf2.events.event.Event)30 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)24 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)15 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)15 DhisSpringTest (org.hisp.dhis.DhisSpringTest)12 Test (org.junit.Test)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 InputStream (java.io.InputStream)9 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)9 EventSearchParams (org.hisp.dhis.dxf2.events.event.EventSearchParams)9 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)9 Date (java.util.Date)8 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)7 Events (org.hisp.dhis.dxf2.events.event.Events)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 IOException (java.io.IOException)5 DataElement (org.hisp.dhis.dataelement.DataElement)5 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)5 Program (org.hisp.dhis.program.Program)5 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)5