All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.kit.kastel.sdq.artemis4j.client.MappingLoaderArtemisClient Maven / Gradle / Ivy

/* Licensed under EPL-2.0 2022-2023. */
package edu.kit.kastel.sdq.artemis4j.client;

import com.fasterxml.jackson.annotation.JsonProperty;
import edu.kit.kastel.sdq.artemis4j.api.ArtemisClientException;
import edu.kit.kastel.sdq.artemis4j.api.artemis.Course;
import edu.kit.kastel.sdq.artemis4j.api.artemis.Exercise;
import edu.kit.kastel.sdq.artemis4j.api.artemis.IMappingLoader;
import edu.kit.kastel.sdq.artemis4j.api.artemis.User;
import edu.kit.kastel.sdq.artemis4j.api.artemis.assessment.Submission;
import edu.kit.kastel.sdq.artemis4j.api.artemis.exam.Exam;
import edu.kit.kastel.sdq.artemis4j.api.artemis.exam.ExerciseGroup;
import edu.kit.kastel.sdq.artemis4j.api.client.ICourseArtemisClient;
import edu.kit.kastel.sdq.artemis4j.api.client.ISubmissionsArtemisClient;
import okhttp3.OkHttpClient;
import okhttp3.Request;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MappingLoaderArtemisClient extends AbstractArtemisClient implements ICourseArtemisClient, IMappingLoader {
	private final ISubmissionsArtemisClient submissionClient;

	private final OkHttpClient client;

	public MappingLoaderArtemisClient(ISubmissionsArtemisClient submissionClient, String hostname, String token) {
		super(hostname);
		this.client = this.createClient(token);
		this.submissionClient = submissionClient;
	}

	@Override
	public List getCourses() throws ArtemisClientException {
		Request request = new Request.Builder().url(this.path(COURSES_PATHPART, "with-user-stats")).get().build();
		Course[] coursesArray = this.call(this.client, request, Course[].class);
		assert coursesArray != null;
		for (Course course : coursesArray) {
			course.init(this);
		}
		return Arrays.asList(coursesArray);
	}

	@Override
	public List getExerciseGroupsForExam(Exam artemisExam, Course course) throws ArtemisClientException {
		Request request = new Request.Builder() //
				.url(this.path(COURSES_PATHPART, course.getCourseId(), EXAMS_PATHPART, artemisExam.getExamId(), "exam-for-assessment-dashboard")).get().build();

		// need to retrieve the exerciseGroups array root node to deserialize it!
		var exerciseGroupWrapper = this.call(this.client, request, ExerciseGroupWrapper.class);
		assert exerciseGroupWrapper != null;
		List exerciseGroups = exerciseGroupWrapper.getExerciseGroups();

		for (ExerciseGroup exerciseGroup : exerciseGroups) {
			exerciseGroup.init(this, course, artemisExam);
		}
		return new ArrayList<>(exerciseGroups);
	}

	@Override
	public List getExamsForCourse(Course course) throws ArtemisClientException {
		Request request = new Request.Builder() //
				.url(this.path(COURSES_PATHPART, course.getCourseId(), EXAMS_PATHPART)).get().build();

		Exam[] examsArray = this.call(this.client, request, Exam[].class);
		assert examsArray != null;
		for (Exam exam : examsArray) {
			exam.init(this, course);
		}
		return Arrays.asList(examsArray);
	}

	@Override
	public List getGradingExercisesForCourse(Course course) throws ArtemisClientException {
		Request request = new Request.Builder() //
				.url(this.path(COURSES_PATHPART, course.getCourseId(), "with-exercises")).get().build();

		// get the part of the json that we want to deserialize
		var exerciseWrapper = this.call(this.client, request, ExerciseWrapper.class);
		assert exerciseWrapper != null;
		final List exercises = exerciseWrapper.getExercises();

		for (Exercise exercise : exercises) {
			exercise.init(this, course, null);
		}

		// Here we filter all programming exercises
		return exercises.stream().filter(Exercise::isProgramming).toList();
	}

	@Override
	public Submission getSubmissionById(Exercise exercise, int submissionId) throws ArtemisClientException {
		return this.submissionClient.getSubmissionById(exercise, submissionId);
	}

	private static class ExerciseGroupWrapper {
		@JsonProperty
		private final List exerciseGroups = new ArrayList<>();

		public List getExerciseGroups() {
			return new ArrayList<>(exerciseGroups);
		}
	}

	private static class ExerciseWrapper {
		@JsonProperty
		private final List exercises = new ArrayList<>();

		public List getExercises() {
			return new ArrayList<>(exercises);
		}
	}

	@Override
	public List getTAs(Course course) throws ArtemisClientException {
		Request request = new Request.Builder() //
				.url(this.path(COURSES_PATHPART, course.getCourseId(), "tutors")).get().build();
		User[] teachingAssistants = this.call(this.client, request, User[].class);
		assert teachingAssistants != null;
		return Arrays.asList(teachingAssistants);
	}

	@Override
	public void removeTAFromCourse(Course course, User teachingAssistant) throws ArtemisClientException {
		Request request = new Request.Builder() //
				.url(this.path(COURSES_PATHPART, course.getCourseId(), "tutors", teachingAssistant.getLogin())).delete().build();
		this.call(this.client, request, null);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy