Forum.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.Map;
  26. import java.util.concurrent.ConcurrentHashMap;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  30. import com.l2jserver.gameserver.communitybbs.Manager.ForumsBBSManager;
  31. import com.l2jserver.gameserver.communitybbs.Manager.TopicBBSManager;
  32. public final class Forum
  33. {
  34. private static final Logger _log = LoggerFactory.getLogger(Forum.class);
  35. // type
  36. public static final int ROOT = 0;
  37. public static final int NORMAL = 1;
  38. public static final int CLAN = 2;
  39. public static final int MEMO = 3;
  40. public static final int MAIL = 4;
  41. // perm
  42. public static final int INVISIBLE = 0;
  43. public static final int ALL = 1;
  44. public static final int CLANMEMBERONLY = 2;
  45. public static final int OWNERONLY = 3;
  46. private final List<Forum> _children = new ArrayList<>();
  47. private final Map<Integer, Topic> _topic = new ConcurrentHashMap<>();
  48. private final int _forumId;
  49. private String _forumName;
  50. private int _forumType;
  51. private int _forumPost;
  52. private int _forumPerm;
  53. private final Forum _fParent;
  54. private int _ownerID;
  55. private boolean _loaded = false;
  56. /**
  57. * Creates new instance of Forum. When you create new forum, use {@link com.l2jserver.gameserver.communitybbs.Manager.ForumsBBSManager# addForum(com.l2jserver.gameserver.communitybbs.BB.Forum)} to add forum to the forums manager.
  58. * @param Forumid
  59. * @param FParent
  60. */
  61. public Forum(int Forumid, Forum FParent)
  62. {
  63. _forumId = Forumid;
  64. _fParent = FParent;
  65. }
  66. /**
  67. * @param name
  68. * @param parent
  69. * @param type
  70. * @param perm
  71. * @param OwnerID
  72. */
  73. public Forum(String name, Forum parent, int type, int perm, int OwnerID)
  74. {
  75. _forumName = name;
  76. _forumId = ForumsBBSManager.getInstance().getANewID();
  77. _forumType = type;
  78. _forumPost = 0;
  79. _forumPerm = perm;
  80. _fParent = parent;
  81. _ownerID = OwnerID;
  82. parent._children.add(this);
  83. ForumsBBSManager.getInstance().addForum(this);
  84. _loaded = true;
  85. }
  86. private void load()
  87. {
  88. try (Connection con = ConnectionFactory.getInstance().getConnection();
  89. PreparedStatement ps = con.prepareStatement("SELECT * FROM forums WHERE forum_id=?"))
  90. {
  91. ps.setInt(1, _forumId);
  92. try (ResultSet rs = ps.executeQuery())
  93. {
  94. if (rs.next())
  95. {
  96. _forumName = rs.getString("forum_name");
  97. _forumPost = rs.getInt("forum_post");
  98. _forumType = rs.getInt("forum_type");
  99. _forumPerm = rs.getInt("forum_perm");
  100. _ownerID = rs.getInt("forum_owner_id");
  101. }
  102. }
  103. }
  104. catch (Exception e)
  105. {
  106. _log.warn("Could not get from database forum ID {}!", _forumId, e);
  107. }
  108. try (Connection con = ConnectionFactory.getInstance().getConnection();
  109. PreparedStatement ps = con.prepareStatement("SELECT * FROM topic WHERE topic_forum_id=? ORDER BY topic_id DESC"))
  110. {
  111. ps.setInt(1, _forumId);
  112. try (ResultSet rs = ps.executeQuery())
  113. {
  114. while (rs.next())
  115. {
  116. Topic t = new Topic(Topic.ConstructorType.RESTORE, rs.getInt("topic_id"), rs.getInt("topic_forum_id"), rs.getString("topic_name"), rs.getLong("topic_date"), rs.getString("topic_ownername"), rs.getInt("topic_ownerid"), rs.getInt("topic_type"), rs.getInt("topic_reply"));
  117. _topic.put(t.getID(), t);
  118. if (t.getID() > TopicBBSManager.getInstance().getMaxID(this))
  119. {
  120. TopicBBSManager.getInstance().setMaxID(t.getID(), this);
  121. }
  122. }
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. _log.warn("Could not get from database topics for forum ID {}", _forumId, e);
  128. }
  129. }
  130. private void getChildren()
  131. {
  132. try (Connection con = ConnectionFactory.getInstance().getConnection();
  133. PreparedStatement ps = con.prepareStatement("SELECT forum_id FROM forums WHERE forum_parent=?"))
  134. {
  135. ps.setInt(1, _forumId);
  136. try (ResultSet rs = ps.executeQuery())
  137. {
  138. while (rs.next())
  139. {
  140. final Forum f = new Forum(rs.getInt("forum_id"), this);
  141. _children.add(f);
  142. ForumsBBSManager.getInstance().addForum(f);
  143. }
  144. }
  145. }
  146. catch (Exception e)
  147. {
  148. _log.warn("Could not get from database child forums for forum ID {}", _forumId, e);
  149. }
  150. }
  151. public int getTopicSize()
  152. {
  153. vload();
  154. return _topic.size();
  155. }
  156. public Topic getTopic(int j)
  157. {
  158. vload();
  159. return _topic.get(j);
  160. }
  161. public void addTopic(Topic t)
  162. {
  163. vload();
  164. _topic.put(t.getID(), t);
  165. }
  166. /**
  167. * @return the forum Id
  168. */
  169. public int getID()
  170. {
  171. return _forumId;
  172. }
  173. public String getName()
  174. {
  175. vload();
  176. return _forumName;
  177. }
  178. public int getType()
  179. {
  180. vload();
  181. return _forumType;
  182. }
  183. /**
  184. * @param name the forum name
  185. * @return the forum for the given name
  186. */
  187. public Forum getChildByName(String name)
  188. {
  189. vload();
  190. return _children.stream().filter(f -> f.getName().equals(name)).findFirst().orElse(null);
  191. }
  192. /**
  193. * @param id
  194. */
  195. public void rmTopicByID(int id)
  196. {
  197. _topic.remove(id);
  198. }
  199. public void insertIntoDb()
  200. {
  201. try (Connection con = ConnectionFactory.getInstance().getConnection();
  202. PreparedStatement ps = con.prepareStatement("INSERT INTO forums (forum_id,forum_name,forum_parent,forum_post,forum_type,forum_perm,forum_owner_id) VALUES (?,?,?,?,?,?,?)"))
  203. {
  204. ps.setInt(1, _forumId);
  205. ps.setString(2, _forumName);
  206. ps.setInt(3, _fParent.getID());
  207. ps.setInt(4, _forumPost);
  208. ps.setInt(5, _forumType);
  209. ps.setInt(6, _forumPerm);
  210. ps.setInt(7, _ownerID);
  211. ps.execute();
  212. }
  213. catch (Exception e)
  214. {
  215. _log.error("Could not save forum ID {} in database!", _forumId, e);
  216. }
  217. }
  218. public void vload()
  219. {
  220. if (!_loaded)
  221. {
  222. load();
  223. getChildren();
  224. _loaded = true;
  225. }
  226. }
  227. }