← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 4803: Improved EmailMessageSender, controles which users will receive email notifications based on a us...

 

------------------------------------------------------------
revno: 4803
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2011-10-03 12:33:46 +0200
message:
  Improved EmailMessageSender, controles which users will receive email notifications based on a user setting
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.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/main/java/org/hisp/dhis/user/DefaultUserService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml


--
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/user/UserService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java	2011-09-09 06:18:32 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java	2011-10-03 10:33:46 +0000
@@ -1,6 +1,8 @@
 package org.hisp.dhis.user;
 
+import java.io.Serializable;
 import java.util.Collection;
+import java.util.Map;
 
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -316,5 +318,15 @@
      * 
      * @param userSetting the UserSetting to delete.
      */
-    void deleteUserSetting( UserSetting userSetting );    
+    void deleteUserSetting( UserSetting userSetting );
+
+    /**
+     * Returns a Map with an entry for all UserSettings with the given name where
+     * the key is the user and the value is the value of the user setting.
+     * 
+     * @param name the name of the UserSetting.
+     * @param defaultValue the value to return if the UserSetting value is null.  
+     * @return a Map.
+     */
+    Map<User,Serializable> getUserSettings( String name, Serializable defaultValue );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java	2011-09-30 12:21:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java	2011-10-03 10:33:46 +0000
@@ -48,7 +48,7 @@
     final String KEY_CURRENT_DATADICTIONARY = "currentDataDictionary";
     final String KEY_STYLE = "stylesheet";
     final String KEY_STYLE_DIRECTORY = "stylesheetDirectory";
-
+    final String KEY_MESSAGE_EMAIL_NOTIFICATION = "keyMessageEmailNotification";
 
     final int DEFAULT_CHARTS_IN_DASHBOARD = 4;
     final List<Integer> DASHBOARD_CHARTS_TO_DISPLAY = Arrays.asList( 4, 6, 8 );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java	2011-10-03 09:15:35 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java	2011-10-03 10:33:46 +0000
@@ -273,4 +273,12 @@
      * @param userSetting the UserSetting to delete.
      */
     void deleteUserSetting( UserSetting userSetting );
+    
+    /**
+     * Returns all UserSettings with the given name.
+     * 
+     * @param name the name.
+     * @return a Collection of UserSettings.
+     */
+    Collection<UserSetting> getUserSettings( String name );
 }

=== modified 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	2011-09-30 17:01:57 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java	2011-10-03 10:33:46 +0000
@@ -30,16 +30,22 @@
 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 static org.hisp.dhis.user.UserSettingService.*;
 
+import java.io.Serializable;
+import java.util.Map;
 import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 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;
+import org.hisp.dhis.user.UserService;
 
 /**
  * @author Lars Helge Overland
@@ -47,6 +53,8 @@
 public class EmailMessageSender
     implements MessageSender
 {
+    private static final Log log = LogFactory.getLog( EmailMessageSender.class );
+    
     private static final int SMTP_PORT = 587;
     private static final String FROM_ADDRESS = "noreply@xxxxxxxxx";
 
@@ -61,6 +69,13 @@
         this.systemSettingManager = systemSettingManager;
     }
     
+    private UserService userService;
+
+    public void setUserService( UserService userService )
+    {
+        this.userService = userService;
+    }
+
     // -------------------------------------------------------------------------
     // MessageSender implementation
     // -------------------------------------------------------------------------
@@ -77,24 +92,26 @@
             return;
         }
         
+        Map<User,Serializable> settings = userService.getUserSettings( KEY_MESSAGE_EMAIL_NOTIFICATION, false );
+        
         for ( User user : users )
         {
-            try
+            if ( (Boolean) settings.get( user ) == new Boolean( true ) && user.getEmail() != null && !user.getEmail().isEmpty() )
             {
-                String toAddress = StringUtils.trimToNull( user.getEmail() );
-                
-                if ( user.getEmail() != null )
+                try
                 {
+                    String toAddress = StringUtils.trimToNull( user.getEmail() );
+                    
                     Email email = getEmail( hostName, username, password );
                     email.setSubject( subject );
                     email.setMsg( text );
                     email.addTo( toAddress );
                     email.send();
                 }
-            }
-            catch ( EmailException ex )
-            {
-                throw new RuntimeException( ex );
+                catch ( EmailException ex )
+                {
+                    log.warn( "Could not send email to user: " + user + " with email address: " + user.getEmail() );
+                }
             }
         }
     }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java	2011-10-03 09:15:35 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserService.java	2011-10-03 10:33:46 +0000
@@ -1,8 +1,11 @@
 package org.hisp.dhis.user;
 
+import java.io.Serializable;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -442,4 +445,16 @@
     {
         return userStore.getUserSetting( user, name );
     }
+    
+    public Map<User,Serializable> getUserSettings( String name, Serializable defaultValue )
+    {
+        Map<User,Serializable> map = new HashMap<User, Serializable>();
+        
+        for ( UserSetting setting : userStore.getUserSettings( name ) )
+        {
+            map.put( setting.getUser(), setting.getValue() != null ? setting.getValue() : defaultValue );
+        }
+        
+        return map;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java	2011-10-03 09:15:35 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java	2011-10-03 10:33:46 +0000
@@ -419,14 +419,22 @@
     public Collection<UserSetting> getAllUserSettings( User user )
     {
         Session session = sessionFactory.getCurrentSession();
-
         Query query = session.createQuery( "from UserSetting us where us.user = :user" );
-
         query.setEntity( "user", user );
 
         return query.list();
     }
 
+    @SuppressWarnings( "unchecked" )
+    public Collection<UserSetting> getUserSettings( String name )
+    {
+        Session session = sessionFactory.getCurrentSession();
+        Query query = session.createQuery( "from UserSetting us where us.name = :name" );
+        query.setEntity( "name", name );
+
+        return query.list();
+    }
+
     public void deleteUserSetting( UserSetting userSetting )
     {
         Session session = sessionFactory.getCurrentSession();

=== 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-10-03 09:23:01 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2011-10-03 10:33:46 +0000
@@ -448,6 +448,7 @@
 
   <bean id="emailMessageSender" class="org.hisp.dhis.message.EmailMessageSender">
 	<property name="systemSettingManager" ref="org.hisp.dhis.options.SystemSettingManager" />
+	<property name="userService" ref="org.hisp.dhis.user.UserService" />
   </bean>
 
   <bean id="org.hisp.dhis.concept.ConceptService" class="org.hisp.dhis.concept.DefaultConceptService">