mvc.MessageController.java Source code

Java tutorial

Introduction

Here is the source code for mvc.MessageController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mvc;

import entity.AdminMessage;
import entity.AuthorMessage;
import entity.Message;
import entity.MessageFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import mvc.parent.WebController;
import mvc.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import rights.NotRightException;
import service.MessageService;
import support.ErrorMess;
import support.ServiceResult;
import support.StringAdapter;

/**
 * ?
 *
 * @author Rice Pavel
 */
@Controller
@RequestMapping("/Message")
public class MessageController extends WebController {

    public static final String SEARCH_ADMIN_MESSAGE_URL = "/Message/searchAdmin";

    public static final String SEARCH_AUTHOR_MESSAGE_URL = "/Message/searchAuthor";

    @ModelAttribute
    public void setInfo(Map<String, Object> model) {
        setInfoForCommonPage(model);
    }

    @Autowired
    private MessageService messageService;

    /**
     *  ? 
     *
     * @param model
     * @param obj
     * @param bindRes
     * @param orderId
     * @param files
     * @return
     */
    @RequestMapping(value = "/addAdmin", params = { "ajax" })
    @ResponseBody
    public AjaxResult addAdminMessage(Map<String, Object> model, @ModelAttribute("adminMessage") AdminMessage obj,
            BindingResult bindRes, @RequestParam("orderId") Long orderId,
            @RequestParam(value = "file", required = false) MultipartFile[] files) {

        try {

            if (!hasBranchRight(orderId, "/Message/addAdmin")) {
                AjaxResult ajaxRes = new AjaxResult();
                ajaxRes.addError("??  ? ? ");
                return ajaxRes;
            }

            if (!bindRes.hasErrors()) {
                ServiceResult serviceRes = messageService.addAdminMessage(obj, files, orderId);
                return getAjaxResult(serviceRes);
            } else {
                AjaxResult ajaxRes = new AjaxResult();
                ajaxRes.addError(" ");
                return ajaxRes;
            }

        } catch (Exception e) {
            AjaxResult ajaxRes = new AjaxResult();
            ajaxRes.setError(StringAdapter.getStackExeption(e));
            ajaxRes.setErrorCode(1234);
            return ajaxRes;
        }

    }

    @RequestMapping("/countNotReadyAuthorMessages")
    @ResponseBody
    public Map countNotReadyMessages() {
        Map<String, Object> map = new HashMap();
        String orderSearchUrl = OrderController.getUrlForOrderSearch();
        Set<Long> branchIds = branchRightsHolder.getBranchIds(orderSearchUrl);
        int count = messageService.countAllNotReadyMessages(branchIds);
        map.put("count", count);
        return map;
    }

    /**
     *  ? ??  
     *
     * @param model
     * @param authorId
     * @param orderId
     * @return
     */
    @RequestMapping("/searchAuthor")
    public String findByAuthor(Map<String, Object> model, Long authorId, Long orderId) {
        List<AuthorMessage> messages = messageService.findByAuthor(orderId, authorId);
        model.put("authorMessages", messages);
        return "AuthorMessages_messageList";
    }

    /**
     *  ? 
     *
     * @param model
     * @param obj
     * @param bindRes
     * @param orderId
     * @param files
     * @param destinationAuthorId
     * @param cost
     * @return
     */
    @RequestMapping(value = "/addAuthor", params = { "ajax" })
    @ResponseBody
    public AjaxResult addAuthorMessage(Map<String, Object> model,
            @ModelAttribute("authorMessage") AuthorMessage obj, BindingResult bindRes,
            @RequestParam("orderId") Long orderId,
            @RequestParam(value = "file", required = false) MultipartFile[] files,
            @RequestParam(value = "destinationAuthorId") Long destinationAuthorId,
            @RequestParam(value = "cost", required = false) Double cost,
            @RequestParam(value = "sendToAll", required = false) String sendToAll) {

        try {
            boolean toAll = (sendToAll != null && !sendToAll.isEmpty());
            if (!hasBranchRight(orderId, "/Message/addAuthor")) {
                return new AjaxResult(
                        "??  ? ? ");
            }

            if (!bindRes.hasErrors()) {
                ServiceResult serviceRes = _addAuthorMessage(obj, files, orderId, destinationAuthorId, cost, toAll);
                return getAjaxResult(serviceRes);
            } else {
                return new AjaxResult(" ");
            }

        } catch (Throwable e) {
            return new AjaxResult(StringAdapter.getStackExeption(e));
        }

    }

    private ServiceResult _addAuthorMessage(AuthorMessage obj, MultipartFile[] files, Long orderId,
            Long destinationAuthorId, Double cost, boolean toAll) throws IOException {
        if (toAll) {
            return messageService.addAuthorMessageToAll(obj, files, orderId);
        } else {
            return messageService.addAuthorMessage(obj, files, orderId, destinationAuthorId, cost);
        }
    }

    /**
     *  ? 
     *
     * @param model
     * @param obj
     * @param bindRes
     * @param orderId
     * @param files
     * @param attr
     * @return
     * @throws IOException
     * @throws NotRightException
     */
    @RequestMapping("/addAdmin")
    public String addAdminMessage(Map<String, Object> model,
            @ModelAttribute("adminMessage") @Valid AdminMessage obj, BindingResult bindRes,
            @RequestParam("orderId") Long orderId,
            @RequestParam(value = "file", required = false) MultipartFile[] files, RedirectAttributes attr)
            throws IOException, NotRightException {

        checkBranchRight(orderId, "/Message/addAdmin");

        String errorAttrName = "errors";
        if (!bindRes.hasErrors()) {
            ServiceResult res = messageService.addAdminMessage(obj, files, orderId);
            if (res.hasErrors()) {
                attr.addFlashAttribute(errorAttrName, res.getErrors());
            }
        } else {
            attr.addFlashAttribute(errorAttrName, bindRes.getAllErrors());
        }
        return "redirect:/Order/get?orderId=" + orderId;
    }

    /**
     *  ? 
     *
     * @param model
     * @param obj
     * @param bindRes
     * @param orderId
     * @param files
     * @param attr
     * @param destinationAuthorId
     * @param cost
     * @return
     * @throws IOException
     * @throws NotRightException
     */
    @RequestMapping("/addAuthor")
    public String addAuthorMessage(Map<String, Object> model,
            @ModelAttribute("authorMessage") @Valid AuthorMessage obj, BindingResult bindRes,
            @RequestParam("orderId") Long orderId,
            @RequestParam(value = "file", required = false) MultipartFile[] files, RedirectAttributes attr,
            @RequestParam(value = "destinationAuthorId") Long destinationAuthorId,
            @RequestParam(value = "cost", required = false) Double cost,
            @RequestParam(value = "sendToAll", required = false) String sendToAll)
            throws IOException, NotRightException {

        checkBranchRight(orderId, "/Message/addAuthor");
        boolean toAll = (sendToAll != null && !sendToAll.isEmpty());
        String errorAttrName = "errors";
        if (!bindRes.hasErrors()) {
            ServiceResult res = _addAuthorMessage(obj, files, orderId, destinationAuthorId, cost, toAll);
            if (res.hasErrors()) {
                attr.addFlashAttribute(errorAttrName, res.getErrors());
            }
        } else {
            attr.addFlashAttribute(errorAttrName, bindRes.getAllErrors());
        }
        return "redirect:/Order/get?orderId=" + orderId;
    }

    /**
     *  ?
     *
     * @param model
     * @param submit
     * @param messageId
     * @param orderId
     * @return
     * @throws NotRightException
     */
    @RequestMapping("/delete")
    public String delete(Map<String, Object> model, String submit, @RequestParam("messageId") Long messageId,
            @RequestParam("orderId") Long orderId, RedirectAttributes ra) throws NotRightException {
        try {
            Message message = messageService.find(messageId);
            branchRightsHolder.checkRight("/Message/delete", message.getOrder().getBranch().getBranchId());

            messageService.delete(messageId);
        } catch (DataIntegrityViolationException e) {
            addErrorToRedirecrt(ra, ErrorMess.EXIST_RELATED_ENTITY);
        }
        return "redirect:/Order/get?orderId=" + orderId;
    }

    /**
     *  
     *
     * @param fileId
     * @param response
     * @throws FileNotFoundException
     * @throws IOException
     * @throws NotRightException
     */
    @RequestMapping("getFile")
    public void getFile(@RequestParam("fileId") Long fileId, HttpServletResponse response)
            throws FileNotFoundException, IOException, NotRightException {
        checkRightsByFileId(fileId, "/Message/getFile");
        _getFile(response, fileId);
    }

    protected void checkRightsByFileId(Long fileId, String url) throws NotRightException {
        MessageFile mf = fileService.findMessageFile(fileId);
        branchRightsHolder.checkRight(url, mf.getMessage().getOrder().getBranch().getBranchId());
    }

}