Search in sources :

Example 1 with MessageException

use of cn.edu.zjnu.acm.judge.exception.MessageException in project judge by zjnu-acm.

the class MessageService method save.

@Transactional
public void save(Long parentId, Long problemId, String userId, String title, String content) {
    long depth = 0;
    long orderNum = 0;
    final long nextId = messageMapper.nextId();
    final Message parent = parentId != null ? Optional.ofNullable(messageMapper.findOne(parentId)).orElseThrow(() -> new MessageException("No such parent message", HttpStatus.NOT_FOUND)) : null;
    if (parent != null) {
        orderNum = parent.getOrder();
        final long depth1 = parent.getDepth();
        List<Message> messages = messageMapper.findAllByThreadIdAndOrderNumGreaterThanOrderByOrderNum(parent.getThread(), parent.getOrder());
        for (Message m : messages) {
            depth = m.getDepth();
            if (depth <= depth1) {
                break;
            }
            orderNum = m.getOrder();
        }
        depth = depth1 + 1;
        messageMapper.updateOrderNumByThreadIdAndOrderNumGreaterThan(parent.getThread(), orderNum);
        ++orderNum;
    }
    messageMapper.save(nextId, parentId, orderNum, problemId, depth, userId, title, content);
    if (parent != null) {
        messageMapper.updateThreadIdByThreadId(nextId, parent.getThread());
    }
}
Also used : Message(cn.edu.zjnu.acm.judge.domain.Message) MessageException(cn.edu.zjnu.acm.judge.exception.MessageException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with MessageException

use of cn.edu.zjnu.acm.judge.exception.MessageException in project judge by zjnu-acm.

the class MailController method delete.

@GetMapping("/deletemail")
public String delete(@RequestParam("mail_id") long mailId, Authentication authentication) {
    Mail mail = mailMapper.findOne(mailId);
    if (mail == null) {
        throw new MessageException("No such mail", HttpStatus.NOT_FOUND);
    }
    if (!UserDetailsServiceImpl.isUser(authentication, mail.getTo())) {
        throw new MessageException("Sorry, invalid access", HttpStatus.FORBIDDEN);
    }
    mailMapper.delete(mailId);
    return "redirect:/mail";
}
Also used : Mail(cn.edu.zjnu.acm.judge.domain.Mail) MessageException(cn.edu.zjnu.acm.judge.exception.MessageException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with MessageException

use of cn.edu.zjnu.acm.judge.exception.MessageException in project judge by zjnu-acm.

the class ShowSourceController method showSource.

@Secured("ROLE_USER")
@GetMapping("/showsource")
@SuppressWarnings("AssignmentToMethodParameter")
public String showSource(HttpServletRequest request, @RequestParam("solution_id") long submissionId, @RequestParam(value = "style", required = false) Integer style, Authentication authentication) {
    Submission submission = submissionMapper.findOne(submissionId);
    if (submission == null) {
        throw new BusinessException(BusinessCode.SUBMISSION_NOT_FOUND, submissionId);
    }
    contestOnlyService.checkViewSource(request, submission);
    String userId = authentication != null ? authentication.getName() : null;
    if (!submissionService.canView(request, submission)) {
        throw new MessageException("You have no permission to view the source.", HttpStatus.FORBIDDEN);
    }
    String language = languageService.getLanguageName(submission.getLanguage());
    if (style == null) {
        style = userPerferenceMapper.getStyle(userId);
    } else {
        userPerferenceMapper.setStyle(userId, style);
    }
    String source = submissionMapper.findSourceById(submissionId);
    request.setAttribute("submission", submission);
    if (submission.getContest() != null) {
        request.setAttribute("contestId", submission.getContest());
    }
    request.setAttribute("language", language);
    request.setAttribute("result", ResultType.getShowsourceString(submission.getScore()));
    request.setAttribute("style", style);
    request.setAttribute("source", source);
    return "submissions/source";
}
Also used : BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) Submission(cn.edu.zjnu.acm.judge.domain.Submission) MessageException(cn.edu.zjnu.acm.judge.exception.MessageException) GetMapping(org.springframework.web.bind.annotation.GetMapping) Secured(org.springframework.security.access.annotation.Secured)

Example 4 with MessageException

use of cn.edu.zjnu.acm.judge.exception.MessageException in project judge by zjnu-acm.

the class ShowSubmissionDetailsController method showSolutionDetails.

@GetMapping("/showsolutiondetails")
public String showSolutionDetails(HttpServletRequest request, @RequestParam("solution_id") long submissionId, Authentication authentication) {
    Submission submission = submissionMapper.findOne(submissionId);
    if (submission == null) {
        throw new BusinessException(BusinessCode.SUBMISSION_NOT_FOUND, submissionId);
    }
    Long contestId = submission.getContest();
    if (contestId != null) {
        Contest contest = contestMapper.findOne(contestId);
        if (contest != null) {
            request.setAttribute("contestId", contest.getId());
        }
    }
    if (!submissionService.canView(request, submission)) {
        throw new MessageException("You have no permission to view solution '" + submissionId + "'", HttpStatus.FORBIDDEN);
    }
    String submissionDetail = submissionMapper.getSubmissionDetail(submissionId);
    if (submissionDetail == null) {
        submissionDetail = "";
    }
    String[] detailsArray = submissionDetail.split(",");
    SubmissionDetail[] details = new SubmissionDetail[detailsArray.length / 4];
    for (int i = 0; i < detailsArray.length / 4; ++i) {
        details[i] = SubmissionDetail.builder().result(ResultType.getCaseScoreDescription(Integer.parseInt(detailsArray[i << 2]))).score(detailsArray[i << 2 | 1]).time(detailsArray[i << 2 | 2]).memory(detailsArray[i << 2 | 3]).build();
    }
    request.setAttribute("details", details);
    request.setAttribute("user", submission.getUser());
    request.setAttribute("problem", submission.getProblem());
    request.setAttribute("result", ResultType.getResultDescription(submission.getScore()));
    request.setAttribute("score", submission.getScore());
    request.setAttribute("time", submission.getTime());
    request.setAttribute("memory", submission.getMemory());
    return "submissions/detail";
}
Also used : BusinessException(cn.edu.zjnu.acm.judge.exception.BusinessException) Submission(cn.edu.zjnu.acm.judge.domain.Submission) MessageException(cn.edu.zjnu.acm.judge.exception.MessageException) SubmissionDetail(cn.edu.zjnu.acm.judge.data.dto.SubmissionDetail) Contest(cn.edu.zjnu.acm.judge.domain.Contest) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with MessageException

use of cn.edu.zjnu.acm.judge.exception.MessageException in project judge by zjnu-acm.

the class ModifyUserController method update.

@PostMapping("/modifyuser")
@SuppressWarnings("AssignmentToMethodParameter")
public String update(Model model, @RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword, @RequestParam("rptPassword") String rptPassword, @RequestParam("email") String email, @RequestParam("nick") String nick, @RequestParam("school") String school, Authentication authentication) {
    if (!Objects.equals(newPassword, rptPassword)) {
        throw new MessageException("Passwords are not match");
    }
    String userId = authentication.getName();
    User user = accountService.findOne(userId);
    String password = user.getPassword();
    if (!passwordEncoder.matches(oldPassword, password)) {
        throw new MessageException("password is not correct");
    }
    if (StringUtils.isEmpty(nick)) {
        nick = userId;
    }
    if (StringUtils.isEmpty(newPassword)) {
        newPassword = oldPassword;
    } else {
        ValueCheck.checkPassword(newPassword);
    }
    if (StringUtils.hasText(email)) {
        ValueCheck.checkEmail(email);
    } else {
        email = "";
    }
    ValueCheck.checkNick(nick);
    user = User.builder().id(userId).email(email).nick(nick).password(newPassword).school(school).build();
    accountService.updateSelective(userId, user);
    model.addAttribute("user", user);
    return "modifyusersuccess";
}
Also used : User(cn.edu.zjnu.acm.judge.domain.User) MessageException(cn.edu.zjnu.acm.judge.exception.MessageException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

MessageException (cn.edu.zjnu.acm.judge.exception.MessageException)7 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 Mail (cn.edu.zjnu.acm.judge.domain.Mail)3 Submission (cn.edu.zjnu.acm.judge.domain.Submission)2 BusinessException (cn.edu.zjnu.acm.judge.exception.BusinessException)2 SubmissionDetail (cn.edu.zjnu.acm.judge.data.dto.SubmissionDetail)1 Contest (cn.edu.zjnu.acm.judge.domain.Contest)1 Message (cn.edu.zjnu.acm.judge.domain.Message)1 User (cn.edu.zjnu.acm.judge.domain.User)1 Secured (org.springframework.security.access.annotation.Secured)1 Transactional (org.springframework.transaction.annotation.Transactional)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1