← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17627: [mobile] support formula for SMSCode

 

------------------------------------------------------------
revno: 17627
committer: Long Ngo Thanh <thanhlongngo1988@xxxxxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-12-01 09:32:57 +0700
message:
  [mobile] support formula for SMSCode
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/parse/ParserType.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/smscommand/SMSCode.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/smscommand/SMSCommand.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/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sms/hibernate/SMSCode.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/java/org/hisp/dhis/mobile/action/smscommand/SMSCommandAction.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	2014-07-30 04:28:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sms/parse/ParserType.java	2014-12-01 02:32:57 +0000
@@ -35,5 +35,6 @@
     ALERT_PARSER,
     UNREGISTERED_PARSER,
     ANONYMOUS_PROGRAM_PARSER,
-    TRACKED_ENTITY_REGISTRATION_PARSER
+    TRACKED_ENTITY_REGISTRATION_PARSER,
+    ADVANCE_KEY_VALUE_PARSER
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/smscommand/SMSCode.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/smscommand/SMSCode.java	2014-08-04 13:20:47 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/smscommand/SMSCode.java	2014-12-01 02:32:57 +0000
@@ -42,6 +42,8 @@
     private TrackedEntityAttribute trackedEntityAttribute;
 
     private int optionId;
+    
+    private String formula;
 
     public SMSCode( String code, DataElement dataElement, int optionId )
     {
@@ -110,6 +112,14 @@
     {
         this.trackedEntityAttribute = trackedEntityAttribute;
     }
-    
-    
+
+    public String getFormula()
+    {
+        return formula;
+    }
+
+    public void setFormula( String formula )
+    {
+        this.formula = formula;
+    }
 }

=== 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	2014-09-10 04:36:51 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/smscommand/SMSCommand.java	2014-12-01 02:32:57 +0000
@@ -37,24 +37,24 @@
 
 public class SMSCommand
 {
-    //Default message
-    
+    // Default message
+
     public static final String WRONG_FORMAT_MESSAGE = "Wrong format for command";
-    
+
     public static final String MORE_THAN_ONE_ORGUNIT_MESSAGE = "Found more than one org unit for this number. Please specify one organisation unit";
-    
+
     public static final String NO_USER_MESSAGE = "No user associated with this phone number. Please contact your supervisor.";
-    
+
     public static final String ALERT_FEEDBACK = "Your alert message sent";
-    
-    //Completeness method code
-    
+
+    // Completeness method code
+
     public static final int RECEIVE_ALL_DATAVALUE = 1;
-    
+
     public static final int RECEIVE_AT_LEAST_ONE_DATAVALUE = 2;
-    
+
     public static final int DO_NOT_MARK_COMPLETE = 3;
-    
+
     private int id;
 
     private String name;
@@ -64,46 +64,46 @@
     private ParserType parserType;
 
     private String separator;
-    
-    //Dataset
+
+    // Dataset
 
     private DataSet dataset;
 
     private Set<SMSCode> codes;
 
     private String codeSeparator;
-    
-    //Usergroup
+
+    // Usergroup
 
     private UserGroup userGroup;
-    
-    //Program
-    
+
+    // Program
+
     private Program program;
 
     private Set<SMSSpecialCharacter> specialCharacters;
 
     private boolean currentPeriodUsedForReporting = false; // default is prev
-    
+
     private Integer completenessMethod;
-    
-    //Messages
-    
+
+    // Messages
+
     private String defaultMessage;
 
     private String receivedMessage;
-    
+
     private String wrongFormatMessage;
-    
+
     private String noUserMessage;
-    
+
     private String moreThanOneOrgUnitMessage;
-    
+
     private String successMessage;
-    
 
     public SMSCommand( String name, String parser, ParserType parserType, String separator, DataSet dataset,
-        Set<SMSCode> codes, String codeSeparator, String defaultMessage, UserGroup userGroup, String receivedMessage, Set<SMSSpecialCharacter> specialCharacters )
+        Set<SMSCode> codes, String codeSeparator, String defaultMessage, UserGroup userGroup, String receivedMessage,
+        Set<SMSSpecialCharacter> specialCharacters )
     {
         super();
         this.name = name;

=== 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	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/DataValueSMSListener.java	2014-12-01 02:32:57 +0000
@@ -47,6 +47,7 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.dataset.CompleteDataSetRegistration;
 import org.hisp.dhis.dataset.CompleteDataSetRegistrationService;
 import org.hisp.dhis.dataset.DataSet;
@@ -92,6 +93,8 @@
 
     private IncomingSmsService incomingSmsService;
 
+    private DataElementService dataElementService;
+
     @Transactional
     @Override
     public boolean accept( IncomingSms sms )
@@ -420,6 +423,77 @@
             }
         }
 
+        if ( code.getFormula() != null )
+        {
+            try
+            {
+
+                // +de
+                String formula = code.getFormula();
+
+                String targetDataElementId = formula.substring( 1, formula.length() );
+                String operation = String.valueOf( formula.charAt( 0 ) );
+
+                System.out.println( "Operation: " + operation );
+
+                DataElement targetDataElement = dataElementService.getDataElement( Integer
+                    .parseInt( targetDataElementId ) );
+
+                if ( targetDataElement == null )
+                {
+                    return false;
+                }
+
+                DataValue targetDataValue = dataValueService.getDataValue( targetDataElement, period, orgunit,
+                    dataElementCategoryService.getDefaultDataElementCategoryOptionCombo() );
+                int targetValue = 0;
+                boolean newTargetDataValue = false;
+                if ( targetDataValue == null )
+                {
+                    targetDataValue = new DataValue();
+                    targetDataValue.setCategoryOptionCombo( dataElementCategoryService
+                        .getDefaultDataElementCategoryOptionCombo() );
+                    targetDataValue.setSource( orgunit );
+                    targetDataValue.setDataElement( targetDataElement );
+                    targetDataValue.setPeriod( period );
+                    targetDataValue.setComment( "" );
+                    newTargetDataValue = true;
+                }
+                else
+                {
+                    targetValue = Integer.parseInt( targetDataValue.getValue() );
+                }
+
+                if ( operation.equals( "+" ) )
+                {
+                    targetValue = targetValue + Integer.parseInt( value );
+                }
+                else if ( operation.equals( "-" ) )
+                {
+                    targetValue = targetValue - Integer.parseInt( value );
+                }
+
+
+                targetDataValue.setValue( String.valueOf( targetValue ) );
+                targetDataValue.setLastUpdated( new java.util.Date() );
+                targetDataValue.setStoredBy( storedBy );
+                if ( newTargetDataValue )
+                {
+                    dataValueService.addDataValue( targetDataValue );
+                }
+                else
+                {
+                    dataValueService.updateDataValue( targetDataValue );
+                }
+
+            }
+            catch ( Exception e )
+            {
+                e.printStackTrace();
+                return false;
+            }
+        }
+
         return true;
     }
 
@@ -586,14 +660,14 @@
         }
         else
         {
-            if ( codesWithoutDataValues.size() > 0 )
-            {
-                smsSender.sendMessage( reportBack + notInReport, sender );
-            }
-            else
-            {
-                smsSender.sendMessage( reportBack, sender );
-            }
+            // if ( codesWithoutDataValues.size() > 0 )
+            // {
+            // smsSender.sendMessage( reportBack + notInReport, sender );
+            // }
+            // else
+            // {
+            smsSender.sendMessage( reportBack, sender );
+            // }
         }
 
     }
@@ -711,4 +785,15 @@
     {
         this.incomingSmsService = incomingSmsService;
     }
+
+    public DataElementService getDataElementService()
+    {
+        return dataElementService;
+    }
+
+    public void setDataElementService( DataElementService dataElementService )
+    {
+        this.dataElementService = dataElementService;
+    }
+
 }

=== 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	2014-11-30 10:30:06 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2014-12-01 02:32:57 +0000
@@ -870,6 +870,7 @@
     <property name="userService" ref="org.hisp.dhis.user.UserService" />
     <property name="dataSetService" ref="org.hisp.dhis.dataset.DataSetService" />
     <property name="incomingSmsService" ref="org.hisp.dhis.sms.incoming.IncomingSmsService" />
+    <property name="dataElementService" ref="org.hisp.dhis.dataelement.DataElementService" />
   </bean>
 
   <bean id="org.hisp.dhis.sms.J2MEDataValueSMSListener" class="org.hisp.dhis.sms.J2MEDataValueSMSListener">

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sms/hibernate/SMSCode.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sms/hibernate/SMSCode.hbm.xml	2014-07-30 07:07:31 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/sms/hibernate/SMSCode.hbm.xml	2014-12-01 02:32:57 +0000
@@ -12,6 +12,9 @@
     </id>
 
     <property name="code" type="text" />
+    
+    <property name="formula" type="text" />
+    
     <many-to-one name="dataElement" class="org.hisp.dhis.dataelement.DataElement" column="dataelementid"
       foreign-key="fk_dataelement_dataelementid" />
       

=== 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	2014-10-23 16:38:54 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/EditSMSCommandForm.java	2014-12-01 02:32:57 +0000
@@ -56,7 +56,7 @@
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
-    
+
     private SMSCommandService smsCommandService;
 
     public void setSmsCommandService( SMSCommandService smsCommandService )
@@ -154,6 +154,14 @@
             c.setCode( x.getString( "code" ) );
             c.setDataElement( dataElementService.getDataElement( x.getInt( "dataElementId" ) ) );
             c.setOptionId( x.getInt( "optionId" ) );
+            if ( !x.getString( "formula" ).trim().equals( "" ) )
+            {
+                c.setFormula( x.getString( "formula" ) );
+            }
+            else
+            {
+                c.setFormula( null );
+            }
             codeSet.add( c );
         }
 

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/SMSCommandAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/SMSCommandAction.java	2014-10-16 06:17:19 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/java/org/hisp/dhis/mobile/action/smscommand/SMSCommandAction.java	2014-12-01 02:32:57 +0000
@@ -139,6 +139,18 @@
         this.codes = codes;
     }
 
+    private Map<String, String> formulas = new HashMap<>();
+
+    public Map<String, String> getFormulas()
+    {
+        return formulas;
+    }
+
+    public void setFormulas( Map<String, String> formulas )
+    {
+        this.formulas = formulas;
+    }
+
     public ParserType[] getParserType()
     {
         return ParserType.values();
@@ -170,6 +182,11 @@
                     codes.put( "" + x.getDataElement().getId() + x.getOptionId(), x.getCode() );
                 }
 
+                if ( x.getFormula() != null )
+                {
+                    formulas.put( "" + x.getDataElement().getId() + x.getOptionId(), x.getFormula() );
+                }
+
             }
         }
         userGroupList = new ArrayList<>( userGroupService.getAllUserGroups() );

=== 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	2014-10-23 16:38:54 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/javascript/command.js	2014-12-01 02:32:57 +0000
@@ -50,3 +50,49 @@
 {
 	jQuery("[name=trSpecialCharacter" + rowId + "]").remove();
 }
+
+function openFormulaForm(displayName, index)
+{
+	$("#displayName").html(displayName);
+	$("#index").val(index);
+	
+	$("#removeButton").hide();
+	$('#targetDataElement option').prop('selected', false);
+	$('#operator option').prop('selected', false);
+	$("#selectedTargetDataElement").html($("#targetDataElement option:selected").text());
+	
+	var formulaText = $("#" + "formula" + index).val();
+
+	if (formulaText != "") {
+		var operator = formulaText.substring(0,1);
+		var dataElementId = formulaText.substring(1,formulaText.length);
+		
+		$("#removeButton").show();
+		$('#targetDataElement  option[value="' + dataElementId + '"]').prop("selected", true);
+		$('#operator  option[value="' + operator + '"]').prop("selected", true);
+		$("#selectedTargetDataElement").html($("#targetDataElement option:selected").text());
+	}
+	
+	dialog.dialog("option", "title", "Formula Form");
+	dialog.dialog("open");
+}
+	
+function collectFormula() {
+	var operator = $("#operator option:selected").text();
+	var deId = $("#targetDataElement option:selected").val();
+	var index = $("#index").val();
+	$("#addFormula" + index).val("Edit Formula");
+	$("#" + "formula" + index).val(operator + "" + deId);
+	dialog.dialog( "close" );
+}
+
+function closeFormulaForm() {
+	dialog.dialog( "close" );
+}
+
+function closeAndFormulaForm() {
+	var index = $("#index").val();
+	$("#" + "formula" + index).val("");
+	$("#addFormula" + index).val("Add Formula");
+	dialog.dialog( "close" );
+}

=== 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	2014-10-23 16:38:54 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-mobile/src/main/webapp/dhis-web-maintenance-mobile/smscommand/edit-sms-command.vm	2014-12-01 02:32:57 +0000
@@ -12,7 +12,8 @@
 		{
 			selectedDataOptions += '{"dataElementId" :' + document.getElementById('codeId'+i).value.split('.')[0] +  ','; 
 			selectedDataOptions += '"optionId" :' + document.getElementById('codeId'+i).value.split('.')[1] +  ','; 
-			selectedDataOptions += '"code" :"' + document.getElementById('codeValue'+i).value + '"},';
+			selectedDataOptions += '"code" :"' + document.getElementById('codeValue'+i).value + '",';
+			selectedDataOptions += '"formula" :"' + document.getElementById('formula'+i).value + '"},';
 		}
 		selectedDataOptions += ']}';
 		$("#codeDataelementOption").val(selectedDataOptions);
@@ -118,7 +119,15 @@
 		changeParserType( getFieldValue( 'parserType' ) );
 		validation( "updateSMSCommandForm" );
 		checkValueIsExist( "name", "validateSMSCommand.action", {id:"$selectedCommandID"});
+		
+		$("#targetDataElement").change( function() {
+			$("#selectedTargetDataElement").html($("#targetDataElement option:selected").text());
+		});
+		
 	});
+	
+
+	
 </script>
 
 <h3>$i18n.getString( "edit_command" )</h3>
@@ -235,6 +244,16 @@
 								<input type="hidden" id="codeId$index" name="codeId$index" value="$dataElement.id.$x.id"/>
 								<input type="text" id="codeValue$index" name="codeValue$index" class="{validate:{required:true}}" value='$!codes[$str]' onblur="checkDuplicatedCode(this.value, $index)"/>
 						   </td>
+						   <td>
+								#if($dataElement.type == "int" || $dataElement.type == "number")
+									#if ($formulas[$str])
+										<input type="button" name="addFormula$index" id="addFormula$index" value="Edit Formula" onclick="openFormulaForm('$dataElement.name $x.name', '$index')"/>
+									#else
+										<input type="button" name="addFormula$index" id="addFormula$index" value="Add Formula" onclick="openFormulaForm('$dataElement.name $x.name', '$index')"/>
+									#end
+								#end
+								<input type="hidden" id="formula$index" value='$!formulas[$str]'/>
+						   </td>
                        </tr>
 				#end
             #else
@@ -291,4 +310,46 @@
 	
     <input type="button" style="width: 100px" onclick="prepSubmit()" value="$i18n.getString( 'save' )" />
     <input type="button" id="btnBack" name="btnBack" value="$i18n.getString('cancel')" style="width:8em" onclick="window.location.href='SMSCommands.action'"/>
-</form>
\ No newline at end of file
+</form>
+
+<script type="text/javascript">
+var i18n_no_constant_to_select = '$encoder.jsEscape( $i18n.getString( "no_constant_to_select" ) , "'")';
+var i18n_no_dataelement_to_select = '$encoder.jsEscape( $i18n.getString( "no_dataelement_to_select" ) , "'")';
+var i18n_edit_numerator = '$encoder.jsEscape( $i18n.getString( "edit_numerator" ) , "'")';
+var i18n_edit_denominator = '$encoder.jsEscape( $i18n.getString( "edit_denominator" ) , "'")';
+
+jQuery( document ).ready( function(){
+
+	dialog = jQuery("#formula-form-container").dialog({
+		modal: true,
+		autoOpen: false,
+		minWidth: 500,
+		width: 500
+	});
+});
+</script>
+
+<div id="formula-form-container">
+	<select id="targetDataElement">
+		<option value="">Please select</option>
+		#foreach( $dataElement in $dataElements)
+			#if($dataElement.type == "int" || $dataElement.type == "number")
+				<option value="$dataElement.id">$dataElement.name</option>
+			#end
+		#end
+	<select/><br>
+	= <br>
+	<label id="selectedTargetDataElement"></label><br>
+	<select name="operator" id="operator">
+		<option value="">Please select</option>
+		<option value="+">+</option>
+		<option value="-">-</option>
+	<select><br>
+	<label id="displayName"></label><br>
+	<input type="hidden" id="index"/>
+	<div>
+		<input type="button" value="$i18n.getString( 'save' )" style="width:125px" onclick="collectFormula()"/>
+		<input type="button" value="$i18n.getString( 'cancel' )" style="width:125px" onclick="closeFormulaForm()"/>
+		<input type="button" name="removeButton" id="removeButton" value="$i18n.getString( 'remove' )" style="width:125px" onclick="closeAndFormulaForm()"/>
+	</div>
+</div>
\ No newline at end of file