KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freeforums > user > UserManagerBean


1 /*
2  * UserManagerBean.java
3  *
4  * Session bean that represents the User Management
5  * facilities for the FreeForums System.
6  *
7  * @author: Devraj Mukherjee
8  *
9  * Created: 24th June 2001
10  *
11  */

12
13  package freeforums.user;
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.List JavaDoc;
22
23  import org.jdom.*;
24
25  public class UserManagerBean implements SessionBean {
26
27      /**
28      Helper function to get the home interface of a specified Bean. This is used by
29      most of the methods in this class.
30      */

31      private Object JavaDoc getHome (String JavaDoc 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      /**
42      Empty method body
43      */

44      public UserManagerBean() {}
45      /**
46      Empty method body
47      */

48      public void ejbCreate() {}
49      /**
50      Empty method body
51      */

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

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

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

64      public void setSessionContext(SessionContext sc) {}
65
66      public Document modifyUser(Document userDetails) throws RemoteException JavaDoc {
67
68          /*****
69           * Parse XML data
70           *
71           * This modifyUser method now supports multiple users
72           *****/

73
74          String JavaDoc userName,password,realName,emailAddress;
75          boolean superUser;
76
77          Element userListElement = userDetails.getRootElement();
78          Element responseElement = new Element("Response");
79
80          // Extract the User Elements
81

82          List JavaDoc 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 JavaDoc e) {
106              // error occured
107
responseElement.addContent("Failed");
108          }
109
110          return (new Document(responseElement));
111      }
112
113      /**************
114       *
115       * Method to add new usrs
116       *
117       **************/

118
119      public Document addUser(Document userDetails) throws RemoteException JavaDoc {
120
121          /*****
122           * Parse XML data
123           *****/

124
125          String JavaDoc userName,password,realName,emailAddress;
126          boolean superUser;
127
128          Element userListElement = userDetails.getRootElement();
129          Element responseElement = new Element("Response");
130
131          // Extract the User Elements
132

133          List JavaDoc 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          // Get all the details off the user element
141

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 JavaDoc oldUserName = existingUser.getUserName(); // Force loading of data here
153
// If we get here, the user is already in the collection
154
System.out.println("Could not complete request \n User exists " + oldUserName);
155              responseElement.addContent("Duplicate");
156         }
157         // The database does not have an User with the same username
158
// so it is now safe to try and add a new user
159

160         catch (Exception JavaDoc 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 JavaDoc ex) {
171                 responseElement.addContent("Failed");
172             }
173         }
174
175         return (new Document(responseElement));
176
177      }
178
179      /*
180       * Return a user object
181       */

182
183       public Document getUser(Document userDocument) throws RemoteException JavaDoc {
184
185          Element rootElement = userDocument.getRootElement();
186          String JavaDoc 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 JavaDoc e) {
208              Element rootResponseElement = new Element("Response");
209              rootResponseElement.addContent("Not Found");
210              return (new Document(rootResponseElement));
211          }
212      }
213
214      /*
215       * Login process for a user
216       *
217       * Check for the existance of the user account and then check the password.
218       *
219       */

220
221      public Document loginUser(Document loginDocument) throws RemoteException JavaDoc {
222
223          UserHome home = (UserHome) getHome("java:comp/env/ejb/User");
224
225          Element userListElement = loginDocument.getRootElement();
226          List JavaDoc userElementList = userListElement.getChildren();
227          Element [] userElements = (Element[])userElementList.toArray(new Element[0]);
228
229          String JavaDoc userName = userElements[0].getChild("UserName").getText();
230          String JavaDoc password = userElements[0].getChild("Password").getText();
231
232          Element responseElement = new Element("Response");
233
234          try {
235              User existingUser = home.findByPrimaryKey(userName); // find the user
236
if(existingUser.getPassword().equals(password))
237              return getUser(new Document(new Element("UserName").addContent(userName)));
238              else responseElement.addContent("Failed");
239          }
240          catch(Exception JavaDoc 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      /*
250       * add a user to a forum
251       *
252       * if the user already is subscribed to that forum then
253       * this method will return false, else it will return true.
254       */

255
256      public Document addUserToForum(Document userDetails) throws RemoteException JavaDoc {
257
258          Element rootElement = userDetails.getRootElement();
259          Element responseElement = new Element("Response");
260
261          List JavaDoc userElementList = rootElement.getChildren();
262          Element [] userElements = (Element[])userElementList.toArray(new Element[0]);
263
264          String JavaDoc userName = userElements[0].getChild("UserName").getText();
265          Integer JavaDoc forumID = new Integer JavaDoc(userElements[0].getChild("ForumID").getText());
266          String JavaDoc 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 JavaDoc existingUserName = existingForumUser.getUserName();
277              responseElement.addContent("Done");
278              System.out.println("User " + userName + " allowed to use " + forumID);
279          }
280          catch(Exception JavaDoc 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 JavaDoc 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      /***
297       * remove a user from a forum
298       ***/

299
300      public Document removeUserFromForum(Document userDetails) throws RemoteException JavaDoc {
301
302          Element rootElement = userDetails.getRootElement();
303          Element responseElement = new Element("Response");
304
305          List JavaDoc userElementList = rootElement.getChildren();
306          Element [] userElements = (Element[])userElementList.toArray(new Element[0]);
307
308          String JavaDoc userName = userElements[0].getChild("UserName").getText();
309          Integer JavaDoc forumID = new Integer JavaDoc(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 JavaDoc e) {
318              responseElement.addContent("Failed");
319          }
320
321          return (new Document(responseElement));
322      }
323
324
325      /*****
326       *
327       * findAll()
328       *
329       * Creates a XML representation of the User List based on User.dtd
330       *
331       *****/

332
333       public Document findAll() throws RemoteException JavaDoc, FinderException {
334
335           UserHome home = (UserHome) getHome("java:comp/env/ejb/User");
336           Collection JavaDoc 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  } // this bracket ends the class file.
Popular Tags