Example usage for org.springframework.beans.propertyeditors CustomDateEditor setAsText

List of usage examples for org.springframework.beans.propertyeditors CustomDateEditor setAsText

Introduction

In this page you can find the example usage for org.springframework.beans.propertyeditors CustomDateEditor setAsText.

Prototype

@Override
public void setAsText(@Nullable String text) throws IllegalArgumentException 

Source Link

Document

Parse the Date from the given text, using the specified DateFormat.

Usage

From source file:org.openmrs.module.programlocation.web.controller.PatientProgramFormController.java

public ModelAndView complete(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String returnPage = request.getParameter("returnPage");
    if (returnPage == null) {
        throw new IllegalArgumentException("must specify a returnPage parameter in a call to enroll()");
    }//from w w w.  j a  v a 2 s.  c om

    String patientProgramIdStr = request.getParameter("patientProgramId");
    String dateCompletedStr = request.getParameter("dateCompleted");

    // make sure we parse dates the same was as if we were using the initBinder + property editor method 
    CustomDateEditor cde = new CustomDateEditor(Context.getDateFormat(), true, 10);
    cde.setAsText(dateCompletedStr);
    Date dateCompleted = (Date) cde.getValue();

    org.openmrs.PatientProgram p = Context.getProgramWorkflowService()
            .getPatientProgram(Integer.valueOf(patientProgramIdStr));
    p.setDateCompleted(dateCompleted);
    Context.getProgramWorkflowService().savePatientProgram(p);

    return new ModelAndView(new RedirectView(returnPage));
}

From source file:org.openmrs.module.programlocation.web.controller.PatientProgramFormController.java

public ModelAndView enroll(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String returnPage = request.getParameter("returnPage");
    if (returnPage == null) {
        throw new IllegalArgumentException("must specify a returnPage parameter in a call to enroll()");
    }//from  w w  w  .j  av  a2 s  . co  m

    String patientIdStr = request.getParameter("patientId");
    String programIdStr = request.getParameter("programId");
    String enrollmentDateStr = request.getParameter("dateEnrolled");
    String locationIdStr = request.getParameter("locationId");
    String completionDateStr = request.getParameter("dateCompleted");

    log.debug("enroll " + patientIdStr + " in " + programIdStr + " on " + enrollmentDateStr);

    ProgramWorkflowService pws = Context.getService(ProgramWorkflowService.class);

    // make sure we parse dates the same was as if we were using the initBinder + property editor method 
    CustomDateEditor cde = new CustomDateEditor(Context.getDateFormat(), true, 10);
    cde.setAsText(enrollmentDateStr);
    Date enrollmentDate = (Date) cde.getValue();
    cde.setAsText(completionDateStr);
    Date completionDate = (Date) cde.getValue();
    Patient patient = Context.getPatientService().getPatient(Integer.valueOf(patientIdStr));

    Location location;
    try {
        location = Context.getLocationService().getLocation(Integer.valueOf(locationIdStr));
    } catch (Exception e) {
        location = null;
    }

    Program program = pws.getProgram(Integer.valueOf(programIdStr));

    List<org.openmrs.PatientProgram> pps = pws.getPatientPrograms(patient, program, null, completionDate,
            enrollmentDate, null, false);

    if (!pps.isEmpty())
        request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Program.error.already");
    else {
        PatientProgram pp = new PatientProgram();
        pp.setPatient(patient);
        pp.setLocation(location);
        pp.setProgram(program);
        pp.setDateEnrolled(enrollmentDate);
        pp.setDateCompleted(completionDate);
        Context.getProgramWorkflowService().savePatientProgram(pp);
    }
    return new ModelAndView(new RedirectView(returnPage));
}

From source file:org.openmrs.web.controller.program.PatientProgramFormController.java

public ModelAndView complete(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String returnPage = request.getParameter("returnPage");
    if (returnPage == null) {
        throw new IllegalArgumentException("must specify a returnPage parameter in a call to enroll()");
    }// ww w. java 2  s  .  c  o m

    String patientProgramIdStr = request.getParameter("patientProgramId");
    String dateCompletedStr = request.getParameter("dateCompleted");

    // make sure we parse dates the same was as if we were using the initBinder + property editor method 
    CustomDateEditor cde = new CustomDateEditor(Context.getDateFormat(), true, 10);
    cde.setAsText(dateCompletedStr);
    Date dateCompleted = (Date) cde.getValue();

    PatientProgram p = Context.getProgramWorkflowService()
            .getPatientProgram(Integer.valueOf(patientProgramIdStr));
    p.setDateCompleted(dateCompleted);
    try {
        String message = validateWithErrorCodes(p);
        if (message != null) {
            request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR, message);
        } else {
            Context.getProgramWorkflowService().savePatientProgram(p);
        }
    } catch (APIException e) {
        request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR, e.getMessage());
    }

    return new ModelAndView(new RedirectView(returnPage));
}

From source file:org.openmrs.web.controller.program.PatientProgramFormController.java

public ModelAndView enroll(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String returnPage = request.getParameter("returnPage");
    if (returnPage == null) {
        throw new IllegalArgumentException("must specify a returnPage parameter in a call to enroll()");
    }/*from  w ww.  jav  a 2 s  .c  om*/

    String patientIdStr = request.getParameter("patientId");
    String programIdStr = request.getParameter("programId");
    String enrollmentDateStr = request.getParameter("dateEnrolled");
    String locationIdStr = request.getParameter("locationId");
    String completionDateStr = request.getParameter("dateCompleted");

    log.debug("enroll " + patientIdStr + " in " + programIdStr + " on " + enrollmentDateStr);

    ProgramWorkflowService pws = Context.getProgramWorkflowService();

    // make sure we parse dates the same was as if we were using the initBinder + property editor method 
    CustomDateEditor cde = new CustomDateEditor(Context.getDateFormat(), true, 10);
    cde.setAsText(enrollmentDateStr);
    Date enrollmentDate = (Date) cde.getValue();
    cde.setAsText(completionDateStr);
    Date completionDate = (Date) cde.getValue();
    Patient patient = Context.getPatientService().getPatient(Integer.valueOf(patientIdStr));

    Location location;
    try {
        location = Context.getLocationService().getLocation(Integer.valueOf(locationIdStr));
    } catch (Exception e) {
        location = null;
    }

    Program program;
    try {
        program = pws.getProgram(Integer.valueOf(programIdStr));
    } catch (NumberFormatException e) {
        request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Program.error.programRequired");
        return new ModelAndView(new RedirectView(returnPage));
    }
    if (enrollmentDate == null) {
        request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                "Program.error.enrollmentDateRequired");
    } else if (!pws.getPatientPrograms(patient, program, null, completionDate, enrollmentDate, null, false)
            .isEmpty()) {
        request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Program.error.already");
    } else {
        PatientProgram pp = new PatientProgram();
        pp.setPatient(patient);
        pp.setLocation(location);
        pp.setProgram(program);
        pp.setDateEnrolled(enrollmentDate);
        pp.setDateCompleted(completionDate);

        // Set any initial states if passed in
        for (ProgramWorkflow workflow : program.getAllWorkflows()) {
            String stateIdStr = request.getParameter("initialState." + workflow.getProgramWorkflowId());
            if (StringUtils.hasText(stateIdStr)) {
                Integer stateId = Integer.valueOf(stateIdStr);
                ProgramWorkflowState state = workflow.getState(stateId);
                log.debug("Transitioning to state: " + state);
                pp.transitionToState(state, enrollmentDate);
            }
        }
        try {
            String message = validateWithErrorCodes(pp);
            if (message != null) {
                request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR, message);
            } else {
                Context.getProgramWorkflowService().savePatientProgram(pp);
            }
        } catch (APIException e) {
            request.getSession().setAttribute(WebConstants.OPENMRS_ERROR_ATTR, e.getMessage());
        }
    }
    return new ModelAndView(new RedirectView(returnPage));
}