KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > generic > GenericPrivateMessageDAO


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: 20/05/2004 - 15:51:10
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.dao.generic;
44
45 import java.sql.PreparedStatement JavaDoc;
46 import java.sql.ResultSet JavaDoc;
47 import java.sql.Timestamp JavaDoc;
48 import java.text.SimpleDateFormat JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.List JavaDoc;
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 /**
63  * @author Rafael Steil
64  * @version $Id: GenericPrivateMessageDAO.java,v 1.6 2006/01/29 15:06:25 rafaelsteil Exp $
65  */

66 public class GenericPrivateMessageDAO extends AutoKeys implements net.jforum.dao.PrivateMessageDAO
67 {
68     /**
69      * @see net.jforum.dao.PrivateMessageDAO#send(net.jforum.entities.PrivateMessage)
70      */

71     public void send(PrivateMessage pm) throws Exception JavaDoc
72     {
73         // We should store 2 copies: one for the sendee's sent box
74
// and another for the target user's inbox.
75
PreparedStatement JavaDoc p = this.getStatementForAutoKeys("PrivateMessageModel.add");
76
77         // Sendee's sent box
78
this.addPm(pm, p);
79         this.addPmText(pm);
80         
81         // Target user's inbox
82
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 JavaDoc
91     {
92         PreparedStatement JavaDoc 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 JavaDoc p) throws Exception JavaDoc
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 JavaDoc(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     /**
119      * @see net.jforum.dao.PrivateMessageDAO#delete(net.jforum.entities.PrivateMessage[])
120      */

121     public void delete(PrivateMessage[] pm) throws Exception JavaDoc
122     {
123         PreparedStatement JavaDoc 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 JavaDoc 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     /**
142      * @see net.jforum.dao.PrivateMessageDAO#selectFromInbox(net.jforum.entities.User)
143      */

144     public List JavaDoc selectFromInbox(User user) throws Exception JavaDoc
145     {
146         String JavaDoc query = SystemGlobals.getSql("PrivateMessageModel.baseListing");
147         query = query.replaceAll("#FILTER#", SystemGlobals.getSql("PrivateMessageModel.inbox"));
148         
149         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(query);
150         p.setInt(1, user.getId());
151         
152         List JavaDoc pmList = new ArrayList JavaDoc();
153
154         ResultSet JavaDoc 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     /**
174      * @see net.jforum.dao.PrivateMessageDAO#selectFromSent(net.jforum.entities.User)
175      */

176     public List JavaDoc selectFromSent(User user) throws Exception JavaDoc
177     {
178         String JavaDoc query = SystemGlobals.getSql("PrivateMessageModel.baseListing");
179         query = query.replaceAll("#FILTER#", SystemGlobals.getSql("PrivateMessageModel.sent"));
180         
181         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(query);
182         p.setInt(1, user.getId());
183         
184         List JavaDoc pmList = new ArrayList JavaDoc();
185
186         ResultSet JavaDoc 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 JavaDoc rs) throws Exception JavaDoc
206     {
207         return this.getPm(rs, true);
208     }
209     
210     protected PrivateMessage getPm(ResultSet JavaDoc rs, boolean full) throws Exception JavaDoc
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 JavaDoc df = new SimpleDateFormat JavaDoc(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 JavaDoc getPmText(ResultSet JavaDoc rs) throws Exception JavaDoc
241     {
242         return rs.getString("privmsgs_text");
243     }
244
245     /**
246      * @see net.jforum.dao.PrivateMessageDAO#selectById(net.jforum.entities.PrivateMessage)
247      */

248     public PrivateMessage selectById(PrivateMessage pm) throws Exception JavaDoc
249     {
250         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PrivateMessageModel.selectById"));
251         p.setInt(1, pm.getId());
252         
253         ResultSet JavaDoc 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     /**
265      * @see net.jforum.dao.PrivateMessageDAO#updateType(net.jforum.entities.PrivateMessage)
266      */

267     public void updateType(PrivateMessage pm) throws Exception JavaDoc
268     {
269         PreparedStatement JavaDoc 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