← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4786: Improved messaging, added support for sending message to the email address of recipients. Made Dh...

 

------------------------------------------------------------
revno: 4786
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-09-30 19:01:57 +0200
message:
  Improved messaging, added support for sending message to the email address of recipients. Made DhisSpringTest also look for a beans-test.xml location for the context, makes it easier to stub (replace) proxied beans during test phase. Added an interface MessageSender which can be implemented and easily hooked into MessageService for new types, for instance SMS.
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageSender.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MockMessageSender.java
  dhis-2/dhis-services/dhis-service-core/src/test/resources/META-INF/
  dhis-2/dhis-services/dhis-service-core/src/test/resources/META-INF/dhis/
  dhis-2/dhis-services/dhis-service-core/src/test/resources/META-INF/dhis/beans-test.xml
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageConversation.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/options/SystemSettingManager.java
  dhis-2/dhis-services/dhis-service-core/pom.xml
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MessageServiceTest.java
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisSpringTest.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/message/MessageConversation.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageConversation.java	2011-08-04 10:45:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageConversation.java	2011-09-30 17:01:57 +0000
@@ -145,6 +145,18 @@
         }
     }
     
+    public Set<User> getUsers()
+    {
+        Set<User> users = new HashSet<User>();
+        
+        for ( UserMessage userMessage : userMessages )
+        {
+            users.add( userMessage.getUser() );
+        }
+        
+        return users;
+    }
+    
     public String getLastSenderName()
     {
         return lastSenderFirstname + " " + lastSenderSurname;

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageSender.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageSender.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/message/MessageSender.java	2011-09-30 17:01:57 +0000
@@ -0,0 +1,46 @@
+package org.hisp.dhis.message;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.Set;
+
+import org.hisp.dhis.user.User;
+
+/**
+ * @author Lars Helge Overland
+ */
+public interface MessageSender
+{
+    /**
+     * Sends a message. The given message will be sent to the given set of users.
+     * 
+     * @param message the message to send.
+     * @param users the users to send the message to.
+     */
+    void sendMessage( String subject, String text, Set<User> users );
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/options/SystemSettingManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/options/SystemSettingManager.java	2011-09-30 13:18:24 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/options/SystemSettingManager.java	2011-09-30 17:01:57 +0000
@@ -57,6 +57,9 @@
     final String KEY_PATIENT_EXCEL_TEMPLATE_FILE_NAME = "patientExcelTemplateFileName";
     final String KEY_DATAMART_TASK = "keyDataMartTask";
     final String KEY_DATASETCOMPLETENESS_TASK = "keyDataSetCompletenessTask";
+    final String KEY_EMAIL_HOST_NAME = "keyEmailHostName";
+    final String KEY_EMAIL_USERNAME = "keyEmailUsername";
+    final String KEY_EMAIL_PASSWORD = "keyEmailPassword";
     
     final int DEFAULT_MAX_NUMBER_OF_ATTEMPTS = 20;
     final int DEFAULT_TIMEFRAME_MINUTES = 1;

=== modified file 'dhis-2/dhis-services/dhis-service-core/pom.xml'
--- dhis-2/dhis-services/dhis-service-core/pom.xml	2011-08-28 17:52:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/pom.xml	2011-09-30 17:01:57 +0000
@@ -57,6 +57,10 @@
       <artifactId>commons-codec</artifactId>
     </dependency>
     <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-email</artifactId>
+    </dependency>
+    <dependency>
       <groupId>commons-collections</groupId>
       <artifactId>commons-collections</artifactId>
     </dependency>

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java	2011-08-04 10:45:31 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/DefaultMessageService.java	2011-09-30 17:01:57 +0000
@@ -68,7 +68,14 @@
     {
         this.configurationService = configurationService;
     }
+    
+    private List<MessageSender> messageSenders;
 
+    public void setMessageSenders( List<MessageSender> messageSenders )
+    {
+        this.messageSenders = messageSenders;
+    }
+    
     // -------------------------------------------------------------------------
     // MessageService implementation
     // -------------------------------------------------------------------------
@@ -101,7 +108,11 @@
             conversation.addUserMessage( new UserMessage( user ) );        
         }
         
-        return saveMessageConversation( conversation );
+        int id = saveMessageConversation( conversation );
+        
+        invokeMessageSenders( subject, text, users );
+        
+        return id;
     }
 
     public int sendFeedback( String subject, String text )
@@ -117,7 +128,9 @@
         
         conversation.markReplied( sender, message );
         
-        updateMessageConversation( conversation );        
+        updateMessageConversation( conversation );
+        
+        invokeMessageSenders( conversation.getSubject(), text, conversation.getUsers() );
     }
         
     public int saveMessageConversation( MessageConversation conversation )
@@ -149,4 +162,16 @@
     {
         return messageConversationStore.getMessageConversations( currentUserService.getCurrentUser(), first, max );
     }
+
+    // -------------------------------------------------------------------------
+    // Supportive methods
+    // -------------------------------------------------------------------------
+
+    private void invokeMessageSenders( String subject, String text, Set<User> users )
+    {
+        for ( MessageSender sender : messageSenders )
+        {
+            sender.sendMessage( subject, text, users );
+        }
+    }
 }

=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java	2011-09-30 17:01:57 +0000
@@ -0,0 +1,114 @@
+package org.hisp.dhis.message;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static org.hisp.dhis.options.SystemSettingManager.KEY_EMAIL_HOST_NAME;
+import static org.hisp.dhis.options.SystemSettingManager.KEY_EMAIL_PASSWORD;
+import static org.hisp.dhis.options.SystemSettingManager.KEY_EMAIL_USERNAME;
+
+import java.util.Set;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.mail.DefaultAuthenticator;
+import org.apache.commons.mail.Email;
+import org.apache.commons.mail.EmailException;
+import org.apache.commons.mail.SimpleEmail;
+import org.hisp.dhis.options.SystemSettingManager;
+import org.hisp.dhis.user.User;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class EmailMessageSender
+    implements MessageSender
+{
+    private static final int SMTP_PORT = 587;
+    private static final String FROM_ADDRESS = "noreply@xxxxxxxxx";
+
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private SystemSettingManager systemSettingManager;
+    
+    public void setSystemSettingManager( SystemSettingManager systemSettingManager )
+    {
+        this.systemSettingManager = systemSettingManager;
+    }
+    
+    // -------------------------------------------------------------------------
+    // MessageSender implementation
+    // -------------------------------------------------------------------------
+
+    @Override
+    public void sendMessage( String subject, String text, Set<User> users )
+    {
+        String hostName = StringUtils.trimToNull( (String) systemSettingManager.getSystemSetting( KEY_EMAIL_HOST_NAME ) );
+        String username = StringUtils.trimToNull( (String) systemSettingManager.getSystemSetting( KEY_EMAIL_USERNAME ) );
+        String password = StringUtils.trimToNull( (String) systemSettingManager.getSystemSetting( KEY_EMAIL_PASSWORD ) );
+        
+        if ( hostName == null || username == null || password == null )
+        {
+            return;
+        }
+        
+        for ( User user : users )
+        {
+            try
+            {
+                String toAddress = StringUtils.trimToNull( user.getEmail() );
+                
+                if ( user.getEmail() != null )
+                {
+                    Email email = getEmail( hostName, username, password );
+                    email.setSubject( subject );
+                    email.setMsg( text );
+                    email.addTo( toAddress );
+                    email.send();
+                }
+            }
+            catch ( EmailException ex )
+            {
+                throw new RuntimeException( ex );
+            }
+        }
+    }
+
+    private Email getEmail( String hostName, String username, String password )
+        throws EmailException
+    {
+        Email email = new SimpleEmail();
+        email.setHostName( hostName );
+        email.setSmtpPort( SMTP_PORT );
+        email.setAuthenticator( new DefaultAuthenticator( username, password ) );
+        email.setTLS( true );
+        email.setFrom( FROM_ADDRESS );
+        
+        return email;
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2011-09-30 13:48:31 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2011-09-30 17:01:57 +0000
@@ -444,6 +444,15 @@
     <property name="messageConversationStore" ref="org.hisp.dhis.message.MessageConversationStore" />
     <property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
     <property name="configurationService" ref="org.hisp.dhis.configuration.ConfigurationService" />
+	<property name="messageSenders">
+	  <list>
+		<ref bean="emailMessageSender"/>
+	  </list>
+	</property>
+  </bean>
+
+  <bean id="emailMessageSender" class="org.hisp.dhis.message.EmailMessageSender">
+	<property name="systemSettingManager" ref="org.hisp.dhis.options.SystemSettingManager" />
   </bean>
 
   <bean id="org.hisp.dhis.concept.ConceptService" class="org.hisp.dhis.concept.DefaultConceptService">

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MessageServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MessageServiceTest.java	2011-08-04 10:06:15 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MessageServiceTest.java	2011-09-30 17:01:57 +0000
@@ -37,6 +37,7 @@
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserService;
+import org.junit.Before;
 import org.junit.Test;
 
 /**
@@ -50,16 +51,16 @@
     private User userB;
 
     private Set<User> users;
-
+    
     // -------------------------------------------------------------------------
     // Fixture
     // -------------------------------------------------------------------------
 
-    @Override
+    @Before
     public void setUpTest()
     {
+        messageService = (MessageService) getBean( MessageService.ID );
         userService = (UserService) getBean( UserService.ID );
-        messageService = (MessageService) getBean( MessageService.ID );
         
         sender = createUser( 'S' );
         userA = createUser( 'A' );

=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MockMessageSender.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MockMessageSender.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/message/MockMessageSender.java	2011-09-30 17:01:57 +0000
@@ -0,0 +1,42 @@
+package org.hisp.dhis.message;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.Set;
+
+import org.hisp.dhis.user.User;
+
+public class MockMessageSender
+    implements MessageSender
+{
+    @Override
+    public void sendMessage( String subject, String text, Set<User> users )
+    {
+        // Do nothing
+    }
+}

=== added directory 'dhis-2/dhis-services/dhis-service-core/src/test/resources/META-INF'
=== added directory 'dhis-2/dhis-services/dhis-service-core/src/test/resources/META-INF/dhis'
=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/resources/META-INF/dhis/beans-test.xml'
--- dhis-2/dhis-services/dhis-service-core/src/test/resources/META-INF/dhis/beans-test.xml	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/resources/META-INF/dhis/beans-test.xml	2011-09-30 17:01:57 +0000
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="
+http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd";>
+
+  <bean id="emailMessageSender" class="org.hisp.dhis.message.MockMessageSender"/>
+
+</beans>

=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisSpringTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisSpringTest.java	2010-05-17 19:31:07 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisSpringTest.java	2011-09-30 17:01:57 +0000
@@ -43,7 +43,7 @@
  * @version $Id: DhisSpringTest.java 6335 2008-11-20 11:11:26Z larshelg $
  */
 @RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations={"classpath*:/META-INF/dhis/beans.xml"})
+@ContextConfiguration(locations={"classpath*:/META-INF/dhis/beans.xml","classpath*:/META-INF/dhis/beans-test.xml"})
 @Transactional
 public abstract class DhisSpringTest
     extends DhisConvenienceTest implements ApplicationContextAware
@@ -52,7 +52,7 @@
     // ApplicationContextAware implementation
     // -------------------------------------------------------------------------
 
-    private ApplicationContext context;
+    protected ApplicationContext context;
 
     public void setApplicationContext( ApplicationContext context )
     {