1 12 13 package freeforums.forum; 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.Vector ; 22 23 import freeforums.util.AutoNumberHome; 24 import freeforums.util.AutoNumber; 25 26 import freeforums.user.ForumUser; 27 import freeforums.user.ForumUserHome; 28 import freeforums.user.ForumUserPK; 29 30 import org.jdom.*; 31 import java.util.List ; 32 33 public class ForumManagerBean implements SessionBean { 34 35 39 private Object getHome (String path) { 40 try { 41 return (new InitialContext().lookup(path)); 42 } 43 catch (NamingException e) { 44 System.err.println("An exception occured " + e); 45 return null; 46 } 47 } 48 49 52 public ForumManagerBean() {} 53 56 public void ejbCreate() {} 57 60 public void ejbRemove() {} 61 64 public void ejbActivate() {} 65 68 public void ejbPassivate() {} 69 72 public void setSessionContext(SessionContext sc) {} 73 74 public Document addForum(Document forumDetails) throws RemoteException { 75 76 77 Element RootElement = forumDetails.getRootElement(); 78 79 List forumListElement = RootElement.getChildren(); 80 Element [] forumElements = (Element[])forumListElement.toArray(new Element[0]); 81 82 String forumName = forumElements[0].getChild("ForumName").getText(); 83 String forumDescription = forumElements[0].getChild("ForumDescription").getText(); 84 85 Element responseElement = new Element("Response"); 86 String identifier = "Forum"; 87 Integer id=null; 88 89 try { 90 91 94 AutoNumberHome ANHome = (AutoNumberHome) getHome("java:comp/env/ejb/AutoNumber"); 95 AutoNumber AutoNumberFactory = ANHome.findByPrimaryKey(identifier); 96 id = AutoNumberFactory.getAutoNumberValue(); 97 98 } 99 catch (Exception e) { 100 104 try { 105 AutoNumberHome ANHome = (AutoNumberHome) getHome("java:comp/env/ejb/AutoNumber"); 106 AutoNumber AutoNumberFactory = ANHome.create(identifier); 107 id = AutoNumberFactory.getAutoNumberValue(); 108 } 109 catch(Exception exp) { 110 113 } 114 115 } 116 117 if (id==null) return (new Document(responseElement.addContent("Failed"))); 118 119 try { 120 121 124 125 ForumHome home = (ForumHome) getHome("java:comp/env/ejb/Forum"); 126 Forum forum = home.create(id,forumName,forumDescription); 127 return (new Document(responseElement.addContent("Done"))); 128 129 } 130 catch(Exception e) { 131 System.out.println("Exception occured while creating Forum \n" + e); 133 return (new Document(responseElement.addContent("Failed"))); 134 } 135 } 136 137 140 141 public Document findAll() throws RemoteException , FinderException { 142 143 ForumHome home = (ForumHome) getHome("java:comp/env/ejb/Forum"); 144 Collection all = home.findAll(); 145 Forum [] forumArray = (Forum[]) all.toArray(new Forum[0]); 146 147 Element rootElement = new Element("ForumList"); 148 149 for(int counter=0; counter<forumArray.length; counter++) { 150 151 Forum currentForum = forumArray[counter]; 152 153 Element ForumIDElement = new Element("ForumID").addContent((currentForum.getForumID()).toString()); 154 Element ForumNameElement = new Element("ForumName").addContent(currentForum.getForumName()); 155 Element ForumDescriptionElement = new Element("ForumDescription").addContent(currentForum.getForumDescription()); 156 157 Element ForumElement = new Element("Forum"); 158 ForumElement.addContent(ForumIDElement); 159 ForumElement.addContent(ForumNameElement); 160 ForumElement.addContent(ForumDescriptionElement); 161 162 rootElement.addContent(ForumElement); 163 164 } 165 166 return (new Document(rootElement)); 167 } 168 169 172 173 public Document findForum(Document forumID) throws RemoteException , FinderException { 174 175 Integer forumid = new Integer (forumID.getRootElement().getText()); 176 177 try { 178 ForumHome home = (ForumHome) getHome("java:comp/env/ejb/Forum"); 179 Forum currentForum = home.findByPrimaryKey(forumid); 180 Element ForumIDElement = new Element("ForumID").addContent((currentForum.getForumID()).toString()); 181 Element ForumNameElement = new Element("ForumName").addContent(currentForum.getForumName()); 182 Element ForumDescriptionElement = new Element("ForumDescription").addContent(currentForum.getForumDescription()); 183 Element ForumElement = new Element("Forum"); 184 185 ForumElement.addContent(ForumIDElement); 186 ForumElement.addContent(ForumNameElement); 187 ForumElement.addContent(ForumDescriptionElement); 188 189 return (new Document(ForumElement)); 190 } 191 catch(Exception e) { 192 return (new Document(new Element("Response").addContent("Failed"))); 193 } 194 } 195 196 199 200 private Element findForumAsElement(Document forumID) throws RemoteException , FinderException { 201 202 Integer forumid = new Integer (forumID.getRootElement().getText()); 203 204 try { 205 ForumHome home = (ForumHome) getHome("java:comp/env/ejb/Forum"); 206 Forum currentForum = home.findByPrimaryKey(forumid); 207 Element ForumIDElement = new Element("ForumID").addContent((currentForum.getForumID()).toString()); 208 Element ForumNameElement = new Element("ForumName").addContent(currentForum.getForumName()); 209 Element ForumDescriptionElement = new Element("ForumDescription").addContent(currentForum.getForumDescription()); 210 Element ForumElement = new Element("Forum"); 211 212 ForumElement.addContent(ForumIDElement); 213 ForumElement.addContent(ForumNameElement); 214 ForumElement.addContent(ForumDescriptionElement); 215 216 return (ForumElement); 217 } 218 catch(Exception e) { 219 return (new Element("Response").addContent("Failed")); 220 } 221 } 222 223 226 227 public Document findForumsForUser(Document userDetails) throws RemoteException , FinderException { 228 229 Element RootElement = userDetails.getRootElement(); 230 List UserListElement = RootElement.getChildren(); 231 Element [] UserElements = (Element[])UserListElement.toArray(new Element[0]); 232 233 String username = (UserElements[0].getChild("UserName").getText()); 234 String superuser = (UserElements[0].getChild("SuperUser").getText()); 235 236 boolean adminStatus = false; 237 238 if(superuser.equals("Yes")) adminStatus = true; 239 240 int ForumCounter = 0; 241 242 Element rootElement = new Element("ForumList"); 243 244 try { 245 ForumUserHome home = (ForumUserHome) getHome("java:comp/env/ejb/ForumUser"); 246 Collection user_forums = home.findAll(); 247 ForumUser[] user_forums_array = (ForumUser[]) user_forums.toArray(new ForumUser[0]); 248 249 for(int counter=0; counter<user_forums_array.length; counter++) { 250 if(user_forums_array[counter].getUserName().equals(username)) { 251 Element tempForum = findForumAsElement(new Document(new Element("ForumID").addContent(user_forums_array[counter].getForumID().toString()))); 252 if(adminStatus==true) { 253 if(user_forums_array[counter].isAdministrator()) { 254 rootElement.addContent(tempForum); 255 ForumCounter++; 256 } 257 258 } 259 else { 260 rootElement.addContent(tempForum); 261 ForumCounter++; 262 } 263 } 264 } 265 266 System.out.println("Found " + ForumCounter + " forums for " + username); 267 268 return (new Document(rootElement)); 269 } 270 catch(Exception e) { 271 System.out.println("Exception occurred while finding forums\n" + e); 273 return (new Document(new Element("Response").addContent("Failed"))); 274 } 275 276 } 277 278 281 282 public Document isUserAllowed(Document userDetails) throws RemoteException , FinderException { 283 284 Element rootElement = userDetails.getRootElement(); 285 286 Element responseElement = new Element("Response"); 287 288 String username = rootElement.getChild("UserName").getText(); 289 Integer forumID = new Integer ((rootElement.getChild("ForumID")).getText()); 290 291 try { 292 ForumUserHome home = (ForumUserHome) getHome("java:comp/env/ejb/ForumUser"); 293 ForumUser isAllowed = home.findByPrimaryKey(new ForumUserPK(username, forumID)); 294 if (isAllowed == null) responseElement.addContent("Failed"); 295 else responseElement.addContent("Done"); 296 } 297 catch(Exception e) { 298 System.out.println("Exception occurred while finding forums\n" + e); 300 responseElement.addContent("Failed"); 301 } 302 303 return (new Document(responseElement)); 304 305 } 306 307 } | Popular Tags |