1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.element;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
20 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
21 import org.kuali.rice.krad.datadictionary.parse.BeanTags;
22 import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
23 import org.kuali.rice.krad.datadictionary.validator.Validator;
24 import org.kuali.rice.krad.uif.UifConstants;
25 import org.kuali.rice.krad.uif.component.Component;
26 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
27 import org.kuali.rice.krad.uif.util.ComponentUtils;
28 import org.kuali.rice.krad.uif.util.LifecycleElement;
29 import org.kuali.rice.krad.uif.util.MessageStructureUtils;
30 import org.kuali.rice.krad.util.KRADConstants;
31
32 import java.util.List;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36
37
38
39
40
41
42
43
44
45
46 @BeanTags({@BeanTag(name = "message-bean", parent = "Uif-Message"),
47 @BeanTag(name = "instructionalMessage-bean", parent = "Uif-InstructionalMessage"),
48 @BeanTag(name = "constraintMessage-bean", parent = "Uif-ConstraintMessage"),
49 @BeanTag(name = "requiredMessage-bean", parent = "Uif-RequiredMessage"),
50 @BeanTag(name = "requiredInstructionsMessage-bean", parent = "Uif-RequiredInstructionsMessage"),
51 @BeanTag(name = "stateBased-requiredInstructionsMessage-bean",
52 parent = "Uif-StateBased-RequiredInstructionsMessage"),
53 @BeanTag(name = "dialogPrompt-bean", parent = "Uif-DialogPrompt"),
54 @BeanTag(name = "imageCutineMessage-bean", parent = "Uif-ImageCutineMessage")})
55 public class Message extends ContentElementBase {
56 private static final long serialVersionUID = 4090058533452450395L;
57
58
59 private static Pattern blockElementCheck = Pattern.compile(
60 "[\\[|\\<](?!color|action|link|css|button|input|label|select|textarea|abbr"
61 + "|strong|img|a[\\s\\]]|span[\\s\\]]|b[\\s\\]]|i[\\s\\]]|br[\\s\\]/])[^/]*?/?[\\]|\\>]");
62
63 private String messageText;
64 private String wrapperTag;
65 private boolean renderWrapperTag;
66
67 private List<Component> inlineComponents;
68 private List<Component> messageComponentStructure;
69
70 private boolean parseComponents;
71 private boolean richMessage;
72 private boolean containsBlockElements;
73
74 public Message() {
75 super();
76
77 renderWrapperTag = true;
78 parseComponents = true;
79 }
80
81
82
83
84
85
86
87 @Override
88 public void performApplyModel(Object model, LifecycleElement parent) {
89 super.performApplyModel(model, parent);
90
91
92
93 if (messageText != null && messageText.contains(KRADConstants.MessageParsing.LEFT_TOKEN) &&
94 messageText.contains(KRADConstants.MessageParsing.RIGHT_TOKEN) &&
95 (messageComponentStructure == null || messageComponentStructure.isEmpty())) {
96 richMessage = true;
97
98
99 Matcher matcher = blockElementCheck.matcher(messageText);
100 containsBlockElements = matcher.find();
101
102 if (StringUtils.isBlank(wrapperTag) && containsBlockElements) {
103 wrapperTag = UifConstants.WrapperTags.DIV;
104 }
105
106 messageComponentStructure = MessageStructureUtils.parseMessage(this.getId(), this.getMessageText(),
107 this.getInlineComponents(), ViewLifecycle.getView(), parseComponents);
108 } else if (messageText != null && messageText.contains("<") && messageText.contains(">")) {
109
110
111 Matcher matcher = blockElementCheck.matcher(messageText);
112 containsBlockElements = matcher.find();
113
114
115 if (StringUtils.isBlank(wrapperTag) && containsBlockElements) {
116 wrapperTag = UifConstants.WrapperTags.DIV;
117 }
118 }
119
120
121
122 if (StringUtils.isBlank(wrapperTag)) {
123 wrapperTag = UifConstants.WrapperTags.P;
124 }
125 }
126
127
128
129
130 @Override
131 public void performFinalize(Object model, LifecycleElement parent) {
132 super.performFinalize(model, parent);
133
134 if (messageComponentStructure != null && !messageComponentStructure.isEmpty()) {
135
136 this.addDataAttribute(UifConstants.DataAttributes.PARENT, parent.getId());
137 }
138 }
139
140
141
142
143
144
145
146 @Override
147 public boolean isRender() {
148 boolean render = super.isRender();
149
150 if (render) {
151 render = getPropertyExpressions().containsKey("messageText") || (StringUtils.isNotBlank(messageText)
152 && !StringUtils.equals(messageText, " "));
153 }
154
155 return render;
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 @BeanTagAttribute(name = "messageText")
181 public String getMessageText() {
182 return this.messageText;
183 }
184
185
186
187
188
189
190 public void setMessageText(String messageText) {
191 this.messageText = messageText;
192 }
193
194
195
196
197
198
199
200 @BeanTagAttribute(name = "wrapperTag")
201 public String getWrapperTag() {
202 return wrapperTag;
203 }
204
205
206
207
208 public void setWrapperTag(String wrapperTag) {
209 this.wrapperTag = wrapperTag;
210 }
211
212
213
214
215
216
217
218
219
220
221
222 @BeanTagAttribute(name = "renderWrapperTag")
223 public boolean isRenderWrapperTag() {
224 return renderWrapperTag && wrapperTag != null;
225 }
226
227
228
229
230
231
232 public void setRenderWrapperTag(boolean renderWrapperTag) {
233 this.renderWrapperTag = renderWrapperTag;
234 }
235
236
237
238
239
240
241
242
243
244
245 public List<Component> getMessageComponentStructure() {
246 return messageComponentStructure;
247 }
248
249
250
251
252
253
254
255 public void setMessageComponentStructure(List<Component> messageComponentStructure) {
256 this.messageComponentStructure = messageComponentStructure;
257 }
258
259
260
261
262
263
264
265
266
267
268
269 @BeanTagAttribute(name = "inlineComponents", type = BeanTagAttribute.AttributeType.LISTBEAN)
270 public List<Component> getInlineComponents() {
271 return inlineComponents;
272 }
273
274
275
276
277
278
279 public void setInlineComponents(List<Component> inlineComponents) {
280 this.inlineComponents = inlineComponents;
281 }
282
283
284
285
286
287
288 @BeanTagAttribute(name = "parseComponents")
289 public boolean isParseComponents() {
290 return parseComponents;
291 }
292
293
294
295
296
297
298 public void setParseComponents(boolean parseComponents) {
299 this.parseComponents = parseComponents;
300 }
301
302
303
304
305
306
307
308 public boolean isRichMessage() {
309 return richMessage;
310 }
311
312
313
314
315
316
317 public boolean isContainsBlockElements() {
318 return containsBlockElements;
319 }
320
321
322
323
324 @Override
325 public void completeValidation(ValidationTrace tracer) {
326 tracer.addBean(this);
327
328
329 if (getMessageText() == null) {
330 if (Validator.checkExpressions(this, "messageText")) {
331 String currentValues[] = {"messageText =" + getMessageText()};
332 tracer.createWarning("MessageText should be set", currentValues);
333 }
334 }
335
336 super.completeValidation(tracer.getCopy());
337 }
338 }