Post.java 5.4 KB

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