1 25 26 package org.snipsnap.snip; 27 28 import org.snipsnap.app.Application; 29 import org.snipsnap.date.Month; 30 import org.snipsnap.semanticweb.rss.Rssify; 31 import org.snipsnap.user.Permissions; 32 import org.snipsnap.user.Roles; 33 import org.snipsnap.xmlrpc.WeblogsPing; 34 35 import java.sql.Date ; 36 import java.util.Calendar ; 37 import java.util.GregorianCalendar ; 38 import java.util.List ; 39 40 46 47 public class BlogImpl implements Blog { 48 private String startName; 49 private String name; 50 private Snip blog; 51 private SnipSpace space; 52 53 public BlogImpl(SnipSpace space, String blogName) { 54 this.space = space; 55 this.startName = Application.get().getConfiguration().getStartSnip(); 57 if (blogName == null || "".equals(blogName)) { 58 blogName = startName; 59 } 60 this.name = blogName; 61 this.blog = space.load(name); 62 } 63 64 public String getName() { 65 return this.name; 66 } 67 68 public Snip post(String content, String title) { 69 return post(BlogKit.getContent(title, content)); 70 } 71 72 public Snip post(String content) { 73 Date date = new Date (new java.util.Date ().getTime()); 74 return post(content, date); 75 } 76 77 public Snip post(String content, Date date) { 78 String snipName = name + "/" + SnipUtil.toName(date); 79 Snip snip = null; 80 81 int max = findMaxPost(snipName); 84 if (true) { snip = snip = space.create(snipName + "/" + (max + 1), content); 91 } else { 92 if (max != 0) { 94 snipName = snipName + "/" + max; 95 } 96 if (space.exists(snipName)) { 97 snip = space.load(snipName); 98 snip.setContent(content + "\n\n" + snip.getContent()); 99 } else { 100 snip = space.create(snipName, content); 101 } 102 } 103 104 snip.addPermission(Permissions.EDIT_SNIP, Roles.OWNER); 106 space.systemStore(snip); 107 108 WeblogsPing.ping(blog); 110 return snip; 111 } 112 113 private int findMaxPost(String snipName) { 114 Snip[] existing = space.match(snipName + "/"); 115 int max = 0; 116 for (int i = 0; i < existing.length; i++) { 118 Snip post = existing[i]; 119 String name = post.getName(); 120 int index = name.lastIndexOf('/'); 121 if (index != -1) { 123 try { 124 max = Math.max(Integer.parseInt(name.substring(index + 1)), max); 126 } catch (NumberFormatException e) { 128 } 130 } 131 } 132 return max; 133 } 134 135 public List getFlatPosts() { 136 return Rssify.rssify(getPosts(10)); 137 } 138 139 public List getPosts(int count) { 140 Calendar endC = new GregorianCalendar (); 141 endC.setTime(new java.util.Date ()); 142 endC.add(Calendar.DAY_OF_MONTH, 1); 143 Calendar startC = new GregorianCalendar (); 144 startC.setTimeInMillis(blog.getCTime().getTime()); 145 146 List posts = space.getByDate(blog.getName(), Month.toKey(startC), Month.toKey(endC)); 147 return posts.subList(0, Math.min(posts.size(), count)); 148 } 149 150 public Snip getSnip() { 151 return blog; 152 } 153 } 154 | Popular Tags |