KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > session > helper > UserHelperBean


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27 /*
28  * Created on Sep 7, 2004
29  *
30  *
31  */

32 package olstore.session.helper;
33
34 import javax.ejb.CreateException;
35 import javax.ejb.SessionBean;
36 import javax.ejb.SessionContext;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.Iterator;
40 import java.util.Vector;
41 import olstore.entity.UserLocal;
42 import olstore.entity.UserLocalHome;
43 import olstore.entity.FriendLocal;
44 import olstore.entity.FriendLocalHome;
45 import olstore.entity.AddressLocal;
46 import olstore.entity.AddressLocalHome;
47 import olstore.dto.AddressValue;
48 import olstore.dto.CreateUserValue;
49 import olstore.dto.UserValue;
50 import olstore.framework.EJBHomeFactory;
51
52 import org.apache.commons.beanutils.BeanUtils;
53 /**
54  * @ejb.bean name="UserHelper"
55  * type="Stateless"
56  * view-type="local"
57  * local-jndi-name="UserHelperLocal_L"
58  *
59  * @ejb.ejb-ref
60  * ejb-name="User"
61  * view-type="local"
62  *
63  * @ejb.ejb-ref
64  * ejb-name="Address"
65  * view-type="local"
66  *
67  * @ejb.ejb-ref
68  * ejb-name="Friend"
69  * view-type="local"
70  *
71  * @ejb.transaction
72  * type="Required"
73  *
74  *--
75  * This is needed for JOnAS.
76  * If you are not using JOnAS you can safely remove the tags below.
77  * @jonas.bean ejb-name="UserHelper"
78  * jndi-name="UserHelperLocal"
79  *
80  *--
81  **/

82
83 public abstract class UserHelperBean implements SessionBean {
84     SessionContext ejbContext;
85     
86     // ------------------------------------------------------------------
87
// SessionBean implementation
88
// ------------------------------------------------------------------
89

90     
91     public void setSessionContext(SessionContext ctx) {
92         ejbContext = ctx;
93     }
94     
95     
96     public void ejbRemove() {
97     }
98     
99     
100     public void ejbCreate() throws CreateException {
101     }
102     
103     public void ejbPassivate() {
104     }
105     
106     public void ejbActivate() {
107     }
108     
109     
110     // ------------------------------------------------------------------
111
// UserHelper implementation
112
// ------------------------------------------------------------------
113

114     
115     /**
116      * Creates a collection of friends usernames and returns it.
117      *
118      * @param user The user to get the collections from.
119      * @return A collection containing the username's of the user's friends
120      */

121     private Collection CalculateFriendsName (UserLocal user) throws Exception{
122         Collection c= new Vector();
123         Collection friends=user.getFriends();
124         Iterator it= friends.iterator();
125         while (it.hasNext()){
126             c.add (((FriendLocal)it.next()).getUser().getUsername());
127         }
128         Collections.sort((Vector)c);
129         return c;
130     }
131     
132     /**
133      * A Method to get and return a collection of usernames that are not
134      * the user's username or that of any of there friend's usernames.
135      *
136      * @param user The user
137      * @param friendsList A collection of the user's friends username's
138      * @return A collection of usernames excluding the user's and there friend's usernames
139      * @throws Exception
140      */

141     private Collection CalculateAllUsersList (UserLocal user, Collection friendsList) throws Exception {
142         EJBHomeFactory factory = EJBHomeFactory.getInstance();
143         UserLocalHome userHome=(UserLocalHome) factory.getLocalHome (EJBHomeFactory.USER);
144         Collection allUsers= userHome.findAll();
145         Iterator it=allUsers.iterator();
146         Collection c=new Vector();
147         String currentUser;
148         while (it.hasNext()){
149             currentUser=(((UserLocal)it.next()).getUsername());
150             c.add(currentUser);
151         }
152         
153         Collections.sort((Vector)c);
154         
155         //remove the users friends
156
c.removeAll (friendsList);
157         
158         //remove the user from the collection
159
c.remove(user.getUsername());
160         
161         return c;
162     }
163     
164     
165     /**
166      * Method to copy the user's information from the
167      * user entity bean to the userValue dto.
168      *
169      * @param user The user to get the information from.
170      * @return The userValue dto.
171      * @throws Exception
172      */

173     private UserValue UserToDTO (UserLocal user) throws Exception {
174         UserValue userValue=new UserValue ();
175         Collection friendsList=CalculateFriendsName(user);
176         userValue.setFriendsList(friendsList);
177         userValue.setAllUsersList (CalculateAllUsersList(user, friendsList));
178         BeanUtils.copyProperties (userValue, user);
179         return userValue;
180     }
181     
182     
183     /**
184      * Used to save the user's information from a DTO back into
185      * the user entity bean
186      *
187      * @param userValue The DTO storing the user information
188      * @param addressValue The DTO storing the address information
189      * @param username The username of the user
190      *
191      * @ejb.interface-method
192      */

193     public void SaveUser (UserValue userValue, AddressValue addressValue, String username) throws Exception {
194         EJBHomeFactory factory=EJBHomeFactory.getInstance();
195         UserLocalHome userHome=(UserLocalHome)factory.getLocalHome (EJBHomeFactory.USER);
196         UserLocal user=userHome.findByUsername (username);
197         FriendLocalHome friendsHome=(FriendLocalHome)factory.getLocalHome (EJBHomeFactory.FRIENDS);
198         AddressLocal address= user.getAddress();
199         user.setLname (userValue.getLname());
200         user.setFname (userValue.getFname());
201         user.setEmailAdd (userValue.getEmailAdd());
202         user.setPhoneNum (userValue.getPhoneNum());
203         address.setCity(addressValue.getCity());
204         address.setPostalCode(addressValue.getPostalCode());
205         address.setStreet1(addressValue.getStreet1());
206         address.setStreet2(addressValue.getStreet2());
207         address.setCountry(addressValue.getCountry());
208         address.setProvince(addressValue.getProvince());
209         user.setAddress (address);
210         
211         
212         Collection friends=user.getFriends();
213         Iterator friendsIt=friends.iterator();
214         
215         //remove the old instances of friends
216
while (friendsIt.hasNext()){
217             FriendLocal friendsLoc=(FriendLocal)friendsIt.next();
218             friendsIt.remove();
219             friendsLoc.remove();
220         }
221         
222         //load the new friends
223
String[] newFriends=userValue.getFriendsListSelected();
224         for (int i=0; i<newFriends.length;i++){
225             friends.add (friendsHome.create (userHome.findByUsername (newFriends[i])));
226         }
227         user.setFriends (friends);
228     }
229     
230     /**
231      * Return the user's DTO when the username is passed to it
232      *
233      * @param username The username of the user
234      * @return The DTO for the user
235      *
236      * @ejb.interface-method
237      *
238      */

239     public UserValue getUserValue (String username) throws Exception {
240         EJBHomeFactory factory = EJBHomeFactory.getInstance();
241         UserLocalHome userHome=(UserLocalHome) factory.getLocalHome (EJBHomeFactory.USER);
242         UserLocal user = userHome.findByUsername (username);
243         UserValue userValue= UserToDTO (user);
244         return userValue;
245     }
246     
247     /**
248      *
249      *
250      * Creates a new user entity bean
251      *
252      * @param createUserValue The DTO for the User
253      * @param addressValue The DTO for the User's Address
254      *
255      * @ejb.interface-method
256      *
257      */

258     public void CreateUser (CreateUserValue createUserValue, AddressValue addressValue) throws Exception{
259         EJBHomeFactory factory=EJBHomeFactory.getInstance();
260         AddressLocalHome addressHome=null;
261         addressHome=(AddressLocalHome)factory.getLocalHome(EJBHomeFactory.ADDRESS);
262         UserLocalHome userHome=(UserLocalHome)factory.getLocalHome(EJBHomeFactory.USER);
263         UserLocal user=userHome.create ( createUserValue.getUsername(), "customer",
264                 createUserValue.getLname(), createUserValue.getFname(), createUserValue.getEmailAdd(),
265                 createUserValue.getPhoneNum(), createUserValue.getPasswd1());
266         AddressLocal address=addressHome.create ( addressValue.getCountry(), addressValue.getCity(),
267                 addressValue.getPostalCode(), addressValue.getStreet1(), addressValue.getStreet2(),
268                 addressValue.getProvince());
269         user.setAddress (address);
270     }
271     
272     
273     
274     
275     
276 }
277
Popular Tags