001: /*
002: * Created on Feb 21, 2006
003: */
004: package org.openedit.blog.archive;
005:
006: import java.io.IOException;
007: import java.io.Reader;
008: import java.io.Writer;
009: import java.util.Date;
010: import java.util.Iterator;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.dom4j.Document;
015: import org.dom4j.DocumentHelper;
016: import org.dom4j.Element;
017: import org.openedit.links.Link;
018: import org.openedit.repository.filesystem.StringItem;
019:
020: import com.openedit.OpenEditException;
021: import com.openedit.blog.Blog;
022: import com.openedit.blog.BlogEntry;
023: import com.openedit.config.XMLConfiguration;
024: import com.openedit.page.Page;
025:
026: public class BlogArchive extends BaseArchive {
027: protected EntryArchive fieldEntryArchive;
028:
029: private static final Log log = LogFactory.getLog(BlogArchive.class);
030:
031: public boolean hasChanged(Blog inBlog) throws OpenEditException {
032: Page settings = getPageManager().getPage(
033: inBlog.getBlogHome() + "/blogsettings.xml");
034: Date date = settings.getLastModified();
035:
036: boolean same = inBlog.getLastModified().equals(date);
037: return !same;
038:
039: }
040:
041: public void loadBlog(Blog inBlog) throws OpenEditException {
042: Page settings = getPageManager().getPage(
043: inBlog.getBlogHome() + "/blogsettings.xml");
044: //read in some XML
045: Reader read = settings.getReader();
046:
047: inBlog.setPageManager(getPageManager());
048: inBlog.setUserManager(getUserManager());
049: inBlog.setEntryArchive(getEntryArchive());
050: Element root = null;
051: root = getXmlUtil().getXml(read, "UTF-8");
052:
053: inBlog.setTitle(root.elementText("title"));
054: inBlog.setHostName(root.elementText("hostname"));
055: inBlog.setAuthor(root.elementText("author"));
056: inBlog.setDescription(root.elementText("description"));
057:
058: inBlog.setAllowAnonymous("true".equals(root
059: .elementText("allow-anonymous-comments")));
060:
061: String dir = root.elementText("archive-root-directory");
062: if (dir != null) {
063: if (dir.endsWith("/")) {
064: dir = dir.substring(0, dir.length() - 1);
065: }
066: inBlog.setArchiveRootDirectory(dir);
067: } else {
068: inBlog.setArchiveRootDirectory(inBlog.getBlogHome()
069: + "/permalink");
070: }
071:
072: //inBlog.setBlogRoot(root.elementText("blogroot"));
073: String autoPublishCommentsStr = root
074: .elementText("auto-publish-comments");
075: if (autoPublishCommentsStr == null) {
076: inBlog.setAutoPublishingComments(true);
077: } else {
078: inBlog.setAutoPublishingComments(Boolean
079: .parseBoolean(autoPublishCommentsStr));
080: }
081: inBlog.setAutoPublishEntries(Boolean.parseBoolean(root
082: .elementText("auto-publish-entries")));
083: inBlog.setUseNotification(Boolean.parseBoolean(root
084: .elementText("use-notification")));
085:
086: // List questions = new ArrayList();
087: // for (Iterator iter = root.elementIterator("question"); iter.hasNext();)
088: // {
089: // Element q = (Element) iter.next();
090: // Question ques = new Question();
091: // ques.setId(q.attributeValue("id"));
092: // ques.setDescription(q.attributeValue("description"));
093: // ques.setAnswer(q.attributeValue("answer"));
094: // questions.add(ques);
095: // }
096: // inBlog.setQuestions(questions);
097: inBlog.setLastModified(settings.getLastModified());
098: getEntryArchive().loadLinks(inBlog);
099: }
100:
101: public void saveBlog(Blog inBlog, Writer inWriter, String inEncoding)
102: throws IOException {
103: Element root = DocumentHelper.createElement("blog");
104: root.addElement("title").setText(inBlog.getTitle());
105: root.addElement("hostname").setText(inBlog.getHostName());
106: root.addElement("author").setText(inBlog.getAuthor());
107: root.addElement("description").setText(inBlog.getDescription());
108: //root.addElement("blogroot").setText(inBlog.getBlogRoot());
109: root.addElement("allow-anonymous-comments").setText(
110: String.valueOf(inBlog.getAllowAnonymous()));
111: root.addElement("auto-publish-comments").setText(
112: String.valueOf(inBlog.isAutoPublishingComments()));
113:
114: //auto-publish-entries
115: //use-notification
116: root.addElement("archive-root-directory").setText(
117: inBlog.getArchiveRootDirectory());
118:
119: Document doc = DocumentHelper.createDocument(root);
120:
121: getXmlUtil().saveXml(doc, inWriter, inEncoding);
122: }
123:
124: public void saveEntry(Blog inBlog, BlogEntry inEntry)
125: throws OpenEditException {
126: Page page = getPageManager().getPage(inEntry.getPath());
127: String content = "";
128: if (inEntry.getDescription() != null) {
129: content = inEntry.getDescription().getValue();
130: }
131: StringItem item = new StringItem(inEntry.getPath(), content,
132: "UTF-8");
133: page.setContentItem(item);
134: getPageManager().putPage(page);
135:
136: //save the xconf
137: XMLConfiguration pageconfig = new XMLConfiguration("page");
138: XMLConfiguration blog = (XMLConfiguration) pageconfig
139: .addChild("blog");
140: blog.setAttribute("author", inEntry.getAuthor());
141: blog.setAttribute("username", inEntry.getUser().getUserName());
142:
143: blog.setAttribute("title", inEntry.getTitle());
144: blog.addChild("publishdate").setValue(
145: inEntry.getGmtStandard().format(
146: inEntry.getPublishedDate()));
147: blog.addChild("visible").setValue(
148: String.valueOf(inEntry.isVisible()));
149:
150: for (Iterator iter = inEntry.getProperties().keySet()
151: .iterator(); iter.hasNext();) {
152: String key = (String) iter.next();
153: String value = inEntry.getProperty(key);
154: if (key != null && value != null) {
155: XMLConfiguration c = (XMLConfiguration) blog
156: .addChild("property");
157: c.setAttribute("id", key);
158: c.setValue(value);
159: }
160: }
161:
162: StringItem xconf = new StringItem(page.getPageSettings()
163: .getXConf().getPath(), pageconfig.toXml("UTF-8"),
164: "UTF-8");
165: getPageManager().getPageSettingsManager().saveSetting(xconf);
166: getPageManager().clearCache(page);
167:
168: Link existing = inEntry.getLinkTree().getLink(inEntry.getId());
169: if (existing == null) {
170: //add it to the link.xml file
171: Link link = new Link();
172: link.setId(inEntry.getId());
173: link.setPath(inEntry.getPath());
174: //getLinkTree().removeLink(link);
175: inEntry.getLinkTree().insertLink("index", link);
176: }
177: getEntryArchive().saveLinks(inBlog);
178:
179: }
180:
181: public EntryArchive getEntryArchive() {
182: return fieldEntryArchive;
183: }
184:
185: public void setEntryArchive(EntryArchive inEntryArchive) {
186: fieldEntryArchive = inEntryArchive;
187: }
188:
189: }
|