001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 30/05/2004 15:10:57
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.dao.generic;
044:
045: import java.sql.Connection;
046: import java.sql.PreparedStatement;
047: import java.sql.ResultSet;
048: import java.sql.SQLException;
049: import java.sql.Timestamp;
050:
051: import net.jforum.entities.UserSession;
052: import net.jforum.exceptions.DatabaseException;
053: import net.jforum.util.DbUtils;
054: import net.jforum.util.preferences.SystemGlobals;
055:
056: /**
057: * @author Rafael Steil
058: * @version $Id: GenericUserSessionDAO.java,v 1.7 2006/08/23 02:13:43 rafaelsteil Exp $
059: */
060: public class GenericUserSessionDAO implements
061: net.jforum.dao.UserSessionDAO {
062: /**
063: * @see net.jforum.dao.UserSessionDAO#add(net.jforum.entities.UserSession, java.sql.Connection)
064: */
065: public void add(UserSession us, Connection conn) {
066: this .add(us, conn, false);
067: }
068:
069: private void add(UserSession us, Connection conn, boolean checked) {
070: if (!checked && this .selectById(us, conn) != null) {
071: return;
072: }
073:
074: PreparedStatement p = null;
075: try {
076: p = conn.prepareStatement(SystemGlobals
077: .getSql("UserSessionModel.add"));
078: p.setString(1, us.getSessionId());
079: p.setInt(2, us.getUserId());
080: p.setTimestamp(3,
081: new Timestamp(us.getStartTime().getTime()));
082:
083: p.executeUpdate();
084: } catch (SQLException e) {
085: throw new DatabaseException(e);
086: } finally {
087: DbUtils.close(p);
088: }
089: }
090:
091: /**
092: * @see net.jforum.dao.UserSessionDAO#update(net.jforum.entities.UserSession,
093: * java.sql.Connection)
094: */
095: public void update(UserSession us, Connection conn) {
096: if (this .selectById(us, conn) == null) {
097: this .add(us, conn, true);
098: return;
099: }
100:
101: PreparedStatement p = null;
102: try {
103: p = conn.prepareStatement(SystemGlobals
104: .getSql("UserSessionModel.update"));
105: p.setTimestamp(1,
106: new Timestamp(us.getStartTime().getTime()));
107: p.setLong(2, us.getSessionTime());
108: p.setString(3, us.getSessionId());
109: p.setInt(4, us.getUserId());
110:
111: p.executeUpdate();
112: } catch (SQLException e) {
113: throw new DatabaseException(e);
114: } finally {
115: DbUtils.close(p);
116: }
117: }
118:
119: /**
120: * @see net.jforum.dao.UserSessionDAO#selectById(net.jforum.entities.UserSession,
121: * java.sql.Connection)
122: */
123: public UserSession selectById(UserSession us, Connection conn) {
124: PreparedStatement p = null;
125: ResultSet rs = null;
126: try {
127: p = conn.prepareStatement(SystemGlobals
128: .getSql("UserSessionModel.selectById"));
129: p.setInt(1, us.getUserId());
130:
131: rs = p.executeQuery();
132: boolean found = false;
133:
134: UserSession returnUs = new UserSession(us);
135:
136: if (rs.next()) {
137: returnUs.setSessionTime(rs.getLong("session_time"));
138: returnUs.setStartTime(rs.getTimestamp("session_start"));
139: found = true;
140: }
141:
142: return (found ? returnUs : null);
143: } catch (SQLException e) {
144: throw new DatabaseException(e);
145: } finally {
146: DbUtils.close(rs, p);
147: }
148: }
149: }
|