Search in sources :

Example 21 with IdSchemes

use of org.hisp.dhis.common.IdSchemes in project dhis2-core by dhis2.

the class AdxDataServiceIntegrationTest method testGetFromUrl1.

// --------------------------------------------------------------------------
// Test get data export params from URL arguments
// --------------------------------------------------------------------------
@Test
void testGetFromUrl1() {
    Date now = new Date();
    DataExportParams expected = new DataExportParams().setDataSets(Sets.newHashSet(dsA)).setPeriods(Sets.newHashSet(pe202001)).setOrganisationUnits(Sets.newHashSet(ouA)).setIncludeDescendants(true).setIncludeDeleted(false).setLastUpdated(now).setLimit(999).setOutputIdSchemes(new IdSchemes().setIdScheme("CODE"));
    DataExportParams actual = adxDataService.getFromUrl(DataValueSetQueryParams.builder().dataSet(Sets.newHashSet(dsA.getUid())).period(Sets.newHashSet("202001")).orgUnit(Sets.newHashSet(ouA.getUid())).children(true).includeDeleted(false).lastUpdated(now).limit(999).build());
    assertEquals(expected.toString(), actual.toString());
}
Also used : IdSchemes(org.hisp.dhis.common.IdSchemes) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) Date(java.util.Date) DhisTest(org.hisp.dhis.DhisTest) Test(org.junit.jupiter.api.Test)

Example 22 with IdSchemes

use of org.hisp.dhis.common.IdSchemes in project dhis2-core by dhis2.

the class DefaultSynchronizationManager method executeDataValuePush.

/**
 * Executes a push of data values to the given remote instance.
 *
 * @param instance the remote system instance.
 * @return an ImportSummary.
 */
private ImportConflicts executeDataValuePush(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 = SyncUtils.getLastSyncSuccess(systemSettingManager, SettingKey.LAST_SUCCESSFUL_DATA_VALUE_SYNC);
    final Date skipChangedBefore = systemSettingManager.getDateSetting(SettingKey.SKIP_SYNCHRONIZATION_FOR_DATA_CHANGED_BEFORE);
    final Date lastUpdatedAfter = lastSuccessTime.after(skipChangedBefore) ? lastSuccessTime : skipChangedBefore;
    final int objectsToSynchronize = dataValueService.getDataValueCountLastUpdatedAfter(lastUpdatedAfter, true);
    log.info("DataValues last changed before " + skipChangedBefore + " will not be synchronized.");
    if (objectsToSynchronize == 0) {
        SyncUtils.setLastSyncSuccess(systemSettingManager, SettingKey.LAST_SUCCESSFUL_DATA_VALUE_SYNC, startTime);
        log.debug("Skipping data values push, no new or updated data values");
        ImportCount importCount = new ImportCount(0, 0, 0, 0);
        return new ImportSummary(ImportStatus.SUCCESS, "No new or updated data values to push.", importCount);
    }
    log.info("Data Values: " + objectsToSynchronize + " to push since last synchronization 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.exportDataValueSetJson(lastUpdatedAfter, request.getBody(), new IdSchemes());
    };
    final int maxSyncAttempts = systemSettingManager.getIntSetting(SettingKey.MAX_SYNC_ATTEMPTS);
    Optional<AbstractWebMessageResponse> responseSummary = SyncUtils.runSyncRequest(restTemplate, requestCallback, SyncEndpoint.DATA_VALUE_SETS.getKlass(), instance.getUrl(), maxSyncAttempts);
    ImportSummary summary = null;
    if (responseSummary.isPresent()) {
        summary = (ImportSummary) responseSummary.get();
        if (ImportStatus.SUCCESS.equals(summary.getStatus())) {
            log.info("Push successful: " + summary);
        } else {
            log.warn("Push failed: " + summary);
        }
    }
    return summary;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) WebMessageParseException(org.hisp.dhis.dxf2.webmessage.WebMessageParseException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Date(java.util.Date) ImportCount(org.hisp.dhis.dxf2.importsummary.ImportCount) SyncEndpoint(org.hisp.dhis.dxf2.sync.SyncEndpoint) DataValueService(org.hisp.dhis.datavalue.DataValueService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) MetadataImportService(org.hisp.dhis.dxf2.metadata.MetadataImportService) AbstractWebMessageResponse(org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse) User(org.hisp.dhis.user.User) SyncUtils(org.hisp.dhis.dxf2.sync.SyncUtils) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) RestTemplate(org.springframework.web.client.RestTemplate) IdSchemes(org.hisp.dhis.common.IdSchemes) DataValueSetService(org.hisp.dhis.dxf2.datavalueset.DataValueSetService) MediaType(org.springframework.http.MediaType) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) SchemaService(org.hisp.dhis.schema.SchemaService) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) RequestCallback(org.springframework.web.client.RequestCallback) CodecUtils(org.hisp.dhis.system.util.CodecUtils) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Optional(java.util.Optional) SettingKey(org.hisp.dhis.setting.SettingKey) RequestCallback(org.springframework.web.client.RequestCallback) IdSchemes(org.hisp.dhis.common.IdSchemes) AbstractWebMessageResponse(org.hisp.dhis.dxf2.webmessage.AbstractWebMessageResponse) ImportCount(org.hisp.dhis.dxf2.importsummary.ImportCount) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Date(java.util.Date) SyncEndpoint(org.hisp.dhis.dxf2.sync.SyncEndpoint)

Example 23 with IdSchemes

use of org.hisp.dhis.common.IdSchemes in project dhis2-core by dhis2.

the class DataValueSetServiceExportTest method testExportOutputIdSchemeAttribute.

@Test
void testExportOutputIdSchemeAttribute() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    String attributeIdScheme = IdScheme.ATTR_ID_SCHEME_PREFIX + atA.getUid();
    IdSchemes idSchemes = new IdSchemes().setDataElementIdScheme(attributeIdScheme).setOrgUnitIdScheme(attributeIdScheme).setCategoryOptionComboIdScheme(attributeIdScheme);
    DataExportParams params = new DataExportParams().setDataSets(Sets.newHashSet(dsB)).setOrganisationUnits(Sets.newHashSet(ouA)).setPeriods(Sets.newHashSet(peB)).setOutputIdSchemes(idSchemes);
    dataValueSetService.exportDataValueSetJson(params, out);
    DataValueSet dvs = jsonMapper.readValue(out.toByteArray(), DataValueSet.class);
    assertNotNull(dvs);
    assertNotNull(dvs.getDataSet());
    assertEquals(dsB.getUid(), dvs.getDataSet());
    assertEquals(2, dvs.getDataValues().size());
    for (org.hisp.dhis.dxf2.datavalue.DataValue dv : dvs.getDataValues()) {
        assertNotNull(dv);
        assertEquals(avA.getValue(), dv.getDataElement());
        assertEquals(avB.getValue(), dv.getOrgUnit());
    }
}
Also used : IdSchemes(org.hisp.dhis.common.IdSchemes) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 24 with IdSchemes

use of org.hisp.dhis.common.IdSchemes in project dhis2-core by dhis2.

the class DataValueSetServiceExportTest method testExportLastUpdated.

@Test
void testExportLastUpdated() throws IOException {
    Date lastUpdated = getDate(1970, 1, 1);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    dataValueSetService.exportDataValueSetJson(lastUpdated, out, new IdSchemes());
    DataValueSet dvs = jsonMapper.readValue(out.toByteArray(), DataValueSet.class);
    assertNotNull(dvs);
    assertEquals(12, dvs.getDataValues().size());
    for (org.hisp.dhis.dxf2.datavalue.DataValue dv : dvs.getDataValues()) {
        assertNotNull(dv);
    }
}
Also used : IdSchemes(org.hisp.dhis.common.IdSchemes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 25 with IdSchemes

use of org.hisp.dhis.common.IdSchemes in project dhis2-core by dhis2.

the class DataValueSetServiceTest method testImportDataValuesXmlUpdatesOnly.

@Test
void testImportDataValuesXmlUpdatesOnly() throws Exception {
    in = new ClassPathResource("datavalueset/dataValueSetB.xml").getInputStream();
    ImportOptions importOptions = new ImportOptions().setImportStrategy(ImportStrategy.UPDATES);
    IdSchemes idSchemes = new IdSchemes();
    idSchemes.setIdScheme("UID");
    idSchemes.setDataElementIdScheme("UID");
    idSchemes.setOrgUnitIdScheme("UID");
    importOptions.setIdSchemes(idSchemes);
    ImportSummary summary = dataValueSetService.importDataValueSetXml(in, importOptions);
    assertHasNoConflicts(summary);
    assertEquals(0, summary.getImportCount().getImported());
    assertEquals(0, summary.getImportCount().getUpdated());
    assertEquals(0, summary.getImportCount().getDeleted());
    assertEquals(12, summary.getImportCount().getIgnored());
    assertEquals(ImportStatus.SUCCESS, summary.getStatus());
    Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
    assertNotNull(dataValues);
    assertEquals(0, dataValues.size());
}
Also used : IdSchemes(org.hisp.dhis.common.IdSchemes) DataValue(org.hisp.dhis.datavalue.DataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ClassPathResource(org.springframework.core.io.ClassPathResource) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

IdSchemes (org.hisp.dhis.common.IdSchemes)25 DataExportParams (org.hisp.dhis.datavalue.DataExportParams)9 Date (java.util.Date)8 Test (org.junit.jupiter.api.Test)7 ArrayList (java.util.ArrayList)6 IdScheme (org.hisp.dhis.common.IdScheme)6 User (org.hisp.dhis.user.User)6 HashSet (java.util.HashSet)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 IOException (java.io.IOException)4 List (java.util.List)4 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)4 DataSet (org.hisp.dhis.dataset.DataSet)4 DataValue (org.hisp.dhis.datavalue.DataValue)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashMap (java.util.HashMap)3 Slf4j (lombok.extern.slf4j.Slf4j)3 CachingMap (org.hisp.dhis.commons.collection.CachingMap)3 DataValueService (org.hisp.dhis.datavalue.DataValueService)3