Post.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.communitybbs.BB;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javolution.util.FastList;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.communitybbs.Manager.PostBBSManager;
  25. /**
  26. * @author Maktakien
  27. */
  28. public class Post
  29. {
  30. private static Logger _log = Logger.getLogger(Post.class.getName());
  31. public static class CPost
  32. {
  33. public int postId;
  34. public String postOwner;
  35. public int postOwnerId;
  36. public long postDate;
  37. public int postTopicId;
  38. public int postForumId;
  39. public String postTxt;
  40. }
  41. private List<CPost> _post;
  42. //public enum ConstructorType {REPLY, CREATE };
  43. /**
  44. * @param _PostOwner
  45. * @param _PostOwnerID
  46. * @param date
  47. * @param tid
  48. * @param _PostForumID
  49. * @param txt
  50. */
  51. public Post(String _PostOwner,int _PostOwnerID,long date,int tid,int _PostForumID,String txt)
  52. {
  53. _post = new FastList<>();
  54. CPost cp = new CPost();
  55. cp.postId = 0;
  56. cp.postOwner = _PostOwner;
  57. cp.postOwnerId = _PostOwnerID;
  58. cp.postDate = date;
  59. cp.postTopicId = tid;
  60. cp.postForumId = _PostForumID;
  61. cp.postTxt = txt;
  62. _post.add(cp);
  63. insertindb(cp);
  64. }
  65. public void insertindb(CPost cp)
  66. {
  67. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  68. 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 (?,?,?,?,?,?,?)"))
  69. {
  70. ps.setInt(1, cp.postId);
  71. ps.setString(2, cp.postOwner);
  72. ps.setInt(3, cp.postOwnerId);
  73. ps.setLong(4, cp.postDate);
  74. ps.setInt(5, cp.postTopicId);
  75. ps.setInt(6, cp.postForumId);
  76. ps.setString(7, cp.postTxt);
  77. ps.execute();
  78. }
  79. catch (Exception e)
  80. {
  81. _log.log(Level.WARNING, "Error while saving new Post to db " + e.getMessage(), e);
  82. }
  83. }
  84. public Post(Topic t)
  85. {
  86. _post = new FastList<>();
  87. load(t);
  88. }
  89. public CPost getCPost(int id)
  90. {
  91. int i = 0;
  92. for(CPost cp : _post)
  93. {
  94. if(i++ == id)
  95. {
  96. return cp;
  97. }
  98. }
  99. return null;
  100. }
  101. public void deleteme(Topic t)
  102. {
  103. PostBBSManager.getInstance().delPostByTopic(t);
  104. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  105. PreparedStatement ps = con.prepareStatement("DELETE FROM posts WHERE post_forum_id=? AND post_topic_id=?"))
  106. {
  107. ps.setInt(1, t.getForumID());
  108. ps.setInt(2, t.getID());
  109. ps.execute();
  110. }
  111. catch (Exception e)
  112. {
  113. _log.log(Level.WARNING, "Error while deleting post: " + e.getMessage(), e);
  114. }
  115. }
  116. /**
  117. * @param t
  118. */
  119. private void load(Topic t)
  120. {
  121. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  122. PreparedStatement ps = con.prepareStatement("SELECT * FROM posts WHERE post_forum_id=? AND post_topic_id=? ORDER BY post_id ASC"))
  123. {
  124. ps.setInt(1, t.getForumID());
  125. ps.setInt(2, t.getID());
  126. try (ResultSet rs = ps.executeQuery())
  127. {
  128. while(rs.next())
  129. {
  130. CPost cp = new CPost();
  131. cp.postId = rs.getInt("post_id");
  132. cp.postOwner = rs.getString("post_owner_name");
  133. cp.postOwnerId = rs.getInt("post_ownerid");
  134. cp.postDate = rs.getLong("post_date");
  135. cp.postTopicId = rs.getInt("post_topic_id");
  136. cp.postForumId = rs.getInt("post_forum_id");
  137. cp.postTxt = rs.getString("post_txt");
  138. _post.add(cp);
  139. }
  140. }
  141. }
  142. catch (Exception e)
  143. {
  144. _log.log(Level.WARNING, "Data error on Post " + t.getForumID() + "/"+t.getID()+" : " + e.getMessage(), e);
  145. }
  146. }
  147. /**
  148. * @param i
  149. */
  150. public void updatetxt(int i)
  151. {
  152. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  153. PreparedStatement ps = con.prepareStatement("UPDATE posts SET post_txt=? WHERE post_id=? AND post_topic_id=? AND post_forum_id=?"))
  154. {
  155. CPost cp = getCPost(i);
  156. ps.setString(1, cp.postTxt);
  157. ps.setInt(2, cp.postId);
  158. ps.setInt(3, cp.postTopicId);
  159. ps.setInt(4, cp.postForumId);
  160. ps.execute();
  161. }
  162. catch (Exception e)
  163. {
  164. _log.log(Level.WARNING, "Error while saving new Post to db " + e.getMessage(), e);
  165. }
  166. }
  167. }