001: /*
002: * Created on Feb 18, 2005
003: */
004: package com.openedit.blog;
005:
006: import java.io.Serializable;
007: import java.text.ParseException;
008: import java.text.SimpleDateFormat;
009: import java.util.ArrayList;
010: import java.util.Date;
011: import java.util.HashMap;
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.Map;
015:
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018: import org.openedit.blog.archive.CommentArchive;
019: import org.openedit.links.LinkTree;
020:
021: import com.openedit.OpenEditException;
022: import com.openedit.OpenEditRuntimeException;
023: import com.openedit.page.manage.PageManager;
024: import com.openedit.users.User;
025: import com.sun.syndication.feed.synd.SyndContentImpl;
026: import com.sun.syndication.feed.synd.SyndEntryImpl;
027:
028: /**
029: * @author cburkey
030: *
031: */
032: public class BlogEntry extends SyndEntryImpl implements Serializable,
033: Comparable {
034: protected String fieldPath;
035: protected List fieldComments;
036: transient protected PageManager fieldPageManager;
037: protected LinkTree fieldLinkTree;
038: protected boolean fieldVisible = false;
039: protected static SimpleDateFormat fieldGmtStandard;
040: protected User fieldUser;
041: protected List fieldNotify;
042: protected CommentArchive fieldCommentArchive;
043: private static final Log log = LogFactory.getLog(BlogEntry.class);
044: protected Map fieldProperties;
045:
046: public BlogEntry() {
047: }
048:
049: public String getPath() {
050: return fieldPath;
051: }
052:
053: /**
054: * A path is just the end part of a link
055: * @param inPath
056: */
057:
058: public void setPath(String inPath) {
059: fieldPath = inPath;
060: }
061:
062: public String published(String inFormat) {
063: SimpleDateFormat format = new SimpleDateFormat(inFormat);
064: return format.format(getPublishedDate());
065: }
066:
067: public String getId() {
068: return getPath();
069: }
070:
071: /**
072: * Returns the total number of comments on this blog entry, both visible
073: * (published) and invisible (unpublished).
074: *
075: * @return The number of comments
076: *
077: * @throws OpenEditException
078: * If the comments needed to be loaded and could not be
079: */
080: public int countComments() throws OpenEditException {
081: return getComments().size();
082: }
083:
084: /**
085: * Returns the number of visible (published) comments on this blog entry.
086: *
087: * @return The number of visible comments
088: *
089: * @throws OpenEditException
090: * If the comments needed to be loaded and could not be
091: */
092: public int countVisibleComments() throws OpenEditException {
093: return getVisibleComments().size();
094: }
095:
096: /**
097: * Returns all the comments on this blog, published or unpublished.
098: *
099: * @return A {@link List} of {@link Comment}s
100: *
101: * @throws OpenEditException
102: * If the comments needed to be loaded and could not be
103: */
104: public List getComments() throws OpenEditException {
105: if (fieldComments == null) {
106: fieldComments = new ArrayList();
107: getCommentArchive().loadComments(this );
108: }
109: return fieldComments;
110: }
111:
112: /**
113: * Returns all comments on this blog entry that are visible (published).
114: *
115: * @return A {@link List} of {@link Comment}s
116: *
117: * @throws OpenEditException
118: * If the comments needed to be loaded and could not be
119: */
120: public List getVisibleComments() throws OpenEditException {
121: List comments = getComments();
122: List visibleComments = new ArrayList();
123: for (Iterator iter = comments.iterator(); iter.hasNext();) {
124: Comment comment = (Comment) iter.next();
125: if (comment.isVisible()) {
126: visibleComments.add(comment);
127: }
128: }
129: return visibleComments;
130: }
131:
132: public List getAllowedComments(User inUser)
133: throws OpenEditException {
134: List comments = getComments();
135: List allowedComments = new ArrayList();
136: for (Iterator iter = comments.iterator(); iter.hasNext();) {
137: Comment comment = (Comment) iter.next();
138: if (comment.isVisible() || comment.canEdit(inUser)) {
139: allowedComments.add(comment);
140: }
141: }
142: return allowedComments;
143: }
144:
145: public PageManager getPageManager() {
146: return fieldPageManager;
147: }
148:
149: public void setPageManager(PageManager inPageManager) {
150: fieldPageManager = inPageManager;
151: }
152:
153: public LinkTree getLinkTree() {
154: return fieldLinkTree;
155: }
156:
157: public void setLinkTree(LinkTree inLinkTree) {
158: fieldLinkTree = inLinkTree;
159: }
160:
161: /**
162: * Adds a comment to this blog entry. The comment's data, including the
163: * ID, must already be populated.
164: *
165: * @param inComment The new comment
166: */
167: public void addComment(Comment inComment) throws OpenEditException {
168: // TODO: check for dups
169: getComments().add(inComment);
170: }
171:
172: /**
173: * Returns the comment with the given comment ID.
174: *
175: * @param inId The comment ID
176: *
177: * @return The comment, or <code>null</code> if this entry does not have a
178: * comment with the given ID
179: */
180: public Comment getComment(String inId) throws OpenEditException {
181: for (Iterator iter = getComments().iterator(); iter.hasNext();) {
182: Comment comment = (Comment) iter.next();
183: if (comment.getId().equals(inId)) {
184: return comment;
185: }
186: }
187: return null;
188: }
189:
190: public void removeComment(String inId) throws OpenEditException {
191: Comment comment = getComment(inId);
192: removeComment(comment);
193: }
194:
195: public void removeComment(Comment inComment)
196: throws OpenEditException {
197: if (inComment != null) {
198: getComments().remove(inComment);
199: }
200: }
201:
202: /**
203: * @param inContent
204: */
205: public void setDescription(String inContent) {
206: SyndContentImpl content = new SyndContentImpl();
207: content.setType("text/html");
208: content.setValue(inContent);
209: setDescription(content);
210: }
211:
212: public boolean isVisible() {
213: return fieldVisible;
214: }
215:
216: public void setVisible(boolean invisible) {
217: this .fieldVisible = invisible;
218: }
219:
220: public SimpleDateFormat getGmtStandard() {
221: if (fieldGmtStandard == null) {
222: fieldGmtStandard = new SimpleDateFormat(
223: "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
224: }
225: return fieldGmtStandard;
226: }
227:
228: public Date parse(String inDate) {
229: try {
230: if (inDate.startsWith("2")) {
231: return getGmtStandard().parse(inDate);
232: } else {
233: return SimpleDateFormat.getDateTimeInstance().parse(
234: inDate);
235: }
236: } catch (ParseException ex) {
237: throw new OpenEditRuntimeException(ex);
238: }
239: }
240:
241: public void setGmtStandard(SimpleDateFormat inGmtStandard) {
242: fieldGmtStandard = inGmtStandard;
243: }
244:
245: public User getUser() {
246: return fieldUser;
247: }
248:
249: public void setUser(User inUser) {
250: fieldUser = inUser;
251: }
252:
253: public List getNotify() {
254: return fieldNotify;
255: }
256:
257: public void setNotify(List inNotify) {
258: fieldNotify = inNotify;
259: }
260:
261: public CommentArchive getCommentArchive() {
262: return fieldCommentArchive;
263: }
264:
265: public void setCommentArchive(CommentArchive inCommentArchive) {
266: fieldCommentArchive = inCommentArchive;
267: }
268:
269: public int compareTo(Object inEntry) {
270: BlogEntry in = (BlogEntry) inEntry;
271: return getPublishedDate().compareTo(in.getPublishedDate());
272: }
273:
274: public Map getProperties() {
275: if (fieldProperties == null) {
276: fieldProperties = new HashMap();
277:
278: }
279:
280: return fieldProperties;
281: }
282:
283: public void setProperties(Map fieldProperties) {
284: this .fieldProperties = fieldProperties;
285: }
286:
287: public void addProperty(String key, String value) {
288: getProperties().put(key, value);
289: }
290:
291: public String getProperty(String key) {
292: return (String) getProperties().get(key);
293: }
294:
295: }
|