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: Aizaz Ahmed
022: * Vivek Lakshmanan
023: * Andrew Overholt
024: * Matthew Wringe
025: *
026: */package olstore.session.helper;
027:
028: import javax.ejb.CreateException;
029: import javax.ejb.SessionBean;
030: import javax.ejb.SessionContext;
031: import java.util.Collection;
032: import java.util.Collections;
033: import java.util.Iterator;
034: import java.util.Vector;
035: import olstore.entity.UserLocal;
036: import olstore.entity.UserLocalHome;
037: import olstore.entity.FriendLocal;
038: import olstore.entity.FriendLocalHome;
039: import olstore.entity.AddressLocal;
040: import olstore.entity.AddressLocalHome;
041: import olstore.dto.AddressValue;
042: import olstore.dto.CreateUserValue;
043: import olstore.dto.UserValue;
044: import olstore.framework.EJBHomeFactory;
045:
046: import org.apache.commons.beanutils.BeanUtils;
047:
048: /**
049: * @ejb.bean name="UserHelper"
050: * type="Stateless"
051: * view-type="local"
052: * local-jndi-name="UserHelperLocal_L"
053: *
054: * @ejb.ejb-ref
055: * ejb-name="User"
056: * view-type="local"
057: *
058: * @ejb.ejb-ref
059: * ejb-name="Address"
060: * view-type="local"
061: *
062: * @ejb.ejb-ref
063: * ejb-name="Friend"
064: * view-type="local"
065: *
066: * @ejb.transaction
067: * type="Required"
068: *
069: *--
070: * This is needed for JOnAS.
071: * If you are not using JOnAS you can safely remove the tags below.
072: * @jonas.bean ejb-name="UserHelper"
073: * jndi-name="UserHelperLocal"
074: *
075: *--
076: **/
077:
078: public abstract class UserHelperBean implements SessionBean {
079: SessionContext ejbContext;
080:
081: // ------------------------------------------------------------------
082: // SessionBean implementation
083: // ------------------------------------------------------------------
084:
085: public void setSessionContext(SessionContext ctx) {
086: ejbContext = ctx;
087: }
088:
089: public void ejbRemove() {
090: }
091:
092: public void ejbCreate() throws CreateException {
093: }
094:
095: public void ejbPassivate() {
096: }
097:
098: public void ejbActivate() {
099: }
100:
101: // ------------------------------------------------------------------
102: // UserHelper implementation
103: // ------------------------------------------------------------------
104:
105: /**
106: * Creates a collection of friends usernames and returns it.
107: *
108: * @param user The user to get the collections from.
109: * @return A collection containing the username's of the user's friends
110: */
111: private Collection CalculateFriendsName(UserLocal user)
112: throws Exception {
113: Collection c = new Vector();
114: Collection friends = user.getFriends();
115: Iterator it = friends.iterator();
116: while (it.hasNext()) {
117: c.add(((FriendLocal) it.next()).getUser().getUsername());
118: }
119: Collections.sort((Vector) c);
120: return c;
121: }
122:
123: /**
124: * A Method to get and return a collection of usernames that are not
125: * the user's username or that of any of there friend's usernames.
126: *
127: * @param user The user
128: * @param friendsList A collection of the user's friends username's
129: * @return A collection of usernames excluding the user's and there friend's usernames
130: * @throws Exception
131: */
132: private Collection CalculateAllUsersList(UserLocal user,
133: Collection friendsList) throws Exception {
134: EJBHomeFactory factory = EJBHomeFactory.getInstance();
135: UserLocalHome userHome = (UserLocalHome) factory
136: .getLocalHome(EJBHomeFactory.USER);
137: Collection allUsers = userHome.findAll();
138: Iterator it = allUsers.iterator();
139: Collection c = new Vector();
140: String currentUser;
141: while (it.hasNext()) {
142: currentUser = (((UserLocal) it.next()).getUsername());
143: c.add(currentUser);
144: }
145:
146: Collections.sort((Vector) c);
147:
148: //remove the users friends
149: c.removeAll(friendsList);
150:
151: //remove the user from the collection
152: c.remove(user.getUsername());
153:
154: return c;
155: }
156:
157: /**
158: * Method to copy the user's information from the
159: * user entity bean to the userValue dto.
160: *
161: * @param user The user to get the information from.
162: * @return The userValue dto.
163: * @throws Exception
164: */
165: private UserValue UserToDTO(UserLocal user) throws Exception {
166: UserValue userValue = new UserValue();
167: Collection friendsList = CalculateFriendsName(user);
168: userValue.setFriendsList(friendsList);
169: userValue.setAllUsersList(CalculateAllUsersList(user,
170: friendsList));
171: BeanUtils.copyProperties(userValue, user);
172: return userValue;
173: }
174:
175: /**
176: * Used to save the user's information from a DTO back into
177: * the user entity bean
178: *
179: * @param userValue The DTO storing the user information
180: * @param addressValue The DTO storing the address information
181: * @param username The username of the user
182: *
183: * @ejb.interface-method
184: */
185: public void SaveUser(UserValue userValue,
186: AddressValue addressValue, String username)
187: throws Exception {
188: EJBHomeFactory factory = EJBHomeFactory.getInstance();
189: UserLocalHome userHome = (UserLocalHome) factory
190: .getLocalHome(EJBHomeFactory.USER);
191: UserLocal user = userHome.findByUsername(username);
192: FriendLocalHome friendsHome = (FriendLocalHome) factory
193: .getLocalHome(EJBHomeFactory.FRIENDS);
194: AddressLocal address = user.getAddress();
195: user.setLname(userValue.getLname());
196: user.setFname(userValue.getFname());
197: user.setEmailAdd(userValue.getEmailAdd());
198: user.setPhoneNum(userValue.getPhoneNum());
199: address.setCity(addressValue.getCity());
200: address.setPostalCode(addressValue.getPostalCode());
201: address.setStreet1(addressValue.getStreet1());
202: address.setStreet2(addressValue.getStreet2());
203: address.setCountry(addressValue.getCountry());
204: address.setProvince(addressValue.getProvince());
205: user.setAddress(address);
206:
207: Collection friends = user.getFriends();
208: Iterator friendsIt = friends.iterator();
209:
210: //remove the old instances of friends
211: while (friendsIt.hasNext()) {
212: FriendLocal friendsLoc = (FriendLocal) friendsIt.next();
213: friendsIt.remove();
214: friendsLoc.remove();
215: }
216:
217: //load the new friends
218: String[] newFriends = userValue.getFriendsListSelected();
219: for (int i = 0; i < newFriends.length; i++) {
220: friends.add(friendsHome.create(userHome
221: .findByUsername(newFriends[i])));
222: }
223: user.setFriends(friends);
224: }
225:
226: /**
227: * Return the user's DTO when the username is passed to it
228: *
229: * @param username The username of the user
230: * @return The DTO for the user
231: *
232: * @ejb.interface-method
233: *
234: */
235: public UserValue getUserValue(String username) throws Exception {
236: EJBHomeFactory factory = EJBHomeFactory.getInstance();
237: UserLocalHome userHome = (UserLocalHome) factory
238: .getLocalHome(EJBHomeFactory.USER);
239: UserLocal user = userHome.findByUsername(username);
240: UserValue userValue = UserToDTO(user);
241: return userValue;
242: }
243:
244: /**
245: *
246: *
247: * Creates a new user entity bean
248: *
249: * @param createUserValue The DTO for the User
250: * @param addressValue The DTO for the User's Address
251: *
252: * @ejb.interface-method
253: *
254: */
255: public void CreateUser(CreateUserValue createUserValue,
256: AddressValue addressValue) throws Exception {
257: EJBHomeFactory factory = EJBHomeFactory.getInstance();
258: AddressLocalHome addressHome = null;
259: addressHome = (AddressLocalHome) factory
260: .getLocalHome(EJBHomeFactory.ADDRESS);
261: UserLocalHome userHome = (UserLocalHome) factory
262: .getLocalHome(EJBHomeFactory.USER);
263: UserLocal user = userHome.create(createUserValue.getUsername(),
264: "customer", createUserValue.getLname(), createUserValue
265: .getFname(), createUserValue.getEmailAdd(),
266: createUserValue.getPhoneNum(), createUserValue
267: .getPasswd1());
268: AddressLocal address = addressHome.create(addressValue
269: .getCountry(), addressValue.getCity(), addressValue
270: .getPostalCode(), addressValue.getStreet1(),
271: addressValue.getStreet2(), addressValue.getProvince());
272: user.setAddress(address);
273: }
274:
275: }
|