Post.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 net.sf.l2j.gameserver.communitybbs.BB;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.List;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastList;
  21. import net.sf.l2j.L2DatabaseFactory;
  22. import net.sf.l2j.gameserver.communitybbs.Manager.PostBBSManager;
  23. /**
  24. * @author Maktakien
  25. *
  26. */
  27. public class Post
  28. {
  29. private static Logger _log = Logger.getLogger(Post.class.getName());
  30. public class CPost
  31. {
  32. public int postId;
  33. public String postOwner;
  34. public int postOwnerId;
  35. public long postDate;
  36. public int postTopicId;
  37. public int postForumId;
  38. public String postTxt;
  39. }
  40. private List<CPost> _post;
  41. /**
  42. * @param restore
  43. * @param t
  44. */
  45. //public enum ConstructorType {REPLY, CREATE };
  46. public Post(String _PostOwner,int _PostOwnerID,long date,int tid,int _PostForumID,String txt)
  47. {
  48. _post = new FastList<CPost>();
  49. CPost cp = new CPost();
  50. cp.postId = 0;
  51. cp.postOwner = _PostOwner;
  52. cp.postOwnerId = _PostOwnerID;
  53. cp.postDate = date;
  54. cp.postTopicId = tid;
  55. cp.postForumId = _PostForumID;
  56. cp.postTxt = txt;
  57. _post.add(cp);
  58. insertindb(cp);
  59. }
  60. public void insertindb(CPost cp)
  61. {
  62. java.sql.Connection con = null;
  63. try
  64. {
  65. con = L2DatabaseFactory.getInstance().getConnection();
  66. 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 (?,?,?,?,?,?,?)");
  67. statement.setInt(1, cp.postId);
  68. statement.setString(2, cp.postOwner);
  69. statement.setInt(3, cp.postOwnerId);
  70. statement.setLong(4, cp.postDate);
  71. statement.setInt(5, cp.postTopicId);
  72. statement.setInt(6, cp.postForumId);
  73. statement.setString(7, cp.postTxt);
  74. statement.execute();
  75. statement.close();
  76. }
  77. catch (Exception e)
  78. {
  79. _log.warning("error while saving new Post to db " + e);
  80. }
  81. finally
  82. {
  83. try
  84. {
  85. con.close();
  86. }
  87. catch (Exception e)
  88. {
  89. }
  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. i++;
  107. }
  108. return null;
  109. }
  110. public void deleteme(Topic t)
  111. {
  112. PostBBSManager.getInstance().delPostByTopic(t);
  113. java.sql.Connection con = null;
  114. try
  115. {
  116. con = L2DatabaseFactory.getInstance().getConnection();
  117. PreparedStatement statement = con.prepareStatement("DELETE FROM posts WHERE post_forum_id=? AND post_topic_id=?");
  118. statement.setInt(1, t.getForumID());
  119. statement.setInt(2, t.getID());
  120. statement.execute();
  121. statement.close();
  122. }
  123. catch (Exception e)
  124. {
  125. e.printStackTrace();
  126. }
  127. finally
  128. {
  129. try
  130. {
  131. con.close();
  132. }
  133. catch (Exception e)
  134. {
  135. }
  136. }
  137. }
  138. /**
  139. * @param t
  140. */
  141. private void load(Topic t)
  142. {
  143. java.sql.Connection con = null;
  144. try
  145. {
  146. con = L2DatabaseFactory.getInstance().getConnection();
  147. PreparedStatement statement = con.prepareStatement("SELECT * FROM posts WHERE post_forum_id=? AND post_topic_id=? ORDER BY post_id ASC");
  148. statement.setInt(1, t.getForumID());
  149. statement.setInt(2, t.getID());
  150. ResultSet result = statement.executeQuery();
  151. while(result.next())
  152. {
  153. CPost cp = new CPost();
  154. cp.postId = Integer.parseInt(result.getString("post_id"));
  155. cp.postOwner = result.getString("post_owner_name");
  156. cp.postOwnerId = Integer.parseInt(result.getString("post_ownerid"));
  157. cp.postDate = Long.parseLong(result.getString("post_date"));
  158. cp.postTopicId = Integer.parseInt(result.getString("post_topic_id"));
  159. cp.postForumId = Integer.parseInt(result.getString("post_forum_id"));
  160. cp.postTxt = result.getString("post_txt");
  161. _post.add(cp);
  162. }
  163. result.close();
  164. statement.close();
  165. }
  166. catch (Exception e)
  167. {
  168. _log.warning("data error on Post " + t.getForumID() + "/"+t.getID()+" : " + e);
  169. e.printStackTrace();
  170. }
  171. finally
  172. {
  173. try
  174. {
  175. con.close();
  176. }
  177. catch (Exception e)
  178. {
  179. }
  180. }
  181. }
  182. /**
  183. * @param i
  184. */
  185. public void updatetxt(int i)
  186. {
  187. java.sql.Connection con = null;
  188. try
  189. {
  190. CPost cp = getCPost(i);
  191. con = L2DatabaseFactory.getInstance().getConnection();
  192. PreparedStatement statement = con.prepareStatement("UPDATE posts SET post_txt=? WHERE post_id=? AND post_topic_id=? AND post_forum_id=?");
  193. statement.setString(1, cp.postTxt);
  194. statement.setInt(2, cp.postId);
  195. statement.setInt(3, cp.postTopicId);
  196. statement.setInt(4, cp.postForumId);
  197. statement.execute();
  198. statement.close();
  199. }
  200. catch (Exception e)
  201. {
  202. _log.warning("error while saving new Post to db " + e);
  203. }
  204. finally
  205. {
  206. try
  207. {
  208. con.close();
  209. }
  210. catch (Exception e)
  211. {
  212. }
  213. }
  214. }
  215. /**
  216. *
  217. */
  218. }