Ravi Srinivasan
2018-09-10 aaf094af7ecf98e07e31a231fdab871b25961bd1
commit | author | age
aaf094 1 package com.redhat.training.ui;
RS 2
3 import javax.enterprise.context.RequestScoped;
4 import javax.faces.application.FacesMessage;
5 import javax.faces.context.FacesContext;
6 import javax.inject.Named;
7 import javax.validation.ConstraintViolation;
8 import javax.validation.ConstraintViolationException;
9
10 import com.redhat.training.model.Person;
11 import com.redhat.training.services.PersonService;
12
13 import javax.inject.Inject;
14 import java.util.List;
15
16
17
18 @RequestScoped
19 @Named("hello")
20 public class Hello {
21     private String name;
22     private Long id;
23
24     @Inject
25     private PersonService personService;
26
27     public void sayHello() {
28         try {
29             String response = personService.hello(name);
30             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(response));
31         }catch(Exception e){
32             System.out.println(e.getCause());
33             if(e.getCause() != null && e.getCause() instanceof ConstraintViolationException) {
34                 ConstraintViolationException ex = (ConstraintViolationException) e.getCause();
35                 String violations = "";
36                 for(ConstraintViolation<?> cv: ex.getConstraintViolations()) {
37                     
38                     violations += cv.getMessage() + "\n";
39                     
40                     System.out.println("Violations: "+violations);
41                 }
42                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(violations));
43             }
44             
45         }
46         
47     }
48
49     public String getName() {
50         return name;
51     }
52
53     public void setName(String name) {
54         this.name = name;
55     }
56     
57     public Long getId() {
58         return id;
59     }
60
61     public void setId(Long id) {
62         this.id = id;
63     }
64     
65     public void getPerson() {
66         try {
67             String response = personService.getPerson(id);
68             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(response));
69         }catch(Exception e){
70             System.out.println(e.getCause());
71             if(e.getCause() != null && e.getCause() instanceof ConstraintViolationException) {
72                 ConstraintViolationException ex = (ConstraintViolationException) e.getCause();
73                 String violations = "";
74                 for(ConstraintViolation<?> cv: ex.getConstraintViolations()) {
75                     
76                     violations += cv.getMessage() + "\n";
77                     
78                     System.out.println("Violations: "+violations);
79                 }
80                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(violations));
81             }
82             
83         }
84         
85     }
86     
87     
88     public List<Person> getPersons() {
89         return personService.getAllPersons();
90         
91     }
92     
93     
94     
95
96 }