1 27 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 82 83 public abstract class UserHelperBean implements SessionBean { 84 SessionContext ejbContext; 85 86 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 114 115 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 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 c.removeAll (friendsList); 157 158 c.remove(user.getUsername()); 160 161 return c; 162 } 163 164 165 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 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 while (friendsIt.hasNext()){ 217 FriendLocal friendsLoc=(FriendLocal)friendsIt.next(); 218 friendsIt.remove(); 219 friendsLoc.remove(); 220 } 221 222 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 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 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 |