KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freeforums > forum > ForumManagerBean


1 /*
2  * ForumManagerBean.java
3  *
4  * Forum Manager bean represent functions for
5  * managing the forums
6  *
7  * @author: Devraj Mukherjee
8  *
9  * Created: 07th July 2001
10  *
11  */

12
13  package freeforums.forum;
14
15  import java.rmi.RemoteException JavaDoc;
16  import javax.ejb.*;
17  import javax.naming.*;
18  import javax.rmi.*;
19  import java.rmi.ServerException JavaDoc;
20  import java.util.Collection JavaDoc;
21  import java.util.Vector JavaDoc;
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 JavaDoc;
32
33  public class ForumManagerBean implements SessionBean {
34
35      /**
36      Helper function to get the home interface of a specified Bean. This is used by
37      most of the methods in this class.
38      */

39      private Object JavaDoc getHome (String JavaDoc 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      /**
50      Empty method body
51      */

52      public ForumManagerBean() {}
53      /**
54      Empty method body
55      */

56      public void ejbCreate() {}
57      /**
58      Empty method body
59      */

60      public void ejbRemove() {}
61      /**
62      Empty method body
63      */

64      public void ejbActivate() {}
65      /**
66      Empty method body
67      */

68      public void ejbPassivate() {}
69      /**
70      Empty method body
71      */

72      public void setSessionContext(SessionContext sc) {}
73
74      public Document addForum(Document forumDetails) throws RemoteException JavaDoc {
75
76
77          Element RootElement = forumDetails.getRootElement();
78
79          List JavaDoc forumListElement = RootElement.getChildren();
80          Element [] forumElements = (Element[])forumListElement.toArray(new Element[0]);
81
82          String JavaDoc forumName = forumElements[0].getChild("ForumName").getText();
83          String JavaDoc forumDescription = forumElements[0].getChild("ForumDescription").getText();
84
85          Element responseElement = new Element("Response");
86          String JavaDoc identifier = "Forum";
87          Integer JavaDoc id=null;
88
89          try {
90
91              /**
92               Process of creating AutoNumbers
93               **/

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 JavaDoc e) {
100              /**
101               This Key was not found in the storage so
102               create a new one with this name
103               **/

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 JavaDoc exp) {
110                   /**
111                    Could not create this primary key
112                    **/

113                }
114
115           }
116
117           if (id==null) return (new Document(responseElement.addContent("Failed")));
118
119           try {
120
121               /**
122               Process of creating the Forum Object
123               **/

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 JavaDoc e) {
131              //do nothing
132
System.out.println("Exception occured while creating Forum \n" + e);
133              return (new Document(responseElement.addContent("Failed")));
134          }
135      }
136
137      /*
138       * Modified version returns XML document description of Forum List
139       */

140
141       public Document findAll() throws RemoteException JavaDoc, FinderException {
142
143           ForumHome home = (ForumHome) getHome("java:comp/env/ejb/Forum");
144           Collection JavaDoc 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       /*
170        * Find Forum method, finds a method given it's primary key
171        */

172
173        public Document findForum(Document forumID) throws RemoteException JavaDoc, FinderException {
174
175            Integer JavaDoc forumid = new Integer JavaDoc(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 JavaDoc e) {
192                return (new Document(new Element("Response").addContent("Failed")));
193            }
194        }
195
196       /*
197        * Find Forum method, finds a method given it's primary key
198        */

199
200        private Element findForumAsElement(Document forumID) throws RemoteException JavaDoc, FinderException {
201
202            Integer JavaDoc forumid = new Integer JavaDoc(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 JavaDoc e) {
219                return (new Element("Response").addContent("Failed"));
220            }
221        }
222
223        /*
224         * Returns an Array of Forum object for a given username
225         */

226
227       public Document findForumsForUser(Document userDetails) throws RemoteException JavaDoc, FinderException {
228
229           Element RootElement = userDetails.getRootElement();
230           List JavaDoc UserListElement = RootElement.getChildren();
231           Element [] UserElements = (Element[])UserListElement.toArray(new Element[0]);
232
233           String JavaDoc username = (UserElements[0].getChild("UserName").getText());
234           String JavaDoc 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 JavaDoc 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 JavaDoc e) {
271               //something happened on the way to heaven
272
System.out.println("Exception occurred while finding forums\n" + e);
273               return (new Document(new Element("Response").addContent("Failed")));
274           }
275
276       }
277
278       /*
279        * Is user allowed to use a particular forum
280        */

281
282       public Document isUserAllowed(Document userDetails) throws RemoteException JavaDoc, FinderException {
283
284           Element rootElement = userDetails.getRootElement();
285
286           Element responseElement = new Element("Response");
287
288           String JavaDoc username = rootElement.getChild("UserName").getText();
289           Integer JavaDoc forumID = new Integer JavaDoc((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 JavaDoc e) {
298               //something happened on the way to heaven
299
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  } // end of class file
308
Popular Tags