KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > blog > modules > BlogModule


1 /*
2  * Created on Feb 18, 2005
3  */

4 package org.openedit.blog.modules;
5
6 import java.io.StringWriter JavaDoc;
7 import java.io.Writer JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.openedit.blog.archive.BlogArchive;
15 import org.openedit.repository.filesystem.StringItem;
16
17 import com.openedit.OpenEditException;
18 import com.openedit.WebPageRequest;
19 import com.openedit.blog.Blog;
20 import com.openedit.blog.BlogCommentNotification;
21 import com.openedit.blog.BlogEntry;
22 import com.openedit.blog.Comment;
23 import com.openedit.blog.Question;
24 import com.openedit.modules.BaseModule;
25 import com.openedit.modules.html.EditorSession;
26 import com.openedit.page.Page;
27 import com.openedit.users.User;
28 import com.openedit.users.filesystem.FileSystemUser;
29 import com.sun.syndication.feed.synd.SyndFeed;
30 import com.sun.syndication.feed.synd.SyndFeedImpl;
31 import com.sun.syndication.io.SyndFeedOutput;
32
33 /**
34  * @author cburkey
35  *
36  */

37 public class BlogModule extends BaseModule
38 {
39     protected Map JavaDoc fieldBlogs;
40     private static final Log log = LogFactory.getLog(BlogModule.class);
41     protected BlogCommentNotification fieldCommentNotification;
42     
43     public Blog getBlog(WebPageRequest req ) throws OpenEditException
44     {
45         Page inPath = req.getPage();
46         String JavaDoc home = inPath.get("bloghome");
47
48         if( home == null)
49         {
50             Blog blog = (Blog)req.getPageValue("blog"); //already loaded once
51
if( blog != null)
52             {
53                 return blog;
54             }
55             throw new OpenEditException("Need to define bloghome page property");
56         }
57         Blog blog = getBlog(home);
58         req.putPageValue("blog",blog);
59         req.putPageValue("bloghome",blog.getBlogHome());
60         return blog;
61     }
62
63     public Blog getBlog(String JavaDoc home) throws OpenEditException
64     {
65         if( home.endsWith("/"))
66         {
67             home = home.substring(0,home.length()-1);
68         }
69         Blog blog = (Blog)getBlogs().get(home);
70         boolean changed = false;
71         if ( blog == null)
72         {
73             changed = true;
74         }
75         else
76         {
77             //check the time stamp on the link file
78
changed = blog.hasChanged();
79         }
80         
81         if (changed)
82         {
83             blog = new Blog();
84             blog.setBlogHome(home);
85             BlogArchive archive = getArchive(home + "/index.html");
86             archive.loadBlog(blog);
87             getBlogs().put(home,blog);
88         }
89         return blog;
90     }
91     
92     public void getRandomQuestion(WebPageRequest inReq) throws Exception JavaDoc
93     {
94         Question q = (Question)inReq.getSessionValue("question");
95         if( q == null)
96         {
97             Blog blog = getBlog( inReq );
98             q = blog.getRandomQuestion();
99             inReq.putSessionValue("question", q);
100         }
101     }
102     
103     
104     public void writeBlogSettings(WebPageRequest inReq) throws Exception JavaDoc
105     {
106         Blog blog = getBlog( inReq );
107         Page settings = getPageManager().getPage(blog.getBlogHome() + "/blogsettings.xml");
108         //read in some XML
109

110         String JavaDoc parameter = inReq.getRequestParameter("blogtitle");
111         blog.setTitle(parameter);
112         parameter = inReq.getRequestParameter("bloghostname");
113         blog.setHostName(parameter);
114         parameter = inReq.getRequestParameter("blogauthor");
115         blog.setAuthor(parameter);
116         parameter = inReq.getRequestParameter("blogdescription");
117         blog.setDescription(parameter);
118         parameter = inReq.getRequestParameter("bloganonymous");
119         if (parameter == null)
120         {
121             parameter = "false";
122         }
123         blog.setAllowAnonymous("true".equals(parameter));
124         parameter = inReq.getRequestParameter("blogautopublishcomments");
125         if (parameter == null)
126         {
127             parameter = "false";
128         }
129         blog.setAutoPublishingComments("true".equals(parameter));
130         
131         StringWriter JavaDoc writer = new StringWriter JavaDoc();
132         try
133         {
134             new BlogArchive().saveBlog(blog,writer, settings.getCharacterEncoding());
135         }
136         finally
137         {
138             writer.close();
139         }
140         
141         // lets write to a file
142
StringItem item = new StringItem( settings.getPath(), writer.toString(), settings.getCharacterEncoding() );
143         item.setMessage( "Edited blog settings via admin interface" );
144         item.setAuthor( inReq.getUser().getUserName() );
145         settings.setContentItem(item);
146         getPageManager().putPage(settings);
147     }
148     
149     public void generateFeed(WebPageRequest req) throws Exception JavaDoc
150     {
151         SyndFeed feed = new SyndFeedImpl();
152         String JavaDoc feedtype = req.getPage().get("feedtype");
153         if ( feedtype == null)
154         {
155             feedtype = "atom_0.3";
156         }
157         feed.setFeedType(feedtype);
158
159         Blog blog = getBlog(req);
160
161         feed.setTitle(blog.getTitle());
162         feed.setLink(blog.getHostName());
163         feed.setDescription(blog.getDescription());
164         
165         List JavaDoc recent = blog.getRecentEntries();
166         feed.setEntries(recent);
167         
168         Writer JavaDoc writer = new StringWriter JavaDoc();
169          SyndFeedOutput output = new SyndFeedOutput();
170          output.output(feed,writer);
171          writer.close();
172          req.putPageValue("feedresults",writer.toString());
173     }
174
175     public Map JavaDoc getBlogs()
176     {
177         if ( fieldBlogs == null)
178         {
179             fieldBlogs = new HashMap JavaDoc();
180         }
181         return fieldBlogs;
182     }
183     public void setBlogs(Map JavaDoc inBlogs)
184     {
185         fieldBlogs = inBlogs;
186     }
187     /**
188      * @param inReq
189      */

190     public BlogEntry getEntry(WebPageRequest inReq) throws OpenEditException
191     {
192         Blog blog = getBlog(inReq);
193         String JavaDoc entryId = inReq.getRequestParameter("entryId");
194         BlogEntry entry = blog.getEntry(entryId);
195         inReq.putPageValue("entry",entry);
196         return entry;
197     }
198     public void editEntry(WebPageRequest inReq) throws Exception JavaDoc
199     {
200         Blog blog = getBlog(inReq);
201         String JavaDoc entryId = inReq.getRequestParameter("entryId");
202         BlogEntry entry = blog.getEntry(entryId);
203         inReq.putPageValue("blog",blog);
204         inReq.putSessionValue("entry",entry);
205         inReq.setRequestParameter("editPath",entry.getPath());
206         inReq.setRequestParameter("originalPath",blog.getBlogHome());
207         
208     }
209
210     public void loadPermalink(WebPageRequest inReq) throws Exception JavaDoc
211     {
212         //some page is being requested
213
Blog blog = getBlog(inReq);
214         String JavaDoc path = inReq.getPath();
215
216         //get entry will check two places for the path.
217
BlogEntry entry = blog.getEntry(path);
218         if ( entry != null)
219         {
220             inReq.putPageValue("entry",entry);
221         }
222         else
223         {
224             log.error("Null entry" + path);
225         }
226     }
227 // public void loadLink(WebPageRequest inReq) throws Exception
228
// {
229
// //some page is being requested
230
// String path = inReq.getPath();
231
// Blog blog = getBlog(inReq);
232
// String id = path.substring(blog.getArchiveRootDirectory().length(),path.length());
233
// id = blog.getBlogHome() + id;
234
// BlogEntry entry = blog.getEntry(id);
235
// inReq.putPageValue("entry",entry);
236
// }
237
public void loadAdminLink(WebPageRequest inReq) throws Exception JavaDoc
238     {
239         //some page is being requested
240
String JavaDoc path = inReq.getPath();
241         Blog blog = getBlog(inReq);
242         path = "admin/" + path;
243         BlogEntry entry = blog.getEntry(path);
244         inReq.putPageValue("entry",entry);
245     }
246     /**
247      * @param inReq
248      */

249     public void addNewEntry(WebPageRequest inReq) throws Exception JavaDoc
250     {
251         Blog blog = getBlog(inReq);
252         BlogEntry entry = blog.createNewEntry(inReq.getUser());
253         
254         inReq.putPageValue("blog",blog);
255         inReq.putSessionValue("entry",entry);
256         
257         inReq.setRequestParameter("editPath",entry.getPath());
258         inReq.setRequestParameter("originalURL",blog.getBlogHome());
259         
260     }
261     
262     public void saveEntry(WebPageRequest inReq) throws Exception JavaDoc
263     {
264         Blog blog = getBlog(inReq);
265         if ( !blog.canEdit(inReq.getUser()) )
266         {
267             throw new OpenEditException("User cannot edit blog");
268         }
269
270         BlogEntry entry = (BlogEntry)inReq.getSessionValue("entry");
271         if ( entry == null)
272         {
273             entry = blog.createNewEntry(inReq.getUser());
274         }
275
276         //inReq.getPageStreamer().entry.getLink();
277

278         String JavaDoc content = inReq.getRequestParameter("content");
279         if ( content != null)
280         {
281             EditorSession session = new EditorSession();
282             content = session.stripBody(content);
283             //atom does not like XHTML entities NEW VERSION FIXES THIS
284
//content.replaceAll("™","™");
285
//content.replaceAll("®","®");
286
//content.replaceAll("©","©");
287
//content.replaceAll(" "," ");
288

289             //replace  
290
/*
291             //<!ENTITY sharp "&#35;">
292             <!ENTITY trade "&#8482;">
293             <!ENTITY reg "&#174;">
294             <!ENTITY copy "&#169;">
295             <!ENTITY nbsp "&#160;">
296             */

297             
298         }
299         String JavaDoc author = inReq.getRequestParameter("author");
300         entry.setAuthor( author);
301
302         String JavaDoc title = inReq.getRequestParameter("title");
303         entry.setTitle(title);
304         entry.setDescription(content);
305         
306         entry.setVisible(blog.isAutoPublishEntries());
307         
308         BlogArchive archive = getArchive(inReq.getPath());
309
310         archive.saveEntry(blog,entry);
311         archive.getEntryArchive().saveLinks(blog);
312     }
313     public void removeEntry(WebPageRequest inReq) throws Exception JavaDoc
314     {
315         Blog blog = getBlog(inReq);
316
317         if ( !blog.canEdit(inReq.getUser()) )
318         {
319             throw new OpenEditException("User cannot edit blog");
320         }
321
322         String JavaDoc entryId = inReq.getRequestParameter("entryId");
323         BlogEntry entry = blog.getEntry(entryId);
324         
325         blog.removeEntry(entry);
326         BlogArchive archive = getArchive(blog.getBlogHome());
327         archive.getEntryArchive().saveLinks(blog);
328     }
329     protected BlogArchive getArchive(String JavaDoc inPath) throws OpenEditException
330     {
331         Page path = getPageManager().getPage(inPath);
332         return getArchive(path);
333     }
334     protected BlogArchive getArchive(Page inPath) throws OpenEditException
335     {
336         //get the bloghome archive id
337
String JavaDoc name = inPath.getProperty("blogarchivename");
338         if ( name == null)
339         {
340             name = "BlogArchive";
341         }
342         BlogArchive archive = (BlogArchive)getBeanFactory().getBean(name);
343         return archive;
344     }
345     /**
346      * @param inReq
347      */

348     public void addNewComment(WebPageRequest inReq) throws Exception JavaDoc
349     {
350         String JavaDoc content = inReq.getRequestParameter("content");
351         String JavaDoc entryId = inReq.getRequestParameter("entryId");
352         if( content == null || entryId == null)
353         {
354             return;
355         }
356         Blog blog = getBlog(inReq);
357         if ( checkQuestion(inReq) )
358         {
359             BlogEntry entry = blog.getEntry(entryId);
360             
361             String JavaDoc author = inReq.getRequestParameter("username");
362             User user = getUserManager().getUser(author);
363             if( user == null)
364             {
365                 if( "anonymous".equals( author ) && blog.getAllowAnonymous() )
366                 {
367                     user = new FileSystemUser();
368                     user.setUserName(author);
369                 }
370                 else
371                 {
372                     throw new OpenEditException("Anonymous user not allowed");
373                 }
374             }
375             Comment comment = blog.createNewComment(user, content);
376             entry.addComment(comment);
377             BlogArchive archive = getArchive(inReq.getPage());
378             archive.getEntryArchive().getCommentArchive().saveComments(blog, entry);
379             getCommentNotification().commentAdded(inReq, blog, entry,comment);
380         }
381         //inReq.removeSessionValue("question");
382
}
383     
384     public boolean checkQuestion(WebPageRequest inReq) throws OpenEditException
385     {
386         if( inReq.getSessionValue("validperson") != null )
387         {
388             return true;
389         }
390         
391         String JavaDoc answer = inReq.getRequestParameter("answerid");
392         Blog blog = getBlog(inReq);
393         Question q = (Question)inReq.getSessionValue("question");
394         if ( !q.checkAnswer( answer) )
395         {
396             inReq.redirect(blog.getBlogHome() + "/questionerror.html");
397             return false;
398         }
399         inReq.putSessionValue("validperson", "true");
400         return true;
401     }
402     public void removeComment(WebPageRequest inReq) throws Exception JavaDoc
403     {
404         Blog blog = getBlog(inReq);
405         String JavaDoc entryId = inReq.getRequestParameter("entryId");
406         BlogEntry entry = blog.getEntry(entryId);
407         String JavaDoc commentId = inReq.getRequestParameter("commentId");
408         Comment comment = entry.getComment(commentId);
409         User user = (User)inReq.getSessionValue("user");
410
411         if (comment.canEdit(user))
412         {
413             entry.removeComment(comment);
414             BlogArchive archive = getArchive(inReq.getPage());
415             archive.getEntryArchive().getCommentArchive().saveComments(blog, entry);
416         }
417         redirectToOrig(inReq);
418     }
419     public void changeCommentVisibility(WebPageRequest inReq) throws Exception JavaDoc
420     {
421         String JavaDoc entryId = inReq.getRequestParameter("entryId");
422         if( entryId != null)
423         {
424             Blog blog = getBlog(inReq);
425             String JavaDoc commentId = inReq.getRequestParameter("commentId");
426             BlogEntry entry = blog.getEntry(entryId);
427             Comment comment = entry.getComment(commentId);
428             if ( comment != null )
429             {
430                 if(comment.canEdit(inReq.getUser()) || blog.canEdit(inReq.getUser() ))
431                 {
432                     comment.setVisible(!comment.isVisible());
433                     BlogArchive archive = getArchive(inReq.getPage());
434                     archive.getEntryArchive().getCommentArchive().saveComments(blog, entry);
435                 }
436             }
437             redirectToOrig(inReq);
438         }
439     }
440     private void redirectToOrig(WebPageRequest inReq)
441     {
442         String JavaDoc origURL = inReq.getRequestParameter("origURL");
443         if (origURL != null && origURL.length() > 0)
444         {
445             inReq.redirect(origURL);
446         }
447     }
448     public void login(WebPageRequest inReq) throws Exception JavaDoc
449     {
450         String JavaDoc username = inReq.getRequestParameter("username");
451         User user = getUserManager().getUser(username);
452         String JavaDoc password = inReq.getRequestParameter("password");
453         if( getUserManager().authenticate(user , password) )
454         {
455             inReq.putSessionValue("user",user);
456         }
457     }
458     /*
459     public void registerNewUser(WebPageRequest inReq) throws Exception
460     {
461         //loop over all the properties
462         String email = inReq.getRequestParameter("property-email");
463         User user = getUserManager().getUserByEmail(email);
464         if ( user != null)
465         {
466             inReq.putPageValue("oe-error","Duplicate user. email address already in system");
467             return;
468         }
469         String password = inReq.getRequestParameter("password1");
470         String password2 = inReq.getRequestParameter("password2");
471         if ( password == null || password2 == null || !password.equals(password2) || password.length() == 0)
472         {
473             inReq.putPageValue("oe-error","Passwords do not match");
474             return;
475         }
476         User newuser = getUserManager().createUser(null,password);
477         for (Iterator iter = inReq.getParameterMap().keySet().iterator(); iter.hasNext();)
478         {
479             String keyId = (String) iter.next();
480             if ( keyId.indexOf("property-") == 0)
481             {
482                 newuser.put(keyId.substring("property-".length()),inReq.getRequestParameter(keyId));
483             }
484         }
485         getUserManager().saveUser(newuser);
486         inReq.putSessionValue("user",newuser);
487         String commentlink = inReq.getRequestParameter("loginokpage");
488         if ( commentlink != null && commentlink.length() > 0)
489         {
490             inReq.redirect(commentlink);
491         }
492     }
493     */

494     public void changeEntryVisibility(WebPageRequest inReq) throws Exception JavaDoc
495     {
496         Blog blog = getBlog(inReq);
497
498         if ( !blog.canEdit(inReq.getUser()) )
499         {
500             throw new OpenEditException("User cannot edit blog");
501         }
502
503         String JavaDoc entryId = inReq.getRequestParameter("entryId");
504         BlogEntry entry = blog.getEntry(entryId);
505         
506         entry.setVisible(!entry.isVisible());
507         BlogArchive archive = getArchive(inReq.getPage());
508         archive.saveEntry(blog, entry);
509     }
510
511     public BlogCommentNotification getCommentNotification()
512     {
513         return fieldCommentNotification;
514     }
515
516     public void setCommentNotification(BlogCommentNotification inCommentNotification)
517     {
518         fieldCommentNotification = inCommentNotification;
519     }
520 }
521
Popular Tags