package org.gluu.jsf2.message;

import java.io.Serializable;

import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

/**
 * @author Yuriy Movchan
 * @version 05/06/2017
 */
@RequestScoped
public class FacesMessages implements Serializable {

	private static final long serialVersionUID = -6408439483194578659L;

	@Inject
	private FacesContext facesContext;

	@Inject
	private ExternalContext externalContext;

	public void add(Severity severity, String message) {
		facesContext.addMessage(null, new FacesMessage(severity, message, message));
		setKeepMessages();
	}

	public void add(String clientId, Severity severity, String message) {
		facesContext.addMessage(clientId, new FacesMessage(severity, message, message));
		setKeepMessages();
	}

	public void add(Severity severity, String message, Object ... params) {
		// TODO: CDI Review. Add parameters to message
		add(severity, message);
		setKeepMessages();
	}

	public void setKeepMessages() {
		externalContext.getFlash().setKeepMessages(true);
	}

}

@@ -55,24 +55,122 @@
 lizable;%0D%0A%0D%0A
+import javax.el.ELContext;%0D%0Aimport javax.el.ExpressionFactory;%0D%0Aimport javax.el.ValueExpression;%0D%0A
 import javax
@@ -779,32 +779,84 @@
 ing message) %7B%0D%0A
+%09%09String evaluatedMessage = evalAsString(message);%0D%0A
 %09%09facesContext.a
@@ -894,34 +894,52 @@
 e(severity, 
-message, m
+evaluatedMessage, evaluatedM
 essage));%0D%0A%09
@@ -1037,16 +1037,68 @@
 age) %7B%0D%0A
+%09%09String evaluatedMessage = evalAsString(message);%0D%0A
 %09%09facesC
@@ -1156,18 +1156,36 @@
 ty, 
-message, m
+evaluatedMessage, evaluatedM
 essa
@@ -1277,17 +1277,16 @@
 , Object
- 
 ... para
@@ -1490,11 +1490,414 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%09public String evalAsString(String expression) %7B%0D%0A%09%09ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();%0D%0A%09%09ELContext elContext = facesContext.getELContext();%0D%0A%09%09ValueExpression valueExpression = expressionFactory.createValueExpression(elContext, expression, String.class);%0D%0A%09%09String result = (String) valueExpression.getValue(elContext);%0D%0A%0D%0A%09%09return result;%0D%0A%09%7D%0D%0A
 %7D%0D%0A

Evaluate message string as EL expression before adding message