From c968eacb26875e3888462ae0b936372f1b09ef7b Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Sun, 29 Dec 2024 16:16:40 +0700 Subject: [PATCH] Jerahmeel: add task to move problem to problem set (#674) --- .../judgels/JudgelsServerApplication.java | 1 + .../judgels/jerahmeel/JerahmeelComponent.java | 2 + .../problem/MoveProblemToProblemSetTask.java | 96 +++++++++++++++++++ .../jerahmeel/problem/ProblemModule.java | 29 +++++- 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problem/MoveProblemToProblemSetTask.java diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/JudgelsServerApplication.java b/judgels-backends/judgels-server-app/src/main/java/judgels/JudgelsServerApplication.java index 64c96ebf0..c2c8d0842 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/JudgelsServerApplication.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/JudgelsServerApplication.java @@ -272,6 +272,7 @@ private void runJerahmeel(JudgelsServerApplicationConfiguration config, Environm env.admin().addTask(component.deleteProblemTask()); env.admin().addTask(component.moveProblemToChapterTask()); + env.admin().addTask(component.moveProblemToProblemSetTask()); env.admin().addTask(component.problemSetStatsTask()); env.admin().addTask(component.contestStatsTask()); env.admin().addTask(component.submissionsDuplexToAwsTask()); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/JerahmeelComponent.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/JerahmeelComponent.java index 180c7210a..9d1e04049 100755 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/JerahmeelComponent.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/JerahmeelComponent.java @@ -14,6 +14,7 @@ import judgels.jerahmeel.hibernate.JerahmeelHibernateDaoModule; import judgels.jerahmeel.problem.DeleteProblemTask; import judgels.jerahmeel.problem.MoveProblemToChapterTask; +import judgels.jerahmeel.problem.MoveProblemToProblemSetTask; import judgels.jerahmeel.problem.ProblemModule; import judgels.jerahmeel.problem.ProblemResource; import judgels.jerahmeel.problem.ProblemTagResource; @@ -89,6 +90,7 @@ public interface JerahmeelComponent { DeleteProblemTask deleteProblemTask(); MoveProblemToChapterTask moveProblemToChapterTask(); + MoveProblemToProblemSetTask moveProblemToProblemSetTask(); ProblemSetStatsTask problemSetStatsTask(); ContestStatsTask contestStatsTask(); SubmissionsDuplexToAwsTask submissionsDuplexToAwsTask(); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problem/MoveProblemToProblemSetTask.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problem/MoveProblemToProblemSetTask.java new file mode 100644 index 000000000..1351662d0 --- /dev/null +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problem/MoveProblemToProblemSetTask.java @@ -0,0 +1,96 @@ +package judgels.jerahmeel.problem; + +import io.dropwizard.hibernate.UnitOfWork; +import io.dropwizard.servlets.tasks.Task; +import java.io.PrintWriter; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import judgels.jerahmeel.persistence.ChapterProblemDao; +import judgels.jerahmeel.persistence.ChapterProblemModel; +import judgels.jerahmeel.persistence.ProblemSetDao; +import judgels.jerahmeel.persistence.ProblemSetModel; +import judgels.jerahmeel.persistence.ProblemSetProblemDao; +import judgels.jerahmeel.persistence.ProblemSetProblemModel; +import judgels.jerahmeel.persistence.ProgrammingSubmissionDao; +import judgels.sandalphon.api.problem.ProblemType; +import judgels.sandalphon.persistence.ProblemDao; +import judgels.sandalphon.persistence.ProblemModel; + +public class MoveProblemToProblemSetTask extends Task { + private final ProblemDao problemDao; + private final ProblemSetDao problemSetDao; + private final ChapterProblemDao chapterProblemDao; + private final ProblemSetProblemDao problemSetProblemDao; + private final ProgrammingSubmissionDao programmingSubmissionDao; + + public MoveProblemToProblemSetTask( + ProblemDao problemDao, + ProblemSetDao problemSetDao, + ChapterProblemDao chapterProblemDao, + ProblemSetProblemDao problemSetProblemDao, + ProgrammingSubmissionDao programmingSubmissionDao) { + + super("jerahmeel-move-problem-to-problem-set"); + + this.problemDao = problemDao; + this.problemSetDao = problemSetDao; + this.chapterProblemDao = chapterProblemDao; + this.problemSetProblemDao = problemSetProblemDao; + this.programmingSubmissionDao = programmingSubmissionDao; + } + + @Override + @UnitOfWork + public void execute(Map> parameters, PrintWriter out) { + List problemSlugs = parameters.get("problemSlug"); + if (problemSlugs == null || problemSlugs.isEmpty()) { + return; + } + String problemSlug = problemSlugs.get(0); + + List toProblemSetJids = parameters.get("toProblemSetJid"); + if (toProblemSetJids == null || toProblemSetJids.isEmpty()) { + return; + } + String toProblemSetJid = toProblemSetJids.get(0); + + List aliases = parameters.get("alias"); + if (aliases == null || aliases.isEmpty()) { + return; + } + String alias = aliases.get(0); + + Optional maybeProblemModel = problemDao.selectBySlug(problemSlug); + if (maybeProblemModel.isEmpty()) { + return; + } + String problemJid = maybeProblemModel.get().jid; + + Optional maybeProblemSetModel = problemSetDao.selectByJid(toProblemSetJid); + if (maybeProblemSetModel.isEmpty()) { + return; + } + + List problemSetProblemModels = problemSetProblemDao.selectAllByProblemJid(problemJid); + if (!problemSetProblemModels.isEmpty()) { + for (ProblemSetProblemModel model : problemSetProblemModels) { + model.problemSetJid = toProblemSetJid; + model.alias = alias; + problemSetProblemDao.update(model); + } + } else { + ProblemSetProblemModel model = new ProblemSetProblemModel(); + model.problemSetJid = toProblemSetJid; + model.alias = alias; + model.problemJid = problemJid; + model.type = ProblemType.PROGRAMMING.name(); + problemSetProblemDao.insert(model); + } + + Optional maybeChapterProblemModel = chapterProblemDao.selectByProblemJid(problemJid); + maybeChapterProblemModel.ifPresent(chapterProblemDao::delete); + + programmingSubmissionDao.updateContainerJid(problemJid, toProblemSetJid); + } +} diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problem/ProblemModule.java b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problem/ProblemModule.java index c4c7ad8f0..7726f838a 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problem/ProblemModule.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/jerahmeel/problem/ProblemModule.java @@ -7,6 +7,7 @@ import judgels.jerahmeel.persistence.BundleItemSubmissionDao; import judgels.jerahmeel.persistence.ChapterDao; import judgels.jerahmeel.persistence.ChapterProblemDao; +import judgels.jerahmeel.persistence.ProblemSetDao; import judgels.jerahmeel.persistence.ProblemSetProblemDao; import judgels.jerahmeel.persistence.ProgrammingGradingDao; import judgels.jerahmeel.persistence.ProgrammingSubmissionDao; @@ -51,7 +52,7 @@ static DeleteProblemTask deleteProblemTask( @Provides @Singleton - static MoveProblemToChapterTask problemMoveToChapterTask( + static MoveProblemToChapterTask moveProblemToChapterTask( UnitOfWorkAwareProxyFactory unitOfWorkAwareProxyFactory, ProblemDao problemDao, ChapterDao chapterDao, @@ -74,4 +75,30 @@ static MoveProblemToChapterTask problemMoveToChapterTask( problemSetProblemDao, programmingSubmissionDao}); } + + @Provides + @Singleton + static MoveProblemToProblemSetTask moveProblemToProblemSetTask( + UnitOfWorkAwareProxyFactory unitOfWorkAwareProxyFactory, + ProblemDao problemDao, + ProblemSetDao problemSetDao, + ChapterProblemDao chapterProblemDao, + ProblemSetProblemDao problemSetProblemDao, + ProgrammingSubmissionDao programmingSubmissionDao) { + + return unitOfWorkAwareProxyFactory.create( + MoveProblemToProblemSetTask.class, + new Class[] { + ProblemDao.class, + ProblemSetDao.class, + ChapterProblemDao.class, + ProblemSetProblemDao.class, + ProgrammingSubmissionDao.class}, + new Object[] { + problemDao, + problemSetDao, + chapterProblemDao, + problemSetProblemDao, + programmingSubmissionDao}); + } }