Message.java 9.7 KB

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