Search in sources :

Example 1 with MetadataVersionException

use of org.hisp.dhis.webapi.controller.exception.MetadataVersionException 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)

Example 2 with MetadataVersionException

use of org.hisp.dhis.webapi.controller.exception.MetadataVersionException in project dhis2-core by dhis2.

the class MetadataVersionController method createSystemVersion.

//Creates version in versioning table, exports the metadata and saves the snapshot in datastore
@PreAuthorize("hasRole('ALL') or hasRole('F_METADATA_MANAGE')")
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/create", method = RequestMethod.POST, produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public MetadataVersion createSystemVersion(@RequestParam(value = "type") VersionType versionType) throws MetadataVersionException, BadRequestException {
    MetadataVersion versionToReturn = null;
    boolean enabled = isMetadataVersioningEnabled();
    try {
        if (!enabled) {
            throw new BadRequestException("Metadata versioning is not enabled for this instance.");
        }
        synchronized (versionService) {
            versionService.saveVersion(versionType);
            versionToReturn = versionService.getCurrentVersion();
            return versionToReturn;
        }
    } catch (MetadataVersionServiceException ex) {
        throw new MetadataVersionException("Unable to create version in system. " + ex.getMessage());
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataVersionException(org.hisp.dhis.webapi.controller.exception.MetadataVersionException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with MetadataVersionException

use of org.hisp.dhis.webapi.controller.exception.MetadataVersionException in project dhis2-core by dhis2.

the class MetadataVersionController method getMetaDataVersion.

//Gets the version by versionName or latest system version
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT, method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public MetadataVersion getMetaDataVersion(@RequestParam(value = "versionName", required = false) String versionName) throws MetadataVersionException, BadRequestException {
    MetadataVersion versionToReturn = null;
    boolean enabled = isMetadataVersioningEnabled();
    try {
        if (!enabled) {
            throw new BadRequestException("Metadata versioning is not enabled for this instance.");
        }
        if (StringUtils.isNotEmpty(versionName)) {
            versionToReturn = versionService.getVersionByName(versionName);
            if (versionToReturn == null) {
                throw new MetadataVersionException("No metadata version with name " + versionName + " exists. Please check again later.");
            }
        } else {
            versionToReturn = versionService.getCurrentVersion();
            if (versionToReturn == null) {
                throw new MetadataVersionException("No metadata versions exist. Please check again later.");
            }
        }
        return versionToReturn;
    } catch (MetadataVersionServiceException ex) {
        throw new MetadataVersionException("Exception occurred while getting metadata version." + (StringUtils.isNotEmpty(versionName) ? versionName : " ") + ex.getMessage(), ex);
    }
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataVersionException(org.hisp.dhis.webapi.controller.exception.MetadataVersionException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with MetadataVersionException

use of org.hisp.dhis.webapi.controller.exception.MetadataVersionException in project dhis2-core by dhis2.

the class MetadataVersionController method downloadGZipVersion.

//endpoint to download metadata in gzip format
@PreAuthorize("hasRole('ALL') or hasRole('F_METADATA_MANAGE')")
@RequestMapping(value = MetadataVersionSchemaDescriptor.API_ENDPOINT + "/{versionName}/data.gz", method = RequestMethod.GET, produces = "*/*")
public void downloadGZipVersion(@PathVariable("versionName") String versionName, HttpServletResponse response) throws MetadataVersionException, IOException, BadRequestException {
    boolean enabled = isMetadataVersioningEnabled();
    try {
        if (!enabled) {
            throw new BadRequestException("Metadata versioning is not enabled for this instance.");
        }
        contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_GZIP, CacheStrategy.NO_CACHE, "metadata.json.gz", true);
        response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
        String versionData = versionService.getVersionData(versionName);
        if (versionData == null) {
            throw new MetadataVersionException("No metadata version snapshot found for the given version " + versionName);
        }
        GZIPOutputStream gos = new GZIPOutputStream(response.getOutputStream());
        gos.write(versionData.getBytes(StandardCharsets.UTF_8));
        gos.close();
    } catch (MetadataVersionServiceException ex) {
        throw new MetadataVersionException("Unable to download version from system: " + versionName + ex.getMessage());
    }
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) GZIPOutputStream(java.util.zip.GZIPOutputStream) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataVersionException(org.hisp.dhis.webapi.controller.exception.MetadataVersionException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)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 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1