KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > forum > PrivateMessageAction


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 - 21:05:45
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum;
44
45 import java.util.List JavaDoc;
46
47 import net.jforum.Command;
48 import net.jforum.SessionFacade;
49 import net.jforum.dao.DataAccessDriver;
50 import net.jforum.dao.UserDAO;
51 import net.jforum.entities.Post;
52 import net.jforum.entities.PrivateMessage;
53 import net.jforum.entities.PrivateMessageType;
54 import net.jforum.entities.User;
55 import net.jforum.entities.UserSession;
56 import net.jforum.repository.SmiliesRepository;
57 import net.jforum.util.I18n;
58 import net.jforum.util.concurrent.executor.QueuedExecutor;
59 import net.jforum.util.mail.EmailSenderTask;
60 import net.jforum.util.mail.PrivateMessageSpammer;
61 import net.jforum.util.preferences.ConfigKeys;
62 import net.jforum.util.preferences.SystemGlobals;
63 import net.jforum.util.preferences.TemplateKeys;
64 import net.jforum.view.forum.common.PostCommon;
65 import net.jforum.view.forum.common.ViewCommon;
66
67 /**
68  * @author Rafael Steil
69  * @version $Id: PrivateMessageAction.java,v 1.34 2006/02/21 13:59:49 rafaelsteil Exp $
70  */

71 public class PrivateMessageAction extends Command
72 {
73     public void inbox() throws Exception JavaDoc
74     {
75         if (!SessionFacade.isLogged()) {
76             this.setTemplateName(ViewCommon.contextToLogin());
77             return;
78         }
79         
80         User user = new User();
81         user.setId(SessionFacade.getUserSession().getUserId());
82         
83         List JavaDoc pmList = DataAccessDriver.getInstance().newPrivateMessageDAO().selectFromInbox(user);
84
85         this.context.put("inbox", true);
86         this.context.put("pmList", pmList);
87         this.setTemplateName(TemplateKeys.PM_INBOX);
88         this.context.put("pageTitle", I18n.getMessage("ForumBase.privateMessages")+" "+I18n.getMessage("PrivateMessage.inbox"));
89         this.putTypes();
90     }
91     
92     public void sentbox() throws Exception JavaDoc
93     {
94         if (!SessionFacade.isLogged()) {
95             this.setTemplateName(ViewCommon.contextToLogin());
96             return;
97         }
98         
99         User user = new User();
100         user.setId(SessionFacade.getUserSession().getUserId());
101         
102         List JavaDoc pmList = DataAccessDriver.getInstance().newPrivateMessageDAO().selectFromSent(user);
103
104         this.context.put("sentbox", true);
105         this.context.put("pmList", pmList);
106         this.setTemplateName(TemplateKeys.PM_SENTBOX);
107         this.context.put("pageTitle", I18n.getMessage("ForumBase.privateMessages")+" "+I18n.getMessage("PrivateMessage.sentbox"));
108         this.putTypes();
109     }
110     
111     private void putTypes()
112     {
113         this.context.put("NEW", new Integer JavaDoc(PrivateMessageType.NEW));
114         this.context.put("READ", new Integer JavaDoc(PrivateMessageType.READ));
115         this.context.put("UNREAD", new Integer JavaDoc(PrivateMessageType.UNREAD));
116     }
117     
118     public void send() throws Exception JavaDoc
119     {
120         if (!SessionFacade.isLogged()) {
121             this.setTemplateName(ViewCommon.contextToLogin());
122             return;
123         }
124         
125         User user = DataAccessDriver.getInstance().newUserDAO().selectById(
126                         SessionFacade.getUserSession().getUserId());
127         user.setSignature(PostCommon.processText(user.getSignature()));
128         user.setSignature(PostCommon.processSmilies(user.getSignature(), SmiliesRepository.getSmilies()));
129
130         this.sendFormCommon(user);
131     }
132     
133     public void sendTo() throws Exception JavaDoc
134     {
135         if (!SessionFacade.isLogged()) {
136             this.setTemplateName(ViewCommon.contextToLogin());
137             return;
138         }
139         
140         User user = DataAccessDriver.getInstance().newUserDAO().selectById(
141                 SessionFacade.getUserSession().getUserId());
142
143         int userId = this.request.getIntParameter("user_id");
144         
145         if (userId > 0){
146             User recipient = DataAccessDriver.getInstance().newUserDAO().selectById(userId);
147             
148             this.context.put("pmRecipient", recipient);
149             this.context.put("toUserId", new Integer JavaDoc(recipient.getId()));
150             this.context.put("toUsername", recipient.getUsername());
151             this.context.put("pageTitle", I18n.getMessage("PrivateMessage.title")
152                 + " " + I18n.getMessage("PrivateMessage.to")
153                 + " " + recipient.getUsername());
154         }
155
156         this.sendFormCommon(user);
157     }
158     
159     private void sendFormCommon(User user)
160     {
161         this.setTemplateName(TemplateKeys.PM_SENDFORM);
162         
163         this.context.put("user", user);
164         this.context.put("moduleName", "pm");
165         this.context.put("action", "sendSave");
166         this.context.put("htmlAllowed", true);
167         this.context.put("attachmentsEnabled", false);
168         this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
169         this.context.put("attachmentsEnabled", false);
170         this.context.put("maxAttachmentsSize", new Integer JavaDoc(0));
171         this.context.put("smilies", SmiliesRepository.getSmilies());
172     }
173     
174     public void sendSave() throws Exception JavaDoc
175     {
176         if (!SessionFacade.isLogged()) {
177             this.setTemplateName(ViewCommon.contextToLogin());
178             return;
179         }
180         
181         UserDAO userDao = DataAccessDriver.getInstance().newUserDAO();
182         
183         String JavaDoc toUserIdStr = this.request.getParameter("toUserId");
184         String JavaDoc toUsername = this.request.getParameter("toUsername");
185         String JavaDoc userEmail;
186         
187         int toUserId = -1;
188         
189         // If we don't have an user id, then probably the user
190
// inserted the username by hand in the form's field
191
if (toUserIdStr == null || "".equals(toUserIdStr.trim())) {
192             List JavaDoc l = userDao.findByName(toUsername, true);
193             
194             if (l.size() > 0) {
195                 User u = (User)l.get(0);
196                 toUserId = u.getId();
197                 userEmail = u.getEmail();
198             }
199         }
200         else {
201             toUserId = Integer.parseInt(toUserIdStr);
202         }
203         
204         // We failed to get the user id?
205
if (toUserId == -1) {
206             this.setTemplateName(TemplateKeys.PM_SENDSAVE_USER_NOTFOUND);
207             this.context.put("message", I18n.getMessage("PrivateMessage.userIdNotFound"));
208             return;
209         }
210         
211         PrivateMessage pm = new PrivateMessage();
212         pm.setPost(PostCommon.fillPostFromRequest());
213         
214         // Sender
215
User fromUser = new User();
216         fromUser.setId(SessionFacade.getUserSession().getUserId());
217         pm.setFromUser(fromUser);
218         
219         // Recipient
220
User toUser = userDao.selectById(toUserId);
221         pm.setToUser(toUser);
222         
223         boolean preview = ("1".equals(this.request.getParameter("preview")));
224
225         if (!preview) {
226             DataAccessDriver.getInstance().newPrivateMessageDAO().send(pm);
227             
228             this.setTemplateName(TemplateKeys.PM_SENDSAVE);
229             this.context.put("message", I18n.getMessage("PrivateMessage.messageSent",
230                             new String JavaDoc[] { this.request.getContextPath() +"/pm/inbox"
231                                             + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)}));
232             
233             // If the target user if in the forum, then increments its
234
// private messate count
235
String JavaDoc sid = SessionFacade.isUserInSession(toUserId);
236             if (sid != null) {
237                 UserSession us = SessionFacade.getUserSession(sid);
238                 us.setPrivateMessages(us.getPrivateMessages() + 1);
239             }
240             
241             if (toUser.getEmail() != null && toUser.getEmail().trim().length() > 0) {
242                 if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) {
243                     try {
244                         QueuedExecutor.getInstance().execute(new EmailSenderTask(new PrivateMessageSpammer(toUser)));
245                     }
246                     catch (Exception JavaDoc e) {
247                         System.out.println(e);
248                     }
249                 }
250             }
251         }
252         else {
253             this.context.put("preview", true);
254             this.context.put("post", pm.getPost());
255             
256             Post postPreview = new Post(pm.getPost());
257             this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview));
258             this.context.put("pm", pm);
259
260             this.send();
261         }
262     }
263     
264     public void findUser() throws Exception JavaDoc
265     {
266         boolean showResult = false;
267         String JavaDoc username = this.request.getParameter("username");
268
269         if (username != null && !username.equals("")) {
270             List JavaDoc namesList = DataAccessDriver.getInstance().newUserDAO().findByName(username, false);
271             this.context.put("namesList", namesList);
272             showResult = true;
273         }
274
275         this.setTemplateName(TemplateKeys.PM_FIND_USER);
276         
277         this.context.put("username", username);
278         this.context.put("showResult", showResult);
279     }
280     
281     public void read() throws Exception JavaDoc
282     {
283         if (!SessionFacade.isLogged()) {
284             this.setTemplateName(ViewCommon.contextToLogin());
285             return;
286         }
287         
288         int id = this.request.getIntParameter("id");
289         
290         PrivateMessage pm = new PrivateMessage();
291         pm.setId(id);
292         
293         pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm);
294         
295         // Don't allow the read of messages that don't belongs
296
// to the current user
297
UserSession us = SessionFacade.getUserSession();
298         int userId = us.getUserId();
299         
300         if (pm.getToUser().getId() == userId || pm.getFromUser().getId() == userId) {
301             pm.getPost().setText(PostCommon.preparePostForDisplay(pm.getPost()).getText());
302             
303             // Update the message status, if needed
304
if (pm.getType() == PrivateMessageType.NEW) {
305                 pm.setType(PrivateMessageType.READ);
306                 DataAccessDriver.getInstance().newPrivateMessageDAO().updateType(pm);
307                 us.setPrivateMessages(us.getPrivateMessages() - 1);
308             }
309             
310             User u = pm.getFromUser();
311             ViewCommon.prepareUserSignature(u);
312             
313             this.context.put("pm", pm);
314             this.setTemplateName(TemplateKeys.PM_READ);
315         }
316         else {
317             this.setTemplateName(TemplateKeys.PM_READ_DENIED);
318             this.context.put("message", I18n.getMessage("PrivateMessage.readDenied"));
319         }
320     }
321     
322     public void review() throws Exception JavaDoc
323     {
324         this.read();
325         this.setTemplateName(TemplateKeys.PM_READ_REVIEW);
326     }
327     
328     public void delete() throws Exception JavaDoc
329     {
330         if (!SessionFacade.isLogged()) {
331             this.setTemplateName(ViewCommon.contextToLogin());
332             return;
333         }
334         
335         String JavaDoc ids[] = this.request.getParameterValues("id");
336         
337         if (ids != null && ids.length > 0) {
338             PrivateMessage[] pm = new PrivateMessage[ids.length];
339             User u = new User();
340             u.setId(SessionFacade.getUserSession().getUserId());
341     
342             for (int i = 0; i < ids.length; i++) {
343                 pm[i] = new PrivateMessage();
344                 pm[i].setFromUser(u);
345                 pm[i].setId(Integer.parseInt(ids[i]));
346             }
347             
348             DataAccessDriver.getInstance().newPrivateMessageDAO().delete(pm);
349         }
350         
351         this.setTemplateName(TemplateKeys.PM_DELETE);
352         this.context.put("message", I18n.getMessage("PrivateMessage.deleteDone",
353                         new String JavaDoc[] { this.request.getContextPath() + "/pm/inbox"
354                                         + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) }));
355     }
356     
357     public void reply() throws Exception JavaDoc
358     {
359         if (!SessionFacade.isLogged()) {
360             this.setTemplateName(ViewCommon.contextToLogin());
361             return;
362         }
363         
364         int id = this.request.getIntParameter("id");
365         
366         PrivateMessage pm = new PrivateMessage();
367         pm.setId(id);
368         pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm);
369         
370         int userId = SessionFacade.getUserSession().getUserId();
371         
372         if (pm.getToUser().getId() != userId && pm.getFromUser().getId() != userId) {
373             this.setTemplateName(TemplateKeys.PM_READ_DENIED);
374             this.context.put("message", I18n.getMessage("PrivateMessage.readDenied"));
375             return;
376         }
377         
378         pm.getPost().setSubject(I18n.getMessage("PrivateMessage.replyPrefix") + pm.getPost().getSubject());
379         
380         this.context.put("pm", pm);
381         this.context.put("pmReply", true);
382         
383         this.sendFormCommon(DataAccessDriver.getInstance().newUserDAO().selectById(
384                 SessionFacade.getUserSession().getUserId()));
385     }
386     
387     public void quote() throws Exception JavaDoc
388     {
389         if (!SessionFacade.isLogged()) {
390             this.setTemplateName(ViewCommon.contextToLogin());
391             return;
392         }
393         
394         int id = this.request.getIntParameter("id");
395         
396         PrivateMessage pm = new PrivateMessage();
397         pm.setId(id);
398         pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm);
399
400         int userId = SessionFacade.getUserSession().getUserId();
401         
402         if (pm.getToUser().getId() != userId && pm.getFromUser().getId() != userId) {
403             this.setTemplateName(TemplateKeys.PM_READ_DENIED);
404             this.context.put("message", I18n.getMessage("PrivateMessage.readDenied"));
405             return;
406         }
407         
408         pm.getPost().setSubject(I18n.getMessage("PrivateMessage.replyPrefix") + pm.getPost().getSubject());
409         
410         this.sendFormCommon(DataAccessDriver.getInstance().newUserDAO().selectById(userId));
411         
412         this.context.put("quote", "true");
413         this.context.put("quoteUser", pm.getFromUser().getUsername());
414         this.context.put("post", pm.getPost());
415         this.context.put("pm", pm);
416     }
417     
418     /**
419      * @see net.jforum.Command#list()
420      */

421     public void list() throws Exception JavaDoc
422     {
423         this.inbox();
424     }
425 }
426
Popular Tags