KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > storage > JDBCUserStorage


1 /* Rss
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip.storage;
27
28 import org.radeox.util.logging.Logger;
29 import org.snipsnap.app.Application;
30 import org.snipsnap.jdbc.*;
31 import org.snipsnap.user.Roles;
32 import org.snipsnap.user.User;
33 import org.snipsnap.util.ConnectionManager;
34 import org.snipsnap.util.log.SQLLogger;
35
36 import javax.sql.DataSource JavaDoc;
37 import java.sql.*;
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40
41 /**
42  * JDBC SnipStorage backend for user data
43  *
44  * @author Stephan J. Schmidt
45  * @version $Id: JDBCUserStorage.java 1256 2003-12-11 13:24:57Z leo $
46  */

47
48 public class JDBCUserStorage implements UserStorage {
49   private FinderFactory finders;
50   private DataSource JavaDoc ds;
51
52   public JDBCUserStorage(DataSource JavaDoc ds) {
53     this.ds = ds;
54     finders = new FinderFactory(ds, "SELECT applicationOid, login, passwd, email, status, roles, " +
55         " cTime, mTime, lastLogin, lastAccess, lastLogout " +
56         " FROM SnipUser ");
57   }
58
59   public static void createStorage() {
60     DataSource JavaDoc datasource = ConnectionManager.getDataSource();
61     System.err.println("JDBCUserStorage: dropping SnipUser SQL table");
62     JDBCTemplate droptemplate = new JDBCTemplate(datasource);
63     try {
64       droptemplate.update("DROP TABLE SnipUser");
65     } catch (Exception JavaDoc e) {
66       SQLLogger.warn("JDBCUserStorage: unable to drop table (new install?)", e);
67     }
68
69     System.err.println("JDBCUserStorage: creating SnipUser SQL table");
70     JDBCTemplate template = new JDBCTemplate(datasource);
71     template.update(
72         " CREATE TABLE SnipUser ( " +
73         " login VARCHAR(100) NOT NULL, " +
74         " applicationOid VARCHAR(100) NOT NULL," +
75         " cTime TIMESTAMP, " +
76         " mTime TIMESTAMP, " +
77         " lastLogin TIMESTAMP, " +
78         " lastAccess TIMESTAMP, " +
79         " lastLogout TIMESTAMP, " +
80         " passwd VARCHAR(100), " +
81         " email VARCHAR(100)," +
82         " status VARCHAR(50), " +
83         " roles VARCHAR(200) )");
84     return;
85   }
86
87   public void storageStore(final User user) {
88     JDBCTemplate template = new JDBCTemplate(ds);
89     template.update(
90         "UPDATE SnipUser SET login=?, passwd=?, email=?, status=?, roles=?, " +
91         " cTime=?, mTime=?, lastLogin=?, lastAccess=?, lastLogout=? " +
92         " WHERE login=? AND applicationOid=?",
93         new PreparedStatementSetter() {
94           public void setValues(PreparedStatement ps) throws SQLException {
95             ps.setString(1, user.getLogin());
96             ps.setString(2, user.getPasswd());
97             ps.setString(3, user.getEmail());
98             ps.setString(4, user.getStatus());
99             ps.setString(5, user.getRoles().toString());
100             ps.setTimestamp(6, user.getCTime());
101             ps.setTimestamp(7, user.getMTime());
102             ps.setTimestamp(8, user.getLastLogin());
103             ps.setTimestamp(9, user.getLastAccess());
104             ps.setTimestamp(10, user.getLastLogout());
105             ps.setString(11, user.getLogin());
106             ps.setString(12, user.getApplication());
107           }
108         });
109     return;
110   }
111
112   public User storageCreate(String JavaDoc login, String JavaDoc passwd, String JavaDoc email) {
113     String JavaDoc applicationOid = (String JavaDoc) Application.get().getObject(Application.OID);
114
115     final User user = new User(login, passwd, email);
116     final Timestamp cTime = new Timestamp(new java.util.Date JavaDoc().getTime());
117     user.setCTime(cTime);
118     user.setMTime(cTime);
119     user.setLastLogin(cTime);
120     user.setLastAccess(cTime);
121     user.setLastLogout(cTime);
122     user.setApplication(applicationOid);
123
124     JDBCTemplate template = new JDBCTemplate(ds);
125     template.update(
126         "INSERT INTO SnipUser " +
127         " (login, passwd, email, status, roles, " +
128         " cTime, mTime, lastLogin, lastAccess, lastLogout, applicationOid) " +
129         " VALUES (?,?,?,?,?,?,?,?,?,?,?)",
130         new PreparedStatementSetter() {
131           public void setValues(PreparedStatement ps) throws SQLException {
132             ps.setString(1, user.getLogin());
133             ps.setString(2, user.getPasswd());
134             ps.setString(3, user.getEmail());
135             ps.setString(4, "");
136             ps.setString(5, "");
137             ps.setTimestamp(6, cTime);
138             ps.setTimestamp(7, cTime);
139             ps.setTimestamp(8, cTime);
140             ps.setTimestamp(9, cTime);
141             ps.setTimestamp(10, cTime);
142             ps.setString(11, user.getApplication());
143           }
144         });
145     return user;
146   }
147
148   public void storageRemove(final User user) {
149     JDBCTemplate template = new JDBCTemplate(ds);
150     template.update(
151         "DELETE FROM SnipUser WHERE login=? AND applicationOid=?",
152         new PreparedStatementSetter() {
153           public void setValues(PreparedStatement ps) throws SQLException {
154             ps.setString(1, user.getLogin());
155             ps.setString(2, user.getApplication());
156           }
157         });
158     return;
159   }
160
161   public int storageUserCount() {
162     final String JavaDoc applicationOid = (String JavaDoc) Application.get().getObject(Application.OID);
163     final IntHolder holder = new IntHolder(-1);
164
165     JDBCTemplate template = new JDBCTemplate(ds);
166     template.query(
167         "SELECT count(*) FROM SnipUser WHERE applicationOid=?",
168         new RowCallbackHandler() {
169           public void processRow(ResultSet rs) throws SQLException {
170             holder.setValue(rs.getInt(1));
171           }
172         },
173         new PreparedStatementSetter() {
174           public void setValues(PreparedStatement ps) throws SQLException {
175             ps.setString(1, applicationOid);
176           }
177         });
178     return holder.getValue();
179   }
180
181   public User storageLoad(final String JavaDoc login) {
182     //System.err.println("storageLoad: User login="+login);
183
final String JavaDoc applicationOid = (String JavaDoc) Application.get().getObject(Application.OID);
184     final List JavaDoc users = new ArrayList JavaDoc();
185     JDBCTemplate template = new JDBCTemplate(ds);
186     template.query(
187         "SELECT applicationOid, login, passwd, email, status, roles, cTime, mTime, " +
188         " lastLogin, lastAccess, lastLogout " +
189         " FROM SnipUser " +
190         " WHERE login=? AND applicationOid=?",
191         new RowCallbackHandler() {
192           public void processRow(ResultSet rs) throws SQLException {
193             users.add(createUser(rs));
194           }
195         },
196         new PreparedStatementSetter() {
197           public void setValues(PreparedStatement ps) throws SQLException {
198             ps.setString(1, login);
199             ps.setString(2, applicationOid);
200           }
201         });
202     return (users.size() > 0 ? (User) users.get(0) : null);
203   }
204
205   public List JavaDoc storageAll() {
206     String JavaDoc applicationOid = (String JavaDoc) Application.get().getObject(Application.OID);
207
208     Finder finder = finders.getFinder(" WHERE applicationOid=? ORDER BY login ")
209         .setString(1, applicationOid);
210     List JavaDoc list = createObjects(finder.execute());
211     finder.close();
212     return list;
213   }
214
215
216   public List JavaDoc createObjects(ResultSet result) {
217     return createObjects(result, Integer.MAX_VALUE);
218   }
219
220   public List JavaDoc createObjects(ResultSet result, int size) {
221     List JavaDoc resultList = new ArrayList JavaDoc();
222     if (null != result) {
223       try {
224         User user = null;
225         while (result.next() && size-- > 0) {
226           user = createUser(result);
227           resultList.add(user);
228         }
229       } catch (SQLException e) {
230         Logger.warn("Finder: SQL Error", e);
231       }
232     }
233     return resultList;
234   }
235
236   public User createUser(ResultSet result) throws SQLException {
237     String JavaDoc applicationOid = result.getString("applicationOid");
238     String JavaDoc login = result.getString("login");
239     String JavaDoc passwd = result.getString("passwd");
240     String JavaDoc email = result.getString("email");
241     Timestamp cTime = result.getTimestamp("cTime");
242     Timestamp mTime = result.getTimestamp("mTime");
243     Timestamp lastLogin = result.getTimestamp("lastLogin");
244     Timestamp lastAccess = result.getTimestamp("lastAccess");
245     Timestamp lastLogout = result.getTimestamp("lastLogout");
246     String JavaDoc status = result.getString("status");
247     User user = new User(login, passwd, email);
248     user.setApplication(applicationOid);
249     user.setStatus(status);
250     user.setRoles(new Roles(result.getString("roles")));
251     user.setCTime(cTime);
252     user.setMTime(mTime);
253     user.setLastLogin(lastLogin);
254     user.setLastAccess(lastAccess);
255     user.setLastLogout(lastLogout);
256     return user;
257   }
258 }
259
Popular Tags