Search in sources :

Example 6 with KeyJsonValue

use of org.hisp.dhis.keyjsonvalue.KeyJsonValue in project dhis2-core by dhis2.

the class DefaultMetadataVersionService method createMetadataVersionInDataStore.

@Override
public void createMetadataVersionInDataStore(String versionName, String versionSnapshot) {
    if (StringUtils.isEmpty(versionSnapshot)) {
        throw new MetadataVersionServiceException("The Metadata Snapshot is null while trying to create a Metadata Version entry in DataStore.");
    }
    KeyJsonValue keyJsonValue = new KeyJsonValue();
    keyJsonValue.setKey(versionName);
    keyJsonValue.setNamespace(MetadataVersionService.METADATASTORE);
    keyJsonValue.setValue(versionSnapshot);
    try {
        keyJsonValueService.addKeyJsonValue(keyJsonValue);
    } catch (Exception ex) {
        String message = "Exception occurred while saving the Metadata snapshot in Data Store" + ex.getMessage();
        log.error(message, ex);
        throw new MetadataVersionServiceException(message, ex);
    }
}
Also used : MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) KeyJsonValue(org.hisp.dhis.keyjsonvalue.KeyJsonValue) MetadataVersionServiceException(org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 7 with KeyJsonValue

use of org.hisp.dhis.keyjsonvalue.KeyJsonValue in project dhis2-core by dhis2.

the class KeyJsonValueController method addKeyJsonValue.

/**
     * Creates a new KeyJsonValue Object on the given namespace with the key and value supplied.
     */
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
public void addKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, @RequestParam(defaultValue = "false") boolean encrypt, HttpServletResponse response) throws IOException, WebMessageException {
    if (!hasAccess(namespace)) {
        throw new WebMessageException(WebMessageUtils.forbidden("The namespace '" + namespace + "' is protected, and you don't have the right authority to access it."));
    }
    if (keyJsonValueService.getKeyJsonValue(namespace, key) != null) {
        throw new WebMessageException(WebMessageUtils.conflict("The key '" + key + "' already exists on the namespace '" + namespace + "'."));
    }
    if (!renderService.isValidJson(body)) {
        throw new WebMessageException(WebMessageUtils.badRequest("The data is not valid JSON."));
    }
    KeyJsonValue keyJsonValue = new KeyJsonValue();
    keyJsonValue.setKey(key);
    keyJsonValue.setNamespace(namespace);
    keyJsonValue.setValue(body);
    keyJsonValue.setEncrypted(encrypt);
    keyJsonValueService.addKeyJsonValue(keyJsonValue);
    response.setStatus(HttpServletResponse.SC_CREATED);
    messageService.sendJson(WebMessageUtils.created("Key '" + key + "' created."), response);
}
Also used : KeyJsonValue(org.hisp.dhis.keyjsonvalue.KeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with KeyJsonValue

use of org.hisp.dhis.keyjsonvalue.KeyJsonValue in project dhis2-core by dhis2.

the class KeyJsonValueController method deleteKeyJsonValue.

/**
     * Delete a key from the given namespace.
     */
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.DELETE, produces = "application/json")
public void deleteKeyJsonValue(@PathVariable String namespace, @PathVariable String key, HttpServletResponse response) throws WebMessageException {
    if (!hasAccess(namespace)) {
        throw new WebMessageException(WebMessageUtils.forbidden("The namespace '" + namespace + "' is protected, and you don't have the right authority to access it."));
    }
    KeyJsonValue keyJsonValue = keyJsonValueService.getKeyJsonValue(namespace, key);
    if (keyJsonValue == null) {
        throw new WebMessageException(WebMessageUtils.notFound("The key '" + key + "' was not found in the namespace '" + namespace + "'."));
    }
    keyJsonValueService.deleteKeyJsonValue(keyJsonValue);
    messageService.sendJson(WebMessageUtils.ok("Key '" + key + "' deleted from namespace '" + namespace + "'."), response);
}
Also used : KeyJsonValue(org.hisp.dhis.keyjsonvalue.KeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

KeyJsonValue (org.hisp.dhis.keyjsonvalue.KeyJsonValue)8 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 DhisSpringTest (org.hisp.dhis.DhisSpringTest)3 Test (org.junit.Test)3 DataElement (org.hisp.dhis.dataelement.DataElement)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 MetadataVersionServiceException (org.hisp.dhis.dxf2.metadata.version.exception.MetadataVersionServiceException)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1