001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Greg Lapouchnian
022: * Patrick Smith
023: *
024: */package olstore.domain.manager;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.ejb.EJBException;
031: import javax.ejb.FinderException;
032:
033: import olstore.dto.AddressValue;
034: import olstore.dto.CreateUserValue;
035: import olstore.dto.OrderEntry;
036: import olstore.dto.UserValue;
037: import olstore.entity.OrderLocal;
038: import olstore.entity.UserLocal;
039: import olstore.entity.UserLocalHome;
040: import olstore.session.helper.UserHelper;
041:
042: /**
043: * A wrapper class for user actions so that there is no need to reference the
044: * beans directly, allowing us to swap out the backend beans for a different
045: * implementation.
046: */
047: public class UserManager {
048:
049: /** A reference to the LocalHome inteface of the user EJB */
050: private UserLocalHome home;
051: /** A refernce to the user helper object */
052: private UserHelper helper;
053:
054: /**
055: * Constructor for the manager which is populated by Spring.
056: * @param home a reference to the LocalHome interface
057: * @param helper a helper object
058: */
059: public UserManager(UserLocalHome home, UserHelper helper) {
060: this .home = home;
061: this .helper = helper;
062: }
063:
064: /**
065: * Find a user by their username.
066: * @param username the username to lookup
067: * @return the UserLocal object for the user with the given username
068: */
069: public UserLocal findByUsername(String username) {
070: UserLocal user;
071: try {
072: user = home.findByUsername(username);
073: } catch (FinderException ex) {
074: throw new EJBException("Unable to find user", ex);
075: }
076:
077: return user;
078:
079: }
080:
081: /**
082: * Find all registered users in this store.
083: * @return a collection of all users.
084: */
085: public Collection findAll() {
086: Collection c;
087: try {
088: c = home.findAll();
089: } catch (FinderException ex) {
090: throw new EJBException("Unable to find users", ex);
091: }
092: return c;
093: }
094:
095: /**
096: * Find a user with given name and return a POJO for that user.
097: * @param username the username to lookup
098: * @return a plain java object for the user with the given username
099: */
100: public UserValue findUserValue(String username) {
101: UserValue user;
102: try {
103: user = helper.UserToDTO(home.findByUsername(username));
104: } catch (Exception ex) {
105: throw new EJBException("Unable to find user", ex);
106: }
107:
108: return user;
109: }
110:
111: /**
112: * Return all the orders that belong to the user.
113: * @param username the username of the user whose orders are required
114: * @return a collection of OrderEntry objects that belong to this user
115: */
116: public Collection getOrders(String username) {
117: UserLocal user;
118: try {
119: user = home.findByUsername(username);
120: } catch (FinderException ex) {
121: throw new EJBException("Unable to find user", ex);
122: }
123:
124: Collection entries = new ArrayList();
125: Collection orderLocals = user.getOrders();
126:
127: Iterator orderIterator = orderLocals.iterator();
128: while (orderIterator.hasNext()) {
129: OrderLocal orderLocal = (OrderLocal) orderIterator.next();
130: OrderEntry orderEntry = new OrderEntry(orderLocal);
131: entries.add(orderEntry);
132: }
133:
134: return entries;
135: }
136:
137: public Collection getUserPurchased(String username) {
138: UserLocal user;
139: try {
140: user = home.findByUsername(username);
141: } catch (FinderException ex) {
142: throw new EJBException("Unable to find user", ex);
143: }
144: return user.getPurchased();
145: }
146:
147: /**
148: * Save a user with the given username and all of their profile information
149: * @param user the object representing the user
150: * @param address the object that contains the user's address
151: * @param username user's username
152: * @throws Exception if an error occurs while saving user's information
153: */
154: public void saveUser(UserValue user, AddressValue address,
155: String username) throws Exception {
156: helper.SaveUser(user, address, username);
157: }
158:
159: /**
160: * Create a new user for customer in the store.
161: * @param user the object representing the user
162: * @param address the object representing the customer
163: * @throws Exception if an error occures while creating the new user
164: */
165: public void createUser(UserValue user, AddressValue address)
166: throws Exception {
167: CreateUserValue userValue = new CreateUserValue();
168: userValue.setUsername(user.getUsername());
169: userValue.setPhoneNum(user.getPhoneNum());
170: userValue.setPasswd1(user.getPasswd());
171: userValue.setLname(user.getLname());
172: userValue.setFname(user.getFname());
173: userValue.setEmailAdd(user.getEmailAdd());
174: helper.CreateUser(userValue, address);
175: }
176:
177: /**
178: * Returns a collection of all friends that have purchased the given item, and
179: * are friends of the given user.
180: * @param username the user whose friends we are interested in.
181: * @param itemId the item ID to see which friends have purchased.
182: * @return a collection of all friends that have purchased the given item, and
183: * are friends of the given user.
184: */
185: public Collection findFriendsThatPurchasedItem(String username,
186: Integer itemId) {
187: Collection c;
188: ArrayList friends = new ArrayList();
189: try {
190: c = home.findFriendsThatPurchasedItem(username, itemId);
191: } catch (FinderException ex) {
192: throw new EJBException("Unable to find user/item", ex);
193: }
194:
195: Iterator i = c.iterator();
196: while (i.hasNext()) {
197: friends.add(((UserLocal) i.next()).getUsername());
198: }
199:
200: return friends;
201: }
202: }
|