1 25 package org.snipsnap.snip; 26 27 import org.radeox.util.i18n.ResourceManager; 28 import org.snipsnap.app.Application; 29 import org.snipsnap.config.Configuration; 30 import org.snipsnap.user.Permissions; 31 import org.snipsnap.user.Roles; 32 33 import java.io.IOException ; 34 import java.text.MessageFormat ; 35 import java.util.HashSet ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Set ; 39 40 46 public class Comments { 47 private Snip snip; 48 private List comments; 49 private SnipSpace space; 50 private Set users; 51 52 public Comments(Snip snip) { 53 this.snip = snip; 54 space = SnipSpaceFactory.getInstance(); 55 } 56 57 61 private void init() { 62 if (null == comments) { 63 comments = SnipSpaceFactory.getInstance().getComments(snip); 64 } 65 66 if (null == users) { 67 users = new HashSet (); 68 Iterator iterator = comments.iterator(); 69 while (iterator.hasNext()) { 70 Snip snip = (Snip) iterator.next(); 71 users.add(snip.getCUser()); 72 } 73 } 74 } 75 76 81 public List getComments() { 82 init(); 83 return comments; 84 } 85 86 92 public Snip postComment(String content) { 93 init(); 94 String name = "comment-" + snip.getName() + "-" + (getCount() + 1); 95 Snip comment = space.create(name, content); 96 comment.setCommentedSnip(this.snip); 97 comment.addPermission(Permissions.EDIT_SNIP, Roles.OWNER); 98 space.store(comment); 99 comments.add(comment); 100 users.add(comment.getCUser()); 101 return comment; 102 } 103 104 111 public String getCommentString() { 112 StringBuffer buffer = new StringBuffer (); 113 if (getCount() > 0) { 114 MessageFormat mf = new MessageFormat (ResourceManager.getString("i18n.messages", "comments.count")); 115 SnipLink.appendLinkWithRoot(buffer, SnipLink.getCommentsRoot(), 117 SnipLink.encode(snip.getName()), 118 mf.format(new Object []{new Integer (getCount())})); 119 buffer.append(" "); 120 MessageFormat mfBy = new MessageFormat (ResourceManager.getString("i18n.messages", "comments.by")); 121 buffer.append(mfBy.format(new Object []{getUserString()})); 122 } else { 123 buffer.append(ResourceManager.getString("i18n.messages", "comments.none")); 124 } 125 126 return buffer.toString(); 127 } 128 129 public String getPostUrl() throws IOException { 130 Configuration config = Application.get().getConfiguration(); 131 return SnipLink.getCommentsRoot() + "/" + snip.getNameEncoded() + "#post"; 132 } 133 134 public String getPostString() { 135 StringBuffer buffer = new StringBuffer (); 136 SnipLink.appendLinkWithRoot(buffer, 137 SnipLink.getCommentsRoot(), 138 snip.getNameEncoded() + "#post", 139 ResourceManager.getString("i18n.messages", "comments.post")); 140 return buffer.toString(); 141 } 142 143 147 public String getUserString() { 148 init(); 149 StringBuffer buffer = new StringBuffer (); 150 Iterator userIterator = users.iterator(); 151 while (userIterator.hasNext()) { 152 String s = (String ) userIterator.next(); 153 SnipLink.appendLink(buffer, s); 154 if (userIterator.hasNext()) { 155 buffer.append(", "); 156 } 157 } 158 return buffer.toString(); 159 } 160 161 166 public int getCount() { 167 init(); 168 return comments.size(); 169 } 170 171 public String toString() { 172 return getCommentString(); 173 } 174 } 175 | Popular Tags |