Search in sources :

Example 1 with SUCCESS

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

the class MetaDataImportAction method execute.

// -------------------------------------------------------------------------
// Action Implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    strategy = strategy != null ? strategy : ImportStrategy.NEW_AND_UPDATES;
    User user = currentUserService.getCurrentUser();
    TaskId taskId = new TaskId(TaskCategory.METADATA_IMPORT, user);
    notifier.clear(taskId);
    InputStream in = StreamUtils.wrapAndCheckCompressionFormat(new FileInputStream(upload));
    MetadataImportParams importParams = createMetadataImportParams(taskId, strategy, atomicMode, dryRun).setFilename(uploadFileName);
    if ("csv".equals(importFormat)) {
        if (classKey != null && CSV_SUPPORTED_CLASSES.containsKey(classKey)) {
            scheduler.executeTask(new ImportMetaDataCsvTask(importService, csvImportService, schemaService, importParams, in, CSV_SUPPORTED_CLASSES.get(classKey)));
        }
    } else if ("gml".equals(importFormat)) {
        scheduler.executeTask(new ImportMetaDataGmlTask(gmlImportService, importParams, in));
    } else if ("json".equals(importFormat) || "xml".equals(importFormat)) {
        scheduler.executeTask(new ImportMetaDataTask(importService, schemaService, importParams, in, importFormat));
    }
    return SUCCESS;
}
Also used : User(org.hisp.dhis.user.User) TaskId(org.hisp.dhis.scheduling.TaskId) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ImportMetaDataTask(org.hisp.dhis.importexport.action.util.ImportMetaDataTask) ImportMetaDataCsvTask(org.hisp.dhis.importexport.action.util.ImportMetaDataCsvTask) FileInputStream(java.io.FileInputStream) ImportMetaDataGmlTask(org.hisp.dhis.importexport.action.util.ImportMetaDataGmlTask)

Example 2 with SUCCESS

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.SUCCESS 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 3 with SUCCESS

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.SUCCESS 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 4 with SUCCESS

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

the class MetadataSyncPostProcessor method sendSuccessMailToAdmin.

public void sendSuccessMailToAdmin(MetadataSyncSummary metadataSyncSummary) {
    ImportReport importReport = metadataSyncSummary.getImportReport();
    StringBuilder text = new StringBuilder("Successful Import Report for the scheduler run for Metadata synchronization \n\n").append("Imported Version Details \n ").append("Version Name: " + metadataSyncSummary.getMetadataVersion().getName() + "\n").append("Version Type: " + metadataSyncSummary.getMetadataVersion().getType() + "\n");
    if (importReport.getTypeReportCount() == 0) {
        text.append("New Version created. It does not have any metadata changes. \n");
    } else {
        text.append("Imported Object Details: \n");
        importReport.forEachTypeReport(typeReport -> {
            Stats stats = typeReport.getStats();
            text.append("Metadata Object Type: ").append(typeReport.getKlass()).append("\n").append("Stats: \n").append("total: " + stats.getTotal() + "\n");
            if (stats.getCreated() > 0) {
                text.append(" created: " + stats.getCreated() + "\n");
            }
            if (stats.getUpdated() > 0) {
                text.append(" updated: " + stats.getUpdated() + "\n");
            }
            if (stats.getIgnored() > 0) {
                text.append(" ignored: " + stats.getIgnored() + "\n");
            }
        });
        text.append("\n\n");
    }
    if (text.length() > 0) {
        log.info("Success mail will be sent with the following message: " + text);
        emailService.sendSystemEmail(new Email("Success Notification: Metadata Synchronization", text.toString()));
    }
}
Also used : Email(org.hisp.dhis.email.Email) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Stats(org.hisp.dhis.feedback.Stats)

Example 5 with SUCCESS

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

the class EventManager method incrementSummaryTotals.

private void incrementSummaryTotals(final List<Event> events, final ImportSummaries importSummaries, final ImportStrategy importStrategy) {
    for (final Event event : events) {
        if (!importSummaries.getByReference(event.getUid()).isPresent()) {
            final ImportSummary is = new ImportSummary();
            is.setReference(event.getUid());
            is.setStatus(SUCCESS);
            switch(importStrategy) {
                case CREATE:
                    is.incrementImported();
                    break;
                case UPDATE:
                    is.incrementUpdated();
                    break;
                case DELETE:
                    is.incrementDeleted();
                    is.setDescription("Deletion of event " + event.getUid() + " was successful");
                    break;
                default:
                    log.warn("Invalid Import Strategy during summary increment, ignoring");
            }
            importSummaries.addImportSummary(is);
        }
    }
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Event(org.hisp.dhis.dxf2.events.event.Event) ImmutableEvent(org.hisp.dhis.dxf2.events.importer.shared.ImmutableEvent)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)20 User (org.hisp.dhis.user.User)10 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)9 Test (org.junit.jupiter.api.Test)9 Event (org.hisp.dhis.dxf2.events.event.Event)6 Date (java.util.Date)4 Program (org.hisp.dhis.program.Program)4 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 DataValueService (org.hisp.dhis.datavalue.DataValueService)3 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 Enrollment (org.hisp.dhis.dxf2.events.enrollment.Enrollment)3 Events (org.hisp.dhis.dxf2.events.event.Events)3 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)3 IOException (java.io.IOException)2 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)2 IdScheme (org.hisp.dhis.common.IdScheme)2 IdSchemes (org.hisp.dhis.common.IdSchemes)2 ImportSummariesResponseExtractor (org.hisp.dhis.dxf2.common.ImportSummariesResponseExtractor)2 DataValueSetService (org.hisp.dhis.dxf2.datavalueset.DataValueSetService)2