📄 blogentry.java
字号:
return link.toString(); } /** * Gets a list of all comments and TrackBacks. * * @return a List of all Response instances */ public List<Response> getResponses() { List<Response> responses = new ArrayList(); responses.addAll(getComments()); responses.addAll(getTrackBacks()); Collections.sort(responses, new ResponseByDateComparator()); return responses; } /** * Gets a collection of all comments. * * @return a List of Comment instances */ public List<Comment> getComments() { List<Comment> allComments = new ArrayList(); Iterator it = comments.iterator(); while (it.hasNext()) { allComments.addAll(getComments((Comment)it.next())); } return allComments; } private List<Comment> getComments(Comment comment) { List<Comment> allComments = new ArrayList(); allComments.add(comment); Iterator it = comment.getComments().iterator(); while (it.hasNext()) { allComments.addAll(getComments((Comment)it.next())); } return allComments; } /** * Gets the number of comments that have been left for this blog entry. * * @return the number of comments as a int */ public int getNumberOfComments() { return getComments().size(); } /** * Gets a collection of all trackbacks. * * @return a List of TrackBack instances */ public List<TrackBack> getTrackBacks() { return new ArrayList<TrackBack>(trackBacks); } /** * Gets the number of trackbacks that have been left for this blog entry. * * @return the number of trackbacks as a int */ public int getNumberOfTrackBacks() { return trackBacks.size(); } /** * Gets the number of responses that have been left for this blog entry. * * @return the number of responses as a int */ public int getNumberOfResponses() { return getResponses().size(); } /** * Creates a new comment for this blog entry. This method doesn't actually * <b>add</b> the comment too. * * @param title the title of the comment * @param body the body of the comment * @param author the author of the comment * @param email the author's e-mail address * @param website the author's website * @param ipAddress the IP address of the author * @param date the date that the comment was created * @param state the state of the comment * @return a new Comment instance with the specified properties */ public Comment createComment(String title, String body, String author, String email, String website, String ipAddress, Date date, State state) { return new Comment(title, body, author, email, website, ipAddress, date, state, this); } /** * Creates a new comment for this blog entry, with a creation date of now. * This method doesn't actually <b>add</b> the comment too. * * @param title the title of the comment * @param body the body of the comment * @param author the author of the comment * @param email the author's e-mail address * @param website the author's website * @param ipAddress the IP address of the author * @return a new Comment instance with the specified properties */ public Comment createComment(String title, String body, String author, String email, String website, String ipAddress) { Calendar cal = getBlog().getCalendar(); return createComment(title, body, author, email, website, ipAddress, cal.getTime(), State.APPROVED); } /** * Adds the specified comment. * * @param comment a Comment instance */ public synchronized void addComment(Comment comment) { if (comment == null) { return; } Comment existingComment = getComment(comment.getId()); if (existingComment != null && existingComment != comment) { // there is an existing comment with the same ID, but it's // not the same instance comment.setDate(new Date(comment.getDate().getTime() + 1)); addComment(comment); } else if (existingComment != null) { return; } else { if (comment.getParent() != null) { Comment parent = getComment(comment.getParent().getId()); if (parent != null) { parent.addComment(comment); } else { comments.add(comment); } } else { comments.add(comment); } comment.setBlogEntry(this); if (areEventsEnabled()) { addEvent(new CommentEvent(comment, CommentEvent.COMMENT_ADDED)); comment.setEventsEnabled(true); } } } /** * Creates a new trackback for this blog entry. This method doesn't actually * <b>add</b> the trackback too. * * @param title the title of the entry * @param excerpt the excerpt of the entry * @param url the url (permalink) of the entry * @param blogName the name of the blog * @param ipAddress the IP address of the author * @param date the date the trackback was received * @return a new TrackBack instance with the specified properties */ public TrackBack createTrackBack(String title, String excerpt, String url, String blogName, String ipAddress, Date date, State state) { return new TrackBack(title, excerpt, url, blogName, ipAddress, date, state, this); } /** * Creates a new trackback for this blog entry with a date of now. * This method doesn't actually <b>add</b> the trackback too. * * @param title the title of the entry * @param excerpt the excerpt of the entry * @param url the url (permalink) of the entry * @param blogName the name of the blog * @param ipAddress the IP address of the author * @return a new Comment instance with the specified properties */ public TrackBack createTrackBack(String title, String excerpt, String url, String blogName, String ipAddress) { Calendar cal = getBlog().getCalendar(); return createTrackBack(title, excerpt, url, blogName, ipAddress, cal.getTime(), State.APPROVED); } /** * Adds the specified trackback. * * @param trackBack a TrackBack instance */ public synchronized void addTrackBack(TrackBack trackBack) { if (trackBack == null || trackBacks.contains(trackBack)) { return; } trackBacks.add(trackBack); if (areEventsEnabled()) { addEvent(new TrackBackEvent(trackBack, TrackBackEvent.TRACKBACK_ADDED)); trackBack.setEventsEnabled(true); } } /** * Removes the specified comment. * * @param id the id of the comment to be removed */ public synchronized void removeComment(long id) { Comment comment = getComment(id); if (comment != null) { // get all children and delete them for (Comment child : comment.getComments()) { comment.removeComment(child); } if (comment.getParent() != null) { comment.getParent().removeComment(comment); } else { comments.remove(comment); } if (areEventsEnabled()) { addEvent(new CommentEvent(comment, CommentEvent.COMMENT_REMOVED)); } } else { log.warn("A comment with id=" + id + " could not be found - " + "perhaps it has been removed already."); } } /** * Gets the specified comment. * * @param id the id of the comment */ public Comment getComment(long id) { Iterator it = getComments().iterator(); while (it.hasNext()) { Comment comment = (Comment) it.next(); if (comment.getId() == id) { return comment; } } return null; } /** * Gets the specified TrackBack. * * @param id the id of the TrackBack */ public TrackBack getTrackBack(long id) { Iterator it = getTrackBacks().iterator(); while (it.hasNext()) { TrackBack trackBack = (TrackBack)it.next(); if (trackBack.getId() == id) { return trackBack; } } return null; } /** * Gets the response specified by the guid. * * @param guid the response guid * @return a Response object, or null if no response exists */ public Response getResponse(String guid) { long id = Long.parseLong(guid.substring(guid.lastIndexOf("/")+1)); if (guid.startsWith("c")) { return getComment(id); } else { return getTrackBack(id); } } /** * Removes the specified TrackBack. * * @param id the id of the TrackBack to be removed */ public synchronized void removeTrackBack(long id) { TrackBack trackBack = getTrackBack(id); if (trackBack != null) { trackBacks.remove(trackBack); if (areEventsEnabled()) { addEvent(new TrackBackEvent(trackBack, TrackBackEvent.TRACKBACK_REMOVED)); } } else { log.warn("A TrackBack with id=" + id + " could not be found - " + "perhaps it has been removed already."); } } /** * Removes the specified comment or TrackBack. * * @param response the Response to be removed */ public void removeResponse(Response response) { if (response instanceof Comment) { removeComment(response.getId()); } else if (response instanceof TrackBack) { removeTrackBack(response.getId()); } } /** * Returns the blog entry that was posted before this one. * * @return a BlogEntry instance, or null if this is the first entry */ public BlogEntry getPreviousBlogEntry() { return getBlog().getPreviousBlogEntry(this); } /** * Returns the blog entry that was posted after this one. * * @return a BlogEntry instance, or null is this is the last entry */ public BlogEntry getNextBlogEntry() { return getBlog().getNextBlogEntry(this); } public void validate(ValidationContext context) { } /** * Indicates whether some other object is "equal to" this one. * * @param o the reference object with which to compare. * @return <code>true</code> if this object is the same as the obj * argument; <code>false</code> otherwise. * @see #hashCode() * @see java.util.Hashtable */ public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof BlogEntry)) { return false; } BlogEntry blogEntry = (BlogEntry)o; return getGuid().equals(blogEntry.getGuid()); } public String getGuid() { return "blogEntry/" + getBlog().getId() + "/" + getId(); } public int hashCode() { return getGuid().hashCode(); } /** * Creates and returns a copy of this object. * * @return a clone of this instance. * @see Cloneable */ public Object clone() { BlogEntry entry = new BlogEntry(getBlog()); entry.setEventsEnabled(false); entry.setPersistent(isPersistent()); entry.setPublished(isPublished()); entry.setTitle(getTitle()); entry.setSubtitle(getSubtitle()); entry.setExcerpt(getExcerpt()); entry.setBody(getBody()); entry.setDate(getDate()); entry.setTimeZoneId(timeZoneId); entry.setState(getState()); entry.setAuthor(getAuthor()); entry.setOriginalPermalink(getOriginalPermalink()); entry.setCommentsEnabled(commentsEnabled); entry.setTrackBacksEnabled(trackBacksEnabled); if (attachment != null) { entry.setAttachment((Attachment)attachment.clone()); } // copy the categories Iterator it = categories.iterator(); while (it.hasNext()) { entry.addCategory((Category)it.next()); } entry.setTags(tags); // also copy the comments it = getComments().iterator(); while (it.hasNext()) { Comment comment = (Comment)it.next(); Comment clonedComment = (Comment)comment.clone(); entry.addComment(clonedComment); } // and TrackBacks it = getTrackBacks().iterator(); while (it.hasNext()) { TrackBack trackBack = (TrackBack)it.next(); TrackBack clonedTrackBack = (TrackBack)trackBack.clone(); clonedTrackBack.setBlogEntry(entry); entry.addTrackBack(clonedTrackBack); } return entry; } /** * Sets whether events are enabled. * * @param b true to enable events, false otherwise */ void setEventsEnabled(boolean b) { super.setEventsEnabled(b); // and cascade for (Response response : getResponses()) { response.setEventsEnabled(b); } } public void clearEvents() { super.clearEvents(); for (Response response : getResponses()) { response.clearEvents(); } } /** * Sets the state of this blog entry. */ void setState(State state) { State previousState = getState(); super.setState(state); if (areEventsEnabled()) { if (isPublished() && previousState == State.UNPUBLISHED) { addEvent(new BlogEntryEvent(this, BlogEntryEvent.BLOG_ENTRY_PUBLISHED)); } else if (isUnpublished() && previousState == State.PUBLISHED) { addEvent(new BlogEntryEvent(this, BlogEntryEvent.BLOG_ENTRY_UNPUBLISHED)); } } } public String toString() { return getGuid() + ":" + super.hashCode(); } public TimeZone getTimeZone() { return TimeZone.getTimeZone(getTimeZoneId()); } public String getTimeZoneId() { if (this.timeZoneId != null) { return timeZoneId; } else { return getBlog().getTimeZoneId(); } } public void setTimeZoneId(String timeZoneId) { this.timeZoneId = timeZoneId; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -