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 - 21:05:45
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum;
044:
045: import java.util.List;
046:
047: import net.jforum.Command;
048: import net.jforum.SessionFacade;
049: import net.jforum.dao.DataAccessDriver;
050: import net.jforum.dao.PrivateMessageDAO;
051: import net.jforum.dao.UserDAO;
052: import net.jforum.entities.Post;
053: import net.jforum.entities.PrivateMessage;
054: import net.jforum.entities.PrivateMessageType;
055: import net.jforum.entities.User;
056: import net.jforum.entities.UserSession;
057: import net.jforum.repository.SmiliesRepository;
058: import net.jforum.util.I18n;
059: import net.jforum.util.concurrent.Executor;
060: import net.jforum.util.mail.EmailSenderTask;
061: import net.jforum.util.mail.PrivateMessageSpammer;
062: import net.jforum.util.preferences.ConfigKeys;
063: import net.jforum.util.preferences.SystemGlobals;
064: import net.jforum.util.preferences.TemplateKeys;
065: import net.jforum.view.forum.common.PostCommon;
066: import net.jforum.view.forum.common.ViewCommon;
067:
068: /**
069: * @author Rafael Steil
070: * @version $Id: PrivateMessageAction.java,v 1.43 2007/08/17 15:53:28 rafaelsteil Exp $
071: */
072: public class PrivateMessageAction extends Command {
073: public void inbox() {
074: if (!SessionFacade.isLogged()) {
075: this .setTemplateName(ViewCommon.contextToLogin());
076: return;
077: }
078:
079: User user = new User();
080: user.setId(SessionFacade.getUserSession().getUserId());
081:
082: List pmList = DataAccessDriver.getInstance()
083: .newPrivateMessageDAO().selectFromInbox(user);
084:
085: this .setTemplateName(TemplateKeys.PM_INBOX);
086: this .context.put("inbox", true);
087: this .context.put("pmList", pmList);
088: this .context.put("pageTitle", I18n
089: .getMessage("ForumBase.privateMessages")
090: + " " + I18n.getMessage("PrivateMessage.inbox"));
091: this .putTypes();
092: }
093:
094: public void sentbox() {
095: if (!SessionFacade.isLogged()) {
096: this .setTemplateName(ViewCommon.contextToLogin());
097: return;
098: }
099:
100: User user = new User();
101: user.setId(SessionFacade.getUserSession().getUserId());
102:
103: List pmList = DataAccessDriver.getInstance()
104: .newPrivateMessageDAO().selectFromSent(user);
105:
106: this .context.put("sentbox", true);
107: this .context.put("pmList", pmList);
108: this .setTemplateName(TemplateKeys.PM_SENTBOX);
109: this .context.put("pageTitle", I18n
110: .getMessage("ForumBase.privateMessages")
111: + " " + I18n.getMessage("PrivateMessage.sentbox"));
112: this .putTypes();
113: }
114:
115: private void putTypes() {
116: this .context.put("NEW", new Integer(PrivateMessageType.NEW));
117: this .context.put("READ", new Integer(PrivateMessageType.READ));
118: this .context.put("UNREAD", new Integer(
119: PrivateMessageType.UNREAD));
120: }
121:
122: public void send() {
123: if (!SessionFacade.isLogged()) {
124: this .setTemplateName(ViewCommon.contextToLogin());
125: return;
126: }
127:
128: User user = DataAccessDriver.getInstance().newUserDAO()
129: .selectById(SessionFacade.getUserSession().getUserId());
130:
131: ViewCommon.prepareUserSignature(user);
132:
133: this .sendFormCommon(user);
134: }
135:
136: public void sendTo() {
137: if (!SessionFacade.isLogged()) {
138: this .setTemplateName(ViewCommon.contextToLogin());
139: return;
140: }
141:
142: User user = DataAccessDriver.getInstance().newUserDAO()
143: .selectById(SessionFacade.getUserSession().getUserId());
144:
145: int userId = this .request.getIntParameter("user_id");
146:
147: if (userId > 0) {
148: User recipient = DataAccessDriver.getInstance()
149: .newUserDAO().selectById(userId);
150:
151: this .context.put("pmRecipient", recipient);
152: this .context
153: .put("toUserId", new Integer(recipient.getId()));
154: this .context.put("toUsername", recipient.getUsername());
155: this .context.put("pageTitle", I18n
156: .getMessage("PrivateMessage.title")
157: + " "
158: + I18n.getMessage("PrivateMessage.to")
159: + " "
160: + recipient.getUsername());
161: }
162:
163: this .sendFormCommon(user);
164: }
165:
166: private void sendFormCommon(User user) {
167: this .setTemplateName(TemplateKeys.PM_SENDFORM);
168:
169: this .context.put("user", user);
170: this .context.put("moduleName", "pm");
171: this .context.put("action", "sendSave");
172: this .context.put("htmlAllowed", true);
173: this .context.put("attachmentsEnabled", false);
174: this .context.put("maxAttachments", SystemGlobals
175: .getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
176: this .context.put("attachmentsEnabled", false);
177: this .context.put("maxAttachmentsSize", new Integer(0));
178: this .context.put("moderationLoggingEnabled", false);
179: this .context.put("smilies", SmiliesRepository.getSmilies());
180: }
181:
182: public void sendSave() {
183: if (!SessionFacade.isLogged()) {
184: this .setTemplateName(ViewCommon.contextToLogin());
185: return;
186: }
187:
188: UserDAO userDao = DataAccessDriver.getInstance().newUserDAO();
189:
190: String toUserIdStr = this .request.getParameter("toUserId");
191: String toUsername = this .request.getParameter("toUsername");
192:
193: int toUserId = -1;
194:
195: // If we don't have an user id, then probably the user
196: // inserted the username by hand in the form's field
197: if (toUserIdStr == null || "".equals(toUserIdStr.trim())) {
198: List l = userDao.findByName(toUsername, true);
199:
200: if (l.size() > 0) {
201: User u = (User) l.get(0);
202: toUserId = u.getId();
203: }
204: } else {
205: toUserId = Integer.parseInt(toUserIdStr);
206: }
207:
208: // We failed to get the user id?
209: if (toUserId == -1) {
210: this
211: .setTemplateName(TemplateKeys.PM_SENDSAVE_USER_NOTFOUND);
212: this .context.put("message", I18n
213: .getMessage("PrivateMessage.userIdNotFound"));
214: return;
215: }
216:
217: PrivateMessage pm = new PrivateMessage();
218: pm.setPost(PostCommon.fillPostFromRequest());
219:
220: // Sender
221: User fromUser = new User();
222: fromUser.setId(SessionFacade.getUserSession().getUserId());
223: pm.setFromUser(fromUser);
224:
225: // Recipient
226: User toUser = userDao.selectById(toUserId);
227: pm.setToUser(toUser);
228:
229: boolean preview = ("1".equals(this .request
230: .getParameter("preview")));
231:
232: if (!preview) {
233: DataAccessDriver.getInstance().newPrivateMessageDAO().send(
234: pm);
235:
236: this .setTemplateName(TemplateKeys.PM_SENDSAVE);
237: this .context
238: .put(
239: "message",
240: I18n
241: .getMessage(
242: "PrivateMessage.messageSent",
243: new String[] { this .request
244: .getContextPath()
245: + "/pm/inbox"
246: + SystemGlobals
247: .getValue(ConfigKeys.SERVLET_EXTENSION) }));
248:
249: // If the target user if in the forum, then increments its
250: // private messate count
251: String sid = SessionFacade.isUserInSession(toUserId);
252: if (sid != null) {
253: UserSession us = SessionFacade.getUserSession(sid);
254: us.setPrivateMessages(us.getPrivateMessages() + 1);
255: }
256:
257: if (toUser.getEmail() != null
258: && toUser.getEmail().trim().length() > 0
259: && SystemGlobals
260: .getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) {
261: Executor.execute(new EmailSenderTask(
262: new PrivateMessageSpammer(toUser)));
263: }
264: } else {
265: this .context.put("preview", true);
266: this .context.put("post", pm.getPost());
267:
268: Post postPreview = new Post(pm.getPost());
269: this .context.put("postPreview", PostCommon
270: .preparePostForDisplay(postPreview));
271: this .context.put("pm", pm);
272:
273: this .send();
274: }
275: }
276:
277: public void findUser() {
278: boolean showResult = false;
279: String username = this .request.getParameter("username");
280:
281: if (username != null && !username.equals("")) {
282: List namesList = DataAccessDriver.getInstance()
283: .newUserDAO().findByName(username, false);
284: this .context.put("namesList", namesList);
285: showResult = true;
286: }
287:
288: this .setTemplateName(TemplateKeys.PM_FIND_USER);
289:
290: this .context.put("username", username);
291: this .context.put("showResult", showResult);
292: }
293:
294: public void read() {
295: if (!SessionFacade.isLogged()) {
296: this .setTemplateName(ViewCommon.contextToLogin());
297: return;
298: }
299:
300: int id = this .request.getIntParameter("id");
301:
302: PrivateMessage pm = new PrivateMessage();
303: pm.setId(id);
304:
305: pm = DataAccessDriver.getInstance().newPrivateMessageDAO()
306: .selectById(pm);
307:
308: // Don't allow the read of messages that don't belongs
309: // to the current user
310: UserSession us = SessionFacade.getUserSession();
311: int userId = us.getUserId();
312:
313: if (pm.getToUser().getId() == userId
314: || pm.getFromUser().getId() == userId) {
315: pm.getPost().setText(
316: PostCommon.preparePostForDisplay(pm.getPost())
317: .getText());
318:
319: // Update the message status, if needed
320: if (pm.getType() == PrivateMessageType.NEW) {
321: pm.setType(PrivateMessageType.READ);
322: DataAccessDriver.getInstance().newPrivateMessageDAO()
323: .updateType(pm);
324:
325: int totalMessages = us.getPrivateMessages();
326:
327: if (totalMessages > 0) {
328: us.setPrivateMessages(totalMessages - 1);
329: }
330: }
331:
332: User u = pm.getFromUser();
333: ViewCommon.prepareUserSignature(u);
334:
335: this .context.put("pm", pm);
336: this .setTemplateName(TemplateKeys.PM_READ);
337: } else {
338: this .setTemplateName(TemplateKeys.PM_READ_DENIED);
339: this .context.put("message", I18n
340: .getMessage("PrivateMessage.readDenied"));
341: }
342: }
343:
344: public void review() {
345: this .read();
346: this .setTemplateName(TemplateKeys.PM_READ_REVIEW);
347: }
348:
349: public void delete() {
350: if (!SessionFacade.isLogged()) {
351: this .setTemplateName(ViewCommon.contextToLogin());
352: return;
353: }
354:
355: String ids[] = this .request.getParameterValues("id");
356:
357: if (ids != null && ids.length > 0) {
358: PrivateMessage[] deleteList = new PrivateMessage[ids.length];
359:
360: int unreadCount = 0;
361: PrivateMessageDAO dao = DataAccessDriver.getInstance()
362: .newPrivateMessageDAO();
363:
364: for (int i = 0; i < ids.length; i++) {
365: PrivateMessage pm = dao.selectById(new PrivateMessage(
366: Integer.parseInt(ids[i])));
367:
368: if (pm.getType() == PrivateMessageType.NEW) {
369: unreadCount++;
370: }
371:
372: deleteList[i] = pm;
373: }
374:
375: UserSession userSession = SessionFacade.getUserSession();
376:
377: dao.delete(deleteList, userSession.getUserId());
378:
379: // Subtracts the number of delete messages
380: int total = userSession.getPrivateMessages() - unreadCount;
381:
382: if (total < 0) {
383: total = 0;
384: }
385:
386: userSession.setPrivateMessages(total);
387: }
388:
389: this .setTemplateName(TemplateKeys.PM_DELETE);
390: this .context
391: .put(
392: "message",
393: I18n
394: .getMessage(
395: "PrivateMessage.deleteDone",
396: new String[] { this .request
397: .getContextPath()
398: + "/pm/inbox"
399: + SystemGlobals
400: .getValue(ConfigKeys.SERVLET_EXTENSION) }));
401: }
402:
403: public void reply() {
404: if (!SessionFacade.isLogged()) {
405: this .setTemplateName(ViewCommon.contextToLogin());
406: return;
407: }
408:
409: int id = this .request.getIntParameter("id");
410:
411: PrivateMessage pm = new PrivateMessage();
412: pm.setId(id);
413: pm = DataAccessDriver.getInstance().newPrivateMessageDAO()
414: .selectById(pm);
415:
416: int userId = SessionFacade.getUserSession().getUserId();
417:
418: if (pm.getToUser().getId() != userId
419: && pm.getFromUser().getId() != userId) {
420: this .setTemplateName(TemplateKeys.PM_READ_DENIED);
421: this .context.put("message", I18n
422: .getMessage("PrivateMessage.readDenied"));
423: return;
424: }
425:
426: pm.getPost().setSubject(
427: I18n.getMessage("PrivateMessage.replyPrefix")
428: + pm.getPost().getSubject());
429:
430: this .context.put("pm", pm);
431: this .context.put("pmReply", true);
432:
433: this
434: .sendFormCommon(DataAccessDriver.getInstance()
435: .newUserDAO().selectById(
436: SessionFacade.getUserSession()
437: .getUserId()));
438: }
439:
440: public void quote() {
441: if (!SessionFacade.isLogged()) {
442: this .setTemplateName(ViewCommon.contextToLogin());
443: return;
444: }
445:
446: int id = this .request.getIntParameter("id");
447:
448: PrivateMessage pm = new PrivateMessage();
449: pm.setId(id);
450: pm = DataAccessDriver.getInstance().newPrivateMessageDAO()
451: .selectById(pm);
452:
453: int userId = SessionFacade.getUserSession().getUserId();
454:
455: if (pm.getToUser().getId() != userId
456: && pm.getFromUser().getId() != userId) {
457: this .setTemplateName(TemplateKeys.PM_READ_DENIED);
458: this .context.put("message", I18n
459: .getMessage("PrivateMessage.readDenied"));
460: return;
461: }
462:
463: pm.getPost().setSubject(
464: I18n.getMessage("PrivateMessage.replyPrefix")
465: + pm.getPost().getSubject());
466:
467: this .sendFormCommon(DataAccessDriver.getInstance().newUserDAO()
468: .selectById(userId));
469:
470: this .context.put("quote", "true");
471: this .context.put("quoteUser", pm.getFromUser().getUsername());
472: this .context.put("post", pm.getPost());
473: this .context.put("pm", pm);
474: }
475:
476: /**
477: * @see net.jforum.Command#list()
478: */
479: public void list() {
480: this.inbox();
481: }
482: }
|