Example usage for org.springframework.web.context WebApplicationContext getMessage

List of usage examples for org.springframework.web.context WebApplicationContext getMessage

Introduction

In this page you can find the example usage for org.springframework.web.context WebApplicationContext getMessage.

Prototype

String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;

Source Link

Document

Try to resolve the message using all the attributes contained within the MessageSourceResolvable argument that was passed in.

Usage

From source file:org.sakaiproject.metaobj.shared.control.XsltArtifactView.java

protected Source createXsltSource(Map map, String string, HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) throws Exception {

    httpServletResponse.setContentType(getContentType());
    WebApplicationContext context = getWebApplicationContext();
    setUriResolver((URIResolver) context.getBean(uriResolverBeanName));

    ToolSession toolSession = SessionManager.getCurrentToolSession();

    String homeType = null;//  w  w  w . j a v a2  s.  c om

    ElementBean bean = (ElementBean) map.get("bean");

    Element root = null;
    Map paramsMap = new Hashtable();

    for (Enumeration e = httpServletRequest.getParameterNames(); e.hasMoreElements();) {
        String k = e.nextElement().toString();
        // Do not allow reserved parameter names to be overwritten
        if (!reservedParams.contains(k)) {
            paramsMap.put(k, httpServletRequest.getParameter(k));
        }
    }

    httpServletRequest.setAttribute(STYLESHEET_PARAMS, paramsMap);
    if (toolSession.getAttribute(FormHelper.PREVIEW_HOME_TAG) != null) {
        paramsMap.put("preview", "true");
    }

    if (toolSession.getAttribute(ResourceToolAction.ACTION_PIPE) != null) {
        paramsMap.put("fromResources", "true");
    }

    if (httpServletRequest.getAttribute(FormHelper.URL_DECORATION) != null) {
        paramsMap.put("urlDecoration", httpServletRequest.getAttribute(FormHelper.URL_DECORATION));
    }

    HashMap<String, String> xslParams = new HashMap<String, String>();
    xslParams.put(FormHelper.XSL_PRESENTATION_ID, FormHelper.PRESENTATION_ID);
    xslParams.put(FormHelper.XSL_PRESENTATION_TYPE, FormHelper.PRESENTATION_TEMPLATE_ID);
    xslParams.put(FormHelper.XSL_PRESENTATION_ITEM_ID, FormHelper.PRESENTATION_ITEM_DEF_ID);
    xslParams.put(FormHelper.XSL_PRESENTATION_ITEM_NAME, FormHelper.PRESENTATION_ITEM_DEF_NAME);
    xslParams.put(FormHelper.XSL_FORM_TYPE, ResourceEditingHelper.CREATE_SUB_TYPE);
    xslParams.put(FormHelper.XSL_ARTIFACT_REFERENCE, ResourceEditingHelper.ATTACHMENT_ID);
    xslParams.put(FormHelper.XSL_OBJECT_ID, FormHelper.XSL_OBJECT_ID);
    xslParams.put(FormHelper.XSL_OBJECT_TITLE, FormHelper.XSL_OBJECT_TITLE);
    xslParams.put(FormHelper.XSL_WIZARD_PAGE_ID, FormHelper.XSL_WIZARD_PAGE_ID);

    // Load up our XSL parameters according to the mapping into the tool session above.
    // Note that this is not always one-to-one due to some string/key inconsistencies around the tools.
    for (Entry<String, String> item : xslParams.entrySet()) {
        Object val = toolSession.getAttribute(item.getValue());
        if (val != null) {
            paramsMap.put(item.getKey(), val);
        }
    }

    Id id = null;

    if (bean instanceof Artifact) {
        root = getStructuredArtifactDefinitionManager().createFormViewXml((Artifact) bean, null);
        homeType = getHomeType((Artifact) bean);
        id = ((Artifact) bean).getId();
    } else {
        EditedArtifactStorage sessionBean = (EditedArtifactStorage) httpServletRequest.getSession()
                .getAttribute(EditedArtifactStorage.EDITED_ARTIFACT_STORAGE_SESSION_KEY);

        if (sessionBean != null) {
            root = getStructuredArtifactDefinitionManager()
                    .createFormViewXml((Artifact) sessionBean.getRootArtifact(), null);

            replaceNodes(root, bean, sessionBean);
            paramsMap.put("subForm", "true");
            homeType = getHomeType(sessionBean.getRootArtifact());
            id = sessionBean.getRootArtifact().getId();
        } else {
            return new javax.xml.transform.dom.DOMSource();
        }
    }

    if (id != null) {
        paramsMap.put("edit", "true");
        paramsMap.put(FormHelper.XSL_ARTIFACT_ID, id.getValue());
    }

    httpServletRequest.setAttribute(STYLESHEET_LOCATION,
            getStructuredArtifactDefinitionManager().getTransformer(homeType, readOnly));

    Errors errors = BindingResultUtils.getBindingResult(map, "bean");
    if (errors != null && errors.hasErrors()) {
        Element errorsElement = new Element("errors");

        List errorsList = errors.getAllErrors();

        for (Iterator i = errorsList.iterator(); i.hasNext();) {
            Element errorElement = new Element("error");
            ObjectError error = (ObjectError) i.next();
            if (error instanceof FieldError) {
                FieldError fieldError = (FieldError) error;
                errorElement.setAttribute("field", fieldError.getField());
                Element rejectedValue = new Element("rejectedValue");
                if (fieldError.getRejectedValue() != null) {
                    rejectedValue.addContent(fieldError.getRejectedValue().toString());
                }
                errorElement.addContent(rejectedValue);
            }
            Element message = new Element("message");
            message.addContent(context.getMessage(error, getResourceLoader().getLocale()));
            errorElement.addContent(message);
            errorsElement.addContent(errorElement);
        }

        root.addContent(errorsElement);
    }

    if (httpServletRequest.getParameter("success") != null) {
        Element success = new Element("success");
        success.setAttribute("messageKey", httpServletRequest.getParameter("success"));
        root.addContent(success);
    }

    if (toolSession.getAttribute(ResourceEditingHelper.CUSTOM_CSS) != null) {
        Element uri = new Element("uri");
        uri.setText((String) toolSession.getAttribute(ResourceEditingHelper.CUSTOM_CSS));
        root.getChild("css").addContent(uri);
        uri.setAttribute("order", "100");
    }

    if (toolSession.getAttribute(FormHelper.FORM_STYLES) != null) {
        List styles = (List) toolSession.getAttribute(FormHelper.FORM_STYLES);
        int index = 101;
        for (Iterator<String> i = styles.iterator(); i.hasNext();) {
            Element uri = new Element("uri");
            uri.setText(i.next());
            root.getChild("css").addContent(uri);
            uri.setAttribute("order", "" + index);
            index++;
        }
    }

    Document doc = new Document(root);
    return new JDOMSource(doc);
}