Message.java 8.9 KB

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