View Javadoc
1   /*
2    * Copyright 2010 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl1.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.document.web.renderers;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.JspWriter;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.Tag;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.struts.taglib.html.HiddenTag;
27  
28  /**
29   * Render which displays a field that can't be input directly but could be changed when other fields change.
30   * An example of such is the chartOfAccountsCode in accounting lines, when accounts can't cross charts and
31   * chart code is set automatically by account number.   
32   */
33  public class DynamicReadOnlyRender extends ReadOnlyRenderer {
34      // A hidden input field to store a copy of the field value so that the field will appear read-only to users
35      // but could be set by the system and read into the data object.
36      private HiddenTag shadowInputTag = new HiddenTag();
37      
38      /**
39       * @see org.kuali.ole.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
40       */
41      public void render(PageContext pageContext, Tag parentTag) throws JspException {
42          JspWriter out = pageContext.getOut();
43  
44          try {
45              String value = discoverRenderValue();            
46              out.write(buildBeginSpan());
47              
48              if (!StringUtils.isEmpty(value)) {
49                  if (shouldRenderInquiryLink()) {
50                      out.write(buildBeginInquiryLink());
51                  }                
52                  out.write(value);                
53                  if (shouldRenderInquiryLink()) {
54                      out.write(buildEndInquiryLink());
55                  }                                
56              } else {
57                  out.write(buildNonBreakingSpace());
58              }
59              
60              out.write(buildEndSpan());
61              renderShadowInputTag(pageContext, parentTag);
62          }
63          catch (IOException ioe) {
64              throw new JspException("Difficulty rendering read only field", ioe);
65          }
66      }
67  
68      /**
69       * Generates the HTML for the opening span tag to wrap the displayed value
70       * @param propertyPrefix the property path from the form the business object being rendered
71       * @return the HTML for the opening span 
72       */
73      protected String buildBeginSpan() {
74          StringBuilder beginSpan = new StringBuilder();        
75          beginSpan.append("<span id=\"");
76          beginSpan.append(getFieldName());
77          beginSpan.append(".div\">");        
78          return beginSpan.toString();
79      }    
80  
81      /**
82       * Renders the value of the field in the hidden input tag so it can be read into the data object.
83       * @param pageContext the page context to render to
84       * @param parentTag the tag requesting all this rendering
85       */
86      protected void renderShadowInputTag(PageContext pageContext, Tag parentTag) throws JspException {
87          shadowInputTag.setPageContext(pageContext);
88          shadowInputTag.setParent(parentTag);
89          shadowInputTag.setProperty(getFieldName());
90          shadowInputTag.setValue(getField().getPropertyValue());        
91          shadowInputTag.doStartTag();
92          shadowInputTag.doEndTag();
93      }
94      
95  }