Search in sources :

Example 1 with KeyJsonValue

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

the class KeyJsonValueController method getKeyJsonValueMetaData.

/**
     * Retrieves the KeyJsonValue represented by the given key from the given namespace.
     */
@RequestMapping(value = "/{namespace}/{key}/metaData", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public KeyJsonValue getKeyJsonValueMetaData(@PathVariable String namespace, @PathVariable String key, HttpServletResponse response) throws Exception {
    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 + "'."));
    }
    KeyJsonValue metaDataValue = new KeyJsonValue();
    BeanUtils.copyProperties(metaDataValue, keyJsonValue);
    metaDataValue.setValue(null);
    return metaDataValue;
}
Also used : KeyJsonValue(org.hisp.dhis.keyjsonvalue.KeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with KeyJsonValue

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

the class KeyJsonValueController method updateKeyJsonValue.

/**
     * Update a key in the given namespace.
     */
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json")
public void updateKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, HttpServletRequest request, HttpServletResponse response) throws WebMessageException, IOException {
    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 + "'."));
    }
    if (!renderService.isValidJson(body)) {
        throw new WebMessageException(WebMessageUtils.badRequest("The data is not valid JSON."));
    }
    keyJsonValue.setValue(body);
    keyJsonValueService.updateKeyJsonValue(keyJsonValue);
    response.setStatus(HttpServletResponse.SC_OK);
    messageService.sendJson(WebMessageUtils.ok("Key '" + key + "' updated."), response);
}
Also used : KeyJsonValue(org.hisp.dhis.keyjsonvalue.KeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with KeyJsonValue

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

the class DefaultMetadataVersionServiceTest method testShouldCreateASnapshotThatContainsOnlyDelta.

@Test
public void testShouldCreateASnapshotThatContainsOnlyDelta() {
    versionService.addVersion(versionA);
    DataElement de1 = createDataElement('A');
    manager.save(de1);
    sleepFor(100);
    versionService.saveVersion(VersionType.BEST_EFFORT);
    de1 = createDataElement('B');
    manager.save(de1);
    sleepFor(100);
    versionService.saveVersion(VersionType.BEST_EFFORT);
    KeyJsonValue expectedJson = keyJsonValueService.getKeyJsonValue(MetadataVersionService.METADATASTORE, "Version_3");
    assertEquals(false, expectedJson.getPlainValue().contains("DataElementA"));
    assertEquals(true, expectedJson.getPlainValue().contains("DataElementB"));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) KeyJsonValue(org.hisp.dhis.keyjsonvalue.KeyJsonValue) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 4 with KeyJsonValue

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

the class DefaultMetadataVersionServiceTest method testShouldGiveValidVersionDataIfExists.

@Test
public void testShouldGiveValidVersionDataIfExists() throws Exception {
    KeyJsonValue keyJsonValue = new KeyJsonValue();
    keyJsonValue.setNamespace(MetadataVersionService.METADATASTORE);
    keyJsonValue.setKey("myVersion");
    keyJsonValue.setPlainValue("myJson");
    keyJsonValueService.addKeyJsonValue(keyJsonValue);
    assertEquals("myJson", versionService.getVersionData("myVersion"));
}
Also used : KeyJsonValue(org.hisp.dhis.keyjsonvalue.KeyJsonValue) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 5 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)

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