Forum.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.communitybbs.Manager.ForumsBBSManager;
  25. import com.l2jserver.gameserver.communitybbs.Manager.TopicBBSManager;
  26. import javolution.util.FastList;
  27. import javolution.util.FastMap;
  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. try
  123. {
  124. con.close();
  125. }
  126. catch (Exception e)
  127. {
  128. }
  129. }
  130. try
  131. {
  132. con = L2DatabaseFactory.getInstance().getConnection();
  133. PreparedStatement statement = con.prepareStatement("SELECT * FROM topic WHERE topic_forum_id=? ORDER BY topic_id DESC");
  134. statement.setInt(1, _forumId);
  135. ResultSet result = statement.executeQuery();
  136. while (result.next())
  137. {
  138. 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"));
  139. _topic.put(t.getID(), t);
  140. if (t.getID() > TopicBBSManager.getInstance().getMaxID(this))
  141. {
  142. TopicBBSManager.getInstance().setMaxID(t.getID(), this);
  143. }
  144. }
  145. result.close();
  146. statement.close();
  147. }
  148. catch (Exception e)
  149. {
  150. _log.log(Level.WARNING, "Data error on Forum " + _forumId + " : " + e.getMessage(), e);
  151. }
  152. finally
  153. {
  154. try
  155. {
  156. con.close();
  157. }
  158. catch (Exception e)
  159. {
  160. }
  161. }
  162. }
  163. /**
  164. *
  165. */
  166. private void getChildren()
  167. {
  168. Connection con = null;
  169. try
  170. {
  171. con = L2DatabaseFactory.getInstance().getConnection();
  172. PreparedStatement statement = con.prepareStatement("SELECT forum_id FROM forums WHERE forum_parent=?");
  173. statement.setInt(1, _forumId);
  174. ResultSet result = statement.executeQuery();
  175. while (result.next())
  176. {
  177. Forum f = new Forum(result.getInt("forum_id"), this);
  178. _children.add(f);
  179. ForumsBBSManager.getInstance().addForum(f);
  180. }
  181. result.close();
  182. statement.close();
  183. }
  184. catch (Exception e)
  185. {
  186. _log.log(Level.WARNING, "Data error on Forum (children): " + e.getMessage(), e);
  187. }
  188. finally
  189. {
  190. try
  191. {
  192. con.close();
  193. }
  194. catch (Exception e)
  195. {
  196. }
  197. }
  198. }
  199. public int getTopicSize()
  200. {
  201. vload();
  202. return _topic.size();
  203. }
  204. public Topic getTopic(int j)
  205. {
  206. vload();
  207. return _topic.get(j);
  208. }
  209. public void addTopic(Topic t)
  210. {
  211. vload();
  212. _topic.put(t.getID(), t);
  213. }
  214. /**
  215. * @return
  216. */
  217. public int getID()
  218. {
  219. return _forumId;
  220. }
  221. public String getName()
  222. {
  223. vload();
  224. return _forumName;
  225. }
  226. public int getType()
  227. {
  228. vload();
  229. return _forumType;
  230. }
  231. /**
  232. * @param name
  233. * @return
  234. */
  235. public Forum getChildByName(String name)
  236. {
  237. vload();
  238. for (Forum f : _children)
  239. {
  240. if (f.getName().equals(name))
  241. {
  242. return f;
  243. }
  244. }
  245. return null;
  246. }
  247. /**
  248. * @param id
  249. */
  250. public void rmTopicByID(int id)
  251. {
  252. _topic.remove(id);
  253. }
  254. /**
  255. *
  256. */
  257. public void insertIntoDb()
  258. {
  259. Connection con = null;
  260. try
  261. {
  262. con = L2DatabaseFactory.getInstance().getConnection();
  263. PreparedStatement statement = con.prepareStatement("INSERT INTO forums (forum_id,forum_name,forum_parent,forum_post,forum_type,forum_perm,forum_owner_id) VALUES (?,?,?,?,?,?,?)");
  264. statement.setInt(1, _forumId);
  265. statement.setString(2, _forumName);
  266. statement.setInt(3, _fParent.getID());
  267. statement.setInt(4, _forumPost);
  268. statement.setInt(5, _forumType);
  269. statement.setInt(6, _forumPerm);
  270. statement.setInt(7, _ownerID);
  271. statement.execute();
  272. statement.close();
  273. }
  274. catch (Exception e)
  275. {
  276. _log.log(Level.WARNING, "Error while saving new Forum to db " + e.getMessage(), e);
  277. }
  278. finally
  279. {
  280. try
  281. {
  282. con.close();
  283. }
  284. catch (Exception e)
  285. {
  286. }
  287. }
  288. }
  289. /**
  290. *
  291. */
  292. public void vload()
  293. {
  294. if (_loaded == false)
  295. {
  296. load();
  297. getChildren();
  298. _loaded = true;
  299. }
  300. }
  301. }