-
Notifications
You must be signed in to change notification settings - Fork 36
/
FacesUtils.java
73 lines (62 loc) · 1.99 KB
/
FacesUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package br.com.triadworks.loja.util;
import java.util.Iterator;
import javax.faces.application.FacesMessage;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("request")
public class FacesUtils {
private final FacesContext facesContext;
@Autowired
public FacesUtils(FacesContext facesContext) {
this.facesContext = facesContext;
}
public void adicionaMensagemDeErro(String msg) {
facesContext.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
}
public void adicionaMensagemDeInformacao(String msg) {
facesContext.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
}
public boolean possuiMensagem(String msg) {
Iterator<FacesMessage> messages = facesContext.getMessages();
while (messages.hasNext()) {
FacesMessage message = messages.next();
boolean confere = message.getDetail().equals(msg);
if (confere)
return true;
}
return false;
}
/**
* Limpa os dados dos componentes de edição e de seus filhos,
* recursivamente. Checa se o componente é instância de EditableValueHolder
* e 'reseta' suas propriedades.
* <p>
* Quando este método, por algum motivo, não funcionar, parta para ignorância
* e limpe o componente assim:
* <p><blockquote><pre>
* component.getChildren().clear()
* </pre></blockquote>
* :-)
*/
public void cleanSubmittedValues(UIComponent component) {
if (component instanceof EditableValueHolder) {
EditableValueHolder evh = (EditableValueHolder) component;
evh.setSubmittedValue(null);
evh.setValue(null);
evh.setLocalValueSet(false);
evh.setValid(true);
}
if(component.getChildCount()>0){
for (UIComponent child : component.getChildren()) {
cleanSubmittedValues(child);
}
}
}
}