001: /*
002: * Created on Oct 18, 2006
003: */
004: package org.openedit.blog.archive;
005:
006: import java.io.File;
007: import java.io.IOException;
008: import java.io.Reader;
009: import java.text.ParseException;
010: import java.util.Date;
011: import java.util.Iterator;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015: import org.dom4j.DocumentHelper;
016: import org.dom4j.Element;
017: import org.openedit.repository.filesystem.FileItem;
018:
019: import com.openedit.OpenEditException;
020: import com.openedit.blog.Blog;
021: import com.openedit.blog.BlogEntry;
022: import com.openedit.blog.Comment;
023: import com.openedit.page.Page;
024: import com.openedit.users.User;
025: import com.openedit.users.UserManager;
026: import com.openedit.util.FileUtils;
027: import com.openedit.util.PathUtilities;
028:
029: public class CommentArchive extends BaseArchive {
030: private static final Log log = LogFactory
031: .getLog(CommentArchive.class);
032:
033: protected Comment loadComment(BlogEntry inEntry,
034: Element inCommentElement) throws ParseException {
035: Comment comment = new Comment();
036: comment.setAuthor(inCommentElement.attributeValue("author"));
037:
038: String username = inCommentElement.attributeValue("username");
039: if (username != null) {
040: User user = getUserManager().getUser(username);
041: comment.setUser(user);
042: }
043:
044: String id = inCommentElement.attributeValue("id");
045: comment.setId(id);
046: comment.setContent(inCommentElement.getTextTrim());
047: Date date = inEntry.parse(inCommentElement
048: .attributeValue("date"));
049: comment.setDateTime(date);
050: String visible = inCommentElement.attributeValue("visible");
051: if (visible == null) {
052: visible = "true";
053: }
054: comment.setVisible(Boolean.parseBoolean(visible));
055: return comment;
056: }
057:
058: public void loadComments(BlogEntry inEntry)
059: throws OpenEditException {
060: Page page = commentsPage(inEntry);
061:
062: if (page.exists()) {
063: log.debug("Loading comments for entry " + inEntry.getId()
064: + " from " + page.getPath());
065: Reader reader = page.getReader();
066: try {
067: Element root = getXmlUtil().getXml(reader,
068: page.getCharacterEncoding());
069: for (Iterator iter = root.elementIterator("comment"); iter
070: .hasNext();) {
071: Element element = (Element) iter.next();
072: Comment comment = loadComment(inEntry, element);
073: inEntry.addComment(comment);
074: }
075: } catch (Exception ex) {
076: throw new OpenEditException(ex);
077: } finally {
078: FileUtils.safeClose(reader);
079: }
080: }
081: }
082:
083: public void setUserManager(UserManager inUserManager) {
084: fieldUserManager = inUserManager;
085: }
086:
087: protected Page commentsPage(BlogEntry inEntry)
088: throws OpenEditException {
089: String dir = PathUtilities.extractPagePath(inEntry.getPath());
090: String path = dir + "-comments.xml";
091:
092: Page page = getPageManager().getPage(path);
093: return page;
094: }
095:
096: public void saveComments(Blog inBlog, BlogEntry inEntry)
097: throws OpenEditException {
098: Page page = commentsPage(inEntry);
099: Element root = DocumentHelper.createDocument().addElement(
100: "comments");
101: for (Iterator iter = inEntry.getComments().iterator(); iter
102: .hasNext();) {
103: Comment com = (Comment) iter.next(); // <comment author="admin" date="Feb 18, 2005 2:42:29 PM">This is a snide remark</comment>
104:
105: Element comment = root.addElement("comment");
106: comment.addAttribute("author", com.getAuthor());
107: if (com.getUser() != null) {
108: comment.addAttribute("username", com.getUser()
109: .getUserName());
110: }
111:
112: comment.addAttribute("date", inEntry.getGmtStandard()
113: .format(com.getDateTime()));
114: comment.addAttribute("id", com.getId());
115: comment.addAttribute("visible", String.valueOf(com
116: .isVisible()));
117: comment.setText(com.getContent());
118: }
119: try {
120: //TODO: Add locking
121: File tmp = File.createTempFile("blog", "junk");
122: getXmlUtil().saveXml(root.getDocument(), tmp);
123: FileItem item = new FileItem();
124: item.setFile(tmp);
125: item.setPath(page.getPath());
126: Page tmpPage = new Page(page);
127: tmpPage.setContentItem(item);
128:
129: getPageManager().copyPage(tmpPage, page); //Copy over as a tmp file in case there is a problem
130: tmp.delete();
131: } catch (IOException ex) {
132: throw new OpenEditException(ex);
133: }
134: }
135:
136: }
|