KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > weblog > actions > BakeWeblogAction


1 package org.roller.presentation.weblog.actions;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.apache.struts.action.ActionError;
6 import org.apache.struts.action.ActionErrors;
7 import org.apache.struts.action.ActionForm;
8 import org.apache.struts.action.ActionForward;
9 import org.apache.struts.action.ActionMapping;
10 import org.apache.struts.actions.DispatchAction;
11 import org.apache.velocity.VelocityContext;
12 import org.apache.velocity.app.Velocity;
13 import org.apache.velocity.exception.MethodInvocationException;
14 import org.apache.velocity.exception.ParseErrorException;
15 import org.apache.velocity.exception.ResourceNotFoundException;
16 import org.roller.RollerException;
17 import org.roller.model.UserManager;
18 import org.roller.model.WeblogManager;
19 import org.roller.pojos.PageData;
20 import org.roller.pojos.WebsiteData;
21 import org.roller.presentation.RollerContext;
22 import org.roller.presentation.RollerRequest;
23 import org.roller.presentation.RollerSession;
24 import org.roller.presentation.velocity.ContextLoader;
25 import org.roller.presentation.weblog.tags.WeblogEntryMacros;
26 import org.roller.util.DateUtil;
27
28 import java.io.File JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.StringWriter JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import javax.servlet.jsp.JspFactory JavaDoc;
43 import javax.servlet.jsp.PageContext JavaDoc;
44
45 /////////////////////////////////////////////////////////////////////////////
46
/**
47  * Generates static pages for each day containing Weblog entries.
48  *
49  * @struts.action name="bakeWeblogForm" path="/editor/bake"
50  * scope="session" parameter="method"
51  *
52  * @struts.action-forward name="baking.done" path="/weblog/bakeWeblog.jsp"
53  *
54  * @author llavandowska
55  */

56 public class BakeWeblogAction extends DispatchAction {
57     private static Log mLogger =
58             LogFactory.getFactory().getInstance(BakeWeblogAction.class);
59     
60     public ActionForward load(
61             ActionMapping mapping,
62             ActionForm actionForm,
63             HttpServletRequest JavaDoc request,
64             HttpServletResponse JavaDoc response)
65             throws IOException JavaDoc, ServletException JavaDoc {
66         RollerRequest rreq = RollerRequest.getRollerRequest(request);
67         try {
68             if ( !rreq.isUserAuthorizedToEdit() ) {
69                 return mapping.findForward("access-denied");
70             }
71         } catch (Exception JavaDoc e) {
72             return mapping.findForward("access-denied");
73         }
74         return mapping.findForward("baking.done");
75     }
76     
77     public ActionForward bake(
78             ActionMapping mapping,
79             ActionForm actionForm,
80             HttpServletRequest JavaDoc request,
81             HttpServletResponse JavaDoc response)
82             throws IOException JavaDoc, ServletException JavaDoc {
83         ActionForward forward = mapping.findForward("baking.done");
84         ActionErrors errors = new ActionErrors();
85         try {
86             PageContext JavaDoc pageContext =
87                     JspFactory.getDefaultFactory().getPageContext(
88                     this.getServlet(), request, response,"", true, 8192, true);
89             RollerRequest rreq = RollerRequest.getRollerRequest(pageContext);
90             if ( !rreq.isUserAuthorizedToEdit() ) {
91                 return mapping.findForward("access-denied");
92             }
93             
94             String JavaDoc pid = getDefaultPageId( rreq );
95             
96             VelocityContext context = new VelocityContext();
97             ContextLoader.setupContext( context, rreq, response );
98             
99             // Get this months Entries
100
WeblogManager mgr = rreq.getRoller().getWeblogManager();
101             //Map entryMap = mgr.getWeblogEntryMonthMap(
102
//rreq.getUser().getUserName(),
103
//rreq.getDate(true),
104
//null,
105
//false,
106
//true );
107

108             Map JavaDoc entryMap = mgr.getWeblogEntryObjectMap(
109                     rreq.getWebsite(),
110                     null, // startDate
111
rreq.getDate(true), // endDate
112
null, // catName
113
WeblogManager.PUB_ONLY, // status
114
null); // maxEntries
115

116             if (mLogger.isDebugEnabled()) {
117                 mLogger.debug("Num Days to Bake: " + entryMap.size());
118             }
119             
120             Iterator JavaDoc iter = entryMap.keySet().iterator();
121             if (iter.hasNext()) {
122                 Date JavaDoc d = (Date JavaDoc)iter.next();
123                 
124                 if (mLogger.isDebugEnabled()) {
125                     mLogger.debug("Bake Weblog for date:" + d);
126                 }
127                 
128                 // Do we need this to continue supporting Macros?
129
WeblogEntryMacros macros =
130                         new WeblogEntryMacros( rreq.getPageContext(), d );
131                 context.put( "macros", macros );
132                 
133                 List JavaDoc entries = (List JavaDoc) entryMap.get( d );
134                 String JavaDoc content = transformTemplate(pid, entries, context, rreq);
135                 
136                 String JavaDoc fileName = writeToFile(content, d, rreq);
137                 request.getSession().setAttribute(
138                         RollerSession.STATUS_MESSAGE,
139                         "Weblog baked to :" + fileName);
140             }
141             
142             /** put some user message **/
143             String JavaDoc message = (String JavaDoc) request.getSession().getAttribute(
144                     RollerSession.STATUS_MESSAGE);
145             if (message == null) message = "No Files Written";
146             request.getSession().setAttribute(
147                     RollerSession.STATUS_MESSAGE,
148                     message);
149         } catch (Exception JavaDoc e) {
150             forward = mapping.findForward("error");
151             
152             errors.add(ActionErrors.GLOBAL_ERROR,
153                     new ActionError("error.bake.weblog", e.toString()));
154             saveErrors(request,errors);
155             
156             mLogger.error(getResources(request).getMessage("error.bake.weblog")
157             + e.toString(),e);
158         }
159         
160         return forward;
161     }
162     
163     /**
164      * Write the content generated by transformTemplate to a file in the Upload
165      * directory, where the user can reach it.
166      *
167      * @param content
168      * @param d
169      * @param rreq
170      */

171     private String JavaDoc writeToFile(String JavaDoc content, Date JavaDoc d, RollerRequest rreq)
172     throws IOException JavaDoc {
173         if (mLogger.isDebugEnabled()) {
174             mLogger.debug("Write file for:" + d);
175         }
176         
177         String JavaDoc dir = RollerContext.getUploadDir( rreq.getServletContext() );
178         String JavaDoc username = rreq.getUser().getUserName();
179         File JavaDoc dirF = new File JavaDoc(dir + username + File.separator + "baked");
180         if (!dirF.exists()) {
181             dirF.mkdirs();
182         }
183         
184         SimpleDateFormat JavaDoc mFmt = DateUtil.get8charDateFormat();
185         String JavaDoc dString = mFmt.format( d ) + ".html";
186         File JavaDoc pFile = new File JavaDoc(dirF, dString);
187         
188         OutputStream JavaDoc bos = null;
189         try {
190             bos = new FileOutputStream JavaDoc(pFile);
191             bos.write( content.getBytes() );
192         } catch (IOException JavaDoc e) {
193             mLogger.error("Unable to write to " + pFile.getName());
194         } finally {
195             try {
196                 if (bos != null) bos.close();
197             } catch (IOException JavaDoc e1) {
198                 mLogger.error("Unable to close stream to " + pFile.getName());
199             }
200         }
201         
202         return pFile.getName();
203     }
204     
205     /**
206      * Transform the Page specified by the pageId using Velocity. Return the
207      * resulting String.
208      *
209      * @param pid
210      * @param context
211      * @throws ResourceNotFoundException
212      * @throws ParseErrorException
213      * @throws MethodInvocationException
214      * @throws Exception
215      */

216     private String JavaDoc transformTemplate(String JavaDoc pid, List JavaDoc entries,
217             VelocityContext context, RollerRequest rreq)
218             throws
219             ResourceNotFoundException,
220             ParseErrorException,
221             MethodInvocationException,
222             Exception JavaDoc {
223         
224         context.put( "entries", entries );
225         
226         StringWriter JavaDoc sw = new StringWriter JavaDoc();
227         Velocity.mergeTemplate(pid, Velocity.ENCODING_DEFAULT, context, sw );
228         return sw.toString();
229     }
230     
231     /**
232      * Get the default pageId for this Website, this is assumed to be the Weblog
233      * page since we have no other way of declaring it.
234      *
235      * @param rreq
236      * @return String
237      * @throws RollerException
238      */

239     private String JavaDoc getDefaultPageId(RollerRequest rreq) throws RollerException {
240         UserManager userMgr = rreq.getRoller().getUserManager();
241         WebsiteData wd = rreq.getWebsite();
242         PageData pd = userMgr.retrievePage(wd.getDefaultPageId());
243         return pd.getId();
244     }
245     
246 }
247
Popular Tags