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