📄 blogentry.java
字号:
/* * Copyright (c) 2003-2006, Simon Brown * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * - Neither the name of Pebble nor the names of its contributors may * be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */package net.sourceforge.pebble.domain;import net.sourceforge.pebble.api.event.blogentry.BlogEntryEvent;import net.sourceforge.pebble.api.event.comment.CommentEvent;import net.sourceforge.pebble.api.event.trackback.TrackBackEvent;import net.sourceforge.pebble.comparator.ResponseByDateComparator;import net.sourceforge.pebble.web.validation.ValidationContext;import net.sourceforge.pebble.trackback.TrackBackTokenManager;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import java.util.*;/** * Represents a blog entry. * * @author Simon Brown */public class BlogEntry extends PageBasedContent { /** * the log used by this class */ private static Log log = LogFactory.getLog(BlogEntry.class); public static final String EXCERPT_PROPERTY = "excerpt"; public static final String COMMENTS_ENABLED_PROPERTY = "commentsEnabed"; public static final String TRACKBACKS_ENABLED_PROPERTY = "trackBacksEnabled"; public static final String ATTACHMENT_PROPERTY = "attachment"; public static final String CATEGORIES_PROPERTY = "categories"; public static final String TAGS_PROPERTY = "tags"; /** the permalink */ private String permalink; /** * the category that the blog entry falls into */ private Set categories = new HashSet(); /** the list of tags for this blog entry */ private String tags = ""; /** the List of tags for this blog entry */ private List<Tag> tagsAsList = new ArrayList<Tag>(); /** * the excerpt of the blog entry */ private String excerpt = ""; /** * a flag to indicate whether comments are enabled for this entry */ private boolean commentsEnabled = true; /** * a flag to indicate whether TrackBacks are enabled for this entry */ private boolean trackBacksEnabled = true; /** * the collection of comments for the blog entry */ private List comments = new ArrayList(); /** * the collection of trackbacks for the blog entry */ private List trackBacks = new ArrayList(); /** the attachment for this blog entry, if applicable */ private Attachment attachment; /** the timezone that this entry was posted in */ private String timeZoneId; /** * Creates a new blog entry. * * @param blog the owning Blog */ public BlogEntry(Blog blog) { super(blog); setPublished(false); } /** * Sets the title of this blog entry. * * @param newTitle the title as a String */ public void setTitle(String newTitle) { super.setTitle(newTitle); // and cause the permalink to be re-generated this.permalink = null; } /** * Gets the category of this blog entry. * * @return the category as a String */ public Set<Category> getCategories() { return new HashSet<Category>(categories); } /** * Gets a list of all tags. * * @return a List of tags */ public List<Tag> getAllTags() { List<Tag> list = new ArrayList<Tag>(); if (getCategories().size() > 0) { Iterator it = getCategories().iterator(); while (it.hasNext()) { Category category = (Category)it.next(); List tagsForCategory = category.getAllTags(); Collections.reverse(tagsForCategory); Iterator jt = tagsForCategory.iterator(); while (jt.hasNext()) { Tag tag = (Tag)jt.next(); if (!list.contains(tag)) { list.add(tag); } } } } else { List tagsForCategory = getBlog().getRootCategory().getAllTags(); Iterator it = tagsForCategory.iterator(); while (it.hasNext()) { Tag tag = (Tag)it.next(); if (!list.contains(tag)) { list.add(tag); } } } Iterator it = getTagsAsList().iterator(); while (it.hasNext()) { Tag tag = (Tag)it.next(); if (!list.contains(tag)) { list.add(tag); } } Collections.sort(list); return list; } /** * Sets the category of this blog entry. * * @param category the category as a String */ public synchronized void addCategory(Category category) { if (category != null && !categories.contains(category)) { Set oldCategories = new HashSet(categories); categories.add(category); Set newCategories = new HashSet(categories); propertyChangeSupport.firePropertyChange(CATEGORIES_PROPERTY, oldCategories, newCategories); } } /** * Removes all categories from this blog entry. */ public synchronized void removeAllCategories() { propertyChangeSupport.firePropertyChange(CATEGORIES_PROPERTY, new HashSet(categories), new HashSet()); categories.clear(); } /** * Sets the categories for this blog entry. * * @param newCategories a Collection of Category instances */ public synchronized void setCategories(Collection newCategories) { if (newCategories != null) { Set oldCategories = new HashSet(categories); categories.clear(); Iterator it = newCategories.iterator(); while (it.hasNext()) { categories.add(it.next()); } propertyChangeSupport.firePropertyChange(CATEGORIES_PROPERTY, oldCategories, new HashSet(newCategories)); } } /** * Determines whether this blog entry is in the specified category. * * @param category a Category instance * @return true if this entry is in the specified category, * false otherwise */ public boolean inCategory(Category category) { if (category != null) { Iterator it = categories.iterator(); while (it.hasNext()) { Category c = (Category)it.next(); if (c.equals(category) || c.hasParent(category)) { return true; } } return false; } else { return true; } } /** * Determines whether this blog entry has the specified tag. * * @param s a String * @return true if this entry has the specified tag, * false otherwise */ public boolean hasTag(String s) { if (s != null) { return getAllTags().contains(new Tag(s, getBlog())); } else { return false; } } /** * Gets the tags associated with this category. * * @return a list of tags */ public String getTags() { return this.tags; } /** * Gets the tags associated with this category, as a List. * * @return a List of tags */ public List<Tag> getTagsAsList() { return this.tagsAsList; } /** * Sets the set of tags associated with this category. * * @param newTags a set of tags */ public void setTags(String newTags) { if (newTags != null && newTags.indexOf(",") > -1) { // if the tags have been comma separated, convert them to // whitespace separated by // - remove whitespace // - convert commas to whitespace newTags = newTags.replaceAll(" ", "").replaceAll(",", " "); } propertyChangeSupport.firePropertyChange(TAGS_PROPERTY, tags, newTags); this.tags = newTags; this.tagsAsList = Tag.parse(getBlog(), tags); } /** * Gets the content of this response. * * @return a String */ public String getContent() { if (excerpt != null && excerpt.length() > 0) { return excerpt; } else { return getBody(); } } /** * Gets the excerpt of this blog entry. * * @return the excerpt as a String */ public String getExcerpt() { return excerpt; } /** * Sets the excerpt of this blog entry. * * @param newExcerpt the excerpt as a String */ public void setExcerpt(String newExcerpt) { if (newExcerpt != null) { newExcerpt = newExcerpt.trim(); } propertyChangeSupport.firePropertyChange(EXCERPT_PROPERTY, excerpt, newExcerpt); this.excerpt = newExcerpt; } /** * Gets the date that this blog entry was last updated. * * @return a Date instance representing the time of the last comment/TrackBack */ public Date getLastModified() { Date date = getDate(); Iterator it = comments.iterator(); while (it.hasNext()) { Comment comment = (Comment)it.next(); if (comment.getDate().after(date)) { date = comment.getDate(); } } it = trackBacks.iterator(); while (it.hasNext()) { TrackBack trackBack = (TrackBack)it.next(); if (trackBack.getDate().after(date)) { date = trackBack.getDate(); } } return date; } /** * Sets the date that this blog entry was created. * * @param newDate a java.util.Date instance */ public void setDate(Date newDate) { super.setDate(newDate); // and cause the permalink to be re-generated this.permalink = null; } /** * Gets a permalink for this blog entry that is local to the blog. In other * words, it doesn't take into account the original permalink for * aggregated content. * * @return an absolute URL as a String */ public String getLocalPermalink() { if (this.permalink == null) { String s = getBlog().getPermalinkProvider().getPermalink(this); if (s != null && s.length() > 0) { this.permalink = getBlog().getUrl() + s.substring(1); } } return permalink; } /** * Gets the attachment associated with this blog entry. * * @return an Attachment instance, or null if one doesn't exist */ public Attachment getAttachment() { return attachment; } /** * Sets the attachment associated with thie blog entry. * * @param newAttachment an Attachment instance */ public void setAttachment(Attachment newAttachment) { propertyChangeSupport.firePropertyChange(ATTACHMENT_PROPERTY, attachment, newAttachment); this.attachment = newAttachment; } /** * Determines whether comments are enabled for this blog entry. * * @return true if comments are enabled, false otherwise */ public boolean isCommentsEnabled() { return this.commentsEnabled; } /** * Sets whether comments are enabled for this blog entry. * * @param newCommentsEnabled true if comments should be enabled, * false otherwise */ public void setCommentsEnabled(boolean newCommentsEnabled) { propertyChangeSupport.firePropertyChange(COMMENTS_ENABLED_PROPERTY, commentsEnabled, newCommentsEnabled); this.commentsEnabled = newCommentsEnabled; } /** * Gets a link to the comments for this blog entry. * * @return an absolute URL as a String */ public String getCommentsLink() { return getLocalPermalink() + "#comments"; } /** * Determines whether TrackBacks are enabled for this blog entry. * * @return true if TrackBacks are enabled, false otherwise */ public boolean isTrackBacksEnabled() { return this.trackBacksEnabled; } /** * Sets whether TrackBacks are enabled for this blog entry. * * @param newTrackBacksEnabled true if TrackBacks should be enabled, * false otherwise */ public void setTrackBacksEnabled(boolean newTrackBacksEnabled) { propertyChangeSupport.firePropertyChange(TRACKBACKS_ENABLED_PROPERTY, trackBacksEnabled, newTrackBacksEnabled); this.trackBacksEnabled = newTrackBacksEnabled; } /** * Gets a link to the trackbacks for this blog entry. * * @return an absolute URL as a String */ public String getTrackBacksLink() { return getLocalPermalink() + "#trackbacks"; } /** * Gets the link that blogs can send TrackBacks too. */ public String getTrackBackLink() { StringBuffer link = new StringBuffer(); link.append(getBlog().getUrl()); link.append("addTrackBack.action?entry="); link.append(getId()); link.append("&token="); link.append(TrackBackTokenManager.getInstance().generateToken());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -