PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/com.atlassian.connector.eclipse.model/src/com/atlassian/theplugin/commons/crucible/api/model/Comment.java

https://github.com/spingel/mylyn-reviews
Java | 350 lines | 261 code | 60 blank | 29 comment | 64 complexity | 25e0bbfc8cb6a189f8d7bc19dd726375 MD5 | raw file
  1. /**
  2. * Copyright (C) 2008 Atlassian
  3. *
  4. * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
  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 com.atlassian.theplugin.commons.crucible.api.model;
  17. import com.atlassian.theplugin.commons.util.MiscUtil;
  18. import org.jetbrains.annotations.Nullable;
  19. import java.util.ArrayList;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. public abstract class Comment {
  25. public enum ReadState {
  26. UNKNOWN("Unknown"), READ("Read"), UNREAD("Not read"), LEAVE_UNREAD("Leave unread");
  27. private final String name;
  28. ReadState(String name) {
  29. this.name = name;
  30. }
  31. @Override
  32. public String toString() {
  33. return name;
  34. }
  35. }
  36. private PermId permId;
  37. private String message = null;
  38. private boolean draft = false;
  39. private boolean deleted = false;
  40. private boolean defectRaised = false;
  41. private boolean defectApproved = false;
  42. private User author = null;
  43. private Date createDate = new Date();
  44. private ReadState readState;
  45. private List<Comment> replies = new ArrayList<Comment>();
  46. private boolean isReply = false;
  47. private final Map<String, CustomField> customFields = new HashMap<String, CustomField>();
  48. private static final int HASH_INT = 31;
  49. private final Review review;
  50. @Nullable
  51. private final Comment parentComment;
  52. public Comment(Review review, @Nullable Comment parentComment) {
  53. this.review = review;
  54. this.parentComment = parentComment;
  55. }
  56. public Comment(Comment bean) {
  57. review = bean.getReview();
  58. parentComment = bean.parentComment;
  59. setPermId(bean.getPermId());
  60. setMessage(bean.getMessage());
  61. setDraft(bean.isDraft());
  62. setCreateDate(bean.getCreateDate());
  63. setDefectApproved(bean.isDefectApproved());
  64. setDefectRaised(bean.isDefectRaised());
  65. setDeleted(bean.isDeleted());
  66. setAuthor(bean.getAuthor());
  67. setAuthor(bean.getAuthor());
  68. setReply(bean.isReply());
  69. setReadState(bean.getReadState());
  70. if (bean.getCustomFields() != null) {
  71. for (Map.Entry<String, CustomField> entry : bean.getCustomFields().entrySet()) {
  72. getCustomFields().put(entry.getKey(), new CustomFieldBean(entry.getValue()));
  73. }
  74. }
  75. if (bean.getReplies() != null) {
  76. for (Comment reply : bean.getReplies()) {
  77. replies.add(createReplyBean(reply));
  78. }
  79. }
  80. }
  81. protected abstract Comment createReplyBean(Comment reply);
  82. @Nullable
  83. public Comment getParentComment() {
  84. return parentComment;
  85. }
  86. public Review getReview() {
  87. return review;
  88. }
  89. public PermId getPermId() {
  90. return permId;
  91. }
  92. public void setPermId(PermId permId) {
  93. this.permId = permId;
  94. }
  95. public String getMessage() {
  96. return message;
  97. }
  98. public void setMessage(String message) {
  99. this.message = message;
  100. }
  101. public boolean isDraft() {
  102. return draft;
  103. }
  104. public void setDraft(boolean draft) {
  105. this.draft = draft;
  106. }
  107. public boolean isDeleted() {
  108. return deleted;
  109. }
  110. public void setDeleted(boolean deleted) {
  111. this.deleted = deleted;
  112. }
  113. public boolean isDefectRaised() {
  114. return defectRaised;
  115. }
  116. public void setDefectRaised(boolean defectRaised) {
  117. this.defectRaised = defectRaised;
  118. }
  119. public boolean isDefectApproved() {
  120. return defectApproved;
  121. }
  122. public boolean isReply() {
  123. return isReply;
  124. }
  125. public void setReply(boolean reply) {
  126. isReply = reply;
  127. }
  128. public void setReplies(List<Comment> replies) {
  129. if (replies == null) {
  130. this.replies = new ArrayList<Comment>();
  131. } else {
  132. this.replies = replies;
  133. }
  134. }
  135. public void addReply(Comment comment) {
  136. replies.add(comment);
  137. }
  138. /**
  139. * Removes reply from this comment.
  140. * @param reply reply to remove
  141. * @return true if given reply was a reply to this comment and was removed, false otherwise
  142. */
  143. public boolean removeReply(@Nullable Comment reply) {
  144. return replies.remove(reply);
  145. }
  146. public List<Comment> getReplies() {
  147. return replies;
  148. }
  149. public void setDefectApproved(boolean defectApproved) {
  150. this.defectApproved = defectApproved;
  151. }
  152. public User getAuthor() {
  153. return author;
  154. }
  155. public void setAuthor(User author) {
  156. this.author = author;
  157. }
  158. public Date getCreateDate() {
  159. return new Date(createDate.getTime());
  160. }
  161. public void setCreateDate(Date createDate) {
  162. if (createDate != null) {
  163. this.createDate = new Date(createDate.getTime());
  164. }
  165. }
  166. public Map<String, CustomField> getCustomFields() {
  167. return customFields;
  168. }
  169. public ReadState getReadState() {
  170. return readState;
  171. }
  172. public void setReadState(ReadState readState) {
  173. this.readState = readState;
  174. }
  175. @Override
  176. public String toString() {
  177. return getMessage();
  178. }
  179. @Override
  180. public boolean equals(Object o) {
  181. if (this == o) {
  182. return true;
  183. }
  184. if (o == null || getClass() != o.getClass()) {
  185. return false;
  186. }
  187. Comment that = (Comment) o;
  188. if (!MiscUtil.isEqual(review.getPermId(), that.review.getPermId())) {
  189. return false;
  190. }
  191. // if (parentComment == null) {
  192. // if (that.parentComment != null) {
  193. // return false;
  194. // }
  195. // } else {
  196. //
  197. // }
  198. if (!MiscUtil.isEqual(parentComment, that.parentComment)) {
  199. return false;
  200. }
  201. if (defectApproved != that.defectApproved) {
  202. return false;
  203. }
  204. if (defectRaised != that.defectRaised) {
  205. return false;
  206. }
  207. if (deleted != that.deleted) {
  208. return false;
  209. }
  210. if (draft != that.draft) {
  211. return false;
  212. }
  213. if (isReply != that.isReply) {
  214. return false;
  215. }
  216. if (author != null ? !author.equals(that.author) : that.author != null) {
  217. return false;
  218. }
  219. if (createDate != null ? !createDate.equals(that.createDate) : that.createDate != null) {
  220. return false;
  221. }
  222. if (customFields != null ? !customFields.equals(that.customFields) : that.customFields != null) {
  223. return false;
  224. }
  225. if (message != null ? !message.equals(that.message) : that.message != null) {
  226. return false;
  227. }
  228. if (permId != null ? !permId.equals(that.permId) : that.permId != null) {
  229. return false;
  230. }
  231. //noinspection RedundantIfStatement
  232. if (readState != null ? !readState.equals(that.readState) : that.readState != null) {
  233. return false;
  234. }
  235. return true;
  236. }
  237. @Override
  238. public int hashCode() {
  239. int result;
  240. result = (permId != null ? permId.hashCode() : 0);
  241. result = HASH_INT * result + (message != null ? message.hashCode() : 0);
  242. result = HASH_INT * result + (draft ? 1 : 0);
  243. result = HASH_INT * result + (deleted ? 1 : 0);
  244. result = HASH_INT * result + (defectRaised ? 1 : 0);
  245. result = HASH_INT * result + (defectApproved ? 1 : 0);
  246. result = HASH_INT * result + (author != null ? author.hashCode() : 0);
  247. result = HASH_INT * result + (createDate != null ? createDate.hashCode() : 0);
  248. result = HASH_INT * result + (isReply ? 1 : 0);
  249. result = HASH_INT * result + (customFields != null ? customFields.hashCode() : 0);
  250. result = HASH_INT * result + (readState != null ? readState.ordinal() : 0);
  251. return result;
  252. }
  253. public int getNumReplies() {
  254. int res = replies.size();
  255. for (Comment reply : replies) {
  256. res += reply.getNumReplies();
  257. }
  258. return res;
  259. }
  260. public int getNumberOfUnreadReplies() {
  261. int counter = 0;
  262. for (Comment reply : replies) {
  263. if (reply.isEffectivelyUnread()) {
  264. ++counter;
  265. }
  266. counter += reply.getNumberOfUnreadReplies();
  267. }
  268. return counter;
  269. }
  270. public int getNumberOfDraftReplies() {
  271. int counter = 0;
  272. for (Comment reply : replies) {
  273. if (reply.isDraft()) {
  274. ++counter;
  275. }
  276. counter += reply.getNumberOfDraftReplies();
  277. }
  278. return counter;
  279. }
  280. public boolean isEffectivelyUnread() {
  281. return (getReadState() == Comment.ReadState.UNREAD || getReadState() == Comment.ReadState.LEAVE_UNREAD);
  282. }
  283. public boolean hasDraftParents() {
  284. //noinspection SimplifiableIfStatement
  285. if (parentComment == null) {
  286. return false;
  287. }
  288. return parentComment.isDraft() || parentComment.hasDraftParents();
  289. }
  290. }