View Javadoc
1   package org.myorg.jpatickets.dao;
2   
3   import java.util.HashMap;
4   import java.util.List;
5   import java.util.Map;
6   
7   import javax.persistence.EntityManager;
8   import javax.persistence.TypedQuery;
9   import javax.persistence.criteria.CriteriaBuilder;
10  import javax.persistence.criteria.CriteriaQuery;
11  import javax.persistence.criteria.Expression;
12  import javax.persistence.criteria.Join;
13  import javax.persistence.criteria.Predicate;
14  import javax.persistence.criteria.Root;
15  
16  import org.myorg.jpatickets.bo.Event;
17  import org.myorg.jpatickets.bo.Seat;
18  import org.myorg.jpatickets.bo.Ticket;
19  
20  public class EventMgmtDAOImpl implements EventMgmtDAO {
21      EntityManager em;
22      
23      public void setEntityManager(EntityManager em) {
24          this.em = em;
25      }
26  
27      @Override
28      public Event createEvent(Event event) {
29          em.persist(event);
30          return event;
31      }
32      
33      @Override
34      public Event getEvent(int eventId) {
35          return em.find(Event.class, eventId);
36      }
37      
38      @Override
39      public Event fetchEventTickets(int id) {
40          List<Event> events = em.createNamedQuery("JPATicketEvent.fetchEventTickets", 
41                  Event.class)
42                  .setParameter("eventId", id)
43                  .getResultList();
44          return events.isEmpty() ? null : events.get(0);
45      }    
46      
47      @Override
48      public Event fetchEventTicketsSeats(int id) {
49          List<Event> events = em.createNamedQuery("JPATicketEvent.fetchEventTicketsSeats", 
50                  Event.class)
51                  .setParameter("eventId", id)
52                  .getResultList();
53          return events.isEmpty() ? null : events.get(0);
54      }
55      
56      @Override
57      public Map<String, Object> fetchEventDTOData(int eventId) {
58          @SuppressWarnings("unchecked")
59          List<Object[]> rows = em.createNamedQuery("JPATicketEvent.fetchEventDTO")
60                  .setParameter("eventId", eventId)
61                  .getResultList();
62          
63          Map<String, Object> dtoData = new HashMap<String, Object>();
64          if (!rows.isEmpty()) {
65              Object[] row = rows.get(0);
66              Event event = (Event) row[0];
67              String venueName = (String) row[1];
68              Number numTickets = (Number) row[2];
69              dtoData.put(EVENT, event);
70              dtoData.put(VENUE_NAME, venueName);
71              dtoData.put(NUM_TICKETS, numTickets.intValue());
72          }
73          return dtoData;
74      }
75      
76      @Override
77      public List<Seat> findSeats(Event event, String section, Integer row,
78              Integer position, int offset, int limit) {
79          CriteriaBuilder cb = em.getCriteriaBuilder();
80          CriteriaQuery<Seat> qdef = cb.createQuery(Seat.class);
81          Root<Ticket> t = qdef.from(Ticket.class);
82          Join<Ticket, Event> e = t.join("event");
83          Join<Ticket, Seat> s = t.join("seat");
84          
85          Predicate pred = cb.conjunction();
86          List<Expression<Boolean>> expr = pred.getExpressions();
87          if (section != null) {
88              expr.add(cb.equal(s.get("pk").get("section"), section));
89          }
90          if (row !=null) {
91              expr.add(cb.equal(s.get("pk").get("row"), row));
92          }
93          if (position !=null) {
94              expr.add(cb.equal(s.get("pk").get("position"), position));
95          }
96          
97          qdef.select(s).where(cb.and(cb.equal(e, event), pred)).orderBy(cb.asc(t.get("price")));
98          TypedQuery<Seat> query = em.createQuery(qdef);
99          if (offset > 0) {
100             query.setFirstResult(offset);
101         }
102         if (limit > 0) {
103             query.setMaxResults(limit);
104         }
105         
106         return query.getResultList();
107     }
108     
109     @Override
110     public List<Ticket> findTickets(Event event, List<Seat> seats) {
111         return em.createNamedQuery("JPATicketTicket.findTickets", 
112                 Ticket.class)
113                 .setParameter("seats", seats)
114                 .getResultList();
115     }
116 }