KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > velocity > deprecated > OldPageHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.velocity.deprecated;
20
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Vector JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.servlet.jsp.PageContext JavaDoc;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.struts.Globals;
35 import org.apache.struts.util.RequestUtils;
36 import org.apache.roller.RollerException;
37 import org.apache.roller.model.WeblogEntryPlugin;
38 import org.apache.roller.model.PluginManager;
39 import org.apache.roller.model.Roller;
40 import org.apache.roller.model.RollerFactory;
41 import org.apache.roller.pojos.FolderData;
42 import org.apache.roller.pojos.WebsiteData;
43 import org.apache.roller.pojos.wrapper.RefererDataWrapper;
44 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
45 import org.apache.roller.ui.core.RequestConstants;
46 import org.apache.roller.ui.core.RollerContext;
47 import org.apache.roller.ui.core.RollerSession;
48 import org.apache.roller.ui.core.tags.calendar.CalendarModel;
49 import org.apache.roller.ui.core.tags.calendar.CalendarTag;
50 import org.apache.roller.ui.core.tags.menu.EditorNavigationBarTag;
51 import org.apache.roller.ui.core.tags.menu.MenuTag;
52 import org.apache.roller.ui.core.tags.calendar.BigWeblogCalendarModel;
53 import org.apache.roller.ui.core.tags.calendar.WeblogCalendarModel;
54 import org.apache.roller.ui.rendering.util.WeblogPageRequest;
55
56 /**
57  * Provides assistance to VelociMacros, filling in where Velocity falls.
58  */

59 public class OldPageHelper {
60     
61     private static Log mLogger = LogFactory.getLog(OldPageHelper.class);
62     
63     private PageContext JavaDoc mPageContext = null;
64     private HttpServletRequest JavaDoc mRequest = null;
65     private HttpServletResponse JavaDoc mResponse = null;
66     
67     private Map JavaDoc mPagePlugins = null; // Plugins keyed by name
68
private WebsiteData mWebsite = null;
69     private Date JavaDoc mDate = null;
70     private FolderData mFolder = null;
71     private String JavaDoc mPageName = null;
72     private WeblogPageRequest mPageRequest = null;
73     
74     /**
75      * Initialize VelocityHelper, setting the variables it will be hiding from
76      * the Velocimacros.
77      */

78     public OldPageHelper(HttpServletRequest JavaDoc request,
79                       HttpServletResponse JavaDoc response,
80                       Map JavaDoc ctx,
81                       WebsiteData website,
82                       Date JavaDoc date,
83                       FolderData folder,
84                       String JavaDoc pageName,
85                       PageContext JavaDoc pageContext,
86                       WeblogPageRequest pageRequest) throws RollerException {
87         
88         // general request objects
89
mRequest = request;
90         mResponse = response;
91         mPageContext = pageContext;
92         mPageRequest = pageRequest;
93         
94         // data that we'll be reusing
95
mWebsite = website;
96         mDate = date;
97         mFolder = folder;
98         
99         // init plugins
100
Roller roller = RollerFactory.getRoller();
101         PluginManager ppmgr = roller.getPagePluginManager();
102         mPagePlugins = ppmgr.getWeblogEntryPlugins(mWebsite);
103     }
104     
105     
106     /**
107      * Another stupid helper method to make up for the shortcomings of Velocity.
108      * @return HashMap
109      */

110     public Hashtable JavaDoc addParam(String JavaDoc key, String JavaDoc value, Hashtable JavaDoc map) {
111         if (map == null) map = new Hashtable JavaDoc();
112         if (key != null && value != null)
113             map.put(key, value);
114         return map;
115     }
116     
117     
118     /**
119      * Evaluates the String as a Velocimacro, returning the results.
120      *
121      * @deprecated shouldn't be used anymore because it's dangerous
122      *
123      * @param str String
124      * @return String
125      */

126     public String JavaDoc evaluateString(String JavaDoc str) {
127         // we no longer allow users to do this because it is dangerous
128
return str;
129     }
130     
131     
132     /** Build the URL for editing an WeblogEntry **/
133     public String JavaDoc getEntryEditUrl(WeblogEntryDataWrapper entry) {
134         Hashtable JavaDoc params = new Hashtable JavaDoc();
135         params.put( RequestConstants.WEBLOGENTRY_ID, entry.getId());
136         params.put( RequestConstants.ANCHOR, entry.getAnchor());
137         if (mWebsite != null) {
138             params.put( RequestConstants.USERNAME, mWebsite.getHandle());
139         }
140         try {
141             return RequestUtils.computeURL( mPageContext,
142                     "weblogEdit", null, null, null, params, null, false);
143         } catch (MalformedURLException JavaDoc mue) {
144             mLogger.warn("RollerRequest.editEntryUrl exception: ", mue);
145         }
146         return mRequest.getContextPath() + "edtior/weblog.do?method=edit";
147     }
148     
149     
150     public String JavaDoc getToggleLinkbackDisplayHTML(RefererDataWrapper referer) {
151         String JavaDoc ret = "";
152         String JavaDoc link = null;
153         try {
154             RollerSession rollerSession =
155                     RollerSession.getRollerSession(mRequest);
156             if (mWebsite != null
157                     && rollerSession.isUserAuthorizedToAdmin(mWebsite)) {
158                 Hashtable JavaDoc params = new Hashtable JavaDoc();
159                 params.put( RequestConstants.REFERRER_ID, referer.getId());
160                 params.put( RequestConstants.WEBLOG, mWebsite.getHandle());
161                 link = RequestUtils.computeURL( mPageContext,
162                         "toggleLinkback", null, null, null, params,null,false);
163                 
164                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
165                 sb.append("[<a HREF=\"");
166                 sb.append(link);
167                 if ( referer.getVisible().booleanValue() ) {
168                     sb.append("\">Visible</a>] ");
169                 } else {
170                     sb.append("\">Hidden</a>] ");
171                 }
172                 ret = sb.toString();
173             }
174         } catch (Exception JavaDoc e) {
175             // should never happen, but if it does:
176
mLogger.error("ERROR creating toggle-linkback URL",e);
177         }
178         
179         return ret;
180     }
181     
182     
183     public boolean isUserAuthorizedToEdit() {
184         try {
185             RollerSession rses =
186                     RollerSession.getRollerSession(mRequest);
187             if ( rses.getAuthenticatedUser() != null
188                     && mWebsite != null) {
189                 return rses.isUserAuthorizedToAdmin(mWebsite);
190             }
191         } catch (Exception JavaDoc e) {
192             mLogger.warn("PageHelper.isUserAuthorizedToEdit)", e);
193         }
194         return false;
195     }
196     
197     
198     public void setContentType( String JavaDoc type ) {
199         mResponse.setContentType(type);
200     }
201     
202     
203     /**
204      * Display big weblog calendar, well suited for an archive page.
205      * @return HTML for calendar.
206      */

207     public String JavaDoc showBigWeblogCalendar() {
208         return showWeblogCalendar(true, null);
209     }
210     
211     
212     /**
213      * Call hybrid EditorNavBarTag to render editor navbar.
214      * @param vertical True for vertical navbar.
215      * @return String HTML for navbar.
216      */

217     public String JavaDoc showEditorNavBar(boolean vertical) {
218         EditorNavigationBarTag editorTag = new EditorNavigationBarTag();
219         editorTag.setPageContext(mPageContext);
220         if ( vertical ) {
221             editorTag.setView("templates/navbar/navbar-vertical.vm");
222         } else {
223             editorTag.setView("templates/navbar/navbar-horizontal.vm");
224         }
225         editorTag.setModel("editor-menu.xml");
226         return editorTag.emit();
227     }
228     
229     
230     /**
231      * Call hybrid EditorNavBarTag to render editor navbar.
232      * @param model Name of XML file in WEB-INF that contains XML for menu.
233      * @param template Name of Velocity template in classpath to display menu.
234      * @return String HTML for menu.
235      */

236     public String JavaDoc showMenu(String JavaDoc model, String JavaDoc template) {
237         MenuTag menuTag = new MenuTag();
238         menuTag.setPageContext(mPageContext);
239         menuTag.setModel(model);
240         menuTag.setView(template);
241         return menuTag.emit();
242     }
243     
244     //------------------------------------------------- WeblogCalendar methods
245

246     /**
247      * Display weblog calendar.
248      * @return HTML for calendar.
249      */

250     public String JavaDoc showWeblogCalendar() {
251         return showWeblogCalendar(false, null);
252     }
253     
254     
255     /**
256      * Weblog calendar display implementation.
257      * @param big Show big archive style calendar.
258      * @return HTML for calendar.
259      */

260     public String JavaDoc showWeblogCalendar( boolean big, String JavaDoc cat ) {
261         if (OldWeblogPageModel.VELOCITY_NULL.equals(cat)) cat = null;
262         String JavaDoc ret = null;
263         try {
264             // setup weblog calendar model
265
CalendarModel model = null;
266             if ( big ) {
267                 model = new BigWeblogCalendarModel(mPageRequest, cat);
268             } else {
269                 model = new WeblogCalendarModel(mPageRequest, cat);
270             }
271             
272             // save model in JSP page context so CalendarTag can find it
273
mPageContext.setAttribute("calendarModel", model);
274             
275             // Create and setup calendar tag
276
CalendarTag calTag = new CalendarTag();
277             calTag.setPageContext(mPageContext);
278             calTag.setName("calendar");
279             calTag.setModel("calendarModel");
280             calTag.setLocale(mPageRequest.getLocaleInstance());
281             if ( big ) {
282                 calTag.setClassSuffix("Big");
283             }
284             ret = calTag.emit();
285         } catch (Exception JavaDoc e) {
286             mLogger.error("Unexpected exception",e);
287         }
288         return ret;
289     }
290     
291     
292     /**
293      * Convenience method, contrived helper for Velocity.
294      * @param useIds
295      * @param isAction
296      * @param path
297      * @param val1
298      * @param val2
299      * @return String
300      */

301     public String JavaDoc strutsUrlHelper( boolean useIds, boolean isAction,
302             String JavaDoc path, String JavaDoc val1, String JavaDoc val2) {
303         Hashtable JavaDoc params = new Hashtable JavaDoc();
304         return strutsUrlHelper1( useIds, isAction, path, val1, val2, params);
305     }
306     
307     
308     /**
309      * Very contrived helper method for Velocimacros generating Struts links.
310      * This is really only of use to the showNavBar macro.
311      * @param useIds
312      * @param isAction
313      * @param path
314      * @param val1
315      * @param val2
316      * @return String
317      */

318     public String JavaDoc strutsUrlHelper1( boolean useIds, boolean isAction,
319             String JavaDoc path, String JavaDoc val1, String JavaDoc val2, Hashtable JavaDoc params) {
320         if (useIds) {
321             if (mFolder != null) {
322                 params.put(RequestConstants.FOLDER_ID,
323                         mFolder.getId());
324             }
325             if (mWebsite != null) {
326                 params.put(RequestConstants.WEBLOG, mWebsite.getHandle());
327             }
328         }
329         
330         if (OldStringUtils.isNotEmpty(val1) && !val1.equals("null")) {
331             params.clear();
332             params.put("weblog", val1);
333         }
334         
335         String JavaDoc returnUrl = "";
336         try {
337             if (isAction) {
338                 returnUrl = RequestUtils.computeURL( mPageContext,
339                         path, null, null, null, params, null, false);
340             } else {
341                 returnUrl = RequestUtils.computeURL( mPageContext,
342                         null, path, null, null, params, null, false);
343             }
344         } catch (MalformedURLException JavaDoc mue) {
345             mLogger.warn("RollerRequest.strutsUrlHelper exception: ", mue);
346             returnUrl = "<span class=\"error\">ERROR generating link</span>";
347         }
348         return returnUrl;
349     }
350     
351     
352     /**
353      * Pass the String through any PagePlugins that have been
354      * assigned to the PageHelper, as selected by the Entry.
355      *
356      * @param entry Entry being rendered.
357      * @param str String to which plugins are to be applied.
358      * @return Result of applying plugins to str.
359      */

360     public String JavaDoc renderPlugins(WeblogEntryDataWrapper entry, String JavaDoc str) {
361         String JavaDoc ret = str;
362         mLogger.debug("Applying page plugins to string");
363         
364         if (mPagePlugins != null) {
365             List JavaDoc entryPlugins = entry.getPluginsList();
366             // if no Entry plugins, don't bother looping.
367
if (entryPlugins != null && !entryPlugins.isEmpty()) {
368                 
369                 // now loop over mPagePlugins, matching
370
// against Entry plugins (by name):
371
// where a match is found render Plugin.
372
Iterator JavaDoc iter = mPagePlugins.keySet().iterator();
373                 while (iter.hasNext()) {
374                     String JavaDoc key = (String JavaDoc)iter.next();
375                     if (entryPlugins.contains(key)) {
376                         WeblogEntryPlugin pagePlugin = (WeblogEntryPlugin)mPagePlugins.get(key);
377                         try {
378                             ret = pagePlugin.render(entry.getPojo(), ret);
379                         } catch (Throwable JavaDoc t) {
380                             mLogger.error("ERROR from plugin: " + pagePlugin.getName(), t);
381                         }
382                     }
383                 }
384             }
385         }
386         
387         return ret;
388     }
389     
390     
391     /**
392      * This method used to return an array of supported locales based on some
393      * of the old i18n work done in Roller, however, as of Roller 3.0 there is
394      * no longer a list of supported languages. The languages available to a
395      * weblog are unbounded and are purely determined by the weblog author.
396      *
397      * This method always returns null.
398      */

399     public Locale JavaDoc[] getSupportedLanguages() {
400         return null;
401     }
402     
403     
404     /**
405      * @return relative URL to page, starting with /username
406      */

407     public String JavaDoc getPathInfo() {
408         String JavaDoc pathInfo = mRequest.getPathInfo();
409         if(pathInfo == null) {
410             pathInfo = "";
411         }
412         
413         return pathInfo;
414     }
415     
416     
417     public String JavaDoc getCommentAuthenticatorHtml() {
418         // deprecated, does nothing now
419
return "";
420     }
421     
422 }
423
Popular Tags