KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > store > sql > SqlUserStore


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server.store.sql;
20
21 import java.util.*;
22 import java.sql.*;
23
24 import org.lucane.common.concepts.UserConcept;
25 import org.lucane.server.store.*;
26 import org.lucane.server.*;
27 import org.lucane.server.database.*;
28
29 public class SqlUserStore extends UserStore
30 {
31     private static final String JavaDoc TABLENAME = "users";
32     private DatabaseAbstractionLayer layer;
33     
34     public SqlUserStore()
35     throws Exception JavaDoc
36     {
37         this.layer = Server.getInstance().getDBLayer();
38     }
39     
40     //-- interface
41
public void storeUser(UserConcept user)
42     throws SQLException
43     {
44         //store user
45
Connection c = layer.getConnection();
46         PreparedStatement insert = c.prepareStatement("INSERT INTO " + TABLENAME
47             + " VALUES(?, ?, ?, ?, ?, ?, ?, ?);");
48         
49         insert.setString(1, user.getName());
50         insert.setString(2, user.getPassword());
51         insert.setString(3, user.getRealName());
52         insert.setString(4, user.getMailAddress());
53         insert.setString(5, user.getLanguage());
54         insert.setInt(6, user.isLocked() ? 1 : 0);
55         insert.setString(7, user.getStartupPlugin());
56         insert.setString(8, user.getDescription());
57         insert.execute();
58         insert.close();
59         
60         c.close();
61     }
62
63
64     public void updateUser(UserConcept user)
65     throws SQLException
66     {
67         Connection c = layer.getConnection();
68         Statement s = c.createStatement();
69
70         //try to delete user
71
try {
72             PreparedStatement delete = c.prepareStatement("DELETE FROM " + TABLENAME
73                 + " WHERE login=?");
74             delete.setString(1, user.getName());
75             delete.execute();
76         } catch(SQLException e) {
77             //no such user
78
}
79         
80         //store user
81
PreparedStatement insert = c.prepareStatement("INSERT INTO " + TABLENAME
82             + " VALUES(?, ?, ?, ?, ?, ?, ?, ?);");
83         
84         insert.setString(1, user.getName());
85         insert.setString(2, user.getPassword());
86         insert.setString(3, user.getRealName());
87         insert.setString(4, user.getMailAddress());
88         insert.setString(5, user.getLanguage());
89         insert.setInt(6, user.isLocked() ? 1 : 0);
90         insert.setString(7, user.getStartupPlugin());
91         insert.setString(8, user.getDescription());
92         insert.execute();
93         insert.close();
94             
95         c.close();
96     }
97     public void removeUser(UserConcept user)
98     throws SQLException
99     {
100         Connection c = layer.getConnection();
101         
102         PreparedStatement delete = c.prepareStatement("DELETE FROM " + TABLENAME
103             + " WHERE login=?");
104         delete.setString(1, user.getName());
105         delete.execute();
106         
107         delete = c.prepareStatement("DELETE FROM " + SqlGroupStore.USERLINKS
108             + " WHERE userName=?");
109         delete.setString(1, user.getName());
110         delete.execute();
111         
112         c.close();
113     }
114     
115     public UserConcept getUser(String JavaDoc login)
116     throws SQLException
117     {
118         UserConcept user = null;
119
120         Connection c = layer.getConnection();
121         
122         PreparedStatement select = c.prepareStatement(
123                 "SELECT login, passwd, realName, mailAddress, language, locked, startup, description" +
124                 " FROM " + TABLENAME +
125                 " WHERE login=?");
126         select.setString(1, login);
127         ResultSet rs = select.executeQuery();
128                     
129         if(rs.next())
130         {
131             login = rs.getString(1);
132             String JavaDoc passwd = rs.getString(2);
133             String JavaDoc realName = rs.getString(3);
134             String JavaDoc mailAddress = rs.getString(4);
135             String JavaDoc language = rs.getString(5);
136             boolean locked = (rs.getInt(6) != 0);
137             String JavaDoc startup = rs.getString(7);
138             String JavaDoc description = rs.getString(8);
139             
140             user = new UserConcept(login, passwd, realName, mailAddress, language, locked, startup);
141             user.setDescription(description);
142         }
143     
144         rs.close();
145         select.close();
146         c.close();
147             
148         return user;
149     }
150
151     public Iterator getAllUsers()
152     throws SQLException
153     {
154         ArrayList all = new ArrayList();
155         
156         Connection c = layer.getConnection();
157         PreparedStatement select = c.prepareStatement(
158                 "SELECT login, passwd, realName, mailAddress, language, locked, startup, description FROM " + TABLENAME);
159         ResultSet rs = select.executeQuery();
160                             
161         while(rs.next())
162         {
163             String JavaDoc login = rs.getString(1);
164             String JavaDoc passwd = rs.getString(2);
165             String JavaDoc realName = rs.getString(3);
166             String JavaDoc mailAddress = rs.getString(4);
167             String JavaDoc language = rs.getString(5);
168             boolean locked = (rs.getInt(6) != 0);
169             String JavaDoc startup = rs.getString(7);
170             String JavaDoc description = rs.getString(8);
171             
172             UserConcept user = new UserConcept(login, passwd, realName, mailAddress, language, locked, startup);
173             user.setDescription(description);
174             all.add(user);
175         }
176     
177         rs.close();
178         select.close();
179         c.close();
180         
181         return all.iterator();
182     }
183 }
184
Popular Tags