Forum.java 7.4 KB

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