dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #15109
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5327: Implemented first cut of SmsMessageSender (WIP)
------------------------------------------------------------
revno: 5327
committer: Jo Størset <storset@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2011-12-07 22:06:08 +0100
message:
Implemented first cut of SmsMessageSender (WIP)
added:
dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/SmsMessageSender.java
dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/TestOutboundSmsService.java
dhis-2/dhis-services/dhis-service-mobile/src/test/java/org/hisp/dhis/api/
dhis-2/dhis-services/dhis-service-mobile/src/test/java/org/hisp/dhis/api/sms/
dhis-2/dhis-services/dhis-service-mobile/src/test/java/org/hisp/dhis/api/sms/SmsMessageSenderTest.java
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java
dhis-2/dhis-services/dhis-service-mobile/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/UserSettingService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java 2011-10-07 11:48:18 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserSettingService.java 2011-12-07 21:06:08 +0000
@@ -49,6 +49,7 @@
final String KEY_STYLE = "stylesheet";
final String KEY_STYLE_DIRECTORY = "stylesheetDirectory";
final String KEY_MESSAGE_EMAIL_NOTIFICATION = "keyMessageEmailNotification";
+ final String KEY_MESSAGE_SMS_NOTIFICATION = "keyMessageSmsNotification";
final int DEFAULT_CHARTS_IN_DASHBOARD = 4;
final List<Integer> DASHBOARD_CHARTS_TO_DISPLAY = Arrays.asList( 4, 6, 8 );
=== added file 'dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/SmsMessageSender.java'
--- dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/SmsMessageSender.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/SmsMessageSender.java 2011-12-07 21:06:08 +0000
@@ -0,0 +1,122 @@
+package org.hisp.dhis.mobile.service;
+
+/*
+ * Copyright (c) 2011, 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.user.UserSettingService.KEY_MESSAGE_SMS_NOTIFICATION;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.api.sms.OutboundSmsService;
+import org.hisp.dhis.message.MessageSender;
+import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class SmsMessageSender
+ implements MessageSender
+{
+ private static final Log log = LogFactory.getLog( SmsMessageSender.class );
+
+ // -------------------------------------------------------------------------
+ // Dependencies
+ // -------------------------------------------------------------------------
+
+ private UserService userService;
+
+ private OutboundSmsService outboundSmsService;
+
+ @Autowired
+ public void setUserService( UserService userService )
+ {
+ this.userService = userService;
+ }
+
+ @Autowired( required = false )
+ public void setOutboundSmsService( OutboundSmsService outboundSmsService )
+ {
+ this.outboundSmsService = outboundSmsService;
+ }
+
+
+ // -------------------------------------------------------------------------
+ // MessageSender implementation
+ // -------------------------------------------------------------------------
+
+
+ @Override
+ public void sendMessage( String subject, String text, User sender, Set<User> users )
+ {
+
+ if ( outboundSmsService == null || !outboundSmsService.isSmsServiceAvailable() )
+ {
+ return;
+ }
+
+ String name = "unknown";
+ if ( sender != null )
+ name = sender.getUsername();
+
+ text = "From " + name + ", " + subject + ": " + text;
+
+ // Simplistic cutoff 160 characters..
+ int length = text.length();
+ if ( length > 160 )
+ text = text.substring( 0, 157 ) + "...";
+
+ Map<User, Serializable> settings = userService.getUserSettings( KEY_MESSAGE_SMS_NOTIFICATION, false );
+
+ List<String> recipients = new ArrayList<String>();
+
+ for ( User user : users )
+ {
+ boolean smsNotification = settings.get( user ) != null && (Boolean) settings.get( user );
+ System.out.println(smsNotification);
+
+ String phoneNumber = user.getPhoneNumber();
+ if ( smsNotification && phoneNumber != null && !phoneNumber.trim().isEmpty() )
+ {
+ recipients.add( phoneNumber );
+
+ log.debug( "Adding user as sms recipient: " + user + " with phone number: " + phoneNumber );
+ }
+ }
+
+ if ( !recipients.isEmpty() )
+ {
+ outboundSmsService.sendMessage( text, recipients.toArray( new String[recipients.size()] ) );
+ }
+
+ }
+
+}
=== added file 'dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/TestOutboundSmsService.java'
--- dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/TestOutboundSmsService.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/java/org/hisp/dhis/mobile/service/TestOutboundSmsService.java 2011-12-07 21:06:08 +0000
@@ -0,0 +1,37 @@
+package org.hisp.dhis.mobile.service;
+
+import java.net.URL;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.api.sms.OutboundSmsService;
+import org.hisp.dhis.api.sms.SmsServiceException;
+
+public class TestOutboundSmsService
+ implements OutboundSmsService
+{
+
+ private static final Log log = LogFactory.getLog( TestOutboundSmsService.class );
+
+ @Override
+ public boolean isSmsServiceAvailable()
+ {
+ log.info( "Is service is available?" );
+ return true;
+ }
+
+ @Override
+ public void sendMessage( String message, String... recipients )
+ throws SmsServiceException
+ {
+ log.info( "Send message '" + message + "' to " + recipients);
+ }
+
+ @Override
+ public void sendOtaMessage( URL url, String prompt, String... recipients )
+ throws SmsServiceException
+ {
+ log.info( "Send OTA message '" + prompt + "', url " + url + " to " + recipients);
+ }
+
+}
=== modified file 'dhis-2/dhis-services/dhis-service-mobile/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-mobile/src/main/resources/META-INF/dhis/beans.xml 2011-11-14 12:19:36 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/main/resources/META-INF/dhis/beans.xml 2011-12-07 21:06:08 +0000
@@ -45,4 +45,12 @@
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
+ <!-- SMS Message sender -->
+
+ <bean id="org.hisp.dhis.mobile.service.SmsMessageSender"" class="org.hisp.dhis.mobile.service.SmsMessageSender" />
+
+ <!-- OutboundSmsService stub just logging invocations, only to be used for testing! -->
+
+ <!-- <bean id="org.hisp.dhis.mobile.service.TestOuboundSmsService" class="org.hisp.dhis.mobile.service.TestOuboundSmsService" /> -->
+
</beans>
=== added directory 'dhis-2/dhis-services/dhis-service-mobile/src/test/java/org/hisp/dhis/api'
=== added directory 'dhis-2/dhis-services/dhis-service-mobile/src/test/java/org/hisp/dhis/api/sms'
=== added file 'dhis-2/dhis-services/dhis-service-mobile/src/test/java/org/hisp/dhis/api/sms/SmsMessageSenderTest.java'
--- dhis-2/dhis-services/dhis-service-mobile/src/test/java/org/hisp/dhis/api/sms/SmsMessageSenderTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-mobile/src/test/java/org/hisp/dhis/api/sms/SmsMessageSenderTest.java 2011-12-07 21:06:08 +0000
@@ -0,0 +1,67 @@
+package org.hisp.dhis.api.sms;
+
+import static org.hisp.dhis.user.UserSettingService.KEY_MESSAGE_SMS_NOTIFICATION;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+
+import org.hisp.dhis.mobile.service.SmsMessageSender;
+import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserService;
+import org.junit.Test;
+
+public class SmsMessageSenderTest
+{
+
+ SmsMessageSender smsMessageSender;
+
+ @Test
+ public void testMessageSender()
+ {
+
+ smsMessageSender = new SmsMessageSender();
+
+ OutboundSmsService outboundSmsService = mock( OutboundSmsService.class );
+ when( outboundSmsService.isSmsServiceAvailable() ).thenReturn( true );
+
+ UserService userService = mock( UserService.class );
+ final User user = getUser();
+ Map<User, Serializable> settings = getUserSettings( user );
+ when( userService.getUserSettings( KEY_MESSAGE_SMS_NOTIFICATION, false ) ).thenReturn( settings );
+
+ smsMessageSender.setOutboundSmsService( outboundSmsService );
+ smsMessageSender.setUserService( userService );
+ smsMessageSender.sendMessage( "Hello", "hello", user, getUserSet( user ));
+
+ verify(outboundSmsService).isSmsServiceAvailable();
+ verify( userService ).getUserSettings( KEY_MESSAGE_SMS_NOTIFICATION, false );
+ verify( outboundSmsService ).sendMessage( eq("From null, Hello: hello"), eq("222222") );
+ }
+
+ private HashSet<User> getUserSet( final User user )
+ {
+ return new HashSet<User>(){{add( user );}};
+ }
+
+ private Map<User, Serializable> getUserSettings( final User user )
+ {
+ Map<User, Serializable> settings = new HashMap<User, Serializable>(){{put(user, true);}};
+ return settings;
+ }
+
+ private User getUser()
+ {
+ final User user = new User();
+ user.setId( 1 );
+ user.setPhoneNumber( "222222" );
+ user.setFirstName( "firstName" );
+ user.setSurname( "surname" );
+ return user;
+ }
+}