dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #18756
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 7941: Re-implemented the parser with regex instead of sub-strings. Added default message and other mino...
------------------------------------------------------------
revno: 7941
committer: Magnus Korvald <korvald@xxxxxxxxxxx>
branch nick: trunk
timestamp: Mon 2012-08-20 15:46:56 +0200
message:
Re-implemented the parser with regex instead of sub-strings. Added default message and other minor changes.
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/smscommand/SMSCommand.java
dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/smscommand/SMSCommand.hbm.xml
dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/EditSMSCommandForm.java
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/sms-commands.vm
dhis-2/dhis-web/dhis-web-sms/src/main/java/org/hisp/dhis/sms/input/SMSInput.java
dhis-2/dhis-web/dhis-web-sms/src/main/java/org/hisp/dhis/sms/input/SMSParserKeyValue.java
dhis-2/dhis-web/dhis-web-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-api/src/main/java/org/hisp/dhis/smscommand/SMSCommand.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/smscommand/SMSCommand.java 2012-06-13 08:44:07 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/smscommand/SMSCommand.java 2012-08-20 13:46:56 +0000
@@ -16,6 +16,8 @@
private Set<SMSCode> codes;
private String codeSeperator;
+ private String defaultMessage;
+
public SMSCommand(String name, String parser, String seperator, DataSet dataset, Set<SMSCode> codes,
String codeSeperator) {
this.name = name;
@@ -114,5 +116,12 @@
this.codeSeperator = codeSeperator;
}
+ public String getDefaultMessage() {
+ return defaultMessage;
+ }
+
+ public void setDefaultMessage(String defaultMessage) {
+ this.defaultMessage = defaultMessage;
+ }
}
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/smscommand/SMSCommand.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/smscommand/SMSCommand.hbm.xml 2012-06-14 09:41:45 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/smscommand/SMSCommand.hbm.xml 2012-08-20 13:46:56 +0000
@@ -5,6 +5,7 @@
[<!ENTITY identifiableProperties SYSTEM "classpath://org/hisp/dhis/common/identifiableProperties.hbm">]
>
<hibernate-mapping package="org.hisp.dhis.smscommand">
+
<class name="SMSCommand" table="smscommands">
<cache usage="read-write" />
<id name="id" column="smscommandid">
@@ -14,6 +15,7 @@
<property name="parser" type="text" />
<property name="seperator" type="text" />
<property name="codeSeperator" type="text" />
+ <property name="defaultMessage" type="text" />
<many-to-one name="dataset" class="org.hisp.dhis.dataset.DataSet" column="datasetid" foreign-key="fk_dataset_datasetid" />
<set name="codes" table="smscommandcodes">
@@ -21,6 +23,6 @@
<key column="id" />
<many-to-many class="org.hisp.dhis.smscommand.SMSCode" column="codeid" unique="true" />
</set>
-
</class>
+
</hibernate-mapping>
\ No newline at end of file
=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/EditSMSCommandForm.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/EditSMSCommandForm.java 2012-07-30 10:56:25 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/EditSMSCommandForm.java 2012-08-20 13:46:56 +0000
@@ -36,6 +36,8 @@
private String codeSeperator;
+ private String defaultMessage;
+
private int selectedCommandID = -1;
// -------------------------------------------------------------------------
@@ -67,11 +69,12 @@
SMSCommand c = getSMSCommand();
if ( selectedDataSetID > -1 && c != null )
{
- c.setDataset( getDataSetService().getDataSet( getSelectedDataSetID() ) );
+ // c.setDataset( getDataSetService().getDataSet( getSelectedDataSetID() ) );
c.setName( name );
c.setSeperator( seperator );
c.setCodes( codeSet );
- c.setCodeSeperator( codeSeperator );
+ // c.setCodeSeperator( codeSeperator );
+ c.setDefaultMessage( defaultMessage );
smsCommandService.save( c );
}
@@ -96,7 +99,6 @@
public SMSCommand getSMSCommand()
{
return smsCommandService.getSMSCommand( selectedCommandID );
-
}
public DataSetService getDataSetService()
@@ -188,4 +190,12 @@
{
this.codeSeperator = codeSeperator;
}
+
+ public String getDefaultMessage() {
+ return defaultMessage;
+ }
+
+ public void setDefaultMessage(String defaultMessage) {
+ this.defaultMessage = defaultMessage;
+ }
}
\ No newline at end of file
=== 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 2012-07-22 12:04:11 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/smscommand/edit-sms-command.vm 2012-08-20 13:46:56 +0000
@@ -26,8 +26,6 @@
* OID
-->
-
-
<table>
<thead>
<tr>
@@ -39,33 +37,21 @@
<input type="hidden" value="$selectedCommandID" name="selectedCommandID" />
</td>
</tr>
+
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="$SMSCommand.name" style="width:20em" /></td>
</tr>
<tr>
- <td>DataSet:</td>
+ <td>Code and value Separator:</td>
<td>
-
- <!-- onchange="window.location.href='editSMSCommand.action?selectedDataSetID=' + this.options[this.selectedIndex].value" -->
- <select name="selectedDataSetID" style="min-width:20em; margin:0;" >
- #foreach( $dataSet in $dataSets )
- <option value="$dataSet.id" #if ($SMSCommand.dataset.id == $dataSet.id) selected #end >$dataSet.name</option>
- #end
- </select>
+ <input type="text" #if($SMSCommand.seperator) value="$SMSCommand.seperator" #end name="seperator" style="width:20em" />
</td>
</tr>
- <tr>
- <td>Separator:</td>
- <td>
- <input type="text" #if($SMSCommand.seperator) value="$SMSCommand.seperator" #end name="seperator" style="width:20em" />
- </td>
- </tr>
- <tr>
- <td>Code seperator:</td>
- <td><input type="text" #if($SMSCommand.codeSeperator) value="$SMSCommand.codeSeperator" #end name="codeSeperator" style="width:20em" /></td>
- </tr>
-
+ <tr>
+ <td>Reply message if no codes <br /> are sent (only the command):</td>
+ <td><textarea type="text" name="defaultMessage">#if($SMSCommand.defaultMessage)$SMSCommand.defaultMessage#end</textarea></td>
+ </tr>
</table>
</br>
=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/smscommand/sms-commands.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/smscommand/sms-commands.vm 2012-06-27 07:25:34 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/smscommand/sms-commands.vm 2012-08-20 13:46:56 +0000
@@ -11,10 +11,9 @@
<td style="vertical-align:top">
<table width="100%">
<tr>
-
- <td >
- <input type="button" value="New command" onclick="window.location.href='newSMSCommand.action'" style="width:100px"/></a>
- </td>
+ <td >
+ <input type="button" value="New command" onclick="window.location.href='newSMSCommand.action'" style="width:100px"/></a>
+ </td>
</tr>
</table>
=== modified file 'dhis-2/dhis-web/dhis-web-sms/src/main/java/org/hisp/dhis/sms/input/SMSInput.java'
--- dhis-2/dhis-web/dhis-web-sms/src/main/java/org/hisp/dhis/sms/input/SMSInput.java 2012-07-22 12:04:11 +0000
+++ dhis-2/dhis-web/dhis-web-sms/src/main/java/org/hisp/dhis/sms/input/SMSInput.java 2012-08-20 13:46:56 +0000
@@ -16,7 +16,8 @@
import org.hisp.dhis.sms.incoming.IncomingSmsStore;
import org.hisp.dhis.sms.incoming.SmsMessageEncoding;
import org.hisp.dhis.sms.incoming.SmsMessageStatus;
-import org.hisp.dhis.sms.output.SMSOutput;
+import org.hisp.dhis.sms.outbound.OutboundSms;
+import org.hisp.dhis.sms.outbound.OutboundSmsService;
import org.hisp.dhis.smscommand.SMSCode;
import org.hisp.dhis.smscommand.SMSCommand;
import org.hisp.dhis.smscommand.SMSCommandService;
@@ -54,11 +55,16 @@
private UserService userService;
private SMSCommandService smsCommandService;
-
+
@Autowired
private DataElementCategoryService dataElementCategoryService;
-
- private SMSOutput smsOutput;
+
+ private OutboundSmsService outboundSmsService;
+
+ public void setOutboundSmsService( OutboundSmsService outboundSmsService )
+ {
+ this.outboundSmsService = outboundSmsService;
+ }
public SMSInput()
{
@@ -93,8 +99,7 @@
sms.setId( msg_id );
sms.setGatewayId( "HARDCODEDTESTGATEWAY" );
- int result = smsStore.save( sms );
- System.out.println( "The result of SMS save is *trommevirvel* " + result );
+ smsStore.save( sms );
OrganisationUnit orgunit = null;
for ( User user : userService.getUsersByPhoneNumber( sender ) )
@@ -103,7 +108,6 @@
// Might be undefined if the user has more than one org.units
// "attached"
-
if ( orgunit == null )
{
orgunit = ou;
@@ -114,34 +118,50 @@
}
else
{
- // orgunit and ou are different, ie. the phone number is
- // registered to users at multiple facilities.
- // Now what should we do?
- System.out.println( "user is registered to more than one orgunit, what orgunit should we pick?" );
+ sendSMS( "User is associated with more than one orgunit. Please contact your supervisor." );
return ERROR;
}
}
+ if ( orgunit == null )
+ {
+ sendSMS( "No user associated with this phone number. Please contact your supervisor." );
+ return ERROR;
+ }
+
String[] marr = message.trim().split( " " );
if ( marr.length < 1 )
{
+ sendSMS("No marr");
return ERROR;
}
String commandString = marr[0];
+ boolean foundCommand = false;
for ( SMSCommand command : smsCommandService.getSMSCommands() )
{
if ( command.getName().equalsIgnoreCase( commandString ) )
{
+ foundCommand = true;
// Insert message type handler later :)
- IParser p = new SMSParserKeyValue( command.getSeperator(), " ", " ", true, false );
+ SMSParserKeyValue p = new SMSParserKeyValue();
+ if ( !StringUtils.isBlank( command.getSeperator() ) )
+ {
+ p.setSeparator( command.getSeperator() );
+ }
+
Map<String, String> parsedMessage = p.parse( message );
-
+ if ( parsedMessage.isEmpty() )
+ {
+ sendSMS( command.getDefaultMessage() );
+ return ERROR;
+ }
for ( SMSCode code : command.getCodes() )
{
String upperCaseCode = code.getCode().toUpperCase();
- if ( parsedMessage.containsKey( upperCaseCode ) ) // Or fail hard???
+ if ( parsedMessage.containsKey( upperCaseCode ) ) // Or fail
+ // hard???
{
String storedBy = currentUserService.getCurrentUsername();
@@ -189,14 +209,33 @@
}
}
}
-
-
- smsOutput.sendSMS( "Your sms has been received", sender );
+ if ( foundCommand )
+ {
+ sendSMS( "SMS successfully received" );
+ }
+ else
+ {
+ sendSMS( "Command '" + commandString + "' does not exist" );
+ return ERROR;
+ }
// TODO DataEntry stuff
return SUCCESS;
}
+ private void sendSMS( String message )
+ {
+ if ( outboundSmsService != null )
+ {
+ outboundSmsService.sendMessage( new OutboundSms( message, sender ), null );
+ }
+ else
+ {
+ // Just for testing
+ System.out.println( "\n\n\n SMS: " + message + "\n\n\n" );
+ }
+ }
+
public void setDataElementCategoryService( DataElementCategoryService dataElementCategoryService )
{
this.dataElementCategoryService = dataElementCategoryService;
@@ -358,9 +397,4 @@
this.source_id = source_id;
}
- public void setSmsOutput( SMSOutput smsOutput )
- {
- this.smsOutput = smsOutput;
- }
-
}
=== modified file 'dhis-2/dhis-web/dhis-web-sms/src/main/java/org/hisp/dhis/sms/input/SMSParserKeyValue.java'
--- dhis-2/dhis-web/dhis-web-sms/src/main/java/org/hisp/dhis/sms/input/SMSParserKeyValue.java 2012-07-22 12:04:11 +0000
+++ dhis-2/dhis-web/dhis-web-sms/src/main/java/org/hisp/dhis/sms/input/SMSParserKeyValue.java 2012-08-20 13:46:56 +0000
@@ -6,163 +6,46 @@
import java.util.HashMap;
import java.util.Map;
+import java.util.regex.MatchResult;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
- *
- * @author Christian
+ * @author Magnus Korvald
*/
public class SMSParserKeyValue
implements IParser
{
- private String separatorBetweenKeyValuePairs = " "; // The separator between
- // two different keys,
- // ie. 'ANC10 BCG15'
- // separator being ' '
-
- private String separatorBetweenKeyAndValue = "";
-
- private boolean canUseNumbersInKey = false;
-
- private boolean firstWordIsKeyWord = true;
-
- private String separatorForDataEntryKeyword = " ";
+ private String defaultPattern = "([a-zA-Z]+)\\s*(\\d+)";
+
+ private Pattern pattern = Pattern.compile( defaultPattern );
@Override
public Map<String, String> parse( String sms )
{
- System.out.println( "SMS Input is: '" + sms + "'" );
- sms = sms.replaceAll( "\r\n", " " );
- sms = sms.replaceAll( "\r", "" );
- sms = sms.replaceAll( "\n", " " );
HashMap<String, String> output = new HashMap<String, String>();
- if ( firstWordIsKeyWord )
+ Matcher m = pattern.matcher( sms );
+ while ( m.find() )
{
- int reportKeyWordLength = sms.indexOf( separatorForDataEntryKeyword );
- String dataEntryKeyword = sms.substring( 0, reportKeyWordLength );
- System.out.println( "Report keyword is: " + dataEntryKeyword );
- output.put( DATA_ENTRY_KEYWORD, dataEntryKeyword.toUpperCase() );
- sms = sms.substring( reportKeyWordLength ); // Remove the keyword
-
- if ( sms.startsWith( separatorForDataEntryKeyword ) )
+ String key = m.group( 1 );
+ String value = m.group( 2 );
+ System.out.println( "Key: " + key + " Value: " + value );
+ if ( key != null && value != null )
{
- sms = sms.replaceFirst( separatorForDataEntryKeyword, "" ); // remove
- // the
- // keyword
- // separator
- // at
- // the
- // beginning
- }
- System.out.println( "SMS is now: '" + sms + "'" );
- }
- // Now move onto the message/data entry part
- sms = sms.trim();
- String[] smsArr = sms.split( separatorBetweenKeyValuePairs );
-
- for ( int i = 0; i < smsArr.length; i++ )
- {
- String s = smsArr[i];
- s = s.trim();
-
- if ( !canUseNumbersInKey && separatorBetweenKeyAndValue.isEmpty() )
- { // Can't use numbers in key and no sep between key and value
- for ( int j = 0; j < s.length(); j++ )
- {
- if ( Character.isDigit( s.charAt( j ) ) )
- { // If char is a digit and we can't use numbers in key
- String key = s.substring( 0, j ).trim().toUpperCase();
- String value = s.substring( j );
-
- output.put( key, value.trim() );
- System.out.println( "Found Key: " + key + " Value: " + value );
- System.out.println( "" );
- break;
- }
- }
- }
- else if ( !separatorBetweenKeyAndValue.isEmpty() )
- { // When there is a separator between key and value you can
- // implisitly use numbers in key.
- String[] split = s.split( separatorBetweenKeyAndValue );
- if ( split.length != 2 )
- continue;
- String key = split[0].trim().toUpperCase();
- String value = split[1];
- output.put( key, value.trim() );
- }
-
+ output.put( key.toUpperCase(), value );
+ }
}
return output;
}
- private boolean containsIllegalChars( String input )
- {
- if ( input.indexOf( separatorBetweenKeyAndValue ) != -1 )
- {
- return true;
- }
- else if ( input.indexOf( separatorBetweenKeyValuePairs ) == -1 )
- { // Perhaps just remove it? though we can't be sure if it's correct
- return true;
- }
- else if ( input.indexOf( separatorForDataEntryKeyword ) == -1 )
- {
- return true;
- }
-
- return false;
- }
-
- public SMSParserKeyValue()
- {
- }
-
- public SMSParserKeyValue( String sepBetwKeyAndValue, String sepBetwKeyValuePairs, String sepForDataEntryKeyword,
- boolean firstWordIsKeyWord, boolean canUseNumbersInKey )
- {
- this.separatorBetweenKeyAndValue = sepBetwKeyAndValue;
- this.separatorBetweenKeyValuePairs = sepBetwKeyValuePairs;
- this.separatorForDataEntryKeyword = sepForDataEntryKeyword;
-
- this.canUseNumbersInKey = canUseNumbersInKey;
- this.firstWordIsKeyWord = firstWordIsKeyWord;
-
- if ( !sepBetwKeyAndValue.isEmpty() )
- this.canUseNumbersInKey = true;
- }
-
- public void setCanUseNumbersInKey( boolean canUseNumbersInKey )
- {
- this.canUseNumbersInKey = canUseNumbersInKey;
- }
-
- public void setFirstWordIsKeyWord( boolean firstWordIsKeyWord )
- {
- this.firstWordIsKeyWord = firstWordIsKeyWord;
- }
-
- public void setSeparatorBetweenKeyAndValue( String separatorBetweenKeyAndValue )
- {
- this.separatorBetweenKeyAndValue = separatorBetweenKeyAndValue;
- if ( separatorBetweenKeyAndValue != null && !separatorBetweenKeyAndValue.isEmpty() )
- {
- canUseNumbersInKey = true;
- }
-
- }
-
- public void setSeparatorBetweenKeyValuePairs( String separatorBetweenKeyValuePairs )
- {
- this.separatorBetweenKeyValuePairs = separatorBetweenKeyValuePairs;
- }
-
- public void setSeparatorForDataEntryKeyword( String separatorForDataEntryKeyword )
- {
- this.separatorForDataEntryKeyword = separatorForDataEntryKeyword;
+ public void setSeparator( String separator )
+ {
+ String x = "(\\w+)\\s*:\\s*(\\d+)";
+ pattern = Pattern.compile( x );
}
}
=== modified file 'dhis-2/dhis-web/dhis-web-sms/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-sms/src/main/resources/META-INF/dhis/beans.xml 2012-08-07 08:29:20 +0000
+++ dhis-2/dhis-web/dhis-web-sms/src/main/resources/META-INF/dhis/beans.xml 2012-08-20 13:46:56 +0000
@@ -24,12 +24,10 @@
<property name="smsCommandService" ref="smsCommandService" />
<property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService"/>
<property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService"/>
- <property name="smsOutput" ref="org.hisp.dhis.sms.output.BulkSMSOutput" /> <!-- replace later -->
</bean>
<bean id="org.hisp.dhis.sms.output.BulkSMSOutput" class="org.hisp.dhis.sms.output.BulkSMSOutput" />
-
<!-- Data entry -->
<bean id="org.hisp.dhis.sms.dataentry.utils.FormUtils"