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: * This file creation date: 20/05/2004 - 15:51:10
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: import java.text.SimpleDateFormat;
051: import java.util.ArrayList;
052: import java.util.Date;
053: import java.util.List;
054:
055: import net.jforum.JForumExecutionContext;
056: import net.jforum.dao.DataAccessDriver;
057: import net.jforum.dao.UserDAO;
058: import net.jforum.entities.Post;
059: import net.jforum.entities.PrivateMessage;
060: import net.jforum.entities.PrivateMessageType;
061: import net.jforum.entities.User;
062: import net.jforum.exceptions.DatabaseException;
063: import net.jforum.util.DbUtils;
064: import net.jforum.util.preferences.ConfigKeys;
065: import net.jforum.util.preferences.SystemGlobals;
066:
067: /**
068: * @author Rafael Steil
069: * @version $Id: GenericPrivateMessageDAO.java,v 1.12 2007/08/31 22:56:40 rafaelsteil Exp $
070: */
071: public class GenericPrivateMessageDAO extends AutoKeys implements
072: net.jforum.dao.PrivateMessageDAO {
073: /**
074: * @see net.jforum.dao.PrivateMessageDAO#send(net.jforum.entities.PrivateMessage)
075: */
076: public void send(PrivateMessage pm) {
077: // We should store 2 copies: one for the sendee's sent box
078: // and another for the target user's inbox.
079: PreparedStatement p = null;
080:
081: try {
082: p = this .getStatementForAutoKeys("PrivateMessageModel.add");
083:
084: // Sendee's sent box
085: this .addPm(pm, p);
086: this .addPmText(pm);
087:
088: // Target user's inbox
089: p.setInt(1, PrivateMessageType.NEW);
090: pm.setId(this .executeAutoKeysQuery(p));
091:
092: this .addPmText(pm);
093: } catch (Exception e) {
094: throw new DatabaseException(e);
095: } finally {
096: DbUtils.close(p);
097: }
098: }
099:
100: protected void addPmText(PrivateMessage pm) throws Exception {
101: PreparedStatement text = JForumExecutionContext
102: .getConnection()
103: .prepareStatement(
104: SystemGlobals
105: .getSql("PrivateMessagesModel.addText"));
106:
107: text.setInt(1, pm.getId());
108: text.setString(2, pm.getPost().getText());
109: text.executeUpdate();
110:
111: text.close();
112: }
113:
114: protected void addPm(PrivateMessage pm, PreparedStatement p)
115: throws SQLException {
116: p.setInt(1, PrivateMessageType.SENT);
117: p.setString(2, pm.getPost().getSubject());
118: p.setInt(3, pm.getFromUser().getId());
119: p.setInt(4, pm.getToUser().getId());
120: p.setTimestamp(5, new Timestamp(pm.getPost().getTime()
121: .getTime()));
122: p.setInt(6, pm.getPost().isBbCodeEnabled() ? 1 : 0);
123: p.setInt(7, pm.getPost().isHtmlEnabled() ? 1 : 0);
124: p.setInt(8, pm.getPost().isSmiliesEnabled() ? 1 : 0);
125: p.setInt(9, pm.getPost().isSignatureEnabled() ? 1 : 0);
126:
127: this .setAutoGeneratedKeysQuery(SystemGlobals
128: .getSql("PrivateMessagesModel.lastGeneratedPmId"));
129: pm.setId(this .executeAutoKeysQuery(p));
130: }
131:
132: /**
133: * @see net.jforum.dao.PrivateMessageDAO#delete(net.jforum.entities.PrivateMessage[], int)
134: */
135: public void delete(PrivateMessage[] pm, int userId) {
136: PreparedStatement deleteMessage = null;
137: PreparedStatement deleteText = null;
138: PreparedStatement isDeleteAllowed = null;
139:
140: try {
141: Connection connection = JForumExecutionContext
142: .getConnection();
143:
144: deleteMessage = connection.prepareStatement(SystemGlobals
145: .getSql("PrivateMessageModel.delete"));
146: deleteText = connection.prepareStatement(SystemGlobals
147: .getSql("PrivateMessagesModel.deleteText"));
148:
149: isDeleteAllowed = connection.prepareStatement(SystemGlobals
150: .getSql("PrivateMessagesModel.isDeleteAllowed"));
151: isDeleteAllowed.setInt(2, userId);
152: isDeleteAllowed.setInt(3, userId);
153:
154: for (int i = 0; i < pm.length; i++) {
155: PrivateMessage currentMessage = pm[i];
156:
157: isDeleteAllowed.setInt(1, currentMessage.getId());
158:
159: ResultSet rs = null;
160:
161: try {
162: rs = isDeleteAllowed.executeQuery();
163:
164: if (rs.next()) {
165: deleteText.setInt(1, currentMessage.getId());
166: deleteText.executeUpdate();
167:
168: deleteMessage.setInt(1, currentMessage.getId());
169: deleteMessage.executeUpdate();
170: }
171: } finally {
172: DbUtils.close(rs);
173: }
174: }
175: } catch (SQLException e) {
176: throw new DatabaseException(e);
177: } finally {
178: DbUtils.close(deleteMessage);
179: DbUtils.close(deleteText);
180: DbUtils.close(isDeleteAllowed);
181: }
182: }
183:
184: /**
185: * @see net.jforum.dao.PrivateMessageDAO#selectFromInbox(net.jforum.entities.User)
186: */
187: public List selectFromInbox(User user) {
188: String query = SystemGlobals
189: .getSql("PrivateMessageModel.baseListing");
190: query = query.replaceAll("#FILTER#", SystemGlobals
191: .getSql("PrivateMessageModel.inbox"));
192:
193: PreparedStatement p = null;
194: ResultSet rs = null;
195: try {
196: p = JForumExecutionContext.getConnection()
197: .prepareStatement(query);
198: p.setInt(1, user.getId());
199:
200: List pmList = new ArrayList();
201:
202: rs = p.executeQuery();
203: while (rs.next()) {
204: PrivateMessage pm = this .getPm(rs, false);
205:
206: User fromUser = new User();
207: fromUser.setId(rs.getInt("user_id"));
208: fromUser.setUsername(rs.getString("username"));
209:
210: pm.setFromUser(fromUser);
211:
212: pmList.add(pm);
213: }
214:
215: return pmList;
216: } catch (SQLException e) {
217: throw new DatabaseException(e);
218: } finally {
219: DbUtils.close(rs, p);
220: }
221: }
222:
223: /**
224: * @see net.jforum.dao.PrivateMessageDAO#selectFromSent(net.jforum.entities.User)
225: */
226: public List selectFromSent(User user) {
227: String query = SystemGlobals
228: .getSql("PrivateMessageModel.baseListing");
229: query = query.replaceAll("#FILTER#", SystemGlobals
230: .getSql("PrivateMessageModel.sent"));
231:
232: PreparedStatement p = null;
233: ResultSet rs = null;
234: try {
235: p = JForumExecutionContext.getConnection()
236: .prepareStatement(query);
237: p.setInt(1, user.getId());
238:
239: List pmList = new ArrayList();
240:
241: rs = p.executeQuery();
242: while (rs.next()) {
243: PrivateMessage pm = this .getPm(rs, false);
244:
245: User toUser = new User();
246: toUser.setId(rs.getInt("user_id"));
247: toUser.setUsername(rs.getString("username"));
248:
249: pm.setToUser(toUser);
250:
251: pmList.add(pm);
252: }
253: return pmList;
254: } catch (SQLException e) {
255: throw new DatabaseException(e);
256: } finally {
257: DbUtils.close(rs, p);
258: }
259: }
260:
261: protected PrivateMessage getPm(ResultSet rs) throws SQLException {
262: return this .getPm(rs, true);
263: }
264:
265: protected PrivateMessage getPm(ResultSet rs, boolean full)
266: throws SQLException {
267: PrivateMessage pm = new PrivateMessage();
268: Post p = new Post();
269:
270: pm.setId(rs.getInt("privmsgs_id"));
271: pm.setType(rs.getInt("privmsgs_type"));
272: p.setTime(new Date(rs.getTimestamp("privmsgs_date").getTime()));
273: p.setSubject(rs.getString("privmsgs_subject"));
274:
275: SimpleDateFormat df = new SimpleDateFormat(SystemGlobals
276: .getValue(ConfigKeys.DATE_TIME_FORMAT));
277: pm.setFormatedDate(df.format(p.getTime()));
278:
279: if (full) {
280: UserDAO um = DataAccessDriver.getInstance().newUserDAO();
281: pm.setFromUser(um.selectById(rs
282: .getInt("privmsgs_from_userid")));
283: pm
284: .setToUser(um.selectById(rs
285: .getInt("privmsgs_to_userid")));
286:
287: p
288: .setBbCodeEnabled(rs
289: .getInt("privmsgs_enable_bbcode") == 1);
290: p
291: .setSignatureEnabled(rs
292: .getInt("privmsgs_attach_sig") == 1);
293: p.setHtmlEnabled(rs.getInt("privmsgs_enable_html") == 1);
294: p
295: .setSmiliesEnabled(rs
296: .getInt("privmsgs_enable_smilies") == 1);
297: p.setText(this .getPmText(rs));
298: }
299:
300: pm.setPost(p);
301:
302: return pm;
303: }
304:
305: protected String getPmText(ResultSet rs) throws SQLException {
306: return rs.getString("privmsgs_text");
307: }
308:
309: /**
310: * @see net.jforum.dao.PrivateMessageDAO#selectById(net.jforum.entities.PrivateMessage)
311: */
312: public PrivateMessage selectById(PrivateMessage pm) {
313: PreparedStatement p = null;
314: ResultSet rs = null;
315: try {
316: p = JForumExecutionContext
317: .getConnection()
318: .prepareStatement(
319: SystemGlobals
320: .getSql("PrivateMessageModel.selectById"));
321: p.setInt(1, pm.getId());
322:
323: rs = p.executeQuery();
324: if (rs.next()) {
325: pm = this .getPm(rs);
326: }
327:
328: return pm;
329: } catch (SQLException e) {
330: throw new DatabaseException(e);
331: } finally {
332: DbUtils.close(rs, p);
333: }
334: }
335:
336: /**
337: * @see net.jforum.dao.PrivateMessageDAO#updateType(net.jforum.entities.PrivateMessage)
338: */
339: public void updateType(PrivateMessage pm) {
340: PreparedStatement p = null;
341: try {
342: p = JForumExecutionContext
343: .getConnection()
344: .prepareStatement(
345: SystemGlobals
346: .getSql("PrivateMessageModel.updateType"));
347: p.setInt(1, pm.getType());
348: p.setInt(2, pm.getId());
349: p.executeUpdate();
350: } catch (SQLException e) {
351: throw new DatabaseException(e);
352: } finally {
353: DbUtils.close(p);
354: }
355: }
356: }
|