Post.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.communitybbs.BB;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.util.List;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastList;
  27. import com.l2jserver.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.communitybbs.Manager.PostBBSManager;
  29. /**
  30. * @author Maktakien
  31. */
  32. public class Post
  33. {
  34. private static Logger _log = Logger.getLogger(Post.class.getName());
  35. public static class CPost
  36. {
  37. public int postId;
  38. public String postOwner;
  39. public int postOwnerId;
  40. public long postDate;
  41. public int postTopicId;
  42. public int postForumId;
  43. public String postTxt;
  44. }
  45. private final List<CPost> _post;
  46. /**
  47. * @param _PostOwner
  48. * @param _PostOwnerID
  49. * @param date
  50. * @param tid
  51. * @param _PostForumID
  52. * @param txt
  53. */
  54. public Post(String _PostOwner, int _PostOwnerID, long date, int tid, int _PostForumID, String txt)
  55. {
  56. _post = new FastList<>();
  57. CPost cp = new CPost();
  58. cp.postId = 0;
  59. cp.postOwner = _PostOwner;
  60. cp.postOwnerId = _PostOwnerID;
  61. cp.postDate = date;
  62. cp.postTopicId = tid;
  63. cp.postForumId = _PostForumID;
  64. cp.postTxt = txt;
  65. _post.add(cp);
  66. insertindb(cp);
  67. }
  68. public void insertindb(CPost cp)
  69. {
  70. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  71. PreparedStatement ps = con.prepareStatement("INSERT INTO posts (post_id,post_owner_name,post_ownerid,post_date,post_topic_id,post_forum_id,post_txt) values (?,?,?,?,?,?,?)"))
  72. {
  73. ps.setInt(1, cp.postId);
  74. ps.setString(2, cp.postOwner);
  75. ps.setInt(3, cp.postOwnerId);
  76. ps.setLong(4, cp.postDate);
  77. ps.setInt(5, cp.postTopicId);
  78. ps.setInt(6, cp.postForumId);
  79. ps.setString(7, cp.postTxt);
  80. ps.execute();
  81. }
  82. catch (Exception e)
  83. {
  84. _log.log(Level.WARNING, "Error while saving new Post to db " + e.getMessage(), e);
  85. }
  86. }
  87. public Post(Topic t)
  88. {
  89. _post = new FastList<>();
  90. load(t);
  91. }
  92. public CPost getCPost(int id)
  93. {
  94. int i = 0;
  95. for (CPost cp : _post)
  96. {
  97. if (i++ == id)
  98. {
  99. return cp;
  100. }
  101. }
  102. return null;
  103. }
  104. public void deleteme(Topic t)
  105. {
  106. PostBBSManager.getInstance().delPostByTopic(t);
  107. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  108. PreparedStatement ps = con.prepareStatement("DELETE FROM posts WHERE post_forum_id=? AND post_topic_id=?"))
  109. {
  110. ps.setInt(1, t.getForumID());
  111. ps.setInt(2, t.getID());
  112. ps.execute();
  113. }
  114. catch (Exception e)
  115. {
  116. _log.log(Level.WARNING, "Error while deleting post: " + e.getMessage(), e);
  117. }
  118. }
  119. /**
  120. * @param t
  121. */
  122. private void load(Topic t)
  123. {
  124. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  125. PreparedStatement ps = con.prepareStatement("SELECT * FROM posts WHERE post_forum_id=? AND post_topic_id=? ORDER BY post_id ASC"))
  126. {
  127. ps.setInt(1, t.getForumID());
  128. ps.setInt(2, t.getID());
  129. try (ResultSet rs = ps.executeQuery())
  130. {
  131. while (rs.next())
  132. {
  133. CPost cp = new CPost();
  134. cp.postId = rs.getInt("post_id");
  135. cp.postOwner = rs.getString("post_owner_name");
  136. cp.postOwnerId = rs.getInt("post_ownerid");
  137. cp.postDate = rs.getLong("post_date");
  138. cp.postTopicId = rs.getInt("post_topic_id");
  139. cp.postForumId = rs.getInt("post_forum_id");
  140. cp.postTxt = rs.getString("post_txt");
  141. _post.add(cp);
  142. }
  143. }
  144. }
  145. catch (Exception e)
  146. {
  147. _log.log(Level.WARNING, "Data error on Post " + t.getForumID() + "/" + t.getID() + " : " + e.getMessage(), e);
  148. }
  149. }
  150. /**
  151. * @param i
  152. */
  153. public void updatetxt(int i)
  154. {
  155. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  156. PreparedStatement ps = con.prepareStatement("UPDATE posts SET post_txt=? WHERE post_id=? AND post_topic_id=? AND post_forum_id=?"))
  157. {
  158. CPost cp = getCPost(i);
  159. ps.setString(1, cp.postTxt);
  160. ps.setInt(2, cp.postId);
  161. ps.setInt(3, cp.postTopicId);
  162. ps.setInt(4, cp.postForumId);
  163. ps.execute();
  164. }
  165. catch (Exception e)
  166. {
  167. _log.log(Level.WARNING, "Error while saving new Post to db " + e.getMessage(), e);
  168. }
  169. }
  170. }