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;
}
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);
}
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"));
}
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"));
}
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);
}
Aggregations