/*
 * 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.comment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import ru.org.linux.auth.AccessViolationException;
import ru.org.linux.site.Template;
import ru.org.linux.search.SearchQueueSender;
import ru.org.linux.site.*;
import ru.org.linux.topic.TopicDao;
import ru.org.linux.user.UserDao;
import ru.org.linux.topic.Topic;
import ru.org.linux.user.User;
import ru.org.linux.user.UserErrorException;
import ru.org.linux.user.UserNotFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Controller
public class DeleteCommentController {
  private SearchQueueSender searchQueueSender;
  private CommentDao commentDao;
  private TopicDao messageDao;
  private UserDao userDao;
  private CommentPrepareService prepareService;

  private static final int DELETE_PERIOD = 60 * 60 * 1000; // milliseconds

  @Autowired
  @Required
  public void setSearchQueueSender(SearchQueueSender searchQueueSender) {
    this.searchQueueSender = searchQueueSender;
  }

  @Autowired
  public void setMessageDao(TopicDao messageDao) {
    this.messageDao = messageDao;
  }

  @Autowired
  public void setCommentDao(CommentDao commentDao) {
    this.commentDao = commentDao;
  }

  @Autowired
  public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
  }

  @Autowired
  public void setPrepareService(CommentPrepareService prepareService) {
    this.prepareService = prepareService;
  }

  @RequestMapping(value = "/delete_comment.jsp", method = RequestMethod.GET)
  public ModelAndView showForm(
    HttpSession session,
    HttpServletRequest request,
    @RequestParam("msgid") int msgid
  ) throws Exception {
    Map<String, Object> params = new HashMap<String, Object>();

    if (!Template.isSessionAuthorized(session)) {
      throw new AccessViolationException("нет авторизованы");
    }

    Template tmpl = Template.getTemplate(request);

    params.put("msgid", msgid);

    Comment comment = commentDao.getById(msgid);

    if (comment.isDeleted()) {
      throw new UserErrorException("комментарий уже удален");
    }

    int topicId = comment.getTopicId();

    Topic topic = messageDao.getById(topicId);

    if (topic.isDeleted()) {
      throw new AccessViolationException("тема удалена");
    }

    params.put("topic", topic);

    CommentList comments = commentDao.getCommentList(topic, tmpl.isModeratorSession());

    CommentFilter cv = new CommentFilter(comments);

    List<Comment> list = cv.getCommentsSubtree(msgid);

    params.put("commentsPrepared", prepareService.prepareCommentList(comments, list, request.isSecure()));
    params.put("comments", comments);

    return new ModelAndView("delete_comment", params);
  }

  @RequestMapping(value = "/delete_comment.jsp", method = RequestMethod.POST)
  public ModelAndView deleteComments(
    @RequestParam("msgid") int msgid,
    @RequestParam("reason") String reason,
    @RequestParam(value="bonus", defaultValue="0") int bonus,
    HttpSession session,
    HttpServletRequest request
  ) throws Exception {
    if (bonus < 0 || bonus > 20) {
      throw new BadParameterException("неправильный размер штрафа");
    }

    if (!Template.isSessionAuthorized(session)) {
      throw new AccessViolationException("нет авторизации");
    }

    Template tmpl = Template.getTemplate(request);

    tmpl.updateCurrentUser(userDao);

    User user = tmpl.getCurrentUser();
    user.checkBlocked();
    user.checkAnonymous();

    Comment comment = commentDao.getById(msgid);
    Topic topic = messageDao.getById(comment.getTopicId());

    if (comment.isDeleted()) {
      throw new UserErrorException("комментарий уже удален");
    }

    boolean perm = false;
    boolean selfDel = false;

    if (comment.getUserid() == user.getId()) {
      if(!user.isModerator()) {
        perm = (System.currentTimeMillis() - comment.getPostdate().getTime()) < DELETE_PERIOD;
        if (perm) {
          selfDel = true;
        }
      } else {
        bonus = 0;
      }
    }

    if (!perm && user.isModerator()) {
      perm = true;
    }

    if (!perm) {
      user.checkDelete();
    }

    StringBuilder out = new StringBuilder();

    LinkedList<Integer> deleted = new LinkedList<Integer>();
    deleted.add(msgid);

    if (!selfDel) {
      List<Integer> deletedReplys = commentDao.deleteReplys(msgid, user, bonus > 2);
      if (!deletedReplys.isEmpty()) {
        out.append("Удаленные ответы: ").append(deletedReplys).append("<br>");
      }

      deleted.addAll(deletedReplys);

      if (commentDao.deleteComment(msgid, reason, user, -bonus)) {
        out.append("Сообщение ").append(msgid).append(" удалено");
      }
    } else {
      if (commentDao.deleteComment(msgid, reason, user, 0)) {
        out.append("Сообщение ").append(msgid).append(" удалено");
      } else {
        out.append("Сообщение ").append(msgid).append(" уже было удалено");
      }
    }

    searchQueueSender.updateComment(deleted);

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("message", "Удалено успешно");
    params.put("bigMessage", out.toString());

    params.put("link", topic.getLink());

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

  @ExceptionHandler({ScriptErrorException.class, UserErrorException.class, AccessViolationException.class})
  @ResponseStatus(HttpStatus.FORBIDDEN)
  public ModelAndView handleUserNotFound(Exception ex, HttpServletRequest request, HttpServletResponse response) {
    ModelAndView mav = new ModelAndView("error-good-penguin");
    mav.addObject("msgTitle", "Ошибка: " + ex.getMessage());
    mav.addObject("msgHeader", ex.getMessage());
    mav.addObject("msgMessage", "");
    return mav;
  }

}

@@ -2887,13 +2887,12 @@
 %D0%BE%D1%80%D0%B8%D0%B7
-%D0%BE%D0%B2%D0%B0%D0%BD%D1%8B
+%D0%B0%D1%86%D0%B8%D0%B8
 %22);%0A

fix очепятко