Search in sources :

Example 46 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class CurrentUserController method getCurrentUser.

@RequestMapping
@ResponseBody
public RootNode getCurrentUser(HttpServletResponse response) throws Exception {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    if (fields.isEmpty()) {
        fields.add(":all");
    }
    CollectionNode collectionNode = fieldFilterService.filter(User.class, Collections.singletonList(currentUser), fields);
    RootNode rootNode = new RootNode(collectionNode.getChildren().get(0));
    rootNode.setDefaultNamespace(DxfNamespaces.DXF_2_0);
    rootNode.setNamespace(DxfNamespaces.DXF_2_0);
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) User(org.hisp.dhis.user.User) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) CollectionNode(org.hisp.dhis.node.types.CollectionNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 47 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class LockExceptionController method getLockExceptions.

// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public RootNode getLockExceptions(@RequestParam(required = false) String key, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws IOException, WebMessageException {
    List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    List<LockException> lockExceptions = new ArrayList<>();
    if (key != null) {
        LockException lockException = dataSetService.getLockException(MathUtils.parseInt(key));
        if (lockException == null) {
            throw new WebMessageException(WebMessageUtils.notFound("Cannot find LockException with key: " + key));
        }
        lockExceptions.add(lockException);
    } else if (!filters.isEmpty()) {
        lockExceptions = dataSetService.filterLockExceptions(filters);
    } else {
        lockExceptions = dataSetService.getAllLockExceptions();
    }
    WebOptions options = new WebOptions(rpParameters);
    WebMetadata metadata = new WebMetadata();
    Pager pager = metadata.getPager();
    if (options.hasPaging() && pager == null) {
        pager = new Pager(options.getPage(), lockExceptions.size(), options.getPageSize());
        lockExceptions = PagerUtils.pageCollection(lockExceptions, pager);
    }
    RootNode rootNode = NodeUtils.createMetadata();
    if (pager != null) {
        rootNode.addChild(NodeUtils.createPager(pager));
    }
    rootNode.addChild(fieldFilterService.filter(LockException.class, lockExceptions, fields));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) LockException(org.hisp.dhis.dataset.LockException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Pager(org.hisp.dhis.common.Pager) ArrayList(java.util.ArrayList) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 48 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class MessageConversationController method setUserAssigned.

//--------------------------------------------------------------------------
// Assign user
//--------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/assign", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
@ResponseBody
public RootNode setUserAssigned(@PathVariable String uid, @RequestParam(required = false) String userId, HttpServletResponse response) {
    RootNode responseNode = new RootNode("response");
    User user = currentUserService.getCurrentUser();
    if (!canModifyUserConversation(user, user) && (messageService.hasAccessToManageFeedbackMessages(user))) {
        throw new UpdateAccessDeniedException("Not authorized to modify this object.");
    }
    org.hisp.dhis.message.MessageConversation messageConversation = messageService.getMessageConversation(uid);
    if (messageConversation == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "No MessageConversation found for the given ID."));
        return responseNode;
    }
    User userToAssign;
    if ((userToAssign = userService.getUser(userId)) == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        responseNode.addChild(new SimpleNode("message", "Could not find user to assign"));
        return responseNode;
    }
    if (!configurationService.isUserInFeedbackRecipientUserGroup(userToAssign)) {
        response.setStatus(HttpServletResponse.SC_CONFLICT);
        responseNode.addChild(new SimpleNode("message", "User provided is not a member of the system's feedback recipient group"));
        return responseNode;
    }
    messageConversation.setAssignee(userToAssign);
    messageService.updateMessageConversation(messageConversation);
    responseNode.addChild(new SimpleNode("message", "User " + userToAssign.getName() + " was assigned to ticket"));
    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) SimpleNode(org.hisp.dhis.node.types.SimpleNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 49 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class MessageConversationController method getObject.

@Override
public RootNode getObject(@PathVariable String uid, Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
    org.hisp.dhis.message.MessageConversation messageConversation = messageService.getMessageConversation(uid);
    if (messageConversation == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        RootNode responseNode = new RootNode("reply");
        responseNode.addChild(new SimpleNode("message", "No MessageConversation found with UID: " + uid));
        return responseNode;
    }
    if (!canReadMessageConversation(currentUserService.getCurrentUser(), messageConversation)) {
        throw new AccessDeniedException("Not authorized to access this conversation.");
    }
    return super.getObject(uid, rpParameters, request, response);
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Example 50 with RootNode

use of org.hisp.dhis.node.types.RootNode 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)

Aggregations

RootNode (org.hisp.dhis.node.types.RootNode)60 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)33 CollectionNode (org.hisp.dhis.node.types.CollectionNode)30 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)30 SimpleNode (org.hisp.dhis.node.types.SimpleNode)27 ComplexNode (org.hisp.dhis.node.types.ComplexNode)14 ArrayList (java.util.ArrayList)12 User (org.hisp.dhis.user.User)11 Pager (org.hisp.dhis.common.Pager)10 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)10 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)9 Node (org.hisp.dhis.node.Node)8 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)7 List (java.util.List)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 Date (java.util.Date)4 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)4 WebMetadata (org.hisp.dhis.webapi.webdomain.WebMetadata)4 Lists (com.google.common.collect.Lists)3 IOException (java.io.IOException)3