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

📄 onlineuserimpl.java

📁 java servlet著名论坛源代码
💻 JAVA
字号:
/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/auth/OnlineUserImpl.java,v 1.10 2004/06/29 02:17:35 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.10 $
 * $Date: 2004/06/29 02:17:35 $
 *
 * ====================================================================
 *
 * 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
 */
package com.mvnforum.auth;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Locale;

import java.awt.image.BufferedImage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mvnforum.*;
import com.mvnforum.common.MVNCaptchaService;
import com.mvnforum.db.DAOFactory;
import com.mvnforum.db.MemberBean;
import com.octo.captcha.image.ImageCaptcha;
import net.myvietnam.mvncore.exception.BadInputException;
import net.myvietnam.mvncore.util.DateUtil;

class OnlineUserImpl implements OnlineUser {

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

    private int memberID = MVNForumConstant.MEMBER_ID_OF_GUEST;

    private String memberName = "";

    private int authenticationType = AUTHENTICATION_TYPE_UNAUTHENTICATED;

    private MVNForumPermission permission = null;

    private OnlineUserAction onlineUserAction = new OnlineUserAction();

    private int memberPostsPerPage = 10;

    private int hourOffset = 0;

    /* private DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     * Igor: previous line should be: new SimpleDateFormat(..., Locale.US)
     * Otherwise won't work for users who don't have en/US as default.
     */
    private DateFormat timestampFormatter = null;
    private DateFormat dateFormatter = null;

    private Timestamp lastLogonTimestamp = null;

    private String localeName = "";

    private Locale locale = null;

    private boolean gender = true;

    private ImageCaptcha imageCaptcha = null;

    /**
     * default access constructor, prevent outsite creation
     */
    OnlineUserImpl() {
    }

    public int getMemberID() {
        return memberID;
    }

    public String getMemberName() {
        return memberName;
    }

    public boolean isGuest() {
        return ( (memberID==0) || (memberID==MVNForumConstant.MEMBER_ID_OF_GUEST) );
    }

    public boolean isMember() {
        return !isGuest();
    }

    public boolean isInvisibleMember() {
        // @todo: temp implementation
        return false;
    }

    public int getAuthenticationType() {
        return authenticationType;
    }

    public MVNForumPermission getPermission() {
        return permission;
    }

    public void reloadPermission() {
        try {
            if (isGuest()) {
                permission = MVNForumPermissionFactory.getAnonymousPermission();
            } else {
                permission = MVNForumPermissionFactory.getAuthenticatedPermission(memberID);
            }
        } catch (Exception ex) {
            log.error("Error when reload permission in OnlineUserImpl for memberID = " + memberID , ex);
        }
    }

    public void reloadProfile() {
        try {
            if (isGuest()) {
                // currently just do nothing, implement later
            } else {
                MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forViewCurrentMember(memberID);

                int timeZone = memberBean.getMemberTimeZone();
                localeName = memberBean.getMemberLanguage();
                int postsPerPage = memberBean.getMemberPostsPerPage();

                setTimeZone(timeZone);
                setLocaleName(localeName);
                setGender(memberBean.getMemberGender() != 0);
                setPostsPerPage(postsPerPage);
            }
        } catch (Exception ex) {
            log.error("Error when reload profile in OnlineUserImpl for memberID = " + memberID , ex);
        }
    }

    public OnlineUserAction getOnlineUserAction() {
        return onlineUserAction;
    }

    public java.util.Date convertGMTDate(java.util.Date gmtDate) {
        return DateUtil.convertGMTDate(gmtDate, hourOffset);
    }

    public Timestamp convertGMTTimestamp(Timestamp gmtTimestamp) {
        return DateUtil.convertGMTTimestamp(gmtTimestamp, hourOffset);
    }

    public String getGMTDateFormat(java.util.Date gmtDate) {
       return getGMTDateFormat(gmtDate, true);
    }

    public String getGMTDateFormat(java.util.Date gmtDate, boolean adjustTimeZone) {
        if (gmtDate == null) return "";

        java.util.Date date = gmtDate;
        if (adjustTimeZone) {
            date = DateUtil.convertGMTDate(gmtDate, hourOffset);
        }
        return dateFormatter.format(date);
    }

    public String getGMTTimestampFormat(Timestamp gmtTimestamp) {
       return getGMTTimestampFormat(gmtTimestamp, true);
    }

    public String getGMTTimestampFormat(Timestamp gmtTimestamp, boolean adjustTimeZone) {
        if (gmtTimestamp == null) return "";

        Timestamp timestamp = gmtTimestamp;
        if (adjustTimeZone) {
            timestamp = DateUtil.convertGMTTimestamp(gmtTimestamp, hourOffset);
        }
        return timestampFormatter.format(timestamp);
    }

    public String getLocaleName() {
        return localeName;
    }

    public Locale getLocale() {
        return locale;
    }

    public Timestamp getLastLogonTimestamp() {
        return lastLogonTimestamp;
    }

    public boolean getGender() {
        return gender;
    }

    public int getPostsPerPage() {
        return memberPostsPerPage;
    }

    /**
     * Build a new captcha, this method must be called before using some
     * action that need captcha validation.
     */
    public void buildNewCaptcha() {
        destroyCurrentCaptcha();

        // this line of code could throw Exception in case the captcha image
        // is small to hold the whole captcha
        imageCaptcha = MVNCaptchaService.getInstance().getNextImageCaptcha();
    }

    /**
     * Destroy the current captcha, this method must be called after validate
     * the captcha
     */
    public void destroyCurrentCaptcha() {
        imageCaptcha = null;
    }

    /**
     * Get the captcha image to challenge the user
     *
     * @return BufferedImage the captcha image to challenge the user
     */
    public BufferedImage getCurrentCaptchaImage() {
        if (imageCaptcha == null) {
            return null;
        }
        return (BufferedImage)(imageCaptcha.getChallenge());
    }

    /**
     * Validate the anwser of the captcha from user
     *
     * @param anwser String the captcha anwser from user
     * @return boolean true if the answer is valid, otherwise return false
     */
    public boolean validateCaptchaResponse(String anwser) {
        if (imageCaptcha == null) {
            return false;
        }
        anwser = anwser.toUpperCase();//use upper case for easier usage
        return (imageCaptcha.validateResponse(anwser)).booleanValue();
    }

    /**
     * Check to make sure that the captcha answer is correct
     *
     * @param answer String the captcha answer to check
     * @throws BadInputException in case the captcha answer is not correct
     */
    public void ensureCorrectCaptchaResponse(String answer)
        throws BadInputException {

        if (validateCaptchaResponse(answer) == false) {
            throw new BadInputException("You have entered the wrong CAPTCHA response. Cannot proceed.");
        }
    }

/*****************************************************************
 * Default-scope methods, only for internal package usage
 *****************************************************************/
    void setMemberID(int memberID) {
        if (memberID == 0) {
            this.memberID = MVNForumConstant.MEMBER_ID_OF_GUEST;
        } else {
            this.memberID = memberID;
        }
        onlineUserAction.setMemberID(this.memberID);

    }

    void setMemberName(String memberName) {
        this.memberName = memberName;
        onlineUserAction.setMemberName(memberName);
    }

    void setAuthenticationType(int authType) {
        authenticationType = authType;
    }

    /**
     * NOTE: this method SHOULD ONLY BE CALLED from OnlineUserFactory
     */
    void setPermission(MVNForumPermission permission) {
        this.permission = permission;
    }

    void setTimeZone(int timeZone) {
        if ( (timeZone >= -12) && (timeZone <= 12) ) {
            this.hourOffset = timeZone;
        }
    }

    void setLocaleName(String localeName) {
        this.localeName = localeName;

        if (localeName.length() == 0) {
            locale = MVNForumConfig.getDefaultLocale();
        } else {
            locale = MyUtil.getLocale(localeName);
        }

        // now init the 2 class variables
        dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
        timestampFormatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, locale);
    }

    void setLastLogonTimestamp(Timestamp lastLogon) {
        lastLogonTimestamp = lastLogon;
    }

    void setGender(boolean gender) {
        this.gender = gender;
    }

    void setPostsPerPage(int postsPerPage) {
        if (postsPerPage < 5) {
            postsPerPage = 5;
        }
        this.memberPostsPerPage = postsPerPage;
    }
}

⌨️ 快捷键说明

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