001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.document.web.renderers;
017    
018    import java.io.IOException;
019    
020    import javax.servlet.jsp.JspException;
021    import javax.servlet.jsp.JspWriter;
022    import javax.servlet.jsp.PageContext;
023    import javax.servlet.jsp.tagext.Tag;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.apache.struts.taglib.html.HiddenTag;
027    import org.springframework.web.util.HtmlUtils;
028    
029    /**
030     * Renders the dynamic label portion of a field
031     */
032    public class DynamicNameLabelRenderer implements Renderer {
033        private String fieldName = null;
034        private String fieldValue = null;
035        private HiddenTag valuePersistingTag = new HiddenTag();
036        
037        /**
038         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#clear()
039         */
040        public void clear() {
041            fieldName = null;
042            fieldValue = null;
043            
044            valuePersistingTag.setPageContext(null);
045            valuePersistingTag.setParent(null);
046            valuePersistingTag.setProperty(null);
047            valuePersistingTag.setValue(null);
048        }
049    
050        /**
051         * 
052         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag, org.kuali.rice.kns.bo.BusinessObject)
053         */
054        public void render(PageContext pageContext, Tag parentTag) throws JspException {
055            JspWriter out = pageContext.getOut();
056            try {
057                out.write("<br />");
058                out.write("<div id=\""+fieldName+".div\" class=\"fineprint\">");
059                if (!StringUtils.isBlank(fieldValue)) {
060                    out.write(fieldValue);
061                }
062                out.write("</div>");
063                
064                if (!StringUtils.isBlank(fieldValue)) {
065                    renderValuePersistingTag(pageContext, parentTag);
066                }
067            }
068            catch (IOException ioe) {
069                throw new JspException("Difficulty rendering a dynamic field label", ioe);
070            }
071        }
072        
073        /**
074         * If the value is present, renders that value in a tag
075         * @param pageContext the page context to render to
076         * @param parentTag the tag requesting all this rendering
077         */
078        protected void renderValuePersistingTag(PageContext pageContext, Tag parentTag) throws JspException {
079            valuePersistingTag.setPageContext(pageContext);
080            valuePersistingTag.setParent(parentTag);
081            valuePersistingTag.setProperty(fieldName);
082            valuePersistingTag.setValue(HtmlUtils.htmlEscape(fieldValue));
083            
084            valuePersistingTag.doStartTag();
085            valuePersistingTag.doEndTag();
086        }
087    
088        /**
089         * Gets the fieldName attribute. 
090         * @return Returns the fieldName.
091         */
092        public String getFieldName() {
093            return fieldName;
094        }
095    
096        /**
097         * Sets the fieldName attribute value.
098         * @param fieldName The fieldName to set.
099         */
100        public void setFieldName(String fieldName) {
101            this.fieldName = fieldName;
102        }
103    
104        /**
105         * Gets the fieldValue attribute. 
106         * @return Returns the fieldValue.
107         */
108        public String getFieldValue() {
109            return fieldValue;
110        }
111    
112        /**
113         * Sets the fieldValue attribute value.
114         * @param fieldValue The fieldValue to set.
115         */
116        public void setFieldValue(String fieldValue) {
117            this.fieldValue = fieldValue;
118        }
119    }