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

📄 postwebhandler.java

📁 java servlet著名论坛源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

                // has the permission now, then insert to database
                try {
                    DAOFactory.getFavoriteThreadDAO().create(memberID, threadID, forumID,
                        favoriteCreationDate, favoriteType, favoriteOption,
                        favoriteStatus);
                } catch (DuplicateKeyException ex) {
                    // already add favorite thread, just ignore
                }
            }
        }

        //add watch if user checked it
        if (addWatchThread) {
            permission.ensureIsAuthenticated();
            permission.ensureIsActivated();
            if (MVNForumConfig.getEnableWatch() == false) {
                // should never happen, because if it happen, then the whole process is broken
                throw new AssertionException("Cannot add Watch because Watch feature is disabled by administrator.");
            }

            int watchType               = 0;//ParamUtil.getParameterInt(request, "WatchType");
            int watchOption             = 0;//ParamUtil.getParameterInt(request, "WatchOption");
            int watchStatus             = 0;//ParamUtil.getParameterInt(request, "WatchStatus");
            Timestamp watchCreationDate = now;
            Timestamp watchLastSentDate = now;
            Timestamp watchEndDate      = now;// @todo: check it !!!

            try {
                DAOFactory.getWatchDAO().create(memberID, 0/*watchCategoryID*/, 0/*watchForumID*/,
                                           threadID, watchType, watchOption,
                                           watchStatus, watchCreationDate, watchLastSentDate,
                                           watchEndDate);
            } catch (DuplicateKeyException ex) {
                // User try to create a duplicate watch, just ignore
            }
        }

        // now, update the Search Index
        //@todo check the performance here
        PostBean justAddedPostBean = DAOFactory.getPostDAO().getPost(postID);
        PostIndexer.scheduleAddPostTask(justAddedPostBean);

        request.setAttribute("PostBean", justAddedPostBean);
        request.setAttribute("ForumID", String.valueOf(forumID));
        request.setAttribute("ThreadID", String.valueOf(threadID));
        request.setAttribute("PostID", String.valueOf(postID));
        request.setAttribute("AttachMore", new Boolean(attachMore));
        request.setAttribute("AddFavoriteParentThread", new Boolean(addFavoriteThread));
        request.setAttribute("AddWatchParentThread", new Boolean(addWatchThread));
        request.setAttribute("IsPendingThread", new Boolean(isPendingThread));
        /**@todo: review, this variable is still reserved*/
        //request.setAttribute("ParentPostID", String.valueOf(parentPostID));

        FloodControl.increaseCount(MVNForumGlobal.FLOOD_ID_NEW_POST, currentIP);
    }

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

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

        int postID = ParamUtil.getParameterInt(request, "post");

        PostBean postBean = DAOFactory.getPostDAO().getPost(postID);
        int threadID = postBean.getThreadID();
        int forumID = postBean.getForumID();

        ForumCache.getInstance().getBean(forumID).ensureNotDisabledForum();
        permission.ensureCanReadPost(forumID);

        // to show the thread topic
        ThreadBean threadBean = DAOFactory.getThreadDAO().getBean(threadID);

        MemberBean memberBean = null;
        if (postBean.getMemberID()>0) {
            memberBean = DAOFactory.getMemberDAO().getMember_forPublic(postBean.getMemberID());
        }
        postBean.setMemberBean(memberBean);

        int postAttachCount = postBean.getPostAttachCount();
        if ( (postAttachCount > 0) && MVNForumConfig.getEnableAttachment()) {
            Collection attachBeans = DAOFactory.getAttachmentDAO().getBeans_inPost(postID);
            int actualAttachCount = attachBeans.size();

            // now check if the attachCount in talbe Post equals to the actual attachCount in table Attachment
            if (postAttachCount != actualAttachCount) {
                if (actualAttachCount != DAOFactory.getAttachmentDAO().getNumberOfBeans_inPost(postID)) {
                    throw new AssertionException("AssertionException: Serious error: cannot process Attachment Count in table Attachment");
                }
                log.warn("The attachment count in table Post and Attachment are not synchronized. In table Post = " + postAttachCount + " and in table Attachment = " + actualAttachCount + ". Synchronize to " + actualAttachCount);
                DAOFactory.getPostDAO().updateAttachCount(postID, actualAttachCount);
            }
            if (actualAttachCount > 0) {
                postBean.setAttachmentBeans(attachBeans);
            }
        }

        request.setAttribute("PostBean", postBean);
        request.setAttribute("ThreadBean", threadBean);
    }

    /**
     * then, it will be forward to addpost.jsp
     * NOTE: This method MUST NOT use parameter MessageParent (need some process to figure out)
     */
    void prepareEdit(HttpServletRequest request)
        throws ObjectNotFoundException, DatabaseException, BadInputException, AuthenticationException, AssertionException {

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

        // a guest CANNOT edit a post, because it need Authenticated Permission
        permission.ensureIsAuthenticated();

        int postID = ParamUtil.getParameterInt(request, "post");
        PostBean postBean = DAOFactory.getPostDAO().getPost(postID);
        int forumID = postBean.getForumID();

        ForumBean forumBean = ForumCache.getInstance().getBean(forumID);
        forumBean.ensureNotDisabledForum();
        forumBean.ensureNotLockedForum();

        // now check if thread is closed or locked, if it is, then cannot reply to a post
        int threadID = postBean.getThreadID();
        ThreadBean threadBean = DAOFactory.getThreadDAO().getBean(threadID);
        threadBean.ensureStatusCanEdit();

        int logonMemberID = onlineUser.getMemberID();
        int authorID      = postBean.getMemberID();

        // check constraint
        if (permission.canEditPost(forumID)) {
            // have permission, just do nothing, that is dont check the max day contraint
        } else if (logonMemberID == authorID) {// same author
            // check date here, usually must not older than 7 days
            Timestamp now = DateUtil.getCurrentGMTTimestamp();
            Timestamp postDate = postBean.getPostCreationDate();
            int maxDays = MVNForumConfig.getMaxEditDays();
            if ( (now.getTime() - postDate.getTime()) > (DateUtil.DAY * maxDays) ) {
                /** @todo choose a better Exception here */
                throw new BadInputException("You cannot edit a post which is older than " + maxDays + " days.");
            }
            /** @todo check status of this post */
            /*
            if (postBean.getPostStatus() == ?) {
                throw new BadInputException("Cannot edit message which is disable.");
            }*/
        } else {//not an author, so this user must have Edit Permission
            permission.ensureCanEditPost(forumID);// this method ALWAYS throws AuthenticationException
        }

        request.setAttribute("PostToEdit", postBean);
        request.setAttribute("action", "update");

        boolean isPreviewing = ParamUtil.getParameterBoolean(request, "preview");
        if (isPreviewing) {
            // Check if user enter some text or not
            ParamUtil.getParameter(request, "PostTopic", true);
            ParamUtil.getParameter(request, "message", true);// use message instead of MessageBody

            MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forPublic(onlineUser.getMemberID());
            request.setAttribute("MemberBean", memberBean);
        }
    }

    /**
     * @todo: log the modification
     * @todo: check the comment below, it's obsolete now :(
     * @todo: check coi messageTopic co the la optional khi reply
     * NOTE: This method MUST NOT get parameter MessageParent (need some process to figure out)
     *      so it needs to call setAttribute with messageParent for page updatepostsuccess.jsp
     */
    void processUpdate(HttpServletRequest request)
        throws ObjectNotFoundException, BadInputException, DatabaseException, CreateException,
               ForeignKeyNotFoundException, AuthenticationException, AssertionException, InterceptorException {

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

        // a guest CANNOT edit a post, because it need Authenticated Permission
        permission.ensureIsAuthenticated();

        Timestamp now   = DateUtil.getCurrentGMTTimestamp();

        int postID      = ParamUtil.getParameterInt(request, "post");// dont change

        // check constraint
        PostBean postBean = DAOFactory.getPostDAO().getPost(postID);
        int forumID = postBean.getForumID();
        int threadID = postBean.getThreadID();

        ForumBean forumBean = ForumCache.getInstance().getBean(forumID);
        forumBean.ensureNotDisabledForum();
        forumBean.ensureNotLockedForum();

        // now check if thread is locked, if it is, then cannot reply to a post
        // Please note that if the threadStatus is closed, post can still be edited
        ThreadBean threadBean = DAOFactory.getThreadDAO().getBean(threadID);
        threadBean.ensureStatusCanEdit();

        String postTopic  = ParamUtil.getParameter(request, "PostTopic", true);
        postTopic = DisableHtmlTagFilter.filter(postTopic);// always disable HTML
        InterceptorService.getInstance().validateContent(postTopic);

        String postBody   = ParamUtil.getParameter(request, "message", true);// use message instead of PostBody
        postBody = DisableHtmlTagFilter.filter(postBody);// always disable HTML
        InterceptorService.getInstance().validateContent(postBody);

        int    logonMemberID    = onlineUser.getMemberID();
        String logonMemberName  = onlineUser.getMemberName();
        int    authorID         = postBean.getMemberID();

        // check constraint
        if (permission.canEditPost(forumID)) {
            // have permission, just do nothing, that is dont check the max day contraint
        } else if (logonMemberID == authorID) {// same author
            // check date here, usually must not older than 7 days
            Timestamp postDate = postBean.getPostCreationDate();
            /** @todo config maxDays */
            int maxDays = 7;
            if ( (now.getTime() - postDate.getTime()) > (DateUtil.DAY * maxDays) ) {
                /** @todo choose a better Exception here */
                throw new BadInputException("You cannot edit a post which is older than " + maxDays + " days.");
            }
            /** @todo check status of this post */
            /*
            if (postBean.getPostStatus() == ?) {
                throw new BadInputException("Cannot edit a post which is disable.");
            }*/
        } else {//not an author, so this user must have Edit Permission
            permission.ensureCanEditPost(forumID);// this method ALWAYS throws AuthenticationException
        }

        String postLastEditIP       = request.getRemoteAddr();
        int postFormatOption        = 0;//@todo review and support it later
        int postOption              = 0;//@todo review and support it later
        int postStatus              = postBean.getPostStatus();// use old post status
        String postIcon = ParamUtil.getParameter(request, "PostIcon");
        postIcon = DisableHtmlTagFilter.filter(postIcon);// always disable HTML

        /*
         * Note that although the 2 methods below can be combined,
         * I dont do that for clearness
         */
        /** @todo log the modification here */
        DAOFactory.getPostDAO().update(postID, // primary key
                             logonMemberName, postTopic, postBody,
                             now/*postLastEditDate*/, postLastEditIP, postFormatOption,
                             postOption, postStatus, postIcon);
        DAOFactory.getPostDAO().increaseEditCount(postID);

        if (postBean.getParentPostID() == 0) {//edit a top post ( thread )
            String threadIcon = postIcon;
            DAOFactory.getThreadDAO().updateTopic_Body_Icon(threadID, postTopic, postBody, threadIcon);
        }

        boolean attachMore  = ParamUtil.getParameterBoolean(request, "AttachMore");
        boolean addFavoriteThread = ParamUtil.getParameterBoolean(request, "AddFavoriteParentThread");
        boolean addWatchThread    = ParamUtil.getParameterBoolean(request, "AddWatchParentThread");
        //add favorite thread if user checked it
        if (addFavoriteThread) {
            permission.ensureIsAuthenticated();
            //@todo: add checking of MVNForumConfig.getEnableFavoriteThread()
            // check to make sure that this user doesnt exceed his favorite max
            int currentFavoriteCount = DAOFactory.getFavoriteThreadDAO().getNumberOfBeans_inMember(logonMemberID);
            int maxFavorites = MVNForumConfig.getMaxFavoriteThread();
            if (currentFavoriteCount < maxFavorites) {
                Timestamp favoriteCreationDate = now;
                int favoriteType = 0; //@todo implement it later

⌨️ 快捷键说明

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