Search in sources :

Example 6 with Training

use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.

the class BeforeDispatchInitManager method initTrainingSubmission.

@Transactional(rollbackFor = Exception.class)
public void initTrainingSubmission(Long tid, String displayId, UserRolesVo userRolesVo, Judge judge) throws StatusForbiddenException, StatusFailException, StatusAccessDeniedException {
    Training training = trainingEntityService.getById(tid);
    if (training == null || !training.getStatus()) {
        throw new StatusFailException("该训练不存在或不允许显示!");
    }
    trainingValidator.validateTrainingAuth(training, userRolesVo);
    // 查询获取对应的pid和cpid
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    trainingProblemQueryWrapper.eq("tid", tid).eq("display_id", displayId);
    TrainingProblem trainingProblem = trainingProblemEntityService.getOne(trainingProblemQueryWrapper);
    judge.setPid(trainingProblem.getPid());
    Problem problem = problemEntityService.getById(trainingProblem.getPid());
    if (problem.getAuth() == 2) {
        throw new StatusForbiddenException("错误!当前题目不可提交!");
    }
    judge.setDisplayPid(problem.getProblemId()).setGid(training.getGid());
    // 将新提交数据插入数据库
    judgeEntityService.save(judge);
    // 非私有训练不记录
    if (!training.getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
        return;
    }
    TrainingRecord trainingRecord = new TrainingRecord();
    trainingRecord.setPid(problem.getId()).setTid(tid).setTpid(trainingProblem.getId()).setSubmitId(judge.getSubmitId()).setUid(userRolesVo.getUid());
    trainingRecordEntityService.save(trainingRecord);
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) TrainingRecord(top.hcode.hoj.pojo.entity.training.TrainingRecord) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with Training

use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.

the class AdminTrainingManager method changeTrainingStatus.

public void changeTrainingStatus(Long tid, String author, Boolean status) throws StatusForbiddenException, StatusFailException {
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和训练拥有者才能操作
    if (!isRoot && !userRolesVo.getUsername().equals(author)) {
        throw new StatusForbiddenException("对不起,你无权限操作!");
    }
    boolean isOk = trainingEntityService.saveOrUpdate(new Training().setId(tid).setStatus(status));
    if (!isOk) {
        throw new StatusFailException("修改失败");
    }
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session)

Example 8 with Training

use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.

the class AdminTrainingManager method addTraining.

@Transactional(rollbackFor = Exception.class)
public void addTraining(TrainingDto trainingDto) throws StatusFailException {
    Training training = trainingDto.getTraining();
    trainingEntityService.save(training);
    TrainingCategory trainingCategory = trainingDto.getTrainingCategory();
    if (trainingCategory.getId() == null) {
        try {
            trainingCategoryEntityService.save(trainingCategory);
        } catch (Exception ignored) {
            QueryWrapper<TrainingCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", trainingCategory.getName());
            trainingCategory = trainingCategoryEntityService.getOne(queryWrapper, false);
        }
    }
    boolean isOk = mappingTrainingCategoryEntityService.save(new MappingTrainingCategory().setTid(training.getId()).setCid(trainingCategory.getId()));
    if (!isOk) {
        throw new StatusFailException("添加失败!");
    }
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingCategory(top.hcode.hoj.pojo.entity.training.TrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with Training

use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.

the class AdminTrainingProblemManager method importTrainingRemoteOJProblem.

public void importTrainingRemoteOJProblem(String name, String problemId, Long tid) throws StatusFailException {
    QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("problem_id", name.toUpperCase() + "-" + problemId);
    Problem problem = problemEntityService.getOne(queryWrapper, false);
    // 如果该题目不存在,需要先导入
    if (problem == null) {
        Session session = SecurityUtils.getSubject().getSession();
        UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
        try {
            ProblemStrategy.RemoteProblemInfo otherOJProblemInfo = remoteProblemManager.getOtherOJProblemInfo(name.toUpperCase(), problemId, userRolesVo.getUsername());
            if (otherOJProblemInfo != null) {
                problem = remoteProblemManager.adminAddOtherOJProblem(otherOJProblemInfo, name);
                if (problem == null) {
                    throw new StatusFailException("导入新题目失败!请重新尝试!");
                }
            } else {
                throw new StatusFailException("导入新题目失败!原因:可能是与该OJ链接超时或题号格式错误!");
            }
        } catch (Exception e) {
            throw new StatusFailException(e.getMessage());
        }
    }
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    Problem finalProblem = problem;
    trainingProblemQueryWrapper.eq("tid", tid).and(wrapper -> wrapper.eq("pid", finalProblem.getId()).or().eq("display_id", finalProblem.getProblemId()));
    TrainingProblem trainingProblem = trainingProblemEntityService.getOne(trainingProblemQueryWrapper, false);
    if (trainingProblem != null) {
        throw new StatusFailException("添加失败,该题目已添加或者题目的训练展示ID已存在!");
    }
    TrainingProblem newTProblem = new TrainingProblem();
    boolean isOk = trainingProblemEntityService.saveOrUpdate(newTProblem.setTid(tid).setPid(problem.getId()).setDisplayId(problem.getProblemId()));
    if (isOk) {
        // 添加成功
        // 更新训练最近更新时间
        UpdateWrapper<Training> trainingUpdateWrapper = new UpdateWrapper<>();
        trainingUpdateWrapper.set("gmt_modified", new Date()).eq("id", tid);
        trainingEntityService.update(trainingUpdateWrapper);
        // 异步地同步用户对该题目的提交数据
        adminTrainingRecordManager.syncAlreadyRegisterUserRecord(tid, problem.getId(), newTProblem.getId());
    } else {
        throw new StatusFailException("添加失败!");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Training(top.hcode.hoj.pojo.entity.training.Training) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ProblemStrategy(top.hcode.hoj.crawler.problem.ProblemStrategy) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Session(org.apache.shiro.session.Session)

Example 10 with Training

use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.

the class TrainingRecordServiceImpl method submitTrainingProblem.

@Override
@Transactional(rollbackFor = Exception.class)
public CommonResult submitTrainingProblem(ToJudgeDto judgeDto, UserRolesVo userRolesVo, Judge judge) {
    Training training = trainingService.getById(judgeDto.getTid());
    if (training == null || !training.getStatus()) {
        return CommonResult.errorResponse("该训练不存在或不允许显示!");
    }
    CommonResult result = trainingRegisterService.checkTrainingAuth(training, userRolesVo);
    if (result != null) {
        return result;
    }
    // 查询获取对应的pid和cpid
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    trainingProblemQueryWrapper.eq("tid", judgeDto.getTid()).eq("display_id", judgeDto.getPid());
    TrainingProblem trainingProblem = trainingProblemService.getOne(trainingProblemQueryWrapper);
    judge.setPid(trainingProblem.getPid());
    Problem problem = problemService.getById(trainingProblem.getPid());
    if (problem.getAuth() == 2) {
        return CommonResult.errorResponse("错误!当前题目不可提交!", CommonResult.STATUS_FORBIDDEN);
    }
    judge.setDisplayPid(problem.getProblemId());
    // 将新提交数据插入数据库
    judgeService.saveOrUpdate(judge);
    // 非私有训练不记录
    if (!training.getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
        return null;
    }
    TrainingRecord trainingRecord = new TrainingRecord();
    trainingRecord.setPid(problem.getId()).setTid(judgeDto.getTid()).setTpid(trainingProblem.getId()).setSubmitId(judge.getSubmitId()).setUid(userRolesVo.getUid());
    trainingRecordMapper.insert(trainingRecord);
    return null;
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) TrainingRecord(top.hcode.hoj.pojo.entity.training.TrainingRecord) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) CommonResult(top.hcode.hoj.common.result.CommonResult) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Training (top.hcode.hoj.pojo.entity.training.Training)19 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)12 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)9 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)7 TrainingCategory (top.hcode.hoj.pojo.entity.training.TrainingCategory)7 Transactional (org.springframework.transaction.annotation.Transactional)6 MappingTrainingCategory (top.hcode.hoj.pojo.entity.training.MappingTrainingCategory)6 HttpSession (javax.servlet.http.HttpSession)5 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)5 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)5 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)4 Session (org.apache.shiro.session.Session)4 TrainingProblem (top.hcode.hoj.pojo.entity.training.TrainingProblem)4 TrainingRegister (top.hcode.hoj.pojo.entity.training.TrainingRegister)4 CommonResult (top.hcode.hoj.common.result.CommonResult)3 Problem (top.hcode.hoj.pojo.entity.problem.Problem)3 Async (org.springframework.scheduling.annotation.Async)2 TrainingDto (top.hcode.hoj.pojo.dto.TrainingDto)2 ContestProblem (top.hcode.hoj.pojo.entity.contest.ContestProblem)2 TrainingRecord (top.hcode.hoj.pojo.entity.training.TrainingRecord)2