Search in sources :

Example 1 with MetadataVersionServiceException

use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException 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 2 with MetadataVersionServiceException

use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException 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 3 with MetadataVersionServiceException

use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException 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 4 with MetadataVersionServiceException

use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException 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 5 with MetadataVersionServiceException

use of org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException in project dhis2-core by dhis2.

the class MetadataVersionController method getMetaDataVersionHistory.

//Gets the list of all versions in between the passed version name and latest system version
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/history", method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public RootNode getMetaDataVersionHistory(@RequestParam(value = "baseline", required = false) String versionName) throws MetadataVersionException, BadRequestException {
    List<MetadataVersion> allVersionsInBetween = new ArrayList<>();
    boolean enabled = isMetadataVersioningEnabled();
    try {
        if (!enabled) {
            throw new BadRequestException("Metadata versioning is not enabled for this instance.");
        }
        Date startDate;
        if (versionName == null || versionName.isEmpty()) {
            MetadataVersion initialVersion = versionService.getInitialVersion();
            if (initialVersion == null) {
                return versionService.getMetadataVersionsAsNode(allVersionsInBetween);
            }
            startDate = initialVersion.getCreated();
        } else {
            startDate = versionService.getCreatedDate(versionName);
        }
        if (startDate == null) {
            throw new MetadataVersionException("There is no such metadata version. The latest version is Version " + versionService.getCurrentVersion().getName());
        }
        Date endDate = new Date();
        allVersionsInBetween = versionService.getAllVersionsInBetween(startDate, endDate);
        if (allVersionsInBetween != null) {
            //now remove the baseline version details
            for (Iterator<MetadataVersion> iterator = allVersionsInBetween.iterator(); iterator.hasNext(); ) {
                MetadataVersion m = iterator.next();
                if (m.getName().equals(versionName)) {
                    iterator.remove();
                    break;
                }
            }
            if (!allVersionsInBetween.isEmpty()) {
                return versionService.getMetadataVersionsAsNode(allVersionsInBetween);
            }
        }
    } catch (MetadataVersionServiceException ex) {
        throw new MetadataVersionException(ex.getMessage(), ex);
    }
    return null;
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) ArrayList(java.util.ArrayList) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataVersionException(org.hisp.dhis.webapi.controller.exception.MetadataVersionException) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)16 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)10 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 MetadataSyncServiceException (org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException)4 DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)4 BadRequestException (org.hisp.dhis.webapi.controller.exception.BadRequestException)4 MetadataVersionException (org.hisp.dhis.webapi.controller.exception.MetadataVersionException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Date (java.util.Date)2 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)2 AvailabilityStatus (org.hisp.dhis.dxf2.synch.AvailabilityStatus)2 RemoteServerUnavailableException (org.hisp.dhis.exception.RemoteServerUnavailableException)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 List (java.util.List)1