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 |
//TODO Add the stateless annotation |
|
31 |
|
|
32 |
//TODO Add a Path for persons |
|
33 |
|
|
34 |
//TODO Add a Consumes annotation for JSON |
|
35 |
|
|
36 |
//TODO Add a Produces annotation for JSON |
|
37 |
|
|
38 |
@TransactionManagement(TransactionManagementType.BEAN) |
|
39 |
public class PersonService { |
|
40 |
|
|
41 |
@PersistenceContext |
|
42 |
private EntityManager entityManager; |
|
43 |
|
|
44 |
@Resource |
|
45 |
UserTransaction tx; |
|
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 |
// respond back with Hello and convert the name to UPPERCASE. Also, send the |
|
67 |
// current time on the server. |
|
68 |
return "Hello " + name.toUpperCase() + "!. " + "Time on the server is: " + fdate; |
|
69 |
} finally { |
|
70 |
// commit the transaction |
|
71 |
tx.commit(); |
|
72 |
} |
|
73 |
} catch (Exception e) { |
|
74 |
throw new EJBException(e); |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
// CRUD RESTful methods below |
|
79 |
|
|
80 |
// fetch result by Person id |
|
81 |
//TODO add GET annotation |
|
82 |
|
|
83 |
//TODO add path for ID |
|
84 |
|
|
85 |
public Person getPerson(Long id) { |
|
86 |
return entityManager.find(Person.class, id); |
|
87 |
} |
|
88 |
|
|
89 |
// Dump all Person objects in the Database |
|
90 |
//TODO add GET annotation |
|
91 |
|
|
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 |
//TODO add DELETE annotation |
|
101 |
|
|
102 |
//TODO add Path for ID |
|
103 |
|
|
104 |
public void deletePerson(Long id) { |
|
105 |
try { |
|
106 |
try { |
|
107 |
tx.begin(); |
|
108 |
entityManager.remove(getPerson(id)); |
|
109 |
} finally { |
|
110 |
tx.commit(); |
|
111 |
} |
|
112 |
} catch (Exception e) { |
|
113 |
throw new EJBException(); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
// Save a Person object to Database |
|
118 |
//TODO add POST annotation |
|
119 |
|
|
120 |
public Response savePerson(Person person) { |
|
121 |
try { |
|
122 |
try { |
|
123 |
ResponseBuilder builder; |
|
124 |
if (person.getId() == null) { |
|
125 |
Person newPerson = new Person(); |
|
126 |
newPerson.setName(person.getName()); |
|
127 |
tx.begin(); |
|
128 |
entityManager.persist(newPerson); |
|
129 |
builder = Response.ok(); |
|
130 |
} else { |
|
131 |
Person uPerson; |
|
132 |
Person updatePerson = getPerson(person.getId()); |
|
133 |
updatePerson.setName(person.getName()); |
|
134 |
uPerson = entityManager.merge(updatePerson); |
|
135 |
builder = Response.ok(uPerson); |
|
136 |
} |
|
137 |
|
|
138 |
return builder.build(); |
|
139 |
}finally { |
|
140 |
tx.commit(); |
|
141 |
} |
|
142 |
}catch (Exception e) { |
|
143 |
throw new EJBException(e); |
|
144 |
} |
|
145 |
} |
|
146 |
|
|
147 |
} |