← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11187: auto detect unicode, accept SMS without keyword from patient, accept SMS with keyword from anonym...

 

------------------------------------------------------------
revno: 11187
committer: Lai <lai.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-06-10 18:42:40 +0700
message:
  auto detect unicode, accept SMS without keyword from patient, accept SMS with keyword from anonymous senders, modify edit sms command page, fix minor bugs
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/parse/ParserType.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml
  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/java/org/hisp/dhis/sms/smslib/SmsLibService.java
  dhis-2/dhis-services/dhis-service-sms/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/CreateSMSCommandForm.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/javascript/command.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/smscommand/edit-sms-command.vm


--
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/parse/ParserType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/parse/ParserType.java	2013-05-20 09:32:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/parse/ParserType.java	2013-06-10 11:42:40 +0000
@@ -4,5 +4,7 @@
 {
     KEY_VALUE_PARSER,
     J2ME_PARSER,
-    ALERT_PARSER;
+    ALERT_PARSER,
+    UNREGISTERED_PARSER,
+    ;
 }

=== 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	2013-06-08 21:51:15 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2013-06-10 11:42:40 +0000
@@ -785,6 +785,14 @@
     <property name="runlevel" value="6" />
     <property name="skipInTests" value="true" />
   </bean>
+  
+  <!--  <bean id="org.hisp.dhis.user.UserDefaultPopulator"
+    class="org.hisp.dhis.user.UserDefaultPopulator">
+    <property name="userService" ref="org.hisp.dhis.user.UserService" />
+    <property name="runlevel" value="5" />
+    <property name="skipInTests" value="true" />
+  </bean>
+  -->
 
   <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
     <property name="targetObject" ref="org.hisp.dhis.system.startup.StartupRoutineExecutor" />
@@ -801,6 +809,7 @@
           <ref local="org.hisp.dhis.organisationunit.OrganisationUnitGroupSetPopulator" />
           <ref local="org.hisp.dhis.dataentryform.DataEntryFormUpgrader" />
           <ref local="org.hisp.dhis.startup.ExpressionUpgrader" />
+          <!--<ref local="org.hisp.dhis.user.UserDefaultPopulator" />-->
         </list>
       </list>
     </property>

=== 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 11:33:06 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/outbound/SmsSender.java	2013-06-10 11:42:40 +0000
@@ -35,7 +35,6 @@
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.message.MessageSender;
 import org.hisp.dhis.sms.SmsServiceException;
-import org.hisp.dhis.sms.smslib.SmsLibService;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserService;
@@ -103,11 +102,6 @@
 
         String gatewayId = transportService.getDefaultGateway();
         
-        if ( SmsLibService.gatewayMap.get( "bulk_gw" ).equals( gatewayId ) )
-        {
-            //bulk is limited in sending long SMS. to be continue....
-        }
-
         if ( gatewayId != null && !gatewayId.trim().isEmpty() )
         {
             for ( User user : users )
@@ -135,15 +129,53 @@
 
             if ( outboundSmsService != null || outboundSmsService.isEnabled() )
             {
-                text = createMessage( subject, text, sender );
-
                 phoneNumbers = getRecipientsPhoneNumber( toSendList );
-
-                if ( !phoneNumbers.isEmpty() && phoneNumbers.size() > 0 )
-                {
-                    message = sendMessage( text, phoneNumbers, gatewayId );
-                }
-
+                /*if ( SmsLibService.gatewayMap.get( "bulk_gw" ).equals( gatewayId ) )
+                {
+                    try
+                    {
+                        String hexText = toHex( text.getBytes( "unicode" ) );
+                        if ( hexText.length() > MAX_HEX_CHAR )
+                        {
+ 
+                            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 );
+                                }
+                            }
+                        }
+                        else
+                        {
+                            //text = createMessage( subject, text, sender );
+                            
+                            if ( !phoneNumbers.isEmpty() && phoneNumbers.size() > 0 )
+                            {
+                                message = sendMessage( text, phoneNumbers, gatewayId );
+                            }
+                        }
+                    }
+                    catch ( UnsupportedEncodingException e )
+                    {
+                        // TODO Auto-generated catch block
+                        e.printStackTrace();
+                    }
+                }
+                else*/
+                {
+                    text = createMessage( subject, text, sender );
+                    
+                    if ( !phoneNumbers.isEmpty() && phoneNumbers.size() > 0 )
+                    {
+                        message = sendMessage( text, phoneNumbers, gatewayId );
+                    }
+                }
             }
         }
         
@@ -229,8 +261,6 @@
     }
 
     private String sendMessage( String text, Set<String> recipients, String gateWayId )
-    // private String sendMessage( String text, Set<String> recipients, String
-    // gateWayId, boolean isUnicode )
     {
         String message = null;
         OutboundSms sms = new OutboundSms();
@@ -251,7 +281,7 @@
         return message;
     }
 
-    public static String toHex( byte[] buf )
+    public String toHex( byte[] buf )
     {
         char[] chars = new char[2 * buf.length];
         for ( int i = 0; i < buf.length; ++i )
@@ -262,7 +292,7 @@
         return new String( chars );
     }
 
-    public static List<String> spitLongHexString( String hexString, String message, List<String> result )
+    public List<String> splitLongHexString( String hexString, String message, List<String> result )
     {
 
         String firstTempHex = null;
@@ -289,7 +319,7 @@
         }
         else
         {
-            return spitLongHexString( secondTempHex, secondTempString, result );
+            return splitLongHexString( secondTempHex, 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-05-30 09:49:45 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/parse/DefaultParserManager.java	2013-06-10 11:42:40 +0000
@@ -59,6 +59,8 @@
 import org.hisp.dhis.message.MessageSender;
 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;
@@ -78,6 +80,7 @@
 import org.hisp.dhis.smscommand.SMSCommandService;
 import org.hisp.dhis.system.util.ValidationUtils;
 import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserCredentials;
 import org.hisp.dhis.user.UserGroup;
 import org.hisp.dhis.user.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -91,6 +94,8 @@
     implements ParserManager
 {
 
+    public static final String ANONYMOUS_USER_NAME = "Anonymous";
+
     private static final Log log = LogFactory.getLog( DefaultParserManager.class );
 
     private CompleteDataSetRegistrationService registrationService;
@@ -118,7 +123,7 @@
     {
         this.messageConversationStore = messageConversationStore;
     }
-    
+
     private MessageSender messageSender;
 
     public void setMessageSender( MessageSender messageSender )
@@ -126,6 +131,13 @@
         this.messageSender = messageSender;
     }
 
+    private PatientService patientService;
+
+    public void setPatientService( PatientService patientService )
+    {
+        this.patientService = patientService;
+    }
+
     @Autowired
     private IncomingSmsService incomingSmsService;
 
@@ -170,19 +182,13 @@
 
         sender = StringUtils.replace( sender, "+", "" );
 
-        Collection<OrganisationUnit> orgUnits = getOrganisationUnitsByPhoneNumber( sender );
-        if ( orgUnits == null || orgUnits.size() == 0 )
-        {
-            log.info( "No user found for phone number: " + sender );
-            throw new SMSParserException( "No user associated with this phone number. Please contact your supervisor." );
-        }
-
         if ( StringUtils.isEmpty( message ) )
         {
             throw new SMSParserException( "No command in SMS" );
         }
 
         String commandString = null;
+        // here, check command first
         if ( message.indexOf( " " ) > 0 )
         {
             commandString = message.substring( 0, message.indexOf( " " ) );
@@ -195,6 +201,8 @@
 
         boolean foundCommand = false;
 
+        Collection<OrganisationUnit> orgUnits = getOrganisationUnitsByPhoneNumber( sender );
+
         for ( SMSCommand command : smsCommandService.getSMSCommands() )
         {
             if ( command.getName().equalsIgnoreCase( commandString ) )
@@ -202,24 +210,55 @@
                 foundCommand = true;
                 if ( ParserType.KEY_VALUE_PARSER.equals( command.getParserType() ) )
                 {
+                    checkIfDHISUsers( orgUnits, sender );
                     runKeyValueParser( sender, message, orgUnits, command );
                     break;
                 }
                 else if ( ParserType.J2ME_PARSER.equals( command.getParserType() ) )
                 {
+                    checkIfDHISUsers( orgUnits, sender );
                     runJ2MEParser( sender, message, orgUnits, command );
                     break;
                 }
                 else if ( ParserType.ALERT_PARSER.equals( command.getParserType() ) )
                 {
+                    checkIfDHISUsers( orgUnits, sender );
                     runDhisMessageAlertParser( sender, message, command );
                     break;
                 }
+                else if ( ParserType.UNREGISTERED_PARSER.equals( command.getParserType() ) )
+                {
+                    runUnregisteredParser( sender, message, command );
+                    break;
+                }
             }
         }
         if ( !foundCommand )
         {
-            throw new SMSParserException( "Command '" + commandString + "' does not exist" );
+            Collection<Patient> patientList = patientService.getPatientsByPhone( sender, null, null );
+            
+            if ( patientList != null && patientList.size() > 0 )
+            {
+                for ( Patient each : patientList )
+                {
+                    if ( each.getHealthWorker() != null )
+                    {
+                        UserCredentials patientUser = userService.getUserCredentialsByUsername( "system" );
+
+                        MessageConversation conversation = new MessageConversation( "Patients' Message", patientUser.getUser() );
+
+                        conversation.addMessage( new Message( sms.getText().trim(), null, patientUser.getUser() ) );
+
+                        conversation.addUserMessage( new UserMessage( each.getHealthWorker(), false ) );
+                        
+                        messageConversationStore.save( conversation );
+                    }
+                }
+            }
+            else
+            {
+                throw new SMSParserException( "Command '" + commandString + "' does not exist" );
+            }
         }
     }
 
@@ -238,6 +277,15 @@
         return orgUnits;
     }
 
+    private void checkIfDHISUsers( Collection<OrganisationUnit> orgUnits, String sender )
+    {
+        if ( orgUnits == null || orgUnits.size() == 0 )
+        {
+            log.info( "No user found for phone number: " + sender );
+            throw new SMSParserException( "No user associated with this phone number. Please contact your supervisor." );
+        }
+    }
+
     private void runKeyValueParser( String sender, String message, Collection<OrganisationUnit> orgUnits,
         SMSCommand command )
     {
@@ -316,10 +364,10 @@
 
                 Set<User> receivers = new HashSet<User>( userGroup.getMembers() );
 
-                //forward to user group by SMS
+                // forward to user group by SMS
                 messageSender.sendMessage( command.getName(), message, sender, receivers, true );
-                
-                //forward to user group by dhis message
+
+                // forward to user group by dhis message
                 if ( sender != null )
                 {
                     receivers.add( sender );
@@ -336,7 +384,7 @@
                     conversation.addUserMessage( new UserMessage( receiver, read ) );
                 }
                 messageConversationStore.save( conversation );
-                //confirm SMS was received and forwarded completely
+                // 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 );
@@ -344,6 +392,30 @@
         }
     }
 
+    private void runUnregisteredParser( String senderNumber, String message, SMSCommand command )
+    {
+        UserGroup userGroup = command.getUserGroup();
+
+        if ( userGroup != null )
+        {
+            Set<User> receivers = new HashSet<User>( userGroup.getMembers() );
+
+            UserCredentials anonymousUser = userService.getUserCredentialsByUsername( "system" );
+
+            MessageConversation conversation = new MessageConversation( command.getName(), anonymousUser.getUser() );
+
+            conversation.addMessage( new Message( message, null, anonymousUser.getUser() ) );
+
+            for ( User receiver : receivers )
+            {
+                boolean read = false;
+
+                conversation.addUserMessage( new UserMessage( receiver, read ) );
+            }
+            messageConversationStore.save( conversation );
+        }
+    }
+
     protected OrganisationUnit selectOrganisationUnit( Collection<OrganisationUnit> orgUnits,
         Map<String, String> parsedMessage )
     {

=== modified file 'dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java'
--- dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java	2013-05-30 09:49:45 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/java/org/hisp/dhis/sms/smslib/SmsLibService.java	2013-06-10 11:42:40 +0000
@@ -52,6 +52,7 @@
 import org.smslib.Service.ServiceStatus;
 
 import java.io.IOException;
+import java.lang.Character.UnicodeBlock;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -147,7 +148,15 @@
 
         OutboundMessage outboundMessage = new OutboundMessage( recipient, sms.getMessage() );
         
-        outboundMessage.setEncoding( MessageEncodings.ENCUCS2 );
+        //Check if text contain any specific unicode character
+        for( char each: sms.getMessage().toCharArray())
+        {
+            if( !Character.UnicodeBlock.of(each).equals( UnicodeBlock.BASIC_LATIN ) )
+            {
+                outboundMessage.setEncoding( MessageEncodings.ENCUCS2 );
+                break;
+            }
+        }
         
         outboundMessage.setStatusReport( 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-05-30 09:49:45 +0000
+++ dhis-2/dhis-services/dhis-service-sms/src/main/resources/META-INF/dhis/beans.xml	2013-06-10 11:42:40 +0000
@@ -62,6 +62,7 @@
     <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="patientService" ref="org.hisp.dhis.patient.PatientService" />
   </bean>
   
 </beans>

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/CreateSMSCommandForm.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/CreateSMSCommandForm.java	2013-05-20 09:32:19 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/CreateSMSCommandForm.java	2013-06-10 11:42:40 +0000
@@ -114,7 +114,7 @@
             DataSet dataset = dataSetService.getDataSet( selectedDataSetID );
             command.setDataset( dataset );
         }
-        else if ( parserType.equals( ParserType.ALERT_PARSER ) )
+        else if ( parserType.equals( ParserType.ALERT_PARSER ) || parserType.equals( ParserType.UNREGISTERED_PARSER ) )
         {
             UserGroup userGroup = new UserGroup();
             userGroup = userGroupService.getUserGroup( userGroupID );

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/javascript/command.js'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/javascript/command.js	2013-05-20 09:40:29 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/javascript/command.js	2013-06-10 11:42:40 +0000
@@ -4,7 +4,7 @@
 	hideAll();
     if ( value == 'KEY_VALUE_PARSER' || value == 'J2ME_PARSER') {
         showById( "dataSetParser" );
-    } else if ( value == 'ALERT_PARSER' ) {
+    } else if ( value == 'ALERT_PARSER' || value == 'UNREGISTERED_PARSER' ) {
     	showById( "alertParser" );
     }
 	currentType = value;

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/smscommand/edit-sms-command.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/smscommand/edit-sms-command.vm	2013-05-30 09:49:45 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/smscommand/edit-sms-command.vm	2013-06-10 11:42:40 +0000
@@ -66,7 +66,7 @@
 				<input type="text"  #if($smsCommand.separator) value="$smsCommand.separator" #end name="separator" style="width:20em" class="validate" />
 			</td>
 		</tr> 
-		#elseif( $smsCommand.parserType == 'ALERT_PARSER' )
+		#elseif( $smsCommand.parserType == 'ALERT_PARSER' || $smsCommand.parserType == 'UNREGISTERED_PARSER' )
 		<tr>
 			<td>$i18n.getString( "userGroup" )</td>
 			<td>