ForumsBBSManager.java 3.9 KB

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