Post.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2004-2015 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.ArrayList;
  24. import java.util.List;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  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 = new ArrayList<>();
  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. final CPost cp = new CPost();
  57. cp.postId = 0;
  58. cp.postOwner = _PostOwner;
  59. cp.postOwnerId = _PostOwnerID;
  60. cp.postDate = date;
  61. cp.postTopicId = tid;
  62. cp.postForumId = _PostForumID;
  63. cp.postTxt = txt;
  64. _post.add(cp);
  65. insertindb(cp);
  66. }
  67. public Post(Topic t)
  68. {
  69. load(t);
  70. }
  71. public void insertindb(CPost cp)
  72. {
  73. try (Connection con = ConnectionFactory.getInstance().getConnection();
  74. 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 (?,?,?,?,?,?,?)"))
  75. {
  76. ps.setInt(1, cp.postId);
  77. ps.setString(2, cp.postOwner);
  78. ps.setInt(3, cp.postOwnerId);
  79. ps.setLong(4, cp.postDate);
  80. ps.setInt(5, cp.postTopicId);
  81. ps.setInt(6, cp.postForumId);
  82. ps.setString(7, cp.postTxt);
  83. ps.execute();
  84. }
  85. catch (Exception e)
  86. {
  87. _log.log(Level.WARNING, "Error while saving new Post to db " + e.getMessage(), e);
  88. }
  89. }
  90. public CPost getCPost(int id)
  91. {
  92. return _post.get(id);
  93. }
  94. public void deleteme(Topic t)
  95. {
  96. PostBBSManager.getInstance().delPostByTopic(t);
  97. try (Connection con = ConnectionFactory.getInstance().getConnection();
  98. PreparedStatement ps = con.prepareStatement("DELETE FROM posts WHERE post_forum_id=? AND post_topic_id=?"))
  99. {
  100. ps.setInt(1, t.getForumID());
  101. ps.setInt(2, t.getID());
  102. ps.execute();
  103. }
  104. catch (Exception e)
  105. {
  106. _log.log(Level.WARNING, "Error while deleting post: " + e.getMessage(), e);
  107. }
  108. }
  109. /**
  110. * @param t
  111. */
  112. private void load(Topic t)
  113. {
  114. try (Connection con = ConnectionFactory.getInstance().getConnection();
  115. PreparedStatement ps = con.prepareStatement("SELECT * FROM posts WHERE post_forum_id=? AND post_topic_id=? ORDER BY post_id ASC"))
  116. {
  117. ps.setInt(1, t.getForumID());
  118. ps.setInt(2, t.getID());
  119. try (ResultSet rs = ps.executeQuery())
  120. {
  121. while (rs.next())
  122. {
  123. final CPost cp = new CPost();
  124. cp.postId = rs.getInt("post_id");
  125. cp.postOwner = rs.getString("post_owner_name");
  126. cp.postOwnerId = rs.getInt("post_ownerid");
  127. cp.postDate = rs.getLong("post_date");
  128. cp.postTopicId = rs.getInt("post_topic_id");
  129. cp.postForumId = rs.getInt("post_forum_id");
  130. cp.postTxt = rs.getString("post_txt");
  131. _post.add(cp);
  132. }
  133. }
  134. }
  135. catch (Exception e)
  136. {
  137. _log.log(Level.WARNING, "Data error on Post " + t.getForumID() + "/" + t.getID() + " : " + e.getMessage(), e);
  138. }
  139. }
  140. /**
  141. * @param i
  142. */
  143. public void updatetxt(int i)
  144. {
  145. try (Connection con = ConnectionFactory.getInstance().getConnection();
  146. PreparedStatement ps = con.prepareStatement("UPDATE posts SET post_txt=? WHERE post_id=? AND post_topic_id=? AND post_forum_id=?"))
  147. {
  148. CPost cp = getCPost(i);
  149. ps.setString(1, cp.postTxt);
  150. ps.setInt(2, cp.postId);
  151. ps.setInt(3, cp.postTopicId);
  152. ps.setInt(4, cp.postForumId);
  153. ps.execute();
  154. }
  155. catch (Exception e)
  156. {
  157. _log.log(Level.WARNING, "Error while saving new Post to db " + e.getMessage(), e);
  158. }
  159. }
  160. }