Example usage for org.springframework.web.context.request WebRequest setAttribute

List of usage examples for org.springframework.web.context.request WebRequest setAttribute

Introduction

In this page you can find the example usage for org.springframework.web.context.request WebRequest setAttribute.

Prototype

void setAttribute(String name, Object value, int scope);

Source Link

Document

Set the value for the scoped attribute of the given name, replacing an existing value (if any).

Usage

From source file:org.openmrs.module.chartsearch.web.controller.SettingsFormController.java

@RequestMapping(method = RequestMethod.POST)
public String handleSubmission(@ModelAttribute("globalPropertiesModel") PropertiesModel propertiesModel,
        Errors errors, WebRequest request) {
    propertiesModel.validate(propertiesModel, errors);
    if (errors.hasErrors())
        return null; // show the form again

    AdministrationService administrationService = Context.getAdministrationService();
    for (GlobalProperty p : propertiesModel.getProperties()) {
        administrationService.saveGlobalProperty(p);
    }/*from  ww w .  ja va2  s.  c  o  m*/

    request.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
            Context.getMessageSourceService().getMessage("general.saved"), WebRequest.SCOPE_SESSION);
    return "redirect:settings.form";
}

From source file:org.socialsignin.springsocial.security.signin.EnsureUniqueConnectInterceptor.java

@Override
public void postConnect(Connection<S> connection, WebRequest request) {

    boolean connectionAlreadyAssociatedWithAnotherUser = usersConnectionRepository
            .findUserIdsWithConnection(connection).size() > 1;
    if (connectionAlreadyAssociatedWithAnotherUser) {
        connectionRepository.removeConnection(connection.getKey());
        NonUniqueConnectionException nonUniqueConnectionException = new NonUniqueConnectionException(
                "The connection is already associated with a different account");
        request.setAttribute("lastSessionException", nonUniqueConnectionException, WebRequest.SCOPE_SESSION);
        throw nonUniqueConnectionException;
    }/* w w w  .jav  a2  s.  c o  m*/

}

From source file:org.openmrs.web.controller.concept.ConceptMapTypeFormController.java

/**
 * Processes requests to unretire a concept map type
 *
 * @param request the {@link WebRequest} object
 * @param conceptMapType the concept map type object to unretire
 * @return the url to redirect to//ww  w  .jav a2  s .  co m
 */
@RequestMapping(method = RequestMethod.POST, value = "/admin/concepts/unretireConceptMapType")
public String unretireConceptMapType(WebRequest request,
        @ModelAttribute(value = "conceptMapType") ConceptMapType conceptMapType) {

    try {
        Context.getConceptService().unretireConceptMapType(conceptMapType);
        if (log.isDebugEnabled()) {
            log.debug("Unretired concept map type with id: " + conceptMapType.getId());
        }
        request.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                Context.getMessageSourceService().getMessage("ConceptMapType.unretired"),
                WebRequest.SCOPE_SESSION);

        return "redirect:" + CONCEPT_MAP_TYPE_LIST_URL + ".list";
    } catch (APIException e) {
        log.error("Error occurred while attempting to unretire concept map type", e);
        request.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                Context.getMessageSourceService().getMessage("ConceptMapType.unretire.error"),
                WebRequest.SCOPE_SESSION);
    }

    //an error occurred
    return CONCEPT_MAP_TYPE_FORM;
}

From source file:org.openmrs.module.webservices.rest.web.controller.SettingsFormController.java

@RequestMapping(method = RequestMethod.POST)
public String handleSubmission(
        @ModelAttribute("globalPropertiesModel") GlobalPropertiesModel globalPropertiesModel, Errors errors,
        WebRequest request) {
    globalPropertiesModel.validate(globalPropertiesModel, errors);
    if (errors.hasErrors())
        return null; // show the form again

    AdministrationService administrationService = Context.getAdministrationService();
    for (GlobalProperty p : globalPropertiesModel.getProperties()) {
        administrationService.saveGlobalProperty(p);
    }//w w  w.j a  v  a2s .  c o  m

    request.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
            Context.getMessageSourceService().getMessage("general.saved"), RequestAttributes.SCOPE_SESSION);
    return "redirect:settings.form";
}

From source file:org.openmrs.web.controller.concept.ConceptMapTypeFormController.java

/**
 * Processes requests to purge a concept map type
 *
 * @param request the {@link WebRequest} object
 * @param conceptMapType// w w w.  j  a  va2 s .co m
 * @return the url to forward to
 */
@RequestMapping(method = RequestMethod.POST, value = "/admin/concepts/purgeConceptMapType")
public String purgeTerm(WebRequest request,
        @ModelAttribute(value = "conceptMapType") ConceptMapType conceptMapType) {
    Integer id = conceptMapType.getId();
    try {
        Context.getConceptService().purgeConceptMapType(conceptMapType);
        if (log.isDebugEnabled()) {
            log.debug("Purged concept map type with id: " + id);
        }
        request.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                Context.getMessageSourceService().getMessage("ConceptMapType.purged"),
                WebRequest.SCOPE_SESSION);
        return "redirect:" + CONCEPT_MAP_TYPE_LIST_URL + ".list";
    } catch (APIException e) {
        log.warn("Error occurred while attempting to purge concept map type", e);
        request.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                Context.getMessageSourceService().getMessage("ConceptMapType.purge.error"),
                WebRequest.SCOPE_SESSION);
    }

    return "redirect:" + CONCEPT_MAP_TYPE_FORM_URL + ".form?conceptMapTypeId=" + id;
}

From source file:org.openmrs.web.controller.concept.ConceptAttributeTypeFormController.java

private String retireConceptAttributeType(WebRequest request, ConceptAttributeType conceptAttributeType,
        BindingResult errors) {/* w  ww.  j  a va 2s.c  om*/
    String retireReason = request.getParameter("retireReason");
    if (conceptAttributeType.getId() != null && !(StringUtils.hasText(retireReason))) {
        errors.reject("retireReason", "general.retiredReason.empty");
        return null;
    }
    Context.getConceptService().retireConceptAttributeType(conceptAttributeType, retireReason);
    request.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
            Context.getMessageSourceService().getMessage("ConceptAttributeType.retired"),
            WebRequest.SCOPE_SESSION);
    return CONCEPT_ATTRIBUTE_TYPES_LIST_URL;
}

From source file:org.openmrs.web.controller.visit.VisitFormController.java

/**
 * Processes requests to unvoid a visit//from   w w w .ja  v  a 2  s  .  c  om
 *
 * @param request the {@link WebRequest} object
 * @param visit the visit object to unvoid
 * @param status the {@link SessionStatus}
 * @param model the {@link ModelMap} object
 * @return the url to forward to
 */
@RequestMapping(method = RequestMethod.POST, value = "/admin/visits/unvoidVisit")
public String unvoidVisit(WebRequest request, @ModelAttribute(value = "visit") Visit visit,
        SessionStatus status, ModelMap model) {
    try {
        Context.getVisitService().unvoidVisit(visit);
        if (log.isDebugEnabled()) {
            log.debug("Unvoided visit with id: " + visit.getId());
        }
        request.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
                Context.getMessageSourceService().getMessage("Visit.unvoided"), WebRequest.SCOPE_SESSION);
        return "redirect:" + VISIT_FORM_URL + ".form?visitId=" + visit.getVisitId() + "&patientId="
                + visit.getPatient().getPatientId();
    } catch (APIException e) {
        log.warn("Error occurred while attempting to unvoid visit", e);
        request.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
                Context.getMessageSourceService().getMessage("Visit.unvoid.error"), WebRequest.SCOPE_SESSION);
    }

    addEncounterAndObservationCounts(visit, null, model);
    return VISIT_FORM;
}

From source file:org.openmrs.web.controller.concept.ConceptMapTypeFormController.java

/**
 * Processes requests to save/update a concept map type
 *
 * @param request the {@link WebRequest} object
 * @param conceptMapType the concept map type object to save/update
 * @param result the {@link BindingResult} object
 * @return the url to redirect to/*from  ww  w .jav  a2s  . c  om*/
 */
@RequestMapping(method = RequestMethod.POST, value = CONCEPT_MAP_TYPE_FORM_URL)
public String saveConceptMapType(WebRequest request,
        @ModelAttribute("conceptMapType") ConceptMapType conceptMapType, BindingResult result) {

    new ConceptMapTypeValidator().validate(conceptMapType, result);
    if (!result.hasErrors()) {
        try {
            Context.getConceptService().saveConceptMapType(conceptMapType);
            if (log.isDebugEnabled()) {
                log.debug("Saved concept map type: " + conceptMapType.toString());
            }
            request.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "ConceptMapType.saved",
                    WebRequest.SCOPE_SESSION);

            return "redirect:" + CONCEPT_MAP_TYPE_LIST_URL + ".list";
        } catch (APIException e) {
            log.error("Error while saving concept map type(s)", e);
            request.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "ConceptMapType.save.error",
                    WebRequest.SCOPE_SESSION);
        }
    }

    //there was an error
    return CONCEPT_MAP_TYPE_FORM;
}

From source file:org.openmrs.web.controller.patient.ShortPatientFormControllerTest.java

/**
 * @see ShortPatientFormController#saveShortPatient(WebRequest,ShortPatientModel,BindingResult,SessionStatus)
 *///from  w  w w  .j a  va2 s . co  m
@Test
@Verifies(value = "should pass if all the form data is valid", method = "saveShortPatient(WebRequest,ShortPatientModel,BindingResult,SessionStatus)")
public void saveShortPatient_shouldPassIfAllTheFormDataIsValid() throws Exception {
    Patient p = Context.getPatientService().getPatient(2);
    ShortPatientModel patientModel = new ShortPatientModel(p);

    WebRequest mockWebRequest = new ServletWebRequest(new MockHttpServletRequest());
    BindException errors = new BindException(patientModel, "patientModel");
    mockWebRequest.setAttribute("personNameCache", BeanUtils.cloneBean(p.getPersonName()),
            WebRequest.SCOPE_SESSION);
    mockWebRequest.setAttribute("personAddressCache", p.getPersonAddress().clone(), WebRequest.SCOPE_SESSION);
    mockWebRequest.setAttribute("patientModel", patientModel, WebRequest.SCOPE_SESSION);

    ShortPatientFormController controller = (ShortPatientFormController) applicationContext
            .getBean("shortPatientFormController");
    String redirectUrl = controller.saveShortPatient(mockWebRequest,
            (PersonName) mockWebRequest.getAttribute("personNameCache", WebRequest.SCOPE_SESSION),
            (PersonAddress) mockWebRequest.getAttribute("personAddressCache", WebRequest.SCOPE_SESSION), null,
            (ShortPatientModel) mockWebRequest.getAttribute("patientModel", WebRequest.SCOPE_SESSION), errors);

    Assert.assertTrue("Should pass with no validation errors", !errors.hasErrors());
    Assert.assertEquals("Patient.saved",
            mockWebRequest.getAttribute(WebConstants.OPENMRS_MSG_ATTR, WebRequest.SCOPE_SESSION));
    Assert.assertEquals("redirect:/patientDashboard.form?patientId=" + p.getPatientId(), redirectUrl);
}

From source file:org.openmrs.web.controller.patient.ShortPatientFormControllerTest.java

/**
 * @see ShortPatientFormController#saveShortPatient(WebRequest,PersonName,PersonAddress,Map,ShortPatientModel, BindingResult)
 *//*ww  w . ja  va2  s. co  m*/
@Test
@Verifies(value = "should not add a new person attribute with an empty value", method = "saveShortPatient(WebRequest,PersonName,PersonAddress,ShortPatientModel,BindingResult)")
public void saveShortPatient_shouldNotAddANewPersonAttributeWithAnEmptyValue() throws Exception {
    Patient p = Context.getPatientService().getPatient(2);
    int originalAttributeCount = p.getAttributes().size();
    ShortPatientModel patientModel = new ShortPatientModel(p);
    //add a new person Attribute with no value
    patientModel.getPersonAttributes()
            .add(new PersonAttribute(Context.getPersonService().getPersonAttributeType(2), null));

    WebRequest mockWebRequest = new ServletWebRequest(new MockHttpServletRequest());
    BindException errors = new BindException(patientModel, "patientModel");
    mockWebRequest.setAttribute("personNameCache", p.getPersonName(), WebRequest.SCOPE_SESSION);
    mockWebRequest.setAttribute("personAddressCache", p.getPersonAddress(), WebRequest.SCOPE_SESSION);
    mockWebRequest.setAttribute("patientModel", patientModel, WebRequest.SCOPE_SESSION);

    ShortPatientFormController controller = (ShortPatientFormController) applicationContext
            .getBean("shortPatientFormController");
    String redirectUrl = controller.saveShortPatient(mockWebRequest,
            (PersonName) mockWebRequest.getAttribute("personNameCache", WebRequest.SCOPE_SESSION),
            (PersonAddress) mockWebRequest.getAttribute("personAddressCache", WebRequest.SCOPE_SESSION), null,
            (ShortPatientModel) mockWebRequest.getAttribute("patientModel", WebRequest.SCOPE_SESSION), errors);

    Assert.assertTrue("Should pass with no validation errors", !errors.hasErrors());
    Assert.assertEquals("Patient.saved",
            mockWebRequest.getAttribute(WebConstants.OPENMRS_MSG_ATTR, WebRequest.SCOPE_SESSION));
    Assert.assertEquals("redirect:/patientDashboard.form?patientId=" + p.getPatientId(), redirectUrl);
    //The new blank person attribute should have been ignored
    Assert.assertEquals(originalAttributeCount, p.getAttributes().size());
}