Comment.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
  3. *
  4. * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.gnu.org/licenses/lgpl-3.0.txt
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.jpress.model;
  17. import io.jpress.model.base.BaseComment;
  18. import io.jpress.model.core.Table;
  19. import io.jpress.model.query.CommentQuery;
  20. import io.jpress.model.query.ContentQuery;
  21. import io.jpress.model.query.UserQuery;
  22. import io.jpress.model.query.VoteQuery;
  23. import java.math.BigInteger;
  24. @Table(tableName = "comment", primaryKey = "id")
  25. public class Comment extends BaseComment<Comment> {
  26. private static final long serialVersionUID = 1L;
  27. public static final String TYPE_COMMENT = "comment";
  28. public static String STATUS_DELETE = "delete";
  29. public static String STATUS_DRAFT = "draft";
  30. public static String STATUS_NORMAL = "normal";
  31. private Content content;
  32. private User user;
  33. private Comment parent;
  34. public Content getContent() {
  35. if (content != null) {
  36. return content;
  37. }
  38. if (getContentId() != null) {
  39. content = ContentQuery.me().findById(getContentId());
  40. }
  41. return content;
  42. }
  43. public Content getContentById(BigInteger id) {
  44. return ContentQuery.me().findById(id);
  45. }
  46. public void setContent(Content content) {
  47. this.content = content;
  48. }
  49. public User getUser() {
  50. if (user != null) {
  51. return user;
  52. }
  53. if (getUserId() != null) {
  54. user = UserQuery.me().findById(getUserId());
  55. }
  56. return user;
  57. }
  58. public void setUser(User user) {
  59. this.user = user;
  60. }
  61. public Comment getParent() {
  62. if (parent != null) {
  63. return parent;
  64. }
  65. if (getContentId() != null) {
  66. parent = CommentQuery.me().findById(getParentId());
  67. }
  68. return parent;
  69. }
  70. public void setParent(Comment parent) {
  71. this.parent = parent;
  72. }
  73. public boolean isDelete() {
  74. return STATUS_DELETE.equals(getStatus());
  75. }
  76. public String getContentUrl() {
  77. BigInteger contentId = getContentId();
  78. if (contentId == null)
  79. return null;
  80. Content c = ContentQuery.me().findById(contentId);
  81. return c == null ? null : c.getUrl();
  82. }
  83. public boolean updateCommentCount() {
  84. long count = CommentQuery.me().findCountByParentIdInNormal(getId());
  85. if (count > 0) {
  86. setCommentCount(count);
  87. return this.update();
  88. }
  89. return false;
  90. }
  91. @Override
  92. public boolean update() {
  93. removeCache(getId());
  94. removeCache(getSlug());
  95. return super.update();
  96. }
  97. @Override
  98. public boolean delete() {
  99. removeCache(getId());
  100. removeCache(getSlug());
  101. return super.delete();
  102. }
  103. @Override
  104. public boolean save() {
  105. removeCache(getId());
  106. removeCache(getSlug());
  107. return super.save();
  108. }
  109. public Comment getlatestSon(BigInteger id){
  110. return CommentQuery.me().findLatestSon(id);
  111. }
  112. public boolean updateVoteUpCount() {
  113. long count = VoteQuery.me().findCountByCommentId(getId());
  114. if (count > 0) {
  115. setVoteUp(count);
  116. return this.update();
  117. }
  118. return false;
  119. }
  120. //判断是否处于已赞状态
  121. public boolean isvoted(BigInteger commentId, BigInteger userId, BigInteger uuUserId) {
  122. return VoteQuery.me().findCommentCountVote(commentId, userId, uuUserId);
  123. }
  124. }