← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18216: Reimplement setting of read property for single message conversation through POST to <uid>/read a...

 

------------------------------------------------------------
revno: 18216
committer: Halvdan Hoem Grelland <halvdanhg@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-02-10 17:46:23 +0100
message:
  Reimplement setting of read property for single message conversation through POST to <uid>/read and <uid>/unread. Refactored and unified request handler methods for read/unread
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MessageConversationController.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MessageConversationController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MessageConversationController.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MessageConversationController.java	2015-02-10 16:46:23 +0000
@@ -267,107 +267,40 @@
         ContextUtils.createdResponse( response, "Feedback created", null );
     }
 
-
     //--------------------------------------------------------------------------
     // Mark conversations read
     //--------------------------------------------------------------------------
 
+    @RequestMapping( value = "/{uid}/read", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE } )
+    public @ResponseBody RootNode markMessageConversationRead(
+         @PathVariable String uid, @RequestParam ( required = false ) String userUid, HttpServletResponse response )
+    {
+        return modifyMessageConversationRead( userUid, new String[]{ uid }, response, true );
+    }
+
     @RequestMapping( value = "/read", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE } )
     public @ResponseBody RootNode markMessageConversationsRead(
         @RequestParam( value = "user", required = false ) String userUid, @RequestBody String[] uids, HttpServletResponse response )
     {
-        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 UIDs." ) );
-            return responseNode;
-        }
-
-        CollectionNode marked = responseNode.addChild( new CollectionNode( "markedRead" ) );
-        marked.setWrapping( false );
-
-        for ( org.hisp.dhis.message.MessageConversation conversation : messageConversations )
-        {
-            if ( conversation.markRead( user ) )
-            {
-                messageService.updateMessageConversation( conversation );
-                marked.addChild( new SimpleNode( "uid", conversation.getUid() ) );
-            }
-        }
-
-        response.setStatus( HttpServletResponse.SC_OK );
-
-        return responseNode;
+        return modifyMessageConversationRead( userUid, uids, response, true );
     }
 
     //--------------------------------------------------------------------------
     // Mark conversations unread
     //--------------------------------------------------------------------------
 
+    @RequestMapping( value = "/{uid}/unread", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE } )
+    public @ResponseBody RootNode markMessageConversationUnread(
+        @PathVariable String uid, @RequestParam ( required = false ) String userUid, HttpServletResponse response )
+    {
+        return modifyMessageConversationRead( userUid, new String[]{ uid }, response, false );
+    }
+
     @RequestMapping( value = "/unread", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE } )
     public @ResponseBody RootNode markMessageConversationsUnread(
         @RequestParam( value = "user", required = false ) String userUid, @RequestBody String[] uids, HttpServletResponse response )
     {
-        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 UIDs." ) );
-            return responseNode;
-        }
-
-        CollectionNode marked = responseNode.addChild( new CollectionNode( "markedUnread" ) );
-        marked.setWrapping( false );
-
-        for ( org.hisp.dhis.message.MessageConversation conversation : messageConversations )
-        {
-            if ( conversation.markUnread( user ) )
-            {
-                messageService.updateMessageConversation( conversation );
-                marked.addChild( new SimpleNode( "uid", conversation.getUid() ) );
-            }
-        }
-
-        response.setStatus( HttpServletResponse.SC_OK );
-
-        return responseNode;
+        return modifyMessageConversationRead( userUid, uids, response, false );
     }
 
     //--------------------------------------------------------------------------
@@ -626,4 +559,56 @@
     {
         return messageConversation.getUsers().contains( user ) || user.getUserCredentials().hasAnyAuthority( AclService.ACL_OVERRIDE_AUTHORITIES );
     }
+
+    /**
+     * 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, 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;
+    }
 }