← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 8354: [mobile] add program attribue and identifier in enrollment form

 

------------------------------------------------------------
revno: 8354
committer: Long <Long@Long-Laptop>
branch nick: dhis2
timestamp: Wed 2012-10-03 14:23:08 +0700
message:
  [mobile] add program attribue and identifier in enrollment form
modified:
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryenrollment/action/GetProgramEnrollmentFormAction.java
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryenrollment/action/SaveMobileProgramEnrollmentAction.java
  dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryregistration/action/SaveBeneficiaryAction.java
  dhis-2/dhis-web/dhis-web-light/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/enrollment/programEnrollmentForm.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-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryenrollment/action/GetProgramEnrollmentFormAction.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryenrollment/action/GetProgramEnrollmentFormAction.java	2012-06-27 10:03:43 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryenrollment/action/GetProgramEnrollmentFormAction.java	2012-10-03 07:23:08 +0000
@@ -28,9 +28,12 @@
 package org.hisp.dhis.light.beneficiaryenrollment.action;
 
 import java.text.SimpleDateFormat;
+import java.util.Collection;
 import java.util.Date;
 
 import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientAttribute;
+import org.hisp.dhis.patient.PatientIdentifierType;
 import org.hisp.dhis.patient.PatientService;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramService;
@@ -109,10 +112,8 @@
     }
 
     private Program program;
-    
+
     private String now;
-    
-    
 
     public String getNow()
     {
@@ -124,13 +125,39 @@
         this.now = now;
     }
 
+    private Collection<PatientIdentifierType> patientIdentifierTypes;
+
+    public Collection<PatientIdentifierType> getPatientIdentifierTypes()
+    {
+        return patientIdentifierTypes;
+    }
+
+    public void setPatientIdentifierTypes( Collection<PatientIdentifierType> patientIdentifierTypes )
+    {
+        this.patientIdentifierTypes = patientIdentifierTypes;
+    }
+
+    private Collection<PatientAttribute> patientAttributes;
+
+    public Collection<PatientAttribute> getPatientAttributes()
+    {
+        return patientAttributes;
+    }
+
+    public void setPatientAttributes( Collection<PatientAttribute> patientAttributes )
+    {
+        this.patientAttributes = patientAttributes;
+    }
+
     @Override
     public String execute()
         throws Exception
     {
         patient = patientService.getPatient( patientId );
         program = programService.getProgram( programId );
-        now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
+        patientAttributes = program.getPatientAttributes();
+        patientIdentifierTypes = program.getPatientIdentifierTypes();
+        now = new SimpleDateFormat( "yyyy-MM-dd" ).format( new Date() );
 
         return SUCCESS;
     }

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryenrollment/action/SaveMobileProgramEnrollmentAction.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryenrollment/action/SaveMobileProgramEnrollmentAction.java	2012-08-16 15:47:08 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryenrollment/action/SaveMobileProgramEnrollmentAction.java	2012-10-03 07:23:08 +0000
@@ -1,12 +1,27 @@
 package org.hisp.dhis.light.beneficiaryenrollment.action;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import org.apache.commons.lang.math.NumberUtils;
+import org.apache.struts2.ServletActionContext;
 import org.hisp.dhis.light.utils.FormUtils;
 import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientAttribute;
+import org.hisp.dhis.patient.PatientAttributeOption;
+import org.hisp.dhis.patient.PatientAttributeOptionService;
+import org.hisp.dhis.patient.PatientAttributeService;
+import org.hisp.dhis.patient.PatientIdentifier;
+import org.hisp.dhis.patient.PatientIdentifierService;
+import org.hisp.dhis.patient.PatientIdentifierType;
+import org.hisp.dhis.patient.PatientIdentifierTypeService;
 import org.hisp.dhis.patient.PatientService;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValue;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValueService;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramInstanceService;
@@ -15,10 +30,11 @@
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStageInstanceService;
 import org.hisp.dhis.system.util.DateUtils;
+import org.hisp.dhis.util.ContextUtils;
 import org.joda.time.format.DateTimeFormatter;
 import org.joda.time.format.ISODateTimeFormat;
-
 import com.opensymphony.xwork2.Action;
+import com.opensymphony.xwork2.ActionContext;
 
 public class SaveMobileProgramEnrollmentAction
     implements Action
@@ -68,6 +84,66 @@
         this.formUtils = formUtils;
     }
 
+    private PatientIdentifierTypeService patientIdentifierTypeService;
+
+    public PatientIdentifierTypeService getPatientIdentifierTypeService()
+    {
+        return patientIdentifierTypeService;
+    }
+
+    public void setPatientIdentifierTypeService( PatientIdentifierTypeService patientIdentifierTypeService )
+    {
+        this.patientIdentifierTypeService = patientIdentifierTypeService;
+    }
+
+    private PatientIdentifierService patientIdentifierService;
+
+    public PatientIdentifierService getPatientIdentifierService()
+    {
+        return patientIdentifierService;
+    }
+
+    public void setPatientIdentifierService( PatientIdentifierService patientIdentifierService )
+    {
+        this.patientIdentifierService = patientIdentifierService;
+    }
+
+    private PatientAttributeService patientAttributeService;
+
+    public PatientAttributeService getPatientAttributeService()
+    {
+        return patientAttributeService;
+    }
+
+    public void setPatientAttributeService( PatientAttributeService patientAttributeService )
+    {
+        this.patientAttributeService = patientAttributeService;
+    }
+
+    private PatientAttributeOptionService patientAttributeOptionService;
+
+    public PatientAttributeOptionService getPatientAttributeOptionService()
+    {
+        return patientAttributeOptionService;
+    }
+
+    public void setPatientAttributeOptionService( PatientAttributeOptionService patientAttributeOptionService )
+    {
+        this.patientAttributeOptionService = patientAttributeOptionService;
+    }
+
+    private PatientAttributeValueService patientAttributeValueService;
+
+    public PatientAttributeValueService getPatientAttributeValueService()
+    {
+        return patientAttributeValueService;
+    }
+
+    public void setPatientAttributeValueService( PatientAttributeValueService patientAttributeValueService )
+    {
+        this.patientAttributeValueService = patientAttributeValueService;
+    }
+
     // -------------------------------------------------------------------------
     // Input/Output
     // -------------------------------------------------------------------------
@@ -165,9 +241,43 @@
         this.validated = validated;
     }
 
+    private Collection<PatientIdentifierType> patientIdentifierTypes;
+
+    public Collection<PatientIdentifierType> getPatientIdentifierTypes()
+    {
+        return patientIdentifierTypes;
+    }
+
+    public void setPatientIdentifierTypes( Collection<PatientIdentifierType> patientIdentifierTypes )
+    {
+        this.patientIdentifierTypes = patientIdentifierTypes;
+    }
+
+    private Collection<PatientAttribute> patientAttributes;
+
+    public Collection<PatientAttribute> getPatientAttributes()
+    {
+        return patientAttributes;
+    }
+
+    public void setPatientAttributes( Collection<PatientAttribute> patientAttributes )
+    {
+        this.patientAttributes = patientAttributes;
+    }
+
     public String execute()
         throws Exception
     {
+        patient = patientService.getPatient( patientId );
+        program = programService.getProgram( programId );
+        patientAttributes = program.getPatientAttributes();
+        patientIdentifierTypes = program.getPatientIdentifierTypes();
+
+        List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+
+        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
+            ServletActionContext.HTTP_REQUEST );
+        Map<String, String> parameterMap = ContextUtils.getParameterMap( request );
         DateTimeFormatter sdf = ISODateTimeFormat.yearMonthDay();
 
         if ( !FormUtils.isDate( enrollmentDate ) )
@@ -180,6 +290,99 @@
             validationMap.put( "incidentDate", "is_invalid_date" );
         }
 
+        // Handle Attribute and Identifier
+
+        for ( PatientIdentifierType patientIdentifierType : patientIdentifierTypes )
+        {
+            {
+                String key = "IDT" + patientIdentifierType.getId();
+                String value = parameterMap.get( key );
+
+                PatientIdentifier duplicateId = null;
+
+                if ( !value.isEmpty() )
+                {
+                    duplicateId = patientIdentifierService.get( patientIdentifierType, value );
+                }
+
+                if ( value != null )
+                {
+                    if ( patientIdentifierType.isMandatory() && value.trim().equals( "" ) )
+                    {
+                        this.validationMap.put( key, "is_mandatory" );
+                    }
+                    else if ( patientIdentifierType.getType().equals( "number" ) && !FormUtils.isNumber( value ) )
+                    {
+                        this.validationMap.put( key, "is_invalid_number" );
+                    }
+                    else if ( duplicateId != null )
+                    {
+                        this.validationMap.put( key, "is_duplicate" );
+                    }
+                    else
+                    {
+                        PatientIdentifier patientIdentifier = new PatientIdentifier();
+                        patientIdentifier.setIdentifierType( patientIdentifierType );
+                        patientIdentifier.setPatient( patient );
+                        patientIdentifier.setIdentifier( value.trim() );
+                        patient.getIdentifiers().add( patientIdentifier );
+                    }
+
+                    this.previousValues.put( key, value );
+                }
+            }
+        }
+
+        for ( PatientAttribute patientAttribute : patientAttributes )
+        {
+            {
+                String key = "AT" + patientAttribute.getId();
+                String value = parameterMap.get( key ).trim();
+
+                if ( value != null )
+                {
+                    if ( patientAttribute.isMandatory() && value.trim().equals( "" ) )
+                    {
+                        this.validationMap.put( key, "is_mandatory" );
+                    }
+                    else if ( value.trim().length() > 0
+                        && patientAttribute.getValueType().equals( PatientAttribute.TYPE_INT )
+                        && !FormUtils.isInteger( value ) )
+                    {
+                        this.validationMap.put( key, "is_invalid_number" );
+                    }
+                    else if ( value.trim().length() > 0
+                        && patientAttribute.getValueType().equals( PatientAttribute.TYPE_DATE )
+                        && !FormUtils.isDate( value ) )
+                    {
+                        this.validationMap.put( key, "is_invalid_date" );
+                    }
+                    else
+                    {
+                        PatientAttributeValue patientAttributeValue = new PatientAttributeValue();
+
+                        if ( PatientAttribute.TYPE_COMBO.equalsIgnoreCase( patientAttribute.getValueType() ) )
+                        {
+                            PatientAttributeOption option = patientAttributeOptionService.get( NumberUtils.toInt(
+                                value, 0 ) );
+
+                            if ( option != null )
+                            {
+                                patientAttributeValue.setPatientAttributeOption( option );
+                            }
+                        }
+
+                        patientAttributeValue.setPatient( patient );
+                        patientAttributeValue.setPatientAttribute( patientAttribute );
+                        patientAttributeValue.setValue( value.trim() );
+                        patientAttributeValues.add( patientAttributeValue );
+                    }
+
+                    this.previousValues.put( key, value );
+                }
+            }
+        }
+
         if ( validationMap.size() > 0 )
         {
             previousValues.put( "enrollmentDate", enrollmentDate );
@@ -188,9 +391,8 @@
             return ERROR;
         }
 
-        patient = patientService.getPatient( patientId );
-
-        program = programService.getProgram(  programId );
+        this.saveAttributeValue( patientAttributeValues );
+        patientService.updatePatient( patient );
 
         Collection<ProgramInstance> programInstances = programInstanceService.getProgramInstances( patient, program,
             false );
@@ -247,8 +449,18 @@
                 programStageInstanceService.updateProgramStageInstance( programStageInstance );
             }
         }
+
         validated = true;
         return SUCCESS;
     }
 
+    private void saveAttributeValue( List<PatientAttributeValue> patientAttributeValues )
+    {
+        for ( PatientAttributeValue patientAttributeValue : patientAttributeValues )
+        {
+            patientAttributeValueService.savePatientAttributeValue( patientAttributeValue );
+        }
+
+    }
+
 }

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryregistration/action/SaveBeneficiaryAction.java'
--- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryregistration/action/SaveBeneficiaryAction.java	2012-09-27 06:54:00 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/beneficiaryregistration/action/SaveBeneficiaryAction.java	2012-10-03 07:23:08 +0000
@@ -452,8 +452,8 @@
                         PatientIdentifier patientIdentifier = new PatientIdentifier();
                         patientIdentifier.setIdentifierType( patientIdentifierType );
                         patientIdentifier.setPatient( patient );
+                        patientIdentifier.setIdentifier( value.trim() );
                         patientIdentifierSet.add( patientIdentifier );
-                        patientIdentifier.setIdentifier( value.trim() );
                     }
 
                     this.previousValues.put( key, value );

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-light/src/main/resources/META-INF/dhis/beans.xml	2012-10-01 04:36:31 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/resources/META-INF/dhis/beans.xml	2012-10-03 07:23:08 +0000
@@ -350,6 +350,14 @@
 			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
 		<property name="formUtils"
 			ref="org.hisp.dhis.light.dataentry.utils.FormUtils" />
+		<property name="patientAttributeValueService"
+			ref="org.hisp.dhis.patientattributevalue.PatientAttributeValueService" />
+		<property name="patientAttributeOptionService"
+			ref="org.hisp.dhis.patient.PatientAttributeOptionService" />
+		<property name="patientAttributeService"
+			ref="org.hisp.dhis.patient.PatientAttributeService" />
+		<property name="patientIdentifierService"
+			ref="org.hisp.dhis.patient.PatientIdentifierService" />
 	</bean>
 
 	<!-- Single Event -->

=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/enrollment/programEnrollmentForm.vm'
--- dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/enrollment/programEnrollmentForm.vm	2012-07-16 07:58:15 +0000
+++ dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/enrollment/programEnrollmentForm.vm	2012-10-03 07:23:08 +0000
@@ -12,8 +12,44 @@
 		<label>$!encoder.htmlEncode( $program.dateOfIncidentDescription ) [yyyy-MM-dd] </label>
 		#if( $validationMap.get( "incidentDate" ) )
            	<br /><span style="color: #990000;"> $i18n.getString($validationMap.get( "incidentDate" ))</span>
-     	#end
-    <input type="text" name="incidentDate" value="#if($previousValues.get("incidentDate"))$previousValues.get("incidentDate")#else$now#end" />
+		#end
+		<input type="text" name="incidentDate" value="#if($previousValues.get("incidentDate"))$previousValues.get("incidentDate")#else$now#end" />
+		
+		#foreach($patientIdentifierType in $patientIdentifierTypes)
+				#set($key = "IDT${patientIdentifierType.id}")
+				<label>$patientIdentifierType.getName()#if( $patientIdentifierType.isMandatory() )*#end </label>
+				#if( $validationMap.get( $key ) )
+		           	<br /><span style="color: #990000;"> $i18n.getString($validationMap.get( $key ))</span>
+		     	#end
+				<input type="text" name="$key" value="$!previousValues.get($key)" />
+
+		#end
+
+		#foreach($patientAttribute in $patientAttributes)
+				#set($key = "AT${patientAttribute.id}")
+				<label>$patientAttribute.getName()#if( $patientAttribute.isMandatory() )*#end  </label>
+				#if( $validationMap.get( $key ) )
+		           	<br /><span style="color: #990000;"> $i18n.getString($validationMap.get( $key ))</span>
+		     	#end
+				#if($patientAttribute.getValueType() == "COMBO")
+					<select type="text" name="$key">
+					#foreach($option in $patientAttribute.getAttributeOptions())
+						<option value="$option.id"
+							#if( $prevDataValues.get( $key ) == $option.id ) 
+								selected="selected"
+							#end
+							> $option.getName() </option>
+					#end
+					</select>
+				#elseif( $patientAttribute.getValueType() == "YES/NO" )
+					<select name="$key">
+							<option #if( $prevDataValues.get( $key ) == "true" ) selected="selected" #end value="true">$i18n.getString("yes")</option>
+							<option #if( $prevDataValues.get( $key ) == "false" ) selected="selected" #end value="false">$i18n.getString("no")</option>
+					</select>
+				#else
+					<input type="text" name="$key" value="$!previousValues.get($key)" />
+				#end
+		#end
 	</p>
 </div>