← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3761: Don't validate date-value into case-entry form, removed unused methods, re-organize case-entry mo...

 

------------------------------------------------------------
revno: 3761
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-05-27 08:09:01 +0700
message:
  Don't validate date-value into case-entry form, removed unused methods, re-organize case-entry module.
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramDataEntryService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramDataEntryService.java
modified:
  dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadDataEntryAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveDateValueAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveValueAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties
  dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/dataEntryForm.vm
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/dataEntry.js
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/select.vm
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/status.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
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramDataEntryService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramDataEntryService.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramDataEntryService.java	2011-05-27 01:09:01 +0000
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2004-2009, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.hisp.dhis.program;
+
+import java.util.Collection;
+
+import org.hisp.dhis.i18n.I18n;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.patientdatavalue.PatientDataValue;
+
+/**
+ * @author Chau Thu Tran
+ * @version $ ProgramDataEntryService.java May 26, 2011 3:56:03 PM $
+ * 
+ */
+public interface ProgramDataEntryService
+{
+    String prepareDataEntryFormForEntry( String htmlCode, Collection<PatientDataValue> dataValues, String disabled,
+        I18n i18n, ProgramStage programStage, ProgramStageInstance programStageInstance,
+        OrganisationUnit organisationUnit );
+}

=== added file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramDataEntryService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramDataEntryService.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramDataEntryService.java	2011-05-27 01:09:01 +0000
@@ -0,0 +1,1189 @@
+/*
+ * Copyright (c) 2004-2009, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.hisp.dhis.program;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.lang.BooleanUtils;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.i18n.I18n;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.patientdatavalue.PatientDataValue;
+import org.hisp.dhis.patientdatavalue.PatientDataValueService;
+
+/**
+ * @author Chau Thu Tran
+ * @version $ DefaultProgramDataEntryService.java May 26, 2011 3:59:43 PM $
+ * 
+ */
+public class DefaultProgramDataEntryService
+    implements ProgramDataEntryService
+{
+    private static final String EMPTY = "";
+
+    private static final String UNKNOW_CLINIC = "unknow_clinic";
+
+    private static final String NOTAVAILABLE = "not_available";
+
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private PatientDataValueService patientDataValueService;
+
+    public void setPatientDataValueService( PatientDataValueService patientDataValueService )
+    {
+        this.patientDataValueService = patientDataValueService;
+    }
+
+    private DataElementService dataElementService;
+
+    public void setDataElementService( DataElementService dataElementService )
+    {
+        this.dataElementService = dataElementService;
+    }
+
+    private ProgramStageInstanceService programStageInstanceService;
+
+    public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService )
+    {
+        this.programStageInstanceService = programStageInstanceService;
+    }
+
+    private ProgramStageService programStageService;
+
+    public void setProgramStageService( ProgramStageService programStageService )
+    {
+        this.programStageService = programStageService;
+    }
+
+    private ProgramStageDataElementService programStageDataElementService;
+
+    public void setProgramStageDataElementService( ProgramStageDataElementService programStageDataElementService )
+    {
+        this.programStageDataElementService = programStageDataElementService;
+    }
+
+    // -------------------------------------------------------------------------
+    // Implementation methods
+    // -------------------------------------------------------------------------
+    
+    @Override
+    public String prepareDataEntryFormForEntry( String htmlCode, Collection<PatientDataValue> dataValues,
+        String disabled, I18n i18n, ProgramStage programStage, ProgramStageInstance programStageInstance,
+        OrganisationUnit organisationUnit )
+    {
+        Map<Integer, Collection<PatientDataValue>> mapDataValue = new HashMap<Integer, Collection<PatientDataValue>>();
+
+        String result = "";
+
+        result = populateCustomDataEntryForTextBox( htmlCode, dataValues, disabled, i18n,
+            programStage, programStageInstance, organisationUnit, mapDataValue );
+
+        result = populateCustomDataEntryForBoolean( result, dataValues, disabled, i18n,
+            programStage, programStageInstance, organisationUnit, mapDataValue );
+
+        result = populateCustomDataEntryForMutiDimentionalString( result, dataValues,
+            disabled, i18n, programStage, programStageInstance, organisationUnit, mapDataValue );
+
+        result = populateCustomDataEntryForDate( result, dataValues, disabled, i18n,
+            programStage, programStageInstance, organisationUnit, mapDataValue );
+
+        result = populateI18nStrings( result, i18n );
+
+        return result;
+    }
+
+    // -------------------------------------------------------------------------
+    // Supportive methods
+    // -------------------------------------------------------------------------
+
+    private String populateCustomDataEntryForTextBox( String dataEntryFormCode,
+        Collection<PatientDataValue> dataValues, String disabled, I18n i18n, ProgramStage programStage, ProgramStageInstance programStageInstance,
+        OrganisationUnit organisationUnit, Map<Integer, Collection<PatientDataValue>> mapDataValue )
+    {
+        // ---------------------------------------------------------------------
+        // Inline Javascript to add to HTML before outputting
+        // ---------------------------------------------------------------------
+
+        final String jsCodeForInputs = " $DISABLED onchange=\"saveValueCustom( this )\" data=\"{compulsory:$COMPULSORY, optionComboId:$OPTIONCOMBOID, dataElementId:$DATAELEMENTID, dataElementName:'$DATAELEMENTNAME', dataElementType:'$DATAELEMENTTYPE', programStageId:$PROGRAMSTAGEID, programStageName: '$PROGRAMSTAGENAME', orgUnitName:'$ORGUNITNAME'}\"  onkeypress=\"return keyPress(event, this)\"   ";
+
+        // ---------------------------------------------------------------------
+        // Metadata code to add to HTML before outputting
+        // ---------------------------------------------------------------------
+
+        StringBuffer sb = new StringBuffer();
+
+        // ---------------------------------------------------------------------
+        // Pattern to match data elements in the HTML code
+        // ---------------------------------------------------------------------
+
+        Pattern dataElementPattern = Pattern.compile( "(<input.*?)[/]?>", Pattern.DOTALL );
+        Matcher dataElementMatcher = dataElementPattern.matcher( dataEntryFormCode );
+
+        // ---------------------------------------------------------------------
+        // Pattern to extract data element ID from data element field
+        // ---------------------------------------------------------------------
+
+        Pattern identifierPattern = Pattern
+            .compile( "\"value\\[([\\p{Digit}.]*)\\].value:value\\[([\\p{Digit}.]*)\\].value:value\\[([\\p{Digit}.]*)\\].value\"" );
+
+        // ---------------------------------------------------------------------
+        // Iterate through all matching data element fields
+        // ---------------------------------------------------------------------
+
+        Map<Integer, DataElement> dataElementMap = getDataElementMap( programStage );
+
+        while ( dataElementMatcher.find() )
+        {
+            // -----------------------------------------------------------------
+            // Get HTML input field code
+            // -----------------------------------------------------------------
+
+            String compulsory = "null";
+            String dataElementCode = dataElementMatcher.group( 1 );
+
+            Matcher identifierMatcher = identifierPattern.matcher( dataElementCode );
+
+            if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 )
+            {
+                // -------------------------------------------------------------
+                // Get data element ID of data element
+                // -------------------------------------------------------------
+
+                int programStageId = Integer.parseInt( identifierMatcher.group( 1 ) );
+
+                int dataElementId = Integer.parseInt( identifierMatcher.group( 2 ) );
+
+                int optionComboId = Integer.parseInt( identifierMatcher.group( 3 ) );
+
+                DataElement dataElement = null;
+
+                String programStageName = programStage.getName();
+
+                if ( programStageId != programStage.getId() )
+                {
+                    dataElement = dataElementService.getDataElement( dataElementId );
+
+                    ProgramStage otherProgramStage = programStageService.getProgramStage( programStageId );
+                    programStageName = otherProgramStage != null ? otherProgramStage.getName() : "N/A";
+
+                }
+                else
+                {
+                    dataElement = dataElementMap.get( dataElementId );
+                    if ( dataElement == null )
+                    {
+                        return i18n.getString( "program_stage_lack_data_elements" );
+                    }
+
+                    ProgramStageDataElement psde = programStageDataElementService.get( programStage, dataElement );
+
+                    compulsory = BooleanUtils.toStringTrueFalse( psde.isCompulsory() );
+                }
+
+                if ( dataElement == null )
+                {
+                    continue;
+                }
+                if ( !DataElement.VALUE_TYPE_INT.equals( dataElement.getType() )
+                    && !DataElement.VALUE_TYPE_STRING.equals( dataElement.getType() ) )
+                {
+                    continue;
+                }
+                // -------------------------------------------------------------
+                // Find type of data element
+                // -------------------------------------------------------------
+
+                String dataElementType = dataElement.getDetailedNumberType();
+
+                // -------------------------------------------------------------
+                // Find existing value of data element in data set
+                // -------------------------------------------------------------
+
+                PatientDataValue patientDataValue = null;
+
+                String dataElementValue = EMPTY;
+
+                if ( programStageId != programStage.getId() )
+                {
+                    Collection<PatientDataValue> patientDataValues = mapDataValue.get( programStageId );
+
+                    if ( patientDataValues == null )
+                    {
+                        ProgramStage otherProgramStage = programStageService.getProgramStage( programStageId );
+                        ProgramStageInstance otherProgramStageInstance = programStageInstanceService
+                            .getProgramStageInstance( programStageInstance.getProgramInstance(), otherProgramStage );
+                        patientDataValues = patientDataValueService.getPatientDataValues( otherProgramStageInstance );
+                        mapDataValue.put( programStageId, patientDataValues );
+                    }
+
+                    patientDataValue = getValue( patientDataValues, dataElementId, optionComboId );
+
+                    dataElementValue = patientDataValue != null ? patientDataValue.getValue() : dataElementValue;
+                }
+                else
+                {
+                    patientDataValue = getValue( dataValues, dataElementId );
+
+                    dataElementValue = patientDataValue != null ? patientDataValue.getValue() : dataElementValue;
+                }
+
+                // -------------------------------------------------------------
+                // Insert value of data element in output code
+                // -------------------------------------------------------------
+
+                if ( dataElementCode.contains( "value=\"\"" ) )
+                {
+                    dataElementCode = dataElementCode.replace( "value=\"\"", "value=\"" + dataElementValue + "\"" );
+                }
+                else
+                {
+                    dataElementCode += "value=\"" + dataElementValue + "\"";
+                }
+
+                // -------------------------------------------------------------
+                // Remove placeholder view attribute from input field
+                // -------------------------------------------------------------
+
+                dataElementCode = dataElementCode.replaceAll( "view=\".*?\"", "" );
+
+                // -------------------------------------------------------------
+                // Append Javascript code and meta data (type/min/max) for
+                // persisting to output code, and insert value and type for
+                // fields
+                // -------------------------------------------------------------
+
+                String appendCode = dataElementCode;
+
+                appendCode += jsCodeForInputs;
+
+                appendCode += " />";
+
+                // -----------------------------------------------------------
+                // Check if this dataElement is from another programStage then
+                // disable
+                // If programStagsInstance is completed then disabled it
+                // -----------------------------------------------------------
+
+                disabled = "";
+                if ( programStageId == programStage.getId() && !programStageInstance.isCompleted() )
+                {
+                    // -----------------------------------------------------------
+                    // Add ProvidedByOtherFacility checkbox
+                    // -----------------------------------------------------------
+
+                    appendCode = addProvidedByOtherFacilityCheckbox( appendCode, patientDataValue );
+
+                }
+                else
+                {
+                    disabled = "disabled=\"\"";
+                }
+
+                // -----------------------------------------------------------
+                // 
+                // -----------------------------------------------------------
+
+                String orgUnitName = i18n.getString( NOTAVAILABLE );
+                if ( patientDataValue != null )
+                {
+                    if ( patientDataValue.isProvidedByAnotherFacility() )
+                    {
+                        orgUnitName = i18n.getString( UNKNOW_CLINIC );
+                    }
+                    else
+                    {
+                        orgUnitName = patientDataValue.getOrganisationUnit().getName();
+                    }
+                }
+
+                appendCode = appendCode.replace( "$DATAELEMENTID", String.valueOf( dataElementId ) );
+                appendCode = appendCode.replace( "$PROGRAMSTAGEID", String.valueOf( programStageId ) );
+                appendCode = appendCode.replace( "$PROGRAMSTAGENAME", programStageName );
+                appendCode = appendCode.replace( "$ORGUNITNAME", orgUnitName );
+                appendCode = appendCode.replace( "$OPTIONCOMBOID", String.valueOf( optionComboId ) );
+                appendCode = appendCode.replace( "$DATAELEMENTNAME", dataElement.getName() );
+                appendCode = appendCode.replace( "$DATAELEMENTTYPE", dataElementType );
+                appendCode = appendCode.replace( "$DISABLED", disabled );
+                appendCode = appendCode.replace( "$COMPULSORY", compulsory );
+                appendCode = appendCode.replace( "$SAVEMODE", "false" );
+
+                dataElementMatcher.appendReplacement( sb, appendCode );
+            }
+        }
+
+        dataElementMatcher.appendTail( sb );
+
+        return sb.toString();
+    }
+
+    private String populateCustomDataEntryForBoolean( String dataEntryFormCode,
+        Collection<PatientDataValue> dataValues, String disabled, I18n i18n, ProgramStage programStage,
+        ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit,
+        Map<Integer, Collection<PatientDataValue>> mapDataValue )
+    {
+
+        // ---------------------------------------------------------------------
+        // Inline Javascript to add to HTML before outputting
+        // ---------------------------------------------------------------------
+
+        final String jsCodeForBoolean = " name=\"entryselect\" data=\"{compulsory:$COMPULSORY, dataElementId:$DATAELEMENTID, dataElementName:'$DATAELEMENTNAME', dataElementType:'$DATAELEMENTTYPE', programStageId:$PROGRAMSTAGEID, programStageName: '$PROGRAMSTAGENAME', orgUnitName:'$ORGUNITNAME'}\" $DISABLED onchange=\"saveChoiceCustom( $PROGRAMSTAGEID, $DATAELEMENTID,this)\"";
+
+        // ---------------------------------------------------------------------
+        // Metadata code to add to HTML before outputting
+        // ---------------------------------------------------------------------
+
+        final String metaDataCode = "<span id=\"value[$DATAELEMENTID].name\" style=\"display:none\">$DATAELEMENTNAME</span>"
+            + "<span id=\"value[$DATAELEMENTID].type\" style=\"display:none\">$DATAELEMENTTYPE</span>";
+        StringBuffer sb = new StringBuffer();
+
+        // ---------------------------------------------------------------------
+        // Pattern to match data elements in the HTML code
+        // ---------------------------------------------------------------------
+
+        Pattern dataElementPattern = Pattern.compile( "(<select.*?)[/]?</select>", Pattern.DOTALL );
+        Matcher dataElementMatcher = dataElementPattern.matcher( dataEntryFormCode );
+
+        // ---------------------------------------------------------------------
+        // Pattern to extract data element ID from data element field
+        // ---------------------------------------------------------------------
+
+        Pattern identifierPattern = Pattern.compile( "value\\[(.*)\\].boolean:value\\[(.*)\\].boolean" );
+
+        // ---------------------------------------------------------------------
+        // Iterate through all matching data element fields
+        // ---------------------------------------------------------------------
+
+        Map<Integer, DataElement> dataElementMap = getDataElementMap( programStage );
+
+        while ( dataElementMatcher.find() )
+        {
+            // -----------------------------------------------------------------
+            // Get HTML input field code
+            // -----------------------------------------------------------------
+
+            String compulsory = "null";
+            String dataElementCode = dataElementMatcher.group( 1 );
+            Matcher identifierMatcher = identifierPattern.matcher( dataElementCode );
+            if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 )
+            {
+                // -------------------------------------------------------------
+                // Get data element ID of data element
+                // -------------------------------------------------------------
+
+                int programStageId = Integer.parseInt( identifierMatcher.group( 1 ) );
+
+                int dataElementId = Integer.parseInt( identifierMatcher.group( 2 ) );
+
+                DataElement dataElement = null;
+
+                String programStageName = programStage.getName();
+
+                if ( programStageId != programStage.getId() )
+                {
+                    dataElement = dataElementService.getDataElement( dataElementId );
+
+                    ProgramStage otherProgramStage = programStageService.getProgramStage( programStageId );
+                    programStageName = otherProgramStage != null ? otherProgramStage.getName() : "N/A";
+                }
+                else
+                {
+                    dataElement = dataElementMap.get( dataElementId );
+                    if ( dataElement == null )
+                    {
+                        return i18n.getString( "program_stage_lack_data_elements" );
+                    }
+
+                    ProgramStageDataElement psde = programStageDataElementService.get( programStage, dataElement );
+
+                    compulsory = BooleanUtils.toStringTrueFalse( psde.isCompulsory() );
+                }
+
+                if ( dataElement == null )
+                {
+                    continue;
+                }
+
+                if ( !DataElement.VALUE_TYPE_BOOL.equals( dataElement.getType() ) )
+                {
+                    continue;
+                }
+                // -------------------------------------------------------------
+                // Find type of data element
+                // -------------------------------------------------------------
+
+                String dataElementType = dataElement.getType();
+
+                // -------------------------------------------------------------
+                // Find existing value of data element in data set
+                // -------------------------------------------------------------
+
+                PatientDataValue patientDataValue = null;
+
+                String dataElementValue = EMPTY;
+
+                if ( programStageId != programStage.getId() )
+                {
+                    Collection<PatientDataValue> patientDataValues = mapDataValue.get( programStageId );
+
+                    if ( patientDataValues == null )
+                    {
+                        ProgramStage otherProgramStage = programStageService.getProgramStage( programStageId );
+                        ProgramStageInstance otherProgramStageInstance = programStageInstanceService
+                            .getProgramStageInstance( programStageInstance.getProgramInstance(), otherProgramStage );
+                        patientDataValues = patientDataValueService.getPatientDataValues( otherProgramStageInstance );
+                        mapDataValue.put( programStageId, patientDataValues );
+                    }
+
+                    patientDataValue = getValue( patientDataValues, dataElementId );
+
+                    dataElementValue = patientDataValue != null ? patientDataValue.getValue() : dataElementValue;
+                }
+                else
+                {
+
+                    patientDataValue = getValue( dataValues, dataElementId );
+                    
+                    if ( patientDataValue != null )
+                    {
+                        dataElementValue = patientDataValue.getValue();
+                    }
+                }
+
+                String appendCode = dataElementCode;
+                appendCode = appendCode.replace( "name=\"entryselect\"", jsCodeForBoolean );
+
+                // -------------------------------------------------------------
+                // Insert value of data element in output code
+                // -------------------------------------------------------------
+
+                if ( patientDataValue != null )
+                {
+
+                    if ( dataElementValue.equalsIgnoreCase( "true" ) )
+                    {
+                        appendCode = appendCode.replace( "<option value=\"true\">", "<option value=\""
+                            + i18n.getString( "true" ) + "\" selected>" );
+                    }
+
+                    if ( dataElementValue.equalsIgnoreCase( "false" ) )
+                    {
+                        appendCode = appendCode.replace( "<option value=\"false\">", "<option value=\""
+                            + i18n.getString( "false" ) + "\" selected>" );
+                    }
+
+                }
+
+                appendCode += "</select>";
+
+                // -------------------------------------------------------------
+                // Remove placeholder view attribute from input field
+                // -------------------------------------------------------------
+
+                dataElementCode = dataElementCode.replaceAll( "view=\".*?\"", "" );
+
+                // -------------------------------------------------------------
+                // Insert title information - Data element id, name, type, min,
+                // max
+                // -------------------------------------------------------------
+
+                if ( dataElementCode.contains( "title=\"\"" ) )
+                {
+                    dataElementCode = dataElementCode.replace( "title=\"\"", "title=\"-- ID:" + dataElement.getId()
+                        + " Name:" + dataElement.getShortName() + " Type:" + dataElement.getType() + "\"" );
+                }
+                else
+                {
+                    dataElementCode += "title=\"-- ID:" + dataElement.getId() + " Name:" + dataElement.getShortName()
+                        + " Type:" + dataElement.getType() + "\"";
+                }
+
+                // -------------------------------------------------------------
+                // Append Javascript code and meta data (type/min/max) for
+                // persisting to output code, and insert value and type for
+                // fields
+                // -------------------------------------------------------------
+
+                appendCode += metaDataCode;
+
+                // -----------------------------------------------------------
+                // Check if this dataElement is from another programStage then
+                // disable
+                // If programStagsInstance is completed then disabled it
+                // -----------------------------------------------------------
+
+                disabled = "";
+                if ( programStageId != programStage.getId() || programStageInstance.isCompleted() )
+                {
+                    disabled = "disabled";
+                }
+                else
+                {
+                    // -----------------------------------------------------------
+                    // Add ProvidedByOtherFacility checkbox
+                    // -----------------------------------------------------------
+                    appendCode = addProvidedByOtherFacilityCheckbox( appendCode, patientDataValue );
+                }
+
+                // -----------------------------------------------------------
+                // 
+                // -----------------------------------------------------------
+                
+                String orgUnitName = i18n.getString( NOTAVAILABLE );
+                if ( patientDataValue != null )
+                {
+                    if ( patientDataValue.isProvidedByAnotherFacility() )
+                    {
+                        orgUnitName = i18n.getString( UNKNOW_CLINIC );
+                    }
+                    else
+                    {
+                        orgUnitName = patientDataValue.getOrganisationUnit().getName();
+                    }
+                }
+
+                appendCode = appendCode.replace( "$DATAELEMENTID", String.valueOf( dataElementId ) );
+                appendCode = appendCode.replace( "$PROGRAMSTAGEID", String.valueOf( programStageId ) );
+                appendCode = appendCode.replace( "$PROGRAMSTAGENAME", programStageName );
+                appendCode = appendCode.replace( "$ORGUNITNAME", orgUnitName );
+                appendCode = appendCode.replace( "$DATAELEMENTNAME", dataElement.getName() );
+                appendCode = appendCode.replace( "$DATAELEMENTTYPE", dataElementType );
+                appendCode = appendCode.replace( "$DISABLED", disabled );
+                appendCode = appendCode.replace( "$COMPULSORY", compulsory );
+                appendCode = appendCode.replace( "i18n_yes", i18n.getString( "yes" ) );
+                appendCode = appendCode.replace( "i18n_no", i18n.getString( "no" ) );
+                appendCode = appendCode.replace( "i18n_select_value", i18n.getString( "select_value" ) );
+                appendCode = appendCode.replace( "$SAVEMODE", "false" );
+
+                appendCode = appendCode.replaceAll( "\\$", "\\\\\\$" );
+
+                dataElementMatcher.appendReplacement( sb, appendCode );
+            }
+        }
+
+        dataElementMatcher.appendTail( sb );
+
+        return sb.toString();
+    }
+
+    private String populateCustomDataEntryForMutiDimentionalString( String dataEntryFormCode,
+        Collection<PatientDataValue> dataValues, String disabled, I18n i18n, ProgramStage programStage,
+        ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit,
+        Map<Integer, Collection<PatientDataValue>> mapDataValue )
+    {
+
+        // ---------------------------------------------------------------------
+        // Inline Javascript to add to HTML before outputting
+        // ---------------------------------------------------------------------
+
+        final String jsCodeForCombo = " name=\"entryselect\" $DISABLED data=\"{compulsory:$COMPULSORY, dataElementId:$DATAELEMENTID, dataElementName:'$DATAELEMENTNAME', dataElementType:'$DATAELEMENTTYPE', programStageId:$PROGRAMSTAGEID, programStageName: '$PROGRAMSTAGENAME', orgUnitName:'$ORGUNITNAME'}\" onchange=\"saveChoiceCustom( $PROGRAMSTAGEID, $DATAELEMENTID,this)\"";
+        
+        // ---------------------------------------------------------------------
+        // Metadata code to add to HTML before outputting
+        // ---------------------------------------------------------------------
+
+        final String metaDataCode = "<span id=\"value[$DATAELEMENTID].name\" style=\"display:none\">$DATAELEMENTNAME</span>"
+            + "<span id=\"value[$DATAELEMENTID].type\" style=\"display:none\">$DATAELEMENTTYPE</span>";
+        StringBuffer sb = new StringBuffer();
+
+        // ---------------------------------------------------------------------
+        // Pattern to match data elements in the HTML code
+        // ---------------------------------------------------------------------
+
+        Pattern dataElementPattern = Pattern.compile( "(<select.*?)[/]?</select>", Pattern.DOTALL );
+        Matcher dataElementMatcher = dataElementPattern.matcher( dataEntryFormCode );
+
+        // ---------------------------------------------------------------------
+        // Pattern to extract data element ID from data element field
+        // ---------------------------------------------------------------------
+
+        Pattern identifierPattern = Pattern
+            .compile( "\"value\\[([\\p{Digit}.]*)\\].combo:value\\[([\\p{Digit}.]*)\\].combo\"" );
+
+        // ---------------------------------------------------------------------
+        // Iterate through all matching data element fields
+        // ---------------------------------------------------------------------
+
+        Map<Integer, DataElement> dataElementMap = getDataElementMap( programStage );
+
+        while ( dataElementMatcher.find() )
+        {
+            // -----------------------------------------------------------------
+            // Get HTML input field code
+            // -----------------------------------------------------------------
+
+            String dataElementCode = dataElementMatcher.group( 1 );
+            
+            Matcher identifierMatcher = identifierPattern.matcher( dataElementCode );
+
+            String compulsory = "null";
+
+            if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 )
+            {
+
+                // -------------------------------------------------------------
+                // Get data element ID of data element
+                // -------------------------------------------------------------
+                int programStageId = Integer.parseInt( identifierMatcher.group( 1 ) );
+                int dataElementId = Integer.parseInt( identifierMatcher.group( 2 ) );
+
+                DataElement dataElement = null;
+
+                String programStageName = programStage.getName();
+
+                if ( programStageId != programStage.getId() )
+                {
+                    dataElement = dataElementService.getDataElement( dataElementId );
+
+                    ProgramStage otherProgramStage = programStageService.getProgramStage( programStageId );
+                    programStageName = otherProgramStage != null ? otherProgramStage.getName() : "N/A";
+                }
+                else
+                {
+                    dataElement = dataElementMap.get( dataElementId );
+                    if ( dataElement == null )
+                    {
+                        return i18n.getString( "program_stage_lack_data_elements" );
+                    }
+
+                    ProgramStageDataElement psde = programStageDataElementService.get( programStage, dataElement );
+
+                    compulsory = BooleanUtils.toStringTrueFalse( psde.isCompulsory() );
+                }
+
+                if ( dataElement == null )
+                {
+                    continue;
+                }
+                if ( !DataElement.VALUE_TYPE_STRING.equals( dataElement.getType() ) )
+                {
+                    continue;
+                }
+                
+                // -------------------------------------------------------------
+                // Find type of data element
+                // -------------------------------------------------------------
+
+                String dataElementType = dataElement.getType();
+
+                // -------------------------------------------------------------
+                // Find existing value of data element in data set
+                // -------------------------------------------------------------
+                
+                PatientDataValue patientDataValue = null;
+                String dataElementValue = EMPTY;
+                if ( programStageId != programStage.getId() )
+                {
+                    Collection<PatientDataValue> patientDataValues = mapDataValue.get( programStageId );
+
+                    if ( patientDataValues == null )
+                    {
+                        ProgramStage otherProgramStage = programStageService.getProgramStage( programStageId );
+                        ProgramStageInstance otherProgramStageInstance = programStageInstanceService
+                            .getProgramStageInstance( programStageInstance.getProgramInstance(), otherProgramStage );
+                        patientDataValues = patientDataValueService.getPatientDataValues( otherProgramStageInstance );
+                        mapDataValue.put( programStageId, patientDataValues );
+                    }
+
+                    patientDataValue = getValue( patientDataValues, dataElementId );
+                    
+                    dataElementValue = patientDataValue != null ? patientDataValue.getValue() : dataElementValue;
+                }
+                else
+                {
+                    patientDataValue = getValue( dataValues, dataElementId );
+
+                    dataElementValue = patientDataValue != null ? patientDataValue.getValue() : dataElementValue;
+                }
+
+                String appendCode = dataElementCode;
+                appendCode = appendCode.replace( "name=\"entryselect\"", jsCodeForCombo );
+                
+                // -------------------------------------------------------------
+                // Insert value of data element in output code
+                // -------------------------------------------------------------
+                
+                if ( patientDataValue != null )
+                {
+                   appendCode = appendCode.replace( "id=\"combo[" + patientDataValue.getOptionCombo().getId()
+                        + "].combo\"", "id=\"combo[" + patientDataValue.getOptionCombo().getId()
+                        + "].combo\" selected=\"selected\"" );
+                }
+
+                appendCode += "</select>";
+                
+                // -------------------------------------------------------------
+                // Remove placeholder view attribute from input field
+                // -------------------------------------------------------------
+
+                dataElementCode = dataElementCode.replaceAll( "view=\".*?\"", "" );
+
+                // -------------------------------------------------------------
+                // Insert title information - Data element id, name, type, min,
+                // max
+                // -------------------------------------------------------------
+
+                if ( dataElementCode.contains( "title=\"\"" ) )
+                {
+                    dataElementCode = dataElementCode.replace( "title=\"\"", "title=\"-- ID:" + dataElement.getId()
+                        + " Name:" + dataElement.getShortName() + " Type:" + dataElement.getType() + "\"" );
+                }
+                else
+                {
+                    dataElementCode += "title=\"-- ID:" + dataElement.getId() + " Name:" + dataElement.getShortName()
+                        + " Type:" + dataElement.getType() + "\"";
+                }
+
+                // -------------------------------------------------------------
+                // Append Javascript code and meta data (type/min/max) for
+                // persisting to output code, and insert value and type for
+                // fields
+                // -------------------------------------------------------------
+
+                appendCode += metaDataCode;
+
+                // -----------------------------------------------------------
+                // Check if this dataElement is from another programStage then
+                // disable
+                // If programStagsInstance is completed then disabled it
+                // -----------------------------------------------------------
+               
+                disabled = "";
+                if ( programStageId != programStage.getId() || programStageInstance.isCompleted() )
+                {
+                    disabled = "disabled";
+                }
+                else
+                {
+                    // -----------------------------------------------------------
+                    // Add ProvidedByOtherFacility checkbox
+                    // -----------------------------------------------------------
+                   
+                    appendCode = addProvidedByOtherFacilityCheckbox( appendCode, patientDataValue );
+                }
+
+                // -----------------------------------------------------------
+                // 
+                // -----------------------------------------------------------
+                
+                String orgUnitName = i18n.getString( NOTAVAILABLE );
+                if ( patientDataValue != null )
+                {
+                    if ( patientDataValue.isProvidedByAnotherFacility() )
+                    {
+                        orgUnitName = i18n.getString( UNKNOW_CLINIC );
+                    }
+                    else
+                    {
+                        orgUnitName = patientDataValue.getOrganisationUnit().getName();
+                    }
+                }
+
+                appendCode = appendCode.replace( "$DATAELEMENTID", String.valueOf( dataElementId ) );
+                appendCode = appendCode.replace( "$PROGRAMSTAGEID", String.valueOf( programStageId ) );
+                appendCode = appendCode.replace( "$PROGRAMSTAGENAME", programStageName );
+                appendCode = appendCode.replace( "$ORGUNITNAME", orgUnitName );
+                appendCode = appendCode.replace( "$DATAELEMENTNAME", dataElement.getName() );
+                appendCode = appendCode.replace( "$DATAELEMENTTYPE", dataElementType );
+                appendCode = appendCode.replace( "$DISABLED", disabled );
+                appendCode = appendCode.replace( "$COMPULSORY", compulsory );
+                appendCode = appendCode.replace( "i18n_select_value", i18n.getString( "select_value" ) );
+                appendCode = appendCode.replace( "$SAVEMODE", "false" );
+                appendCode = appendCode.replaceAll( "\\$", "\\\\\\$" );
+
+                dataElementMatcher.appendReplacement( sb, appendCode );
+            }
+        }
+
+        dataElementMatcher.appendTail( sb );
+
+        return sb.toString();
+    }
+
+    private String populateCustomDataEntryForDate( String dataEntryFormCode, Collection<PatientDataValue> dataValues,
+        String disabled, I18n i18n, ProgramStage programStage, ProgramStageInstance programStageInstance,
+        OrganisationUnit organisationUnit, Map<Integer, Collection<PatientDataValue>> mapDataValue )
+    {
+
+        // ---------------------------------------------------------------------
+        // Inline Javascript to add to HTML before outputting
+        // ---------------------------------------------------------------------
+
+        final String jsCodeForDate = " name=\"entryfield\" $DISABLED onchange=\"saveDateCustom( this )\" data=\"{compulsory:$COMPULSORY, dataElementId:$DATAELEMENTID, dataElementName:'$DATAELEMENTNAME', dataElementType:'$DATAELEMENTTYPE', programStageId:$PROGRAMSTAGEID, programStageName: '$PROGRAMSTAGENAME', orgUnitName:'$ORGUNITNAME'}\"";
+       
+        // ---------------------------------------------------------------------
+        // Metadata code to add to HTML before outputting
+        // ---------------------------------------------------------------------
+
+        final String jQueryCalendar = "<script> "
+            + "datePicker(\"value\\\\\\\\[$PROGRAMSTAGEID\\\\\\\\]\\\\\\\\.date\\\\\\\\:value\\\\\\\\[$DATAELEMENTID\\\\\\\\]\\\\\\\\.date\", false)"
+            + ";</script>";
+
+        final String metaDataCode = "<span id=\"value[$DATAELEMENTID].name\" style=\"display:none\">$DATAELEMENTNAME</span>"
+            + "<span id=\"value[$DATAELEMENTID].type\" style=\"display:none\">$DATAELEMENTTYPE</span>";
+        StringBuffer sb = new StringBuffer();
+
+        // ---------------------------------------------------------------------
+        // Pattern to match data elements in the HTML code
+        // ---------------------------------------------------------------------
+
+        Pattern dataElementPattern = Pattern.compile( "(<input.*?)[/]?/>" );
+        Matcher dataElementMatcher = dataElementPattern.matcher( dataEntryFormCode );
+
+        // ---------------------------------------------------------------------
+        // Pattern to extract data element ID from data element field
+        // ---------------------------------------------------------------------
+
+        Pattern identifierPattern = Pattern
+            .compile( "id=\"value\\[([\\p{Digit}.]*)\\].date:value\\[([\\p{Digit}.]*)\\].date\"" );
+
+        // ---------------------------------------------------------------------
+        // Iterate through all matching data element fields
+        // ---------------------------------------------------------------------
+
+        Map<Integer, DataElement> dataElementMap = getDataElementMap( programStageInstance.getProgramStage() );
+
+        while ( dataElementMatcher.find() )
+        {
+            // -----------------------------------------------------------------
+            // Get HTML input field code
+            // -----------------------------------------------------------------
+            
+            String compulsory = "null";
+            String dataElementCode = dataElementMatcher.group( 1 );
+           
+            Matcher identifierMatcher = identifierPattern.matcher( dataElementCode );
+
+            if ( identifierMatcher.find() && identifierMatcher.groupCount() > 0 )
+            {
+                // -------------------------------------------------------------
+                // Get data element ID of data element
+                // -------------------------------------------------------------
+               
+                int programStageId = Integer.parseInt( identifierMatcher.group( 1 ) );
+                int dataElementId = Integer.parseInt( identifierMatcher.group( 2 ) );
+
+                DataElement dataElement = null;
+
+                String programStageName = programStage.getName();
+
+                if ( programStageId != programStage.getId() )
+                {
+                    dataElement = dataElementService.getDataElement( dataElementId );
+
+                    ProgramStage otherProgramStage = programStageService.getProgramStage( programStageId );
+                    programStageName = otherProgramStage != null ? otherProgramStage.getName() : "N/A";
+                }
+                else
+                {
+                    dataElement = dataElementMap.get( dataElementId );
+                    if ( dataElement == null )
+                    {
+                        return i18n.getString( "program_stage_lack_data_elements" );
+                    }
+
+                    ProgramStageDataElement psde = programStageDataElementService.get( programStage, dataElement );
+
+                    compulsory = BooleanUtils.toStringTrueFalse( psde.isCompulsory() );
+                }
+
+                if ( dataElement == null )
+                {
+                    continue;
+                }
+                if ( !DataElement.VALUE_TYPE_DATE.equals( dataElement.getType() ) )
+                {
+                    continue;
+                }
+                // -------------------------------------------------------------
+                // Find type of data element
+                // -------------------------------------------------------------
+
+                String dataElementType = dataElement.getType();
+
+                // -------------------------------------------------------------
+                // Find existing value of data element in data set
+                // -------------------------------------------------------------
+
+                PatientDataValue patientDataValue = null;
+                String dataElementValue = EMPTY;
+
+                if ( programStageId != programStage.getId() )
+                {
+                    Collection<PatientDataValue> patientDataValues = mapDataValue.get( programStageId );
+
+                    if ( patientDataValues == null )
+                    {
+                        ProgramStage otherProgramStage = programStageService.getProgramStage( programStageId );
+                        ProgramStageInstance otherProgramStageInstance = programStageInstanceService
+                            .getProgramStageInstance( programStageInstance.getProgramInstance(), otherProgramStage );
+                        patientDataValues = patientDataValueService.getPatientDataValues( otherProgramStageInstance );
+                        mapDataValue.put( programStageId, patientDataValues );
+                    }
+
+                    patientDataValue = getValue( patientDataValues, dataElementId );
+                    
+                    dataElementValue = patientDataValue != null ? patientDataValue.getValue() : dataElementValue;
+                }
+                else
+                {
+                    patientDataValue = getValue( dataValues, dataElementId );
+
+                    dataElementValue = patientDataValue != null ? patientDataValue.getValue() : dataElementValue;
+                }
+
+                // -------------------------------------------------------------
+                // Insert value of data element in output code
+                // -------------------------------------------------------------
+
+                if ( dataElementCode.contains( "value=\"\"" ) )
+                {
+                    dataElementCode = dataElementCode.replace( "value=\"\"", "value=\"" + dataElementValue + "\"" );
+                }
+                else
+                {
+                    dataElementCode += "value=\"" + dataElementValue + "\"";
+                }
+
+                // -------------------------------------------------------------
+                // Remove placeholder view attribute from input field
+                // -------------------------------------------------------------
+
+                dataElementCode = dataElementCode.replaceAll( "view=\".*?\"", "" );
+                
+                // -------------------------------------------------------------
+                // Append Javascript code and meta data (type/min/max) for
+                // persisting to output code, and insert value and type for
+                // fields
+                // -------------------------------------------------------------
+
+                String appendCode = dataElementCode + "/>";
+                appendCode = appendCode.replace( "name=\"entryfield\"", jsCodeForDate );
+                
+                appendCode += metaDataCode;
+
+                // -------------------------------------------------------------
+                // Check if this dataElement is from another programStage then
+                // disable
+                // If programStagsInstance is completed then disabled it
+                // -------------------------------------------------------------
+                
+                disabled = "";
+                if ( programStageId != programStage.getId() || programStageInstance.isCompleted() )
+                {
+                    disabled = "disabled=\"\"";
+                }
+                else
+                {
+                    appendCode += jQueryCalendar;
+
+                    // ---------------------------------------------------------
+                    // Add ProvidedByOtherFacility checkbox
+                    // ---------------------------------------------------------
+                    
+                    appendCode = addProvidedByOtherFacilityCheckbox( appendCode, patientDataValue );
+
+                }
+
+                // -------------------------------------------------------------
+                // Get Org Unit name
+                // -------------------------------------------------------------
+                
+                String orgUnitName = i18n.getString( NOTAVAILABLE );
+                if ( patientDataValue != null )
+                {
+                    if ( patientDataValue.isProvidedByAnotherFacility() )
+                    {
+                        orgUnitName = i18n.getString( UNKNOW_CLINIC );
+                    }
+                    else
+                    {
+                        orgUnitName = patientDataValue.getOrganisationUnit().getName();
+                    }
+                }
+
+                appendCode = appendCode.replace( "$DATAELEMENTID", String.valueOf( dataElementId ) );
+
+                appendCode = appendCode.replace( "$PROGRAMSTAGEID", String.valueOf( programStageId ) );
+
+                appendCode = appendCode.replace( "$PROGRAMSTAGENAME", programStageName );
+
+                appendCode = appendCode.replace( "$ORGUNITNAME", orgUnitName );
+
+                appendCode = appendCode.replace( "$DATAELEMENTNAME", dataElement.getName() );
+
+                appendCode = appendCode.replace( "$DATAELEMENTTYPE", dataElementType );
+
+                appendCode = appendCode.replace( "$DISABLED", disabled );
+
+                appendCode = appendCode.replace( "$COMPULSORY", compulsory );
+
+                appendCode = appendCode.replace( "$SAVEMODE", "false" );
+
+                appendCode = appendCode.replaceAll( "\\$", "\\\\\\$" );
+
+                dataElementMatcher.appendReplacement( sb, appendCode );
+            }
+        }
+
+        dataElementMatcher.appendTail( sb );
+
+        return sb.toString();
+    }
+    
+     /**
+     * Returns the value of the PatientDataValue in the Collection of DataValues
+     * with the given data element identifier and category option combo id.
+     */
+    private PatientDataValue getValue( Collection<PatientDataValue> dataValues, int dataElementId,
+        int categoryOptionComboId )
+    {
+        for ( PatientDataValue dataValue : dataValues )
+        {
+            if ( dataValue.getOptionCombo() != null )
+            {
+                if ( dataValue.getDataElement().getId() == dataElementId
+                    && dataValue.getOptionCombo().getId() == categoryOptionComboId )
+                {
+                    return dataValue;
+                }
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Returns the value of the PatientDataValue in the Collection of DataValues
+     * with the given data element identifier.
+     */
+    private PatientDataValue getValue( Collection<PatientDataValue> dataValues, int dataElementId )
+    {
+        for ( PatientDataValue dataValue : dataValues )
+        {
+            if ( dataValue.getDataElement().getId() == dataElementId )
+            {
+                return dataValue;
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Returns a Map of all DataElements in the given ProgramStage where the key
+     * is the DataElement identifier and the value is the DataElement.
+     */
+    private Map<Integer, DataElement> getDataElementMap( ProgramStage programStage )
+    {
+        Collection<DataElement> dataElements = programStageDataElementService.getListDataElement( programStage );
+
+        if ( programStage == null )
+        {
+            return null;
+        }
+        Map<Integer, DataElement> map = new HashMap<Integer, DataElement>();
+
+        for ( DataElement element : dataElements )
+        {
+            map.put( element.getId(), element );
+        }
+
+        return map;
+    }
+
+    /**
+     * Append a ProvidedByOtherFacility Checkbox to the html code
+     * 
+     * @param appendCode: current html code
+     * @param patientDataValue: currrent PatientDataValue
+     * @return full html code after append the check box
+     */
+    private String addProvidedByOtherFacilityCheckbox( String appendCode, PatientDataValue patientDataValue )
+    {
+        appendCode += "<label for=\"$PROGRAMSTAGEID_$DATAELEMENTID_facility\" title=\"is provided by another Facility ?\" ></label><input name=\"providedByAnotherFacility\"  title=\"is provided by another Facility ?\"  id=\"$PROGRAMSTAGEID_$DATAELEMENTID_facility\"  type=\"checkbox\" ";
+
+        if ( patientDataValue != null && patientDataValue.isProvidedByAnotherFacility() )
+        {
+            appendCode += " checked=\"checked\" ";
+        }
+        appendCode += "onChange=\"updateProvidingFacilityCustom( $PROGRAMSTAGEID, $DATAELEMENTID, this )\"  >";
+
+        return appendCode;
+
+    }
+
+    /**
+     * Replaces i18n string in the custom form code.
+     * 
+     * @param dataEntryFormCode the data entry form html.
+     * @param i18n the I18n object.
+     * @return internationalized data entry form html.
+     */
+    private String populateI18nStrings( String dataEntryFormCode, I18n i18n )
+    {
+        StringBuffer sb = new StringBuffer();
+
+        // ---------------------------------------------------------------------
+        // Pattern to match i18n strings in the HTML code
+        // ---------------------------------------------------------------------
+
+        Pattern i18nPattern = Pattern.compile( "(<i18n.*?)[/]?</i18n>", Pattern.DOTALL );
+        Matcher i18nMatcher = i18nPattern.matcher( dataEntryFormCode );
+
+        // ---------------------------------------------------------------------
+        // Iterate through all matching i18n element fields
+        // ---------------------------------------------------------------------
+
+        while ( i18nMatcher.find() )
+        {
+            String i18nCode = i18nMatcher.group( 1 );
+
+            i18nCode = i18nCode.replaceAll( "<i18n>", "" );
+
+            i18nCode = i18n.getString( i18nCode );
+
+            i18nMatcher.appendReplacement( sb, i18nCode );
+        }
+
+        i18nMatcher.appendTail( sb );
+
+        String result = sb.toString();
+
+        result.replaceAll( "</i18n>", "" );
+
+        return result;
+    }
+
+}
\ No newline at end of file

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2011-05-26 03:43:41 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2011-05-27 01:09:01 +0000
@@ -140,6 +140,22 @@
    
 	<!-- Service definitions -->
 	
+	<bean id="org.hisp.dhis.program.ProgramDataEntryService"
+		class="org.hisp.dhis.program.DefaultProgramDataEntryService">
+		<property name="dataElementService" ref="org.hisp.dhis.dataelement.DataElementService" />
+		<property name="programStageInstanceService">
+			<ref bean="org.hisp.dhis.program.ProgramStageInstanceService" />
+		</property>
+		<property name="programStageService">
+			<ref bean="org.hisp.dhis.program.ProgramStageService" />
+		</property>
+		<property name="patientDataValueService">
+			<ref bean="org.hisp.dhis.patientdatavalue.PatientDataValueService" />
+		</property>
+		<property name="programStageDataElementService"
+			ref="org.hisp.dhis.program.ProgramStageDataElementService" />
+	</bean>
+	
 	<bean id="org.hisp.dhis.activityplan.ActivityPlanService"
 		class="org.hisp.dhis.activityplan.DefaultActivityPlanService">
 		<property name="patientDataValueService"
@@ -155,7 +171,7 @@
 	</bean>
 	
 	<bean id="org.hisp.dhis.caseaggregation.CaseAggregationConditionService"
-    class="org.hisp.dhis.caseaggregation.DefaultCaseAggregationConditionService">
+    	class="org.hisp.dhis.caseaggregation.DefaultCaseAggregationConditionService">
     	<property name="aggregationConditionStore" ref="org.hisp.dhis.caseaggregation.CaseAggregationConditionStore" />
     	<property name="dataElementService" ref="org.hisp.dhis.dataelement.DataElementService" />     	
         <property name="patientService" ref="org.hisp.dhis.patient.PatientService" />     	

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadDataEntryAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadDataEntryAction.java	2011-05-25 08:44:29 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadDataEntryAction.java	2011-05-27 01:09:01 +0000
@@ -32,7 +32,6 @@
 import java.util.HashMap;
 import java.util.Map;
 
-import org.hisp.dhis.caseentry.screen.DataEntryScreenManager;
 import org.hisp.dhis.dataentryform.DataEntryForm;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -41,6 +40,7 @@
 import org.hisp.dhis.patient.PatientService;
 import org.hisp.dhis.patientdatavalue.PatientDataValue;
 import org.hisp.dhis.patientdatavalue.PatientDataValueService;
+import org.hisp.dhis.program.ProgramDataEntryService;
 import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramStage;
@@ -67,7 +67,7 @@
 
     private ProgramInstanceService programInstanceService;
 
-    private DataEntryScreenManager dataEntryScreenManager;
+    private ProgramDataEntryService programDataEntryService;
 
     private PatientDataValueService patientDataValueService;
 
@@ -85,8 +85,6 @@
 
     private Integer patientId;
 
-    private Boolean useDefaultForm;
-
     private ProgramStageInstance programStageInstance;
 
     private String customDataEntryFormCode;
@@ -97,6 +95,8 @@
 
     private Map<Integer, PatientDataValue> patientDataValueMap;
 
+    private OrganisationUnit organisationUnit;
+
     // -------------------------------------------------------------------------
     // Getters && Setters
     // -------------------------------------------------------------------------
@@ -121,9 +121,9 @@
         this.programInstanceService = programInstanceService;
     }
 
-    public void setDataEntryScreenManager( DataEntryScreenManager dataEntryScreenManager )
+    public void setProgramDataEntryService( ProgramDataEntryService programDataEntryService )
     {
-        this.dataEntryScreenManager = dataEntryScreenManager;
+        this.programDataEntryService = programDataEntryService;
     }
 
     public void setPatientDataValueService( PatientDataValueService patientDataValueService )
@@ -135,6 +135,11 @@
     {
         this.selectionManager = selectionManager;
     }
+    
+    public OrganisationUnit getOrganisationUnit()
+    {
+        return organisationUnit;
+    }
 
     public ProgramStageInstance getProgramStageInstance()
     {
@@ -156,11 +161,6 @@
         this.programStageId = programStageId;
     }
 
-    public void setUseDefaultForm( Boolean useDefaultForm )
-    {
-        this.useDefaultForm = useDefaultForm;
-    }
-
     public String getCustomDataEntryFormCode()
     {
         return customDataEntryFormCode;
@@ -194,7 +194,7 @@
         ProgramInstance programInstance = programInstanceService.getProgramInstances( patient,
             programStage.getProgram(), false ).iterator().next();
 
-        OrganisationUnit organisationUnit = selectionManager.getSelectedOrganisationUnit();
+        organisationUnit = selectionManager.getSelectedOrganisationUnit();
 
         programStageDataElements = programStage.getProgramStageDataElements();
 
@@ -222,11 +222,10 @@
 
             DataEntryForm dataEntryForm = programStage.getDataEntryForm();
 
-            if ( !useDefaultForm && dataEntryForm != null)
+            if ( dataEntryForm != null )
             {
-              customDataEntryFormCode = dataEntryScreenManager.populateCustomDataEntryScreenForMultiDimensional(
-                    dataEntryForm.getHtmlCode(), patientDataValues, "", i18n, programStage, programStageInstance,
-                    organisationUnit );
+                customDataEntryFormCode = programDataEntryService.prepareDataEntryFormForEntry( dataEntryForm
+                    .getHtmlCode(), patientDataValues, "", i18n, programStage, programStageInstance, organisationUnit );
             }
 
             return SUCCESS;

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveDateValueAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveDateValueAction.java	2011-05-25 05:27:00 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveDateValueAction.java	2011-05-27 01:09:01 +0000
@@ -31,14 +31,15 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.hisp.dhis.caseentry.state.SelectedStateManager;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.i18n.I18n;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientService;
 import org.hisp.dhis.patientdatavalue.PatientDataValue;
 import org.hisp.dhis.patientdatavalue.PatientDataValueService;
 import org.hisp.dhis.program.Program;
@@ -47,6 +48,7 @@
 import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStageInstanceService;
+import org.hisp.dhis.program.ProgramStageService;
 
 import com.opensymphony.xwork2.Action;
 
@@ -63,11 +65,25 @@
     // Dependencies
     // -------------------------------------------------------------------------
 
-    private SelectedStateManager selectedStateManager;
-
-    public void setSelectedStateManager( SelectedStateManager selectedStateManager )
-    {
-        this.selectedStateManager = selectedStateManager;
+    private OrganisationUnitService organisationUnitService;
+
+    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
+    {
+        this.organisationUnitService = organisationUnitService;
+    }
+
+    private PatientService patientService;
+
+    public void setPatientService( PatientService patientService )
+    {
+        this.patientService = patientService;
+    }
+
+    private ProgramStageService programStageService;
+
+    public void setProgramStageService( ProgramStageService programStageService )
+    {
+        this.programStageService = programStageService;
     }
 
     private ProgramInstanceService programInstanceService;
@@ -135,6 +151,27 @@
         return dataElementId;
     }
 
+    private Integer orgunitId;
+
+    public void setOrgunitId( Integer orgunitId )
+    {
+        this.orgunitId = orgunitId;
+    }
+
+    private Integer patientId;
+
+    public void setPatientId( Integer patientId )
+    {
+        this.patientId = patientId;
+    }
+
+    private Integer programStageId;
+
+    public void setProgramStageId( Integer programStageId )
+    {
+        this.programStageId = programStageId;
+    }
+
     private int statusCode;
 
     public int getStatusCode()
@@ -168,16 +205,20 @@
     public String execute()
         throws Exception
     {
-        OrganisationUnit organisationUnit = selectedStateManager.getSelectedOrganisationUnit();
-
-        Patient patient = selectedStateManager.getSelectedPatient();
-
-        Program program = selectedStateManager.getSelectedProgram();
-
-        ProgramStage programStage = selectedStateManager.getSelectedProgramStage();
-
-        Collection<ProgramInstance> progamInstances = programInstanceService.getProgramInstances( patient, program,
-            false );
+        // ---------------------------------------------------------------------
+        // Get program-stage-instance
+        // ---------------------------------------------------------------------
+
+        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( orgunitId );
+
+        Patient patient = patientService.getPatient( patientId );
+
+        ProgramStage programStage = programStageService.getProgramStage( programStageId );
+
+        Program program = programStage.getProgram();
+
+        Collection<ProgramInstance> progamInstances = programInstanceService.getProgramInstances( patient, programStage
+            .getProgram(), false );
 
         ProgramInstance programInstance = progamInstances.iterator().next();
 
@@ -196,22 +237,15 @@
             value = null;
         }
 
-        if ( dataElement.getType().equalsIgnoreCase( DataElement.VALUE_TYPE_DATE ) && value != null )
+        // ---------------------------------------------------------------------
+        // Check inputed value:
+        // value <= DueDate + program.maxDaysAllowedInputData
+        // ---------------------------------------------------------------------
+
+        if ( value != null )
         {
             Date dateValue = format.parseDate( value );
 
-            if ( dateValue == null )
-            {
-                statusCode = 1;
-
-                return SUCCESS;
-            }
-
-            // -----------------------------------------------------------------
-            // Check inputed value:
-            // value <= DueDate + program.maxDaysAllowedInputData
-            // -----------------------------------------------------------------
-
             Date dueDate = programStageInstance.getDueDate();
 
             long diffMillis = dateValue.getTime() - dueDate.getTime();
@@ -228,26 +262,26 @@
 
                 return SUCCESS;
             }
-
         }
 
+        // ---------------------------------------------------------------------
+        // Save value
+        // ---------------------------------------------------------------------
+
         if ( programStageInstance.getExecutionDate() == null )
         {
             programStageInstance.setExecutionDate( new Date() );
             programStageInstanceService.updateProgramStageInstance( programStageInstance );
         }
 
-        if ( patientDataValue == null )
+        if ( patientDataValue == null && value != null )
         {
-            if ( value != null )
-            {
-                LOG.debug( "Adding PatientDataValue, value added" );
-
-                patientDataValue = new PatientDataValue( programStageInstance, dataElement, optionCombo,
-                    organisationUnit, new Date(), value, providedByAnotherFacility );
-
-                patientDataValueService.savePatientDataValue( patientDataValue );
-            }
+            LOG.debug( "Adding PatientDataValue, value added" );
+
+            patientDataValue = new PatientDataValue( programStageInstance, dataElement, optionCombo, organisationUnit,
+                new Date(), value, providedByAnotherFacility );
+
+            patientDataValueService.savePatientDataValue( patientDataValue );
         }
         else
         {

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveValueAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveValueAction.java	2011-05-19 01:11:43 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveValueAction.java	2011-05-27 01:09:01 +0000
@@ -37,7 +37,6 @@
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.dataelement.DataElementService;
-import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.patient.Patient;
@@ -123,13 +122,6 @@
         this.dataElementCategoryService = dataElementCategoryService;
     }
 
-    private I18nFormat format;
-
-    public void setFormat( I18nFormat format )
-    {
-        this.format = format;
-    }
-
     // -------------------------------------------------------------------------
     // Input/Output
     // -------------------------------------------------------------------------
@@ -241,18 +233,6 @@
             value = value.trim();
         }
 
-        if ( dataElement.getType().equalsIgnoreCase( DataElement.VALUE_TYPE_DATE ) && value != null )
-        {
-            Date dateValue = format.parseDate( value );
-
-            if ( dateValue == null )
-            {
-                statusCode = 1;
-
-                return SUCCESS;
-            }
-        }
-
         DataElementCategoryOptionCombo optionCombo = null;
 
         if ( optionComboId == 0 )
@@ -287,16 +267,16 @@
             }
         }
 
+        if ( programStageInstance.getExecutionDate() == null )
+        {
+            programStageInstance.setExecutionDate( new Date() );
+            programStageInstanceService.updateProgramStageInstance( programStageInstance );
+        }
+        
         if ( patientDataValue == null )
         {
             LOG.debug( "Adding PatientDataValue, value added" );
 
-            if ( programStageInstance.getExecutionDate() == null )
-            {
-                programStageInstance.setExecutionDate( new Date() );
-                programStageInstanceService.updateProgramStageInstance( programStageInstance );
-            }
-
             patientDataValue = new PatientDataValue( programStageInstance, dataElement, optionCombo, organisationUnit,
                 new Date(), value, providedByAnotherFacility );
 
@@ -306,12 +286,6 @@
         {
             LOG.debug( "Updating PatientDataValue, value added/changed" );
 
-            if ( programStageInstance.getExecutionDate() == null )
-            {
-                programStageInstance.setExecutionDate( new Date() );
-                programStageInstanceService.updateProgramStageInstance( programStageInstance );
-            }
-
             patientDataValue.setValue( value );
             patientDataValue.setOptionCombo( optionCombo );
             patientDataValue.setProvidedByAnotherFacility( providedByAnotherFacility );

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2011-05-20 09:23:01 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2011-05-27 01:09:01 +0000
@@ -84,10 +84,8 @@
 		scope="prototype">
 		<property name="organisationUnitService"
 			ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
-		<property name="patientService"
-			ref="org.hisp.dhis.patient.PatientService" />
-		<property name="programStageService"
-			ref="org.hisp.dhis.program.ProgramStageService" />
+		<property name="patientService" ref="org.hisp.dhis.patient.PatientService" />
+		<property name="programStageService" ref="org.hisp.dhis.program.ProgramStageService" />
 		<property name="dataElementService" ref="org.hisp.dhis.dataelement.DataElementService" />
 		<property name="dataElementCategoryService"
 			ref="org.hisp.dhis.dataelement.DataElementCategoryService" />
@@ -101,8 +99,10 @@
 	<bean id="org.hisp.dhis.caseentry.action.caseentry.SaveDateValueAction"
 		class="org.hisp.dhis.caseentry.action.caseentry.SaveDateValueAction"
 		scope="prototype">
-		<property name="selectedStateManager"
-			ref="org.hisp.dhis.caseentry.state.SelectedStateManager" />
+		<property name="organisationUnitService"
+			ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
+		<property name="patientService" ref="org.hisp.dhis.patient.PatientService" />
+		<property name="programStageService" ref="org.hisp.dhis.program.ProgramStageService" />
 		<property name="dataElementService" ref="org.hisp.dhis.dataelement.DataElementService" />
 		<property name="programInstanceService" ref="org.hisp.dhis.program.ProgramInstanceService" />
 		<property name="programStageInstanceService"
@@ -123,10 +123,8 @@
 			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
 		<property name="patientDataValueService"
 			ref="org.hisp.dhis.patientdatavalue.PatientDataValueService" />
-		<property name="patientService"
-			ref="org.hisp.dhis.patient.PatientService" />
-		<property name="programStageService"
-			ref="org.hisp.dhis.program.ProgramStageService" />
+		<property name="patientService" ref="org.hisp.dhis.patient.PatientService" />
+		<property name="programStageService" ref="org.hisp.dhis.program.ProgramStageService" />
 	</bean>
 
 	<bean id="org.hisp.dhis.caseentry.screen.DataEntryScreenManager"
@@ -145,14 +143,16 @@
 		<property name="programStageDataElementService"
 			ref="org.hisp.dhis.program.ProgramStageDataElementService" />
 	</bean>
-	
-	<bean id="org.hisp.dhis.caseentry.action.caseentry.LoadProgramStagesAction"
+
+	<bean
+		id="org.hisp.dhis.caseentry.action.caseentry.LoadProgramStagesAction"
 		class="org.hisp.dhis.caseentry.action.caseentry.LoadProgramStagesAction"
 		scope="prototype">
 		<property name="patientService" ref="org.hisp.dhis.patient.PatientService" />
 		<property name="programService" ref="org.hisp.dhis.program.ProgramService" />
 		<property name="programInstanceService" ref="org.hisp.dhis.program.ProgramInstanceService" />
-		<property name="programStageInstanceService" ref="org.hisp.dhis.program.ProgramStageInstanceService" />
+		<property name="programStageInstanceService"
+			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
 	</bean>
 
 	<bean id="org.hisp.dhis.caseentry.action.caseentry.LoadDataEntryAction"
@@ -160,8 +160,8 @@
 		scope="prototype">
 		<property name="programStageService" ref="org.hisp.dhis.program.ProgramStageService" />
 		<property name="programInstanceService" ref="org.hisp.dhis.program.ProgramInstanceService" />
-		<property name="dataEntryScreenManager"
-			ref="org.hisp.dhis.caseentry.screen.DataEntryScreenManager" />
+		<property name="programDataEntryService"
+			ref="org.hisp.dhis.program.ProgramDataEntryService" />
 		<property name="patientDataValueService"
 			ref="org.hisp.dhis.patientdatavalue.PatientDataValueService" />
 		<property name="programStageInstanceService"
@@ -170,7 +170,7 @@
 		<property name="selectionManager"
 			ref="org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager" />
 	</bean>
-	
+
 	<bean
 		id="org.hisp.dhis.caseentry.action.caseentry.CompleteDataEntryAction"
 		class="org.hisp.dhis.caseentry.action.caseentry.CompleteDataEntryAction"
@@ -193,14 +193,15 @@
 			ref="org.hisp.dhis.patient.PatientAttributeService" />
 	</bean>
 
-	<bean id="org.hisp.dhis.caseentry.action.caseentry.GetProgramsByOrgunitAction"
+	<bean
+		id="org.hisp.dhis.caseentry.action.caseentry.GetProgramsByOrgunitAction"
 		class="org.hisp.dhis.caseentry.action.caseentry.GetProgramsByOrgunitAction"
 		scope="prototype">
 		<property name="selectionManager"
 			ref="org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager" />
 		<property name="programService" ref="org.hisp.dhis.program.ProgramService" />
 	</bean>
-	
+
 	<bean id="org.hisp.dhis.caseentry.action.caseentry.GetDataRecordsAction"
 		class="org.hisp.dhis.caseentry.action.caseentry.GetDataRecordsAction"
 		scope="prototype">
@@ -234,7 +235,7 @@
 	</bean>
 
 	<!-- Visit Plan -->
-	
+
 	<bean id="org.hisp.dhis.caseentry.action.visitplan.VisitPlanAction"
 		class="org.hisp.dhis.caseentry.action.visitplan.VisitPlanAction"
 		scope="prototype">
@@ -317,7 +318,7 @@
 	</bean>
 
 	<!-- Case Aggregation -->
-	
+
 	<bean
 		id="org.hisp.dhis.caseentry.action.caseaggregation.CaseAggregationFormAction"
 		class="org.hisp.dhis.caseentry.action.caseaggregation.CaseAggregationFormAction"
@@ -330,17 +331,20 @@
 		class="org.hisp.dhis.caseentry.action.caseaggregation.LoadPeriodsAction"
 		scope="prototype">
 		<property name="dataSetService" ref="org.hisp.dhis.dataset.DataSetService" />
-		<property name="periodGenericManager" ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
+		<property name="periodGenericManager"
+			ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
 	</bean>
-	
+
 	<bean
 		id="org.hisp.dhis.caseentry.action.caseaggregation.ValidationCaseAggregationAction"
 		class="org.hisp.dhis.caseentry.action.caseaggregation.ValidationCaseAggregationAction"
 		scope="prototype">
-		<property name="selectionTreeManager" ref="org.hisp.dhis.oust.manager.SelectionTreeManager" />
-		<property name="periodGenericManager" ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
+		<property name="selectionTreeManager"
+			ref="org.hisp.dhis.oust.manager.SelectionTreeManager" />
+		<property name="periodGenericManager"
+			ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
 	</bean>
-	
+
 	<bean
 		id="org.hisp.dhis.caseentry.action.caseaggregation.CaseAggregationResultAction"
 		class="org.hisp.dhis.caseentry.action.caseaggregation.CaseAggregationResultAction"
@@ -352,44 +356,43 @@
 			ref="org.hisp.dhis.caseaggregation.CaseAggregationConditionService" />
 		<property name="dataValueService" ref="org.hisp.dhis.datavalue.DataValueService" />
 		<property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
-		<property name="periodGenericManager" ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
+		<property name="periodGenericManager"
+			ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
 	</bean>
-	
+
 	<bean
 		id="org.hisp.dhis.caseentry.action.caseaggregation.CaseAggregationResultDetailsAction"
 		class="org.hisp.dhis.caseentry.action.caseaggregation.CaseAggregationResultDetailsAction"
 		scope="prototype">
-		<property name="organisationUnitService" ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
+		<property name="organisationUnitService"
+			ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
 		<property name="periodService" ref="org.hisp.dhis.period.PeriodService" />
 		<property name="aggregationConditionService"
 			ref="org.hisp.dhis.caseaggregation.CaseAggregationConditionService" />
 		<property name="patientDataValueService"
 			ref="org.hisp.dhis.patientdatavalue.PatientDataValueService" />
 	</bean>
-	
+
 	<bean id="org.hisp.dhis.caseentry.state.PeriodGenericManager"
 		class="org.hisp.dhis.caseentry.state.DefaultPeriodGenericManager"
 		scope="singleton">
 		<property name="periodService" ref="org.hisp.dhis.period.PeriodService" />
 	</bean>
-	
-	<bean
-		id="org.hisp.dhis.caseentry.state.NextPeriodsAction"
-		class="org.hisp.dhis.caseentry.state.NextPeriodsAction"
-		scope="prototype">
-		<property name="periodGenericManager"
-			ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
-	</bean>
-
-	<bean
-		id="org.hisp.dhis.caseentry.state.PreviousPeriodsAction"
-		class="org.hisp.dhis.caseentry.state.PreviousPeriodsAction"
-		scope="prototype">
-		<property name="periodGenericManager"
-			ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
-	</bean>
-	
-	<bean id="org.hisp.dhis.caseentry.action.caseentry.ValidateProgramInstanceAction"
+
+	<bean id="org.hisp.dhis.caseentry.state.NextPeriodsAction" class="org.hisp.dhis.caseentry.state.NextPeriodsAction"
+		scope="prototype">
+		<property name="periodGenericManager"
+			ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
+	</bean>
+
+	<bean id="org.hisp.dhis.caseentry.state.PreviousPeriodsAction"
+		class="org.hisp.dhis.caseentry.state.PreviousPeriodsAction" scope="prototype">
+		<property name="periodGenericManager"
+			ref="org.hisp.dhis.caseentry.state.PeriodGenericManager" />
+	</bean>
+
+	<bean
+		id="org.hisp.dhis.caseentry.action.caseentry.ValidateProgramInstanceAction"
 		class="org.hisp.dhis.caseentry.action.caseentry.ValidateProgramInstanceAction"
 		scope="prototype">
 		<property name="selectedStateManager"
@@ -403,10 +406,8 @@
 			ref="org.hisp.dhis.program.ProgramValidationService" />
 		<property name="orgunitService"
 			ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
-		<property name="patientService"
-			ref="org.hisp.dhis.patient.PatientService" />
-		<property name="programStageService"
-			ref="org.hisp.dhis.program.ProgramStageService" />
+		<property name="patientService" ref="org.hisp.dhis.patient.PatientService" />
+		<property name="programStageService" ref="org.hisp.dhis.program.ProgramStageService" />
 	</bean>
-	
+
 </beans>

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties	2011-05-25 05:27:00 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/org/hisp/dhis/caseentry/i18n_module.properties	2011-05-27 01:09:01 +0000
@@ -265,8 +265,8 @@
 to 																= To
 ga_orgunit                                                      = OrganisationUnit
 ga_facilityby                                                   = OrganisationUnit By
-date_is_greater_then_or_equals_due_date							= Date inputed is greater then or equals due date
-date_is_less_then_or_equals_plus_no_max_days					= Date inputed is less then or equals [ Due Date + Number of max days allowed to input data ]
+date_is_greater_then_or_equals_due_date							= Date inputed >= Due date
+date_is_less_then_or_equals_plus_no_max_days					= Date inputed <= ( Due Date + Number of max days allowed to input data )
 min_days														= Min days
 max_days														= Max days
 value_must_number												= Value must be an number

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml	2011-05-20 09:23:01 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml	2011-05-27 01:09:01 +0000
@@ -80,7 +80,7 @@
     <!-- save value -->
     
     <action name="saveValue" class="org.hisp.dhis.caseentry.action.caseentry.SaveValueAction">
-      <result name="success" type="velocity">status.vm</result>
+      <result name="success" type="velocity-xml">status.vm</result>
       <result name="input" type="velocity-xml">/dhis-web-caseentry/responseInput.vm</result>
       <param name="onExceptionReturn">plainTextError</param>      
     </action>

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/dataEntryForm.vm'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/dataEntryForm.vm	2011-05-26 08:25:21 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/dataEntryForm.vm	2011-05-27 01:09:01 +0000
@@ -16,14 +16,16 @@
 		</script>
 </p>
 
+<input type='hidden' id='programStageInstanceId' value='$programStageInstance.id'>
+<input type='hidden' id='programStageInstanceName' value='$programStageInstance.name'>
+<input type='hidden' id='incidentDate' value='$!format.formatDate($programStageInstance.programInstance.dateOfIncident)'>
+<input type='hidden' id='orgunitId' value='$organisationUnit.id'>
+	
 <hr style="clear:both">
 
-<span id="startMsg" style="font-style:italic; color:blue"> </span>
+<span id="startMsg" style="font-style:italic; color:blue; display: #if($programStageInstance.executionDate) none #else block #end">$i18n.getString( "report_date_warning" )</span>
 
-<div id='entryForm'>
-	<input type='hidden' id='programStageInstanceId' value='$programStageInstance.id'>
-	<input type='hidden' id='programStageInstanceName' value='$programStageInstance.name'>
-	<input type='hidden' id='incidentDate' value='$!format.formatDate($programStageInstance.programInstance.dateOfIncident)'>
+<div id='entryForm' style="display: #if($programStageInstance.executionDate) block #else none #end">
 	
 	<div id="customEntryScreenContainer">
 		#if( $customDataEntryFormCode ) 
@@ -124,6 +126,7 @@
 
 	</div>
 </div>
+
 </div>
 <script type="text/javascript"> 
 	initCustomCheckboxes();

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/dataEntry.js'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/dataEntry.js	2011-05-26 08:25:21 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/dataEntry.js	2011-05-27 01:09:01 +0000
@@ -18,7 +18,6 @@
 			jQuery('#searchText').removeAttr( 'readonly' );
 			
 			setFieldValue( 'orgunitName', data.getElementsByTagName( "name" )[0].firstChild.nodeValue );
-			setFieldValue( 'orgunitId', data.getElementsByTagName( "id" )[0].firstChild.nodeValue );
 		
 			hideLoader();
 		},'xml');
@@ -130,37 +129,23 @@
 	hideById('dataEntryFormDiv');
 	if( getFieldValue('programStageId') == '0' )
 	{
+		disable('validationBtn');
+		disable('completeBtn');
 		return;
 	}
 	
 	// Load data-entry form
 	showLoader();
-	var useDefaultForm = ( jQuery('#useDefaultForm').attr('checked')== 'checked' ) ? true : false;
 	
 	jQuery('#dataEntryFormDiv').load("dataentryform.action",
 		{
 			programStageId:getFieldValue('programStageId'),
-			patientId: getFieldValue('patientId'),
-			useDefaultForm: useDefaultForm
+			patientId: getFieldValue('patientId')
 		}, 
 		function( )
 		{
 		}).slideDown('fast', function()
 		{
-			if ( getFieldValue('executionDate') =='' )
-			{
-				hideById('entryForm');
-				setInnerHTML('startMsg', i18n_report_date_warning);
-			}
-			else
-			{
-				showById('entryForm');
-				setInnerHTML('startMsg', '');
-			}
-			
-			selectUseDefaultDataEntryForm();
-			
-			enable('executionDate');
 			enable('validationBtn');
 			enable('completeBtn');
 			enable('useDefaultForm');
@@ -333,7 +318,7 @@
     var executionDateSaver = new ExecutionDateSaver( programStageInstanceId, field.value, '#ccffcc' );
     executionDateSaver.save();
 	
-    if( !jQuery("#entryFormContainer").is(":visible") )
+    if( !jQuery("#entryForm").is(":visible") )
     {
         toggleContentForReportDate(true);
     }
@@ -431,11 +416,19 @@
 
     this.save = function()
     {
+		var params = 'dataElementId=' + dataElementId 
+			params +=  '&value=' + value 
+			params +=  '&providedByAnotherFacility=' + providedByAnotherFacility
+			params += '&orgunitId=' + getFieldValue('orgunitId');
+			params += '&patientId=' + getFieldValue('patientId');
+			params += '&programStageId=' + getFieldValue('programStageId');
+			
         var request = new Request();
         request.setCallbackSuccess( handleResponse );
         request.setCallbackError( handleHttpError );
         request.setResponseTypeXML( 'status' );
-        request.send( 'saveDateValue.action?dataElementId=' + dataElementId + '&value=' + value + '&providedByAnotherFacility=' + providedByAnotherFacility );
+		request.sendAsPost( params );
+        request.send( 'saveDateValue.action' );
     };
 
     function handleResponse( rootElement )
@@ -511,11 +504,19 @@
 
     this.save = function()
     {
+		var params = 'dataElementId=' + dataElementId 
+			params +=  '&value=' + value 
+			params +=  '&providedByAnotherFacility=' + providedByAnotherFacility
+			params += '&orgunitId=' + getFieldValue('orgunitId');
+			params += '&patientId=' + getFieldValue('patientId');
+			params += '&programStageId=' + getFieldValue('programStageId');
+		
         var request = new Request();
         request.setCallbackSuccess( handleResponse );
         request.setCallbackError( handleHttpError );
         request.setResponseTypeXML( 'status' );
-        request.send( 'saveDateValue.action?dataElementId=' + dataElementId + '&value=' + value + '&providedByAnotherFacility=' + providedByAnotherFacility );
+		request.sendAsPost( params );
+        request.send( 'saveDateValue.action');
     };
 
     function handleResponse( rootElement )
@@ -949,8 +950,10 @@
 				params += '&patientId=' + getFieldValue('patientId');
 				params += '&programStageId=' + getFieldValue('programStageId');
 			
-			request.sendAsPost( params );
-            request.send( 'saveValue.action' );
+			request.sendAsPost( params ); 
+			
+			if( type == 'date' ) request.send( 'saveDateValue.action' );
+			else request.send( 'saveValue.action' );
         }
         else
         {	
@@ -962,7 +965,8 @@
 				params += '&programStageId=' + getFieldValue('programStageId');
 				
 			request.sendAsPost( params );
-            request.send( 'saveValue.action' );
+			if( type == 'date' ) request.send( 'saveDateValue.action' );
+			else request.send( 'saveValue.action' );
         }
 		
 		
@@ -981,7 +985,7 @@
             if(value!="")
             {
                 markValue( ERROR );
-                window.alert( i18n_invalid_date );
+                window.alert( rootElement.getElementsByTagName( "message" )[0].firstChild.nodeValue );
             }
             else
             {
@@ -1189,12 +1193,13 @@
 
     if(jQuery(this_).val()!="")
     { 
-        if( !isValidDate( jQuery(this_).val() ) )
+		var d2 = new Date(jQuery(this_).val() );
+        if( d2 == 'Invalid Date' )
         {
             jQuery(this_).css({
                 "background-color":"#ffcc00"
             });
-            window.alert('Incorrect format for date value. The correct format should be ' + dateFormat.replace('yy', 'yyyy') +' \n\n '+data.dataElementName );
+            window.alert('Incorrect format for date value. The correct format should be ' + dateFormat.replace('yy', 'yyyy') +' \n\n ' + data.dataElementName );
 		  
             jQuery(this_).focus();
 
@@ -1283,7 +1288,7 @@
         var dragDiv = jQuery("#dragDiv");
         dragDiv.show();
         var left = screen.width - 500 ;
-        var top = Math.round(jQuery("#startMsg").position().top )  ;
+        var top = Math.round(jQuery("#entryFormContainer").position().top )  ;
         dragDiv.css({
             'left': left+'px',
             'top': top+'px'
@@ -1309,11 +1314,11 @@
 {
     if( show ){
         jQuery("#startMsg").hide();
-        jQuery("#entryFormContainer").show();
+        jQuery("#entryForm").show();
         jQuery("#completeBtn").removeAttr('disabled');
 		jQuery("#validationBtn").removeAttr('disabled');
     }else {
-        jQuery("#entryFormContainer").hide();
+        jQuery("#entryForm").hide();
         jQuery("#completeBtn").attr('disabled', 'disabled');
 		jQuery("#validationBtn").attr('disabled', 'disabled');
         jQuery("#startMsg").show();
@@ -1479,6 +1484,8 @@
 {
 	var currentFocus; 
     if( jQuery("#entryFormContainer") ) {
+		
+		selectUseDefaultDataEntryForm();
         if( jQuery("#executionDate").val() )
         {
             jQuery("#startMsg").hide();
@@ -1491,7 +1498,8 @@
             function(){
                 var childrens = jQuery(this).children("input[name='entryfield'],select[name='entryselect']");
                 
-				if( jQuery(childrens[0]).is(":disabled")) {
+				if( jQuery(childrens[0]).is(":disabled")) 
+				{
                     DRAG_DIV.showData(jQuery(childrens[0]).metadata({
                         "type":"attr",
                         "name":"data"

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/select.vm'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/select.vm	2011-05-25 08:44:29 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/select.vm	2011-05-27 01:09:01 +0000
@@ -42,8 +42,6 @@
 <div id='dataRecordingSelectDiv'></div>
 <div id='dataEntryFormDiv'></div>
 
-<input type='hidden' id='orgunitId' >
-
 #parse( "dhis-web-commons/loader/loader.vm" )
 
 <script type="text/javascript">
@@ -55,7 +53,6 @@
 	var i18n_blood_group = '$encoder.jsEscape( $i18n.getString( "blood_group" ), "'")';
 	var i18n_none = '$encoder.jsEscape( $i18n.getString( "none" ), "'")';
 	var i18n_select = '[' + '$encoder.jsEscape( $i18n.getString( "select" ), "'")' + ']';
-	var i18n_report_date_warning = '$encoder.jsEscape( $i18n.getString( "report_date_warning" ) , "'")';
 	var i18n_program_stages_history_plan = '$encoder.jsEscape( $i18n.getString( "program_stages_history_plan" ) , "'")';
 	
 	var i18n_value_must_integer = '$encoder.jsEscape( $i18n.getString( "value_must_integer" ) , "'")';

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/status.vm'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/status.vm	2010-10-28 09:17:13 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/status.vm	2011-05-27 01:09:01 +0000
@@ -5,5 +5,5 @@
 				<validation>$validation.description</validation>
 		#end
 	</validations>
-	<message>$message</message>
+	<message>$encoder.xmlEncode($message)</message>
 </status>