use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created in project dhis2-core by dhis2.
the class MessageConversationController method postObject.
private void postObject(HttpServletResponse response, HttpServletRequest request, MessageConversation messageConversation) throws WebMessageException {
List<User> users = new ArrayList<>(messageConversation.getUsers());
messageConversation.getUsers().clear();
for (OrganisationUnit ou : messageConversation.getOrganisationUnits()) {
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(ou.getUid());
if (organisationUnit == null) {
throw new WebMessageException(WebMessageUtils.conflict("Organisation Unit does not exist: " + ou.getUid()));
}
messageConversation.getUsers().addAll(organisationUnit.getUsers());
}
for (User u : users) {
User user = userService.getUser(u.getUid());
if (user == null) {
throw new WebMessageException(WebMessageUtils.conflict("User does not exist: " + u.getUid()));
}
messageConversation.getUsers().add(user);
}
for (UserGroup ug : messageConversation.getUserGroups()) {
UserGroup userGroup = userGroupService.getUserGroup(ug.getUid());
if (userGroup == null) {
throw new WebMessageException(WebMessageUtils.notFound("User Group does not exist: " + ug.getUid()));
}
messageConversation.getUsers().addAll(userGroup.getMembers());
}
if (messageConversation.getUsers().isEmpty()) {
throw new WebMessageException(WebMessageUtils.conflict("No recipients selected."));
}
String metaData = MessageService.META_USER_AGENT + request.getHeader(ContextUtils.HEADER_USER_AGENT);
int id = messageService.sendPrivateMessage(messageConversation.getSubject(), messageConversation.getText(), metaData, messageConversation.getUsers());
org.hisp.dhis.message.MessageConversation conversation = messageService.getMessageConversation(id);
response.addHeader("Location", MessageConversationSchemaDescriptor.API_ENDPOINT + "/" + conversation.getUid());
webMessageService.send(WebMessageUtils.created("Message conversation created"), response, request);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created in project dhis2-core by dhis2.
the class InterpretationController method postComment.
// -------------------------------------------------------------------------
// Comment
// -------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/comments", method = RequestMethod.POST, consumes = { "text/html", "text/plain" })
public void postComment(@PathVariable("uid") String uid, @RequestBody String text, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
Interpretation interpretation = interpretationService.getInterpretation(uid);
if (interpretation == null) {
throw new WebMessageException(WebMessageUtils.conflict("Interpretation does not exist: " + uid));
}
InterpretationComment comment = interpretationService.addInterpretationComment(uid, text);
String builder = InterpretationSchemaDescriptor.API_ENDPOINT + "/" + uid + "/comments/" + comment.getUid();
response.addHeader("Location", builder);
webMessageService.send(WebMessageUtils.created("Commented created"), response, request);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created in project dhis2-core by dhis2.
the class InterpretationController method unlike.
@RequestMapping(value = "/{uid}/like", method = RequestMethod.DELETE)
public void unlike(@PathVariable("uid") String uid, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
Interpretation interpretation = interpretationService.getInterpretation(uid);
if (interpretation == null) {
throw new WebMessageException(WebMessageUtils.conflict("Interpretation does not exist: " + uid));
}
boolean like = interpretationService.unlikeInterpretation(interpretation.getId());
if (like) {
webMessageService.send(WebMessageUtils.created("Like removed from interpretation"), response, request);
} else {
webMessageService.send(WebMessageUtils.conflict("Could not remove like, user had not previously liked interpretation"), response, request);
}
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created 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.dxf2.webmessage.WebMessageUtils.created 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