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