← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11212: forward email to user group from alert command

 

------------------------------------------------------------
revno: 11212
committer: Lai <lai.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-06-17 10:27:43 +0700
message:
  forward email to user group from alert command
modified:
  dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/SmsSender.java
  dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/parse/DefaultParserManager.java
  dhis-2/dhis-services/dhis-service-sms/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-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/SmsSender.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/SmsSender.java	2013-06-10 12:26:49 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/SmsSender.java	2013-06-17 03:27:43 +0000
@@ -27,6 +27,8 @@
 
 package org.hisp.dhis.sms.outbound;
 
+import java.lang.Character.UnicodeBlock;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -53,9 +55,7 @@
 {
     private static final Log log = LogFactory.getLog( SmsSender.class );
 
-    private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
-
-    private static final int MAX_HEX_CHAR = 280;
+    private static int MAX_CHAR = 160;
 
     // -------------------------------------------------------------------------
     // Dependencies
@@ -93,7 +93,7 @@
     public String sendMessage( String subject, String text, User sender, Set<User> users, boolean forceSend )
     {
         String message = null;
-        
+
         if ( transportService == null || SmsLibService.gatewayMap == null )
         {
             message = "No gateway";
@@ -103,7 +103,7 @@
         Set<User> toSendList = new HashSet<User>();
 
         String gatewayId = transportService.getDefaultGateway();
-        
+
         if ( gatewayId != null && !gatewayId.trim().isEmpty() )
         {
             for ( User user : users )
@@ -132,47 +132,43 @@
             if ( outboundSmsService != null || outboundSmsService.isEnabled() )
             {
                 phoneNumbers = getRecipientsPhoneNumber( toSendList );
-                /*if ( SmsLibService.gatewayMap.get( "bulk_gw" ).equals( gatewayId ) )
+                
+                text = createMessage( subject, text, sender );
+                
+                //Bulk is limited in sending long SMS, need to cut into small pieces
+                if ( SmsLibService.gatewayMap.get( "bulk_gw" ).equals( gatewayId ) )
                 {
-                    try
+                    //Check if text contain any specific unicode character
+                    for( char each: text.toCharArray())
                     {
-                        String hexText = toHex( text.getBytes( "unicode" ) );
-                        if ( hexText.length() > MAX_HEX_CHAR )
+                        if( !Character.UnicodeBlock.of(each).equals( UnicodeBlock.BASIC_LATIN ) )
                         {
- 
-                            List<String> splitTextList = new ArrayList<String>();
-                            splitTextList = splitLongHexString( hexText, text, splitTextList );
-                            for ( String each: splitTextList )
-                            {
-                                //text = createMessage( subject, each, sender );
-                                
-                                if ( !phoneNumbers.isEmpty() && phoneNumbers.size() > 0 )
-                                {
-                                    //message = sendMessage( text, phoneNumbers, gatewayId );
-                                    message = sendMessage( each, phoneNumbers, gatewayId );
-                                }
-                            }
+                            MAX_CHAR = 40;
+                            break;
                         }
-                        else
+                    }
+                    if ( text.length() > MAX_CHAR )
+                    {
+                        List<String> splitTextList = new ArrayList<String>();
+                        splitTextList = splitLongUnicodeString( text, splitTextList );
+                        for ( String each : splitTextList )
                         {
-                            //text = createMessage( subject, text, sender );
-                            
                             if ( !phoneNumbers.isEmpty() && phoneNumbers.size() > 0 )
                             {
-                                message = sendMessage( text, phoneNumbers, gatewayId );
+                                message = sendMessage( each, phoneNumbers, gatewayId );
                             }
                         }
                     }
-                    catch ( UnsupportedEncodingException e )
+                    else
                     {
-                        // TODO Auto-generated catch block
-                        e.printStackTrace();
+                        if ( !phoneNumbers.isEmpty() && phoneNumbers.size() > 0 )
+                        {
+                            message = sendMessage( text, phoneNumbers, gatewayId );
+                        }
                     }
                 }
-                else*/
+                else
                 {
-                    text = createMessage( subject, text, sender );
-                    
                     if ( !phoneNumbers.isEmpty() && phoneNumbers.size() > 0 )
                     {
                         message = sendMessage( text, phoneNumbers, gatewayId );
@@ -180,7 +176,7 @@
                 }
             }
         }
-        
+
         return message;
     }
 
@@ -283,45 +279,30 @@
         return message;
     }
 
-    public String toHex( byte[] buf )
-    {
-        char[] chars = new char[2 * buf.length];
-        for ( int i = 0; i < buf.length; ++i )
-        {
-            chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
-            chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
-        }
-        return new String( chars );
-    }
-
-    public List<String> splitLongHexString( String hexString, String message, List<String> result )
-    {
-
-        String firstTempHex = null;
-        String secondTempHex = null;
+    public List<String> splitLongUnicodeString( String message, List<String> result )
+    {
         String firstTempString = null;
         String secondTempString = null;
         int indexToCut;
 
-        firstTempHex = hexString.substring( 0, MAX_HEX_CHAR );
-
-        int lastSpaceIndex = firstTempHex.lastIndexOf( "0020" );
-
-        firstTempHex = firstTempHex.substring( 0, lastSpaceIndex );
-        indexToCut = (firstTempHex.length() - 4) / 4;
-        firstTempString = message.substring( 0, indexToCut );
+        firstTempString = message.substring( 0, MAX_CHAR );
+
+        indexToCut = firstTempString.lastIndexOf( " " );
+
+        firstTempString = firstTempString.substring( 0, indexToCut );
+
         result.add( firstTempString );
 
-        secondTempHex = "feff" + hexString.substring( firstTempHex.length() + 4, hexString.length() );
         secondTempString = message.substring( indexToCut + 1, message.length() );
-        if ( secondTempHex.length() <= MAX_HEX_CHAR )
+
+        if ( secondTempString.length() <= MAX_CHAR )
         {
             result.add( secondTempString );
             return result;
         }
         else
         {
-            return splitLongHexString( secondTempHex, secondTempString, result );
+            return splitLongUnicodeString( secondTempString, result );
         }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/parse/DefaultParserManager.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/parse/DefaultParserManager.java	2013-06-10 12:26:49 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/parse/DefaultParserManager.java	2013-06-17 03:27:43 +0000
@@ -53,6 +53,7 @@
 import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.datavalue.DataValue;
 import org.hisp.dhis.datavalue.DataValueService;
+import org.hisp.dhis.message.EmailMessageSender;
 import org.hisp.dhis.message.Message;
 import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.message.MessageConversationStore;
@@ -60,7 +61,6 @@
 import org.hisp.dhis.message.UserMessage;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.patient.Patient;
-import org.hisp.dhis.patient.PatientService;
 import org.hisp.dhis.period.CalendarPeriodType;
 import org.hisp.dhis.period.DailyPeriodType;
 import org.hisp.dhis.period.MonthlyPeriodType;
@@ -124,11 +124,18 @@
         this.messageConversationStore = messageConversationStore;
     }
 
-    private MessageSender messageSender;
-
-    public void setMessageSender( MessageSender messageSender )
-    {
-        this.messageSender = messageSender;
+    private MessageSender smsMessageSender;
+
+    public void setSmsMessageSender( MessageSender smsMessageSender )
+    {
+        this.smsMessageSender = smsMessageSender;
+    }
+
+    private MessageSender emailMessageSender;
+    
+    public void setEmailMessageSender( MessageSender emailMessageSender )
+    {
+        this.emailMessageSender = emailMessageSender;
     }
 
     @Autowired
@@ -358,7 +365,10 @@
                 Set<User> receivers = new HashSet<User>( userGroup.getMembers() );
 
                 // forward to user group by SMS
-                messageSender.sendMessage( command.getName(), message, sender, receivers, true );
+                smsMessageSender.sendMessage( command.getName(), message, sender, receivers, true );
+                
+                // forward to user group by E-mail
+                emailMessageSender.sendMessage( command.getName(), message, sender, receivers, false );
 
                 // forward to user group by dhis message
                 if ( sender != null )
@@ -380,7 +390,8 @@
                 // confirm SMS was received and forwarded completely
                 Set<User> feedbackList = new HashSet<User>();
                 feedbackList.add( sender );
-                messageSender.sendMessage( command.getName(), command.getReceivedMessage(), null, feedbackList, true );
+                smsMessageSender.sendMessage( command.getName(), command.getReceivedMessage(), null, feedbackList, true );
+                
             }
         }
     }

=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-sms/src/main/resources/META-INF/dhis/beans.xml	2013-06-10 12:26:49 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/resources/META-INF/dhis/beans.xml	2013-06-17 03:27:43 +0000
@@ -61,7 +61,8 @@
     <property name="outboundSmsService" ref="org.hisp.dhis.sms.outbound.OutboundSmsService" />  
     <property name="registrationService" ref="org.hisp.dhis.dataset.CompleteDataSetRegistrationService" />
     <property name="messageConversationStore" ref="org.hisp.dhis.message.MessageConversationStore" />
-    <property name="messageSender" ref="org.hisp.dhis.sms.outbound.SmsSender" />
+    <property name="smsMessageSender" ref="org.hisp.dhis.sms.outbound.SmsSender" />
+    <property name="emailMessageSender" ref="emailMessageSender" />
   </bean>
   
 </beans>