← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5458: finished messageconversation for now (webapi)

 

------------------------------------------------------------
revno: 5458
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-12-16 17:55:21 +0100
message:
  finished messageconversation for now (webapi)
added:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/Message.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/UserXmlAdapter.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/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-api/src/main/java/org/hisp/dhis/common/adapter/UserXmlAdapter.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/UserXmlAdapter.java	2011-12-13 18:41:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/UserXmlAdapter.java	2011-12-16 16:55:21 +0000
@@ -47,7 +47,8 @@
 
         user.setUid( identifiableObject.getUid() );
         user.setLastUpdated( identifiableObject.getLastUpdated() );
-        user.setName( identifiableObject.getName() == null ? UUID.randomUUID().toString() : identifiableObject.getName() );
+        user.setFirstName( UUID.randomUUID().toString() );
+        user.setSurname( UUID.randomUUID().toString() );
 
         return user;
     }

=== 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	2011-12-16 13:38:59 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/MessageConversationController.java	2011-12-16 16:55:21 +0000
@@ -27,25 +27,29 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.apache.commons.io.IOUtils;
 import org.hisp.dhis.api.utils.IdentifiableObjectParams;
 import org.hisp.dhis.api.utils.WebLinkPopulator;
+import org.hisp.dhis.api.view.Jaxb2Utils;
+import org.hisp.dhis.api.webdomain.Message;
 import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.message.MessageConversations;
 import org.hisp.dhis.message.MessageService;
+import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserService;
+import org.hisp.dhis.util.ContextUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
-import org.springframework.web.HttpRequestMethodNotSupportedException;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.xml.bind.JAXBException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -59,6 +63,9 @@
     @Autowired
     private MessageService messageService;
 
+    @Autowired
+    private UserService userService;
+
     //-------------------------------------------------------------------------------------------------------
     // GET
     //-------------------------------------------------------------------------------------------------------
@@ -97,16 +104,66 @@
     }
 
     //-------------------------------------------------------------------------------------------------------
-    // POST
+    // POST for new MessageConversation
     //-------------------------------------------------------------------------------------------------------
 
     @RequestMapping( method = RequestMethod.POST, headers = {"Content-Type=application/xml, text/xml"} )
-    public void postMessageConversationXML( HttpServletResponse response, InputStream input ) throws Exception
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_SEND_MESSAGE')" )
+    public void postMessageConversationXML( HttpServletResponse response, HttpServletRequest request, InputStream input ) throws JAXBException
     {
+        Message message = Jaxb2Utils.unmarshal( Message.class, input );
+
+        List<User> users = new ArrayList<User>( message.getUsers() );
+        message.getUsers().clear();
+
+        for ( User user : users )
+        {
+            user = userService.getUser( user.getUid() );
+            message.getUsers().add( user );
+        }
+
+        String metaData = MessageService.META_USER_AGENT + request.getHeader( ContextUtils.HEADER_USER_AGENT );
+
+        int id = messageService.sendMessage( message.getSubject(), message.getText(), metaData, message.getUsers() );
+        MessageConversation m = messageService.getMessageConversation( id );
+
+        System.err.println( "uid: " + m.getUid() );
+
+        response.setStatus( HttpServletResponse.SC_CREATED );
+        response.setHeader( "Location", DataElementController.RESOURCE_PATH + "/" + m.getUid() );
     }
 
     @RequestMapping( method = RequestMethod.POST, headers = {"Content-Type=application/json"} )
+    @PreAuthorize( "hasRole('ALL') or hasRole('F_SEND_MESSAGE')" )
     public void postMessageConversationJSON( HttpServletResponse response, InputStream input ) throws Exception
     {
     }
+
+    //-------------------------------------------------------------------------------------------------------
+    // POST for reply on existing MessageConversation
+    //-------------------------------------------------------------------------------------------------------
+
+    @RequestMapping( value = "/{uid}", method = RequestMethod.POST )
+    public void postMessageConversationReply( @PathVariable( "uid" ) String uid, @RequestBody String body,
+                                              HttpServletRequest request, HttpServletResponse response ) throws Exception
+    {
+        String metaData = MessageService.META_USER_AGENT + request.getHeader( ContextUtils.HEADER_USER_AGENT );
+
+        MessageConversation messageConversation = messageService.getMessageConversation( uid );
+
+        messageService.sendReply( messageConversation, body, metaData );
+    }
+
+    //-------------------------------------------------------------------------------------------------------
+    // POST for feedback
+    //-------------------------------------------------------------------------------------------------------
+
+    @RequestMapping( value = "/feedback", method = RequestMethod.POST )
+    public void postMessageConversationFeedback( @RequestParam( "subject" ) String subject, @RequestBody String body,
+                                                 HttpServletRequest request, HttpServletResponse response ) throws Exception
+    {
+        String metaData = MessageService.META_USER_AGENT + request.getHeader( ContextUtils.HEADER_USER_AGENT );
+
+        messageService.sendFeedback( subject, body, metaData );
+    }
 }

=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/Message.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/Message.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/Message.java	2011-12-16 16:55:21 +0000
@@ -0,0 +1,66 @@
+package org.hisp.dhis.api.webdomain;
+
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+import org.hisp.dhis.common.BaseIdentifiableObject;
+import org.hisp.dhis.common.Dxf2Namespace;
+import org.hisp.dhis.common.adapter.UserXmlAdapter;
+import org.hisp.dhis.user.User;
+
+import javax.xml.bind.annotation.*;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@XmlRootElement( name = "message", namespace = Dxf2Namespace.NAMESPACE )
+@XmlAccessorType( value = XmlAccessType.NONE )
+public class Message
+{
+    private String subject;
+
+    private String text;
+
+    private Set<User> users = new HashSet<User>();
+
+    @XmlElement
+    @JsonProperty
+    public String getSubject()
+    {
+        return subject;
+    }
+
+    public void setSubject( String subject )
+    {
+        this.subject = subject;
+    }
+
+    @XmlElement
+    @JsonProperty
+    public String getText()
+    {
+        return text;
+    }
+
+    public void setText( String text )
+    {
+        this.text = text;
+    }
+
+    @XmlElementWrapper( name = "users" )
+    @XmlElement( name = "user" )
+    @XmlJavaTypeAdapter( UserXmlAdapter.class )
+    @JsonProperty
+    @JsonSerialize( contentAs = BaseIdentifiableObject.class )
+    public Set<User> getUsers()
+    {
+        return users;
+    }
+
+    public void setUsers( Set<User> users )
+    {
+        this.users = users;
+    }
+}