1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.kuali.ole.sys.OLEConstants;
27 import org.kuali.ole.sys.context.SpringContext;
28 import org.kuali.rice.core.api.config.property.ConfigurationService;
29 import org.kuali.rice.core.api.util.KeyValue;
30 import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
31 import org.kuali.rice.kns.web.ui.Field;
32
33
34
35
36 public class ReadOnlyRenderer extends FieldRendererBase {
37 private boolean shouldRenderInquiry = true;
38
39
40
41
42 @Override
43 public void render(PageContext pageContext, Tag parentTag) throws JspException {
44 JspWriter out = pageContext.getOut();
45
46 try {
47 String value = discoverRenderValue();
48 out.write(buildBeginSpan());
49
50 if (!StringUtils.isEmpty(value)) {
51 if (shouldRenderInquiryLink()) {
52 out.write(buildBeginInquiryLink());
53 }
54 out.write(value);
55 if (shouldRenderInquiryLink()) {
56 out.write(buildEndInquiryLink());
57 }
58 } else {
59 out.write(buildNonBreakingSpace());
60 }
61
62 out.write(buildEndSpan());
63 }
64 catch (IOException ioe) {
65 throw new JspException("Difficulty rendering read only field", ioe);
66 }
67 }
68
69
70
71
72
73 @Override
74 public void clear() {
75 super.clear();
76 }
77
78
79
80
81
82
83 protected String buildBeginSpan() {
84 StringBuilder beginSpan = new StringBuilder();
85 beginSpan.append("<span id=\"");
86 beginSpan.append(getFieldName());
87 beginSpan.append(".div\">");
88 return beginSpan.toString();
89 }
90
91
92
93
94
95 protected String buildEndSpan() {
96 return "</span>";
97 }
98
99
100
101
102
103 protected String buildBeginInquiryLink() {
104 StringBuilder beginInquiryLink = new StringBuilder();
105
106 if (getField().getInquiryURL() instanceof AnchorHtmlData) {
107 AnchorHtmlData htmlData = (AnchorHtmlData) getField().getInquiryURL();
108
109 if(htmlData.getHref().startsWith("http")) {
110 beginInquiryLink.append("<a href=\"");
111 }
112 else {
113 beginInquiryLink.append("<a href=\"");
114 beginInquiryLink.append(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.APPLICATION_URL_KEY));
115 beginInquiryLink.append("/kr/");
116 }
117
118 beginInquiryLink.append(htmlData.getHref());
119 beginInquiryLink.append("\" title=\"");
120 beginInquiryLink.append(htmlData.getTitle());
121 beginInquiryLink.append("\" target=\"blank\">");
122
123 }
124
125 return beginInquiryLink.toString();
126 }
127
128
129
130
131
132 protected String buildEndInquiryLink() {
133 if (getField().getInquiryURL() instanceof AnchorHtmlData) {
134 return "</a>";
135 }
136 return "";
137 }
138
139
140
141
142
143 protected boolean shouldRenderInquiryLink() {
144 return getField().getInquiryURL() != null && !StringUtils.isBlank(((AnchorHtmlData)getField().getInquiryURL()).getHref()) && isInquirableValue(getField().getPropertyValue()) && shouldRenderInquiry;
145 }
146
147
148
149
150
151
152 protected boolean isInquirableValue(String propertyValue) {
153 return !StringUtils.isBlank(propertyValue) && !propertyValue.matches("^-*$");
154 }
155
156
157
158
159
160 public void setShouldRenderInquiry(boolean shouldRenderInquiry) {
161 this.shouldRenderInquiry = shouldRenderInquiry;
162 }
163
164
165
166
167
168
169 protected String getValueForDropDown() {
170 for (Object keyLabelPairAsObject : getField().getFieldValidValues()) {
171 final KeyValue keyLabelPair = (KeyValue)keyLabelPairAsObject;
172 if (getField().getPropertyValue().equalsIgnoreCase(keyLabelPair.getKey().toString())) {
173 return keyLabelPair.getValue();
174 }
175 }
176 return null;
177 }
178
179
180
181
182
183
184
185
186 protected String discoverRenderValue() {
187 String value = getField().getPropertyValue();
188 if (getField().getFieldType().equals(Field.DROPDOWN) && !StringUtils.isEmpty(value)) {
189 value = getValueForDropDown();
190 }
191
192 return value;
193 }
194
195
196
197
198 protected String buildNonBreakingSpace() {
199 return " ";
200 }
201
202
203
204
205
206 @Override
207 public boolean renderQuickfinder() {
208 return false;
209 }
210
211
212
213
214 @Override
215 public void closeNoWrapSpan(PageContext pageContext, Tag parentTag) throws JspException {
216
217 }
218
219
220
221
222 @Override
223 public void openNoWrapSpan(PageContext pageContext, Tag parentTag) throws JspException {
224
225 }
226
227 }