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.kuali.kfs.sys.KFSConstants;
027    import org.kuali.kfs.sys.context.SpringContext;
028    import org.kuali.rice.kns.service.KualiConfigurationService;
029    import org.kuali.rice.kns.util.KNSConstants;
030    
031    /**
032     * This renders a label (and not, as I was about to write labels a render).  It's main job
033     * is to render header cells on accounting lines.
034     */
035    public class LabelRenderer implements Renderer {
036        private boolean required = false;
037        private boolean readOnly = false;
038        private String label;
039        private String fullClassNameForHelp;
040        private String attributeEntryForHelp;
041        private String labelFor;
042    
043        /**
044         * Gets the attributeEntryForHelp attribute. 
045         * @return Returns the attributeEntryForHelp.
046         */
047        public String getAttributeEntryForHelp() {
048            return attributeEntryForHelp;
049        }
050    
051        /**
052         * Sets the attributeEntryForHelp attribute value.
053         * @param attributeEntryForHelp The attributeEntryForHelp to set.
054         */
055        public void setAttributeEntryForHelp(String attributeEntryForHelp) {
056            this.attributeEntryForHelp = attributeEntryForHelp;
057        }
058    
059        /**
060         * Gets the fullClassNameForHelp attribute. 
061         * @return Returns the fullClassNameForHelp.
062         */
063        public String getFullClassNameForHelp() {
064            return fullClassNameForHelp;
065        }
066    
067        /**
068         * Sets the fullClassNameForHelp attribute value.
069         * @param fullClassNameForHelp The fullClassNameForHelp to set.
070         */
071        public void setFullClassNameForHelp(String fullClassNameForHelp) {
072            this.fullClassNameForHelp = fullClassNameForHelp;
073        }
074    
075        /**
076         * Gets the label attribute. 
077         * @return Returns the label.
078         */
079        public String getLabel() {
080            return label;
081        }
082    
083        /**
084         * Sets the label attribute value.
085         * @param label The label to set.
086         */
087        public void setLabel(String label) {
088            this.label = label;
089        }
090    
091        /**
092         * Gets the required attribute. 
093         * @return Returns the required.
094         */
095        public boolean isRequired() {
096            return required;
097        }
098    
099        /**
100         * Sets the required attribute value.
101         * @param required The required to set.
102         */
103        public void setRequired(boolean required) {
104            this.required = required;
105        }
106    
107        /**
108         * Gets the readOnly attribute. 
109         * @return Returns the readOnly.
110         */
111        public boolean isReadOnly() {
112            return readOnly;
113        }
114    
115        /**
116         * Sets the readOnly attribute value.
117         * @param readOnly The readOnly to set.
118         */
119        public void setReadOnly(boolean readOnly) {
120            this.readOnly = readOnly;
121        }
122    
123        /**
124         * Gets the labelFor attribute. 
125         * @return Returns the labelFor.
126         */
127        public String getLabelFor() {
128            return labelFor;
129        }
130    
131        /**
132         * Sets the labelFor attribute value.
133         * @param labelFor The labelFor to set.
134         */
135        public void setLabelFor(String labelFor) {
136            this.labelFor = labelFor;
137        }
138    
139        /**
140         * 
141         * @see org.kuali.kfs.sys.document.web.renderers.Renderer#clear()
142         */
143        public void clear() {
144            readOnly = false;
145            required = false;
146            label = null;
147            fullClassNameForHelp = null;
148            attributeEntryForHelp = null;
149            labelFor = null;
150        }
151    
152        private static String APPLICATION_URL;
153        
154        protected String getApplicationURL() {
155            if ( APPLICATION_URL == null ) {
156                APPLICATION_URL = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KNSConstants.APPLICATION_URL_KEY);
157            }
158            return APPLICATION_URL;
159        }
160        
161        /**
162         * 
163         * @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)
164         */
165        public void render(PageContext pageContext, Tag parentTag) throws JspException {
166            try {
167                JspWriter out = pageContext.getOut();
168                if (!StringUtils.isBlank(labelFor)) {
169                    out.write("<label for=\""+labelFor+"\">");
170                }
171                if (required) {
172                    out.write(KFSConstants.REQUIRED_FIELD_SYMBOL);
173                    out.write("&nbsp;");
174                }
175                if (!StringUtils.isBlank(fullClassNameForHelp) && !StringUtils.isBlank(attributeEntryForHelp)) {
176                    out.write("<a href=\"");
177                    out.write(getApplicationURL());
178                    out.write("/kr/help.do?methodToCall=getAttributeHelpText&amp;businessObjectClassName=");
179                    out.write(fullClassNameForHelp);
180                    out.write("&amp;attributeName=");
181                    out.write(attributeEntryForHelp);
182                    out.write("\" target=\"_blank\">");
183                }
184                out.write(label);
185                if (!StringUtils.isBlank(fullClassNameForHelp) && !StringUtils.isBlank(attributeEntryForHelp)) {
186                    out.write("</a>");
187                }
188                if (!StringUtils.isBlank(labelFor)) {
189                    out.write("</label>");
190                }
191            }
192            catch (IOException ioe) {
193                throw new JspException("Difficulty rendering label", ioe);
194            }
195        }
196    
197    }