KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > Comments


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
34 import java.text.MessageFormat JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Set JavaDoc;
39
40 /**
41  * Handler for comments added to snips.
42  *
43  * @author Stephan J. Schmidt
44  * @version $Id: Comments.java 1830 2005-08-08 09:57:05Z leo $
45  */

46 public class Comments {
47   private Snip snip;
48   private List JavaDoc comments;
49   private SnipSpace space;
50   private Set JavaDoc users;
51
52   public Comments(Snip snip) {
53     this.snip = snip;
54     space = SnipSpaceFactory.getInstance();
55   }
56
57   /**
58    * Lazy initialization of containers
59    * (comments, users)
60    */

61   private void init() {
62     if (null == comments) {
63       comments = SnipSpaceFactory.getInstance().getComments(snip);
64     }
65
66     if (null == users) {
67       users = new HashSet JavaDoc();
68       Iterator JavaDoc iterator = comments.iterator();
69       while (iterator.hasNext()) {
70         Snip snip = (Snip) iterator.next();
71         users.add(snip.getCUser());
72       }
73     }
74   }
75
76   /**
77    * Get list of all comments for the snip
78    *
79    * @return List of comments (snips)
80    */

81   public List JavaDoc getComments() {
82     init();
83     return comments;
84   }
85
86   /**
87    * Post a new comment to snip
88    *
89    * @param content Content of the comment
90    * @return The generated comment (snip)
91    */

92   public Snip postComment(String JavaDoc content) {
93     init();
94     String JavaDoc 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   /**
105    * Returns a pretty printed version of the comments
106    * for the snip.
107    * (usernames, count)
108    *
109    * @return Pretty printed version of comments
110    */

111   public String JavaDoc getCommentString() {
112     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
113     if (getCount() > 0) {
114       MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "comments.count"));
115       // @TODO do not link to comments if snip is a comment, but link to parent comments object
116
SnipLink.appendLinkWithRoot(buffer, SnipLink.getCommentsRoot(),
117                                   SnipLink.encode(snip.getName()),
118                                   mf.format(new Object JavaDoc[]{new Integer JavaDoc(getCount())}));
119       buffer.append(" ");
120       MessageFormat JavaDoc mfBy = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "comments.by"));
121       buffer.append(mfBy.format(new Object JavaDoc[]{getUserString()}));
122     } else {
123       buffer.append(ResourceManager.getString("i18n.messages", "comments.none"));
124     }
125
126     return buffer.toString();
127   }
128
129   public String JavaDoc getPostUrl() throws IOException JavaDoc {
130     Configuration config = Application.get().getConfiguration();
131     return SnipLink.getCommentsRoot() + "/" + snip.getNameEncoded() + "#post";
132   }
133
134   public String JavaDoc getPostString() {
135     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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   /**
144    * Append user list "funzel, arte, warg" to
145    * buffer.
146    */

147   public String JavaDoc getUserString() {
148     init();
149     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
150     Iterator JavaDoc userIterator = users.iterator();
151     while (userIterator.hasNext()) {
152       String JavaDoc s = (String JavaDoc) userIterator.next();
153       SnipLink.appendLink(buffer, s);
154       if (userIterator.hasNext()) {
155         buffer.append(", ");
156       }
157     }
158     return buffer.toString();
159   }
160
161   /**
162    * Get number of comments
163    *
164    * @return Number of comments
165    */

166   public int getCount() {
167     init();
168     return comments.size();
169   }
170
171   public String JavaDoc toString() {
172     return getCommentString();
173   }
174 }
175
Popular Tags