Ravi Srinivasan
2018-09-10 aaf094af7ecf98e07e31a231fdab871b25961bd1
commit | author | age
aaf094 1 package com.redhat.training.services;
RS 2
3 import java.time.LocalDateTime;
4 import java.time.format.DateTimeFormatter;
5 import java.util.List;
6
7 import javax.ejb.EJBException;
8 import javax.ejb.Stateless;
9 //import persistence related libraries
10 import javax.persistence.TypedQuery;
11
12 import com.redhat.training.model.Person;
13
14 @Stateless
15
16 public class PersonService {
17     //TODO: obtain an EntityManager instance using @PersistenceContext 
18     
19
20     // Simple non-RESTy method for JSF bean invocation
21     public String hello(String name) {
22         try {
23                 // let's grab the current date and time on the server
24                 LocalDateTime today = LocalDateTime.now();
25
26                 // format it nicely for on-screen display
27                 DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm:ss a");
28                 String fdate = today.format(format);
29
30                 // Create a new Person object and persist to database
31                 Person p = new Person();
32                 p.setName(name);
33                 // call persist() method of entity manager to save the data                                                                
34                 
35                 // respond back with Hello and convert the name to UPPERCASE. Also, send the
36                 // current time on the server.
37                 return "Hello " + name.toUpperCase() + "!. " + "Time on the server is: " + fdate;
38         } catch (Exception e) {
39             throw new EJBException(e);
40         }
41     }
42
43
44     // TODO:add public String getPerson(Long id) method here to fetch result
45         // by Person id using find() method 
46         
47     // Get all Person objects in the Database
48     public List<Person> getAllPersons() {
49         TypedQuery<Person> query = entityManager.createQuery("SELECT p FROM Person p", Person.class);
50         List<Person> persons = query.getResultList();
51
52         return persons;
53     }
54
55 }