Topic.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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.Collection;
  19. import java.util.Map;
  20. import java.util.logging.Logger;
  21. import javolution.util.FastList;
  22. import javolution.util.FastMap;
  23. import com.l2jserver.communityserver.L2DatabaseFactory;
  24. public class Topic
  25. {
  26. private static Logger _log = Logger.getLogger(Topic.class.getName());
  27. // type
  28. public static final int SERVER = 0;
  29. public static final int INBOX = 1;
  30. public static final int OUTBOX = 2;
  31. public static final int ARCHIVE = 3;
  32. public static final int TEMP_ARCHIVE = 4;
  33. public static final int MEMO = 5;
  34. public static final int ANNOUNCE = 6;
  35. public static final int BULLETIN = 7;
  36. //perm
  37. public static final int NONE = 0;
  38. public static final int ALL = 1;
  39. public static final int READ = 2;
  40. private int _id; // same as type
  41. private int _forumId;
  42. private final int _sqlDPId;
  43. private String _topicName;
  44. private int _ownerId;
  45. private int _lastPostId;
  46. private int _permissions;
  47. private Map<Integer, Post> _posts;
  48. /**
  49. * @param restaure
  50. * @param i
  51. * @param j
  52. * @param string
  53. * @param k
  54. * @param string2
  55. * @param l
  56. * @param m
  57. * @param n
  58. */
  59. public Topic(ConstructorType ct, final int sqlDPId, int id, int fid, String name, int oid, int per)
  60. {
  61. _sqlDPId = sqlDPId;
  62. _id = id;
  63. _forumId = fid;
  64. _topicName = name;
  65. _ownerId = oid;
  66. _lastPostId = 0;
  67. _permissions = per;
  68. _posts = new FastMap<Integer, Post>();
  69. if (ct == ConstructorType.CREATE)
  70. {
  71. insertindb();
  72. }
  73. else
  74. {
  75. loadPosts();
  76. }
  77. }
  78. private void loadPosts()
  79. {
  80. java.sql.Connection con = null;
  81. try
  82. {
  83. con = L2DatabaseFactory.getInstance().getConnection();
  84. PreparedStatement statement = con.prepareStatement("SELECT * FROM posts WHERE serverId=? AND post_forum_id=? AND post_topic_id=?");
  85. statement.setInt(1, _sqlDPId);
  86. statement.setInt(2, _forumId);
  87. statement.setInt(3, _id);
  88. ResultSet result = statement.executeQuery();
  89. while (result.next())
  90. {
  91. int postId = Integer.parseInt(result.getString("post_id"));
  92. int postOwner = Integer.parseInt(result.getString("post_ownerid"));
  93. String recipientList = result.getString("post_recipient_list");
  94. long date = Long.parseLong(result.getString("post_date"));
  95. String title = result.getString("post_title");
  96. String text = result.getString("post_txt");
  97. int type = Integer.parseInt(result.getString("post_type"));
  98. int parentId = Integer.parseInt(result.getString("post_parent_id"));
  99. int readCount = Integer.parseInt(result.getString("post_read_count"));
  100. Post p = new Post(ConstructorType.RESTORE, _sqlDPId, postId, postOwner, recipientList, parentId, date, _id, _forumId, title, text, type, readCount);
  101. _posts.put(postId, p);
  102. if (postId > _lastPostId)
  103. _lastPostId = postId;
  104. }
  105. result.close();
  106. statement.close();
  107. }
  108. catch (Exception e)
  109. {
  110. _log.warning("data error on Forum " + _forumId + " : " + e);
  111. e.printStackTrace();
  112. }
  113. finally
  114. {
  115. try
  116. {
  117. con.close();
  118. }
  119. catch (Exception e)
  120. {
  121. }
  122. }
  123. }
  124. public int getNewPostId()
  125. {
  126. return ++_lastPostId;
  127. }
  128. /**
  129. *
  130. */
  131. public void insertindb()
  132. {
  133. java.sql.Connection con = null;
  134. try
  135. {
  136. con = L2DatabaseFactory.getInstance().getConnection();
  137. PreparedStatement statement = con.prepareStatement("INSERT INTO topics (serverId,topic_id,topic_forum_id,topic_name,topic_ownerid,topic_permissions) values (?,?,?,?,?,?)");
  138. statement.setInt(1, _sqlDPId);
  139. statement.setInt(2, _id);
  140. statement.setInt(3, _forumId);
  141. statement.setString(4, _topicName);
  142. statement.setInt(5, _ownerId);
  143. statement.setInt(6, _permissions);
  144. statement.execute();
  145. statement.close();
  146. }
  147. catch (Exception e)
  148. {
  149. _log.warning("error while saving new Topic to db " + e);
  150. }
  151. finally
  152. {
  153. try
  154. {
  155. con.close();
  156. }
  157. catch (Exception e)
  158. {
  159. }
  160. }
  161. }
  162. public enum ConstructorType
  163. {
  164. RESTORE,
  165. CREATE
  166. }
  167. public void clearPosts()
  168. {
  169. _posts.clear();
  170. }
  171. public int getPostsSize()
  172. {
  173. return _posts.size();
  174. }
  175. public Post getPost(int j)
  176. {
  177. return _posts.get(j);
  178. }
  179. public void addPost(Post p)
  180. {
  181. _posts.put(p.getID(), p);
  182. }
  183. public void rmPostByID(int id)
  184. {
  185. _posts.get(id).deleteme();
  186. _posts.remove(id);
  187. }
  188. public Collection<Post> getAllPosts()
  189. {
  190. return _posts.values();
  191. }
  192. public Post[] getLastTwoPosts()
  193. {
  194. // if the Topic type is Announce then only Advertise Posts count
  195. Post[] ret = new Post[2];
  196. for (Post p: _posts.values())
  197. {
  198. if (_id == ANNOUNCE && p.getType() != Post.ADVERTISE)
  199. continue;
  200. if (ret[0] == null || ret[0].getDate() < p.getDate())
  201. {
  202. ret[1] = ret[0];
  203. ret[0] = p;
  204. }
  205. }
  206. return ret;
  207. }
  208. public FastList<Post> getChildrenPosts(Post parent)
  209. {
  210. FastList<Post> ret = new FastList<Post>();
  211. if (parent == null)
  212. return ret;
  213. // parent post always the first
  214. ret.add(parent);
  215. for (Post p: _posts.values())
  216. if (p.getParentId() == parent.getID())
  217. ret.add(p);
  218. return ret;
  219. }
  220. /**
  221. * @return
  222. */
  223. public int getID()
  224. {
  225. return _id;
  226. }
  227. public int getForumID()
  228. {
  229. return _forumId;
  230. }
  231. /**
  232. * @return
  233. */
  234. public String getName()
  235. {
  236. // TODO Auto-generated method stub
  237. return _topicName;
  238. }
  239. /**
  240. *
  241. */
  242. public void deleteme(Forum f)
  243. {
  244. f.rmTopicByID(getID());
  245. java.sql.Connection con = null;
  246. try
  247. {
  248. con = L2DatabaseFactory.getInstance().getConnection();
  249. PreparedStatement statement = con.prepareStatement("DELETE FROM topics WHERE topic_id=? AND topic_forum_id=?");
  250. statement.setInt(1, getID());
  251. statement.setInt(2, f.getID());
  252. statement.execute();
  253. statement.close();
  254. }
  255. catch (Exception e)
  256. {
  257. e.printStackTrace();
  258. }
  259. finally
  260. {
  261. try
  262. {
  263. con.close();
  264. }
  265. catch (Exception e)
  266. {
  267. }
  268. }
  269. }
  270. public int getPermissions()
  271. {
  272. return _permissions;
  273. }
  274. public void setPermissions(int val)
  275. {
  276. _permissions = val;
  277. java.sql.Connection con = null;
  278. try
  279. {
  280. con = L2DatabaseFactory.getInstance().getConnection();
  281. PreparedStatement statement = con.prepareStatement("UPDATE topics SET topic_permissions=? WHERE serverId=? AND topic_id=? AND topic_forum_id=?");
  282. statement.setInt(1, _permissions);
  283. statement.setInt(2, _sqlDPId);
  284. statement.setInt(3, _id);
  285. statement.setInt(4, _forumId);
  286. statement.execute();
  287. statement.close();
  288. }
  289. catch (Exception e)
  290. {
  291. _log.warning("error while saving new permissions to db " + e);
  292. }
  293. finally
  294. {
  295. try
  296. {
  297. con.close();
  298. }
  299. catch (Exception e)
  300. {
  301. }
  302. }
  303. }
  304. }