PrivateMessageAction.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 13k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  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. import java.util.List;
  45. import net.jforum.Command;
  46. import net.jforum.SessionFacade;
  47. import net.jforum.dao.DataAccessDriver;
  48. import net.jforum.dao.PrivateMessageDAO;
  49. import net.jforum.dao.UserDAO;
  50. import net.jforum.entities.Post;
  51. import net.jforum.entities.PrivateMessage;
  52. import net.jforum.entities.PrivateMessageType;
  53. import net.jforum.entities.User;
  54. import net.jforum.entities.UserSession;
  55. import net.jforum.repository.SmiliesRepository;
  56. import net.jforum.util.I18n;
  57. import net.jforum.util.concurrent.Executor;
  58. import net.jforum.util.mail.EmailSenderTask;
  59. import net.jforum.util.mail.PrivateMessageSpammer;
  60. import net.jforum.util.preferences.ConfigKeys;
  61. import net.jforum.util.preferences.SystemGlobals;
  62. import net.jforum.util.preferences.TemplateKeys;
  63. import net.jforum.view.forum.common.PostCommon;
  64. import net.jforum.view.forum.common.ViewCommon;
  65. /**
  66.  * @author Rafael Steil
  67.  * @version $Id: PrivateMessageAction.java,v 1.43 2007/08/17 15:53:28 rafaelsteil Exp $
  68.  */
  69. public class PrivateMessageAction extends Command
  70. {
  71. public void inbox()
  72. {
  73. if (!SessionFacade.isLogged()) {
  74. this.setTemplateName(ViewCommon.contextToLogin());
  75. return;
  76. }
  77. User user = new User();
  78. user.setId(SessionFacade.getUserSession().getUserId());
  79. List pmList = DataAccessDriver.getInstance().newPrivateMessageDAO().selectFromInbox(user);
  80. this.setTemplateName(TemplateKeys.PM_INBOX);
  81. this.context.put("inbox", true);
  82. this.context.put("pmList", pmList);
  83. this.context.put("pageTitle", I18n.getMessage("ForumBase.privateMessages")+" "+I18n.getMessage("PrivateMessage.inbox"));
  84. this.putTypes();
  85. }
  86. public void sentbox()
  87. {
  88. if (!SessionFacade.isLogged()) {
  89. this.setTemplateName(ViewCommon.contextToLogin());
  90. return;
  91. }
  92. User user = new User();
  93. user.setId(SessionFacade.getUserSession().getUserId());
  94. List pmList = DataAccessDriver.getInstance().newPrivateMessageDAO().selectFromSent(user);
  95. this.context.put("sentbox", true);
  96. this.context.put("pmList", pmList);
  97. this.setTemplateName(TemplateKeys.PM_SENTBOX);
  98. this.context.put("pageTitle", I18n.getMessage("ForumBase.privateMessages")+" "+I18n.getMessage("PrivateMessage.sentbox"));
  99. this.putTypes();
  100. }
  101. private void putTypes()
  102. {
  103. this.context.put("NEW", new Integer(PrivateMessageType.NEW));
  104. this.context.put("READ", new Integer(PrivateMessageType.READ));
  105. this.context.put("UNREAD", new Integer(PrivateMessageType.UNREAD));
  106. }
  107. public void send() 
  108. {
  109. if (!SessionFacade.isLogged()) {
  110. this.setTemplateName(ViewCommon.contextToLogin());
  111. return;
  112. }
  113. User user = DataAccessDriver.getInstance().newUserDAO().selectById(
  114. SessionFacade.getUserSession().getUserId());
  115. ViewCommon.prepareUserSignature(user);
  116. this.sendFormCommon(user);
  117. }
  118. public void sendTo()
  119. {
  120. if (!SessionFacade.isLogged()) {
  121. this.setTemplateName(ViewCommon.contextToLogin());
  122. return;
  123. }
  124. User user = DataAccessDriver.getInstance().newUserDAO().selectById(
  125. SessionFacade.getUserSession().getUserId());
  126. int userId = this.request.getIntParameter("user_id");
  127. if (userId > 0){
  128. User recipient = DataAccessDriver.getInstance().newUserDAO().selectById(userId);
  129. this.context.put("pmRecipient", recipient);
  130. this.context.put("toUserId", new Integer(recipient.getId()));
  131. this.context.put("toUsername", recipient.getUsername());
  132. this.context.put("pageTitle", I18n.getMessage("PrivateMessage.title") 
  133. + " " + I18n.getMessage("PrivateMessage.to") 
  134. + " " + recipient.getUsername());
  135. }
  136. this.sendFormCommon(user);
  137. }
  138. private void sendFormCommon(User user)
  139. {
  140. this.setTemplateName(TemplateKeys.PM_SENDFORM);
  141. this.context.put("user", user);
  142. this.context.put("moduleName", "pm");
  143. this.context.put("action", "sendSave");
  144. this.context.put("htmlAllowed", true);
  145. this.context.put("attachmentsEnabled", false);
  146. this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
  147. this.context.put("attachmentsEnabled", false);
  148. this.context.put("maxAttachmentsSize", new Integer(0));
  149. this.context.put("moderationLoggingEnabled", false);
  150. this.context.put("smilies", SmiliesRepository.getSmilies());
  151. }
  152. public void sendSave()
  153. {
  154. if (!SessionFacade.isLogged()) {
  155. this.setTemplateName(ViewCommon.contextToLogin());
  156. return;
  157. }
  158. UserDAO userDao = DataAccessDriver.getInstance().newUserDAO();
  159. String toUserIdStr = this.request.getParameter("toUserId");
  160. String toUsername = this.request.getParameter("toUsername");
  161. int toUserId = -1;
  162. // If we don't have an user id, then probably the user
  163. // inserted the username by hand in the form's field
  164. if (toUserIdStr == null || "".equals(toUserIdStr.trim())) {
  165. List l = userDao.findByName(toUsername, true);
  166. if (l.size() > 0) {
  167. User u = (User)l.get(0);
  168. toUserId = u.getId();
  169. }
  170. }
  171. else {
  172. toUserId = Integer.parseInt(toUserIdStr);
  173. }
  174. // We failed to get the user id?
  175. if (toUserId == -1) {
  176. this.setTemplateName(TemplateKeys.PM_SENDSAVE_USER_NOTFOUND);
  177. this.context.put("message", I18n.getMessage("PrivateMessage.userIdNotFound"));
  178. return;
  179. }
  180. PrivateMessage pm = new PrivateMessage();
  181. pm.setPost(PostCommon.fillPostFromRequest());
  182. // Sender
  183. User fromUser = new User();
  184. fromUser.setId(SessionFacade.getUserSession().getUserId());
  185. pm.setFromUser(fromUser);
  186. // Recipient
  187. User toUser = userDao.selectById(toUserId);
  188. pm.setToUser(toUser);
  189. boolean preview = ("1".equals(this.request.getParameter("preview")));
  190. if (!preview) {
  191. DataAccessDriver.getInstance().newPrivateMessageDAO().send(pm);
  192. this.setTemplateName(TemplateKeys.PM_SENDSAVE);
  193. this.context.put("message", I18n.getMessage("PrivateMessage.messageSent", 
  194. new String[] { this.request.getContextPath() 
  195. + "/pm/inbox"
  196. + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)}));
  197. // If the target user if in the forum, then increments its 
  198. // private messate count
  199. String sid = SessionFacade.isUserInSession(toUserId);
  200. if (sid != null) {
  201. UserSession us = SessionFacade.getUserSession(sid);
  202. us.setPrivateMessages(us.getPrivateMessages() + 1);
  203. }
  204. if (toUser.getEmail() != null 
  205. && toUser.getEmail().trim().length() > 0
  206. && SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) {
  207. Executor.execute(new EmailSenderTask(new PrivateMessageSpammer(toUser)));
  208. }
  209. }
  210. else {
  211. this.context.put("preview", true);
  212. this.context.put("post", pm.getPost());
  213. Post postPreview = new Post(pm.getPost());
  214. this.context.put("postPreview", PostCommon.preparePostForDisplay(postPreview));
  215. this.context.put("pm", pm);
  216. this.send();
  217. }
  218. }
  219. public void findUser()
  220. {
  221. boolean showResult = false;
  222. String username = this.request.getParameter("username");
  223. if (username != null && !username.equals("")) {
  224. List namesList = DataAccessDriver.getInstance().newUserDAO().findByName(username, false);
  225. this.context.put("namesList", namesList);
  226. showResult = true;
  227. }
  228. this.setTemplateName(TemplateKeys.PM_FIND_USER);
  229. this.context.put("username", username);
  230. this.context.put("showResult", showResult);
  231. }
  232. public void read()
  233. {
  234. if (!SessionFacade.isLogged()) {
  235. this.setTemplateName(ViewCommon.contextToLogin());
  236. return;
  237. }
  238. int id = this.request.getIntParameter("id");
  239. PrivateMessage pm = new PrivateMessage();
  240. pm.setId(id);
  241. pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm);
  242. // Don't allow the read of messages that don't belongs
  243. // to the current user
  244. UserSession us = SessionFacade.getUserSession();
  245. int userId = us.getUserId();
  246. if (pm.getToUser().getId() == userId || pm.getFromUser().getId() == userId) {
  247. pm.getPost().setText(PostCommon.preparePostForDisplay(pm.getPost()).getText());
  248. // Update the message status, if needed
  249. if (pm.getType() == PrivateMessageType.NEW) {
  250. pm.setType(PrivateMessageType.READ);
  251. DataAccessDriver.getInstance().newPrivateMessageDAO().updateType(pm);
  252. int totalMessages = us.getPrivateMessages();
  253. if (totalMessages > 0) {
  254. us.setPrivateMessages(totalMessages - 1);
  255. }
  256. }
  257. User u = pm.getFromUser();
  258. ViewCommon.prepareUserSignature(u);
  259.             
  260. this.context.put("pm", pm);
  261. this.setTemplateName(TemplateKeys.PM_READ);
  262. }
  263. else {
  264. this.setTemplateName(TemplateKeys.PM_READ_DENIED);
  265. this.context.put("message", I18n.getMessage("PrivateMessage.readDenied"));
  266. }
  267. }
  268. public void review()
  269. {
  270. this.read();
  271. this.setTemplateName(TemplateKeys.PM_READ_REVIEW);
  272. }
  273. public void delete()
  274. {
  275. if (!SessionFacade.isLogged()) {
  276. this.setTemplateName(ViewCommon.contextToLogin());
  277. return;
  278. }
  279. String ids[] = this.request.getParameterValues("id");
  280. if (ids != null && ids.length > 0) {
  281. PrivateMessage[] deleteList = new PrivateMessage[ids.length];
  282. int unreadCount = 0;
  283. PrivateMessageDAO dao = DataAccessDriver.getInstance().newPrivateMessageDAO();
  284. for (int i = 0; i < ids.length; i++) {
  285. PrivateMessage pm = dao.selectById(new PrivateMessage(Integer.parseInt(ids[i])));
  286. if (pm.getType() == PrivateMessageType.NEW) {
  287. unreadCount++;
  288. }
  289. deleteList[i] = pm;
  290. }
  291. UserSession userSession = SessionFacade.getUserSession();
  292. dao.delete(deleteList, userSession.getUserId());
  293. // Subtracts the number of delete messages
  294. int total = userSession.getPrivateMessages() - unreadCount;
  295. if (total < 0) {
  296. total = 0;
  297. }
  298. userSession.setPrivateMessages(total);
  299. }
  300. this.setTemplateName(TemplateKeys.PM_DELETE);
  301. this.context.put("message", I18n.getMessage("PrivateMessage.deleteDone", 
  302. new String[] { this.request.getContextPath() 
  303. + "/pm/inbox"
  304. + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) }));
  305. }
  306. public void reply()
  307. {
  308. if (!SessionFacade.isLogged()) {
  309. this.setTemplateName(ViewCommon.contextToLogin());
  310. return;
  311. }
  312. int id = this.request.getIntParameter("id");
  313. PrivateMessage pm = new PrivateMessage();
  314. pm.setId(id);
  315. pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm);
  316. int userId = SessionFacade.getUserSession().getUserId();
  317. if (pm.getToUser().getId() != userId && pm.getFromUser().getId() != userId) {
  318. this.setTemplateName(TemplateKeys.PM_READ_DENIED);
  319. this.context.put("message", I18n.getMessage("PrivateMessage.readDenied"));
  320. return;
  321. }
  322. pm.getPost().setSubject(I18n.getMessage("PrivateMessage.replyPrefix") + pm.getPost().getSubject());
  323. this.context.put("pm", pm);
  324. this.context.put("pmReply", true);
  325. this.sendFormCommon(DataAccessDriver.getInstance().newUserDAO().selectById(
  326. SessionFacade.getUserSession().getUserId()));
  327. }
  328. public void quote()
  329. {
  330. if (!SessionFacade.isLogged()) {
  331. this.setTemplateName(ViewCommon.contextToLogin());
  332. return;
  333. }
  334. int id = this.request.getIntParameter("id");
  335. PrivateMessage pm = new PrivateMessage();
  336. pm.setId(id);
  337. pm = DataAccessDriver.getInstance().newPrivateMessageDAO().selectById(pm);
  338. int userId = SessionFacade.getUserSession().getUserId();
  339. if (pm.getToUser().getId() != userId && pm.getFromUser().getId() != userId) {
  340. this.setTemplateName(TemplateKeys.PM_READ_DENIED);
  341. this.context.put("message", I18n.getMessage("PrivateMessage.readDenied"));
  342. return;
  343. }
  344. pm.getPost().setSubject(I18n.getMessage("PrivateMessage.replyPrefix") + pm.getPost().getSubject());
  345. this.sendFormCommon(DataAccessDriver.getInstance().newUserDAO().selectById(userId));
  346. this.context.put("quote", "true");
  347. this.context.put("quoteUser", pm.getFromUser().getUsername());
  348. this.context.put("post", pm.getPost());
  349. this.context.put("pm", pm);
  350. }
  351. /** 
  352.  * @see net.jforum.Command#list()
  353.  */
  354. public void list()
  355. {
  356. this.inbox();
  357. }
  358. }