roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject svn commit: r398712 [21/32] - in /incubator/roller/trunk/src/org/apache: ./ roller/ roller/business/ roller/business/hibernate/ roller/business/referrers/ roller/business/runnable/ roller/business/search/ roller/business/search/operations/ roller/busin...
Date Mon, 01 May 2006 22:23:34 GMT
Added: incubator/roller/trunk/src/org/apache/roller/presentation/velocity/FlavorServlet.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/apache/roller/presentation/velocity/FlavorServlet.java?rev=398712&view=auto
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/presentation/velocity/FlavorServlet.java (added)
+++ incubator/roller/trunk/src/org/apache/roller/presentation/velocity/FlavorServlet.java Mon May  1 15:23:02 2006
@@ -0,0 +1,174 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+*  contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.roller.presentation.velocity;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.velocity.Template;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.servlet.VelocityServlet;
+import org.apache.roller.RollerException;
+import org.apache.roller.presentation.RollerRequest;
+import java.io.IOException;
+import java.util.Date;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.jsp.JspFactory;
+import javax.servlet.jsp.PageContext;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.roller.model.Roller;
+import org.apache.roller.model.RollerFactory;
+import org.apache.roller.model.UserManager;
+import org.apache.roller.model.WeblogManager;
+
+/////////////////////////////////////////////////////////////////////////////
+/**
+ * <p>Responsible for rendering RSS feeds and other "flavors" of output for a
+ * weblog.</p>
+ *
+ * <p>If Servlet is mapped to <code>/rss</code> and user has defined
+ * an RSS override page (i.e. a page named "_rss"), then that Velocity
+ * template will be used for rendering.</p>
+ *
+ * <p>If there is a request parameter named "flavor", then the Velocity
+ * template specified by that parameter will be used for rendering. For
+ * example if the flavor is "rss092" then the template at classpath
+ * "/flavors/rss092.vm" will be used for rendering.</p>
+ *
+ * <p>Otherwise, the template /flavors/rss.vm" will be used for rendering.</p>
+ *
+ * <p>Assumes that If-Modified-Since has already been handled.</p>
+ *
+ * @author David M Johnson
+ *
+ * @web.servlet name="RssServlet"
+ * @web.servlet-mapping url-pattern="/rss/*"
+ * @web.servlet-mapping url-pattern="/atom/*"
+ * @web.servlet-mapping url-pattern="/flavor/*"
+ */
+public class FlavorServlet extends VelocityServlet {
+    
+    static final long serialVersionUID = -2720532269434186051L;
+    
+    private static Log mLogger = LogFactory.getLog(FlavorServlet.class);
+    
+    
+    public Template handleRequest(HttpServletRequest request,
+                                HttpServletResponse response, 
+                                Context ctx) 
+            throws IOException {
+        
+        RollerRequest rreq = null;
+        Template outty = null;
+        
+        // first off lets parse the incoming request and validate it
+        try {
+            PageContext pageContext =
+                    JspFactory.getDefaultFactory().getPageContext(
+                    this, request,  response, "", true, 8192, true);
+            rreq = RollerRequest.getRollerRequest(pageContext);
+            
+            // This is an ugly hack to fix the following bug:
+            // ROL-547: "Site wide RSS feed is your own if you are logged in"
+            String[] pathInfo = StringUtils.split(rreq.getPathInfo(),"/");
+            if (pathInfo.length < 1) {
+                // If website not specified in URL, set it to null
+                rreq.setWebsite(null);
+            }
+        } catch (RollerException e) {
+            
+            // An error initializing the request is considered to be a 404
+            response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            request.setAttribute("DisplayException", e);
+            
+            return null;
+        }
+        
+        
+        // request appears to be valid, lets render
+        try {
+            // get update time before loading context
+            // TODO: this should really be handled elsewhere
+            WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
+            String catname = request.getParameter(RollerRequest.WEBLOGCATEGORYNAME_KEY);
+            Date updateTime = wmgr.getWeblogLastPublishTime(rreq.getWebsite(), catname);
+            request.setAttribute("updateTime", updateTime);
+            
+            ContextLoader.setupContext(ctx, rreq, response);
+            
+            String useTemplate;
+            PageModel pageModel = (PageModel)ctx.get("pageModel");
+            if (request.getServletPath().endsWith("rss")) {
+                if (pageModel.getPageByName("_rss") != null)
+                    // If the request specified the "/rss" mapping and the
+                    // user has defined an RSS override page, we will use that.
+                    useTemplate = pageModel.getPageByName("_rss").getId();
+                else
+                    useTemplate = "/flavors/rss.vm";
+            } else if (request.getServletPath().endsWith("atom")) {
+                if (pageModel.getPageByName("_atom") != null)
+                    // If the request specified the "/atom" mapping and the
+                    // user has defined an Atom override page, we will use that.
+                    useTemplate = pageModel.getPageByName("_atom").getId();
+                else
+                    useTemplate = "/flavors/atom.vm";
+            } else if (request.getParameter("flavor") != null) {
+                // If request specifies a "flavor" then use that.
+                String flavor = request.getParameter("flavor");
+                useTemplate = "/flavors/" + flavor + ".vm";
+            } else {
+                // Fall through to default RSS page template.
+                useTemplate = "/flavors/rss.vm";
+            }
+            
+            outty = getTemplate(useTemplate);
+            
+        } catch(ResourceNotFoundException rnfe) {
+            
+            response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            request.setAttribute("DisplayException", rnfe);
+            mLogger.warn("ResourceNotFound: "+ request.getRequestURL());
+            mLogger.debug(rnfe);
+        } catch(Exception e) {
+            
+            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+            request.setAttribute("DisplayException", e);
+            mLogger.error("Unexpected exception", e);
+        }
+        
+        return outty;
+    }
+    
+    //------------------------------------------------------------------------
+    /**
+     * Handle error in Velocity processing.
+     */
+    protected void error( HttpServletRequest req, HttpServletResponse res,
+            Exception e) throws ServletException, IOException {
+        
+        // this means there was an exception outside of the handleRequest()
+        // method which seems to always be some variant of SocketException
+        // so we just ignore it
+        
+        // make sure anyone downstream knows about the exception
+        req.setAttribute("DisplayException", e);
+    }
+}
+

Added: incubator/roller/trunk/src/org/apache/roller/presentation/velocity/FoafServlet.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/apache/roller/presentation/velocity/FoafServlet.java?rev=398712&view=auto
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/presentation/velocity/FoafServlet.java (added)
+++ incubator/roller/trunk/src/org/apache/roller/presentation/velocity/FoafServlet.java Mon May  1 15:23:02 2006
@@ -0,0 +1,160 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+*  contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.roller.presentation.velocity;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.velocity.Template;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.servlet.VelocityServlet;
+import org.apache.roller.RollerException;
+import org.apache.roller.model.UserManager;
+import org.apache.roller.pojos.UserData;
+import org.apache.roller.pojos.WebsiteData;
+import org.apache.roller.presentation.RollerContext;
+import org.apache.roller.presentation.RollerRequest;
+import org.apache.roller.util.Utilities;
+import java.io.IOException;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.roller.model.Roller;
+import org.apache.roller.model.RollerFactory;
+import org.apache.roller.presentation.RollerSession;
+
+
+/**
+ * <p>ROLLER_2.0: FOAF is broken in Roller 2.0.
+ * Responsible for rendering FOAF feed.  This servlet requires
+ * that the RequestFilter is in place for it, and should also
+ * have the IfModifiedFilter configured.</p>
+ *
+ * <p>Resources:<br />
+ * <a href="http://xmlns.com/foaf/0.1/"
+ *     >FOAF Vocabulary Specification</a><br />
+ * <a href="http://www.xml.com/lpt/a/2004/02/04/foaf.html"
+ *     >An Introduction to FOAF</a></p>
+ *
+ * <p>FOAF Autodiscovery: <br />
+ * <link rel="meta" type="application/rdf+xml" title="FOAF"
+ *     href="$absBaseURL/foaf/$userName" /> </p>
+ *
+ * @author Lance Lavandowska
+ *
+ * @web.servlet name="FoafServlet"
+ * @web.servlet-mapping url-pattern="/foaf/*"
+ */
+public class FoafServlet extends VelocityServlet {
+    
+    static final long serialVersionUID = -1893244416537298619L;
+    
+    private static Log mLogger = LogFactory.getLog(FoafServlet.class);
+    
+    
+    /**
+     * This Velocity servlet does not make use of ContextLoader and associated
+     * classes (as do FlavorServlet and PageServlet) because that is more
+     * work than is really necessary.  It implements its own setupContext()
+     * to load necessary values into the Velocity Context.
+     *
+     * @param ctx
+     * @param rreq
+     * @throws RollerException
+     */
+    public Template handleRequest(HttpServletRequest request,
+            HttpServletResponse response, Context ctx) {
+        
+        RollerRequest rreq = null;
+        try {
+            rreq = RollerRequest.getRollerRequest(request, getServletContext());
+        } catch (RollerException e) {
+            // An error initializing the request is considered to be a 404
+            mLogger.debug("RollerRequest threw Exception", e);
+            
+            try {
+                response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            } catch (IOException e1) {
+                mLogger.debug("IOException sending error", e);
+            }
+            
+            return null;
+        }
+        
+        try {
+            setupContext(ctx, rreq);
+            
+            response.setContentType("application/rdf+xml");
+            return getTemplate("/flavors/foaf.vm");
+            
+        } catch (Exception e) {
+            mLogger.error("ERROR in FoafServlet", e);
+        }
+        
+        return null;
+    }
+    
+    
+    /**
+     * @param ctx
+     */
+    private void setupContext(Context ctx, RollerRequest rreq) throws RollerException {
+        
+        HttpServletRequest request = rreq.getRequest();
+        RollerContext rollerCtx = RollerContext.getRollerContext( );
+        RollerSession rses = RollerSession.getRollerSession(request);
+        Roller roller = RollerFactory.getRoller();
+        // ROLLER_2.0 : figure out how to fix FOAF servlet (does anybody use it?)
+        // UserData user =
+        //   roller.getUserManager().getUser(userName, Boolean.TRUE);
+        // ctx.put("fullName", user.getFullName()); // name for FlavorServlet compatibility
+        
+        // foaf:homepage to equal base URL for user
+        //String homepage = Utilities.escapeHTML(
+        //rollerCtx.getAbsoluteContextUrl(request) +
+        //"/page/" + user.getUserName() );
+        //ctx.put("websiteURL", homepage); // name for FlavorServlet compatibility
+        
+        // see if foaf:weblog is different Page
+        WebsiteData website = rreq.getWebsite();
+        UserManager usrMgr = RollerFactory.getRoller().getUserManager();
+        org.apache.roller.pojos.Template weblog = website.getPageByName("Weblog");
+        
+        // if weblog != homepage, add to context
+        if (weblog != null && !website.getDefaultPageId().equals(weblog.getId())) {
+            //String weblogUrl = Utilities.escapeHTML(
+            //rollerCtx.getAbsoluteContextUrl(request) +
+            //"/page/" + user.getUserName() +
+            //"/" + weblog.getLink() );
+            //ctx.put("weblog", weblogUrl);
+        }
+        
+        // use SHA1 encrypted email address, including mailto: prefix
+        //String shaEmail = Utilities.encodePassword(
+        //"mailto:" + user.getEmailAddress(), "SHA");
+        //ctx.put("shaEmail", shaEmail);
+    }
+    
+    
+    /**
+     * Handle error in Velocity processing.
+     */
+    protected void error( HttpServletRequest req, HttpServletResponse res,
+            Exception e) throws ServletException, IOException {
+        mLogger.warn("ERROR in FoafServlet",e);
+    }
+}

Added: incubator/roller/trunk/src/org/apache/roller/presentation/velocity/LanguageServlet.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/apache/roller/presentation/velocity/LanguageServlet.java?rev=398712&view=auto
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/presentation/velocity/LanguageServlet.java (added)
+++ incubator/roller/trunk/src/org/apache/roller/presentation/velocity/LanguageServlet.java Mon May  1 15:23:02 2006
@@ -0,0 +1,136 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+*  contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+/*
+ * Filename: LanguageServlet.java
+ *
+ * Created on 02-May-04
+ */
+package org.apache.roller.presentation.velocity;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.struts.Globals;
+import org.apache.velocity.Template;
+import org.apache.velocity.context.Context;
+import org.apache.roller.presentation.LanguageUtil;
+import org.apache.roller.presentation.RollerContext;
+
+
+/**
+ * Changes the language of the current Locale to the language specified
+ * by the user. The new language must be supported by Roller.
+ *
+ * And new supported languages to the web.servlet-init-param value. Make sure you add language-only
+ * values at the end of a chain. So "en_US,en" instead of "en,en_US". And no spaces.
+ *
+ * @web.servlet name="LanguageServlet" load-on-startup="10"
+ * @web.servlet-init-param name="org.apache.roller.presentation.supported.languages" value="en,nl,zh_cn,zh_tw,vi"
+ *
+ * @web.servlet-mapping url-pattern="/language/*"
+ *
+ * @author <a href="mailto:molen@mail.com">Jaap van der Molen</a>
+ * @version $Revision: 1.8 $
+ */
+public class LanguageServlet extends PageServlet {
+    
+    static final long serialVersionUID = -6548723098429557183L;
+    
+    private static Log mLogger = LogFactory.getLog(LanguageServlet.class);
+    
+    
+    /**
+     * @see org.apache.roller.presentation.velocity.BasePageServlet#init(javax.servlet.ServletConfig)
+     */
+    public void init(ServletConfig config) throws ServletException {
+        
+        super.init(config);
+        
+        // load supported languages
+        ServletContext ctx = config.getServletContext();
+        String supportedLanguages =
+                config.getInitParameter(LanguageUtil.SUPPORTED_LANGUAGES);
+        if (supportedLanguages != null
+                && supportedLanguages.trim().length() > 0) {
+            // extract langauges
+            ctx.setAttribute(
+                    LanguageUtil.SUPPORTED_LANGUAGES,
+                    LanguageUtil.extractLanguages(supportedLanguages));
+        }
+    }
+    
+    
+    /**
+     * @see org.apache.roller.presentation.velocity.BasePageServlet#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.velocity.context.Context)
+     */
+    public Template handleRequest(HttpServletRequest request,
+                                HttpServletResponse response,
+                                Context ctx) 
+            throws Exception {
+        
+        mLogger.debug("Processing language change...");
+        ServletContext servletContext = RollerContext.getServletContext();
+        
+        Locale[] supportedLanguages =
+                LanguageUtil.getSupportedLanguages(servletContext);
+        
+        if (supportedLanguages == null || supportedLanguages.length == 0) {
+            // add error message
+            ctx.put("languageError", "Unable to switch language: no supported languages defined.");
+            // proceed with request serving
+            return super.handleRequest(request, response, ctx);
+        }
+        
+        String newLang = request.getParameter("language");
+        mLogger.debug("New language in Request: " + newLang);
+        if (newLang == null || newLang.length() == 0) {
+            // add error message
+            ctx.put("languageError", "Unable to switch language: no new language specified.");
+            // proceed with request serving
+            return super.handleRequest(request, response, ctx);
+        }
+        
+        Locale newLocale = LanguageUtil.createLocale(newLang);
+        
+        // verify if new language is supported
+        if (!LanguageUtil.isSupported(newLocale, servletContext)) {
+            // add error message
+            ctx.put("languageError", "Unable to switch language: new language '"+newLang+"' is not supported.");
+            // proceed with request serving
+            return super.handleRequest(request, response, ctx);
+        }
+        
+        // by now, all should be fine: change Locale
+        HttpSession session = request.getSession();
+        session.setAttribute(Globals.LOCALE_KEY, newLocale);
+        mLogger.debug("Changed language to: " + newLocale);
+        
+        // proceed with request serving
+        return super.handleRequest(request, response, ctx);
+    }
+    
+}

Added: incubator/roller/trunk/src/org/apache/roller/presentation/velocity/MathCommentAuthenticator.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/apache/roller/presentation/velocity/MathCommentAuthenticator.java?rev=398712&view=auto
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/presentation/velocity/MathCommentAuthenticator.java (added)
+++ incubator/roller/trunk/src/org/apache/roller/presentation/velocity/MathCommentAuthenticator.java Mon May  1 15:23:02 2006
@@ -0,0 +1,115 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+*  contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.roller.presentation.velocity;
+
+import java.util.ResourceBundle;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.velocity.context.Context;
+import org.apache.roller.pojos.CommentData;
+
+
+/**
+ * Asks the commenter to answer a simple math question.
+ *
+ * @author David M Johnson
+ */
+public class MathCommentAuthenticator implements CommentAuthenticator {
+    
+    private transient ResourceBundle bundle =
+            ResourceBundle.getBundle("ApplicationResources");
+    
+    private static Log mLogger = LogFactory.getLog(MathCommentAuthenticator.class);
+    
+    
+    public String getHtml(Context context, 
+                    HttpServletRequest request, 
+                    HttpServletResponse response) {
+
+        String answer = "";
+        
+        HttpSession session = request.getSession(true);
+        if (session.getAttribute("mathAnswer") == null) {
+            // starting a new test
+            int value1 = (int)(Math.random()*10.0);
+            int value2 = (int)(Math.random()*100.0);
+            int sum = value1 + value2;
+            session.setAttribute("mathValue1", new Integer(value1));
+            session.setAttribute("mathValue2", new Integer(value2));
+            session.setAttribute("mathAnswer", new Integer(sum));
+        } else {
+            // preserve user's answer
+            answer = request.getParameter("answer");
+            answer = (answer == null) ? "" : answer;
+        }
+        
+        // pull existing values out of session
+        Integer value1o = (Integer)request.getSession().getAttribute("mathValue1");
+        Integer value2o = (Integer)request.getSession().getAttribute("mathValue2");
+        
+        StringBuffer sb = new StringBuffer();
+        
+        sb.append("<p>");
+        sb.append(bundle.getString("comments.mathAuthenticatorQuestion"));
+        sb.append("</p><p>");
+        sb.append(value1o);
+        sb.append(" + ");
+        sb.append(value2o);
+        sb.append(" = ");
+        sb.append("<input name=\"answer\" value=\"");
+        sb.append(answer);
+        sb.append("\" /></p>");
+        
+        return sb.toString();
+    }
+    
+    
+    public boolean authenticate(CommentData comment, HttpServletRequest request) {
+        
+        boolean authentic = false;
+        
+        HttpSession session = request.getSession(false);
+        String answerString = request.getParameter("answer");
+        
+        if (answerString != null && session != null) {
+            try {
+                int answer = Integer.parseInt(answerString);
+                Integer sum = (Integer) session.getAttribute("mathAnswer");
+                
+                if (sum != null && answer == sum.intValue()) {
+                    authentic = true;
+                    session.removeAttribute("mathAnswer");
+                    session.removeAttribute("mathValue1");
+                    session.removeAttribute("mathValue2");
+                }
+            } catch (NumberFormatException ignored) {
+                // ignored ... someone is just really bad at math
+            } catch (Exception e) {
+                // unexpected
+                mLogger.error(e);
+            }
+        }
+        
+        return authentic;
+    }
+    
+}
+

Added: incubator/roller/trunk/src/org/apache/roller/presentation/velocity/PageHelper.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/apache/roller/presentation/velocity/PageHelper.java?rev=398712&view=auto
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/presentation/velocity/PageHelper.java (added)
+++ incubator/roller/trunk/src/org/apache/roller/presentation/velocity/PageHelper.java Mon May  1 15:23:02 2006
@@ -0,0 +1,549 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+*  contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.roller.presentation.velocity;
+import java.net.MalformedURLException;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Vector;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.jsp.PageContext;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.struts.Globals;
+import org.apache.struts.util.RequestUtils;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.context.Context;
+import org.apache.roller.RollerException;
+import org.apache.roller.model.PagePlugin;
+import org.apache.roller.model.PagePluginManager;
+import org.apache.roller.model.Roller;
+import org.apache.roller.model.RollerFactory;
+import org.apache.roller.pojos.WeblogEntryData;
+import org.apache.roller.pojos.WebsiteData;
+import org.apache.roller.pojos.wrapper.RefererDataWrapper;
+import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
+import org.apache.roller.presentation.LanguageUtil;
+import org.apache.roller.presentation.RollerContext;
+import org.apache.roller.presentation.RollerRequest;
+import org.apache.roller.presentation.RollerSession;
+import org.apache.roller.presentation.tags.calendar.CalendarModel;
+import org.apache.roller.presentation.tags.calendar.CalendarTag;
+import org.apache.roller.presentation.tags.menu.EditorNavigationBarTag;
+import org.apache.roller.presentation.tags.menu.MenuTag;
+import org.apache.roller.presentation.weblog.tags.BigWeblogCalendarModel;
+import org.apache.roller.presentation.weblog.tags.WeblogCalendarModel;
+import org.apache.roller.util.StringUtils;
+
+/**
+ * Provides assistance to VelociMacros, filling in where Velocity falls.
+ * 
+ * @author llavandowska
+ * @author David M Johnson
+ * 
+ */
+public class PageHelper
+{
+    private static Log mLogger = 
+       LogFactory.getFactory().getInstance(PageHelper.class);
+    
+    private Context              mVelocityContext = null;
+    private PageContext          mPageContext = null;
+    private HttpServletResponse  mResponse = null;     
+    private RollerRequest        mRollerReq = null;  
+    private Map                  mPagePlugins = null;  // Plugins keyed by name   
+    private WebsiteData          mWebsite = null;
+    
+    //------------------------------------------------------------------------
+    
+    /**
+     * Initialize VelocityHelper, setting the variables it will be hiding from
+     * the Velocimacros.
+     */
+    public PageHelper( 
+            HttpServletRequest request, 
+            HttpServletResponse response, 
+            Context ctx) throws RollerException
+    {
+        mVelocityContext = ctx;
+        mRollerReq = RollerRequest.getRollerRequest(request);
+        mResponse = response;
+        if (mRollerReq != null) 
+        {
+            mPageContext = mRollerReq.getPageContext();
+            if (request.getAttribute(RollerRequest.OWNING_WEBSITE) != null)
+            {
+                mWebsite = (WebsiteData)
+                    request.getAttribute(RollerRequest.OWNING_WEBSITE);
+            }
+            else if (mRollerReq.getWebsite() != null )
+            {
+                mWebsite = mRollerReq.getWebsite();
+            }
+        }
+        if (mVelocityContext == null) mVelocityContext = new VelocityContext();
+        Roller roller = RollerFactory.getRoller(); 
+        PagePluginManager ppmgr = roller.getPagePluginManager();
+        mPagePlugins = ppmgr.createAndInitPagePlugins(
+                mWebsite, 
+                RollerContext.getRollerContext().getServletContext(),
+                RollerContext.getRollerContext().getAbsoluteContextUrl(),
+                mVelocityContext);
+    }
+       
+    //------------------------------------------------------------------------
+    
+    /**
+     * Another stupid helper method to make up for the shortcomings of Velocity.
+     * @return HashMap
+     */
+    public Hashtable addParam(String key, String value, Hashtable map)
+    {
+        if (map == null) map = new Hashtable();
+        if (key != null && value != null)
+            map.put(key, value);
+        return map;
+    }
+        
+    //------------------------------------------------------------------------
+    
+    /**
+     * Evaluates the String as a Velocimacro, returning the results.
+     *
+     * @deprecated shouldn't be used anymore because it's dangerous
+     * 
+     * @param str String
+     * @return String
+     */
+    public String evaluateString(String str)
+    {
+        // we no longer allow users to do this because it is dangerous
+        return str;
+    }
+   
+    /** Build the URL for editing an WeblogEntry **/
+    public String getEntryEditUrl(WeblogEntryDataWrapper entry)
+    {
+        Hashtable params = new Hashtable();
+        params.put( RollerRequest.WEBLOGENTRYID_KEY, entry.getId());
+        params.put( RollerRequest.ANCHOR_KEY,        entry.getAnchor());
+        if (mWebsite != null)
+        {    
+            params.put( RollerRequest.USERNAME_KEY,  mWebsite.getHandle());
+        }
+        try
+        {
+            return RequestUtils.computeURL( mPageContext,
+                "weblogEdit", null, null, null, params, null, false);
+        }
+        catch (MalformedURLException mue)
+        {
+            mLogger.warn("RollerRequest.editEntryUrl exception: ", mue);
+        }
+        return 
+           mRollerReq.getRequest().getContextPath() + "edtior/weblog.do?method=edit";
+    }
+    
+    //-------------------------------------------------------------------------
+    public String getToggleLinkbackDisplayHTML(RefererDataWrapper referer)
+    {
+        String ret = "";
+        String link = null;
+        try
+        {
+            RollerSession rollerSession = 
+                RollerSession.getRollerSession(mRollerReq.getRequest());
+            if ( mRollerReq.getWebsite() != null 
+              && rollerSession.isUserAuthorizedToAdmin(mRollerReq.getWebsite()))
+            {
+                Hashtable params = new Hashtable();
+                params.put( RollerRequest.REFERERID_KEY, referer.getId());
+                params.put( RollerRequest.WEBLOG_KEY, mWebsite.getHandle());
+                link = RequestUtils.computeURL( mPageContext,
+                    "toggleLinkback", null, null, null, params,null,false);
+                    
+                StringBuffer sb = new StringBuffer();
+                sb.append("[<a href=\"");
+                sb.append(link);
+                if ( referer.getVisible().booleanValue() )
+                {
+                    sb.append("\">Visible</a>] ");
+                }
+                else
+                {
+                    sb.append("\">Hidden</a>] ");
+                }
+                ret = sb.toString();
+            }
+        }
+        catch (Exception e)
+        {
+           // should never happen, but if it does:
+           mLogger.error("ERROR creating toggle-linkback URL",e);
+        }
+        
+        return ret;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    public boolean isUserAuthorizedToEdit()
+    {
+        try
+        {
+            RollerSession rses = 
+                RollerSession.getRollerSession(mRollerReq.getRequest());
+            if ( rses.getAuthenticatedUser() != null 
+                    && mRollerReq.getWebsite() != null)
+            {
+                return rses.isUserAuthorizedToAdmin(mRollerReq.getWebsite());
+            }
+        }
+        catch (Exception e)
+        {
+            mLogger.warn("PageHelper.isUserAuthorizedToEdit)", e);
+        }
+        return false;
+    }
+    
+    //------------------------------------------------------------------------
+    public void setContentType( String type )
+    {
+        mResponse.setContentType(type);
+    }
+
+    //------------------------------------------------------------------------
+    /** 
+     * Display big weblog calendar, well suited for an archive page.
+     * @return HTML for calendar.
+     */
+    public String showBigWeblogCalendar()
+    {
+        return showWeblogCalendar(true, null);
+    }
+        
+    //------------------------------------------------------------------------
+    
+    /** 
+     * Call hybrid EditorNavBarTag to render editor navbar.
+     * @param vertical True for vertical navbar.
+     * @return String HTML for navbar.
+     */
+    public String showEditorNavBar(boolean vertical)
+    {
+        EditorNavigationBarTag editorTag = new EditorNavigationBarTag();
+        editorTag.setPageContext(mPageContext);
+        if ( vertical )
+        {
+            editorTag.setView("/navbar-vertical.vm");
+        }
+        else
+        {
+            editorTag.setView("/navbar-horizontal.vm");
+        }
+        editorTag.setModel("editor-menu.xml");
+        return editorTag.emit();
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** 
+     * Call hybrid EditorNavBarTag to render editor navbar.
+     * @param model Name of XML file in WEB-INF that contains XML for menu.
+     * @param template Name of Velocity template in classpath to display menu.
+     * @return String HTML for menu.
+     */
+    public String showMenu(String model, String template)
+    {
+        MenuTag menuTag = new MenuTag();
+        menuTag.setPageContext(mPageContext);
+        menuTag.setModel(model);
+        menuTag.setView(template);
+        return menuTag.emit();
+    }
+    
+    //------------------------------------------------- WeblogCalendar methods
+
+    /** 
+     * Display weblog calendar.
+     * @return HTML for calendar.
+     */
+    public String showWeblogCalendar()
+    {
+        return showWeblogCalendar(false, null);
+    }
+
+    //------------------------------------------------------------------------
+    /** 
+     * Weblog calendar display implementation.
+     * @param big Show big archive style calendar.
+     * @return HTML for calendar.
+     */
+    public String showWeblogCalendar( boolean big, String cat )
+    {
+        if (PageModel.VELOCITY_NULL.equals(cat)) cat = null;
+        String ret = null;
+        try
+        {
+            HttpServletRequest request =
+                (HttpServletRequest)mPageContext.getRequest();
+            HttpServletResponse response =
+                (HttpServletResponse)mPageContext.getResponse();
+
+            String selfUrl = null;
+            String pageLink = mRollerReq.getPageLink();
+            if ( pageLink != null )
+            {
+                selfUrl = request.getContextPath() + "/page/" 
+                          + mWebsite.getHandle() + "/"+pageLink;
+            }
+            else
+            {
+                selfUrl = request.getContextPath()+"/page/" + mWebsite.getHandle();
+            }
+
+            // setup weblog calendar model
+            CalendarModel model = null;
+            if ( big )
+            {
+                model = new BigWeblogCalendarModel(
+                           mRollerReq, response, selfUrl, cat);
+            }
+            else
+            {
+                model = new WeblogCalendarModel(
+                            mRollerReq, response, selfUrl, cat);
+            }
+
+            // save model in JSP page context so CalendarTag can find it
+            mPageContext.setAttribute("calendarModel",model);
+
+            // Create and setup calendar tag
+            CalendarTag calTag = new CalendarTag();
+            calTag.setPageContext(mPageContext);
+            calTag.setName("calendar");
+            calTag.setModel("calendarModel");
+            //calTag.setLocale(mRollerReq.getWebsite().getLocaleInstance());
+            calTag.setLocale(LanguageUtil.getViewLocale(request));
+            //calTag.setTimeZone(mRollerReq.getWebsite().getTimeZoneInstance());
+            if ( big )
+            {
+                calTag.setClassSuffix("Big");
+            }
+            ret = calTag.emit();
+        }
+        catch (Exception e)
+        {
+            mLogger.error("Unexpected exception",e);
+        }
+        return ret;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /**
+     * Convenience method, contrived helper for Velocity.
+     * @param useIds
+     * @param isAction
+     * @param path
+     * @param val1
+     * @param val2
+     * @return String
+     */
+    public String strutsUrlHelper( boolean useIds, boolean isAction, 
+        String path, String val1, String val2)
+    {
+        Hashtable params = new Hashtable();
+        return strutsUrlHelper1( useIds, isAction, path, val1, val2, params);
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /**
+     * Very contrived helper method for Velocimacros generating Struts links.
+     * This is really only of use to the showNavBar macro.
+     * @param useIds
+     * @param isAction
+     * @param path
+     * @param val1
+     * @param val2
+     * @return String
+     */
+    public String strutsUrlHelper1( boolean useIds, boolean isAction, 
+        String path, String val1, String val2, Hashtable params)
+    {
+        if (useIds)
+        {
+            if (mRollerReq.getFolder() != null) 
+            {
+                params.put(RollerRequest.FOLDERID_KEY,
+                mRollerReq.getFolder().getId());
+            } 
+            if (mWebsite != null)
+            {
+                params.put(RollerRequest.WEBLOG_KEY, mWebsite.getHandle());
+            }
+        }
+        
+        if (StringUtils.isNotEmpty(val1) && !val1.equals("null"))
+        {
+            params.clear();
+            params.put("weblog", val1);
+        }
+        
+        String returnUrl = "";
+        try
+        {
+            if (isAction)
+            {
+                returnUrl = RequestUtils.computeURL( mPageContext,
+                                path, null, null, null, params, null, false);
+            }
+            else
+            {
+                returnUrl = RequestUtils.computeURL( mPageContext,
+                                null, path, null, null, params, null, false);
+            }
+        }
+        catch (MalformedURLException mue)
+        {
+            mLogger.warn("RollerRequest.strutsUrlHelper exception: ", mue);
+            returnUrl = "<span class=\"error\">ERROR generating link</span>";
+        }
+        return returnUrl;
+    }
+        
+    /**
+     * Pass the String through any PagePlugins that have been
+     * assigned to the PageHelper, as selected by the Entry.
+     * 
+     * @param entry Entry being rendered.
+     * @param str   String to which plugins are to be applied.
+     * @return      Result of applying plugins to str.
+     */
+    public String renderPlugins(WeblogEntryDataWrapper entry, String str)
+    {
+        String ret = str;
+        mLogger.debug("Applying page plugins to string");
+                
+        if (mPagePlugins != null)
+        {
+            List entryPlugins = entry.getPluginsList();
+            // if no Entry plugins, don't bother looping.
+            if (entryPlugins != null && !entryPlugins.isEmpty())
+            {    
+                // need to do this to tell ReadMore not to do its job
+                // if we are in the "view one Entry" page.
+                boolean singleEntry = false;
+                if (mRollerReq == null || mRollerReq.getWeblogEntry() != null)
+                {
+                    singleEntry = true;
+                }
+                
+                // now loop over mPagePlugins, matching
+                // against Entry plugins (by name):
+                // where a match is found render Plugin.
+                Iterator iter = mPagePlugins.keySet().iterator();
+                while (iter.hasNext())
+                {
+                    String key = (String)iter.next();
+                    if (entryPlugins.contains(key))
+                    {
+                        PagePlugin pagePlugin = (PagePlugin)mPagePlugins.get(key);
+                        if (!(singleEntry && pagePlugin.getSkipOnSingleEntry())) { 
+                            try {
+                                ret = pagePlugin.render(entry.getPojo(), ret);
+                            } catch (Throwable t) {
+                                mLogger.error("ERROR from plugin: " + pagePlugin.getName(), t);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        
+        return ret;
+    }
+    
+    /**
+     * This method returns an array of Locales for each supported
+     * language available, with the exeception of the language of the 
+     * current locale, if that language is supported.
+     * 
+     * So, if English and Dutch are supported, and the current Locale is Dutch,
+     * only English is returned. If the current Locale is Spanish, both English and Dutch are
+     * returned.
+     *    
+     * @return
+     */
+    public Locale[] getSupportedLanguages()
+    {
+        Locale currentLocale =
+            (Locale) mPageContext.getSession().getAttribute(Globals.LOCALE_KEY);
+        if (currentLocale==null) 
+        {
+            currentLocale = mPageContext.getRequest().getLocale();
+        }
+            
+        Locale[] supportedLanguages =
+            LanguageUtil.getSupportedLanguages(mPageContext.getServletContext());
+        if (supportedLanguages==null) {
+            return null;
+        }
+        
+        // filter out the current selected language
+        Vector result = new Vector();
+        for (int i = 0; i < supportedLanguages.length; i++)
+        {
+            if (currentLocale == null
+                || (!supportedLanguages[i].equals(currentLocale)
+                && !supportedLanguages[i].equals(
+                    new Locale(currentLocale.getLanguage())))
+                )
+            {
+                result.add(supportedLanguages[i]);
+            }
+        }
+        return (Locale[]) result.toArray(new Locale[result.size()]);
+    }
+
+    /**
+     * @return relative URL to page, starting with /username
+     */ 
+    public String getPathInfo() 
+    {
+        return mRollerReq.getPathInfo();
+    }
+    
+    public String getCommentAuthenticatorHtml()
+    {
+        /* no longer used -- Allen G
+        RollerContext rctx = 
+            RollerContext.getRollerContext(mRollerReq.getRequest());
+        return rctx.getCommentAuthenticator().getHtml(
+            mVelocityContext, mRollerReq.getRequest(), mResponse);
+         */
+        
+        return "";
+    }
+}

Added: incubator/roller/trunk/src/org/apache/roller/presentation/velocity/PageModel.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/apache/roller/presentation/velocity/PageModel.java?rev=398712&view=auto
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/presentation/velocity/PageModel.java (added)
+++ incubator/roller/trunk/src/org/apache/roller/presentation/velocity/PageModel.java Mon May  1 15:23:02 2006
@@ -0,0 +1,827 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+*  contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.roller.presentation.velocity;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.TimeZone;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.RollerException;
+import org.apache.roller.config.RollerRuntimeConfig;
+import org.apache.roller.model.BookmarkManager;
+import org.apache.roller.model.RefererManager;
+import org.apache.roller.model.RollerFactory;
+import org.apache.roller.pojos.Template;
+import org.apache.roller.model.UserManager;
+import org.apache.roller.model.WeblogManager;
+import org.apache.roller.pojos.CommentData;
+import org.apache.roller.pojos.FolderData;
+import org.apache.roller.pojos.RefererData;
+import org.apache.roller.pojos.WeblogCategoryData;
+import org.apache.roller.pojos.WeblogEntryData;
+import org.apache.roller.pojos.WebsiteData;
+import org.apache.roller.pojos.wrapper.CommentDataWrapper;
+import org.apache.roller.pojos.wrapper.FolderDataWrapper;
+import org.apache.roller.pojos.wrapper.RefererDataWrapper;
+import org.apache.roller.pojos.wrapper.TemplateWrapper;
+import org.apache.roller.pojos.wrapper.WeblogCategoryDataWrapper;
+import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
+import org.apache.roller.pojos.wrapper.WebsiteDataWrapper;
+import org.apache.roller.presentation.RollerRequest;
+import org.apache.roller.presentation.RollerSession;
+import org.apache.roller.util.DateUtil;
+import org.apache.roller.util.StringUtils;
+
+/**
+ * Provides Roller page templates with access to Roller domain model objects.
+ *
+ * @author llavandowska
+ * @author David M Johnson
+ */
+public class PageModel {
+    public final static String VELOCITY_NULL = "nil";
+    
+    protected static Log mLogger =
+            LogFactory.getFactory().getInstance(PageModel.class);
+    
+    private BookmarkManager      mBookmarkMgr = null;
+    private WeblogManager        mWeblogMgr = null;
+    private UserManager          mUserMgr = null;
+    private RefererManager       mRefererMgr = null;
+    
+    private Map                  mCategories = new HashMap();
+    private HashMap              mPageMap = new HashMap();
+    private RollerRequest        mRollerReq = null;
+    private String               mHandle = null;
+    private WebsiteData          mWebsite = null;
+    private WeblogEntryDataWrapper      mNextEntry = null;
+    private WeblogEntryDataWrapper      mPreviousEntry = null;
+    private WeblogEntryDataWrapper      mLastEntry = null;
+    private WeblogEntryDataWrapper      mFirstEntry = null;
+    
+    //------------------------------------------------------------------------
+    
+    /** init() must be called to complete construction */
+    public PageModel() {}
+    
+    /**
+     * Initialize PageModel and allow PageModel to initialized VelocityContext.
+     * @param rreq
+     * @param ctx
+     */
+    public void init(RollerRequest rreq) {
+        mRollerReq = rreq;
+        if ( rreq.getRequest().getAttribute(RollerRequest.OWNING_WEBSITE) != null) {
+            mWebsite = (WebsiteData)
+            rreq.getRequest().getAttribute(RollerRequest.OWNING_WEBSITE);
+            mHandle = mWebsite.getHandle();
+        } else if ( rreq.getWebsite() != null ) {
+            mWebsite = rreq.getWebsite();
+            mHandle = mWebsite.getHandle();
+        }
+        
+        try {
+            mBookmarkMgr = RollerFactory.getRoller().getBookmarkManager();
+            mRefererMgr  = RollerFactory.getRoller().getRefererManager();
+            mUserMgr     = RollerFactory.getRoller().getUserManager();
+            mWeblogMgr   = RollerFactory.getRoller().getWeblogManager();
+            
+            // Preload what we can for encapsulation.  What we cannot preload we
+            // will use the Managers later to fetch.
+            
+            // Get the pages, put into context & load map
+            if (mWebsite != null) {
+                // if we have website from RollerRequest, use it
+                mWebsite = rreq.getWebsite();
+                
+                // Get the pages, put into context & load map
+                List pages = mWebsite.getPages();
+                Iterator pageIter = pages.iterator();
+                while (pageIter.hasNext()) {
+                    Template page = (Template) pageIter.next();
+                    mPageMap.put(page.getName(), TemplateWrapper.wrap(page));
+                }
+            }
+        } catch (RollerException e) {
+            mLogger.error("PageModel Roller get*Manager Exception", e);
+        }
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates folder.getBookmarks() & sorting */
+    public Collection getBookmarks(FolderDataWrapper folder) {
+        Collection bookmarks = null;
+        if (folder != null) {
+            mLogger.debug("Getting bookmarks for folder : "+folder.getName());
+
+            // since we already have a wrapped pojo we know the output
+            // will be wrapped as well :)
+            bookmarks = folder.getBookmarks();
+
+            // TODO: need to setup new BookmarkWrapperComparator
+            //List mBookmarks = new ArrayList(bookmarks);
+            //Collections.sort( mBookmarks, new BookmarkComparator() );
+        }
+        return bookmarks;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Get top level bookmark folders. */
+    public Collection getTopLevelFolders() {
+        List tops = null;
+        try {
+            Collection mTops = mBookmarkMgr.getRootFolder(
+                    mUserMgr.getWebsiteByHandle(mHandle)).getFolders();
+            
+            // wrap pojos
+            tops = new ArrayList(mTops.size());
+            Iterator it = mTops.iterator();
+            int i=0;
+            while(it.hasNext()) {
+                tops.add(i, FolderDataWrapper.wrap((FolderData) it.next()));
+                i++;
+            }
+        } catch (RollerException e) {
+            tops = new ArrayList();
+        }
+        return tops;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Get number of approved non-spam comments for entry */
+    public int getCommentCount(String entryId) {
+        return getCommentCount(entryId, true, true);
+    }
+    
+    /** Get number of approved non-spam comments for entry */
+    public int getCommentCount(String entryId, boolean noSpam, boolean approvedOnly) {
+        try {
+            WeblogEntryData entry = mWeblogMgr.getWeblogEntry(entryId);
+            return entry.getComments(noSpam, approvedOnly).size();
+        } catch (RollerException alreadyLogged) {}
+        return 0;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Get comments for weblog entry specified by request */
+    public List getComments(WeblogEntryDataWrapper entry) {
+        return getComments(entry, true, true);
+    }
+    
+    /** Get comments for weblog entry specified by request */
+    public List getComments(WeblogEntryDataWrapper wrapper, boolean noSpam, boolean approvedOnly) {
+        WeblogEntryData entry = wrapper.getPojo();
+        List comments = new ArrayList();
+        List unwrappped = entry.getComments(noSpam, approvedOnly);
+        comments = new ArrayList(unwrappped.size());
+        Iterator it = unwrappped.iterator();
+        while(it.hasNext()) {
+            comments.add(CommentDataWrapper.wrap((CommentData)it.next()));
+        }
+        return comments;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates RefererManager */
+    public int getDayHits() {
+        try {
+            return mRefererMgr.getDayHits(mRollerReq.getWebsite());
+        } catch (RollerException e) {
+            mLogger.error("PageModel getDayHits()", e);
+        }
+        return 0;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates BookmarkManager.getFolder() */
+    public FolderDataWrapper getFolder(String folderPath) {
+        try {
+            return FolderDataWrapper.wrap(
+                    mBookmarkMgr.getFolder(
+                    mUserMgr.getWebsiteByHandle(mHandle), folderPath));
+        } catch (RollerException e) {
+            mLogger.error("PageModel getFolder()", e);
+        }
+        return null;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates UserManager.getPageByName() */
+    public TemplateWrapper getUsersPageByName(WebsiteDataWrapper wrapper, String pageName) {
+        WebsiteData website = wrapper.getPojo();
+        TemplateWrapper page = null;
+        try {
+            if (website == null)
+                throw new NullPointerException("website is null");
+            
+            if (pageName == null)
+                throw new NullPointerException("pageName is null");
+            
+            page = TemplateWrapper.wrap(website.getPageByName(pageName));
+        } catch (NullPointerException npe) {
+            mLogger.warn(npe.getMessage());
+        } catch (RollerException e) {
+            mLogger.error("ERROR getting user's page by name: " + e.getMessage(),e);
+        }
+        return page;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates UserManager.getPageByName() */
+    public TemplateWrapper getPageByName(String pageName) {
+        return (TemplateWrapper) mPageMap.get(pageName);
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates UserManager.getPageByName() */
+    public String getPageIdByName(String pageName) {
+        mLogger.debug("looking up page ["+pageName+"]");
+        
+        String template_id = null;
+        
+        try {
+            Template pd = mWebsite.getPageByName(pageName);
+            if(pd != null) {
+                template_id = pd.getId();
+            }
+        } catch(Exception e) {
+            mLogger.error(e);
+        }
+        
+        mLogger.debug("returning template id ["+template_id+"]");
+        
+        return template_id;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /**
+     * Get collection of user pages.
+     * @return
+     */
+    public Object getPages() {
+        return mPageMap.values();
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /**
+     * Returns a map of up to 100 recent weblog entries for the user and day
+     * specified in the request, filtered by the category specified by the
+     * request, limited by the 'maxEntries' argument, and sorted by reverse
+     * chronological order.
+     *
+     * <p>This method will look for a category name in the following places
+     * and in the following order:</p>
+     * <ul>
+     * <li>The request via RollerRequest.getWeblogCategory().</li>
+     * <li>The categoryName argument to this method.</li>
+     * <li>The default category for the website specified by the request via
+     *     RollerRequest.getWebsite().getDefaultCategory().</li>
+     * <li></li>
+     * </ul>
+     *
+     * @param maxEntries Maximum number of entries to be returned (only applies 
+     *                   if specific day not specified).
+     * @param catName    Only return entries from this category and it's
+     *                   subcategories. If null, returns all categories of entry
+     * @return           Map of Lists of WeblogEntryData, keyed by 8-char date 
+     *                   strings.
+     */
+    public Map getRecentWeblogEntries(int maxEntries, String catName) {
+        if (VELOCITY_NULL.equals(catName)) catName = null;
+        Map ret = new HashMap();
+        try {            
+            // If request specifies a category, then use that
+            String catParam = null;
+            if (mRollerReq.getWeblogCategory() != null) {
+                catParam = mRollerReq.getWeblogCategory().getPath();
+            } else if (catName != null) {
+                // use category argument instead
+                catParam = catName;
+            } else if (mRollerReq.getWebsite() != null) // MAIN
+            {
+                catParam = mRollerReq.getWebsite().getDefaultCategory().getPath();
+                if (catParam.equals("/")) {
+                    catParam = null;
+                }
+            }
+            
+            Calendar cal = null;
+            if (mRollerReq.getWebsite() != null) {
+                TimeZone tz = mRollerReq.getWebsite().getTimeZoneInstance();
+                cal = Calendar.getInstance(tz);
+            } else {
+                cal = Calendar.getInstance();
+            }
+            Integer limit = new Integer(maxEntries);
+            Date startDate = null;
+            Date endDate = mRollerReq.getDate();
+            if (endDate == null) endDate = new Date();
+            if (mRollerReq.isDaySpecified()) { 
+                // URL specified a specific day
+                // so get entries for that day
+                endDate = DateUtil.getEndOfDay(endDate, cal);
+                startDate = DateUtil.getStartOfDay(endDate, cal); 
+                // and get them ALL, no limit
+                limit = null;                  
+            } else if (mRollerReq.isMonthSpecified()) {
+                endDate = DateUtil.getEndOfMonth(endDate, cal);
+            }
+            Map mRet = RollerFactory.getRoller().getWeblogManager().getWeblogEntryObjectMap(
+                    mRollerReq.getWebsite(),
+                    startDate,                    // startDate
+                    endDate,                      // endDate
+                    catParam,                     // catName
+                    WeblogEntryData.PUBLISHED,    // status
+                    limit);     // maxEntries
+            
+            // need to wrap pojos
+            java.util.Date key = null;
+            Iterator days = mRet.keySet().iterator();
+            while(days.hasNext()) {
+                key = (java.util.Date)days.next();
+                
+                // now we need to go through each entry in a day and wrap
+                List wrappedEntries = new ArrayList();
+                List entries = (List) mRet.get(key);
+                for(int i=0; i < entries.size(); i++) {
+                    wrappedEntries.add(i,
+                         WeblogEntryDataWrapper.wrap((WeblogEntryData)entries.get(i)));
+                }
+                mRet.put(key, wrappedEntries);
+            }
+            
+            ret = mRet;
+            
+            setFirstAndLastEntries( ret );
+        } catch (Exception e) {
+            mLogger.error("PageModel getRecentWeblogEntries()", e);
+        }
+        return ret;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /**
+     * Pull the last WeblogEntryData out of the Map.
+     * @param ret
+     */
+    private void setFirstAndLastEntries(Map days) {
+        int numDays = days.keySet().size();
+        if (numDays > 0) // there is at least one day
+        {
+            // get first entry in map
+            Object[] keys = days.keySet().toArray(new Object[numDays]);
+            List vals = (List)days.get( keys[0] );
+            int valSize = vals.size();
+            if (valSize > 0) {
+                mFirstEntry = (WeblogEntryDataWrapper)vals.get(0);
+            }
+            
+            // get last entry in map
+            vals = (List)days.get( keys[--numDays] );
+            valSize = vals.size();
+            if (valSize > 0) {
+                mLastEntry = (WeblogEntryDataWrapper)vals.get(--valSize);
+            }
+        }
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /**
+     * Returns list of recent weblog entries for the user and day specified in
+     * the request, filtered by the category specified by the request, limited
+     * by the 'maxEntries' argument, and sorted by reverse chronological order.
+     *
+     * <p>This method will look for a category name in the same places and
+     * same order as does the getRecentWeblogEntries() method.</p>
+     *
+     * @param maxEntries   Maximum number of entries to be returned.
+     * @param categoryName Only return entries from this category and it's
+     *         subcategories. If null, returns all categories of entry.
+     * @return List of WeblogEntryData objects in revese chronological order.
+     */
+    public List getRecentWeblogEntriesArray(int maxEntries, String categoryName) {
+        if (VELOCITY_NULL.equals(categoryName)) categoryName = null;
+        List ret = new ArrayList();
+        try {
+            Date day = mRollerReq.getDate();
+            if (day == null) day = new Date();
+            
+            // If request specifies a category, then use that
+            String catParam = null;
+            if (mRollerReq.getWeblogCategory() != null) {
+                catParam = mRollerReq.getWeblogCategory().getPath();
+            } else if (categoryName != null) {
+                // use category argument instead
+                catParam = categoryName;
+            } else if (mRollerReq.getWebsite() != null) // MAIN
+            {
+                catParam = mRollerReq.getWebsite().getDefaultCategory().getPath();
+                if (catParam.equals("/")) {
+                    catParam = null;
+                }
+            }
+            WeblogManager mgr = RollerFactory.getRoller().getWeblogManager();
+            
+            //ret = mgr.getRecentWeblogEntriesArray(
+            //name, day, catParam, maxEntries, true );
+            
+            List mEntries = mgr.getWeblogEntries(
+                    mRollerReq.getWebsite(),
+                    null,                        // startDate
+                    day,                         // endDate
+                    catParam,                    // catName
+                    WeblogEntryData.PUBLISHED,   // status
+                    null,                        // sortby (null for pubTime)
+                    new Integer(maxEntries));    // maxEntries
+            
+            // wrap pojos
+            ret = new ArrayList(mEntries.size());
+            Iterator it = mEntries.iterator();
+            int i=0;
+            while(it.hasNext()) {
+                ret.add(i, WeblogEntryDataWrapper.wrap((WeblogEntryData) it.next()));
+                i++;
+            }
+        } catch (Exception e) {
+            mLogger.error("PageModel getRecentWeblogEntries()", e);
+        }
+        return ret;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates RefererManager **/
+    public List getReferers(String date) {
+        date = date.trim();
+        ArrayList referers = new ArrayList();
+        try {
+            List refs =
+                    mRefererMgr.getReferersToDate(mRollerReq.getWebsite(), date);
+            RollerSession rses =
+                    RollerSession.getRollerSession(mRollerReq.getRequest());
+            
+            for (Iterator rdItr = refs.iterator(); rdItr.hasNext();) {
+                RefererData referer = (RefererData) rdItr.next();
+                String title =referer.getTitle();
+                String excerpt = referer.getExcerpt();
+                if (   StringUtils.isNotEmpty(title)
+                && StringUtils.isNotEmpty(excerpt) ) {
+                    if (   referer.getVisible().booleanValue()
+                    || rses.isUserAuthorizedToAdmin(referer.getWebsite()) ) {
+                        referers.add(RefererDataWrapper.wrap(referer));
+                    }
+                }
+            }
+            
+        } catch (Exception e) {
+            mLogger.error("PageModel getReferersToDate() fails with URL"
+                    + mRollerReq.getRequestURL(), e);
+        }
+        return referers;
+    }
+    
+    /** Encapsulates RefererManager **/
+    public List getEntryReferers(WeblogEntryDataWrapper entry) {
+        ArrayList referers = new ArrayList();
+        try {
+            List refs = mRefererMgr.getReferersToEntry(entry.getId());
+            RollerSession rses =
+               RollerSession.getRollerSession(mRollerReq.getRequest());
+            
+            for (Iterator rdItr = refs.iterator(); rdItr.hasNext();) {
+                RefererData referer = (RefererData) rdItr.next();
+                String title =referer.getTitle();
+                String excerpt = referer.getExcerpt();
+                if (   StringUtils.isNotEmpty(title)
+                && StringUtils.isNotEmpty(excerpt) ) {
+                    if (referer.getVisible().booleanValue()
+                    || rses.isUserAuthorizedToAdmin(referer.getWebsite()) ) {
+                        referers.add(RefererDataWrapper.wrap(referer));
+                    }
+                }
+            }
+            
+        } catch (Exception e) {
+            mLogger.error("PageModel getReferersToDate() fails with URL"
+                    + mRollerReq.getRequestURL(), e);
+        }
+        return referers;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates RefererManager */
+    public List getTodaysReferers() {
+        List referers = null;
+        try {
+            List mReferers = mRefererMgr.getTodaysReferers(mRollerReq.getWebsite());
+            
+            // wrap pojos
+            referers = new ArrayList(mReferers.size());
+            Iterator it = mReferers.iterator();
+            int i=0;
+            while(it.hasNext()) {
+                referers.add(i, RefererDataWrapper.wrap((RefererData) it.next()));
+                i++;
+            }
+            
+        } catch (RollerException e) {
+            mLogger.error("PageModel getTodaysReferers()", e);
+        }
+        return (referers == null ? Collections.EMPTY_LIST : referers);
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates RefererManager */
+    public int getTotalHits() {
+        try {
+            return mRefererMgr.getTotalHits(mRollerReq.getWebsite());
+        } catch (RollerException e) {
+            mLogger.error("PageModel getTotalHits()", e);
+        }
+        return 0;
+    }
+    
+    //------------------------------------------------------------------------
+    /**
+     * Returns most recent update time of collection of weblog entries.
+     * @param weblogEntries Collection of weblog entries.
+     * @return Most recent update time.
+     */
+    public static Date getUpdateTime( ArrayList weblogEntries ) {
+        Date updateTime = null;
+        Iterator iter = weblogEntries.iterator();
+        while (iter.hasNext()) {
+            // NOTE: this will need to be WeblogEntryDataWrapper
+            WeblogEntryData wd = (WeblogEntryData)iter.next();
+            if ( updateTime == null ) {
+                updateTime = wd.getUpdateTime();
+            }
+            //else if ( updateTime.compareTo(wd.getUpdateTime()) < 0 )
+            else if (updateTime.before( wd.getUpdateTime() )) {
+                updateTime = wd.getUpdateTime();
+            }
+        }
+        return updateTime;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates WeblogManager.getWeblogCategories() */
+    public List getWeblogCategories(String categoryName) {
+        List ret = null;
+        if (VELOCITY_NULL.equals(categoryName)) categoryName = null;
+        
+        // Make sure we have not already fetched this category.
+        if (categoryName != null) {
+            ret = (List)mCategories.get(categoryName);
+        } else {
+            ret = (List)mCategories.get("zzz_null_zzz");
+        }
+        
+        if (null == ret) {
+            try {
+                WeblogCategoryData category = null;
+                if (categoryName != null) {
+                    category = mWeblogMgr.getWeblogCategoryByPath(
+                            mRollerReq.getWebsite(), null, categoryName);
+                } else {
+                    category = mRollerReq.getWebsite().getDefaultCategory();
+                }
+                
+                List mRet = category.getWeblogCategories();
+                
+                // wrap pojos
+                ret = new ArrayList(mRet.size());
+                Iterator it = mRet.iterator();
+                int i=0;
+                while(it.hasNext()) {
+                    ret.add(i, WeblogCategoryDataWrapper.wrap((WeblogCategoryData)it.next()));
+                    i++;
+                }
+                if (categoryName != null) {
+                    mCategories.put(categoryName, ret);
+                } else {
+                    mCategories.put("zzz_null_zzz", ret);
+                }
+            } catch (RollerException e) {
+                mLogger.error(e);
+            }
+        }
+        return ret;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /** Encapsulates RollerRequest.getWeblogEntry() */
+    public WeblogEntryDataWrapper getWeblogEntry() {
+        WeblogEntryData entry = mRollerReq.getWeblogEntry();
+        
+        if(entry != null && entry.getStatus().equals(WeblogEntryData.PUBLISHED))
+            return WeblogEntryDataWrapper.wrap(entry);
+        else
+            return null;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /**
+     * Get the next occurring Entry.
+     */
+    public WeblogEntryDataWrapper getNextEntry() {
+        WeblogEntryDataWrapper currentEntry = getWeblogEntry();
+        if (mFirstEntry != null) currentEntry = mFirstEntry;
+        if (mNextEntry == null && currentEntry != null) {
+            String catName = null;
+            if (mRollerReq.getWeblogCategory() != null) {
+                catName = mRollerReq.getWeblogCategory().getName();
+            }
+            try {
+                WeblogEntryData nextEntry =
+                        mWeblogMgr.getNextEntry(currentEntry.getPojo(), catName);
+                
+                if(nextEntry != null)
+                    mNextEntry = WeblogEntryDataWrapper.wrap(nextEntry);
+                
+                // make sure that mNextEntry is not published to future
+                if (mNextEntry != null &&
+                        mNextEntry.getPubTime().after( new Date() )) {
+                    mNextEntry = null;
+                }
+            } catch (RollerException e) {
+                mLogger.error("PageModel.getNextEntry)", e);
+            }
+        }
+        return mNextEntry;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    /**
+     * Get the previous occurring Entry.
+     */
+    public WeblogEntryDataWrapper getPreviousEntry() {
+        WeblogEntryDataWrapper currentEntry = getWeblogEntry();
+        if (mLastEntry != null) currentEntry = mLastEntry;
+        if (mPreviousEntry == null && currentEntry != null ) {
+            String catName = null;
+            if (mRollerReq.getWeblogCategory() != null) {
+                catName = mRollerReq.getWeblogCategory().getName();
+            }
+            try {
+                WeblogEntryData prevEntry =
+                        mWeblogMgr.getPreviousEntry(currentEntry.getPojo(), catName);
+                
+                if(prevEntry != null)
+                    mPreviousEntry = WeblogEntryDataWrapper.wrap(prevEntry);
+            } catch (RollerException e) {
+                mLogger.error("PageModel.getPreviousEntry)", e);
+            }
+        }
+        return mPreviousEntry;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    public boolean isUserAuthorizedToEdit() {
+        try {
+            RollerSession rses =
+                    RollerSession.getRollerSession(mRollerReq.getRequest());
+            if (rses.getAuthenticatedUser() != null
+                    && mRollerReq.getWebsite() != null) {
+                return rses.isUserAuthorizedToAuthor(mRollerReq.getWebsite());
+            }
+        } catch (Exception e) {
+            mLogger.warn("PageModel.isUserAuthorizedToEdit()", e);
+        }
+        return false;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    public boolean isUserAuthorizedToAdmin() {
+        try {
+            RollerSession rses =
+                    RollerSession.getRollerSession(mRollerReq.getRequest());
+            if (rses.getAuthenticatedUser() != null
+                    && mRollerReq.getWebsite() != null) {
+                return rses.isUserAuthorizedToAdmin(mRollerReq.getWebsite());
+            }
+        } catch (Exception e) {
+            mLogger.warn("PageModel.isUserAuthorizedToAdmin()", e);
+        }
+        return false;
+    }
+    
+    //------------------------------------------------------------------------
+    
+    public boolean isUserAuthenticated() {
+        return (mRollerReq.getRequest().getUserPrincipal() != null);
+    }
+    
+    //------------------------------------------------------------------------
+    
+    public String getRequestParameter(String key) {
+        return mRollerReq.getRequest().getParameter(key);
+    }
+    
+    //------------------------------------------------------------------------
+    
+    public FolderDataWrapper getFolderByPath(String path) {
+        try {
+            FolderData folder = mBookmarkMgr.getFolderByPath(
+                    mWebsite, null, path);
+            
+            if(folder != null)
+                return FolderDataWrapper.wrap(folder);
+        } catch (RollerException e) {
+            mLogger.error(e);
+        }
+        
+        return null;
+    }
+    
+    /**
+     * Facade for WeblogManager.getRecentComments().
+     * Get the most recent (chronologically) posted Comments
+     * for this website, limited to maxCount.
+     * @return List of Comments.
+     */
+    public List getRecentComments(int maxCount) {
+        List recentComments = new ArrayList();
+        try {
+            WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
+            List recent = wmgr.getComments(
+                    mWebsite,
+                    null,  // weblog entry
+                    null,  // search String
+                    null,  // startDate
+                    null,  // endDate
+                    null,  // pending
+                    Boolean.TRUE,  // approved only
+                    Boolean.FALSE, // no spam
+                    true,          // we want reverse chrono order
+                    0,             // offset
+                    maxCount);     // no limit
+            
+            // wrap pojos
+            recentComments = new ArrayList(recent.size());
+            Iterator it = recent.iterator();
+            while(it.hasNext()) {
+                recentComments.add(CommentDataWrapper.wrap((CommentData) it.next()));
+            }
+        } catch (RollerException e) {
+            mLogger.error(e);
+        }
+        return recentComments;
+    }
+    
+    public boolean getEmailComments() {
+        if (mRollerReq != null) {
+            WebsiteData website = mRollerReq.getWebsite();
+            if (website != null) {
+                boolean emailComments = RollerRuntimeConfig.getBooleanProperty("users.comments.emailnotify");        
+                return (website.getEmailComments().booleanValue() && emailComments);
+            }
+        }
+        return false;
+    }
+}



Mime
View raw message