ForumsBBSManager.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.Manager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import com.l2jserver.L2DatabaseFactory;
  23. import com.l2jserver.gameserver.communitybbs.BB.Forum;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import javolution.util.FastList;
  26. public class ForumsBBSManager extends BaseBBSManager
  27. {
  28. private static Logger _log = Logger.getLogger(ForumsBBSManager.class.getName());
  29. private List<Forum> _table;
  30. private int _lastid = 1;
  31. /**
  32. * @return
  33. */
  34. public static ForumsBBSManager getInstance()
  35. {
  36. return SingletonHolder._instance;
  37. }
  38. private ForumsBBSManager()
  39. {
  40. _table = new FastList<Forum>();
  41. Connection con = null;
  42. try
  43. {
  44. con = L2DatabaseFactory.getInstance().getConnection();
  45. PreparedStatement statement = con.prepareStatement("SELECT forum_id FROM forums WHERE forum_type=0");
  46. ResultSet result = statement.executeQuery();
  47. while (result.next())
  48. {
  49. int forumId = result.getInt("forum_id");
  50. Forum f = new Forum(forumId, null);
  51. addForum(f);
  52. }
  53. result.close();
  54. statement.close();
  55. }
  56. catch (Exception e)
  57. {
  58. _log.log(Level.WARNING, "Data error on Forum (root): " + e.getMessage(), e);
  59. }
  60. finally
  61. {
  62. try
  63. {
  64. con.close();
  65. }
  66. catch (Exception e)
  67. {
  68. }
  69. }
  70. }
  71. public void initRoot()
  72. {
  73. for (Forum f : _table)
  74. f.vload();
  75. _log.info("Loaded " + _table.size() + " forums. Last forum id used: " + _lastid);
  76. }
  77. public void addForum(Forum ff)
  78. {
  79. if (ff == null)
  80. return;
  81. _table.add(ff);
  82. if (ff.getID() > _lastid)
  83. {
  84. _lastid = ff.getID();
  85. }
  86. }
  87. @Override
  88. public void parsecmd(String command, L2PcInstance activeChar)
  89. {
  90. }
  91. /**
  92. * @param string
  93. * @return
  94. */
  95. public Forum getForumByName(String Name)
  96. {
  97. for (Forum f : _table)
  98. {
  99. if (f.getName().equals(Name))
  100. {
  101. return f;
  102. }
  103. }
  104. return null;
  105. }
  106. /**
  107. * @param name
  108. * @param forumByName
  109. * @return
  110. */
  111. public Forum createNewForum(String name, Forum parent, int type, int perm, int oid)
  112. {
  113. Forum forum = new Forum(name, parent, type, perm, oid);
  114. forum.insertIntoDb();
  115. return forum;
  116. }
  117. /**
  118. * @return
  119. */
  120. public int getANewID()
  121. {
  122. return ++_lastid;
  123. }
  124. /**
  125. * @param idf
  126. * @return
  127. */
  128. public Forum getForumByID(int idf)
  129. {
  130. for (Forum f : _table)
  131. {
  132. if (f.getID() == idf)
  133. {
  134. return f;
  135. }
  136. }
  137. return null;
  138. }
  139. @Override
  140. public void parsewrite(String ar1, String ar2, String ar3, String ar4, String ar5, L2PcInstance activeChar)
  141. {
  142. }
  143. @SuppressWarnings("synthetic-access")
  144. private static class SingletonHolder
  145. {
  146. protected static final ForumsBBSManager _instance = new ForumsBBSManager();
  147. }
  148. }