⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blogjsptemplate.java

📁 OpenCMS内容管理入门指南
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * This library is part of the code for the book: OpenCms for Developers
 *
 * Copyright (c) 2007, 2008 Dan Liliedahl
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.deepthoughts.templates;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsUser;
import org.opencms.file.collectors.I_CmsResourceCollector;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.i18n.CmsEncoder;
import org.opencms.jsp.CmsJspTagContentCheck;
import org.opencms.jsp.CmsJspTagContentLoad;
import org.opencms.jsp.CmsJspTagEditable;
import org.opencms.jsp.CmsJspXmlContentBean;
import org.opencms.jsp.I_CmsXmlContentContainer;
import org.opencms.jsp.Messages;
import org.opencms.jsp.util.CmsJspElFunctions;
import org.opencms.loader.CmsImageScaler;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.publish.CmsPublishManager;
import org.opencms.security.CmsOrganizationalUnit;
import org.opencms.security.CmsPrincipal;
import org.opencms.security.CmsRole;
import org.opencms.workplace.editors.directedit.CmsDirectEditMode;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentFactory;
import org.opencms.xml.types.I_CmsXmlContentValue;

/**
 * This bean provides logic for the templates of the blog site.  It subclasses the CmsJspActionElement 
 * class and provides methods that make it simpler to create blog templates.
 * 
 * To use the bean, initialize this bean at the beginning of the JSP template with:
 * <pre>
 * &lt;jsp:useBean id="blog" class="com.deepthoughts.templates.BlogJspTemplate"&gt;
 * &lt% blog.init(pageContext, request, response); %&gt;
 * &lt;/jsp:useBean&gt;
 * </pre>
 * 
 * Blog content may then be accessed using either of these methods:
 * <pre>
 * &lt%
 * // retrieve all blogs in the current folder
 * blog.getBlogsInFolder();
 * %&gt;
 * 
 * &lt% 
 * // get the blog entry that was clicked on
 * blog.getBlog(blog.getCmsObject().getRequestContext().getUri());
 * %&gt;
 * </pre>
 * 
 * @author Dan Liliedahl
 */
public class BlogJspTemplate extends CmsJspXmlContentBean {

	// useful constant
	protected final static String NULLSTR = "";

	// path to our blog respository, blogs are arranged by month and year
	protected final static String CONTENT_PATH = "/Blogs/";

	// fieldnames in our blogentry content type
	protected final static String FIELD_IMAGE = "Image";
	protected final static String FIELD_ALIGNMENT = "Alignment";
	protected final static String FIELD_TOPIC = "Category";
	protected final static String FIELD_COMMENT = "Comment";
	
	// the resource type id for our BlogEntry resource
	protected final static int BLOG_RESOURCE_TYPEID = 3000;

    protected static final String URL_REGISTER = "/system/modules/com.deepthoughts.templates/elements/Register.jsp";
    protected static final String URL_ADD_COMMENT = "/system/modules/com.deepthoughts.templates/elements/AddComment.jsp";
    
	// container for the currently loaded list of blogs
	I_CmsXmlContentContainer m_iContent;

	// container for comments for the current blog
	I_CmsXmlContentContainer m_iComments;
	
	// Registration action constants
    public static final int FORMACTION_SHOWFORM = 0;
    public static final int FORMACTION_OK = 1;
    public static final int FORMACTION_ERROR = 2;
    
	// contains error messages from the login/registration action
	protected String m_strRegisterMessage;
	
	// Organizational Units
	public static final String OU_DEEPTHOUGHTS = "DeepThoughts/";
	public static final String OU_DEEPTHOUGHTS_REGUSERS = OU_DEEPTHOUGHTS + "SubscribedUser/";
	// Groups
	public static final String GRP_DEEPTHOUGHTS_WEBUSERS = "WebUsers";
	
	// Users
	public static final String USR_DEEPTHOUGHTS_BLOG_ADMIN = OU_DEEPTHOUGHTS + "BlogAdmin";
	public static final String USR_DEEPTHOUGHTS_BLOG_ADMIN_PW = "blogadmin";
	public static final String USR_DEEPTHOUGHTS_COMMENT_PUBLISHER = OU_DEEPTHOUGHTS_REGUSERS + "CommentPublisher";
	public static final String USR_DEEPTHOUGHTS_COMMENT_PUBLISHER_PW = "password";

	// constant used for both the RSS Feed parameter name and the user setting key
    public static final String RSS_FEED_PARAM = "RSS_FEED";

    // default feed url if none found
    public static final String DEFAULT_RSS_URL = "http://news.google.com/?output=rss";
    
	
    /**
     * Empty constructor, required for every JavaBean.
     */
    public BlogJspTemplate() {
        super();
    }

    /**
     * Constructor, with parameters.
     * 
     * @param context the JSP page context object
     * @param req the JSP request 
     * @param res the JSP response 
     */
    public BlogJspTemplate(PageContext context, HttpServletRequest req, HttpServletResponse res) {
        super(context, req, res);
        String strAction = req.getParameter("action"); 
        if (null != strAction) {
        	if (strAction.equals("logout")) {
            	logout();
        	} else if (strAction.equals("login")) {
        		loginUser(req.getParameter("username"), req.getParameter("password"));
        	}
        }
    }

    /**
     * This method overrides the base implementation se we may specify a direct edit provider instead of
     * using the default one.  For our templates we will use the CmsDirectEditTextButtonProvider and
     * place the edit controls manually.
     * 
     * @throws JspException if a problem occurs
     */
    public void editable() throws JspException {
    	CmsJspTagEditable.editableTagAction(getJspContext(),
   			"org.opencms.workplace.editors.directedit.CmsDirectEditTextButtonProvider", 
   			CmsDirectEditMode.AUTO, 
   			null);
    }

    /**
     * Returns the name of the blog folder currently being viewed.  If the current blog folder
     * is the most recent blog entries then return NULLSTR.   This is used on the page to indicate
     * that an archive folder is being browsed vs. the most current list of blogs.
     *  
     * @return String with name of archive folder or NULLSTR
     */
    public String getCurrentFolderName() {
		String URI = getRequest().getParameter("uri");
		if (null != URI) {
			CmsResource res;
			try {
				res = getCmsObject().readResource(URI);
				String strTitle = getCmsObject().readPropertyObject(res, 
						CmsPropertyDefinition.PROPERTY_TITLE, false).getValue();
				return "- Archive: " + strTitle;
			} catch (CmsException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return NULLSTR;
    }
    
    /**
     * This method return the most recent folder path in the blog respository.  It is used
     * to retrieve the current list of blogs from.  It relies on the Folder's 'Modified Date'
     * attribute to determine the most recent entry. 
     * 
     * @return String with the path of the most recent Blogs
     */
    public String getMostRecentBlogFolder() {
    	String strLatestPath = CONTENT_PATH;
    	
    	// get the list of blog folders
        List collectorResult = getBlogFolders();
        if (null != collectorResult && collectorResult.iterator().hasNext()) {
        	// the first one in the list is the most recent
        	CmsResource res = (CmsResource) collectorResult.iterator().next();
        	strLatestPath = CONTENT_PATH + res.getName() + "/";
        }
    	return strLatestPath;
    }

    /**
     * This method will return a list of blog folders in descending order sorted by the 
     * 'Modified Date' attribute.
     * 
     * @return List of CmsResources of blog folders or null if problem happens
     */
    public List getBlogFolders() {
    	// Use the collector to obtain a list of resources
    	String collectorName = "allInFolderDateReleasedDesc";
        try {
            // now collect the resources
            I_CmsResourceCollector collector = OpenCms.getResourceManager().getContentCollector(collectorName);
            if (null == collector) {
                throw new CmsException(Messages.get().container(Messages.ERR_COLLECTOR_NOT_FOUND_1, collectorName));
            }
            // get list of folders underneath the '/Blogs' folder
            List collectorResult = collector.getResults(getCmsObject(), collectorName, 
            		CONTENT_PATH + "|" + Integer.toString(CmsResourceTypeFolder.RESOURCE_TYPE_ID));
            return collectorResult;
        } catch (CmsException e) {
			e.printStackTrace();
        }
    	return null;
    }
    
    /**
     * This method performs a content load, allowing us to manually place the direct edit buttons 
     * within our content loop.
     * 
     * @param collectorName the collector name to use
     * @param collectorParam the parameters for the collector
     * 
     * @return an XML content container loaded with the selected content
     * 
     * @throws JspException in case something goes wrong
     */
    protected I_CmsXmlContentContainer contentloadManualEdit(String collectorName, String collectorParam) throws JspException {
    	return new CmsJspTagContentLoad(null, getJspContext(), collectorName, collectorParam,
		        null, null, getRequestContext().getLocale(), CmsDirectEditMode.MANUAL);
    }
    
    /**
     * Retrieve the blog specified by the URI.
     * 
     * @return I_CmsXmlContentContainer containing a list of all the recent blogs
     * @throws JspException
     */
    public boolean getBlog(String URI)  {

    	try {
	        m_iContent = contentloadManualEdit("singleFile", URI);
	        return true;
		} catch (JspException e) {
			e.printStackTrace();
		}
		return false;
    }

    /**
     * Retrieve the list of blogs in the given folder.  If the request has a parameter named
     * 'uri', then that folder will be used.  Otherwise blog entries will be retrieved from 
     * the most recent folder in the blog repository.
     * 
     * @return I_CmsXmlContentContainer containing a list of all the recent blogs
     */
    public I_CmsXmlContentContainer getBlogsInFolder() {
		try {
			String strPath = null;
			String URI = getRequest().getParameter("uri");
			if (null == URI) {
				strPath = getMostRecentBlogFolder();
			} else {
				strPath = URI;
			}
			// we retrieve everything in the folder with the resource id for our BlogEntry type
	        m_iContent = contentloadManualEdit("allInFolderDateReleasedDesc", strPath.concat("|").
	        		concat(Integer.toString(BLOG_RESOURCE_TYPEID)));
		} catch (JspException e) {
			e.printStackTrace();
		}
        return m_iContent;
    }

    /**
     * Returns count of blogs in the given folder
     * 
     * @param res CmsResource of a folder containing blog entries

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -