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/MessageFolderWebHandler.java,v 1.20 2005/01/18 11:52:23 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.20 $
 * $Date: 2005/01/18 11:52:23 $
 *
 * ====================================================================
 *
 * 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.sql.Timestamp;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.mvnforum.*;
import com.mvnforum.auth.*;
import com.mvnforum.common.PrivateMessageUtil;
import com.mvnforum.db.DAOFactory;
import com.mvnforum.db.MessageFolderBean;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

class MessageFolderWebHandler {

    private static Log log = LogFactory.getLog(MessageFolderWebHandler.class);

    private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();

    public MessageFolderWebHandler() {
    }

    public void prepareAdd(HttpServletRequest request)
        throws AssertionException, DatabaseException, AuthenticationException {

        Locale locale = I18nUtil.getLocaleInRequest(request);

        if (MVNForumConfig.getEnablePrivateMessage() == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.private_message_disabled");
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot operate with private messages. Because the private message feature is disabled");
        }

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureIsAuthenticated();
        permission.ensureCanUseMessage();
    }

    public void processAdd(HttpServletRequest request)
        throws CreateException, DuplicateKeyException, DatabaseException, BadInputException,
        AssertionException, AuthenticationException, ForeignKeyNotFoundException {

        Locale locale = I18nUtil.getLocaleInRequest(request);

        if (MVNForumConfig.getEnablePrivateMessage() == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.private_message_disabled");
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot operate with private messages. Because the private message feature is disabled");
        }

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureIsAuthenticated();
        permission.ensureCanUseMessage();

        int folderStatus = 0;
        int folderOption = 0;
        int folderType   = 0;
        Timestamp now    = DateUtil.getCurrentGMTTimestamp();
        String folderName = ParamUtil.getParameterSafe(request, "FolderName", true);
        StringUtil.checkGoodName(folderName);

        int folderOrder = DAOFactory.getMessageFolderDAO().getMaxFolderOrder(onlineUser.getMemberID());
        folderOrder++;// One value more than the current max value
        if (folderOrder < 10) folderOrder = 10;

        DAOFactory.getMessageFolderDAO().create(folderName, onlineUser.getMemberID(),
                                                folderOrder, folderStatus, folderOption, folderType, now, now);
    }

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

        Locale locale = I18nUtil.getLocaleInRequest(request);

        if (MVNForumConfig.getEnablePrivateMessage() == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.private_message_disabled");
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot operate with private messages. Because the private message feature is disabled");
        }

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureIsAuthenticated();
        permission.ensureCanUseMessage();

        int memberID = onlineUser.getMemberID();

        Collection messageFolderBeans = DAOFactory.getMessageFolderDAO().getMessageFolders_inMember(memberID);
        for (Iterator iter = messageFolderBeans.iterator(); iter.hasNext(); ) {
            MessageFolderBean messageFolder = (MessageFolderBean) iter.next();
            int messageCount;
            int unreadMessageCount;
            if (messageFolder.getFolderName().equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_DRAFT)) {
                // also get the draft public messages
                messageCount = DAOFactory.getMessageDAO().getNumberOfAllMessages_inMember_inFolder(memberID, messageFolder.getFolderName());
                unreadMessageCount = DAOFactory.getMessageDAO().getNumberOfUnreadAllMessages_inMember_inFolder(memberID, messageFolder.getFolderName());
            } else {
                messageCount = DAOFactory.getMessageDAO().getNumberOfNonPublicMessages_inMember_inFolder(memberID, messageFolder.getFolderName());
                unreadMessageCount = DAOFactory.getMessageDAO().getNumberOfUnreadNonPublicMessages_inMember_inFolder(memberID, messageFolder.getFolderName());
            }
            messageFolder.setMessageCount(messageCount);
            messageFolder.setUnreadMessageCount(unreadMessageCount);
        }
        request.setAttribute("FolderMessageBeans", messageFolderBeans);
    }

    public void prepareDelete(HttpServletRequest request)
        throws AssertionException, DatabaseException, AuthenticationException, BadInputException, ObjectNotFoundException {

        Locale locale = I18nUtil.getLocaleInRequest(request);

        if (MVNForumConfig.getEnablePrivateMessage() == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.private_message_disabled");
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot operate with private messages. Because the private message feature is disabled");
        }

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureIsAuthenticated();
        permission.ensureCanUseMessage();

        String folderName = ParamUtil.getParameterSafe(request, "folder", true);
        StringUtil.checkGoodName(folderName);

        if ((folderName.equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_INBOX)) ||
            (folderName.equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_SENT)) ||
            (folderName.equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_DRAFT)) ||
            (folderName.equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_TRASH))) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete_default_folder");
            throw new BadInputException(localizedMessage);
        }

        //Checking if this Folder belong to current Member
        MessageFolderBean messageFolderBean = null;
        try {
            messageFolderBean = DAOFactory.getMessageFolderDAO().getMessageFolder(folderName, onlineUser.getMemberID());
        } catch (ObjectNotFoundException onf) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.messagefolder_not_exists", new Object[] {folderName});
            throw new ObjectNotFoundException(localizedMessage);
        }

        int numberOfMessages = DAOFactory.getMessageDAO().getNumberOfNonPublicMessages_inMember_inFolder(onlineUser.getMemberID(), folderName);
        int numberOfUnreadMessages = DAOFactory.getMessageDAO().getNumberOfUnreadNonPublicMessages_inMember_inFolder(onlineUser.getMemberID(), folderName);

        request.setAttribute("MessageFolderBean", messageFolderBean);
        request.setAttribute("NumberOfMessages", new Integer(numberOfMessages));
        request.setAttribute("NumberOfUnreadMessages", new Integer(numberOfUnreadMessages));
    }

    public void processDelete(HttpServletRequest request)
        throws AssertionException, DatabaseException, AuthenticationException,
        BadInputException, ObjectNotFoundException {

        Locale locale = I18nUtil.getLocaleInRequest(request);

        if (MVNForumConfig.getEnablePrivateMessage() == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.private_message_disabled");
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot operate with private messages. Because the private message feature is disabled");
        }

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureIsAuthenticated();
        permission.ensureCanUseMessage();

        // now check the password
        MyUtil.ensureCorrectCurrentPassword(request);

        int memberID = onlineUser.getMemberID();
        String folderName = ParamUtil.getParameterSafe(request, "folder", true);
        StringUtil.checkGoodName(folderName);

        if ((folderName.equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_INBOX)) ||
            (folderName.equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_SENT)) ||
            (folderName.equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_DRAFT)) ||
            (folderName.equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_TRASH))) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_delete_default_folder");
            throw new BadInputException(localizedMessage);
        }

        try {
           DAOFactory.getMessageFolderDAO().findByPrimaryKey(folderName, memberID);
        } catch (ObjectNotFoundException onf) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.messagefolder_not_exists", new Object[] {folderName});
            throw new ObjectNotFoundException(localizedMessage);
        }

        PrivateMessageUtil.deleteMessageFolderInDatabase(folderName, memberID);
    }

    public void processUpdateOrder(HttpServletRequest request)
        throws AssertionException, DatabaseException, AuthenticationException,
        BadInputException, ObjectNotFoundException {

        Locale locale = I18nUtil.getLocaleInRequest(request);

        if (MVNForumConfig.getEnablePrivateMessage() == false) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.private_message_disabled");
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot operate with private messages. Because the private message feature is disabled");
        }

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureIsAuthenticated();
        permission.ensureCanUseMessage();

        int memberID = onlineUser.getMemberID();
        String action = ParamUtil.getParameterSafe(request, "action", true);
        String folderName = ParamUtil.getParameterSafe(request, "folder", true);
        StringUtil.checkGoodName(folderName);

        try {
           DAOFactory.getMessageFolderDAO().findByPrimaryKey(folderName, memberID);
        } catch (ObjectNotFoundException onf) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.messagefolder_not_exists", new Object[] {folderName});
            throw new ObjectNotFoundException(localizedMessage);
        }

        Timestamp now = DateUtil.getCurrentGMTTimestamp();

        if (action.equals("up")) {
            DAOFactory.getMessageFolderDAO().decreaseFolderOrder(folderName, memberID, now);
        } else if (action.equals("down")) {
            DAOFactory.getMessageFolderDAO().increaseFolderOrder(folderName, memberID, now);
        } else {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_update_order.unknown_action", new Object[] {action});
            throw new BadInputException(localizedMessage);
            //throw new BadInputException("Cannot update Order: unknown action: " + action);
        }
    }

}


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.