Message.java 8.8 KB

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