Announcement.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.model.announce;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  28. /**
  29. * @author UnAfraid
  30. */
  31. public class Announcement implements IAnnouncement
  32. {
  33. protected static final Logger _log = Logger.getLogger(Announcement.class.getName());
  34. private static final String INSERT_QUERY = "INSERT INTO announcements (type, content, author) VALUES (?, ?, ?)";
  35. private static final String UPDATE_QUERY = "UPDATE announcements SET type = ?, content = ?, author = ? WHERE id = ?";
  36. private static final String DELETE_QUERY = "DELETE FROM announcements WHERE id = ?";
  37. protected int _id;
  38. private AnnouncementType _type;
  39. private String _content;
  40. private String _author;
  41. public Announcement(AnnouncementType type, String content, String author)
  42. {
  43. _type = type;
  44. _content = content;
  45. _author = author;
  46. }
  47. public Announcement(ResultSet rset) throws SQLException
  48. {
  49. _id = rset.getInt("id");
  50. _type = AnnouncementType.findById(rset.getInt("type"));
  51. _content = rset.getString("content");
  52. _author = rset.getString("author");
  53. }
  54. @Override
  55. public int getId()
  56. {
  57. return _id;
  58. }
  59. @Override
  60. public AnnouncementType getType()
  61. {
  62. return _type;
  63. }
  64. @Override
  65. public void setType(AnnouncementType type)
  66. {
  67. _type = type;
  68. }
  69. @Override
  70. public String getContent()
  71. {
  72. return _content;
  73. }
  74. @Override
  75. public void setContent(String content)
  76. {
  77. _content = content;
  78. }
  79. @Override
  80. public String getAuthor()
  81. {
  82. return _author;
  83. }
  84. @Override
  85. public void setAuthor(String author)
  86. {
  87. _author = author;
  88. }
  89. @Override
  90. public boolean isValid()
  91. {
  92. return true;
  93. }
  94. @Override
  95. public boolean storeMe()
  96. {
  97. try (Connection con = ConnectionFactory.getInstance().getConnection();
  98. PreparedStatement ps = con.prepareStatement(INSERT_QUERY, Statement.RETURN_GENERATED_KEYS))
  99. {
  100. ps.setInt(1, _type.ordinal());
  101. ps.setString(2, _content);
  102. ps.setString(3, _author);
  103. ps.execute();
  104. try (ResultSet rset = ps.getGeneratedKeys())
  105. {
  106. if (rset.next())
  107. {
  108. _id = rset.getInt(1);
  109. }
  110. }
  111. }
  112. catch (Exception e)
  113. {
  114. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't store announcement: ", e);
  115. return false;
  116. }
  117. return true;
  118. }
  119. @Override
  120. public boolean updateMe()
  121. {
  122. try (Connection con = ConnectionFactory.getInstance().getConnection();
  123. PreparedStatement ps = con.prepareStatement(UPDATE_QUERY))
  124. {
  125. ps.setInt(1, _type.ordinal());
  126. ps.setString(2, _content);
  127. ps.setString(3, _author);
  128. ps.setInt(4, _id);
  129. ps.execute();
  130. }
  131. catch (Exception e)
  132. {
  133. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't store announcement: ", e);
  134. return false;
  135. }
  136. return true;
  137. }
  138. @Override
  139. public boolean deleteMe()
  140. {
  141. try (Connection con = ConnectionFactory.getInstance().getConnection();
  142. PreparedStatement ps = con.prepareStatement(DELETE_QUERY))
  143. {
  144. ps.setInt(1, _id);
  145. ps.execute();
  146. }
  147. catch (Exception e)
  148. {
  149. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't remove announcement: ", e);
  150. return false;
  151. }
  152. return true;
  153. }
  154. }