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

📄 kgutils.java

📁 java servlet著名论坛源代码
💻 JAVA
字号:
/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/common/kg/KGUtils.java,v 1.4 2004/03/27 09:51:39 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.4 $
 * $Date: 2004/03/27 09:51:39 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2004 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
 * @author: Igor Manic   imanic@users.sourceforge.net
 */
package com.mvnforum.common.kg;

import java.sql.Date;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import com.mvnforum.*;
import com.mvnforum.auth.*;
import com.mvnforum.common.SendMailUtil;
import com.mvnforum.db.DAOFactory;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.interceptor.InterceptorService;
import net.myvietnam.mvncore.security.Encoder;
import net.myvietnam.mvncore.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.myvietnam.mvncore.exception.DatabaseException;
import net.myvietnam.mvncore.exception.ObjectNotFoundException;
import net.myvietnam.mvncore.exception.ForeignKeyNotFoundException;
import net.myvietnam.mvncore.exception.DuplicateKeyException;
import net.myvietnam.mvncore.exception.CreateException;

/**
 * NOTE for KG
 * When deploy mvnForum, below is the check list:
 * - Set the loginID Interceptor
 * - MVNForumConfig.ENABLE_AUTO_FORUM_OWNER = true
 * - Emoticon = false
 * - Attachment = false
 * - Rss = false
 */
public class KGUtils {

    public static final String KG_PREFIX = "kg_";

    public static final int KG_FORUM_ADMIN_GROUP = 4;

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

    private KGUtils() {
    }

    public static void addNewMember(HttpServletRequest request, String memberName, String memberPassword, String memberEmail, Date memberBirthday)
        throws BadInputException, ObjectNotFoundException, CreateException, DatabaseException, DuplicateKeyException,
        ForeignKeyNotFoundException, InterceptorException {

        Timestamp now = DateUtil.getCurrentGMTTimestamp();

        memberName = KG_PREFIX + memberName;
        StringUtil.checkGoodName(memberName);
        if (memberName.length() > MVNForumGlobal.MAX_MEMBER_LOGIN_LENGTH) {
            throw new BadInputException("MemberName cannot be longer than 30 characters.");
        }

        memberPassword       = Encoder.getMD5_Base64(memberPassword);

        if (memberEmail.length() > MVNForumGlobal.MAX_MEMBER_EMAIL_LENGTH) {
            throw new BadInputException("MemberEmail cannot be longer than 60 characters.");
        }
        String memberFirstEmail     = memberEmail;
        InterceptorService.getInstance().validateMail(memberFirstEmail);

        int memberEmailVisible      = 0;
        int memberNameVisible       = 1;
        String memberFirstIP        = request.getRemoteAddr();
        String memberLastIP         = memberFirstIP;
        int memberOption            = 0;//@todo review and support it later
        int memberStatus            = 0;// @todo review and support it later, ex: should it be active or not?
        String memberActivateCode   = "";// @todo review and support it later
        int memberMessageOption     = 0;// @todo review and support it later
        int memberPostsPerPage      = 10; //default for all preregistered users
        if (memberBirthday == null) {
            memberBirthday          = new java.sql.Date(now.getTime());
        }

        DAOFactory.getMemberDAO().create(memberName, memberPassword, memberFirstEmail,
                                   memberEmail, memberEmailVisible, memberNameVisible,
                                   memberFirstIP, memberLastIP, 0/*memberViewCount*/,
                                   0/*memberPostCount*/, now/*memberCreationDate*/, now/*memberModifiedDate*/,
                                   now/*memberLastLogon*/, memberOption, memberStatus,
                                   memberActivateCode, ""/*memberTempPassword*/, 0/*memberMessageCount*/,
                                   memberMessageOption, memberPostsPerPage, 0/*memberWarnCount*/,
                                   0/*memberVoteCount*/, 0/*memberVoteTotalStars*/, 0/*memberRewardPoints*/,
                                   ""/*memberTitle*/, 0/*memberTimeZone*/, ""/*memberSignature*/,
                                   ""/*memberAvatar*/, ""/*memberSkin*/, ""/*memberLanguage*/,
                                   " "/*memberFirstname*/, " "/*memberLastname*/, 1/*memberGender*/,
                                   memberBirthday, ""/*memberAddress*/, ""/*memberCity*/,
                                   ""/*memberState*/, ""/*memberCountry*/, ""/*memberPhone*/,
                                   ""/*memberMobile*/, ""/*memberFax*/, ""/*memberCareer*/,
                                   ""/*memberHomepage*/, ""/*memberYahoo*/, ""/*memberAol*/,
                                   ""/*memberIcq*/, ""/*memberMsn*/, ""/*memberCoolLink1*/,
                                   ""/*memberCoolLink2*/);

        // Now, create 2 default folders for each member
        int memberID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName);
        DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_INBOX, memberID, 0, now, now);
        DAOFactory.getMessageFolderDAO().create(MVNForumConstant.MESSAGE_FOLDER_SENT, memberID, 0, now, now);

        // now, if require activation, then we will send mail
        // Note that because after this page succeed,
        // we redirect to usermanagement so not use mvnforum.mail.failed now
        if (MVNForumConfig.getRequireActivation()) {
            String serverName = ParamUtil.getServer2(request);
            try {
                SendMailUtil.sendActivationCodeEmail(memberID, serverName);
            } catch (Exception ex) {
                log.error("Cannot send mail after registration!", ex);
                request.setAttribute("mvnforum.mail.failed", "Cannot send activation email after registration!");
                //@todo: save the error message to displayed later
            }
        }
    }

    public static void grantPermission(String memberName, int forumID)
        throws DatabaseException, ObjectNotFoundException, ForeignKeyNotFoundException, DuplicateKeyException, CreateException {

        memberName = KG_PREFIX + memberName;
        int memberID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName);
        DAOFactory.getMemberForumDAO().create(memberID, forumID, MVNForumPermission.PERMISSION_FORUM_MODERATOR);
    }

    public static void revokePermission(String memberName, int forumID)
        throws DatabaseException, ObjectNotFoundException, ForeignKeyNotFoundException, DuplicateKeyException, CreateException {

        memberName = KG_PREFIX + memberName;
        int memberID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName);
        DAOFactory.getMemberForumDAO().delete(memberID, forumID, MVNForumPermission.PERMISSION_FORUM_MODERATOR);
    }

    public static void grantForumAdminPermission(String memberName)
        throws DatabaseException, ForeignKeyNotFoundException, DuplicateKeyException, CreateException {

        memberName = KG_PREFIX + memberName;
        Timestamp now = DateUtil.getCurrentGMTTimestamp();
        DAOFactory.getMemberGroupDAO().create(KG_FORUM_ADMIN_GROUP, memberName, 0/* default privilege*/, now/*creationDate*/, now/*modifiedDate*/);
    }

    /*
    public static void main(String[] args) {
        try {
            addNewMember(null, "test", "test", "test@kiengiang.gov.vn", null);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            grantPermission("test", 1);
        } catch (Exception ex1) {
            ex1.printStackTrace();
        }
    }*/

}

⌨️ 快捷键说明

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