Message.java 9.8 KB

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