Message.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.entity;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.util.concurrent.ScheduledFuture;
  25. import com.l2jserver.gameserver.ThreadPoolManager;
  26. import com.l2jserver.gameserver.datatables.CharNameTable;
  27. import com.l2jserver.gameserver.idfactory.IdFactory;
  28. import com.l2jserver.gameserver.instancemanager.MailManager;
  29. import com.l2jserver.gameserver.model.itemcontainer.Mail;
  30. import com.l2jserver.util.Rnd;
  31. /**
  32. * @author Migi, DS
  33. */
  34. public class Message
  35. {
  36. private static final int EXPIRATION = 360; // 15 days
  37. private static final int COD_EXPIRATION = 12; // 12 hours
  38. private static final int UNLOAD_ATTACHMENTS_INTERVAL = 900000; // 15-30 mins
  39. // post state
  40. public static final int DELETED = 0;
  41. public static final int READED = 1;
  42. public static final int REJECTED = 2;
  43. private final int _messageId, _senderId, _receiverId;
  44. private final long _expiration;
  45. private String _senderName = null;
  46. private String _receiverName = null;
  47. private final String _subject, _content;
  48. private boolean _unread, _returned;
  49. private int _sendBySystem;
  50. private boolean _deletedBySender;
  51. private boolean _deletedByReceiver;
  52. private final long _reqAdena;
  53. private boolean _hasAttachments;
  54. private Mail _attachments = null;
  55. private ScheduledFuture<?> _unloadTask = null;
  56. public enum SendBySystem
  57. {
  58. PLAYER,
  59. NEWS,
  60. NONE,
  61. ALEGRIA
  62. }
  63. /*
  64. * Constructor for restoring from DB.
  65. */
  66. public Message(ResultSet rset) throws SQLException
  67. {
  68. _messageId = rset.getInt("messageId");
  69. _senderId = rset.getInt("senderId");
  70. _receiverId = rset.getInt("receiverId");
  71. _subject = rset.getString("subject");
  72. _content = rset.getString("content");
  73. _expiration = rset.getLong("expiration");
  74. _reqAdena = rset.getLong("reqAdena");
  75. _hasAttachments = rset.getBoolean("hasAttachments");
  76. _unread = rset.getBoolean("isUnread");
  77. _deletedBySender = rset.getBoolean("isDeletedBySender");
  78. _deletedByReceiver = rset.getBoolean("isDeletedByReceiver");
  79. _sendBySystem = rset.getInt("sendBySystem");
  80. _returned = rset.getBoolean("isReturned");
  81. }
  82. /*
  83. * This constructor used for creating new message.
  84. */
  85. public Message(int senderId, int receiverId, boolean isCod, String subject, String text, long reqAdena)
  86. {
  87. _messageId = IdFactory.getInstance().getNextId();
  88. _senderId = senderId;
  89. _receiverId = receiverId;
  90. _subject = subject;
  91. _content = text;
  92. _expiration = (isCod ? System.currentTimeMillis() + (COD_EXPIRATION * 3600000) : System.currentTimeMillis() + (EXPIRATION * 3600000));
  93. _hasAttachments = false;
  94. _unread = true;
  95. _deletedBySender = false;
  96. _deletedByReceiver = false;
  97. _reqAdena = reqAdena;
  98. }
  99. /*
  100. * This constructor used for System Mails
  101. */
  102. public Message(int receiverId, String subject, String content, SendBySystem sendBySystem)
  103. {
  104. _messageId = IdFactory.getInstance().getNextId();
  105. _senderId = -1;
  106. _receiverId = receiverId;
  107. _subject = subject;
  108. _content = content;
  109. _expiration = System.currentTimeMillis() + (EXPIRATION * 3600000);
  110. _reqAdena = 0;
  111. _hasAttachments = false;
  112. _unread = true;
  113. _deletedBySender = true;
  114. _deletedByReceiver = false;
  115. _sendBySystem = sendBySystem.ordinal();
  116. _returned = false;
  117. }
  118. /*
  119. * This constructor used for auto-generation of the "return attachments" message
  120. */
  121. public Message(Message msg)
  122. {
  123. _messageId = IdFactory.getInstance().getNextId();
  124. _senderId = msg.getSenderId();
  125. _receiverId = msg.getSenderId();
  126. _subject = "";
  127. _content = "";
  128. _expiration = System.currentTimeMillis() + (EXPIRATION * 3600000);
  129. _unread = true;
  130. _deletedBySender = true;
  131. _deletedByReceiver = false;
  132. _sendBySystem = SendBySystem.NONE.ordinal();
  133. _returned = true;
  134. _reqAdena = 0;
  135. _hasAttachments = true;
  136. _attachments = msg.getAttachments();
  137. msg.removeAttachments();
  138. _attachments.setNewMessageId(_messageId);
  139. _unloadTask = ThreadPoolManager.getInstance().scheduleGeneral(new AttachmentsUnloadTask(this), UNLOAD_ATTACHMENTS_INTERVAL + Rnd.get(UNLOAD_ATTACHMENTS_INTERVAL));
  140. }
  141. public static final PreparedStatement getStatement(Message msg, Connection con) throws SQLException
  142. {
  143. PreparedStatement stmt = con.prepareStatement("INSERT INTO messages (messageId, senderId, receiverId, subject, content, expiration, reqAdena, hasAttachments, isUnread, isDeletedBySender, isDeletedByReceiver, sendBySystem, isReturned) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  144. stmt.setInt(1, msg._messageId);
  145. stmt.setInt(2, msg._senderId);
  146. stmt.setInt(3, msg._receiverId);
  147. stmt.setString(4, msg._subject);
  148. stmt.setString(5, msg._content);
  149. stmt.setLong(6, msg._expiration);
  150. stmt.setLong(7, msg._reqAdena);
  151. stmt.setString(8, String.valueOf(msg._hasAttachments));
  152. stmt.setString(9, String.valueOf(msg._unread));
  153. stmt.setString(10, String.valueOf(msg._deletedBySender));
  154. stmt.setString(11, String.valueOf(msg._deletedByReceiver));
  155. stmt.setString(12, String.valueOf(msg._sendBySystem));
  156. stmt.setString(13, String.valueOf(msg._returned));
  157. return stmt;
  158. }
  159. public final int getId()
  160. {
  161. return _messageId;
  162. }
  163. public final int getSenderId()
  164. {
  165. return _senderId;
  166. }
  167. public final int getReceiverId()
  168. {
  169. return _receiverId;
  170. }
  171. public final String getSenderName()
  172. {
  173. if (_senderName == null)
  174. {
  175. if (_sendBySystem != 0)
  176. {
  177. return "****";
  178. }
  179. _senderName = CharNameTable.getInstance().getNameById(_senderId);
  180. if (_senderName == null)
  181. {
  182. _senderName = "";
  183. }
  184. }
  185. return _senderName;
  186. }
  187. public final String getReceiverName()
  188. {
  189. if (_receiverName == null)
  190. {
  191. _receiverName = CharNameTable.getInstance().getNameById(_receiverId);
  192. if (_receiverName == null)
  193. {
  194. _receiverName = "";
  195. }
  196. }
  197. return _receiverName;
  198. }
  199. public final String getSubject()
  200. {
  201. return _subject;
  202. }
  203. public final String getContent()
  204. {
  205. return _content;
  206. }
  207. public final boolean isLocked()
  208. {
  209. return _reqAdena > 0;
  210. }
  211. public final long getExpiration()
  212. {
  213. return _expiration;
  214. }
  215. public final int getExpirationSeconds()
  216. {
  217. return (int) (_expiration / 1000);
  218. }
  219. public final boolean isUnread()
  220. {
  221. return _unread;
  222. }
  223. public final void markAsRead()
  224. {
  225. if (_unread)
  226. {
  227. _unread = false;
  228. MailManager.getInstance().markAsReadInDb(_messageId);
  229. }
  230. }
  231. public final boolean isDeletedBySender()
  232. {
  233. return _deletedBySender;
  234. }
  235. public final void setDeletedBySender()
  236. {
  237. if (!_deletedBySender)
  238. {
  239. _deletedBySender = true;
  240. if (_deletedByReceiver)
  241. {
  242. MailManager.getInstance().deleteMessageInDb(_messageId);
  243. }
  244. else
  245. {
  246. MailManager.getInstance().markAsDeletedBySenderInDb(_messageId);
  247. }
  248. }
  249. }
  250. public final boolean isDeletedByReceiver()
  251. {
  252. return _deletedByReceiver;
  253. }
  254. public final void setDeletedByReceiver()
  255. {
  256. if (!_deletedByReceiver)
  257. {
  258. _deletedByReceiver = true;
  259. if (_deletedBySender)
  260. {
  261. MailManager.getInstance().deleteMessageInDb(_messageId);
  262. }
  263. else
  264. {
  265. MailManager.getInstance().markAsDeletedByReceiverInDb(_messageId);
  266. }
  267. }
  268. }
  269. public final int getSendBySystem()
  270. {
  271. return _sendBySystem;
  272. }
  273. public final boolean isReturned()
  274. {
  275. return _returned;
  276. }
  277. public final void setIsReturned(boolean val)
  278. {
  279. _returned = val;
  280. }
  281. public final long getReqAdena()
  282. {
  283. return _reqAdena;
  284. }
  285. public final synchronized Mail getAttachments()
  286. {
  287. if (!_hasAttachments)
  288. {
  289. return null;
  290. }
  291. if (_attachments == null)
  292. {
  293. _attachments = new Mail(_senderId, _messageId);
  294. _attachments.restore();
  295. _unloadTask = ThreadPoolManager.getInstance().scheduleGeneral(new AttachmentsUnloadTask(this), UNLOAD_ATTACHMENTS_INTERVAL + Rnd.get(UNLOAD_ATTACHMENTS_INTERVAL));
  296. }
  297. return _attachments;
  298. }
  299. public final boolean hasAttachments()
  300. {
  301. return _hasAttachments;
  302. }
  303. public final synchronized void removeAttachments()
  304. {
  305. if (_attachments != null)
  306. {
  307. _attachments = null;
  308. _hasAttachments = false;
  309. MailManager.getInstance().removeAttachmentsInDb(_messageId);
  310. if (_unloadTask != null)
  311. {
  312. _unloadTask.cancel(false);
  313. }
  314. }
  315. }
  316. public final synchronized Mail createAttachments()
  317. {
  318. if (_hasAttachments || (_attachments != null))
  319. {
  320. return null;
  321. }
  322. _attachments = new Mail(_senderId, _messageId);
  323. _hasAttachments = true;
  324. _unloadTask = ThreadPoolManager.getInstance().scheduleGeneral(new AttachmentsUnloadTask(this), UNLOAD_ATTACHMENTS_INTERVAL + Rnd.get(UNLOAD_ATTACHMENTS_INTERVAL));
  325. return _attachments;
  326. }
  327. protected final synchronized void unloadAttachments()
  328. {
  329. if (_attachments != null)
  330. {
  331. _attachments.deleteMe();
  332. _attachments = null;
  333. }
  334. }
  335. static class AttachmentsUnloadTask implements Runnable
  336. {
  337. private Message _msg;
  338. AttachmentsUnloadTask(Message msg)
  339. {
  340. _msg = msg;
  341. }
  342. @Override
  343. public void run()
  344. {
  345. if (_msg != null)
  346. {
  347. _msg.unloadAttachments();
  348. _msg = null;
  349. }
  350. }
  351. }
  352. }