Ravi Srinivasan
2018-09-14 a37070c239d87ec5f0b0a0544351a57ed06f489b
commit | author | age
b9209f 1 package com.redhat.training.rest;
RS 2
3 import java.time.LocalDateTime;
4 import java.time.format.DateTimeFormatter;
5 import java.util.List;
6
7 import javax.annotation.Resource;
8 import javax.ejb.EJBException;
9 import javax.ejb.Stateless;
10 import javax.ejb.TransactionManagement;
11 import javax.ejb.TransactionManagementType;
12 import javax.inject.Inject;
13 import javax.persistence.EntityManager;
14 import javax.persistence.PersistenceContext;
15 import javax.persistence.TypedQuery;
16 import javax.transaction.UserTransaction;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.core.Response.ResponseBuilder;
27
28 import com.redhat.training.model.Person;
29
30 @Stateless
31 @Path("persons")
32 @Consumes(MediaType.APPLICATION_JSON)
33 @Produces(MediaType.APPLICATION_JSON)
34 @TransactionManagement(TransactionManagementType.BEAN)
35 public class PersonService {
36
37     @PersistenceContext
38     private EntityManager entityManager;
39
40     @Resource
41     UserTransaction tx;
42
43     // Simple non-RESTy method for JSF bean invocation
44     public String hello(String name) {
45         try {
46             try {
47                 // start a new transaction
48                 tx.begin();
49
50                 // let's grab the current date and time on the server
51                 LocalDateTime today = LocalDateTime.now();
52
53                 // format it nicely for on-screen display
54                 DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm:ss a");
55                 String fdate = today.format(format);
56
57                 // Create a new Person object and persist to database
58                 Person p = new Person();
59                 p.setName(name);
60                 entityManager.persist(p);
61
62                 // respond back with Hello and convert the name to UPPERCASE. Also, send the
63                 // current time on the server.
64                 return "Hello " + name.toUpperCase() + "!. " + "Time on the server is: " + fdate;
65             } finally {
66                 // commit the transaction
67                 tx.commit();
68             }
69         } catch (Exception e) {
70             throw new EJBException(e);
71         }
72     }
73
74     // CRUD RESTful methods below
75
76     // fetch result by Person id
77     @GET
78     @Path("{id}")
79     public Person getPerson(@PathParam("id") Long id) {
80         return entityManager.find(Person.class, id);
81     }
82
83     // Dump all Person objects in the Database
84     @GET
85     public List<Person> getAllPersons() {
86         TypedQuery<Person> query = entityManager.createQuery("SELECT p FROM Person p", Person.class);
87         List<Person> persons = query.getResultList();
88
89         return persons;
90     }
91
92     // delete an object by Person id
93     @DELETE
94     @Path("{id}")
95     public void deletePerson(@PathParam("id") Long id) {
96         try {
97             try {
98                 tx.begin();
99                 entityManager.remove(getPerson(id));
100             } finally {
101                 tx.commit();
102             }
103         } catch (Exception e) {
104             throw new EJBException();
105         }
106     }
107
108     // Save a Person object to Database
109     @POST
110     public Response savePerson(Person person) {
111         try {
112             try {
113             ResponseBuilder builder;
114             if (person.getId() == null) {
115                 Person newPerson = new Person();
116                 newPerson.setName(person.getName());
117                 tx.begin();
118                 entityManager.persist(newPerson);
119                 builder = Response.ok();
120             } else {
121                 Person uPerson;
122                 Person updatePerson = getPerson(person.getId());
123                 updatePerson.setName(person.getName());
124                 uPerson = entityManager.merge(updatePerson);
125                 builder = Response.ok(uPerson);
126             }
127
128             return builder.build();
129             }finally {
130                 tx.commit();
131             }
132         }catch (Exception e) {
133             throw new EJBException(e);
134         }
135     }
136
137 }