← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8327: messaging: extended Message class to support userGroup and organisationUnit recipients. Also adde...

 

------------------------------------------------------------
revno: 8327
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2012-10-01 23:36:21 +0700
message:
  messaging: extended Message class to support userGroup and organisationUnit recipients. Also added support for posting JSON messages. mobile: minor changes.
modified:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/message/Message.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MessageConversationController.java
  dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/new-message.vm


--
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-dxf2/src/main/java/org/hisp/dhis/dxf2/message/Message.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/message/Message.java	2012-03-30 13:07:32 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/message/Message.java	2012-10-01 16:36:21 +0000
@@ -36,7 +36,9 @@
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.Dxf2Namespace;
 import org.hisp.dhis.common.view.DetailedView;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserGroup;
 
 import java.util.HashSet;
 import java.util.Set;
@@ -51,8 +53,12 @@
 
     private String text;
 
+    private Set<OrganisationUnit> organisationUnits = new HashSet<OrganisationUnit>();
+
     private Set<User> users = new HashSet<User>();
 
+    private Set<UserGroup> userGroups = new HashSet<UserGroup>();
+
     public Message()
     {
     }
@@ -91,7 +97,20 @@
 
     @JsonProperty
     @JsonSerialize( contentAs = BaseIdentifiableObject.class )
-    @JsonView( {DetailedView.class} )
+    @JacksonXmlElementWrapper( localName = "organisationUnits", namespace = Dxf2Namespace.NAMESPACE )
+    @JacksonXmlProperty( localName = "organisationUnit", namespace = Dxf2Namespace.NAMESPACE )
+    public Set<OrganisationUnit> getOrganisationUnits()
+    {
+        return organisationUnits;
+    }
+
+    public void setOrganisationUnits( Set<OrganisationUnit> organisationUnits )
+    {
+        this.organisationUnits = organisationUnits;
+    }
+
+    @JsonProperty
+    @JsonSerialize( contentAs = BaseIdentifiableObject.class )
     @JacksonXmlElementWrapper( localName = "users", namespace = Dxf2Namespace.NAMESPACE )
     @JacksonXmlProperty( localName = "user", namespace = Dxf2Namespace.NAMESPACE )
     public Set<User> getUsers()
@@ -103,4 +122,30 @@
     {
         this.users = users;
     }
+
+    @JsonProperty
+    @JsonSerialize( contentAs = BaseIdentifiableObject.class )
+    @JacksonXmlElementWrapper( localName = "userGroups", namespace = Dxf2Namespace.NAMESPACE )
+    @JacksonXmlProperty( localName = "userGroup", namespace = Dxf2Namespace.NAMESPACE )
+    public Set<UserGroup> getUserGroups()
+    {
+        return userGroups;
+    }
+
+    public void setUserGroups( Set<UserGroup> userGroups )
+    {
+        this.userGroups = userGroups;
+    }
+
+    @Override
+    public String toString()
+    {
+        return "Message{" +
+            "subject='" + subject + '\'' +
+            ", text='" + text + '\'' +
+            ", organisationUnits=" + organisationUnits +
+            ", users=" + users +
+            ", userGroups=" + userGroups +
+            '}';
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java	2012-07-14 10:40:48 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java	2012-10-01 16:36:21 +0000
@@ -153,7 +153,7 @@
 
     @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
     @PreAuthorize( "hasRole('ALL')" )
-    public void postJsonObject( HttpServletResponse response, InputStream input ) throws Exception
+    public void postJsonObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
     {
         throw new HttpRequestMethodNotSupportedException( RequestMethod.POST.toString() );
     }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MessageConversationController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MessageConversationController.java	2012-06-14 18:00:34 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MessageConversationController.java	2012-10-01 16:36:21 +0000
@@ -33,7 +33,11 @@
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.message.MessageService;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserGroup;
+import org.hisp.dhis.user.UserGroupService;
 import org.hisp.dhis.user.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -61,8 +65,14 @@
     private MessageService messageService;
 
     @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    @Autowired
     private UserService userService;
 
+    @Autowired
+    private UserGroupService userGroupService;
+
     @Override
     protected List<MessageConversation> getEntityList( WebMetaData metaData, WebOptions options )
     {
@@ -100,30 +110,72 @@
     public void postXmlObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
     {
         Message message = JacksonUtils.fromXml( input, Message.class );
-
+        postObject( response, request, message );
+    }
+
+    @Override
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_SEND_MESSAGE')" )
+    public void postJsonObject( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws Exception
+    {
+        Message message = JacksonUtils.fromJson( input, Message.class );
+        postObject( response, request, message );
+    }
+
+    public void postObject( HttpServletResponse response, HttpServletRequest request, Message message )
+    {
         List<User> users = new ArrayList<User>( message.getUsers() );
         message.getUsers().clear();
 
+        for ( OrganisationUnit ou : message.getOrganisationUnits() )
+        {
+            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( ou.getUid() );
+
+            if ( organisationUnit == null )
+            {
+                ContextUtils.conflictResponse( response, "Organisation Unit does not exist: " + ou.getUid() );
+                return;
+            }
+
+            message.getUsers().addAll( organisationUnit.getUsers() );
+        }
+
         for ( User u : users )
         {
-            User user  = userService.getUser( u.getUid() );
-            
+            User user = userService.getUser( u.getUid() );
+
             if ( user == null )
             {
                 ContextUtils.conflictResponse( response, "User does not exist: " + u.getUid() );
                 return;
             }
-            
+
             message.getUsers().add( user );
         }
 
+        for ( UserGroup ug : message.getUserGroups() )
+        {
+            UserGroup userGroup = userGroupService.getUserGroup( ug.getUid() );
+
+            if ( userGroup == null )
+            {
+                ContextUtils.conflictResponse( response, "User Group does not exist: " + ug.getUid() );
+                return;
+            }
+
+            message.getUsers().addAll( userGroup.getMembers() );
+        }
+
         String metaData = MessageService.META_USER_AGENT + request.getHeader( ContextUtils.HEADER_USER_AGENT );
 
+        System.err.println( message );
+
+        /*
         int id = messageService.sendMessage( message.getSubject(), message.getText(), metaData, message.getUsers() );
         MessageConversation m = messageService.getMessageConversation( id );
 
         response.setStatus( HttpServletResponse.SC_CREATED );
         response.setHeader( "Location", MessageConversationController.RESOURCE_PATH + "/" + m.getUid() );
+        */
     }
 
     //--------------------------------------------------------------------------
@@ -132,19 +184,19 @@
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.POST )
     public void postMessageConversationReply( @PathVariable( "uid" ) String uid, @RequestBody String body,
-        HttpServletRequest request, HttpServletResponse response ) throws Exception
+                                              HttpServletRequest request, HttpServletResponse response ) throws Exception
     {
         String metaData = MessageService.META_USER_AGENT + request.getHeader( ContextUtils.HEADER_USER_AGENT );
 
         MessageConversation messageConversation = messageService.getMessageConversation( uid );
-        
+
         if ( messageConversation == null )
         {
             ContextUtils.conflictResponse( response, "Message conversation does not exist: " + uid );
             return;
         }
 
-        messageService.sendReply( messageConversation, body, metaData );        
+        messageService.sendReply( messageConversation, body, metaData );
 
         response.setStatus( HttpServletResponse.SC_CREATED );
     }
@@ -155,12 +207,12 @@
 
     @RequestMapping( value = "/feedback", method = RequestMethod.POST )
     public void postMessageConversationFeedback( @RequestParam( "subject" ) String subject, @RequestBody String body,
-        HttpServletRequest request, HttpServletResponse response ) throws Exception
+                                                 HttpServletRequest request, HttpServletResponse response ) throws Exception
     {
         String metaData = MessageService.META_USER_AGENT + request.getHeader( ContextUtils.HEADER_USER_AGENT );
 
         messageService.sendFeedback( subject, body, metaData );
-        
+
         response.setStatus( HttpServletResponse.SC_CREATED );
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/new-message.vm'
--- dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/new-message.vm	2012-10-01 15:11:04 +0000
+++ dhis-2/dhis-web/dhis-web-mobile/src/main/webapp/WEB-INF/dhis-web-mobile-velocity/new-message.vm	2012-10-01 16:36:21 +0000
@@ -14,7 +14,8 @@
         var u_count = 0;
         var ug_count = 0;
 
-        _( _(selected).keys() ).each(function(item) {
+        _( _(selected).keys() ).each(function(item)
+        {
             if( item.indexOf( 'ou:') != -1 )
             {
                 ou_count++;
@@ -57,8 +58,8 @@
 
     function addRecipient(e)
     {
-        var prefix = $(e).data('prefix');
-        var id = $(e).data('id');
+        var prefix = $(e).jqmData('prefix');
+        var id = $(e).jqmData('id');
 
         if( prefix === undefined || id === undefined )
         {
@@ -68,11 +69,6 @@
         selected[prefix + ':' + id] = $(e).find('a').html();
     }
 
-    function removeRecipient(e)
-    {
-        console.log(e);
-    }
-
     function manageRecipientsPage()
     {
         var fn1 = $('form[role="search"] input:text').data('events')['keyup'][1];