Forum.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.Map;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastList;
  24. import javolution.util.FastMap;
  25. import com.l2jserver.L2DatabaseFactory;
  26. import com.l2jserver.gameserver.communitybbs.Manager.ForumsBBSManager;
  27. import com.l2jserver.gameserver.communitybbs.Manager.TopicBBSManager;
  28. public class Forum
  29. {
  30. //type
  31. public static final int ROOT = 0;
  32. public static final int NORMAL = 1;
  33. public static final int CLAN = 2;
  34. public static final int MEMO = 3;
  35. public static final int MAIL = 4;
  36. //perm
  37. public static final int INVISIBLE = 0;
  38. public static final int ALL = 1;
  39. public static final int CLANMEMBERONLY = 2;
  40. public static final int OWNERONLY = 3;
  41. private static Logger _log = Logger.getLogger(Forum.class.getName());
  42. private List<Forum> _children;
  43. private Map<Integer, Topic> _topic;
  44. private int _forumId;
  45. private String _forumName;
  46. //private int _ForumParent;
  47. private int _forumType;
  48. private int _forumPost;
  49. private int _forumPerm;
  50. private Forum _fParent;
  51. private int _ownerID;
  52. private boolean _loaded = false;
  53. /**
  54. * Creates new instance of Forum. When you create new forum, use
  55. * {@link com.l2jserver.gameserver.communitybbs.Manager.ForumsBBSManager#
  56. * addForum(com.l2jserver.gameserver.communitybbs.BB.Forum)} to add forum
  57. * to the forums manager.
  58. *
  59. * @param i
  60. */
  61. public Forum(int Forumid, Forum FParent)
  62. {
  63. _forumId = Forumid;
  64. _fParent = FParent;
  65. _children = new FastList<Forum>();
  66. _topic = new FastMap<Integer, Topic>();
  67. /*load();
  68. getChildren(); */
  69. }
  70. /**
  71. * @param name
  72. * @param parent
  73. * @param type
  74. * @param perm
  75. */
  76. public Forum(String name, Forum parent, int type, int perm, int OwnerID)
  77. {
  78. _forumName = name;
  79. _forumId = ForumsBBSManager.getInstance().getANewID();
  80. //_ForumParent = parent.getID();
  81. _forumType = type;
  82. _forumPost = 0;
  83. _forumPerm = perm;
  84. _fParent = parent;
  85. _ownerID = OwnerID;
  86. _children = new FastList<Forum>();
  87. _topic = new FastMap<Integer, Topic>();
  88. parent._children.add(this);
  89. ForumsBBSManager.getInstance().addForum(this);
  90. _loaded = true;
  91. }
  92. /**
  93. *
  94. */
  95. private void load()
  96. {
  97. Connection con = null;
  98. try
  99. {
  100. con = L2DatabaseFactory.getInstance().getConnection();
  101. PreparedStatement statement = con.prepareStatement("SELECT * FROM forums WHERE forum_id=?");
  102. statement.setInt(1, _forumId);
  103. ResultSet result = statement.executeQuery();
  104. if (result.next())
  105. {
  106. _forumName = result.getString("forum_name");
  107. //_ForumParent = result.getInt("forum_parent");
  108. _forumPost = result.getInt("forum_post");
  109. _forumType = result.getInt("forum_type");
  110. _forumPerm = result.getInt("forum_perm");
  111. _ownerID = result.getInt("forum_owner_id");
  112. }
  113. result.close();
  114. statement.close();
  115. }
  116. catch (Exception e)
  117. {
  118. _log.log(Level.WARNING, "Data error on Forum " + _forumId + " : " + e.getMessage(), e);
  119. }
  120. finally
  121. {
  122. L2DatabaseFactory.close(con);
  123. }
  124. try
  125. {
  126. con = L2DatabaseFactory.getInstance().getConnection();
  127. PreparedStatement statement = con.prepareStatement("SELECT * FROM topic WHERE topic_forum_id=? ORDER BY topic_id DESC");
  128. statement.setInt(1, _forumId);
  129. ResultSet result = statement.executeQuery();
  130. while (result.next())
  131. {
  132. Topic t = new Topic(Topic.ConstructorType.RESTORE, result.getInt("topic_id"), result.getInt("topic_forum_id"), result.getString("topic_name"), result.getLong("topic_date"), result.getString("topic_ownername"), result.getInt("topic_ownerid"), result.getInt("topic_type"), result.getInt("topic_reply"));
  133. _topic.put(t.getID(), t);
  134. if (t.getID() > TopicBBSManager.getInstance().getMaxID(this))
  135. {
  136. TopicBBSManager.getInstance().setMaxID(t.getID(), this);
  137. }
  138. }
  139. result.close();
  140. statement.close();
  141. }
  142. catch (Exception e)
  143. {
  144. _log.log(Level.WARNING, "Data error on Forum " + _forumId + " : " + e.getMessage(), e);
  145. }
  146. finally
  147. {
  148. L2DatabaseFactory.close(con);
  149. }
  150. }
  151. /**
  152. *
  153. */
  154. private void getChildren()
  155. {
  156. Connection con = null;
  157. try
  158. {
  159. con = L2DatabaseFactory.getInstance().getConnection();
  160. PreparedStatement statement = con.prepareStatement("SELECT forum_id FROM forums WHERE forum_parent=?");
  161. statement.setInt(1, _forumId);
  162. ResultSet result = statement.executeQuery();
  163. while (result.next())
  164. {
  165. Forum f = new Forum(result.getInt("forum_id"), this);
  166. _children.add(f);
  167. ForumsBBSManager.getInstance().addForum(f);
  168. }
  169. result.close();
  170. statement.close();
  171. }
  172. catch (Exception e)
  173. {
  174. _log.log(Level.WARNING, "Data error on Forum (children): " + e.getMessage(), e);
  175. }
  176. finally
  177. {
  178. L2DatabaseFactory.close(con);
  179. }
  180. }
  181. public int getTopicSize()
  182. {
  183. vload();
  184. return _topic.size();
  185. }
  186. public Topic getTopic(int j)
  187. {
  188. vload();
  189. return _topic.get(j);
  190. }
  191. public void addTopic(Topic t)
  192. {
  193. vload();
  194. _topic.put(t.getID(), t);
  195. }
  196. /**
  197. * @return
  198. */
  199. public int getID()
  200. {
  201. return _forumId;
  202. }
  203. public String getName()
  204. {
  205. vload();
  206. return _forumName;
  207. }
  208. public int getType()
  209. {
  210. vload();
  211. return _forumType;
  212. }
  213. /**
  214. * @param name
  215. * @return
  216. */
  217. public Forum getChildByName(String name)
  218. {
  219. vload();
  220. for (Forum f : _children)
  221. {
  222. if (f.getName().equals(name))
  223. {
  224. return f;
  225. }
  226. }
  227. return null;
  228. }
  229. /**
  230. * @param id
  231. */
  232. public void rmTopicByID(int id)
  233. {
  234. _topic.remove(id);
  235. }
  236. /**
  237. *
  238. */
  239. public void insertIntoDb()
  240. {
  241. Connection con = null;
  242. try
  243. {
  244. con = L2DatabaseFactory.getInstance().getConnection();
  245. PreparedStatement statement = con.prepareStatement("INSERT INTO forums (forum_id,forum_name,forum_parent,forum_post,forum_type,forum_perm,forum_owner_id) VALUES (?,?,?,?,?,?,?)");
  246. statement.setInt(1, _forumId);
  247. statement.setString(2, _forumName);
  248. statement.setInt(3, _fParent.getID());
  249. statement.setInt(4, _forumPost);
  250. statement.setInt(5, _forumType);
  251. statement.setInt(6, _forumPerm);
  252. statement.setInt(7, _ownerID);
  253. statement.execute();
  254. statement.close();
  255. }
  256. catch (Exception e)
  257. {
  258. _log.log(Level.WARNING, "Error while saving new Forum to db " + e.getMessage(), e);
  259. }
  260. finally
  261. {
  262. L2DatabaseFactory.close(con);
  263. }
  264. }
  265. /**
  266. *
  267. */
  268. public void vload()
  269. {
  270. if (_loaded == false)
  271. {
  272. load();
  273. getChildren();
  274. _loaded = true;
  275. }
  276. }
  277. }