Search in sources :

Example 1 with MessageConversation

use of org.hisp.dhis.webapi.webdomain.MessageConversation in project dhis2-core by dhis2.

the class MessageConversationController method postXmlObject.

//--------------------------------------------------------------------------
// POST for new MessageConversation
//--------------------------------------------------------------------------
@Override
public void postXmlObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
    MessageConversation messageConversation = renderService.fromXml(request.getInputStream(), MessageConversation.class);
    postObject(response, request, messageConversation);
}
Also used : MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation)

Example 2 with MessageConversation

use of org.hisp.dhis.webapi.webdomain.MessageConversation in project dhis2-core by dhis2.

the class MessageConversationController method getMessage.

/**
 * /* Returns the specified message after making sure the user has access to
 * it.
 *
 * @param mcUid the message conversation UID.
 * @param msgUid the message UID.
 * @param user the user.
 * @return a {@link Message}.
 * @throws WebMessageException
 */
private Message getMessage(String mcUid, String msgUid, User user) throws WebMessageException {
    org.hisp.dhis.message.MessageConversation conversation = messageService.getMessageConversation(mcUid);
    if (conversation == null) {
        throw new WebMessageException(notFound(String.format("No message conversation with uid '%s'", mcUid)));
    }
    if (!canReadMessageConversation(user, conversation)) {
        throw new AccessDeniedException("Not authorized to access this conversation.");
    }
    List<Message> messages = conversation.getMessages().stream().filter(msg -> msg.getUid().equals(msgUid)).collect(Collectors.toList());
    if (messages.size() < 1) {
        throw new WebMessageException(notFound(String.format("No message with uid '%s' in messageConversation '%s", msgUid, mcUid)));
    }
    Message message = messages.get(0);
    if (message.isInternal() && !configurationService.isUserInFeedbackRecipientUserGroup(user)) {
        throw new WebMessageException(conflict("Not authorized to access this message"));
    }
    return message;
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) Order(org.hisp.dhis.query.Order) RequestParam(org.springframework.web.bind.annotation.RequestParam) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) UserMessage(org.hisp.dhis.message.UserMessage) WebMessageUtils.created(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.created) Pagination(org.hisp.dhis.query.Pagination) Autowired(org.springframework.beans.factory.annotation.Autowired) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) MessageService(org.hisp.dhis.message.MessageService) CurrentUser(org.hisp.dhis.user.CurrentUser) MessageConversationStatus(org.hisp.dhis.message.MessageConversationStatus) MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Map(java.util.Map) Message(org.hisp.dhis.message.Message) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) PostMapping(org.springframework.web.bind.annotation.PostMapping) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) MessageConversationPriority(org.hisp.dhis.message.MessageConversationPriority) MessageType(org.hisp.dhis.message.MessageType) UserGroup(org.hisp.dhis.user.UserGroup) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) Set(java.util.Set) Junction(org.hisp.dhis.query.Junction) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Defaults(org.hisp.dhis.fieldfilter.Defaults) SimpleNode(org.hisp.dhis.node.types.SimpleNode) List(java.util.List) FileResourceUtils(org.hisp.dhis.webapi.utils.FileResourceUtils) UserGroupService(org.hisp.dhis.user.UserGroupService) WebMessageUtils.conflict(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RootNode(org.hisp.dhis.node.types.RootNode) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMessageUtils.notFound(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound) CollectionNode(org.hisp.dhis.node.types.CollectionNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestBody(org.springframework.web.bind.annotation.RequestBody) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) User(org.hisp.dhis.user.User) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) QueryParserException(org.hisp.dhis.query.QueryParserException) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) Pager(org.hisp.dhis.common.Pager) FileResource(org.hisp.dhis.fileresource.FileResource) HttpServletResponse(javax.servlet.http.HttpServletResponse) MessageConversationSchemaDescriptor(org.hisp.dhis.schema.descriptors.MessageConversationSchemaDescriptor) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) HttpStatus(org.springframework.http.HttpStatus) ConfigurationService(org.hisp.dhis.configuration.ConfigurationService) FileResourceDomain(org.hisp.dhis.fileresource.FileResourceDomain) Collections(java.util.Collections) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) UserMessage(org.hisp.dhis.message.UserMessage) Message(org.hisp.dhis.message.Message) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException)

Example 3 with MessageConversation

use of org.hisp.dhis.webapi.webdomain.MessageConversation in project dhis2-core by dhis2.

the class MessageConversationController method modifyMessageConversationRead.

/**
     * Internal handler for setting the read property of MessageConversation.
     *
     * @param readValue true when setting as read, false when setting unread.
     */
private RootNode modifyMessageConversationRead(String userUid, List<String> uids, HttpServletResponse response, boolean readValue) {
    RootNode responseNode = new RootNode("response");
    User currentUser = currentUserService.getCurrentUser();
    User user = userUid != null ? userService.getUser(userUid) : currentUser;
    if (user == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No user with uid: " + userUid));
        return responseNode;
    }
    if (!canModifyUserConversation(currentUser, user)) {
        throw new UpdateAccessDeniedException("Not authorized to modify this object.");
    }
    Collection<org.hisp.dhis.message.MessageConversation> messageConversations = messageService.getMessageConversations(user, uids);
    if (messageConversations.isEmpty()) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No MessageConversations found for the given IDs."));
        return responseNode;
    }
    CollectionNode marked = responseNode.addChild(new CollectionNode(readValue ? "markedRead" : "markedUnread"));
    marked.setWrapping(false);
    for (org.hisp.dhis.message.MessageConversation conversation : messageConversations) {
        boolean success = (readValue ? conversation.markRead(user) : conversation.markUnread(user));
        if (success) {
            messageService.updateMessageConversation(conversation);
            marked.addChild(new SimpleNode("uid", conversation.getUid()));
        }
    }
    response.setStatus(HttpServletResponse.SC_OK);
    return responseNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation)

Example 4 with MessageConversation

use of org.hisp.dhis.webapi.webdomain.MessageConversation in project dhis2-core by dhis2.

the class MessageConversationController method postJsonObject.

@Override
public void postJsonObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
    MessageConversation messageConversation = renderService.fromJson(request.getInputStream(), MessageConversation.class);
    postObject(response, request, messageConversation);
}
Also used : MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation)

Example 5 with MessageConversation

use of org.hisp.dhis.webapi.webdomain.MessageConversation in project dhis2-core by dhis2.

the class MessageConversationController method modifyMessageConversationRead.

/**
 * Internal handler for setting the read property of MessageConversation.
 *
 * @param readValue true when setting as read, false when setting unread.
 */
private RootNode modifyMessageConversationRead(String userUid, List<String> uids, HttpServletResponse response, boolean readValue, User currentUser) {
    RootNode responseNode = new RootNode("response");
    User user = userUid != null ? userService.getUser(userUid) : currentUser;
    if (user == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No user with uid: " + userUid));
        return responseNode;
    }
    if (!canModifyUserConversation(currentUser, user)) {
        throw new UpdateAccessDeniedException("Not authorized to modify this object.");
    }
    Collection<org.hisp.dhis.message.MessageConversation> messageConversations = messageService.getMessageConversations(user, uids);
    if (messageConversations.isEmpty()) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No MessageConversations found for the given IDs."));
        return responseNode;
    }
    CollectionNode marked = responseNode.addChild(new CollectionNode(readValue ? "markedRead" : "markedUnread"));
    marked.setWrapping(false);
    for (org.hisp.dhis.message.MessageConversation conversation : messageConversations) {
        boolean success = (readValue ? conversation.markRead(user) : conversation.markUnread(user));
        if (success) {
            messageService.updateMessageConversation(conversation);
            marked.addChild(new SimpleNode("uid", conversation.getUid()));
        }
    }
    response.setStatus(HttpServletResponse.SC_OK);
    return responseNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) MessageConversation(org.hisp.dhis.webapi.webdomain.MessageConversation)

Aggregations

MessageConversation (org.hisp.dhis.webapi.webdomain.MessageConversation)5 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)3 CollectionNode (org.hisp.dhis.node.types.CollectionNode)3 RootNode (org.hisp.dhis.node.types.RootNode)3 SimpleNode (org.hisp.dhis.node.types.SimpleNode)3 User (org.hisp.dhis.user.User)2 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Pager (org.hisp.dhis.common.Pager)1