KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > espada > bugtracker > app > User


1
2 package com.espada.bugtracker.app;
3
4 // Java core API
5
import java.io.*;
6 import java.sql.*;
7 import java.util.*;
8
9 // Espada API
10
import com.espada.*;
11
12 // Espada API
13
import org.webmacro.*;
14 import javax.servlet.*;
15
16 // DB Connection Pool API
17
import com.espada.bugtracker.persistence.*;
18
19
20 /**
21 * This is the User class.
22 *
23 * When an instance of User is created, the class searches the database for an<BR>
24 * existing user with the corresponding user id, and fills up the object properties.<BR>
25 *
26 * <b>Currently not implemented! I add users manually for now ... </B><BR>
27 * To create a new user in the database, the static method User.createUser() must be used.<BR>
28 *
29 *
30 * @version: $Id: User.java,v 1.11 2001/04/17 12:43:02 manik Exp $<BR>
31 * @author: Manik Surtani<BR>
32 **/

33
34 public class User
35 {
36
37 public int uid;
38
39 public int rid;
40
41 public int pid;
42
43 public String JavaDoc username;
44
45 public String JavaDoc password;
46
47 public String JavaDoc email;
48
49 public boolean admin=false;
50
51 public boolean found=false;
52
53   public User(String JavaDoc un)
54   {
55         try
56         {
57             
58             Connection d = DatabaseConnectionPool.getConnection();
59             Statement st = d.createStatement();
60             ResultSet rs = st.executeQuery("select * from user where username='" + un + "'");
61             
62             while (rs.next())
63             {
64                      uid = rs.getInt(1);
65
66                      username = rs.getString(2);
67
68                      password = rs.getString(3);
69
70                      email = rs.getString(4);
71
72                      admin = rs.getInt(5)==0?false:true;
73
74                      found = true;
75
76             }
77
78             st.close();
79             DatabaseConnectionPool.freeConnection(d);
80
81         }
82
83         catch (Exception JavaDoc E)
84         {
85                 
86                 found = false;
87         }
88
89   } //end of method User
90

91
92   public User(int uidn)
93   {
94         try
95         {
96          
97              Connection d = DatabaseConnectionPool.getConnection();
98              Statement st = d.createStatement();
99              ResultSet rs = st.executeQuery("select * from user where uid=" + uidn);
100              
101              while (rs.next())
102              {
103                       uid = rs.getInt(1);
104
105                       username = rs.getString(2);
106
107                       password = rs.getString(3);
108
109                       email = rs.getString(4);
110
111                       found = true;
112
113              }
114
115             st.close();
116             DatabaseConnectionPool.freeConnection(d);
117
118         }
119
120         catch (Exception JavaDoc E)
121         {
122                 
123                 found = false;
124
125         }
126
127   } //end of method User
128

129   public void makeAdmin()
130   {
131       this.admin = true;
132       try
133       {
134         
135         Connection d = DatabaseConnectionPool.getConnection();
136         Statement st = d.createStatement();
137         st.execute("update projectuser set rid=1 where uid=" + uid);
138         st.execute("insert into projectuser select pid, " + uid + ", 1 from projects");
139         st.close();
140         DatabaseConnectionPool.freeConnection(d);
141
142       } catch (Exception JavaDoc e) { }
143       
144       update();
145   }
146
147   /**
148    *This method removes a user from a project identified by pid.
149    */

150   public void delUserProject(int pid)
151   {
152   String JavaDoc SQL = "delete from projectuser where pid=" + pid + " and uid=" + uid;
153   delete(SQL);
154   }
155   
156   public boolean delUserProject()
157   {
158          String JavaDoc SQL = "delete from projectuser where pid=" + pid + " and uid=" + uid;
159
160          return delete(SQL);
161
162
163   } //end of method delUserProject
164

165
166   public boolean delUser()
167   {
168
169         String JavaDoc SQL = "delete from user where uid=" + uid;
170
171         return delete(SQL);
172
173   } //end of method delUser
174

175
176
177   public boolean delete(String JavaDoc SQL)
178   {
179         try
180         {
181
182             Connection d = DatabaseConnectionPool.getConnection();
183             Statement st = d.createStatement();
184             int rs = st.executeUpdate(SQL);
185             st.close();
186             DatabaseConnectionPool.freeConnection(d);
187             return (rs != 0);
188
189         }
190
191         catch (Exception JavaDoc E)
192         {
193
194                  return false;
195
196         }
197
198   } //end of method delete
199

200
201   public boolean checkPassword(String JavaDoc myPasswd)
202   {
203         
204         return (myPasswd.compareTo(password)==0);
205   
206   } //end of method checkPassword
207

208
209   public boolean update()
210   {
211         try
212         {
213             
214             Connection d = DatabaseConnectionPool.getConnection();
215             Statement st = d.createStatement();
216             String JavaDoc SQL = "update user set username='" + username + "', password='" + password + "', email='" + email + "' where uid=" + uid;
217             int rs = st.executeUpdate(SQL);
218             st.close();
219             DatabaseConnectionPool.freeConnection(d);
220             return (rs != 0);
221
222         }
223
224         catch (Exception JavaDoc E)
225         {
226
227                  return false;
228
229         }
230
231   } //end of method update
232

233
234 /**
235 * Returns 0 on success.
236 * 1 if unable to create user record.
237 * 2 if unable to assign projects.
238 * 3 if database connection/SQL error.
239 */

240
241   public static int createUser(String JavaDoc name, String JavaDoc password, String JavaDoc email)
242   {
243
244     int res = 0;
245     boolean cont = true;
246
247         try
248         {
249             Connection d = DatabaseConnectionPool.getConnection();
250             Statement st = d.createStatement();
251             ResultSet tempRS = st.executeQuery("select count(uid) from user where username='" + name + "';");
252
253             if( tempRS.next() )
254             {
255                 res = ( tempRS.getInt(1) == 0 )?0:1; // username exists!!
256
cont = ( res == 0 );
257             }
258
259             if( cont )
260             {
261                 int rs = st.executeUpdate("insert into user values (0, '" + name + "', '" + password + "', '" + email + "', 0)");
262
263                 if( rs == 0 )
264                 {
265                     res=1;// unable to create user record.
266
}
267
268                 cont = ( res == 0 );
269             }
270
271             /*if( cont )
272             {
273                 ResultSet rs1 = st.executeQuery("select uid from user where username='" + name + "'");
274
275                 if( rs1.next() )
276                 {
277                     int rs2 = st.executeUpdate("insert into projectuser select pid, " + rs1.getInt(1) + ", 4 from projects");
278                 }
279                 else
280                 {
281                     res = 2; // unable to assign any projects.
282                 }
283             } */

284
285             st.close();
286             DatabaseConnectionPool.freeConnection(d);
287             return (res);
288
289         }
290
291         catch (Exception JavaDoc E)
292         {
293             res = 3;
294
295             return res;
296
297         }
298   
299   } //end of method createUser
300

301   public static String JavaDoc forgotPassword(String JavaDoc eml, ServletContext sc)
302   {
303
304     String JavaDoc emailMessage = null;
305
306     try
307     {
308
309             Connection d = DatabaseConnectionPool.getConnection();
310             Statement st = d.createStatement();
311             ResultSet rs = st.executeQuery("select username, password from user where email='" + eml + "';");
312
313             if( rs.next() )
314             {
315 /******************************************
316 This webmacro bit needs SERIOUS debugging!!
317 *******************************************
318             try
319                 {
320                 WebMacro _wm = new WM();
321                 Context c = _wm.getContext();
322                 Template t = _wm.getTemplate(
323                     sc.getRealPath("templates/" +
324                     ConfigFactory.getInstance().getValue("templates", "forgotPasswordPlain")
325                     ) );
326                 c.put("userName", rs.getString(1) );
327                 c.put("password", rs.getString(2) );
328                 emailMessage = (String) t.evaluate(c);
329                 }
330             catch (Exception wme)
331                 {
332                         emailMessage = wme.getMessage();
333
334                 }
335 **********************************
336 end Webmacro bit
337 *********************************/

338 // Temporary hardcoded text solution. :(
339

340                 emailMessage = "Hello.\n\nYour login details at BUGTRACKER is as follows:\nUsername: " + rs.getString(1) + "\nPassword: " + rs.getString(2) + "\n\nThank you.";
341
342             }
343
344             st.close();
345             DatabaseConnectionPool.freeConnection(d);
346             return emailMessage;
347     }
348
349     catch (Exception JavaDoc E)
350     {
351              return null;
352     }
353
354   } //end of method forgotPassword
355

356
357   public static Vector getUsers()
358   {
359
360      Vector v = new Vector();
361
362         try
363         {
364
365             Connection d = DatabaseConnectionPool.getConnection();
366             Statement st = d.createStatement();
367             ResultSet rs = st.executeQuery("select uid from user");
368
369             while (rs.next())
370             {
371
372                     v.add( new User( rs.getInt( 1 ) ) );
373
374             }
375
376             st.close();
377             DatabaseConnectionPool.freeConnection(d);
378             return v;
379
380         }
381
382         catch (Exception JavaDoc E)
383         {
384
385              return v;
386
387         }
388   } //end of method getUsers
389

390  
391   public boolean isAdmin()
392   {
393       boolean i=false;
394       try
395       {
396
397         Connection d = DatabaseConnectionPool.getConnection();
398             Statement st = d.createStatement();
399             ResultSet rs = st.executeQuery("select admin from user where uid=" + uid);
400                 rs.next();
401                 i = rs.getBoolean(1);
402                 st.close();
403             DatabaseConnectionPool.freeConnection(d);
404
405       }
406       catch (Exception JavaDoc e)
407       {
408       }
409     return i;
410   }
411   
412 public int getMostProminentRole()
413 {
414     int i=0;
415       try
416       {
417
418         Connection d = DatabaseConnectionPool.getConnection();
419             Statement st = d.createStatement();
420             ResultSet rs = st.executeQuery("select min(rid) from projectuser where uid=" + uid);
421                 rs.next();
422                 i = rs.getInt(1);
423                 st.close();
424             DatabaseConnectionPool.freeConnection(d);
425
426       }
427       catch (Exception JavaDoc e)
428       {
429       }
430 return i;
431     
432 }
433 /**
434  *This method will get a listing of users in a particular project identified by proId.
435  */

436
437   public static Vector getUserRolesByProject(int proId)
438   {
439      Vector v = new Vector();
440
441       try
442       {
443
444             Connection d = DatabaseConnectionPool.getConnection();
445             Statement st = d.createStatement();
446             ResultSet rs = st.executeQuery("select uid, rid, pid from projectuser where pid=" + proId);
447                 User prouser=null;
448                 
449             while (rs.next())
450             {
451                      prouser = new User( rs.getInt( 1 ) );
452
453                      prouser.rid = rs.getInt( 2 );
454
455                      prouser.pid = rs.getInt( 3 );
456
457                      v.add( prouser );
458
459             }
460
461             st.close();
462             DatabaseConnectionPool.freeConnection(d);
463
464       }
465
466       catch (Exception JavaDoc E)
467       {
468
469
470       }
471
472       return v;
473
474
475   } //end of method getUserRolesByProject
476
/**
477  *This method returns the role a user plays in a particular project identified by pid.
478  */

479   public Roles getRoleInProject(int pid)
480   {
481       Roles r = null;
482    try
483     {
484         Connection d = DatabaseConnectionPool.getConnection();
485         Statement st = d.createStatement();
486     ResultSet rs = st.executeQuery("select rid from projectuser where pid=" + pid + " and uid=" + uid);
487         if (rs.next())
488         {
489             r = new Roles(rs.getInt(1));
490         }
491         st.close();
492         DatabaseConnectionPool.freeConnection(d);
493   }
494
495       catch (Exception JavaDoc E)
496       {
497
498
499       }
500 return r;
501   }
502
503 /**
504  *This method returns a vector of users NOT in a project identified by pid.
505  *
506  *@author Kishan Pieris
507  *@author Manik Surtani (since v0.2beta2)
508  */

509   
510   public static Vector getUsersNotInProject(int pid)
511   {
512       
513       // ok, a fiendishly messy way to do this - but I cant think of a better one right now
514
// given MySQL's current limitations with subqueries. - Manik
515

516       Vector v = new Vector();
517     try
518     {
519         Connection d = DatabaseConnectionPool.getConnection();
520         Statement st = d.createStatement();
521     int uidn = 0;
522         ResultSet rs = st.executeQuery("select uid from projectuser where pid=" + pid);
523         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("select user.uid from user where user.uid not in (");
524         while (rs.next())
525         {
526             sql.append(rs.getString(1));
527             sql.append(",");
528         }
529         sql.setCharAt(sql.length() - 1, ')');
530   
531     rs = st.executeQuery(sql.toString());
532     while (rs.next())
533             {
534                      User gotuser = new User( rs.getInt( 1 ) );
535                      v.add( gotuser );
536             }
537
538         st.close();
539         DatabaseConnectionPool.freeConnection(d);
540   }
541
542       catch (Exception JavaDoc E)
543       {
544
545
546       }
547
548       return v;
549
550   } //end of method getUsersNotInProject
551

552
553
554   public static void addUsersToProjectsRoles(int pid,int uid,int rid)
555   {
556
557     try
558       {
559
560             Connection d = DatabaseConnectionPool.getConnection();
561
562             String JavaDoc SQL = "insert into projectuser values ( " + pid + " , " + uid + " , " + rid + " )";
563             
564             Statement st = d.createStatement();
565
566             st.executeQuery(SQL);
567
568             st.close();
569
570             DatabaseConnectionPool.freeConnection(d);
571
572       }
573
574       catch (Exception JavaDoc E)
575       {
576
577
578       }
579
580   } //end of method addUserRoleProjects
581

582
583
584
585
586 } //end of
Popular Tags