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

📄 forumcontroller.java

📁 如题ServletJSP.rar 为网络收集的JSP网站源文件
💻 JAVA
字号:
 /*
 * XP Forum
 *
 * Copyright (c) 2002-2003 RedSoft Group.  All rights reserved.
 *
 */
package org.redsoft.forum.web;

import java.util.Collection;
import org.redsoft.forum.dao.DAOFactory;
import org.redsoft.forum.dao.ThreadDAO;
import java.util.ArrayList;
import java.sql.SQLException;
import org.redsoft.forum.exception.CategoryNotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import javax.security.auth.Subject;

import org.redsoft.forum.ForumConstants;
import org.redsoft.forum.security.User;


/**
 * Display a list of threads and allow use to navigate the list of
 * threads
 *
 * @author Charles Huang
 * @version 1.0
 * @since JDK 1.4
 */

public class ForumController {

    // Holds a collection of threads retrieved from DB
    private Collection threadList;

	// Holds the category
	private int category = 1;

    // Holds the start index of the threads
    private int startIndex = 1;

    // Holds the current user id
    private String userID = null;

    /**
     *  Constructor
     *
     * @param category - The category of this forum
     */
    public ForumController()  {
    }


    public void perform( final HttpServletRequest request,
                         final HttpServletResponse response ){

        String categoryString = request.getParameter( ForumConstants.FORUM_ID_PARAM );
	    final Subject subject = (Subject)request.getSession().getAttribute(ForumConstants.USER_KEY);
        if( subject != null ){
        	userID = ( (User)subject.getPrincipals( User.class ).iterator().next() ).getName();
		}

        if( categoryString == null || categoryString.length() == 0 ){
			categoryString = (String)request.getAttribute( ForumConstants.FORUM_ID_PARAM );
		}

        final String action = request.getParameter( ForumConstants.ACTION_PARAM );
        if( categoryString != null && categoryString.length() > 0 ){
            category = Integer.parseInt( categoryString );
        }
        if( action != null && action.length() > 0 ){
            startIndex = Integer.parseInt( request.getParameter(ForumConstants.START_INDEX_PARAM) );
            // If action = next,increase the start index
            if( action.equals("next") ){
                startIndex = startIndex + ForumConstants.MAX_THREADS_PER_PAGE;
            }
            // If action = previous
            else if( action.equals("previous") ){
                startIndex = startIndex - ForumConstants.MAX_THREADS_PER_PAGE;
                if( startIndex < 1 ){
                    startIndex = 1;
                }
            }else if( action.equals("remove") ){
				removeThreads( request );
			}
         }
    }


    /**
     * Gets all the top-level threads for the current forum
     *
     * @param startIndex - The index of the first displayed thread
     * @param length - The quantity of the threads displayed in current screen
     * @return Collection - A collection of top-level threads for the forum
     */
    private Collection getThreads( final int startIndex, final int length ){
		final ThreadDAO dao = DAOFactory.getInstance().getThreadDAO();
        try{
            threadList = dao.findByCategory( category, startIndex,startIndex + length -1  );
        }catch ( final SQLException sqlException ){
            sqlException.printStackTrace();
            return new ArrayList();
        }catch ( final CategoryNotFoundException categoryNotFoundException ){
            categoryNotFoundException.printStackTrace();
        }
        return threadList;
    }


	/**
     * Remove selected threads
     *
     */
    private void removeThreads( final HttpServletRequest request ){
		final String[] seletedThreads = request.getParameterValues( "remove" );
		final ThreadDAO dao = DAOFactory.getInstance().getThreadDAO();
        if( seletedThreads != null && seletedThreads.length > 0 ){
        	try{
				for( int index = 0; index < seletedThreads.length; index++ ){
					dao.removeThread( Long.parseLong( seletedThreads[ index ] )  );
				}
        	}catch ( final SQLException sqlException ){
        	    sqlException.printStackTrace();
			}
		}
    }


    /**
     *  GEts all the top-level threads given the start index
     *
     *  @return Collection - A collection of threads
     */
    public Collection getThreads(){
        return getThreads( startIndex, ForumConstants.MAX_THREADS_PER_PAGE );
    }

    /**
     *  Return the start index of the threads in current page
     *
     *  @return int - The start index of the threads in the page
     */
    public int getStartIndex(){
        return startIndex;
    }

    /**
     *  Returns the total count of all top-leve threads under a category
     *
     * @return int - The total count of threads under a category
     */
    public int getTotalCount(){
        final ThreadDAO dao = DAOFactory.getInstance().getThreadDAO();
        int count = 0;
        try{
            count = dao.findCountByCategory( category );
        }catch ( final SQLException sqlException ){
            sqlException.printStackTrace();
        }
        return count;
    }


    /**
     *  Return the selected forum id which is the category in Thread definition
     *
     *  @return int - The selected forum id
     */
    public int getSelectedForum(){
        return category;
    }


	/**
	 * Check if the current user has an adminsitrator role
	 *
	 * @return boolean - True if the current user is in a administrator role
	 */
    public boolean isAdmin(){
		//TODO:Use role-based authrization by using JAAS or other machanism
		// Currently just simple use the explicit id to determine if the user
		// has administration privillage
		if( userID != null && userID.equals("ADMIN") ){
			return true;
		}else{
			return false;
		}
	}
}//EOC

⌨️ 快捷键说明

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