| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.domain; |
| 33 | |
|
| 34 | |
import net.sourceforge.pebble.api.event.comment.CommentEvent; |
| 35 | |
import net.sourceforge.pebble.util.StringUtils; |
| 36 | |
|
| 37 | |
import java.util.ArrayList; |
| 38 | |
import java.util.Date; |
| 39 | |
import java.util.List; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public class Comment extends Response { |
| 47 | |
|
| 48 | |
|
| 49 | |
private String body; |
| 50 | |
|
| 51 | |
|
| 52 | |
private String author; |
| 53 | |
|
| 54 | |
|
| 55 | |
private String email; |
| 56 | |
|
| 57 | |
|
| 58 | |
private String website; |
| 59 | |
|
| 60 | |
|
| 61 | |
private Comment parent; |
| 62 | |
|
| 63 | |
|
| 64 | |
private String avatar; |
| 65 | |
|
| 66 | |
|
| 67 | 900 | private List comments = new ArrayList(); |
| 68 | |
|
| 69 | |
|
| 70 | 900 | private boolean authenticated = false; |
| 71 | |
|
| 72 | 0 | public Comment() { |
| 73 | 0 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
Comment(String title, String body, String author, String email, String website, String avatar, String ipAddress, Date date, State state, BlogEntry blogEntry) { |
| 88 | 900 | super(title, ipAddress, date, state, blogEntry); |
| 89 | |
|
| 90 | 900 | setBody(body); |
| 91 | 900 | setAuthor(author); |
| 92 | 900 | setEmail(email); |
| 93 | 900 | setWebsite(website); |
| 94 | 900 | setAvatar(avatar); |
| 95 | 900 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
public String getBody() { |
| 103 | 692 | return body; |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public String getContent() { |
| 112 | 444 | return getBody(); |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public String getTruncatedBody() { |
| 121 | 32 | return this.getTruncatedContent(); |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public void setTitle(String title) { |
| 130 | 940 | if (title == null || title.length() == 0) { |
| 131 | 96 | if (blogEntry != null) { |
| 132 | 96 | this.title = "Re: " + blogEntry.getTitle(); |
| 133 | |
} else { |
| 134 | 0 | this.title = "Comment"; |
| 135 | |
} |
| 136 | |
} else { |
| 137 | 844 | this.title = title; |
| 138 | |
} |
| 139 | 940 | } |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public void setBody(String body) { |
| 147 | 1144 | if (body == null || body.length() == 0) { |
| 148 | 56 | this.body = null; |
| 149 | |
} else { |
| 150 | 1088 | this.body = body; |
| 151 | |
} |
| 152 | 1144 | } |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
public String getAuthor() { |
| 160 | 612 | return author; |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
public String getSourceName() { |
| 169 | 224 | return getAuthor(); |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
public void setAuthor(String author) { |
| 179 | 952 | if (author == null || author.length() == 0) { |
| 180 | 36 | this.author = "Anonymous"; |
| 181 | |
} else { |
| 182 | 916 | this.author = author; |
| 183 | |
} |
| 184 | 952 | } |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public String getEmail() { |
| 192 | 96 | return email; |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public void setEmail(String email) { |
| 201 | 944 | if (email == null || email.length() == 0) { |
| 202 | 184 | this.email = null; |
| 203 | |
} else { |
| 204 | 760 | this.email = StringUtils.transformHTML(email); |
| 205 | |
} |
| 206 | 944 | } |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
public String getWebsite() { |
| 214 | 348 | return website; |
| 215 | |
} |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
public String getSourceLink() { |
| 223 | 224 | return getWebsite(); |
| 224 | |
} |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
public void setWebsite(String website) { |
| 232 | 972 | this.website = StringUtils.validateUrl(StringUtils.filterHTML(website)); |
| 233 | 972 | } |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
public String getPermalink() { |
| 241 | 4 | if (blogEntry != null) { |
| 242 | 4 | return blogEntry.getLocalPermalink() + "#comment" + getId(); |
| 243 | |
} else { |
| 244 | 0 | return ""; |
| 245 | |
} |
| 246 | |
} |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
public String getAvatar() { |
| 254 | 80 | return avatar; |
| 255 | |
} |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
public void setAvatar(String avatar) { |
| 263 | 932 | this.avatar = StringUtils.validateUrl(StringUtils.filterHTML(avatar)); |
| 264 | 932 | } |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
public Comment getParent() { |
| 272 | 732 | return this.parent; |
| 273 | |
} |
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
public void setParent(Comment parent) { |
| 281 | 424 | this.parent = parent; |
| 282 | 424 | } |
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
public int getNumberOfParents() { |
| 290 | 8 | int count = 0; |
| 291 | 8 | Comment c = getParent(); |
| 292 | 12 | while (c != null) { |
| 293 | 4 | count++; |
| 294 | 4 | c = c.getParent(); |
| 295 | |
} |
| 296 | |
|
| 297 | 8 | return count; |
| 298 | |
} |
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
void addComment(Comment comment) { |
| 306 | 72 | if (comment != null && !comments.contains(comment)) { |
| 307 | 68 | comments.add(comment); |
| 308 | 68 | comment.setParent(this); |
| 309 | |
} |
| 310 | 72 | } |
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
void removeComment(Comment comment) { |
| 318 | 48 | if (comment != null && comments.contains(comment)) { |
| 319 | |
|
| 320 | 20 | for (Comment child : getComments()) { |
| 321 | 24 | comment.removeComment(child); |
| 322 | |
} |
| 323 | |
|
| 324 | |
|
| 325 | 20 | comments.remove(comment); |
| 326 | 20 | comment.setParent(null); |
| 327 | |
|
| 328 | 20 | if (areEventsEnabled()) { |
| 329 | 16 | getBlogEntry().addEvent(new CommentEvent(comment, CommentEvent.COMMENT_REMOVED)); |
| 330 | |
} |
| 331 | |
} |
| 332 | 48 | } |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
public List<Comment> getComments() { |
| 340 | 7072 | return new ArrayList<Comment>(comments); |
| 341 | |
} |
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
public Object clone() { |
| 350 | 292 | Comment comment = new Comment(title, body, author, email, website, avatar, ipAddress, date, getState(), blogEntry); |
| 351 | 292 | comment.setParent(parent); |
| 352 | 292 | comment.setAuthenticated(authenticated); |
| 353 | 292 | return comment; |
| 354 | |
} |
| 355 | |
|
| 356 | |
public boolean equals(Object o) { |
| 357 | 196 | if (this == o) { |
| 358 | 168 | return true; |
| 359 | |
} |
| 360 | |
|
| 361 | 28 | if (!(o instanceof Comment)) { |
| 362 | 0 | return false; |
| 363 | |
} |
| 364 | |
|
| 365 | 28 | Comment comment = (Comment)o; |
| 366 | 28 | return (getId() == comment.getId() && blogEntry.getId().equals(comment.getBlogEntry().getId())); |
| 367 | |
} |
| 368 | |
|
| 369 | |
public int hashCode() { |
| 370 | 0 | return ("" + getId()).hashCode(); |
| 371 | |
} |
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
void setState(State s) { |
| 377 | 1276 | State previousState = getState(); |
| 378 | 1276 | super.setState(s); |
| 379 | |
|
| 380 | 1276 | if (areEventsEnabled()) { |
| 381 | 236 | if (isApproved() && previousState != State.APPROVED) { |
| 382 | 92 | getBlogEntry().addEvent(new CommentEvent(this, CommentEvent.COMMENT_APPROVED)); |
| 383 | 144 | } else if (isRejected() && previousState != State.REJECTED) { |
| 384 | 8 | getBlogEntry().addEvent(new CommentEvent(this, CommentEvent.COMMENT_REJECTED)); |
| 385 | |
} |
| 386 | |
} |
| 387 | 1276 | } |
| 388 | |
|
| 389 | |
public boolean isAuthenticated() { |
| 390 | 28 | return authenticated; |
| 391 | |
} |
| 392 | |
|
| 393 | |
public void setAuthenticated(boolean authenticated) { |
| 394 | 324 | this.authenticated = authenticated; |
| 395 | 324 | } |
| 396 | |
|
| 397 | |
@Override |
| 398 | |
public boolean isHasEmail() { |
| 399 | 0 | return true; |
| 400 | |
} |
| 401 | |
} |