Richard Allred
2019-05-23 497620c4d1bcb410267c56351432f87fb3aee5a4
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
package com.redhat.training.rest;
 
import com.redhat.training.model.Item;
import com.redhat.training.ui.PaginatedListWrapper;
 
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.ws.rs.*;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
 
import java.util.List;
 
@Stateless
@Path("items")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ItemService extends Application {
    
    @PersistenceContext
    private EntityManager entityManager;
 
    private Integer countItems() {
        Query query = entityManager.createQuery("SELECT COUNT(i.id) FROM Item i");
        return ((Long) query.getSingleResult()).intValue();
    }
 
    private List<Item> findItems(int startPosition, int maxResults, String sortFields, String sortDirections) {
        TypedQuery<Item> query =
                entityManager.createQuery("SELECT i FROM Item i ORDER BY i." + sortFields + " " + sortDirections, 
                        Item.class);
        query.setFirstResult(startPosition);
        query.setMaxResults(maxResults);
        return query.getResultList();
    }
 
    private PaginatedListWrapper findItems(PaginatedListWrapper wrapper) {
        wrapper.setTotalResults(countItems());
        int start = (wrapper.getCurrentPage() - 1) * wrapper.getPageSize();
        wrapper.setList(findItems(start,
                                    wrapper.getPageSize(),
                                    wrapper.getSortFields(),
                                    wrapper.getSortDirections()));
        return wrapper;
    }
 
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public PaginatedListWrapper listItems(@DefaultValue("1")
                                            @QueryParam("page")
                                            Integer page,
                                            @DefaultValue("id")
                                            @QueryParam("sortFields")
                                            String sortFields,
                                            @DefaultValue("asc")
                                            @QueryParam("sortDirections")
                                            String sortDirections) {
        PaginatedListWrapper paginatedListWrapper = new PaginatedListWrapper();
        paginatedListWrapper.setCurrentPage(page);
        paginatedListWrapper.setSortFields(sortFields);
        paginatedListWrapper.setSortDirections(sortDirections);
        paginatedListWrapper.setPageSize(10);
        return findItems(paginatedListWrapper);
    }
 
    @GET
    @Path("{id}")
    public Item getitem(@PathParam("id") Long id) {
        return entityManager.find(Item.class, id);
    }
 
    @POST
    public Item saveItem(Item item) {
        if (item.getId() == null) {
            Item itemToSave = new Item();
            itemToSave.setDescription(item.getDescription());
            itemToSave.setDone(item.isDone());
            entityManager.persist(item);
        } else {
            Item itemToUpdate = getitem(item.getId());
            itemToUpdate.setDescription(item.getDescription());
            itemToUpdate.setDone(item.isDone());
            item = entityManager.merge(itemToUpdate);
        }
 
        return item;
    }
 
    @DELETE
    @Path("{id}")
    public void deleteItem(@PathParam("id") Long id) {
        entityManager.remove(getitem(id));
    }
}