1 43 package net.jforum.dao.generic; 44 45 import java.sql.Connection ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 import java.sql.Timestamp ; 49 50 import net.jforum.entities.UserSession; 51 import net.jforum.util.preferences.SystemGlobals; 52 53 57 public class GenericUserSessionDAO implements net.jforum.dao.UserSessionDAO 58 { 59 62 public void add(UserSession us, Connection conn) throws Exception 63 { 64 this.add(us, conn, false); 65 } 66 67 private void add(UserSession us, Connection conn, boolean checked) throws Exception 68 { 69 if (!checked && this.selectById(us, conn) != null) { 70 return; 71 } 72 73 PreparedStatement p = conn.prepareStatement(SystemGlobals.getSql("UserSessionModel.add")); 74 p.setString(1, us.getSessionId()); 75 p.setInt(2, us.getUserId()); 76 p.setTimestamp(3, new Timestamp (us.getStartTime().getTime())); 77 78 p.executeUpdate(); 79 p.close(); 80 } 81 82 85 public void update(UserSession us, Connection conn) throws Exception 86 { 87 if (this.selectById(us, conn) == null) { 88 this.add(us, conn, true); 89 return; 90 } 91 92 PreparedStatement p = conn.prepareStatement(SystemGlobals.getSql("UserSessionModel.update")); 93 p.setTimestamp(1, new Timestamp (us.getStartTime().getTime())); 94 p.setLong(2, us.getSessionTime()); 95 p.setString(3, us.getSessionId()); 96 p.setInt(4, us.getUserId()); 97 98 p.executeUpdate(); 99 p.close(); 100 } 101 102 105 public UserSession selectById(UserSession us, Connection conn) throws Exception 106 { 107 PreparedStatement p = conn.prepareStatement(SystemGlobals.getSql("UserSessionModel.selectById")); 108 p.setInt(1, us.getUserId()); 109 110 ResultSet rs = p.executeQuery(); 111 boolean found = false; 112 113 UserSession returnUs = new UserSession(us); 114 115 if (rs.next()) { 116 returnUs.setSessionTime(rs.getLong("session_time")); 117 returnUs.setStartTime(rs.getTimestamp("session_start")); 118 found = true; 119 } 120 121 rs.close(); 122 p.close(); 123 124 return (found ? returnUs : null); 125 } 126 127 } 128 | Popular Tags |