← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13526: [mobile] sms service accepts any special char comes after command name

 

------------------------------------------------------------
revno: 13526
committer: Long <Long@Long-Laptop>
branch nick: dhis2
timestamp: Wed 2014-01-01 23:19:34 +0700
message:
  [mobile] sms service accepts any special char comes after command name
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DHISMessageAlertListener.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DataValueSMSListener.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/UnregisteredSMSListener.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-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DHISMessageAlertListener.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DHISMessageAlertListener.java	2013-10-10 05:42:48 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DHISMessageAlertListener.java	2014-01-01 16:19:34 +0000
@@ -74,14 +74,16 @@
     {
         String message = sms.getText();
         String commandString = null;
-        if ( message.indexOf( " " ) > 0 )
-        {
-            commandString = message.substring( 0, message.indexOf( " " ) );
-            message = message.substring( commandString.length() );
-        }
-        else
-        {
-            commandString = message;
+
+        for ( int i = 0; i < message.length(); i++ )
+        {
+            String c = String.valueOf( message.charAt( i ) );
+            if ( c.matches( "\\W" ) )
+            {
+                commandString = message.substring( 0, i );
+                message = message.substring( commandString.length() + 1 );
+                break;
+            }
         }
 
         return smsCommandService.getSMSCommand( commandString, ParserType.ALERT_PARSER ) != null;
@@ -93,14 +95,16 @@
     {
         String message = sms.getText();
         String commandString = null;
-        if ( message.indexOf( " " ) > 0 )
-        {
-            commandString = message.substring( 0, message.indexOf( " " ) );
-            message = message.substring( commandString.length() );
-        }
-        else
-        {
-            commandString = message;
+
+        for ( int i = 0; i < message.length(); i++ )
+        {
+            String c = String.valueOf( message.charAt( i ) );
+            if ( c.matches( "\\W" ) )
+            {
+                commandString = message.substring( 0, i );
+                message = message.substring( commandString.length() + 1 );
+                break;
+            }
         }
 
         SMSCommand smsCommand = smsCommandService.getSMSCommand( commandString, ParserType.ALERT_PARSER );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DataValueSMSListener.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DataValueSMSListener.java	2013-12-20 09:26:23 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DataValueSMSListener.java	2014-01-01 16:19:34 +0000
@@ -97,15 +97,18 @@
     {
         String message = sms.getText();
         String commandString = null;
-        if ( message.indexOf( " " ) > 0 )
-        {
-            commandString = message.substring( 0, message.indexOf( " " ) );
-            message = message.substring( commandString.length() );
-        }
-        else
-        {
-            commandString = message;
-        }
+
+        for ( int i = 0; i < message.length(); i++ )
+        {
+            String c = String.valueOf( message.charAt( i ) );
+            if ( c.matches( "\\W" ) )
+            {
+                commandString = message.substring( 0, i );
+                message = message.substring( commandString.length() + 1 );
+                break;
+            }
+        }
+
         return smsCommandService.getSMSCommand( commandString, ParserType.KEY_VALUE_PARSER ) != null;
     }
 
@@ -115,15 +118,18 @@
     {
         String message = sms.getText();
         String commandString = null;
-        if ( message.indexOf( " " ) > 0 )
-        {
-            commandString = message.substring( 0, message.indexOf( " " ) );
-            message = message.substring( commandString.length() );
-        }
-        else
-        {
-            commandString = message;
-        }
+
+        for ( int i = 0; i < message.length(); i++ )
+        {
+            String c = String.valueOf( message.charAt( i ) );
+            if ( c.matches( "\\W" ) )
+            {
+                commandString = message.substring( 0, i );
+                message = message.substring( commandString.length() + 1 );
+                break;
+            }
+        }
+
         SMSCommand smsCommand = smsCommandService.getSMSCommand( commandString, ParserType.KEY_VALUE_PARSER );
         Map<String, String> parsedMessage = this.parse( message, smsCommand );
 
@@ -151,7 +157,7 @@
         {
             if ( parsedMessage.containsKey( code.getCode().toUpperCase() ) )
             {
-                //potential bugs [noted by Lai]
+                // potential bugs [noted by Lai]
                 valueStored = storeDataValue( senderPhoneNumber, orgUnit, parsedMessage, code, smsCommand, date,
                     smsCommand.getDataset() );
             }
@@ -336,11 +342,11 @@
         DataValue dv = dataValueService.getDataValue( code.getDataElement(), period, orgunit, optionCombo );
 
         String value = parsedMessage.get( upperCaseCode );
-        
+
         Set<SMSSpecialCharacter> specialCharacters = command.getSpecialCharacters();
-        
-        //Check for special character cases
-        for( SMSSpecialCharacter each: specialCharacters )
+
+        // Check for special character cases
+        for ( SMSSpecialCharacter each : specialCharacters )
         {
             if ( each.getName().equalsIgnoreCase( value ) )
             {
@@ -348,7 +354,7 @@
                 break;
             }
         }
-        
+
         if ( !StringUtils.isEmpty( value ) )
         {
             boolean newDataValue = false;
@@ -377,7 +383,7 @@
             else if ( StringUtils.equals( dv.getDataElement().getType(), DataElement.VALUE_TYPE_INT ) )
             {
                 try
-                {    
+                {
                     Integer.parseInt( value );
                 }
                 catch ( NumberFormatException e )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/UnregisteredSMSListener.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/UnregisteredSMSListener.java	2013-09-24 08:21:12 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/UnregisteredSMSListener.java	2014-01-01 16:19:34 +0000
@@ -61,7 +61,7 @@
     private SmsMessageSender smsMessageSender;
 
     private IncomingSmsService incomingSmsService;
-    
+
     public static final String USER_NAME = "anonymous";
 
     @Transactional
@@ -70,14 +70,16 @@
     {
         String message = sms.getText();
         String commandString = null;
-        if ( message.indexOf( " " ) > 0 )
-        {
-            commandString = message.substring( 0, message.indexOf( " " ) );
-            message = message.substring( commandString.length() );
-        }
-        else
-        {
-            commandString = message;
+
+        for ( int i = 0; i < message.length(); i++ )
+        {
+            String c = String.valueOf( message.charAt( i ) );
+            if ( c.matches( "\\W" ) )
+            {
+                commandString = message.substring( 0, i );
+                message = message.substring( commandString.length() + 1 );
+                break;
+            }
         }
 
         return smsCommandService.getSMSCommand( commandString, ParserType.UNREGISTERED_PARSER ) != null;
@@ -89,14 +91,16 @@
     {
         String message = sms.getText();
         String commandString = null;
-        if ( message.indexOf( " " ) > 0 )
-        {
-            commandString = message.substring( 0, message.indexOf( " " ) );
-            message = message.substring( commandString.length() );
-        }
-        else
-        {
-            commandString = message;
+
+        for ( int i = 0; i < message.length(); i++ )
+        {
+            String c = String.valueOf( message.charAt( i ) );
+            if ( c.matches( "\\W" ) )
+            {
+                commandString = message.substring( 0, i );
+                message = message.substring( commandString.length() + 1 );
+                break;
+            }
         }
 
         SMSCommand smsCommand = smsCommandService.getSMSCommand( commandString, ParserType.UNREGISTERED_PARSER );
@@ -139,16 +143,16 @@
                     user.setSurname( USER_NAME );
                     user.setFirstName( USER_NAME );
                     user.setUserCredentials( usercredential );
-                    
+
                     userService.addUserCredentials( usercredential );
                     userService.addUser( user );
                     anonymousUser = userService.getUserCredentialsByUsername( "anonymous" );
                 }
-                
+
                 // forward to user group by SMS, E-mail, DHIS conversation
                 messageService.sendMessage( smsCommand.getName(), message, null, receivers, anonymousUser.getUser(),
                     false, false );
-                
+
                 // confirm SMS was received and forwarded completely
                 Set<User> feedbackList = new HashSet<User>();
                 User sender = new User();
@@ -189,5 +193,5 @@
     {
         this.incomingSmsService = incomingSmsService;
     }
-    
+
 }