← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5448: Create many program-stage-instance for an anonymous-program-instance.

 

------------------------------------------------------------
revno: 5448
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-12-16 13:45:30 +0700
message:
  Create many program-stage-instance for an anonymous-program-instance.
added:
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CreateAnonymousEncounterAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonProgramInstances.vm
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientdatavalue/PatientDataValueDeletionHandler.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadAnonymousProgramsAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadProgramStagesAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/RemoveCurrentEncounterAction.java
  dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveExecutionDateAction.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/struts.xml
  dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/anonymousRegistration.vm
  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/anonymousRegistration.js
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/AddProgramAction.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/META-INF/dhis/beans.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java	2011-05-05 21:14:56 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramStageInstance.java	2011-12-16 06:45:30 +0000
@@ -113,6 +113,9 @@
 
         result = result * prime + programInstance.hashCode();
         result = result * prime + programStage.hashCode();
+        result = result * prime + dueDate.hashCode();
+        result = result * prime + ( ( executionDate == null) ? 0 : executionDate.hashCode() );
+        result = result * prime + stageInProgram;
 
         return result;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientdatavalue/PatientDataValueDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientdatavalue/PatientDataValueDeletionHandler.java	2011-12-15 06:47:36 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientdatavalue/PatientDataValueDeletionHandler.java	2011-12-16 06:45:30 +0000
@@ -30,6 +30,7 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.program.ProgramInstance;
 import org.hisp.dhis.program.ProgramStage;
+import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.system.deletion.DeletionHandler;
 import org.springframework.jdbc.core.JdbcTemplate;
 
@@ -73,21 +74,27 @@
     @Override
     public String allowDeleteProgramStage( ProgramStage programStage )
     {
-        String sql = "SELECT COUNT(*) "
-                    + "FROM patientdatavalue pdv INNER JOIN programstageinstance psi "
-                    + "ON pdv.programstageinstanceid=psi.programstageinstanceid "
-                    + "WHERE psi.programstageid=" + programStage.getId() ;
+        String sql = "SELECT COUNT(*) " + "FROM patientdatavalue pdv INNER JOIN programstageinstance psi "
+            + "ON pdv.programstageinstanceid=psi.programstageinstanceid " + "WHERE psi.programstageid="
+            + programStage.getId();
 
         return jdbcTemplate.queryForInt( sql ) == 0 ? null : ERROR;
     }
-    
+
     @Override
     public void deleteProgramInstance( ProgramInstance programInstance )
-    { 
-        String sql = "DELETE FROM patientdatavalue " +
-        		"WHERE programstageinstanceid in " +
-        		"( SELECT programstageinstanceid FROM programstageinstance " +
-        		"WHERE programinstanceid = " + programInstance.getId() + ")";
+    {
+        String sql = "DELETE FROM patientdatavalue " + "WHERE programstageinstanceid in "
+            + "( SELECT programstageinstanceid FROM programstageinstance " + "WHERE programinstanceid = "
+            + programInstance.getId() + ")";
+
+        jdbcTemplate.execute( sql );
+    }
+
+    @Override
+    public void deleteProgramStageInstance( ProgramStageInstance programStageInstance )
+    {
+        String sql = "DELETE FROM patientdatavalue " + "WHERE programstageinstanceid = " + programStageInstance.getId();
 
         jdbcTemplate.execute( sql );
     }

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java	2011-05-10 07:25:28 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CompleteDataEntryAction.java	2011-12-16 06:45:30 +0000
@@ -87,7 +87,7 @@
     {
         this.programStageInstanceId = programStageInstanceId;
     }
-    
+
     // -------------------------------------------------------------------------
     // Implementation Action
     // -------------------------------------------------------------------------
@@ -112,22 +112,25 @@
         // ProgramInstance
         // ----------------------------------------------------------------------
 
-        ProgramInstance programInstance = programStageInstance.getProgramInstance();
-
-        Set<ProgramStageInstance> stageInstances = programInstance.getProgramStageInstances();
-
-        for ( ProgramStageInstance stageInstance : stageInstances )
+        if ( !programStageInstance.getProgramInstance().getProgram().getAnonymous() )
         {
-            if ( !stageInstance.isCompleted() )
+            ProgramInstance programInstance = programStageInstance.getProgramInstance();
+
+            Set<ProgramStageInstance> stageInstances = programInstance.getProgramStageInstances();
+
+            for ( ProgramStageInstance stageInstance : stageInstances )
             {
-                return SUCCESS;
+                if ( !stageInstance.isCompleted() )
+                {
+                    return SUCCESS;
+                }
             }
+
+            programInstance.setCompleted( true );
+            programInstance.setEndDate( new Date() );
+
+            programInstanceService.updateProgramInstance( programInstance );
         }
-
-        programInstance.setCompleted( true );
-        programInstance.setEndDate( new Date() );
-
-        programInstanceService.updateProgramInstance( programInstance );
         
         return SUCCESS;
     }

=== added file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CreateAnonymousEncounterAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CreateAnonymousEncounterAction.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/CreateAnonymousEncounterAction.java	2011-12-16 06:45:30 +0000
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2004-2010, 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.caseentry.action.caseentry;
+
+import java.util.Date;
+
+import org.hisp.dhis.i18n.I18n;
+import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.program.ProgramInstance;
+import org.hisp.dhis.program.ProgramInstanceService;
+import org.hisp.dhis.program.ProgramStage;
+import org.hisp.dhis.program.ProgramStageInstance;
+import org.hisp.dhis.program.ProgramStageInstanceService;
+
+import com.opensymphony.xwork2.Action;
+
+/**
+ * @author Chau Thu Tran
+ * 
+ * @version $Id: CreateAnonymousEncounterAction.java Dec 16, 2011 9:08:12 AM $
+ */
+public class CreateAnonymousEncounterAction
+    implements Action
+{
+    // -------------------------------------------------------------------------
+    // Dependencies
+    // -------------------------------------------------------------------------
+
+    private ProgramInstanceService programInstanceService;
+
+    public void setProgramInstanceService( ProgramInstanceService programInstanceService )
+    {
+        this.programInstanceService = programInstanceService;
+    }
+
+    private ProgramStageInstanceService programStageInstanceService;
+
+    public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService )
+    {
+        this.programStageInstanceService = programStageInstanceService;
+    }
+
+    private I18nFormat format;
+
+    public void setFormat( I18nFormat format )
+    {
+        this.format = format;
+    }
+
+    private I18n i18n;
+
+    public void setI18n( I18n i18n )
+    {
+        this.i18n = i18n;
+    }
+    
+    // -------------------------------------------------------------------------
+    // Input/Output
+    // -------------------------------------------------------------------------
+
+
+    private Integer programInstanceId;
+
+    public void setProgramInstanceId( Integer programInstanceId )
+    {
+        this.programInstanceId = programInstanceId;
+    }
+
+    public String executionDate;
+
+    public void setExecutionDate( String executionDate )
+    {
+        this.executionDate = executionDate;
+    }
+
+    private String message;
+
+    public String getMessage()
+    {
+        return message;
+    }
+
+    // -------------------------------------------------------------------------
+    // Action implementation
+    // -------------------------------------------------------------------------
+
+    @Override
+    public String execute()
+        throws Exception
+    {
+        Date date = format.parseDate( executionDate );
+
+        if ( date != null )
+        {
+
+            ProgramInstance programInstance = programInstanceService.getProgramInstance( programInstanceId );
+
+            ProgramStageInstance programStageInstance = new ProgramStageInstance();
+            programStageInstance.setProgramInstance( programInstance );
+
+            ProgramStage programStage = programInstance.getProgram().getProgramStages().iterator().next();
+            programStageInstance.setProgramStage( programStage );
+
+            programStageInstance.setStageInProgram( programInstance.getProgramStageInstances().size() + 1 );
+            programStageInstance.setDueDate( date );
+            programStageInstance.setExecutionDate( date );
+
+            programStageInstanceService.addProgramStageInstance( programStageInstance );
+            
+            message = programStageInstance.getId() + "";
+            
+            return SUCCESS;
+        }
+
+        message = i18n.getString("please_enter_report_date");
+
+        return INPUT;
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadAnonymousProgramsAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadAnonymousProgramsAction.java	2011-12-14 07:33:18 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadAnonymousProgramsAction.java	2011-12-16 06:45:30 +0000
@@ -27,15 +27,14 @@
 
 package org.hisp.dhis.caseentry.action.caseentry;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
+import java.util.Collection;
 
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
 import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramInstance;
+import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramService;
-import org.hisp.dhis.program.comparator.ProgramNameComparator;
 
 import com.opensymphony.xwork2.Action;
 
@@ -65,15 +64,22 @@
         this.programService = programService;
     }
 
+    private ProgramInstanceService programInstanceService;
+
+    public void setProgramInstanceService( ProgramInstanceService programInstanceService )
+    {
+        this.programInstanceService = programInstanceService;
+    }
+
     // -------------------------------------------------------------------------
     // Output
     // -------------------------------------------------------------------------
 
-    private List<Program> programs;
+    private Collection<ProgramInstance> programInstances;
 
-    public List<Program> getPrograms()
+    public Collection<ProgramInstance> getProgramInstances()
     {
-        return programs;
+        return programInstances;
     }
 
     // -------------------------------------------------------------------------
@@ -87,11 +93,11 @@
 
         if ( orgunit != null )
         {
-            programs = new ArrayList<Program>( programService.getPrograms( true, true, orgunit ) );
+            Collection<Program> programs = programService.getPrograms( true, true, orgunit );
 
-            Collections.sort( programs, new ProgramNameComparator() );
+            programInstances = programInstanceService.getProgramInstances( programs );
         }
-        
+
         return SUCCESS;
     }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadProgramStagesAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadProgramStagesAction.java	2011-12-15 03:09:59 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/LoadProgramStagesAction.java	2011-12-16 06:45:30 +0000
@@ -158,11 +158,15 @@
             programInstance = programInstances.iterator().next();
 
             selectedStateManager.setSelectedProgramInstance( programInstance );
-
-            colorMap = programStageInstanceService.colorProgramStageInstances( programInstance
-                .getProgramStageInstances() );
+            
+            if ( programInstance.getProgramStageInstances() != null )
+            {
+                colorMap = programStageInstanceService.colorProgramStageInstances( programInstance
+                    .getProgramStageInstances() );
+            }
         }
 
+       
         return SUCCESS;
     }
 

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/RemoveCurrentEncounterAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/RemoveCurrentEncounterAction.java	2011-12-15 06:47:36 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/RemoveCurrentEncounterAction.java	2011-12-16 06:45:30 +0000
@@ -29,8 +29,8 @@
 
 import org.hisp.dhis.common.DeleteNotAllowedException;
 import org.hisp.dhis.i18n.I18n;
-import org.hisp.dhis.program.ProgramInstance;
-import org.hisp.dhis.program.ProgramInstanceService;
+import org.hisp.dhis.program.ProgramStageInstance;
+import org.hisp.dhis.program.ProgramStageInstanceService;
 
 import com.opensymphony.xwork2.Action;
 
@@ -46,22 +46,22 @@
     // Dependencies
     // -------------------------------------------------------------------------
 
-    private ProgramInstanceService programInstanceService;
+    private ProgramStageInstanceService programStageInstanceService;
 
-    public void setProgramInstanceService( ProgramInstanceService programInstanceService )
+    public void setProgramStageInstanceService( ProgramStageInstanceService programStageInstanceService )
     {
-        this.programInstanceService = programInstanceService;
+        this.programStageInstanceService = programStageInstanceService;
     }
 
     // --------------------------------------------------------------------------
     // Input
     // -------------------------------------------------------------------------
 
-    private int programInstanceId;
+    private int programStageInstanceId;
 
-    public void setProgramInstanceId( int programInstanceId )
+    public void setProgramStageInstanceId( int programStageInstanceId )
     {
-        this.programInstanceId = programInstanceId;
+        this.programStageInstanceId = programStageInstanceId;
     }
 
     private I18n i18n;
@@ -91,9 +91,9 @@
     {
         try
         {
-            ProgramInstance programInstance = programInstanceService.getProgramInstance( programInstanceId );
+            ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance( programStageInstanceId );
 
-            programInstanceService.deleteProgramInstance( programInstance );
+            programStageInstanceService.deleteProgramStageInstance( programStageInstance );
         }
         catch ( DeleteNotAllowedException ex )
         {

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveExecutionDateAction.java'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveExecutionDateAction.java	2011-12-15 03:09:59 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SaveExecutionDateAction.java	2011-12-16 06:45:30 +0000
@@ -148,7 +148,7 @@
                 ProgramStage programStage = programStageService.getProgramStage( programStageId );
                 Program program = programStage.getProgram();
 
-                if ( programStage.getProgram().getSingleEvent() )
+                if ( programStage.getProgram().getSingleEvent() && !programStage.getProgram().getAnonymous() )
                 {
                     // Add a new program-instance
                     ProgramInstance programInstance = new ProgramInstance();
@@ -156,16 +156,13 @@
                     programInstance.setDateOfIncident( dateValue );
                     programInstance.setProgram( program );
                     programInstance.setCompleted( false );
-                    
-                    if ( !programStage.getProgram().getAnonymous() )
-                    {
-                        programInstance.setPatient( patient );
-                        patient.getPrograms().add( program );
-                        patientService.updatePatient( patient );
-                    }
-                    
+
+                    programInstance.setPatient( patient );
+                    patient.getPrograms().add( program );
+                    patientService.updatePatient( patient );
+
                     programInstanceService.addProgramInstance( programInstance );
-                    
+
                     // Add a new program-stage-instance
                     programStageInstance = new ProgramStageInstance();
                     programStageInstance.setProgramInstance( programInstance );

=== 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-12-15 06:47:36 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml	2011-12-16 06:45:30 +0000
@@ -380,12 +380,22 @@
 			ref="org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager" />
 		<property name="programService"
 			ref="org.hisp.dhis.program.ProgramService" />
+		<property name="programInstanceService"
+			ref="org.hisp.dhis.program.ProgramInstanceService" />
 	</bean>
 	
 	<bean id="org.hisp.dhis.caseentry.action.caseentry.RemoveCurrentEncounterAction"
 		class="org.hisp.dhis.caseentry.action.caseentry.RemoveCurrentEncounterAction" scope="prototype">
+		<property name="programStageInstanceService"
+			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
+	</bean>
+	
+	<bean id="org.hisp.dhis.caseentry.action.caseentry.CreateAnonymousEncounterAction"
+		class="org.hisp.dhis.caseentry.action.caseentry.CreateAnonymousEncounterAction" scope="prototype">
 		<property name="programInstanceService"
 			ref="org.hisp.dhis.program.ProgramInstanceService" />
+		<property name="programStageInstanceService"
+			ref="org.hisp.dhis.program.ProgramStageInstanceService" />
 	</bean>
 
 	<!--  Patient chart -->

=== 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-12-15 06:47:36 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml	2011-12-16 06:45:30 +0000
@@ -434,10 +434,18 @@
 			<param name="stylesheets">../dhis-web-caseentry/style/patient.css</param>
 		</action>
 		
+		<action name="createAnonymousEncounter"
+			class="org.hisp.dhis.caseentry.action.caseentry.CreateAnonymousEncounterAction">
+			<result name="success" type="velocity-json">
+				/dhis-web-commons/ajax/jsonResponseSuccess.vm</result>
+			<result name="input" type="velocity-json">
+				/dhis-web-commons/ajax/jsonResponseInput.vm</result>
+		</action>
+		
 		<action name="loadAnonymousPrograms"
 			class="org.hisp.dhis.caseentry.action.caseentry.LoadAnonymousProgramsAction">
 			<result name="success" type="velocity-json">
-				/dhis-web-commons/ajax/jsonPrograms.vm</result>
+				/dhis-web-caseentry/jsonProgramInstances.vm</result>
 		</action>
 		
 		<action name="removeCurrentEncounter"

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/anonymousRegistration.vm'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/anonymousRegistration.vm	2011-12-15 06:47:36 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/anonymousRegistration.vm	2011-12-16 06:45:30 +0000
@@ -4,6 +4,7 @@
 <div id="actions">	
 	<input type="button" id="createEventBtn" value="$i18n.getString('create_new_event')" onClick="javascript: createNewEvent();" style="width:12em " disabled="disabled">
 	<input type="button" id="deleteCurrentEventBtn" value="$i18n.getString('delete_current_event')" onClick="javascript: deleteCurrentEvent();" style="width:12em " disabled="disabled" >
+	<input type="button" id="completeBtn" value="$i18n.getString('complete')" onClick="doComplete(); afterCompleteStage()" style="width:12em" disabled="disabled" >
 	<input type="button" id="validationBtn" value="$i18n.getString('run_validation')" onClick="javascript: runValidation();" style="width:12em " disabled="disabled" >
 </div>	
 			
@@ -15,8 +16,8 @@
 			<td>
 				<select id='programId' style='width:300px' onchange='showEventForm();'>
 					<option value="">[ $i18n.getString('please_select') ]</option>
-					#foreach( $program in $programs)
-						<option value="$program.id" singleevent='$program.singleEvent'>$program.name</option>
+					#foreach( $programInstance in $programInstances)
+						<option value="$programInstance.program.id" singleevent='$programInstance.program.singleEvent' programInstanceId='$programInstance.id'>$programInstance.program.name</option>
 					#end
 				</select>
 			</td>

=== 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-12-15 06:47:36 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/dataEntryForm.vm	2011-12-16 06:45:30 +0000
@@ -10,9 +10,11 @@
 		}
 		#end
 		
-		$('#executionDate').change(function() {
-			saveExecutionDate( getFieldValue('programStageId'), getFieldValue('executionDate') );
-		});
+		#if( $program.anonymous == "false" )
+			$('#executionDate').change(function() {
+				saveExecutionDate( getFieldValue('programStageId'), getFieldValue('executionDate') );
+			});
+		#end
 			
 		entryFormContainerOnReady();
 	});
@@ -102,9 +104,11 @@
 							</select>
 						#elseif( $programStageDataElement.dataElement.type == "date" )
 							<input type="text" data="{compulsory: $programStageDataElement.compulsory }" #if($programStageInstance.completed) disabled="disabled" #end id="$id" name="entryfield" value="$!encoder.htmlEncode( $patientDataValue.value )" onchange="saveDate( $programStageDataElement.dataElement.id )" onkeypress="return keyPress(event, this)" tabindex="$tabIndex"  style="text-align:center;">
-							<script type="text/javascript">
-								datePicker($programStageDataElement.programStage.id + '-' + $programStageDataElement.dataElement.id + '-val', false);
-							</script>                     
+							#if(!$programStageInstance.completed)
+								<script type="text/javascript">
+									datePicker($programStageDataElement.programStage.id + '-' + $programStageDataElement.dataElement.id + '-val', false);
+								</script>   
+							#end
 						#else
 							#foreach( $optionCombo in $programStageDataElement.dataElement.categoryCombo.optionCombos )
 								#set( $id = $programStageDataElement.programStage.id + '-' + $programStageDataElement.dataElement.id + '-' + $optionCombo.id + '-val' )

=== modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js	2011-12-15 06:47:36 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/anonymousRegistration.js	2011-12-16 06:45:30 +0000
@@ -3,7 +3,6 @@
 {
 	disable('executionDate');
 	setFieldValue('executionDate', '');
-	$('#executionDate').unbind('change');
 	
 	disable('createEventBtn');
 	disable('deleteCurrentEventBtn');
@@ -15,9 +14,9 @@
 			clearListById( 'programId' );
 			addOptionById( 'programId', '', i18n_please_select );
 			
-			for ( i in json.programs ) 
+			for ( i in json.programInstances ) 
 			{
-				$('#programId').append('<option value=' + json.programs[i].id + ' singleevent="true">' + json.programs[i].name + '</option>');
+				$('#programId').append('<option value=' + json.programInstances[i].id + ' singleevent="true" programInstanceId=' + json.programInstances[i].programInstanceId + '>' + json.programInstances[i].name + '</option>');
 			}			
 			
 		} );
@@ -45,7 +44,18 @@
 		function( json ) 
 		{    
 			setFieldValue( 'programStageId', json.programStages[0].id );
-			loadEventRegistrationForm();
+			
+			if( json.programStageInstances.length > 0 )
+			{
+				loadEventRegistrationForm();
+			}
+			else
+			{
+				enable('executionDate');
+				enable('createEventBtn');
+				disable('deleteCurrentEventBtn');
+				hideById('loaderDiv');
+			}
 			
 	});
 }
@@ -57,45 +67,77 @@
 			programStageId:getFieldValue('programStageId')
 		},function( )
 		{
-			enable('executionDate');
 			hideById('loaderDiv');
 			showById('dataEntryFormDiv');
 			
 			var programStageInstanceId = getFieldValue('programStageInstanceId');
-			
 			if( programStageInstanceId == '' )
 			{
-				$('#executionDate').unbind('change');
+				enable('executionDate');
+				enable('createEventBtn');
 				disable('deleteCurrentEventBtn');
-				enable('createEventBtn');
+				disable('completeBtn');
+				
+				$('#executionDate').change(function() {
+					saveExecutionDate( getFieldValue('programStageId'), getFieldValue('executionDate') );
+				});
 			}
 			else
 			{
+				if( getFieldValue('completed') == 'true')
+				{
+					disable('executionDate');
+					enable('createEventBtn');
+					enable('deleteCurrentEventBtn');
+					disable('completeBtn');
+				} 
+				else
+				{
+					enable('executionDate');
+					disable('createEventBtn');
+					enable('deleteCurrentEventBtn');
+					enable('completeBtn');
+					
+					
+					$('#executionDate').change(function() {
+						saveExecutionDate( getFieldValue('programStageId'), getFieldValue('executionDate') );
+					});
+				}
+			}
+			
+		} );
+}
+
+function createNewEvent()
+{
+	jQuery.postJSON( "createAnonymousEncounter.action",
+		{
+			programInstanceId: jQuery('select[id=programId] option:selected').attr('programInstanceId'),
+			executionDate: getFieldValue('executionDate')
+		}, 
+		function( json ) 
+		{    
+			if(json.response=='success')
+			{
 				disable('createEventBtn');
 				enable('deleteCurrentEventBtn');
+				setFieldValue('programStageInstanceId', json.message );
+				
+				loadEventRegistrationForm();
+			}
+			else
+			{
+				showWarmingMessage( json.message );
 			}
 			
-		} );
-}
-
-function createNewEvent()
-{
-	saveExecutionDate( getFieldValue('programStageId'), getFieldValue('executionDate') );
-	loadEventRegistrationForm();
-	
-	disable('createEventBtn');
-	enable('deleteCurrentEventBtn');
-	
-	$('#executionDate').change(function() {
-			saveExecutionDate( getFieldValue('programStageId'), getFieldValue('executionDate') );
-	});
+		});
 }
 
 function deleteCurrentEvent()
 {	
 	jQuery.postJSON( "removeCurrentEncounter.action",
 		{
-			programInstanceId: getFieldValue('programInstanceId')
+			programStageInstanceId: getFieldValue('programStageInstanceId')
 		}, 
 		function( json ) 
 		{    
@@ -103,16 +145,27 @@
 			
 			if( type == 'success' )
 			{
+				hideById('dataEntryFormDiv');
+				
 				showSuccessMessage( i18n_delete_current_event_success );
-				hideById('dataEntryFormDiv');
 				setFieldValue('executionDate','');
-				$('#executionDate').unbind('change');
+				
 				disable('deleteCurrentEventBtn');
 				enable('createEventBtn');
+				
+				enable('executionDate');
+				
+				$('#executionDate').unbind('change');
 			}
 			else if( type == 'input' )
 			{
 				showWarningMessage( json.message );
 			}
 		});
+}
+
+function afterCompleteStage()
+{
+	enable('createEventBtn');
+	disable('createEventBtn');
 }
\ No newline at end of file

=== added file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonProgramInstances.vm'
--- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonProgramInstances.vm	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/jsonProgramInstances.vm	2011-12-16 06:45:30 +0000
@@ -0,0 +1,11 @@
+#set( $size = $programs.size() )
+{ 
+	"programInstances": [
+		#foreach( $programInstance in $programInstances )
+		  {
+			"id":  ${programInstance.program.id} ,
+			"name": "$!encoder.jsonEncode( ${programInstance.program.name} )",
+			"programInstanceId":  ${programInstance.id}
+		  }#if( $velocityCount < $size ),#end
+		#end]
+}
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/AddProgramAction.java'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/AddProgramAction.java	2011-12-15 02:50:32 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/java/org/hisp/dhis/patient/action/program/AddProgramAction.java	2011-12-16 06:45:30 +0000
@@ -27,7 +27,11 @@
 
 package org.hisp.dhis.patient.action.program;
 
+import java.util.Date;
+
 import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramInstance;
+import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramService;
 import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.program.ProgramStageService;
@@ -61,6 +65,13 @@
         this.programStageService = programStageService;
     }
 
+    private ProgramInstanceService programInstanceService;
+
+    public void setProgramInstanceService( ProgramInstanceService programInstanceService )
+    {
+        this.programInstanceService = programInstanceService;
+    }
+
     // -------------------------------------------------------------------------
     // Input/Output
     // -------------------------------------------------------------------------
@@ -134,7 +145,7 @@
         program.setMaxDaysAllowedInputData( maxDaysAllowedInputData );
         program.setSingleEvent( singleEvent );
         program.setAnonymous( anonymous );
-        
+
         programService.saveProgram( program );
 
         if ( singleEvent )
@@ -150,6 +161,22 @@
             programStageService.saveProgramStage( programStage );
         }
 
+        // ---------------------------------------------------------------------
+        // create program-instance for anonymous program
+        // ---------------------------------------------------------------------
+
+        if ( program.getAnonymous() )
+        {
+            // Add a new program-instance
+            ProgramInstance programInstance = new ProgramInstance();
+            programInstance.setEnrollmentDate( new Date() );
+            programInstance.setDateOfIncident( new Date() );
+            programInstance.setProgram( program );
+            programInstance.setCompleted( false );
+
+            programInstanceService.addProgramInstance( programInstance );
+        }
+
         return SUCCESS;
     }
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/META-INF/dhis/beans.xml	2011-12-07 02:20:11 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-patient/src/main/resources/META-INF/dhis/beans.xml	2011-12-16 06:45:30 +0000
@@ -172,6 +172,7 @@
 		class="org.hisp.dhis.patient.action.program.AddProgramAction" scope="prototype">
 		<property name="programService" ref="org.hisp.dhis.program.ProgramService" />
 		<property name="programStageService" ref="org.hisp.dhis.program.ProgramStageService" />
+		<property name="programInstanceService" ref="org.hisp.dhis.program.ProgramInstanceService" />
 	</bean>
 
 	<bean id="org.hisp.dhis.patient.action.program.GetProgramListAction"