Topic.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.BB;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  25. import com.l2jserver.gameserver.communitybbs.Manager.TopicBBSManager;
  26. public class Topic
  27. {
  28. private static final Logger _log = LoggerFactory.getLogger(Topic.class);
  29. public static final int MORMAL = 0;
  30. public static final int MEMO = 1;
  31. private final int _id;
  32. private final int _forumId;
  33. private final String _topicName;
  34. private final long _date;
  35. private final String _ownerName;
  36. private final int _ownerId;
  37. private final int _type;
  38. private final int _cReply;
  39. /**
  40. * @param ct
  41. * @param id
  42. * @param fid
  43. * @param name
  44. * @param date
  45. * @param oname
  46. * @param oid
  47. * @param type
  48. * @param Creply
  49. */
  50. public Topic(ConstructorType ct, int id, int fid, String name, long date, String oname, int oid, int type, int Creply)
  51. {
  52. _id = id;
  53. _forumId = fid;
  54. _topicName = name;
  55. _date = date;
  56. _ownerName = oname;
  57. _ownerId = oid;
  58. _type = type;
  59. _cReply = Creply;
  60. TopicBBSManager.getInstance().addTopic(this);
  61. if (ct == ConstructorType.CREATE)
  62. {
  63. insertindb();
  64. }
  65. }
  66. public void insertindb()
  67. {
  68. try (Connection con = ConnectionFactory.getInstance().getConnection();
  69. PreparedStatement ps = con.prepareStatement("INSERT INTO topic (topic_id,topic_forum_id,topic_name,topic_date,topic_ownername,topic_ownerid,topic_type,topic_reply) values (?,?,?,?,?,?,?,?)"))
  70. {
  71. ps.setInt(1, _id);
  72. ps.setInt(2, _forumId);
  73. ps.setString(3, _topicName);
  74. ps.setLong(4, _date);
  75. ps.setString(5, _ownerName);
  76. ps.setInt(6, _ownerId);
  77. ps.setInt(7, _type);
  78. ps.setInt(8, _cReply);
  79. ps.execute();
  80. }
  81. catch (Exception e)
  82. {
  83. _log.warn("Error while saving new Topic to database!", e);
  84. }
  85. }
  86. public enum ConstructorType
  87. {
  88. RESTORE,
  89. CREATE
  90. }
  91. /**
  92. * @return the topic Id
  93. */
  94. public int getID()
  95. {
  96. return _id;
  97. }
  98. public int getForumID()
  99. {
  100. return _forumId;
  101. }
  102. /**
  103. * @return the topic name
  104. */
  105. public String getName()
  106. {
  107. return _topicName;
  108. }
  109. public String getOwnerName()
  110. {
  111. return _ownerName;
  112. }
  113. /**
  114. * @param f
  115. */
  116. public void deleteme(Forum f)
  117. {
  118. TopicBBSManager.getInstance().delTopic(this);
  119. f.rmTopicByID(getID());
  120. try (Connection con = ConnectionFactory.getInstance().getConnection();
  121. PreparedStatement ps = con.prepareStatement("DELETE FROM topic WHERE topic_id=? AND topic_forum_id=?"))
  122. {
  123. ps.setInt(1, getID());
  124. ps.setInt(2, f.getID());
  125. ps.execute();
  126. }
  127. catch (Exception e)
  128. {
  129. _log.warn("Error while deleting topic ID {} from database!", getID(), e);
  130. }
  131. }
  132. /**
  133. * @return the topic date
  134. */
  135. public long getDate()
  136. {
  137. return _date;
  138. }
  139. }