1 43 package net.jforum.dao.generic; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.sql.Timestamp ; 48 import java.text.SimpleDateFormat ; 49 import java.util.ArrayList ; 50 import java.util.List ; 51 52 import net.jforum.JForumExecutionContext; 53 import net.jforum.dao.DataAccessDriver; 54 import net.jforum.dao.UserDAO; 55 import net.jforum.entities.Post; 56 import net.jforum.entities.PrivateMessage; 57 import net.jforum.entities.PrivateMessageType; 58 import net.jforum.entities.User; 59 import net.jforum.util.preferences.ConfigKeys; 60 import net.jforum.util.preferences.SystemGlobals; 61 62 66 public class GenericPrivateMessageDAO extends AutoKeys implements net.jforum.dao.PrivateMessageDAO 67 { 68 71 public void send(PrivateMessage pm) throws Exception 72 { 73 PreparedStatement p = this.getStatementForAutoKeys("PrivateMessageModel.add"); 76 77 this.addPm(pm, p); 79 this.addPmText(pm); 80 81 p.setInt(1, PrivateMessageType.NEW); 83 pm.setId(this.executeAutoKeysQuery(p)); 84 85 this.addPmText(pm); 86 87 p.close(); 88 } 89 90 protected void addPmText(PrivateMessage pm) throws Exception 91 { 92 PreparedStatement text = JForumExecutionContext.getConnection().prepareStatement( 93 SystemGlobals.getSql("PrivateMessagesModel.addText")); 94 95 text.setInt(1, pm.getId()); 96 text.setString(2, pm.getPost().getText()); 97 text.executeUpdate(); 98 99 text.close(); 100 } 101 102 protected void addPm(PrivateMessage pm, PreparedStatement p) throws Exception 103 { 104 p.setInt(1, PrivateMessageType.SENT); 105 p.setString(2, pm.getPost().getSubject()); 106 p.setInt(3, pm.getFromUser().getId()); 107 p.setInt(4, pm.getToUser().getId()); 108 p.setTimestamp(5, new Timestamp (pm.getPost().getTime().getTime())); 109 p.setInt(6, pm.getPost().isBbCodeEnabled() ? 1 : 0); 110 p.setInt(7, pm.getPost().isHtmlEnabled() ? 1 : 0); 111 p.setInt(8, pm.getPost().isSmiliesEnabled() ? 1 : 0); 112 p.setInt(9, pm.getPost().isSignatureEnabled() ? 1 : 0); 113 114 this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PrivateMessagesModel.lastGeneratedPmId")); 115 pm.setId(this.executeAutoKeysQuery(p)); 116 } 117 118 121 public void delete(PrivateMessage[] pm) throws Exception 122 { 123 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PrivateMessageModel.delete")); 124 p.setInt(2, pm[0].getFromUser().getId()); 125 p.setInt(3, pm[0].getFromUser().getId()); 126 127 PreparedStatement deleteText = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PrivateMessagesModel.deleteText")); 128 129 for (int i = 0; i < pm.length; i++) { 130 deleteText.setInt(1, pm[i].getId()); 131 deleteText.executeUpdate(); 132 133 p.setInt(1, pm[i].getId()); 134 p.executeUpdate(); 135 } 136 137 deleteText.close(); 138 p.close(); 139 } 140 141 144 public List selectFromInbox(User user) throws Exception 145 { 146 String query = SystemGlobals.getSql("PrivateMessageModel.baseListing"); 147 query = query.replaceAll("#FILTER#", SystemGlobals.getSql("PrivateMessageModel.inbox")); 148 149 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(query); 150 p.setInt(1, user.getId()); 151 152 List pmList = new ArrayList (); 153 154 ResultSet rs = p.executeQuery(); 155 while (rs.next()) { 156 PrivateMessage pm = this.getPm(rs, false); 157 158 User fromUser = new User(); 159 fromUser.setId(rs.getInt("user_id")); 160 fromUser.setUsername(rs.getString("username")); 161 162 pm.setFromUser(fromUser); 163 164 pmList.add(pm); 165 } 166 167 rs.close(); 168 p.close(); 169 170 return pmList; 171 } 172 173 176 public List selectFromSent(User user) throws Exception 177 { 178 String query = SystemGlobals.getSql("PrivateMessageModel.baseListing"); 179 query = query.replaceAll("#FILTER#", SystemGlobals.getSql("PrivateMessageModel.sent")); 180 181 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(query); 182 p.setInt(1, user.getId()); 183 184 List pmList = new ArrayList (); 185 186 ResultSet rs = p.executeQuery(); 187 while (rs.next()) { 188 PrivateMessage pm = this.getPm(rs, false); 189 190 User toUser = new User(); 191 toUser.setId(rs.getInt("user_id")); 192 toUser.setUsername(rs.getString("username")); 193 194 pm.setToUser(toUser); 195 196 pmList.add(pm); 197 } 198 199 rs.close(); 200 p.close(); 201 202 return pmList; 203 } 204 205 protected PrivateMessage getPm(ResultSet rs) throws Exception 206 { 207 return this.getPm(rs, true); 208 } 209 210 protected PrivateMessage getPm(ResultSet rs, boolean full) throws Exception 211 { 212 PrivateMessage pm = new PrivateMessage(); 213 Post p = new Post(); 214 215 pm.setId(rs.getInt("privmsgs_id")); 216 pm.setType(rs.getInt("privmsgs_type")); 217 p.setTime(rs.getTimestamp("privmsgs_date")); 218 p.setSubject(rs.getString("privmsgs_subject")); 219 220 SimpleDateFormat df = new SimpleDateFormat (SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT)); 221 pm.setFormatedDate(df.format(p.getTime())); 222 223 if (full) { 224 UserDAO um = DataAccessDriver.getInstance().newUserDAO(); 225 pm.setFromUser(um.selectById(rs.getInt("privmsgs_from_userid"))); 226 pm.setToUser(um.selectById(rs.getInt("privmsgs_to_userid"))); 227 228 p.setBbCodeEnabled(rs.getInt("privmsgs_enable_bbcode") == 1); 229 p.setSignatureEnabled(rs.getInt("privmsgs_attach_sig") == 1); 230 p.setHtmlEnabled(rs.getInt("privmsgs_enable_html") == 1); 231 p.setSmiliesEnabled(rs.getInt("privmsgs_enable_smilies") == 1); 232 p.setText(this.getPmText(rs)); 233 } 234 235 pm.setPost(p); 236 237 return pm; 238 } 239 240 protected String getPmText(ResultSet rs) throws Exception 241 { 242 return rs.getString("privmsgs_text"); 243 } 244 245 248 public PrivateMessage selectById(PrivateMessage pm) throws Exception 249 { 250 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PrivateMessageModel.selectById")); 251 p.setInt(1, pm.getId()); 252 253 ResultSet rs = p.executeQuery(); 254 if (rs.next()) { 255 pm = this.getPm(rs); 256 } 257 258 rs.close(); 259 p.close(); 260 261 return pm; 262 } 263 264 267 public void updateType(PrivateMessage pm) throws Exception 268 { 269 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PrivateMessageModel.updateType")); 270 p.setInt(1,pm.getType()); 271 p.setInt(2, pm.getId()); 272 p.executeUpdate(); 273 p.close(); 274 } 275 } 276 | Popular Tags |