View Javadoc
1   package org.myorg.jpatickets.ejb;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Map;
6   
7   import javax.annotation.PostConstruct;
8   import javax.annotation.PreDestroy;
9   import javax.ejb.Stateless;
10  import javax.ejb.TransactionAttribute;
11  import javax.ejb.TransactionAttributeType;
12  import javax.persistence.EntityManager;
13  import javax.persistence.PersistenceContext;
14  
15  import org.myorg.jpatickets.bl.EventMgmt;
16  import org.myorg.jpatickets.bl.EventMgmtImpl;
17  import org.myorg.jpatickets.bl.UnavailableException;
18  import org.myorg.jpatickets.bo.Event;
19  import org.myorg.jpatickets.bo.Ticket;
20  import org.myorg.jpatickets.bo.Venue;
21  import org.myorg.jpatickets.dao.EventMgmtDAO;
22  import org.myorg.jpatickets.dao.EventMgmtDAOImpl;
23  import org.myorg.jpatickets.dao.VenueDAOImpl;
24  import org.myorg.jpatickets.dto.EventDTO;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  @Stateless
29  public class EventMgmtEJB implements EventMgmtRemote {
30      private static final Logger logger = LoggerFactory.getLogger(EventMgmtEJB.class);
31      
32      //@PersistenceContext(unitName="jpatickets-labex")
33      private EntityManager em;
34      
35      private EventMgmtDAO edao;
36      private EventMgmt eventMgmt;
37      
38      @PostConstruct
39      public void init() {
40          logger.debug("*** EventMgmtEJB:init({}) ***", super.hashCode());
41          
42          edao = new EventMgmtDAOImpl();
43          ((EventMgmtDAOImpl)edao).setEntityManager(em);
44          VenueDAOImpl vdao = new VenueDAOImpl();
45              vdao.setEntityManager(em);
46          eventMgmt = new EventMgmtImpl();
47          ((EventMgmtImpl)eventMgmt).setEventDAO(edao);
48          ((EventMgmtImpl)eventMgmt).setVenueDAO(vdao);
49      }
50      
51      @PreDestroy
52      public void destroy() {
53          logger.debug("*** EventMgmtEJB:destroy({}) ***", super.hashCode()); 
54      }
55      
56  
57      @Override
58      @TransactionAttribute(TransactionAttributeType.REQUIRED)
59      public Event createEvent(Event event, Venue venue) throws UnavailableException {
60          return eventMgmt.createEvent(event, venue);
61      }
62      
63      @Override
64      @TransactionAttribute(TransactionAttributeType.SUPPORTS)
65      public Event getEvent(int id) {
66          logger.debug("getEvent({})", id);
67          return eventMgmt.getEvent(id);
68      }
69      
70      @Override
71      public Event getEventCleansed(int id) {
72          logger.debug("getCleansedEvent({})", id);
73          Event event = getEvent(id);
74          return toCleansed(event);
75      }
76      
77      private Event toCleansed(Event bo) {
78          Event pojo = new Event(bo.getId());
79          pojo.setName(bo.getName());
80          pojo.setStartTime(bo.getStartTime());
81          List<Ticket> tickets = new ArrayList<Ticket>(bo.getTickets().size());
82          for (Ticket t: bo.getTickets()) {
83              toCleansed(t, pojo);
84          }
85          pojo.setTickets(tickets);
86          return pojo;
87      }
88      
89      private Ticket toCleansed(Ticket bo, Event event) {
90          //example cleansing is stopping here for the example
91          Ticket pojo = new Ticket(event, bo.getSeat());
92          pojo.setPrice(bo.getPrice());
93          pojo.setSold(bo.isSold());
94          return pojo;
95      }
96      
97      @Override
98      public Event getEventTouchedSome(int id) {
99          logger.debug("getEventTouchedSome({})", id);
100         Event event = getEvent(id);
101         //touch the ticket collection to load tickets prior to marshaling back
102         event.getTickets().size();
103         return event;
104     }
105     
106     @Override
107     public Event getEventTouchedMore(int id) {
108         logger.debug("getEventTouchedMore({})", id);
109         Event event = getEvent(id);
110         //touch ticket collection and all seats to load both prior to marshaling back
111         event.getTickets().size();
112         event.getVenue().getName();
113         for (Ticket t: event.getTickets()) {
114             t.getSeat().getPosition();
115         }
116         return event;
117     }
118     
119     @Override
120     public Event getEventFetchedSome(int id) {
121         logger.debug("getEventFetchedSome({})", id);
122         Event event = eventMgmt.fetchEventTickets(id);
123         return event;
124     }
125     
126     @Override
127     public Event getEventFetchedMore(int id) {
128         logger.debug("getEventFetchedMore({})", id);
129         Event event = eventMgmt.fetchEventTicketsSeats(id);
130         return event;
131     }
132     
133     @Override
134     public EventDTO getEventLazyDTO(int id) {
135         logger.debug("getEventLazyDTO({})", id);
136         Event event = eventMgmt.getEvent(id);
137         return toEventDTO(event);
138     }
139     
140     private EventDTO toEventDTO(Event event) {
141         EventDTO dto = new EventDTO();
142         dto.setId(event.getId());
143         dto.setEventName(event.getName());
144         dto.setVenueName(event.getVenue().getName());
145         dto.setStartTime(event.getStartTime());
146         dto.setNumTickets(event.getTickets().size());
147         return dto;
148     }
149     
150     @Override
151     public EventDTO getEventFetchedDTO(int eventId) {
152         logger.debug("getEventFetchedDTO({})", eventId);
153         Map<String, Object> dtoData = edao.fetchEventDTOData(eventId);
154         return toEventDTO(dtoData);
155     }
156     
157     private EventDTO toEventDTO(Map<String, Object> dtoData) {
158         EventDTO dto = new EventDTO();
159         Event event = (Event) dtoData.get(EventMgmtDAO.EVENT);
160         String venueName = (String) dtoData.get(EventMgmtDAO.VENUE_NAME);
161         int numTickets = (Integer) dtoData.get(EventMgmtDAO.NUM_TICKETS);
162         dto.setId(event.getId());
163         dto.setEventName(event.getName());
164         dto.setStartTime(event.getStartTime());
165         dto.setVenueName(venueName);
166         dto.setNumTickets(numTickets);
167         return dto;
168     }
169     
170 }