Search in sources :

Example 1 with UserKeyJsonValue

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);
}
Also used : UserKeyJsonValue(org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UserKeyJsonValue

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);
}
Also used : UserKeyJsonValue(org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with UserKeyJsonValue

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);
}
Also used : UserKeyJsonValue(org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)3 UserKeyJsonValue (org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3