Forum.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.communityserver.model;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.util.Map;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastMap;
  21. import com.l2jserver.communityserver.L2DatabaseFactory;
  22. import com.l2jserver.communityserver.model.Topic.ConstructorType;
  23. public class Forum
  24. {
  25. //type
  26. public static final int ROOT = 0;
  27. public static final int NORMAL = 1;
  28. public static final int CLAN = 2;
  29. public static final int PLAYER = 3;
  30. private static Logger _log = Logger.getLogger(Forum.class.getName());
  31. private Map<Integer, Topic> _topic;
  32. private int _forumId;
  33. private final int _sqlDPId;
  34. private String _forumName;
  35. private int _forumType;
  36. private int _ownerID;
  37. private boolean _loaded = false;
  38. /**
  39. * @param i
  40. */
  41. public Forum(final int sqlDPId, int Forumid)
  42. {
  43. _sqlDPId = sqlDPId;
  44. _forumId = Forumid;
  45. _topic = new FastMap<Integer, Topic>();
  46. }
  47. /**
  48. * @param name
  49. * @param parent
  50. * @param type
  51. * @param perm
  52. */
  53. public Forum(final int sqlDPId, int Forumid, String name, int type, int OwnerID)
  54. {
  55. _sqlDPId = sqlDPId;
  56. _forumName = name;
  57. _forumId = Forumid;
  58. _forumType = type;
  59. _ownerID = OwnerID;
  60. _topic = new FastMap<Integer, Topic>();
  61. _loaded = true;
  62. insertindb();
  63. if (type == Forum.PLAYER)
  64. {
  65. _topic.put(Topic.INBOX, new Topic(ConstructorType.CREATE, _sqlDPId, Topic.INBOX, Forumid, name, OwnerID, 0));
  66. _topic.put(Topic.OUTBOX, new Topic(ConstructorType.CREATE, _sqlDPId, Topic.OUTBOX, Forumid, name, OwnerID, 0));
  67. _topic.put(Topic.ARCHIVE, new Topic(ConstructorType.CREATE, _sqlDPId, Topic.ARCHIVE, Forumid, name, OwnerID, 0));
  68. _topic.put(Topic.TEMP_ARCHIVE, new Topic(ConstructorType.CREATE, _sqlDPId, Topic.TEMP_ARCHIVE, Forumid, name, OwnerID, 0));
  69. _topic.put(Topic.MEMO, new Topic(ConstructorType.CREATE, _sqlDPId, Topic.MEMO, Forumid, name, OwnerID, 0));
  70. }
  71. else if (type == Forum.CLAN)
  72. {
  73. _topic.put(Topic.ANNOUNCE, new Topic(ConstructorType.CREATE, _sqlDPId, Topic.ANNOUNCE, Forumid, name, OwnerID, 0));
  74. _topic.put(Topic.BULLETIN, new Topic(ConstructorType.CREATE, _sqlDPId, Topic.BULLETIN, Forumid, name, OwnerID, 0));
  75. }
  76. }
  77. /**
  78. *
  79. */
  80. private void load()
  81. {
  82. java.sql.Connection con = null;
  83. try
  84. {
  85. con = L2DatabaseFactory.getInstance().getConnection();
  86. PreparedStatement statement = con.prepareStatement("SELECT * FROM forums WHERE serverId=? AND forum_id=?");
  87. statement.setInt(1, _sqlDPId);
  88. statement.setInt(2, _forumId);
  89. ResultSet result = statement.executeQuery();
  90. if (result.next())
  91. {
  92. _forumName = result.getString("forum_name");
  93. _forumType = Integer.parseInt(result.getString("forum_type"));
  94. _ownerID = Integer.parseInt(result.getString("forum_owner_id"));
  95. }
  96. result.close();
  97. statement.close();
  98. }
  99. catch (Exception e)
  100. {
  101. _log.warning("data error on Forum " + _forumId + " : " + e);
  102. e.printStackTrace();
  103. }
  104. finally
  105. {
  106. try
  107. {
  108. con.close();
  109. }
  110. catch (Exception e)
  111. {
  112. }
  113. }
  114. try
  115. {
  116. con = L2DatabaseFactory.getInstance().getConnection();
  117. PreparedStatement statement = con.prepareStatement("SELECT * FROM topics WHERE serverId=? AND topic_forum_id=? ORDER BY topic_id DESC");
  118. statement.setInt(1, _sqlDPId);
  119. statement.setInt(2, _forumId);
  120. ResultSet result = statement.executeQuery();
  121. while (result.next())
  122. {
  123. Topic t = new Topic(Topic.ConstructorType.RESTORE, _sqlDPId, Integer.parseInt(result.getString("topic_id")), Integer.parseInt(result.getString("topic_forum_id")), result.getString("topic_name"), Integer.parseInt(result.getString("topic_ownerid")), Integer.parseInt(result.getString("topic_permissions")));
  124. _topic.put(t.getID(), t);
  125. }
  126. result.close();
  127. statement.close();
  128. }
  129. catch (Exception e)
  130. {
  131. _log.warning("data error on Forum " + _forumId + " : " + e);
  132. e.printStackTrace();
  133. }
  134. finally
  135. {
  136. try
  137. {
  138. con.close();
  139. }
  140. catch (Exception e)
  141. {
  142. }
  143. }
  144. }
  145. public int getTopicSize()
  146. {
  147. if (_loaded == false)
  148. {
  149. load();
  150. _loaded = true;
  151. }
  152. return _topic.size();
  153. }
  154. public Topic gettopic(int j)
  155. {
  156. if (_loaded == false)
  157. {
  158. load();
  159. _loaded = true;
  160. }
  161. return _topic.get(j);
  162. }
  163. public void addtopic(Topic t)
  164. {
  165. if (_loaded == false)
  166. {
  167. load();
  168. _loaded = true;
  169. }
  170. _topic.put(t.getID(), t);
  171. }
  172. /**
  173. * @return
  174. */
  175. public int getID()
  176. {
  177. return _forumId;
  178. }
  179. public int getOwner()
  180. {
  181. return _ownerID;
  182. }
  183. public String getName()
  184. {
  185. if (_loaded == false)
  186. {
  187. load();
  188. _loaded = true;
  189. }
  190. return _forumName;
  191. }
  192. public int getType()
  193. {
  194. if (_loaded == false)
  195. {
  196. load();
  197. _loaded = true;
  198. }
  199. return _forumType;
  200. }
  201. /**
  202. * @param id
  203. */
  204. public void rmTopicByID(int id)
  205. {
  206. _topic.remove(id);
  207. }
  208. public final int getSqlDPId()
  209. {
  210. return _sqlDPId;
  211. }
  212. /**
  213. *
  214. */
  215. public void insertindb()
  216. {
  217. java.sql.Connection con = null;
  218. try
  219. {
  220. // TODO: needs to be changed
  221. con = L2DatabaseFactory.getInstance().getConnection();
  222. PreparedStatement statement = con.prepareStatement("INSERT INTO forums (serverId,forum_id,forum_name,forum_type,forum_owner_id) values (?,?,?,?,?)");
  223. statement.setInt(1, _sqlDPId);
  224. statement.setInt(2, _forumId);
  225. statement.setString(3, _forumName);
  226. statement.setInt(4, _forumType);
  227. statement.setInt(5, _ownerID);
  228. statement.execute();
  229. statement.close();
  230. }
  231. catch (Exception e)
  232. {
  233. _log.warning("error while saving new Forum to db " + e);
  234. }
  235. finally
  236. {
  237. try
  238. {
  239. con.close();
  240. }
  241. catch (Exception e)
  242. {
  243. }
  244. }
  245. }
  246. /**
  247. *
  248. */
  249. public void vload()
  250. {
  251. if (_loaded == false)
  252. {
  253. load();
  254. _loaded = true;
  255. }
  256. }
  257. }