home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/ForumWebHandler.java,v 1.33 2005/01/28 12:07:50 tqphong Exp $
 * $Author: tqphong $
 * $Revision: 1.33 $
 * $Date: 2005/01/28 12:07:50 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2005 by MyVietnam.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * All copyright notices regarding mvnForum MUST remain intact
 * in the scripts and in the outputted HTML.
 * The "powered by" text/logo with a link back to
 * http://www.mvnForum.com and http://www.MyVietnam.net in the
 * footer of the pages MUST remain visible when the pages
 * are viewed on the internet or intranet.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Support can be obtained from support forums at:
 * http://www.mvnForum.com/mvnforum/index
 *
 * Correspondence and Marketing Questions can be sent to:
 * info@MyVietnam.net
 *
 * @author: Minh Nguyen  minhnn@MyVietnam.net
 * @author: Mai  Nguyen  mai.nh@MyVietnam.net
 */
package com.mvnforum.user;

import java.util.*;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.util.*;

import com.mvnforum.MVNForumConfig;
import com.mvnforum.MVNForumResourceBundle;
import com.mvnforum.auth.*;
import com.mvnforum.common.ActiveThread;
import com.mvnforum.db.*;

public class ForumWebHandler {

    private OnlineUserManager userManager = OnlineUserManager.getInstance();

    public ForumWebHandler() {
    }

    public void prepareList(HttpServletRequest request, String requestURI)
        throws AssertionException, DatabaseException, AuthenticationException, MissingURLMapEntryException {

        Locale locale = I18nUtil.getLocaleInRequest(request);

        // the following 3 lines fix the bug that no online user found in the first time request
        OnlineUser onlineUser = userManager.getOnlineUser(request);
        Action action = new ActionInUserModule(request, requestURI); // may throw MissingURLMapEntryException
        userManager.updateOnlineUserAction(request, action);

        MVNForumPermission permission = onlineUser.getPermission();

        // Calculate to get the mosts
        long now = DateUtil.getCurrentGMTTimestamp().getTime();
        Timestamp since = new Timestamp(now - DateUtil.WEEK);

        if (MVNForumConfig.getEnableMostActiveMembers()) {
            Collection mostActiveMembers = DAOFactory.getPostDAO().getMostActiveMembers(since, MVNForumConfig.getMaxActiveMembers());

            request.setAttribute("MostActiveMembers", mostActiveMembers);
        }
        if (MVNForumConfig.getEnableMostActiveThreads()) {
            Collection mostActiveThreads = this.getMyMostActiveThreads(permission, since);

            request.setAttribute("MostActiveThreads", mostActiveThreads);
        }

        if (MVNForumConfig.getEnableSiteStatisticsOverview()) {
            int numberOfMembers    = DAOFactory.getMemberDAO().getNumberOfMembers();
            Collection memberBeans = DAOFactory.getMemberDAO().getMembers_withSortSupport_limit(0, 1, "MemberID", "DESC");
            if (memberBeans.size() != 1) {
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.wrong_size", new Object[] {"MemberBeans", "==",new Integer(memberBeans.size())});
                throw new AssertionException(localizedMessage);
                //throw new AssertionException("Assertion: MemberBeans size == 1 (but the value = " + memberBeans.size() + ")");
            }
            MemberBean memberBean = (MemberBean)memberBeans.iterator().next();

            request.setAttribute("MemberBean", memberBean);
            request.setAttribute("NumberOfMembers", new Integer(numberOfMembers));
        }

        // now set the attribute
        boolean duplicateUsers = MVNForumConfig.getEnableDuplicateOnlineUsers();
        request.setAttribute("OnlineUserActions", userManager.getOnlineUserActions(0 /*default*/, duplicateUsers)); // no permission
    }

    public void prepareListIndex(HttpServletRequest request, String requestURI)
        throws AssertionException, DatabaseException,
        AuthenticationException, MissingURLMapEntryException {

        Locale locale = I18nUtil.getLocaleInRequest(request);

        // the following 3 lines fix the bug that no online user found in the first time request
        OnlineUser onlineUser = userManager.getOnlineUser(request);
        Action action = new ActionInUserModule(request, requestURI); // may throw MissingURLMapEntryException
        userManager.updateOnlineUserAction(request, action);

        MVNForumPermission permission = onlineUser.getPermission();

        ForumCache forumCache = ForumCache.getInstance();
        Collection forumBeans = forumCache.getBeans();
        Collection cTotal = new ArrayList();

        for (Iterator forumIterator = forumBeans.iterator(); forumIterator.hasNext(); ) {
            ForumBean forumBean = (ForumBean) forumIterator.next();

            Collection cThreadTemp = DAOFactory.getThreadDAO().getNormalEnableThreads_inForum_withSortSupport_limit(forumBean.getForumID(), 0, 1, "ThreadLastPostDate", "DESC");
            if (cThreadTemp.size() == 0) {
                continue;
            } else if (cThreadTemp.size() > 1) {
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.wrong_size", new Object[] {"ThreadBeans", "<=",new Integer(cThreadTemp.size())});
                throw new AssertionException(localizedMessage);
                //throw new AssertionException("Assertion: ThreadBeans size <= 1 (but the value = " + cThreadTemp.size() + ")");
            }
            ThreadBean threadBean = (ThreadBean) (cThreadTemp.iterator().next());

            Collection cPostTemp = DAOFactory.getPostDAO().getLastEnablePosts_inThread_limit(threadBean.getThreadID(), 1);
            if (cPostTemp.size() != 1) {
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.wrong_size", new Object[] {"PostBeans", "==",new Integer(cPostTemp.size())});
                throw new AssertionException(localizedMessage);
                //throw new AssertionException("Assertion: PostBeans size == 1 (but the value = " + cPostTemp.size() + ")");
            }
            PostBean postBean = (PostBean) (cPostTemp.iterator().next());

            Collection cGeneralInfo = new ArrayList(2);
            cGeneralInfo.add(postBean);
            cGeneralInfo.add(threadBean);
            cTotal.add(cGeneralInfo);
        }

        // Calculate to get the mosts
        long now = DateUtil.getCurrentGMTTimestamp().getTime();
        Timestamp since = new Timestamp(now - DateUtil.WEEK);

        if (MVNForumConfig.getEnableMostActiveMembers()) {
            Collection mostActiveMembers = DAOFactory.getPostDAO().getMostActiveMembers(since, MVNForumConfig.getMaxActiveMembers());

            request.setAttribute("MostActiveMembers", mostActiveMembers);
        }

        if (MVNForumConfig.getEnableMostActiveThreads()) {
            Collection mostActiveThreads = this.getMyMostActiveThreads(permission, since);

            request.setAttribute("MostActiveThreads", mostActiveThreads);
        }

        if (MVNForumConfig.getEnableSiteStatisticsOverview()) {
            int numberOfMembers = DAOFactory.getMemberDAO().getNumberOfMembers();
            Collection memberBeans = DAOFactory.getMemberDAO().getMembers_withSortSupport_limit(0, 1, "MemberID", "DESC");
            if (memberBeans.size() != 1) {
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.wrong_size", new Object[] {"MemberBeans", "==",new Integer(memberBeans.size())});
                throw new AssertionException(localizedMessage);
                //throw new AssertionException("Assertion: MemberBeans size == 1 (but the value = " + memberBeans.size() + ")");
            }
            MemberBean memberBean = (MemberBean) memberBeans.iterator().next();

            request.setAttribute("MemberBean", memberBean);
            request.setAttribute("NumberOfMembers", new Integer(numberOfMembers));
        }

        // now set the attribute
        boolean duplicateUsers = MVNForumConfig.getEnableDuplicateOnlineUsers();
        request.setAttribute("OnlineUserActions", userManager.getOnlineUserActions(0 /*default*/, duplicateUsers)); // no permission
        request.setAttribute("LastPosts", cTotal);
    }

    private Collection getMyMostActiveThreads(MVNForumPermission permission, Timestamp since)
        throws DatabaseException {

        // get more than 2 threads in case user doesnt have permission to view previous threads
        Collection mostActiveThreads = DAOFactory.getPostDAO().getMostActiveThreads(since, MVNForumConfig.getMaxActiveThreads() + 2);
        int remainCount = 0;
        for (Iterator iter = mostActiveThreads.iterator(); iter.hasNext(); ) {
            ActiveThread activeThread = (ActiveThread) iter.next();
            if (permission.canReadPost(activeThread.getForumID()) == false) {
                iter.remove();
            } else if (++remainCount > MVNForumConfig.getMaxActiveThreads()) {
                iter.remove();
            }
        }
        return mostActiveThreads;
    }
}


my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.