Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task for moving a problem to a chapter #666

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ private void runJerahmeel(JudgelsServerApplicationConfiguration config, Environm
component.gradingResponsePoller());
}

env.admin().addTask(component.moveProblemToChapterTask());
env.admin().addTask(component.problemSetStatsTask());
env.admin().addTask(component.contestStatsTask());
env.admin().addTask(component.submissionsDuplexToAwsTask());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import judgels.jerahmeel.course.chapter.CourseChapterResource;
import judgels.jerahmeel.curriculum.CurriculumResource;
import judgels.jerahmeel.hibernate.JerahmeelHibernateDaoModule;
import judgels.jerahmeel.problem.MoveProblemToChapterTask;
import judgels.jerahmeel.problem.ProblemModule;
import judgels.jerahmeel.problem.ProblemResource;
import judgels.jerahmeel.problem.ProblemTagResource;
import judgels.jerahmeel.problemset.ProblemSetResource;
Expand Down Expand Up @@ -59,6 +61,7 @@
GabrielClientModule.class,

// Features
ProblemModule.class,
SubmissionModule.class,
ItemSubmissionModule.class,
StatsModule.class
Expand All @@ -82,6 +85,8 @@ public interface JerahmeelComponent {

JudgelsScheduler scheduler();
GradingResponsePoller gradingResponsePoller();

MoveProblemToChapterTask moveProblemToChapterTask();
ProblemSetStatsTask problemSetStatsTask();
ContestStatsTask contestStatsTask();
SubmissionsDuplexToAwsTask submissionsDuplexToAwsTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import judgels.jerahmeel.persistence.ProgrammingSubmissionModel;
import judgels.persistence.hibernate.HibernateDaoData;
import judgels.sandalphon.hibernate.AbstractProgrammingSubmissionHibernateDao;
import org.hibernate.query.Query;

public class ProgrammingSubmissionHibernateDao
extends AbstractProgrammingSubmissionHibernateDao<ProgrammingSubmissionModel>
Expand All @@ -19,4 +20,16 @@ public ProgrammingSubmissionHibernateDao(HibernateDaoData data) {
public ProgrammingSubmissionModel createSubmissionModel() {
return new ProgrammingSubmissionModel();
}

@Override
public void updateContainer(String problemJid, String containerJid) {
Query query = currentSession().createQuery(
"UPDATE jerahmeel_programming_submission "
+ "SET containerJid = :containerJid "
+ "WHERE problemJid = :problemJid");

query.setParameter("containerJid", containerJid);
query.setParameter("problemJid", problemJid);
query.executeUpdate();
}
}
Original file line number Diff line number Diff line change
@@ -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.ChapterDao;
import judgels.jerahmeel.persistence.ChapterModel;
import judgels.jerahmeel.persistence.ChapterProblemDao;
import judgels.jerahmeel.persistence.ChapterProblemModel;
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 MoveProblemToChapterTask extends Task {
private final ProblemDao problemDao;
private final ChapterDao chapterDao;
private final ChapterProblemDao chapterProblemDao;
private final ProblemSetProblemDao problemSetProblemDao;
private final ProgrammingSubmissionDao programmingSubmissionDao;

public MoveProblemToChapterTask(
ProblemDao problemDao,
ChapterDao chapterDao,
ChapterProblemDao chapterProblemDao,
ProblemSetProblemDao problemSetProblemDao,
ProgrammingSubmissionDao programmingSubmissionDao) {

super("jerahmeel-move-problem-to-chapter");

this.problemDao = problemDao;
this.chapterDao = chapterDao;
this.chapterProblemDao = chapterProblemDao;
this.problemSetProblemDao = problemSetProblemDao;
this.programmingSubmissionDao = programmingSubmissionDao;
}

@Override
@UnitOfWork
public void execute(Map<String, List<String>> parameters, PrintWriter out) {
List<String> problemSlugs = parameters.get("problemSlug");
if (problemSlugs == null || problemSlugs.isEmpty()) {
return;
}
String problemSlug = problemSlugs.get(0);

List<String> toChapterJids = parameters.get("toChapterJid");
if (toChapterJids == null || toChapterJids.isEmpty()) {
return;
}
String toChapterJid = toChapterJids.get(0);

List<String> aliases = parameters.get("alias");
if (aliases == null || aliases.isEmpty()) {
return;
}
String alias = aliases.get(0);

Optional<ProblemModel> maybeProblemModel = problemDao.selectBySlug(problemSlug);
if (maybeProblemModel.isEmpty()) {
return;
}
String problemJid = maybeProblemModel.get().jid;

Optional<ChapterModel> maybeChapterModel = chapterDao.selectByJid(toChapterJid);
if (maybeChapterModel.isEmpty()) {
return;
}

Optional<ChapterProblemModel> maybeChapterProblemModel = chapterProblemDao.selectByProblemJid(problemJid);
if (maybeChapterProblemModel.isPresent()) {
ChapterProblemModel model = maybeChapterProblemModel.get();

model.chapterJid = toChapterJid;
model.alias = alias;
chapterProblemDao.update(model);
} else {
ChapterProblemModel model = new ChapterProblemModel();
model.chapterJid = toChapterJid;
model.alias = alias;
model.problemJid = problemJid;
model.type = ProblemType.PROGRAMMING.name();
chapterProblemDao.insert(model);
}

List<ProblemSetProblemModel> problemSetProblemModels = problemSetProblemDao.selectAllByProblemJid(problemJid);
problemSetProblemModels.forEach(problemSetProblemDao::delete);

programmingSubmissionDao.updateContainer(problemJid, toChapterJid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package judgels.jerahmeel.problem;

import dagger.Module;
import dagger.Provides;
import io.dropwizard.hibernate.UnitOfWorkAwareProxyFactory;
import javax.inject.Singleton;
import judgels.jerahmeel.persistence.ChapterDao;
import judgels.jerahmeel.persistence.ChapterProblemDao;
import judgels.jerahmeel.persistence.ProblemSetProblemDao;
import judgels.jerahmeel.persistence.ProgrammingSubmissionDao;
import judgels.sandalphon.persistence.ProblemDao;

@Module
public class ProblemModule {
private ProblemModule() {}

@Provides
@Singleton
static MoveProblemToChapterTask problemMoveToChapterTask(
UnitOfWorkAwareProxyFactory unitOfWorkAwareProxyFactory,
ProblemDao problemDao,
ChapterDao chapterDao,
ChapterProblemDao chapterProblemDao,
ProblemSetProblemDao problemSetProblemDao,
ProgrammingSubmissionDao programmingSubmissionDao) {

return unitOfWorkAwareProxyFactory.create(
MoveProblemToChapterTask.class,
new Class<?>[] {
ProblemDao.class,
ChapterDao.class,
ChapterProblemDao.class,
ProblemSetProblemDao.class,
ProgrammingSubmissionDao.class},
new Object[] {
problemDao,
chapterDao,
chapterProblemDao,
problemSetProblemDao,
programmingSubmissionDao});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public Map<String, Long> selectCounts(String containerJid, String userJid, Colle
.collect(Collectors.toMap(tuple -> tuple.get(0, String.class), tuple -> tuple.get(1, Long.class)));
}

@Override
public void updateContainer(String problemJid, String containerJid) {
throw new UnsupportedOperationException();
}

@Override
public Collection<String> dump(PrintWriter output, String containerJid) {
List<M> results = select().whereContainerIs(containerJid).orderBy(Model_.ID, OrderDir.ASC).all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface BaseProgrammingSubmissionDao<M extends AbstractProgrammingSubmi
M createSubmissionModel();
BaseProgrammingSubmissionQueryBuilder<M> select();
Map<String, Long> selectCounts(String containerJid, String userJid, Collection<String> problemJids);
void updateContainer(String problemJid, String containerJid);
Collection<String> dump(PrintWriter output, String containerJid);

interface BaseProgrammingSubmissionQueryBuilder<M extends AbstractProgrammingSubmissionModel> extends QueryBuilder<M> {
Expand Down