001: /*
002: * (C) Copyright 2006 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.taglib;
022:
023: import java.io.IOException;
024:
025: import javax.portlet.RenderRequest;
026: import javax.servlet.jsp.JspException;
027: import javax.servlet.jsp.JspWriter;
028: import javax.servlet.jsp.tagext.TagSupport;
029:
030: import com.nabhinc.portlet.mvcportlet.core.Constants;
031: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
032: import com.nabhinc.portlet.mvcportlet.core.FormError;
033:
034: /**
035: * Messages tag. Display alert, status, success, info and error messages, if any.
036: * The style classes used are <code>portlet-msg-alert</code>, <code>portlet-msg-status</code>,
037: * <code>portlet-msg-success</code>, <code>portlet-msg-info</code> and
038: * <code>portlet-msg-error</code> respectively.
039: *
040: * @author Padmanabh Dabke
041: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
042: * @since 1.1
043: */
044: public class MessagesTag extends TagSupport {
045: private String etLineBreak = "<br/>· ";
046:
047: public void release() {
048: super .release();
049: etLineBreak = "<br/>· ";
050: }
051:
052: /**
053: * Returns line break
054: * @return
055: */
056: public String getLineBreak() {
057: return etLineBreak;
058: }
059:
060: /**
061: * Sets line break between errors.
062: * Default is <code><br/>&middot;&nbsp;</code>
063: * @param name
064: */
065: public void setLineBreak(String lineBreak) {
066: etLineBreak = lineBreak;
067: }
068:
069: public int doStartTag() throws JspException {
070:
071: try {
072: RenderRequest renderRequest = (RenderRequest) pageContext
073: .getRequest().getAttribute("javax.portlet.request");
074: JspWriter out = pageContext.getOut();
075: String portletMode = renderRequest.getPortletMode()
076: .toString().toLowerCase();
077: ControllerPortletConfig config = (ControllerPortletConfig) renderRequest
078: .getAttribute(Constants.PORTLET_CONFIG_ATTRIB);
079: printMessage(Constants.ALERT_MESSAGE_PARAM + portletMode,
080: "portlet-msg-alert", renderRequest, out, config);
081: printMessage(Constants.STATUS_MESSAGE_PARAM + portletMode,
082: "portlet-msg-status", renderRequest, out, config);
083: printMessage(Constants.SUCCESS_MESSAGE_PARAM + portletMode,
084: "portlet-msg-success", renderRequest, out, config);
085: printMessage(Constants.INFO_MESSAGE_PARAM + portletMode,
086: "portlet-msg-info", renderRequest, out, config);
087:
088: String errorMessage = renderRequest
089: .getParameter(Constants.ERROR_MESSAGE_PARAM
090: + portletMode);
091: if (errorMessage != null) {
092: out.print("<div class=\"");
093: out.print("portlet-msg-error");
094: out.print("\">");
095: out.print(config.getLocalizedMessage(errorMessage,
096: renderRequest));
097:
098: // Check if there are any form field errors
099: com.nabhinc.portlet.mvcportlet.core.FormErrors formErrors = (com.nabhinc.portlet.mvcportlet.core.FormErrors) renderRequest
100: .getPortletSession()
101: .getAttribute(
102: com.nabhinc.portlet.mvcportlet.core.Constants.FORM_ERRORS_ATTRIB
103: + portletMode);
104: if (formErrors != null) {
105: String formName = formErrors.getFormName();
106: FormError[] formErrorArray = formErrors
107: .getFormErrors();
108: for (int i = 0; i < formErrorArray.length; i++) {
109: String errorKey = "mvcportlet.form_error."
110: + formName + "."
111: + formErrorArray[i].getFieldName()
112: + "." + formErrorArray[i].getError();
113: out.print(etLineBreak);
114: out.print(config.getLocalizedMessage(errorKey,
115: renderRequest));
116: }
117: }
118: out.print("</div>");
119: out.flush();
120: }
121: } catch (Exception ex) {
122: throw new JspException(ex);
123: }
124:
125: return SKIP_BODY;
126: }
127:
128: private void printMessage(String paramName, String style,
129: RenderRequest request, JspWriter out,
130: ControllerPortletConfig config) throws IOException {
131: String msg = request.getParameter(paramName);
132: if (msg != null) {
133: out.print("<div class=\"");
134: out.print(style);
135: out.print("\">");
136: out.print(config.getLocalizedMessage(msg, request));
137: out.print("</div>");
138:
139: }
140: }
141:
142: public int doEndTag() {
143: return EVAL_PAGE;
144: }
145:
146: }
|