001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.server.store.sql;
020:
021: import java.util.*;
022: import java.sql.*;
023:
024: import org.lucane.common.concepts.UserConcept;
025: import org.lucane.server.store.*;
026: import org.lucane.server.*;
027: import org.lucane.server.database.*;
028:
029: public class SqlUserStore extends UserStore {
030: private static final String TABLENAME = "users";
031: private DatabaseAbstractionLayer layer;
032:
033: public SqlUserStore() throws Exception {
034: this .layer = Server.getInstance().getDBLayer();
035: }
036:
037: //-- interface
038: public void storeUser(UserConcept user) throws SQLException {
039: //store user
040: Connection c = layer.getConnection();
041: PreparedStatement insert = c.prepareStatement("INSERT INTO "
042: + TABLENAME + " VALUES(?, ?, ?, ?, ?, ?, ?, ?);");
043:
044: insert.setString(1, user.getName());
045: insert.setString(2, user.getPassword());
046: insert.setString(3, user.getRealName());
047: insert.setString(4, user.getMailAddress());
048: insert.setString(5, user.getLanguage());
049: insert.setInt(6, user.isLocked() ? 1 : 0);
050: insert.setString(7, user.getStartupPlugin());
051: insert.setString(8, user.getDescription());
052: insert.execute();
053: insert.close();
054:
055: c.close();
056: }
057:
058: public void updateUser(UserConcept user) throws SQLException {
059: Connection c = layer.getConnection();
060: Statement s = c.createStatement();
061:
062: //try to delete user
063: try {
064: PreparedStatement delete = c
065: .prepareStatement("DELETE FROM " + TABLENAME
066: + " WHERE login=?");
067: delete.setString(1, user.getName());
068: delete.execute();
069: } catch (SQLException e) {
070: //no such user
071: }
072:
073: //store user
074: PreparedStatement insert = c.prepareStatement("INSERT INTO "
075: + TABLENAME + " VALUES(?, ?, ?, ?, ?, ?, ?, ?);");
076:
077: insert.setString(1, user.getName());
078: insert.setString(2, user.getPassword());
079: insert.setString(3, user.getRealName());
080: insert.setString(4, user.getMailAddress());
081: insert.setString(5, user.getLanguage());
082: insert.setInt(6, user.isLocked() ? 1 : 0);
083: insert.setString(7, user.getStartupPlugin());
084: insert.setString(8, user.getDescription());
085: insert.execute();
086: insert.close();
087:
088: c.close();
089: }
090:
091: public void removeUser(UserConcept user) throws SQLException {
092: Connection c = layer.getConnection();
093:
094: PreparedStatement delete = c.prepareStatement("DELETE FROM "
095: + TABLENAME + " WHERE login=?");
096: delete.setString(1, user.getName());
097: delete.execute();
098:
099: delete = c.prepareStatement("DELETE FROM "
100: + SqlGroupStore.USERLINKS + " WHERE userName=?");
101: delete.setString(1, user.getName());
102: delete.execute();
103:
104: c.close();
105: }
106:
107: public UserConcept getUser(String login) throws SQLException {
108: UserConcept user = null;
109:
110: Connection c = layer.getConnection();
111:
112: PreparedStatement select = c
113: .prepareStatement("SELECT login, passwd, realName, mailAddress, language, locked, startup, description"
114: + " FROM " + TABLENAME + " WHERE login=?");
115: select.setString(1, login);
116: ResultSet rs = select.executeQuery();
117:
118: if (rs.next()) {
119: login = rs.getString(1);
120: String passwd = rs.getString(2);
121: String realName = rs.getString(3);
122: String mailAddress = rs.getString(4);
123: String language = rs.getString(5);
124: boolean locked = (rs.getInt(6) != 0);
125: String startup = rs.getString(7);
126: String description = rs.getString(8);
127:
128: user = new UserConcept(login, passwd, realName,
129: mailAddress, language, locked, startup);
130: user.setDescription(description);
131: }
132:
133: rs.close();
134: select.close();
135: c.close();
136:
137: return user;
138: }
139:
140: public Iterator getAllUsers() throws SQLException {
141: ArrayList all = new ArrayList();
142:
143: Connection c = layer.getConnection();
144: PreparedStatement select = c
145: .prepareStatement("SELECT login, passwd, realName, mailAddress, language, locked, startup, description FROM "
146: + TABLENAME);
147: ResultSet rs = select.executeQuery();
148:
149: while (rs.next()) {
150: String login = rs.getString(1);
151: String passwd = rs.getString(2);
152: String realName = rs.getString(3);
153: String mailAddress = rs.getString(4);
154: String language = rs.getString(5);
155: boolean locked = (rs.getInt(6) != 0);
156: String startup = rs.getString(7);
157: String description = rs.getString(8);
158:
159: UserConcept user = new UserConcept(login, passwd, realName,
160: mailAddress, language, locked, startup);
161: user.setDescription(description);
162: all.add(user);
163: }
164:
165: rs.close();
166: select.close();
167: c.close();
168:
169: return all.iterator();
170: }
171: }
|