1 4 package org.openedit.blog.archive; 5 6 import java.io.StringWriter ; 7 import java.io.Writer ; 8 import java.text.SimpleDateFormat ; 9 import java.util.ArrayList ; 10 import java.util.Date ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.openedit.links.LinkTree; 15 import org.openedit.links.XmlLinkLoader; 16 import org.openedit.repository.ContentItem; 17 import org.openedit.repository.filesystem.StringItem; 18 19 import com.openedit.OpenEditException; 20 import com.openedit.blog.Blog; 21 import com.openedit.blog.BlogEntry; 22 import com.openedit.config.Configuration; 23 import com.openedit.page.Page; 24 import com.openedit.users.User; 25 import com.openedit.util.FileUtils; 26 import com.sun.syndication.feed.synd.SyndCategoryImpl; 27 import com.sun.syndication.feed.synd.SyndContentImpl; 28 29 public class EntryArchive extends BaseArchive 30 { 31 protected CommentArchive fieldCommentArchive; 32 33 public BlogEntry createEntry(Blog inBlog, Page inPath) throws OpenEditException 34 { 35 BlogEntry entry = new BlogEntry(); 40 entry.setCommentArchive(getCommentArchive()); 41 entry.setPath(inPath.getPath()); 42 entry.setLink(inBlog.getHostName() + inPath); 43 44 entry.setPageManager(getPageManager()); 45 entry.setLinkTree(inBlog.getLinkTree()); 46 47 Configuration config = inPath.getPageSettings().getUserDefined("blog"); 48 if ( config != null) 49 { 50 entry.setTitle(config.getAttribute("title")); 51 entry.setAuthor(config.getAttribute("author")); 52 53 String username = config.getAttribute("username"); 54 if( username != null) 55 { 56 User user = getUserManager().getUser(username); 57 entry.setUser(user); 58 } 59 String pubdate = config.getChildValue("publishdate"); 60 String visible = config.getChildValue("visible"); 61 entry.setVisible(visible == null || visible.equals("true")); 62 entry.setPublishedDate( entry.parse(pubdate)); 63 List categories = new ArrayList (); 64 for (Iterator iter = config.getChildren("category").iterator(); iter.hasNext();) 65 { 66 Configuration element = (Configuration) iter.next(); 67 SyndCategoryImpl cat = new SyndCategoryImpl(); 68 cat.setName(element.getValue()); 69 categories.add(cat); 70 } 71 entry.setCategories(categories); 72 entry.setDescription(inPath.getContent()); 73 } 74 else 75 { 76 entry.setTitle(inPath.get("title")); 77 entry.setPublishedDate( inPath.getLastModified()); 78 ContentItem item = getPageManager().getLatestVersion(entry.getPath()); 79 if( item != null) 80 { 81 entry.setAuthor(item.getAuthor()); 82 User user = getUserManager().getUser(item.getAuthor()); 83 entry.setUser(user); 84 } 85 } 86 return entry; 87 } 88 91 public void saveLinks(Blog inBlog) throws OpenEditException 92 { 93 94 String slink = inBlog.getBlogHome() + "/permalinks.xml"; 95 96 Page linkpage = getPageManager().getPage(slink); 97 Writer out = new StringWriter (); 98 try 99 { 100 XmlLinkLoader loader = new XmlLinkLoader(); 101 loader.saveLinks(inBlog.getLinkTree(), out, linkpage.getCharacterEncoding()); 102 } 103 finally 104 { 105 FileUtils.safeClose(out); 106 } 107 StringItem item = new StringItem(linkpage.getPath(), out.toString(), linkpage.getCharacterEncoding()); 108 linkpage.setContentItem(item); 109 getPageManager().putPage(linkpage); 110 111 } 112 113 116 public void loadLinks(Blog inBlog) throws OpenEditException 117 { 118 String slink = inBlog.getBlogHome() + "/permalinks.xml"; 119 Page linkpage = getPageManager().getPage(slink ); 120 if ( !linkpage.exists()) 121 { 122 slink = inBlog.getBlogHome() + "/links.xml"; 123 linkpage = getPageManager().getPage(slink ); 124 if ( !linkpage.exists()) 125 { 126 throw new OpenEditException("could not find " + slink); 127 } 128 } 129 130 XmlLinkLoader loader = new XmlLinkLoader(); 131 LinkTree tree = loader.loadLinks(linkpage); 132 inBlog.setLinkTree(tree); 133 } 134 public CommentArchive getCommentArchive() 135 { 136 return fieldCommentArchive; 137 } 138 public void setCommentArchive(CommentArchive inCommentArchive) 139 { 140 fieldCommentArchive = inCommentArchive; 141 } 142 public BlogEntry createEntry(Blog inBlog, User inUser) 143 { 144 BlogEntry entry = new BlogEntry(); 145 entry.setCommentArchive(getCommentArchive()); 147 Date now = new Date (); 148 entry.setPublishedDate(now); 149 150 SimpleDateFormat format = new SimpleDateFormat ("yyyy/M/d/HHmmss"); 151 String path = inBlog.getArchiveRootDirectory() + "/" + format.format(now) + ".html"; 152 153 entry.setPath( path); 154 155 entry.setLink(inBlog.getHostName() + inBlog.getBlogHome() + path); 156 157 entry.setLinkTree(inBlog.getLinkTree()); 158 entry.setPageManager(getPageManager()); 159 SyndContentImpl content = new SyndContentImpl(); 160 content.setType("text/html"); 161 entry.setDescription(content); 162 163 entry.setUser(inUser); 164 return entry; 165 } 166 167 } 168 | Popular Tags |