Package org.openmrs

Examples of org.openmrs.PatientState


    String stateUuid = (String) widget.getValue(session.getContext(), submission);
    if (!StringUtils.isBlank(stateUuid)) {
      if (Mode.EDIT.equals(session.getContext().getMode())) {
       
        ProgramWorkflowState newState = Context.getProgramWorkflowService().getStateByUuid(stateUuid);
        PatientState oldPatientState = getActivePatientState(session.getContext().getExistingPatient(), session
                .getContext().getPreviousEncounterDate(), workflow);
       
        // if no old state, simply transition to this new state
        if (oldPatientState == null) {

                    // if there is an active program enrollment in this program, add it to the programs to update (so that
                    // it is picked up by the FormSubmissionAction.transitionToState method and a new program is not created)
                    PatientProgram patientProgram = HtmlFormEntryUtil.getPatientProgramByProgramOnDate(session.getPatient(),
                            newState.getProgramWorkflow().getProgram(), session.getEncounter().getEncounterDatetime());

                    if (patientProgram != null) {
                        session.getSubmissionActions().getPatientProgramsToUpdate().add(patientProgram);
                    }

          session.getSubmissionActions().transitionToState(newState);
        }
        else {
          PatientProgram patientProgram = oldPatientState.getPatientProgram();
          Date previousEncounterDate = session.getContext().getPreviousEncounterDate();
          Date newEncounterDate = session.getEncounter().getEncounterDatetime();
         
          // if the encounter date has been moved earlier
          if (previousEncounterDate != null  && newEncounterDate.before(previousEncounterDate)) {
           
            // if there is an existing patient state on the new encounter date and it differs from the state on the old encounter date
            // we need to end it
            PatientState existingPatientStateOnNewEncounterDate = getActivePatientState(session.getContext().getExistingPatient(), newEncounterDate, workflow);
            if (existingPatientStateOnNewEncounterDate != null &&  !existingPatientStateOnNewEncounterDate.equals(oldPatientState)) {
              existingPatientStateOnNewEncounterDate.setEndDate(newEncounterDate);
            }
           
            // we also need to void any other states for this workflow that may have started between the new and old encounter dates
            for (PatientState state : patientProgram.statesInWorkflow(workflow, false)) {
              if (!state.equals(oldPatientState) && (state.getStartDate().after(newEncounterDate) || state.getStartDate().compareTo(newEncounterDate) == 0)
View Full Code Here


          return;
        }
      }
    }
   
    PatientState previousState = null;
    PatientState nextState = null;
    PatientState newState = new PatientState();
    newState.setPatientProgram(patientProgram);
    newState.setState(state);
    newState.setStartDate(encounter.getEncounterDatetime());
    // HACK: we need to set the date created, creator, and uuid here as a hack around a hibernate flushing issue
    // (should be able to remove this once we move to Hibernate Interceptors instead of Spring AOP to set these parameters)
    newState.setDateCreated(new Date());
    newState.setCreator(Context.getAuthenticatedUser());
    newState.setUuid(UUID.randomUUID().toString());
   
    Collection<PatientState> sortedStates = new TreeSet<PatientState>(new Comparator<PatientState>() {
     
      @Override
      public int compare(PatientState o1, PatientState o2) {
        int result = OpenmrsUtil.compareWithNullAsEarliest(o1.getStartDate(), o2.getStartDate());
        if (result == 0) {
          result = OpenmrsUtil.compareWithNullAsLatest(o1.getEndDate(), o2.getEndDate());
        }
        return result;
      }
     
    });
    sortedStates.addAll(patientProgram.statesInWorkflow(state.getProgramWorkflow(), false));
    for (PatientState currentState : sortedStates) {
     
      Date newStartDate = newState.getStartDate();
      Date currentStartDate = currentState.getStartDate();
      Date currentEndDate = currentState.getEndDate();
     
      if (currentEndDate != null) {
        if (currentEndDate.after(newStartDate)) {
          if (currentStartDate.after(newStartDate)) {
            nextState = currentState;
            break;
          } else {
            previousState = currentState;
          }
        } else {
          previousState = currentState;
        }
      } else if (currentStartDate.after(newStartDate)) {
        nextState = currentState;
        break;
      } else {
        previousState = currentState;
        nextState = null;
        break;
      }
    }
   
    if (nextState == null) {
      if (previousState != null) {
        previousState.setEndDate(newState.getStartDate());
      }
    } else {
      if (previousState != null) {
        previousState.setEndDate(newState.getStartDate());
      }
      newState.setEndDate(nextState.getStartDate());
    }
   
    patientProgram.getStates().add(newState);
   
    patientProgramsToUpdate.add(patientProgram);
View Full Code Here

        results.assertLocation(2);
       
        //Then: Workflow state Y is created with a start date of the encounter date
        ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(START_STATE);
        PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), state, DATE);
        PatientState patientState = getPatientState(patientProgram, state, DATE);
       
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getStartDate()));
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientProgram.getDateEnrolled()));
      }
    }.run();
  }
View Full Code Here

        results.assertLocation(2);
       
        //Then: Workflow state Y is created with a start date of June 2011 and a stop date of Jan 2012. Workflow state X stays as is.
        ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(MIDDLE_STATE);
        PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), state, PAST_DATE);
        PatientState patientState = getPatientState(patientProgram, state, PAST_DATE);
       
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(PAST_DATE), dateAsString(patientState.getStartDate()));
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getEndDate()));
       
        state = Context.getProgramWorkflowService().getStateByUuid(END_STATE);
        patientState = getPatientState(patientProgram, state, DATE);
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getStartDate()));
        Assert.assertNull(patientState.getEndDate());
      }
     
      public boolean doViewEncounter() {
        return true;
      }
View Full Code Here

        results.assertLocation(2);
       
        //Then: Workflow state X is stopped with a stop date of Jan 2012, Workflow state Y is created with a start date of Jan 2012 and is still current
        ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(START_STATE);
        PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), state, PAST_DATE);
        PatientState patientState = getPatientState(patientProgram, state, PAST_DATE);
       
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getEndDate()));
       
        state = Context.getProgramWorkflowService().getStateByUuid(MIDDLE_STATE);
        patientState = getPatientState(patientProgram, state, DATE);
       
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getStartDate()));
        Assert.assertNull(patientState.getEndDate());
      }
    }.run();
  }
View Full Code Here

        results.assertLocation(2);
       
        //Then: No change to workflow state
        ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(START_STATE);
        PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), state, PAST_DATE);
        PatientState patientState = getPatientState(patientProgram, state, PAST_DATE);
       
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(PAST_DATE), dateAsString(patientState.getStartDate()));
      }
    }.run();
  }
View Full Code Here

        results.assertLocation(2);
       
        //Then: A new workflow state X is created with a Start date of June 2011 and a stop date of Jan 2012
        ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(START_STATE);
        PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), state, PAST_DATE);
        PatientState patientState = getPatientState(patientProgram, state, PAST_DATE);
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(PAST_DATE), dateAsString(patientState.getStartDate()));
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getEndDate()));
       
        patientProgram = getPatientProgramByState(results.getPatient(), state, DATE);
        patientState = getPatientState(patientProgram, state, DATE);
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getStartDate()));
        Assert.assertNull(patientState.getEndDate());
      }
    }.run();
  }
View Full Code Here

        results.assertProvider(502);
        results.assertLocation(2);
       
        ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(START_STATE);
        PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), state, null);
        PatientState patientState = getPatientState(patientProgram, state, null);
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getStartDate()));
        Assert.assertNull(patientState.getEndDate());
      }
     
      public boolean doViewEncounter() {
        return true;
      }
     
      public void testViewingEncounter(Encounter encounter, String html) {
        Assert.assertTrue("View should contain current state: " + html, html.contains("START STATE"));
      }
     
      public boolean doEditEncounter() {
        return true;
      }
     
      public void testEditFormHtml(String html) {
        Assert.assertTrue("Edit should contain current state: " + html,
            html.contains("selected=\"true\">START STATE"));
      }
    }.run();
   
   
    new RegressionTestHelper() {
     
      @Override
      public String getFormName() {
        return XML_FORM_NAME;
      }
     
      @Override
      public String[] widgetLabels() {
        return new String[] { "Date:", "Location:", "Provider:", "State:" };
      }
     
      @Override
      public void setupRequest(MockHttpServletRequest request, Map<String, String> widgets) {
        request.addParameter(widgets.get("Location:"), "2");
        request.addParameter(widgets.get("Provider:"), "502");
        //When: Html form is entered with an encounter date of Jan 2012 in which workflow state Y is selected
        request.addParameter(widgets.get("Date:"), dateAsString(DATE));
        request.addParameter(widgets.get("State:"), MIDDLE_STATE);
      }
     
      @Override
      public void testResults(SubmissionResults results) {
        results.assertNoErrors();
        results.assertEncounterCreated();
        results.assertProvider(502);
        results.assertLocation(2);
       
        //Then: Workflow state X is stopped with a stop date of Jan 2012, Workflow state Y is created with a start date of Jan 2012 and is still current
        ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(START_STATE);
        PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), state, null);
        PatientState patientState = getPatientState(patientProgram, state, null);
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getStartDate()));
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getEndDate()));
       
        state = Context.getProgramWorkflowService().getStateByUuid(MIDDLE_STATE);
        patientProgram = getPatientProgramByState(results.getPatient(), state, null);
        patientState = getPatientState(patientProgram, state, null);
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getStartDate()));
        Assert.assertNull(patientState.getEndDate());
      }
     
      public boolean doViewEncounter() {
        return true;
      }
View Full Code Here

        results.assertLocation(2);
       
        //Then: Workflow X is changed to a stop date of Sept 2011, Workflow Z is created with a start date of Sept 2011 and a stop date of Jan 2012
        ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(START_STATE);
        PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), state, PAST_DATE);
        PatientState patientState = getPatientState(patientProgram, state, PAST_DATE);
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(PAST_DATE), dateAsString(patientState.getStartDate()));
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getEndDate()));
       
        state = Context.getProgramWorkflowService().getStateByUuid(MIDDLE_STATE);
        patientProgram = getPatientProgramByState(results.getPatient(), state, DATE);
        patientState = getPatientState(patientProgram, state, DATE);
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(DATE), dateAsString(patientState.getStartDate()));
        Assert.assertEquals(dateAsString(FUTURE_DATE), dateAsString(patientState.getEndDate()));
       
        state = Context.getProgramWorkflowService().getStateByUuid(END_STATE);
        patientProgram = getPatientProgramByState(results.getPatient(), state, FUTURE_DATE);
        patientState = getPatientState(patientProgram, state, FUTURE_DATE);
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(FUTURE_DATE), dateAsString(patientState.getStartDate()));
        Assert.assertNull(patientState.getEndDate());
      }
     
      public boolean doViewEncounter() {
        return true;
      }
View Full Code Here

        results.assertLocation(2);
       
        //Then: No action
        ProgramWorkflowState state = Context.getProgramWorkflowService().getStateByUuid(START_STATE);
        PatientProgram patientProgram = getPatientProgramByState(results.getPatient(), state, PAST_DATE);
        PatientState patientState = getPatientState(patientProgram, state, PAST_DATE);
        Assert.assertNotNull(patientProgram);
        Assert.assertEquals(dateAsString(PAST_DATE), dateAsString(patientState.getStartDate()));
        Assert.assertNull(patientState.getEndDate());
      }
     
      public boolean doViewEncounter() {
        return true;
      }
View Full Code Here

TOP

Related Classes of org.openmrs.PatientState

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.