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

📄 postwebhandler.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

        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)
     */
    public void prepareEdit(GenericRequest 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();
        Locale locale = I18nUtil.getLocaleInRequest(request);

        int postID = GenericParamUtil.getParameterInt(request, "post");
        PostBean postBean = null;
        try {
            postBean = DAOFactory.getPostDAO().getPost(postID);
        } catch(ObjectNotFoundException ex) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.postid_not_exists", new Object[] {new Integer(postID)});
            throw new ObjectNotFoundException(localizedMessage);
        }
        int forumID = postBean.getForumID();

        ForumBean forumBean = null;
        try {
            forumBean = ForumCache.getInstance().getBean(forumID);
        } catch (ObjectNotFoundException e) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.forumid_not_exists", new Object[] {new Integer(forumID)});
            throw new ObjectNotFoundException(localizedMessage);
        }
        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 = null;
        try {
            threadBean = DAOFactory.getThreadDAO().getThread(threadID);
        } catch ( ObjectNotFoundException ex ) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.threadid_not_exists", new Object[] {new Integer(threadID)});
            throw new ObjectNotFoundException(localizedMessage);
        }
        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
            // make sure user have permission to edit his own post
            permission.ensureCanEditOwnPost(forumID);

            // 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 */
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_edit.post_is_too_old", new Object[] {new Integer(maxDays)});
                throw new BadInputException(localizedMessage);
                //throw new BadInputException("You cannot edit a post which is older than " + maxDays + " days.");
            }

            // check status of this post
            if (postBean.getPostStatus() == PostBean.POST_STATUS_DISABLED) {
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_edit_your_post.which_is_disabled");
                throw new BadInputException(localizedMessage);
                //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 = GenericParamUtil.getParameterBoolean(request, "preview");
        if (isPreviewing) {
            // Check if user enter some text or not
            GenericParamUtil.getParameter(request, "PostTopic", true);
            GenericParamUtil.getParameter(request, "message", true);// use message instead of MessageBody

            MemberBean memberBean = MemberCache.getInstance().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
     */
    public void processUpdate(GenericRequest 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();
        Locale locale = I18nUtil.getLocaleInRequest(request);

        Timestamp now   = DateUtil.getCurrentGMTTimestamp();

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

        // check constraint
        PostBean postBean = null;
        try {
            postBean = DAOFactory.getPostDAO().getPost(postID);
        } catch(ObjectNotFoundException ex) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.postid_not_exists", new Object[] {new Integer(postID)});
            throw new ObjectNotFoundException(localizedMessage);
        }
        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 = null;
        try {
            threadBean = DAOFactory.getThreadDAO().getThread(threadID);
        } catch ( ObjectNotFoundException ex ) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.threadid_not_exists", new Object[] {new Integer(threadID)});
            throw new ObjectNotFoundException(localizedMessage);
        }
        threadBean.ensureStatusCanEdit();

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

        String postBody   = GenericParamUtil.getParameter(request, "message", true);// use message instead of PostBody
        postBody = DisableHtmlTagFilter.filter(postBody);// always disable HTML
        postBody = 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
            // make sure user have permission to edit his own post
            permission.ensureCanEditOwnPost(forumID);

            // check date here, usually must not older than 7 days
            Timestamp postDate = postBean.getPostCreationDate();
            int maxDays = MVNForumConfig.getMaxEditDays();
            if ( (now.getTime() - postDate.getTime()) > (DateUtil.DAY * maxDays) ) {
                /** @todo choose a better Exception here */
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_edit.post_is_too_old", new Object[] {new Integer(maxDays)});
                throw new BadInputException(localizedMessage);
                //throw new BadInputException("You cannot edit a post which is older than " + maxDays + " days.");
            }

            // check status of this post
            if (postBean.getPostStatus() == PostBean.POST_STATUS_DISABLED) {
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_edit_your_post.which_is_disabled");
                throw new BadInputException(localizedMessage);
                //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
        }

        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 = GenericParamUtil.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  = GenericParamUtil.getParameterBoolean(request, "AttachMore");
        boolean addFavoriteThread = GenericParamUtil.getParameterBoolean(request, "AddFavoriteParentThread");
        boolean addWatchThread    = GenericParamUtil.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().getNumberOfFavoriteThreads_inMember(logonMemberID);
            int maxFavorites = MVNForumConfig.getMaxFavoriteThread();
            if (currentFavoriteCount < maxFavorites) {
                Timestamp favoriteCreationDate = now;
                int favoriteType = 0; //@todo implement it later
                int favoriteOption = 0; //@todo implement it later
                int favoriteStatus = 0; //@todo implement it later

                // now check permission the this user have the readPost permission
                permission.ensureCanReadPost(forumID);

                // has the permission now, then insert to database
                try {
                    DAOFactory.getFavoriteThreadDAO().create(logonMemberID, 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
                String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_add_watch.watch_is_disabled");
                throw new AssertionException(localizedMessage);
                //throw new AssertionException("Cannot add Watch because Watch feature is disabled by administrator.");
            }

            int watchType               = 0;//GenericParamUtil.getParameterInt(request, "WatchType");
            int watchOption             = 0;//GenericParamUtil.getParameterInt(request, "WatchOption");
            int watchStatus             = 0;//GenericParamUtil.getParameterInt(request, "WatchStatus");
            Timestamp watchCreationDate = now;

⌨️ 快捷键说明

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