Post.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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.communityserver.model;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.Collection;
  19. import java.util.Map;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastMap;
  22. import com.l2jserver.communityserver.L2DatabaseFactory;
  23. import com.l2jserver.communityserver.model.Topic.ConstructorType;
  24. public class Post
  25. {
  26. private static Logger _log = Logger.getLogger(Post.class.getName());
  27. // type
  28. public static final int ADVERTISE = 0;
  29. public static final int MISCELLANEOUS = 1;
  30. public static final int INFORMATION = 2;
  31. private final int _sqlDPId;
  32. private int _postId;
  33. private int _postOwnerId;
  34. private String _postRecipientList;
  35. private int _postParentId;
  36. private long _postDate;
  37. private int _postTopicId;
  38. private int _postForumId;
  39. private String _postTitle;
  40. private String _postTxt;
  41. private int _postType;
  42. private int _lastCommentId;
  43. private Map<Integer, Comment> _comments;
  44. private int _readCount;
  45. /**
  46. * @param restore
  47. * @param t
  48. */
  49. //public enum ConstructorType {REPLY, CREATE };
  50. public Post(ConstructorType ct, final int sqlDPId, int postId, int postOwnerID,String recipentList,long date,int tid,int postForumID, String title, String txt, int type, int readCount)
  51. {
  52. _sqlDPId = sqlDPId;
  53. _postId = postId;
  54. _postOwnerId = postOwnerID;
  55. _postRecipientList = recipentList;
  56. _postDate = date;
  57. _postTopicId = tid;
  58. _postForumId = postForumID;
  59. _postTitle = title;
  60. _postTxt = txt;
  61. _postType = type;
  62. _postParentId = -1;
  63. _comments = new FastMap<Integer, Comment>();
  64. _readCount = readCount;
  65. if (ct == ConstructorType.CREATE)
  66. {
  67. insertindb();
  68. }
  69. else
  70. {
  71. loadComments();
  72. }
  73. }
  74. public Post(ConstructorType ct, final int sqlDPId, int postId, int postOwnerID, String recipentList,int postParentId, long date, int tid, int postForumID, String title, String txt, int type, int readCount)
  75. {
  76. _sqlDPId = sqlDPId;
  77. _postId = postId;
  78. _postOwnerId = postOwnerID;
  79. _postRecipientList = recipentList;
  80. _postDate = date;
  81. _postTopicId = tid;
  82. _postForumId = postForumID;
  83. _postTitle = title;
  84. _postTxt = txt;
  85. _postType = type;
  86. _postParentId = postParentId;
  87. _comments = new FastMap<Integer, Comment>();
  88. _readCount = readCount;
  89. if (ct == ConstructorType.CREATE)
  90. {
  91. insertindb();
  92. }
  93. else
  94. {
  95. loadComments();
  96. }
  97. }
  98. private void loadComments()
  99. {
  100. java.sql.Connection con = null;
  101. try
  102. {
  103. con = L2DatabaseFactory.getInstance().getConnection();
  104. PreparedStatement statement = con.prepareStatement("SELECT * FROM comments WHERE serverId=? AND comment_forum_id=? AND comment_topic_id=? AND comment_post_id=?");
  105. statement.setInt(1, _sqlDPId);
  106. statement.setInt(2, _postForumId);
  107. statement.setInt(3, _postTopicId);
  108. statement.setInt(4, _postId);
  109. ResultSet result = statement.executeQuery();
  110. while (result.next())
  111. {
  112. int commentId = Integer.parseInt(result.getString("comment_id"));
  113. int commentOwner = Integer.parseInt(result.getString("comment_ownerid"));
  114. long date = Long.parseLong(result.getString("comment_date"));
  115. String text = result.getString("comment_txt");
  116. Comment c = new Comment(ConstructorType.RESTORE, _sqlDPId, commentId, commentOwner, date, _postId, _postTopicId, _postForumId, text);
  117. _comments.put(commentId, c);
  118. if (commentId > _lastCommentId)
  119. _lastCommentId = commentId;
  120. }
  121. result.close();
  122. statement.close();
  123. }
  124. catch (Exception e)
  125. {
  126. _log.warning("data error on Forum " + _postForumId + " : " + e);
  127. e.printStackTrace();
  128. }
  129. finally
  130. {
  131. try
  132. {
  133. con.close();
  134. }
  135. catch (Exception e)
  136. {
  137. }
  138. }
  139. }
  140. public int getNewCommentId()
  141. {
  142. return ++_lastCommentId;
  143. }
  144. public void insertindb()
  145. {
  146. java.sql.Connection con = null;
  147. try
  148. {
  149. con = L2DatabaseFactory.getInstance().getConnection();
  150. PreparedStatement statement = con.prepareStatement("INSERT INTO posts (serverId, post_id,post_ownerid,post_recipient_list,post_date,post_topic_id,post_forum_id,post_txt,post_title,post_type,post_parent_id,post_read_count) values (?,?,?,?,?,?,?,?,?,?,?,?)");
  151. statement.setInt(1, _sqlDPId);
  152. statement.setInt(2, _postId);
  153. statement.setInt(3, _postOwnerId);
  154. statement.setString(4, _postRecipientList);
  155. statement.setLong(5, _postDate);
  156. statement.setInt(6, _postTopicId);
  157. statement.setInt(7, _postForumId);
  158. statement.setString(8, _postTxt);
  159. statement.setString(9, _postTitle);
  160. statement.setInt(10, _postType);
  161. statement.setInt(11, _postParentId);
  162. statement.setInt(12, _readCount);
  163. statement.execute();
  164. statement.close();
  165. }
  166. catch (Exception e)
  167. {
  168. _log.warning("error while saving new Post to db " + e);
  169. }
  170. finally
  171. {
  172. try
  173. {
  174. con.close();
  175. }
  176. catch (Exception e)
  177. {
  178. }
  179. }
  180. }
  181. public void deleteme()
  182. {
  183. for (Comment c: _comments.values())
  184. c.deleteme();
  185. _comments.clear();
  186. java.sql.Connection con = null;
  187. try
  188. {
  189. con = L2DatabaseFactory.getInstance().getConnection();
  190. PreparedStatement statement = con.prepareStatement("DELETE FROM posts WHERE serverId=? AND post_forum_id=? AND post_topic_id=? AND post_id=?");
  191. statement.setInt(1, _sqlDPId);
  192. statement.setInt(2, _postForumId);
  193. statement.setInt(3, _postTopicId);
  194. statement.setInt(4, _postId);
  195. statement.execute();
  196. statement.close();
  197. }
  198. catch (Exception e)
  199. {
  200. e.printStackTrace();
  201. }
  202. finally
  203. {
  204. try
  205. {
  206. con.close();
  207. }
  208. catch (Exception e)
  209. {
  210. }
  211. }
  212. }
  213. /**
  214. * @param i
  215. */
  216. private void updatePost()
  217. {
  218. java.sql.Connection con = null;
  219. try
  220. {
  221. con = L2DatabaseFactory.getInstance().getConnection();
  222. PreparedStatement statement = con.prepareStatement("UPDATE posts SET post_txt=?,post_title=?,post_recipient_list=?,post_read_count=? WHERE serverId=? AND post_id=? AND post_topic_id=? AND post_forum_id=?");
  223. statement.setString(1, _postTxt);
  224. statement.setString(2, _postTitle);
  225. statement.setString(3, _postRecipientList);
  226. statement.setInt(4, _readCount);
  227. statement.setInt(5, _sqlDPId);
  228. statement.setInt(6, _postId);
  229. statement.setInt(7, _postTopicId);
  230. statement.setInt(8, _postForumId);
  231. statement.execute();
  232. statement.close();
  233. }
  234. catch (Exception e)
  235. {
  236. _log.warning("error while saving new Post to db " + e);
  237. }
  238. finally
  239. {
  240. try
  241. {
  242. con.close();
  243. }
  244. catch (Exception e)
  245. {
  246. }
  247. }
  248. }
  249. public void clearComments()
  250. {
  251. _comments.clear();
  252. }
  253. public int getCommentsSize()
  254. {
  255. return _comments.size();
  256. }
  257. public Comment getComment(int j)
  258. {
  259. return _comments.get(j);
  260. }
  261. public void addComment(Comment c)
  262. {
  263. _comments.put(c.getID(), c);
  264. }
  265. public void rmCommentByID(int id)
  266. {
  267. _comments.get(id).deleteme();
  268. _comments.remove(id);
  269. }
  270. public Collection<Comment> getAllComments()
  271. {
  272. return _comments.values();
  273. }
  274. /**
  275. *
  276. */
  277. /**
  278. * @return
  279. */
  280. public int getID()
  281. {
  282. return _postId;
  283. }
  284. public String getText()
  285. {
  286. return _postTxt;
  287. }
  288. public int getOwnerId()
  289. {
  290. return _postOwnerId;
  291. }
  292. public int getParentId()
  293. {
  294. return _postParentId;
  295. }
  296. public void updatePost(String newTitle, String newTxt)
  297. {
  298. _postTitle = newTitle;
  299. _postTxt = newTxt;
  300. updatePost();
  301. }
  302. public void updatePost(String newTitle, String newTxt, int type)
  303. {
  304. _postTitle = newTitle;
  305. _postTxt = newTxt;
  306. _postType = type;
  307. updatePost();
  308. }
  309. public void setTopic(int newTopicId, int newPostId)
  310. {
  311. _postTopicId = newTopicId;
  312. _postId = newPostId;
  313. insertindb();
  314. }
  315. public String getRecipientList()
  316. {
  317. return _postRecipientList;
  318. }
  319. public String getTitle()
  320. {
  321. return _postTitle;
  322. }
  323. public Long getDate()
  324. {
  325. return _postDate;
  326. }
  327. public int getType()
  328. {
  329. return _postType;
  330. }
  331. public String getTypeName()
  332. {
  333. switch(_postType)
  334. {
  335. case ADVERTISE:
  336. return "[Advertise]";
  337. case MISCELLANEOUS:
  338. return "[Miscellaneous]";
  339. case INFORMATION:
  340. return "[Information]";
  341. }
  342. return "";
  343. }
  344. public int getReadCount()
  345. {
  346. return _readCount;
  347. }
  348. public void increaseReadCount()
  349. {
  350. _readCount++;
  351. updatePost();
  352. }
  353. }