KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freeforums > jspbeans > UserController


1 /****
2  * UserController.java
3  *
4  * JavaBean to allow the JSP pages to perform the right
5  * user functions.
6  *
7  * @author: Devraj Mukherjee
8  *
9  ****/

10
11 package freeforums.jspbeans;
12
13 import javax.naming.*;
14 import java.util.Hashtable JavaDoc;
15 import javax.rmi.PortableRemoteObject JavaDoc;
16 import java.util.Properties JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 import freeforums.user.UserHome;
21 import freeforums.user.User;
22 import freeforums.user.UserManagerHome;
23 import freeforums.user.UserManager;
24
25 import java.util.List JavaDoc;
26
27 import org.jdom.*;
28
29 public class UserController extends Object JavaDoc {
30
31     /*
32      * Default Constructor does not have to initialize any instance vars
33      */

34
35     public void UserController() {
36
37         // Blank Constructor do nothing
38

39     }
40
41     public boolean isSuperUser(Document UserDocument) {
42
43          Element userListElement = UserDocument.getRootElement();
44          List JavaDoc userElementList = userListElement.getChildren();
45          Element [] userElements = (Element[])userElementList.toArray(new Element[0]);
46
47          String JavaDoc SuperUser = userElements[0].getChild("SuperUser").getText();
48
49          if(SuperUser.equals("Yes")) return true; // Is a Superuser
50
return false; // Not a superuser
51

52     }
53
54     public Document authenticateSession(Document userDocument) {
55
56         /*****
57          *
58          * This method will authenticate the session passed on as a userDocument
59          * this method can be modified for tighter secruity be verifying the user
60          * using the Session Beans
61          *
62          *****/

63
64          Element userListElement = userDocument.getRootElement();
65          List JavaDoc userElementList = userListElement.getChildren();
66          Element [] userElements = (Element[])userElementList.toArray(new Element[0]);
67
68          String JavaDoc userName = userElements[0].getChild("UserName").getText();
69          String JavaDoc password = userElements[0].getChild("Password").getText();
70
71          Element responseElement = new Element("Response");
72
73          if(userName.equals("") || password.equals("")) responseElement.addContent("Not Verified");
74          else responseElement.addContent("Verified");
75
76          return (new Document(responseElement));
77
78     }
79
80     public Document getUserList() {
81
82         try {
83             //Get the home interface for the user management bean
84
UserManagerHome home = (UserManagerHome) new InitialContext().lookup("UserManagerBean");
85             // Create the manager object
86
UserManager manager = home.create();
87             return manager.findAll();
88         }
89         catch(Exception JavaDoc e) {
90             // do nothing much
91
return null;
92         }
93
94     }
95
96     public Document addUser(Document xmlUserList) {
97
98         try {
99             //Get the home interface for the user management bean
100
UserManagerHome home = (UserManagerHome) new InitialContext().lookup("UserManagerBean");
101             // Create the manager object
102
UserManager manager = home.create();
103             return manager.addUser(xmlUserList);
104         }
105         catch(Exception JavaDoc e) {
106             // do nothing much
107
return (new Document(new Element("Response").addContent("Failed")));
108         }
109
110     }
111
112     public Document loginUser(Document loginDocument) {
113
114         try {
115             //Get the home interface for the user management bean
116
UserManagerHome home = (UserManagerHome) new InitialContext().lookup("UserManagerBean");
117             // Create the manager object
118
UserManager manager = home.create();
119             return manager.loginUser(loginDocument);
120         }
121         catch(Exception JavaDoc e) {
122             // do nothing much
123
Element responseElement = new Element("Response");
124             responseElement.addContent("Failed");
125             return (new Document(responseElement));
126         }
127
128     }
129
130     public Document getUserDetails(Document loginDocument) {
131
132         Element UserListElement = loginDocument.getRootElement();
133         List JavaDoc UserElementList = UserListElement.getChildren();
134         Element [] UserElements = (Element[])UserElementList.toArray(new Element[0]);
135
136         String JavaDoc UserName = UserElements[0].getChild("UserName").getText();
137
138         try {
139             //Get the home interface for the user management bean
140
UserManagerHome home = (UserManagerHome) new InitialContext().lookup("UserManagerBean");
141             // Create the manager object
142
UserManager manager = home.create();
143             return manager.getUser(new Document(new Element("UserName").addContent(UserName)));
144         }
145         catch(Exception JavaDoc e) {
146             // do nothing much
147
Element responseElement = new Element("Response");
148             responseElement.addContent("Failed");
149             return (new Document(responseElement));
150         }
151     }
152
153     // Bridge method to
154

155     public Document modifyUserDetails(Document UserDocument) {
156
157         try {
158             //Get the home interface for the user management bean
159
UserManagerHome home = (UserManagerHome) new InitialContext().lookup("UserManagerBean");
160             // Create the manager object
161
UserManager manager = home.create();
162             return manager.modifyUser(UserDocument);
163         }
164         catch(Exception JavaDoc e) {
165             // do nothing much
166
Element responseElement = new Element("Response");
167             responseElement.addContent("Failed");
168             return (new Document(responseElement));
169         }
170
171     }
172
173     // Bridge method to addUserToForum
174

175     public Document addUserToForum(Document UserDocument) {
176
177         try {
178             //Get the home interface for the user management bean
179
UserManagerHome home = (UserManagerHome) new InitialContext().lookup("UserManagerBean");
180             // Create the manager object
181
UserManager manager = home.create();
182             return manager.addUserToForum(UserDocument);
183         }
184         catch(Exception JavaDoc e) {
185             // do nothing much
186
Element responseElement = new Element("Response");
187             responseElement.addContent("Failed");
188             return (new Document(responseElement));
189         }
190
191     }
192
193 } // end class file here
194

195
Popular Tags