Search in sources :

Example 11 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class DefaultMetadataVersionService method saveVersion.

/**
     * This method is taking care of 3 steps:
     * 1. Generating a metadata snapshot (using the ExportService)
     * 2. Saving that snapshot to the DataStore
     * 3. Creating the actual MetadataVersion entry.
     */
@Override
public synchronized boolean saveVersion(VersionType versionType) {
    MetadataVersion currentVersion = getCurrentVersion();
    String versionName = MetadataVersionNameGenerator.getNextVersionName(currentVersion);
    Date minDate;
    if (currentVersion == null) {
        minDate = null;
    } else {
        minDate = currentVersion.getCreated();
    }
    //1. Get export of metadata
    ByteArrayOutputStream os = getMetadataExport(minDate);
    //2. Save the metadata snapshot in DHIS Data Store
    String value = getBodyAsString(StandardCharsets.UTF_8, os);
    createMetadataVersionInDataStore(versionName, value);
    //3. Create an entry for the MetadataVersion
    MetadataVersion version = new MetadataVersion();
    version.setName(versionName);
    version.setCreated(new Date());
    version.setType(versionType);
    try {
        String hashCode = HashCodeGenerator.getHashCode(value);
        version.setHashCode(hashCode);
    } catch (NoSuchAlgorithmException e) {
        String message = "Exception occurred while generating MetadataVersion HashCode " + e.getMessage();
        log.error(message, e);
        throw new MetadataVersionServiceException(message, e);
    }
    try {
        addVersion(version);
        metadataSystemSettingService.setSystemMetadataVersion(version.getName());
    } catch (Exception ex) {
        String message = "Exception occurred while saving a new MetadataVersion " + ex.getMessage();
        log.error(message, ex);
        throw new MetadataVersionServiceException(message, ex);
    }
    return true;
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Date(java.util.Date) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 12 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class DefaultMetadataVersionService method getMetadataExport.

//--------------------------------------------------------------------------
// Private methods
//--------------------------------------------------------------------------
/**
     * Generates the metadata export based on the created date of the current version.
     */
private ByteArrayOutputStream getMetadataExport(Date minDate) {
    ByteArrayOutputStream os = null;
    try {
        MetadataExportParams exportParams = new MetadataExportParams();
        if (minDate != null) {
            List<String> defaultFilterList = new ArrayList<String>();
            defaultFilterList.add("lastUpdated:gte:" + DateUtils.getLongGmtDateString(minDate));
            exportParams.setDefaultFilter(defaultFilterList);
            metadataExportService.validate(exportParams);
        }
        os = new ByteArrayOutputStream(1024);
        RootNode metadata = metadataExportService.getMetadataAsNode(exportParams);
        nodeService.serialize(metadata, "application/json", os);
    } catch (//We have to catch the "Exception" object as no specific exception on the contract.
    Exception ex) {
        String message = "Exception occurred while exporting metadata for capturing a metadata version" + ex.getMessage();
        log.error(message, ex);
        throw new MetadataVersionServiceException(message, ex);
    }
    return os;
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) RootNode(org.hisp.dhis.node.types.RootNode) MetadataExportParams(org.hisp.dhis.dxf2.metadata.MetadataExportParams) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 13 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class MetadataVersionDelegate method getMetaDataDifference.

public List<MetadataVersion> getMetaDataDifference(MetadataVersion metadataVersion) {
    String url;
    List<MetadataVersion> metadataVersions = new ArrayList<>();
    if (metadataVersion == null) {
        url = metadataSystemSettingService.getEntireVersionHistory();
    } else {
        url = metadataSystemSettingService.getMetaDataDifferenceURL(metadataVersion.getName());
    }
    DhisHttpResponse dhisHttpResponse = getDhisHttpResponse(url, VERSION_TIMEOUT);
    if (isValidDhisHttpResponse(dhisHttpResponse)) {
        try {
            metadataVersions = renderService.fromMetadataVersion(new ByteArrayInputStream(dhisHttpResponse.getResponse().getBytes()), RenderFormat.JSON);
            return metadataVersions;
        } catch (IOException io) {
            String message = "Exception occurred while trying to do JSON conversion. Caused by:  " + io.getMessage();
            log.error(message, io);
            throw new MetadataVersionServiceException(message, io);
        }
    }
    log.warn("Returning empty for the metadata versions difference");
    return metadataVersions;
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 14 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class MetadataVersionDelegate method getDhisHttpResponse.

//----------------------------------------------------------------------------------------
// Private Methods
//----------------------------------------------------------------------------------------
private DhisHttpResponse getDhisHttpResponse(String url, int timeout) {
    AvailabilityStatus remoteServerAvailable = synchronizationManager.isRemoteServerAvailable();
    if (!(remoteServerAvailable.isAvailable())) {
        String message = remoteServerAvailable.getMessage();
        log.error(message);
        throw new RemoteServerUnavailableException(message);
    }
    String username = metadataSystemSettingService.getRemoteInstanceUserName();
    String password = metadataSystemSettingService.getRemoteInstancePassword();
    log.info("Remote server metadata version  URL: " + url + ", username: " + username);
    DhisHttpResponse dhisHttpResponse = null;
    try {
        dhisHttpResponse = HttpUtils.httpGET(url, true, username, password, null, timeout, true);
    } catch (Exception e) {
        String message = "Exception occurred while trying to make the GET call to URL: " + url;
        log.error(message, e);
        throw new MetadataVersionServiceException(message, e);
    }
    return dhisHttpResponse;
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) DhisHttpResponse(org.hisp.dhis.system.util.DhisHttpResponse) AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus) RemoteServerUnavailableException(org.hisp.dhis.exception.RemoteServerUnavailableException) IOException(java.io.IOException) RemoteServerUnavailableException(org.hisp.dhis.exception.RemoteServerUnavailableException) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)

Example 15 with Metadata

use of org.hisp.dhis.dxf2.metadata.Metadata in project dhis2-core by dhis2.

the class DefaultSynchronizationManager method executeMetadataPull.

@Override
public ImportReport executeMetadataPull(String url) {
    User user = currentUserService.getCurrentUser();
    String userUid = user != null ? user.getUid() : null;
    log.info(String.format("Metadata pull, url: %s, user: %s", url, userUid));
    String json = restTemplate.getForObject(url, String.class);
    Metadata metadata = null;
    try {
        metadata = DefaultRenderService.getJsonMapper().readValue(json, Metadata.class);
    } catch (IOException ex) {
        throw new RuntimeException("Failed to parse remote JSON document", ex);
    }
    MetadataImportParams importParams = new MetadataImportParams();
    importParams.setSkipSharing(true);
    importParams.setAtomicMode(AtomicMode.NONE);
    importParams.addMetadata(schemaService.getMetadataSchemas(), metadata);
    return importService.importMetadata(importParams);
}
Also used : User(org.hisp.dhis.user.User) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) Metadata(org.hisp.dhis.dxf2.metadata.Metadata) IOException(java.io.IOException)

Aggregations

DhisSpringTest (org.hisp.dhis.DhisSpringTest)55 Test (org.junit.Test)55 List (java.util.List)46 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)46 ClassPathResource (org.springframework.core.io.ClassPathResource)42 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)37 DataElement (org.hisp.dhis.dataelement.DataElement)25 User (org.hisp.dhis.user.User)20 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)19 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)19 Metadata (com.google.android.exoplayer2.metadata.Metadata)18 DataSet (org.hisp.dhis.dataset.DataSet)15 ArrayList (java.util.ArrayList)14 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 IOException (java.io.IOException)10 Metadata (org.hisp.dhis.dxf2.metadata.Metadata)10 DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)10 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)10 IntegrationTest (org.hisp.dhis.IntegrationTest)9