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