1 12 13 package freeforums.user; 14 15 import java.rmi.RemoteException ; 16 import javax.ejb.*; 17 import javax.naming.*; 18 import javax.rmi.*; 19 import java.rmi.ServerException ; 20 import java.util.Collection ; 21 import java.util.List ; 22 23 import org.jdom.*; 24 25 public class UserManagerBean implements SessionBean { 26 27 31 private Object getHome (String path) { 32 try { 33 return (new InitialContext().lookup(path)); 34 } 35 catch (NamingException e) { 36 System.err.println("An exception occured " + e); 37 return null; 38 } 39 } 40 41 44 public UserManagerBean() {} 45 48 public void ejbCreate() {} 49 52 public void ejbRemove() {} 53 56 public void ejbActivate() {} 57 60 public void ejbPassivate() {} 61 64 public void setSessionContext(SessionContext sc) {} 65 66 public Document modifyUser(Document userDetails) throws RemoteException { 67 68 73 74 String userName,password,realName,emailAddress; 75 boolean superUser; 76 77 Element userListElement = userDetails.getRootElement(); 78 Element responseElement = new Element("Response"); 79 80 82 List userElementList = userListElement.getChildren(); 83 Element [] userElements = (Element[])userElementList.toArray(new Element[0]); 84 85 int counter=0; 86 87 UserHome home = (UserHome) getHome("java:comp/env/ejb/User"); 88 89 userName = (userElements[counter].getChild("UserName")).getText(); 90 password = (userElements[counter].getChild("Password")).getText(); 91 realName = (userElements[counter].getChild("RealName")).getText(); 92 emailAddress = (userElements[counter].getChild("EmailAddress")).getText(); 93 94 if((userElements[counter].getChild("SuperUser")).getText().equals("Yes")) superUser=true; 95 else superUser=false; 96 97 try { 98 User existingUser = home.findByPrimaryKey(userName); 99 existingUser.setPassword(password); 100 existingUser.setRealName(realName); 101 existingUser.setEmailAddress(emailAddress); 102 existingUser.setSuperUser(superUser); 103 responseElement.addContent("Done"); 104 } 105 catch(Exception e) { 106 responseElement.addContent("Failed"); 108 } 109 110 return (new Document(responseElement)); 111 } 112 113 118 119 public Document addUser(Document userDetails) throws RemoteException { 120 121 124 125 String userName,password,realName,emailAddress; 126 boolean superUser; 127 128 Element userListElement = userDetails.getRootElement(); 129 Element responseElement = new Element("Response"); 130 131 133 List userElementList = userListElement.getChildren(); 134 Element [] userElements = (Element[])userElementList.toArray(new Element[0]); 135 136 UserHome home = (UserHome) getHome("java:comp/env/ejb/User"); 137 138 int counter=0; 139 140 142 userName = (userElements[counter].getChild("UserName")).getText(); 143 password = (userElements[counter].getChild("Password")).getText(); 144 realName = (userElements[counter].getChild("RealName")).getText(); 145 emailAddress = (userElements[counter].getChild("EmailAddress")).getText(); 146 147 if((userElements[counter].getChild("SuperUser")).getText().equals("Yes")) superUser=true; 148 else superUser=false; 149 150 try { 151 User existingUser = home.findByPrimaryKey(userName); 152 String oldUserName = existingUser.getUserName(); System.out.println("Could not complete request \n User exists " + oldUserName); 155 responseElement.addContent("Duplicate"); 156 } 157 160 catch (Exception e) { 161 System.out.println("Added new user with the userName = " + userName); 162 try { 163 User user = home.create(userName); 164 user.setPassword(password); 165 user.setRealName(realName); 166 user.setEmailAddress(emailAddress); 167 user.setSuperUser(superUser); 168 responseElement.addContent("Done"); 169 } 170 catch (Exception ex) { 171 responseElement.addContent("Failed"); 172 } 173 } 174 175 return (new Document(responseElement)); 176 177 } 178 179 182 183 public Document getUser(Document userDocument) throws RemoteException { 184 185 Element rootElement = userDocument.getRootElement(); 186 String username = rootElement.getText(); 187 188 UserHome home = (UserHome) getHome("java:comp/env/ejb/User"); 189 try { 190 User existingUser = home.findByPrimaryKey(username); 191 Element user = new Element("User"); 192 193 user.addContent(new Element("UserName").addContent(existingUser.getUserName())); 194 user.addContent(new Element("Password").addContent(existingUser.getPassword())); 195 user.addContent(new Element("RealName").addContent(existingUser.getRealName())); 196 user.addContent(new Element("EmailAddress").addContent(existingUser.getEmailAddress())); 197 198 if(existingUser.isSuperUser()) user.addContent(new Element("SuperUser").addContent("Yes")); 199 else user.addContent(new Element("SuperUser").addContent("No")); 200 201 Element rootResponseElement = new Element("UserList"); 202 rootResponseElement.addContent(user); 203 204 return (new Document(rootResponseElement)); 205 206 } 207 catch(Exception e) { 208 Element rootResponseElement = new Element("Response"); 209 rootResponseElement.addContent("Not Found"); 210 return (new Document(rootResponseElement)); 211 } 212 } 213 214 220 221 public Document loginUser(Document loginDocument) throws RemoteException { 222 223 UserHome home = (UserHome) getHome("java:comp/env/ejb/User"); 224 225 Element userListElement = loginDocument.getRootElement(); 226 List userElementList = userListElement.getChildren(); 227 Element [] userElements = (Element[])userElementList.toArray(new Element[0]); 228 229 String userName = userElements[0].getChild("UserName").getText(); 230 String password = userElements[0].getChild("Password").getText(); 231 232 Element responseElement = new Element("Response"); 233 234 try { 235 User existingUser = home.findByPrimaryKey(userName); if(existingUser.getPassword().equals(password)) 237 return getUser(new Document(new Element("UserName").addContent(userName))); 238 else responseElement.addContent("Failed"); 239 } 240 catch(Exception e) { 241 System.out.println("Could not complete request \n" + e); 242 responseElement.addContent("Failed"); 243 } 244 245 return (new Document(responseElement)); 246 247 } 248 249 255 256 public Document addUserToForum(Document userDetails) throws RemoteException { 257 258 Element rootElement = userDetails.getRootElement(); 259 Element responseElement = new Element("Response"); 260 261 List userElementList = rootElement.getChildren(); 262 Element [] userElements = (Element[])userElementList.toArray(new Element[0]); 263 264 String userName = userElements[0].getChild("UserName").getText(); 265 Integer forumID = new Integer (userElements[0].getChild("ForumID").getText()); 266 String adminstatus = userElements[0].getChild("Administrator").getText(); 267 268 boolean administrator = false; 269 270 if(adminstatus.equals("Yes")) administrator = true; 271 272 ForumUserHome home = (ForumUserHome) getHome("java:comp/env/ejb/ForumUser"); 273 274 try { 275 ForumUser existingForumUser = home.findByPrimaryKey(new ForumUserPK(userName, forumID)); 276 String existingUserName = existingForumUser.getUserName(); 277 responseElement.addContent("Done"); 278 System.out.println("User " + userName + " allowed to use " + forumID); 279 } 280 catch(Exception e) { 281 try { 282 ForumUser tempForumUser = home.create(new ForumUserPK(userName, forumID)); 283 tempForumUser.setAdministrator(administrator); 284 responseElement.addContent("Done"); 285 System.out.println("User " + userName + " allowed to use " + forumID); 286 } 287 catch (Exception ex) { 288 responseElement.addContent("Failed"); 289 System.out.println("Failed - User " + userName + " allowed to use " + forumID); 290 } 291 } 292 293 return (new Document(responseElement)); 294 } 295 296 299 300 public Document removeUserFromForum(Document userDetails) throws RemoteException { 301 302 Element rootElement = userDetails.getRootElement(); 303 Element responseElement = new Element("Response"); 304 305 List userElementList = rootElement.getChildren(); 306 Element [] userElements = (Element[])userElementList.toArray(new Element[0]); 307 308 String userName = userElements[0].getChild("UserName").getText(); 309 Integer forumID = new Integer (userElements[0].getChild("ForumID").getText()); 310 311 try { 312 ForumUserHome home = (ForumUserHome) getHome("java:comp/env/ejb/ForumUser"); 313 ForumUser existingForumUser = home.findByPrimaryKey(new ForumUserPK(userName, forumID)); 314 existingForumUser.remove(); 315 responseElement.addContent("Done"); 316 } 317 catch(Exception e) { 318 responseElement.addContent("Failed"); 319 } 320 321 return (new Document(responseElement)); 322 } 323 324 325 332 333 public Document findAll() throws RemoteException , FinderException { 334 335 UserHome home = (UserHome) getHome("java:comp/env/ejb/User"); 336 Collection all = home.findAll(); 337 User [] userArray = (User[]) all.toArray(new User[0]); 338 DocType dtd = new DocType("UserList","http://devraj.org/dtd/freeforums/User.dtd"); 339 340 Element root = new Element("UserList"); 341 342 for(int counter=0; counter<userArray.length; counter++) { 343 344 Element user = new Element("User"); 345 User currentUser = userArray[counter]; 346 347 user.addContent(new Element("UserName").addContent(currentUser.getUserName())); 348 user.addContent(new Element("Password").addContent(currentUser.getPassword())); 349 user.addContent(new Element("RealName").addContent(currentUser.getRealName())); 350 user.addContent(new Element("EmailAddress").addContent(currentUser.getEmailAddress())); 351 352 if(currentUser.isSuperUser()) user.addContent(new Element("SuperUser").addContent("Yes")); 353 else user.addContent(new Element("SuperUser").addContent("No")); 354 355 root.addContent(user); 356 357 } 358 359 return (new Document(root,dtd)); 360 } 361 362 363 } | Popular Tags |