Ravi Srinivasan
2018-09-10 aaf094af7ecf98e07e31a231fdab871b25961bd1
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
package com.redhat.training.services;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
 
import javax.ejb.EJBException;
import javax.ejb.Stateless;
//import persistence related libraries
import javax.persistence.TypedQuery;
 
import com.redhat.training.model.Person;
 
@Stateless
 
public class PersonService {
    //TODO: obtain an EntityManager instance using @PersistenceContext 
    
 
    // Simple non-RESTy method for JSF bean invocation
    public String hello(String name) {
        try {
                // let's grab the current date and time on the server
                LocalDateTime today = LocalDateTime.now();
 
                // format it nicely for on-screen display
                DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm:ss a");
                String fdate = today.format(format);
 
                // Create a new Person object and persist to database
                Person p = new Person();
                p.setName(name);
                // call persist() method of entity manager to save the data                                                                
                
                // respond back with Hello and convert the name to UPPERCASE. Also, send the
                // current time on the server.
                return "Hello " + name.toUpperCase() + "!. " + "Time on the server is: " + fdate;
        } catch (Exception e) {
            throw new EJBException(e);
        }
    }
 
 
    // TODO:add public String getPerson(Long id) method here to fetch result
        // by Person id using find() method 
        
    // Get all Person objects in the Database
    public List<Person> getAllPersons() {
        TypedQuery<Person> query = entityManager.createQuery("SELECT p FROM Person p", Person.class);
        List<Person> persons = query.getResultList();
 
        return persons;
    }
 
}