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     * Render which displays a field that can't be input directly but could be changed when other fields change.
031     * An example of such is the chartOfAccountsCode in accounting lines, when accounts can't cross charts and
032     * chart code is set automatically by account number.   
033     */
034    public class DynamicReadOnlyRender extends ReadOnlyRenderer {
035        // A hidden input field to store a copy of the field value so that the field will appear read-only to users
036        // but could be set by the system and read into the data object.
037        private HiddenTag shadowInputTag = new HiddenTag();
038        
039        /**
040         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
041         */
042        public void render(PageContext pageContext, Tag parentTag) throws JspException {
043            JspWriter out = pageContext.getOut();
044    
045            try {
046                String value = discoverRenderValue();            
047                out.write(buildBeginSpan());
048                
049                if (!StringUtils.isEmpty(value)) {
050                    if (shouldRenderInquiryLink()) {
051                        out.write(buildBeginInquiryLink());
052                    }                
053                    out.write(value);                
054                    if (shouldRenderInquiryLink()) {
055                        out.write(buildEndInquiryLink());
056                    }                                
057                } else {
058                    out.write(buildNonBreakingSpace());
059                }
060                
061                out.write(buildEndSpan());
062                renderShadowInputTag(pageContext, parentTag);
063            }
064            catch (IOException ioe) {
065                throw new JspException("Difficulty rendering read only field", ioe);
066            }
067        }
068    
069        /**
070         * Generates the HTML for the opening span tag to wrap the displayed value
071         * @param propertyPrefix the property path from the form the business object being rendered
072         * @return the HTML for the opening span 
073         */
074        protected String buildBeginSpan() {
075            StringBuilder beginSpan = new StringBuilder();        
076            beginSpan.append("<span id=\"");
077            beginSpan.append(getFieldName());
078            beginSpan.append(".div\">");        
079            return beginSpan.toString();
080        }    
081    
082        /**
083         * Renders the value of the field in the hidden input tag so it can be read into the data object.
084         * @param pageContext the page context to render to
085         * @param parentTag the tag requesting all this rendering
086         */
087        protected void renderShadowInputTag(PageContext pageContext, Tag parentTag) throws JspException {
088            shadowInputTag.setPageContext(pageContext);
089            shadowInputTag.setParent(parentTag);
090            shadowInputTag.setProperty(getFieldName());
091            shadowInputTag.setValue(getField().getPropertyValue());        
092            shadowInputTag.doStartTag();
093            shadowInputTag.doEndTag();
094        }
095        
096    }