001: /*
002: * Created on Jan 13, 2006
003: */
004: package org.openedit.blog.modules;
005:
006: import java.io.IOException;
007: import java.io.StringWriter;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: import com.openedit.BaseWebPageRequest;
013: import com.openedit.OpenEditException;
014: import com.openedit.WebPageRequest;
015: import com.openedit.WebServer;
016: import com.openedit.blog.BlogEntry;
017: import com.openedit.blog.Notify;
018: import com.openedit.modules.BaseModule;
019: import com.openedit.modules.email.PostMail;
020: import com.openedit.modules.email.TemplateWebEmail;
021: import com.openedit.modules.html.Tidy;
022: import com.openedit.page.Page;
023: import com.openedit.page.PageRequestKeys;
024: import com.openedit.page.PageStreamer;
025: import com.openedit.util.URLUtilities;
026:
027: public class BlogAdminModule extends BaseModule {
028: private static final Log log = LogFactory
029: .getLog(BlogAdminModule.class);
030: private PostMail postMail;
031:
032: public void cancelNotification(WebPageRequest inReq) {
033: Notify notify = getNotify(inReq);
034: notify.cancel();
035: inReq.removeSessionValue("notify");
036: log.info("Canceled job");
037: }
038:
039: public Notify getNotify(WebPageRequest inReq) {
040: Notify notify = (Notify) inReq.getSessionValue("notify");
041: if (notify == null) {
042: notify = new Notify();
043: notify.setUserManager(getUserManager());
044: notify.setRootDirectory(getRoot());
045: notify.setPageManager(getPageManager());
046: inReq.putSessionValue("notify", notify);
047: }
048: return notify;
049: }
050:
051: public void sendNotification(WebPageRequest inReq)
052: throws OpenEditException, IOException {
053: Notify notify = getNotify(inReq);
054: if (notify.isKeepRunning()) {
055: //already running
056: return;
057: }
058:
059: String[] groupnames = inReq.getRequestParameters("groupnames");
060: if (groupnames != null && groupnames.length > 0) {
061: notify.setGroupNames(groupnames);
062:
063: TemplateWebEmail mailer = postMail.getTemplateWebEmail();
064: mailer.setPageManager(getPageManager());
065:
066: String from = inReq.getRequestParameter("author");
067: String server = inReq.getRequestParameter("server");
068: String subject = inReq.getRequestParameter("title");
069: //String path = inReq.getRequestParameter("editPath");
070: BlogModule module = (BlogModule) getModule("BlogModule");
071: BlogEntry entry = module.getEntry(inReq);
072: mailer.setFrom(from);
073: mailer.setSubject(subject);
074: mailer.setMailTemplatePath(entry.getPath());
075: Page content = getPageManager().getPage(entry.getPath());
076:
077: String uselayout = cleanLink(inReq
078: .getRequestParameter("uselayout"), inReq);
079: if (uselayout != null) {
080: String results = renderUserLayout(inReq, content,
081: uselayout);
082: mailer.setMessage(results);
083:
084: String addplaintext = inReq
085: .getRequestParameter("addplaintext");
086: if (addplaintext != null
087: && addplaintext.equalsIgnoreCase("true")) {
088: String alternative = content.getContent();
089: alternative = new Tidy().removeHtml(alternative);
090: mailer.setAlternativeMessage(alternative);
091: }
092: } else {
093: mailer.setMailTemplatePage(content);
094: }
095:
096: //mailer.setMailTemplatePage(template);
097: notify.sendEmail(mailer, inReq.getWriter());
098: } else {
099: inReq.getWriter().write("Error: No groups selected");
100: }
101: }
102:
103: private String cleanLink(String inRequestParameter,
104: WebPageRequest inReq) {
105: if (inRequestParameter != null) {
106: String home = (String) inReq.getPageValue("home");
107: if (home != null && home.length() > 0) {
108: inRequestParameter = inRequestParameter.substring(home
109: .length(), inRequestParameter.length());
110: }
111: }
112: return inRequestParameter;
113: }
114:
115: public void renderPreview(WebPageRequest inReq)
116: throws OpenEditException {
117: String uselayout = cleanLink(inReq
118: .getRequestParameter("uselayout"), inReq);
119: BlogModule module = (BlogModule) getModule("BlogModule");
120: BlogEntry entry = module.getEntry(inReq);
121:
122: Page content = getPageManager().getPage(entry.getPath());
123: String preview = null;
124:
125: if (uselayout != null) {
126: preview = renderUserLayout(inReq, content, uselayout);
127: } else {
128: preview = content.getContent(); //TODO: Use a notify render for a true preview
129: }
130: inReq.putPageValue("preview", preview);
131: }
132:
133: protected String renderUserLayout(WebPageRequest inReq,
134: Page content, String uselayout) throws OpenEditException {
135: WebServer webserver = (WebServer) getBeanFactory().getBean(
136: "WebServer");
137: BaseWebPageRequest req = (BaseWebPageRequest) inReq
138: .copy(content);
139: req.putPageValue(PageRequestKeys.LAYOUTOVERRIDE, uselayout);
140: req.putPageValue(PageRequestKeys.INNERLAYOUTOVERRIDE,
141: Page.BLANK_LAYOUT); //TODO: Find nicer way
142: req.putPageValue(PageRequestKeys.CONTENT, content);
143: req.putProtectedPageValue(PageRequestKeys.PAGE, content);
144: req.setEditable(false);
145: req.putPageValue(PageRequestKeys.OUTPUT_WRITER,
146: new StringWriter());
147: PageStreamer stream = webserver.getOpenEditEngine()
148: .createPageStreamer(content, req);
149: stream.render();
150: String fcontent = req.getWriter().toString();
151: return fcontent;
152: }
153:
154: public void fixLinks(WebPageRequest inReq) throws OpenEditException {
155: URLUtilities utils = (URLUtilities) inReq
156: .getPageValue(PageRequestKeys.URL_UTILITIES);
157: if (utils != null) {
158: String base = utils.siteRoot() + utils.relativeHomePrefix();
159: Notify notify = getNotify(inReq);
160: String path = inReq.getRequestParameter("editPath");
161: Page template = getPageManager().getPage(path);
162: notify.fixLinks(template, base);
163: inReq.putPageValue("message", "Links have been set to "
164: + base);
165: }
166:
167: }
168:
169: public PostMail getPostMail() {
170: return postMail;
171: }
172:
173: public void setPostMail(PostMail postMail) {
174: this.postMail = postMail;
175: }
176: }
|