KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Jan 13, 2006
3  */

4 package org.openedit.blog.modules;
5
6 import java.io.IOException JavaDoc;
7 import java.io.StringWriter JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import com.openedit.BaseWebPageRequest;
13 import com.openedit.OpenEditException;
14 import com.openedit.WebPageRequest;
15 import com.openedit.WebServer;
16 import com.openedit.blog.BlogEntry;
17 import com.openedit.blog.Notify;
18 import com.openedit.modules.BaseModule;
19 import com.openedit.modules.email.TemplateWebEmail;
20 import com.openedit.modules.html.Tidy;
21 import com.openedit.page.Page;
22 import com.openedit.page.PageRequestKeys;
23 import com.openedit.page.PageStreamer;
24 import com.openedit.util.URLUtilities;
25 import com.openedit.modules.email.PostMail;
26 public class BlogAdminModule extends BaseModule
27 {
28     private static final Log log = LogFactory.getLog(BlogAdminModule.class);
29     private PostMail postMail;
30     public void cancelNotification( WebPageRequest inReq)
31     {
32         Notify notify = getNotify(inReq);
33         notify.cancel();
34         inReq.removeSessionValue("notify");
35         log.info("Canceled job");
36     }
37     
38     public Notify getNotify(WebPageRequest inReq)
39     {
40         Notify notify = (Notify)inReq.getSessionValue("notify");
41         if ( notify == null)
42         {
43             notify = new Notify();
44             notify.setUserManager(getUserManager());
45             notify.setRootDirectory(getRoot());
46             notify.setPageManager(getPageManager());
47             inReq.putSessionValue("notify",notify);
48         }
49         return notify;
50     }
51     
52     public void sendNotification(WebPageRequest inReq) throws OpenEditException, IOException JavaDoc
53     {
54         Notify notify = getNotify(inReq);
55         if ( notify.isKeepRunning() )
56         {
57             //already running
58
return;
59         }
60         
61         String JavaDoc[] groupnames = inReq.getRequestParameters("groupnames");
62         if ( groupnames != null && groupnames.length > 0)
63         {
64             notify.setGroupNames(groupnames);
65             
66             TemplateWebEmail mailer = postMail.getTemplateWebEmail();
67             String JavaDoc from = inReq.getRequestParameter("author");
68             String JavaDoc server = inReq.getRequestParameter("server");
69             String JavaDoc subject = inReq.getRequestParameter("title");
70             //String path = inReq.getRequestParameter("editPath");
71
BlogModule module = (BlogModule)getModule("BlogModule");
72             BlogEntry entry = module.getEntry(inReq);
73             mailer.setFrom(from);
74             mailer.setSubject(subject);
75             mailer.setMailTemplatePath(entry.getPath());
76             Page content = getPageManager().getPage(entry.getPath());
77
78             String JavaDoc uselayout = cleanLink( inReq.getRequestParameter("uselayout"), inReq );
79             if (uselayout != null )
80             {
81                 String JavaDoc results = renderUserLayout(inReq, content, uselayout);
82                 mailer.setMessage(results);
83                 
84                 String JavaDoc addplaintext = inReq.getRequestParameter("addplaintext");
85                 if ( addplaintext != null && addplaintext.equalsIgnoreCase("true"))
86                 {
87                     String JavaDoc alternative = content.getContent();
88                     alternative = new Tidy().removeHtml(alternative);
89                     mailer.setAlternativeMessage(alternative);
90                 }
91             }
92             else
93             {
94                 mailer.setMailTemplatePage(content);
95             }
96             
97             //mailer.setMailTemplatePage(template);
98
notify.sendEmail(mailer, inReq.getWriter() );
99         }
100         else
101         {
102             inReq.getWriter().write("Error: No groups selected");
103         }
104     }
105     private String JavaDoc cleanLink(String JavaDoc inRequestParameter, WebPageRequest inReq)
106     {
107         if ( inRequestParameter != null)
108         {
109             String JavaDoc home = (String JavaDoc)inReq.getPageValue("home");
110             if ( home != null && home.length() > 0)
111             {
112                 inRequestParameter = inRequestParameter.substring(home.length(),inRequestParameter.length());
113             }
114         }
115         return inRequestParameter;
116     }
117
118     public void renderPreview(WebPageRequest inReq ) throws OpenEditException
119     {
120         String JavaDoc uselayout = cleanLink( inReq.getRequestParameter("uselayout"), inReq );
121         BlogModule module = (BlogModule)getModule("BlogModule");
122         BlogEntry entry = module.getEntry(inReq);
123
124         Page content = getPageManager().getPage(entry.getPath());
125         String JavaDoc preview = null;
126         
127         if ( uselayout != null)
128         {
129             preview = renderUserLayout(inReq,content,uselayout);
130         }
131         else
132         {
133             preview = content.getContent(); //TODO: Use a notify render for a true preview
134
}
135         inReq.putPageValue("preview",preview);
136     }
137     protected String JavaDoc renderUserLayout(WebPageRequest inReq, Page content, String JavaDoc uselayout) throws OpenEditException
138     {
139         WebServer webserver = (WebServer)getBeanFactory().getBean("WebServer");
140         BaseWebPageRequest req = (BaseWebPageRequest)inReq.copy(content);
141         req.putPageValue(PageRequestKeys.LAYOUTOVERRIDE,uselayout);
142         req.putPageValue(PageRequestKeys.INNERLAYOUTOVERRIDE,Page.BLANK_LAYOUT); //TODO: Find nicer way
143
req.putPageValue(PageRequestKeys.CONTENT,content);
144         req.putProtectedPageValue(PageRequestKeys.PAGE,content);
145         req.setEditable(false);
146         req.putPageValue(PageRequestKeys.OUTPUT_WRITER, new StringWriter JavaDoc());
147         PageStreamer stream = webserver.getOpenEditEngine().createPageStreamer(content,req);
148         stream.render();
149         String JavaDoc fcontent = req.getWriter().toString();
150         return fcontent;
151     }
152     public void fixLinks(WebPageRequest inReq) throws OpenEditException
153     {
154         URLUtilities utils = (URLUtilities)inReq.getPageValue(PageRequestKeys.URL_UTILITIES);
155         if ( utils != null)
156         {
157             String JavaDoc base = utils.siteRoot() + utils.relativeHomePrefix();
158             Notify notify = getNotify(inReq);
159             String JavaDoc path = inReq.getRequestParameter("editPath");
160             Page template = getPageManager().getPage(path);
161             notify.fixLinks(template,base);
162             inReq.putPageValue("message","Links have been set to " + base);
163         }
164
165     }
166
167     public PostMail getPostMail() {
168         return postMail;
169     }
170
171     public void setPostMail(PostMail postMail) {
172         this.postMail = postMail;
173     }
174 }
175
Popular Tags