Search in sources :

Example 1 with ImportSummaries

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

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

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

the class AbstractTrackedEntityInstanceService method deleteTrackedEntityInstances.

@Override
public ImportSummaries deleteTrackedEntityInstances(List<String> uids) {
    ImportSummaries importSummaries = new ImportSummaries();
    int counter = 0;
    for (String uid : uids) {
        importSummaries.addImportSummary(deleteTrackedEntityInstance(uid));
        if (counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Example 4 with ImportSummaries

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

the class AbstractTrackedEntityInstanceService method addTrackedEntityInstances.

// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries addTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummaries importSummaries = new ImportSummaries();
    int counter = 0;
    for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
        importSummaries.addImportSummary(addTrackedEntityInstance(trackedEntityInstance, importOptions));
        if (counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 5 with ImportSummaries

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

the class JacksonTrackedEntityInstanceService method addTrackedEntityInstanceList.

private ImportSummaries addTrackedEntityInstanceList(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
    ImportSummaries importSummaries = new ImportSummaries();
    List<TrackedEntityInstance> create = new ArrayList<>();
    List<TrackedEntityInstance> update = new ArrayList<>();
    List<String> delete = new ArrayList<>();
    if (importOptions.getImportStrategy().isCreate()) {
        create.addAll(trackedEntityInstances);
    } else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
        for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
            if (StringUtils.isEmpty(trackedEntityInstance.getTrackedEntityInstance())) {
                create.add(trackedEntityInstance);
            } else {
                if (!teiService.trackedEntityInstanceExists(trackedEntityInstance.getTrackedEntityInstance())) {
                    create.add(trackedEntityInstance);
                } else {
                    update.add(trackedEntityInstance);
                }
            }
        }
    } else if (importOptions.getImportStrategy().isUpdate()) {
        update.addAll(trackedEntityInstances);
    } else if (importOptions.getImportStrategy().isDelete()) {
        delete.addAll(trackedEntityInstances.stream().map(TrackedEntityInstance::getTrackedEntityInstance).collect(Collectors.toList()));
    }
    importSummaries.addImportSummaries(addTrackedEntityInstances(create, importOptions));
    importSummaries.addImportSummaries(updateTrackedEntityInstances(update, importOptions));
    importSummaries.addImportSummaries(deleteTrackedEntityInstances(delete));
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries)

Aggregations

ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)26 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)10 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)9 InputStream (java.io.InputStream)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 Date (java.util.Date)6 Lists (com.google.common.collect.Lists)5 IOException (java.io.IOException)5 List (java.util.List)5 Set (java.util.Set)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)5 OrganisationUnitSelectionMode (org.hisp.dhis.common.OrganisationUnitSelectionMode)5 StreamUtils (org.hisp.dhis.commons.util.StreamUtils)5 TextUtils (org.hisp.dhis.commons.util.TextUtils)5 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 StringUtils (org.apache.commons.lang3.StringUtils)4