use of org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue in project dhis2-core by dhis2.
the class UserKeyJsonValueController method addUserKeyJsonValue.
/**
* Creates a new KeyJsonValue Object on the current user with the key, namespace and value supplied.
*/
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
public void addUserKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, @RequestParam(defaultValue = "false") boolean encrypt, HttpServletResponse response) throws IOException, WebMessageException {
if (userKeyJsonValueService.getUserKeyJsonValue(currentUserService.getCurrentUser(), namespace, key) != null) {
throw new WebMessageException(WebMessageUtils.conflict("The key '" + key + "' already exists in the namespace '" + namespace + "'."));
}
if (!renderService.isValidJson(body)) {
throw new WebMessageException(WebMessageUtils.badRequest("The data is not valid JSON."));
}
UserKeyJsonValue userKeyJsonValue = new UserKeyJsonValue();
userKeyJsonValue.setKey(key);
userKeyJsonValue.setUser(currentUserService.getCurrentUser());
userKeyJsonValue.setNamespace(namespace);
userKeyJsonValue.setValue(body);
userKeyJsonValue.setEncrypted(encrypt);
userKeyJsonValueService.addUserKeyJsonValue(userKeyJsonValue);
response.setStatus(HttpServletResponse.SC_CREATED);
messageService.sendJson(WebMessageUtils.created("Key '" + key + "' in namespace '" + namespace + "' created."), response);
}
use of org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue in project dhis2-core by dhis2.
the class UserKeyJsonValueController method deleteUserKeyJsonValue.
/**
* Delete a key.
*/
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.DELETE, produces = "application/json")
public void deleteUserKeyJsonValue(@PathVariable String namespace, @PathVariable String key, HttpServletResponse response) throws WebMessageException {
UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue(currentUserService.getCurrentUser(), namespace, key);
if (userKeyJsonValue == null) {
throw new WebMessageException(WebMessageUtils.notFound("The key '" + key + "' was not found in the namespace '" + namespace + "'."));
}
userKeyJsonValueService.deleteUserKeyJsonValue(userKeyJsonValue);
messageService.sendJson(WebMessageUtils.ok("Key '" + key + "' deleted from the namespace '" + namespace + "'."), response);
}
use of org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue in project dhis2-core by dhis2.
the class UserKeyJsonValueController method updateUserKeyJsonValue.
/**
* Update a key.
*/
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json")
public void updateUserKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, HttpServletResponse response) throws WebMessageException, IOException {
UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue(currentUserService.getCurrentUser(), namespace, key);
if (userKeyJsonValue == 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."));
}
userKeyJsonValue.setValue(body);
userKeyJsonValueService.updateUserKeyJsonValue(userKeyJsonValue);
response.setStatus(HttpServletResponse.SC_OK);
messageService.sendJson(WebMessageUtils.created("Key '" + key + "' in namespace '" + namespace + "' updated."), response);
}
Aggregations