View Javadoc
1   package ejava.examples.asyncmarket.ejb;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.List;
6   
7   import javax.annotation.PostConstruct;
8   import javax.ejb.Stateless;
9   import javax.ejb.TransactionAttribute;
10  import javax.ejb.TransactionAttributeType;
11  import javax.persistence.EntityManager;
12  import javax.persistence.PersistenceContext;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  import ejava.examples.asyncmarket.MarketException;
18  import ejava.examples.asyncmarket.bo.AuctionItem;
19  import ejava.examples.asyncmarket.bo.Bid;
20  import ejava.examples.asyncmarket.bo.Person;
21  import ejava.examples.asyncmarket.dao.PersonDAO;
22  import ejava.examples.asyncmarket.jpa.JPAPersonDAO;
23  
24  @Stateless
25  @TransactionAttribute(TransactionAttributeType.REQUIRED)
26  public class UserMgmtEJB implements UserMgmtRemote, UserMgmtLocal {
27      private static final Log log = LogFactory.getLog(UserMgmtEJB.class);
28      @PersistenceContext(unitName="asyncMarket")
29      private EntityManager em;
30      
31      private PersonDAO userDAO;
32      
33      @PostConstruct
34      public void init() {
35          log.info("*** UserMgmtEJB init() ***");
36          log.debug("em=" + em);
37          
38          userDAO = new JPAPersonDAO();
39          ((JPAPersonDAO)userDAO).setEntityManager(em);
40      }
41  
42      public long createUser(String userId, String name) throws MarketException {
43          try {
44              Person user = new Person();
45              user.setName(name);
46              user.setUserId(userId);
47              return userDAO.createPerson(user).getId();
48          } 
49          catch (Exception ex) {
50              log.error("error creating user", ex);
51              throw new MarketException("error creating user:" + ex);
52          }
53      }
54  
55      public List<Person> getUsers(int index, int count) throws MarketException {
56          try {
57              return makeDTO(userDAO.getPeople(index, count));
58          }
59          catch (Exception ex) {
60              log.error("error getting users", ex);
61              throw new MarketException("error getting users:" + ex);
62          }
63      }
64  
65      public Person getUser(long id) throws MarketException {
66          try {
67              return makeDTO(userDAO.getPerson(id));
68          }
69          catch (Exception ex) {
70              log.error("error getting user", ex);
71              throw new MarketException("error getting user:" + ex);
72          }
73      }
74  
75      public Person getUserByUserId(String userId) throws MarketException {
76          try {
77              Person user = userDAO.getPersonByUserId(userId); 
78              log.debug("getUserByUserId(" + userId + ")=" + user);
79              return makeDTO(user);
80          }
81          catch (Exception ex) {
82              log.error("error getting user by userId", ex);
83              throw new MarketException("error getting user by userId:" + ex);
84          }
85      }
86  
87      public void removeUser(String userId) throws MarketException {
88          try {
89              Person user = userDAO.getPersonByUserId(userId);
90              userDAO.removePerson(user);
91          }
92          catch (Exception ex) {
93              log.error("error getting user by userId", ex);
94              throw new MarketException("error getting user by userId:" + ex);
95          }
96      }
97      
98      private List<Person> makeDTO(List<Person> people) {
99          List<Person> dtos = new ArrayList<Person>();
100         for (Person person : people) {
101             dtos.add(makeDTO(person));
102         }
103         return dtos;
104     }
105 
106     private Person makeDTO(Person user) {
107         Person dto = new Person(user.getId());
108         dto.setUserId(user.getUserId());
109         dto.setName(user.getName());
110         dto.setVersion(user.getVersion());
111         dto.setItems(makeDTO(user.getItems(), dto));
112         return dto;
113     }
114 
115     private Collection<AuctionItem> makeDTO(
116             Collection<AuctionItem> items, Person owner) {
117         Collection<AuctionItem> dtos = new ArrayList<AuctionItem>();
118         for(AuctionItem item : items) {
119             AuctionItem dto = new AuctionItem(item.getId());
120             dto.setVersion(item.getVersion());
121             dto.setName(item.getName());
122             dto.setOwner(owner);
123             owner.getItems().add(dto);
124             dto.setMinBid(item.getMinBid());
125             dto.setProductId(item.getProductId());
126             dto.setStartDate(item.getStartDate());
127             dto.setEndDate(item.getEndDate());
128             dto.setBids(makeDTO(item.getBids(), dto));            
129             if (item.getWinningBid() != null) {
130                 for(Bid bid : dto.getBids()) {
131                     if (bid.getId() == item.getWinningBid().getId()) {
132                         dto.setWinningBid(bid);
133                     }
134                 }
135             }
136             dto.setClosed(item.isClosed());
137             dtos.add(dto);
138         }        
139         return dtos;
140     }
141     
142     private List<Bid> makeDTO(List<Bid> bids, AuctionItem item) {
143         List<Bid> dtos = new ArrayList<Bid>();
144         for(Bid bid : bids) {
145             dtos.add(makeDTO(bid, item));    
146         }
147         return dtos;
148     }
149 
150     private Bid makeDTO(Bid bid, AuctionItem item) {
151         Bid dto = new Bid(bid.getId());
152         dto.setAmount(bid.getAmount());
153         dto.setItem(item);
154         item.getBids().add(dto);
155         dto.setBidder(makeDTO(bid.getBidder(), dto));
156         return dto;
157     }
158 
159     private Person makeDTO(Person bidder, Bid bid) {
160         Person dto = new Person(bidder.getId());
161         dto.setUserId(bidder.getUserId());
162         dto.setVersion(bidder.getVersion());
163         bid.setBidder(dto);
164         dto.getBids().add(bid);
165         return dto;
166     }
167 }