ru.org.linux.topic.TopicModificationController.java Source code

Java tutorial

Introduction

Here is the source code for ru.org.linux.topic.TopicModificationController.java

Source

/*
 * Copyright 1998-2012 Linux.org.ru
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package ru.org.linux.topic;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import ru.org.linux.auth.AccessViolationException;
import ru.org.linux.group.Group;
import ru.org.linux.group.GroupDao;
import ru.org.linux.section.Section;
import ru.org.linux.section.SectionService;
import ru.org.linux.site.Template;
import ru.org.linux.user.User;
import ru.org.linux.user.UserErrorException;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

@Controller
public class TopicModificationController {
    private static final Log logger = LogFactory.getLog(TopicModificationController.class);

    @Autowired
    private TopicPrepareService prepareService;

    @Autowired
    private TopicDao messageDao;

    @Autowired
    private SectionService sectionService;

    @Autowired
    private GroupDao groupDao;

    @RequestMapping(value = "/setpostscore.jsp", method = RequestMethod.GET)
    public ModelAndView showForm(ServletRequest request, @RequestParam int msgid) throws Exception {
        Template tmpl = Template.getTemplate(request);

        if (!tmpl.isModeratorSession()) {
            throw new AccessViolationException("Not moderator");
        }

        ModelAndView mv = new ModelAndView("setpostscore");
        Topic message = messageDao.getById(msgid);
        mv.addObject("message", message);
        mv.addObject("group", groupDao.getGroup(message.getGroupId()));

        return mv;
    }

    @RequestMapping(value = "/setpostscore.jsp", method = RequestMethod.POST)
    public ModelAndView modifyTopic(ServletRequest request, @RequestParam int msgid, @RequestParam int postscore,
            @RequestParam(defaultValue = "false") boolean sticky,
            @RequestParam(defaultValue = "false") boolean notop) throws Exception {
        Template tmpl = Template.getTemplate(request);

        if (!tmpl.isModeratorSession()) {
            throw new AccessViolationException("Not moderator");
        }

        if (postscore < TopicPermissionService.POSTSCORE_UNRESTRICTED) {
            throw new UserErrorException("invalid postscore " + postscore);
        }

        if (postscore > TopicPermissionService.POSTSCORE_UNRESTRICTED
                && postscore < TopicPermissionService.POSTSCORE_REGISTERED_ONLY) {
            throw new UserErrorException("invalid postscore " + postscore);
        }

        if (postscore > TopicPermissionService.POSTSCORE_MODERATORS_ONLY) {
            throw new UserErrorException("invalid postscore " + postscore);
        }

        User user = tmpl.getCurrentUser();
        user.checkCommit();

        Topic msg = messageDao.getById(msgid);

        messageDao.setTopicOptions(msg, postscore, sticky, notop);

        StringBuilder out = new StringBuilder();

        if (msg.getPostScore() != postscore) {
            out.append("?   ?: ")
                    .append(getPostScoreInfoFull(postscore)).append("<br>");
            logger.info("?   ? " + postscore + " ? "
                    + msgid + "  " + user.getNick());
        }

        if (msg.isSticky() != sticky) {
            out.append("?  sticky: ").append(sticky).append("<br>");
            logger.info("?  sticky: " + sticky);
        }

        if (msg.isNotop() != notop) {
            out.append("?  notop: ").append(notop).append("<br>");
            logger.info("?  notop: " + notop);
        }

        ModelAndView mv = new ModelAndView("action-done");
        mv.getModel().put("message", " ");
        mv.getModel().put("bigMessage", out.toString());
        mv.getModel().put("link", msg.getLink());

        return mv;
    }

    @RequestMapping(value = "/mtn.jsp", method = RequestMethod.GET)
    public ModelAndView moveTopicForm(ServletRequest request, @RequestParam int msgid) throws Exception {
        Template tmpl = Template.getTemplate(request);

        if (!tmpl.isModeratorSession()) {
            throw new AccessViolationException("Not authorized");
        }

        ModelAndView mv = new ModelAndView("mtn");

        Topic message = messageDao.getById(msgid);
        Section section = sectionService.getSection(message.getSectionId());

        mv.getModel().put("message", message);

        mv.getModel().put("groups", groupDao.getGroups(section));

        return mv;
    }

    @RequestMapping(value = "/mt.jsp", method = RequestMethod.POST)
    public ModelAndView moveTopic(ServletRequest request, @RequestParam int msgid,
            @RequestParam("moveto") int newgr) throws Exception {
        Template tmpl = Template.getTemplate(request);

        if (!tmpl.isModeratorSession()) {
            throw new AccessViolationException("Not moderator");
        }

        Topic msg = messageDao.getById(msgid);

        if (msg.isDeleted()) {
            throw new AccessViolationException(" ");
        }

        Group newGrp = groupDao.getGroup(newgr);

        if (msg.getGroupId() != newGrp.getId()) {
            messageDao.moveTopic(msg, newGrp, tmpl.getCurrentUser());
        }

        return new ModelAndView(new RedirectView(msg.getLinkLastmod()));
    }

    @RequestMapping(value = "/mt.jsp", method = RequestMethod.GET)
    public ModelAndView moveTopicFormForum(ServletRequest request, @RequestParam int msgid) throws Exception {
        Template tmpl = Template.getTemplate(request);

        if (!tmpl.isModeratorSession()) {
            throw new AccessViolationException("Not authorized");
        }

        ModelAndView mv = new ModelAndView("mtn");

        Topic message = messageDao.getById(msgid);

        mv.getModel().put("message", message);

        Section section = sectionService.getSection(Section.SECTION_FORUM);

        mv.getModel().put("groups", groupDao.getGroups(section));

        return mv;
    }

    @RequestMapping(value = "/uncommit.jsp", method = RequestMethod.GET)
    public ModelAndView uncommitForm(HttpServletRequest request, @RequestParam int msgid) throws Exception {
        Template tmpl = Template.getTemplate(request);

        if (!tmpl.isModeratorSession()) {
            throw new AccessViolationException("Not authorized");
        }

        Topic message = messageDao.getById(msgid);

        checkUncommitable(message);

        ModelAndView mv = new ModelAndView("uncommit");
        mv.getModel().put("message", message);
        mv.getModel().put("preparedMessage",
                prepareService.prepareTopic(message, request.isSecure(), tmpl.getCurrentUser()));

        return mv;
    }

    @RequestMapping(value = "/uncommit.jsp", method = RequestMethod.POST)
    public ModelAndView uncommit(HttpServletRequest request, @RequestParam int msgid) throws Exception {
        Template tmpl = Template.getTemplate(request);

        if (!tmpl.isModeratorSession()) {
            throw new AccessViolationException("Not authorized");
        }

        Topic message = messageDao.getById(msgid);

        checkUncommitable(message);

        messageDao.uncommit(message);

        logger.info("  ?? " + msgid
                + "  " + tmpl.getNick());

        return new ModelAndView("action-done", "message", " ");
    }

    private static void checkUncommitable(Topic message) throws AccessViolationException {
        if (message.isExpired()) {
            throw new AccessViolationException(
                    "? ?? ? ??");
        }

        if (message.isDeleted()) {
            throw new AccessViolationException("? ");
        }

        if (!message.isCommited()) {
            throw new AccessViolationException("?  ");
        }
    }

    public static String getPostScoreInfoFull(int postscore) {
        String info = TopicPermissionService.getPostScoreInfo(postscore);
        if (info.isEmpty()) {
            return " ";
        } else {
            return info;
        }
    }
}