Topic.java 3.7 KB

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