← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22150: Async support added for sms delivery

 

------------------------------------------------------------
revno: 22150
committer: Zubair Asghar<zubair@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2016-03-07 14:15:24 +0100
message:
  Async support added for sms delivery
added:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundNotification.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/SmsSender.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsTransportService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DefaultSmsSender.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundSmsService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundSmsTransportService.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/sms/SmsController.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/sms/SmsSender.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/SmsSender.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/SmsSender.java	2016-03-07 13:15:24 +0000
@@ -45,4 +45,6 @@
         throws SmsServiceException;
 
     String sendMessage( String subject, String text, User sender, List<User> users, boolean forceSend );
+    
+    boolean sendAyncMessages( OutboundSms sms );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsService.java	2016-03-07 13:15:24 +0000
@@ -30,6 +30,8 @@
 
 import java.util.List;
 
+import org.smslib.OutboundMessage;
+
 public interface OutboundSmsService
 {
     String ID = OutboundSmsService.class.getName();
@@ -49,4 +51,6 @@
     List<OutboundSms> getOutboundSms( OutboundSmsStatus status, Integer min, Integer max );
 
     OutboundSms getOutboundSms( int id );
+    
+    OutboundSms convertToOutboundSms (OutboundMessage sms);
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsTransportService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsTransportService.java	2016-01-07 15:15:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/outbound/OutboundSmsTransportService.java	2016-03-07 13:15:24 +0000
@@ -71,4 +71,6 @@
 
     String sendMessage( OutboundSms sms, String gatewayId )
         throws SmsServiceException;
+    
+    boolean sendAyncMessages(OutboundSms sms, String gatewayId);
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DefaultSmsSender.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DefaultSmsSender.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DefaultSmsSender.java	2016-03-07 13:15:24 +0000
@@ -203,6 +203,19 @@
         return message;
     }
 
+    @Transactional
+    @Override
+    public boolean sendAyncMessages( OutboundSms sms )
+        throws SmsServiceException
+    {
+        if ( transportService == null || !transportService.isEnabled() )
+        {
+            throw new SmsServiceNotEnabledException();
+        }
+
+        return transportService.sendAyncMessages( sms, transportService.getDefaultGateway() );
+    }
+
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------

=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundNotification.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundNotification.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundNotification.java	2016-03-07 13:15:24 +0000
@@ -0,0 +1,82 @@
+package org.hisp.dhis.sms.outbound;
+
+/*
+ * Copyright (c) 2004-2016, 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 org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.smslib.AGateway;
+import org.smslib.IOutboundMessageNotification;
+import org.smslib.OutboundMessage;
+import org.smslib.OutboundMessage.MessageStatuses;
+import org.springframework.beans.factory.annotation.Autowired;
+
+public class DefaultOutboundNotification
+    implements IOutboundMessageNotification
+{
+
+    private static final Log log = LogFactory.getLog( DefaultOutboundNotification.class );
+
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    @Autowired
+    private OutboundSmsService outboundSmsService;
+
+    private OutboundSms outboundSms;
+
+    public void setOutboundSms( OutboundSms outboundSms )
+    {
+        this.outboundSms = outboundSms;
+    }
+
+    // -------------------------------------------------------------------------
+    // Implementation
+    // -------------------------------------------------------------------------
+
+    @Override
+    public void process( AGateway gateway, OutboundMessage msg )
+    {
+        log.info( "SENDING via " + gateway.getGatewayId() + " " );
+
+        outboundSms = outboundSmsService.convertToOutboundSms( msg );
+        outboundSmsService.saveOutboundSms( outboundSms );
+
+        if ( msg.getMessageStatus() == MessageStatuses.SENT )
+        {
+            log.info( "SENT To ::: " + msg.getRecipient() );
+        }
+        else
+        {
+            log.info( "FAILED To ::: " + msg.getRecipient() );
+        }
+
+    }
+
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundSmsService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundSmsService.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundSmsService.java	2016-03-07 13:15:24 +0000
@@ -1,5 +1,7 @@
 package org.hisp.dhis.sms.outbound;
 
+import java.util.HashSet;
+
 /*
  * Copyright (c) 2004-2016, University of Oslo
  * All rights reserved.
@@ -29,7 +31,10 @@
  */
 
 import java.util.List;
+import java.util.Set;
 
+import org.smslib.OutboundMessage;
+import org.smslib.OutboundMessage.MessageStatuses;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -104,4 +109,33 @@
     {
         return outboundSmsStore.getAllOutboundSms( min, max );
     }
+
+    @Override
+    public OutboundSms convertToOutboundSms( OutboundMessage sms )
+    {
+        if ( sms == null )
+        {
+            return null;
+        }
+
+        Set<String> recipients = new HashSet<String>();
+        recipients.add( sms.getRecipient() );
+
+        OutboundSms outboundSms = new OutboundSms();
+        outboundSms.setMessage( sms.getText() );
+        outboundSms.setRecipients( recipients );
+        outboundSms.setDate( sms.getDate() );
+
+        if ( sms.getMessageStatus() == MessageStatuses.SENT )
+        {
+            outboundSms.setStatus( OutboundSmsStatus.SENT );
+        }
+        else
+        {
+            outboundSms.setStatus( OutboundSmsStatus.ERROR );
+        }
+
+        return outboundSms;
+    }
+
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundSmsTransportService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundSmsTransportService.java	2016-01-07 15:15:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/outbound/DefaultOutboundSmsTransportService.java	2016-03-07 13:15:24 +0000
@@ -79,6 +79,13 @@
     {
         this.smppInboundMessageNotification = smppInboundMessageNotification;
     }
+    
+    private IOutboundMessageNotification outboundMessageNotification;
+
+    public void setOutboundMessageNotification( IOutboundMessageNotification outboundMessageNotification )
+    {
+        this.outboundMessageNotification = outboundMessageNotification;
+    }
 
     private OutboundSmsService outboundSmsService;
 
@@ -463,13 +470,6 @@
             log.warn( "Unable to send message: " + sms, e );
             message = "Unable to send message: " + sms + " " + e.getCause().getMessage();
         }
-        finally
-        {
-            if ( recipients.size() > 1 )
-            {
-                removeGroup( recipient );
-            }
-        }
 
         if ( outboundMessage.getMessageStatus() == MessageStatuses.SENT )
         {
@@ -496,6 +496,57 @@
         return message;
     }
 
+    @Override
+    public boolean sendAyncMessages( OutboundSms sms, String gatewayId )
+    {
+
+        String recipient = null;
+
+        Set<String> recipients = sms.getRecipients();
+
+        if ( recipients.size() == 1 )
+        {
+            recipient = recipients.iterator().next();
+        }
+        else
+        {
+            recipient = createTmpGroup( recipients );
+        }
+
+        OutboundMessage outboundMessage = new OutboundMessage( recipient, sms.getMessage() );
+
+        for ( char each : sms.getMessage().toCharArray() )
+        {
+            if ( !Character.UnicodeBlock.of( each ).equals( UnicodeBlock.BASIC_LATIN ) )
+            {
+                outboundMessage.setEncoding( MessageEncodings.ENCUCS2 );
+                break;
+            }
+        }
+
+        outboundMessage.setStatusReport( true );
+
+        String longNumber = config.getLongNumber();
+
+        if ( longNumber != null && !longNumber.isEmpty() )
+        {
+            outboundMessage.setFrom( longNumber );
+        }
+
+        try
+        {
+            getService().setOutboundMessageNotification( outboundMessageNotification );
+            getService().queueMessage( outboundMessage, gatewayId );
+            return true;
+        }
+        catch ( Exception e )
+        {
+            log.warn( "ASYNC Message sending failed ", e );
+        }
+
+        return false;
+    }
+
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------
@@ -517,21 +568,17 @@
 
     private String createTmpGroup( Set<String> recipients )
     {
-        String groupName = Thread.currentThread().getName();
-
-        getService().createGroup( groupName );
-
-        for ( String recepient : recipients )
+        String recipientList = "";
+
+        for ( String recipient : recipients )
         {
-            getService().addToGroup( groupName, recepient );
+            recipientList += recipient;
+            recipientList += ",";
         }
 
-        return groupName;
-    }
+        recipientList = recipientList.substring( 0, recipientList.length() - 1 );
 
-    private void removeGroup( String groupName )
-    {
-        getService().removeGroup( groupName );
+        return recipientList;
     }
 
     @Override

=== 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	2016-02-24 04:25:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2016-03-07 13:15:24 +0000
@@ -1115,13 +1115,16 @@
   </bean>
 
   <bean id="org.smslib.IInboundMessageNotification" class="org.hisp.dhis.sms.incoming.SMPPInboundNotification" />
-
+  
+  <bean id="org.smslib.IOutboundMessageNotification" class="org.hisp.dhis.sms.outbound.DefaultOutboundNotification" />
+  
   <bean id="org.hisp.dhis.sms.outbound.OutboundSmsService" class="org.hisp.dhis.sms.outbound.DefaultOutboundSmsService">
     <property name="outboundSmsStore" ref="org.hisp.dhis.sms.hibernate.OutboundSmsStore" />
   </bean>
 
   <bean id="org.hisp.dhis.sms.outbound.OutboundSmsTransportService" class="org.hisp.dhis.sms.outbound.DefaultOutboundSmsTransportService">
-    <property name="smppInboundMessageNotification" ref="org.smslib.IInboundMessageNotification" />
+    <property name="smppInboundMessageNotification" ref="org.smslib.IInboundMessageNotification" />  
+    <property name="outboundMessageNotification" ref="org.smslib.IOutboundMessageNotification" />
     <property name="outboundSmsService" ref="org.hisp.dhis.sms.outbound.OutboundSmsService" />
     <property name="smsPublisher" ref="org.hisp.dhis.sms.SmsPublisher" />
   </bean>

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/sms/SmsController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/sms/SmsController.java	2016-02-01 07:36:06 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/sms/SmsController.java	2016-03-07 13:15:24 +0000
@@ -105,20 +105,32 @@
 
     @PreAuthorize( "hasRole('ALL') or hasRole(' F_MOBILE_SENDSMS')" )
     @RequestMapping( value = "/outbound", method = RequestMethod.POST, consumes = "application/json" )
-    public void sendSMSMessage( HttpServletResponse response, HttpServletRequest request )
-        throws WebMessageException, IOException
+    public void sendSMSMessage( @RequestParam( required = false ) boolean async, HttpServletResponse response,
+        HttpServletRequest request)
+            throws WebMessageException, IOException
     {
         OutboundSms sms = renderService.fromJson( request.getInputStream(), OutboundSms.class );
 
-        String result = smsSender.sendMessage( sms );
-        
-        if ( result.equals( "success" ) )
+        if ( async )
         {
-            webMessageService.send( WebMessageUtils.ok( "Message sent" ), response, request );
+            smsSender.sendAyncMessages( sms );
+
+            webMessageService.send( WebMessageUtils.ok( "Message Sent asynchronously" ), response, request );
+
         }
         else
         {
-            webMessageService.send( WebMessageUtils.error( "Message sending failed" ), response, request );
+            String result = smsSender.sendMessage( sms );
+
+            if ( result.equals( "success" ) )
+            {
+                webMessageService.send( WebMessageUtils.ok( "Message sent" ), response, request );
+            }
+            else
+            {
+                throw new WebMessageException( WebMessageUtils.error( "Message seding failed" ) );
+            }
+
         }
     }