Search in sources :

Example 11 with EntityNotFoundException

use of de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException in project ArTEMiS by ls1intum.

the class ProgrammingExerciseScheduleService method scheduleBuildAndTestAfterDueDateForParticipation.

private void scheduleBuildAndTestAfterDueDateForParticipation(ProgrammingExerciseStudentParticipation participation) {
    scheduleService.scheduleParticipationTask(participation, ParticipationLifecycle.BUILD_AND_TEST_AFTER_DUE_DATE, () -> {
        final ProgrammingExercise exercise = participation.getProgrammingExercise();
        SecurityUtils.setAuthorizationObject();
        try {
            log.info("Invoking scheduled task for participation {} in programming exercise with id {}.", participation.getId(), exercise.getId());
            programmingSubmissionService.triggerBuildForParticipations(List.of(participation));
        } catch (EntityNotFoundException ex) {
            log.error("Programming participation with id {} in exercise {} is no longer available in database for use in scheduled task.", participation.getId(), exercise.getId());
        }
    });
}
Also used : ProgrammingExercise(de.tum.in.www1.artemis.domain.ProgrammingExercise) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)

Example 12 with EntityNotFoundException

use of de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException in project ArTEMiS by ls1intum.

the class ProgrammingExerciseScheduleService method invokeOperationOnAllParticipationsThatSatisfy.

/**
 * Invokes the given <code>operation</code> on all student participations that satisfy the <code>condition</code>-{@link Predicate}.
 * <p>
 *
 * @param programmingExerciseId the programming exercise whose participations should be processed
 * @param operation the operation to perform
 * @param condition the condition that tests whether to invoke the operation on a participation
 * @param operationName the name of the operation, this is only used for logging
 * @return a list containing all participations for which the operation has failed with an exception
 * @throws EntityNotFoundException if the programming exercise can't be found.
 */
private List<ProgrammingExerciseStudentParticipation> invokeOperationOnAllParticipationsThatSatisfy(Long programmingExerciseId, BiConsumer<ProgrammingExercise, ProgrammingExerciseStudentParticipation> operation, Predicate<ProgrammingExerciseStudentParticipation> condition, String operationName) {
    log.info("Invoking (scheduled) task '{}' for programming exercise with id {}.", operationName, programmingExerciseId);
    ProgrammingExercise programmingExercise = programmingExerciseRepository.findWithEagerStudentParticipationsById(programmingExerciseId).orElseThrow(() -> new EntityNotFoundException("ProgrammingExercise", programmingExerciseId));
    List<ProgrammingExerciseStudentParticipation> failedOperations = new ArrayList<>();
    for (StudentParticipation studentParticipation : programmingExercise.getStudentParticipations()) {
        ProgrammingExerciseStudentParticipation programmingExerciseStudentParticipation = (ProgrammingExerciseStudentParticipation) studentParticipation;
        try {
            if (condition.test(programmingExerciseStudentParticipation)) {
                operation.accept(programmingExercise, programmingExerciseStudentParticipation);
            }
        } catch (Exception e) {
            log.error(String.format("'%s' failed for programming exercise with id %d for student repository with participation id %d", operationName, programmingExercise.getId(), studentParticipation.getId()), e);
            failedOperations.add(programmingExerciseStudentParticipation);
        }
    }
    return failedOperations;
}
Also used : ProgrammingExercise(de.tum.in.www1.artemis.domain.ProgrammingExercise) ProgrammingExerciseStudentParticipation(de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseStudentParticipation) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) ProgrammingExerciseStudentParticipation(de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseStudentParticipation) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)

Example 13 with EntityNotFoundException

use of de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException in project ArTEMiS by ls1intum.

the class TextUnitResource method getTextUnit.

/**
 * GET lectures/:lectureId/text-units/:textUnitId : gets the text unit with the specified id
 *
 * @param textUnitId the id of the textUnit to retrieve
 * @param lectureId the id of the lecture to which the unit belongs
 * @return the ResponseEntity with status 200 (OK) and with body the text unit, or with status 404 (Not Found)
 */
@GetMapping("lectures/{lectureId}/text-units/{textUnitId}")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<TextUnit> getTextUnit(@PathVariable Long textUnitId, @PathVariable Long lectureId) {
    log.debug("REST request to get TextUnit : {}", textUnitId);
    Optional<TextUnit> optionalTextUnit = textUnitRepository.findById(textUnitId);
    if (optionalTextUnit.isEmpty()) {
        throw new EntityNotFoundException("TextUnit");
    }
    TextUnit textUnit = optionalTextUnit.get();
    if (textUnit.getLecture() == null || textUnit.getLecture().getCourse() == null || !textUnit.getLecture().getId().equals(lectureId)) {
        throw new ConflictException("Input data not valid", ENTITY_NAME, "inputInvalid");
    }
    authorizationCheckService.checkHasAtLeastRoleForLectureElseThrow(Role.EDITOR, textUnit.getLecture(), null);
    return ResponseEntity.ok().body(textUnit);
}
Also used : ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) TextUnit(de.tum.in.www1.artemis.domain.lecture.TextUnit) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 14 with EntityNotFoundException

use of de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException in project ArTEMiS by ls1intum.

the class VideoUnitResource method getVideoUnit.

/**
 * GET lectures/:lectureId/video-units/:videoUnitId: gets the video unit with the specified id
 *
 * @param videoUnitId the id of the videoUnit to retrieve
 * @param lectureId the id of the lecture to which the unit belongs
 * @return the ResponseEntity with status 200 (OK) and with body the video unit, or with status 404 (Not Found)
 */
@GetMapping("lectures/{lectureId}/video-units/{videoUnitId}")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<VideoUnit> getVideoUnit(@PathVariable Long videoUnitId, @PathVariable Long lectureId) {
    log.debug("REST request to get VideoUnit : {}", videoUnitId);
    var videoUnit = videoUnitRepository.findById(videoUnitId).orElseThrow(() -> new EntityNotFoundException("VideoUnit", videoUnitId));
    if (videoUnit.getLecture() == null || videoUnit.getLecture().getCourse() == null) {
        throw new ConflictException("Lecture unit must be associated to a lecture of a course", "VideoUnit", "lectureOrCourseMissing");
    }
    if (!videoUnit.getLecture().getId().equals(lectureId)) {
        throw new ConflictException("Requested lecture unit is not part of the specified lecture", "VideoUnit", "lectureIdMismatch");
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.EDITOR, videoUnit.getLecture().getCourse(), null);
    return ResponseEntity.ok().body(videoUnit);
}
Also used : ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 15 with EntityNotFoundException

use of de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException in project ArTEMiS by ls1intum.

the class ExamRegistrationService method unregisterAllStudentFromExam.

/**
 * Unregisters all students from the exam
 *
 * @param examId the exam for which a student should be unregistered
 * @param deleteParticipationsAndSubmission whether the participations and submissions of the student should be deleted
 */
public void unregisterAllStudentFromExam(Long examId, boolean deleteParticipationsAndSubmission) {
    var exam = examRepository.findWithRegisteredUsersById(examId).orElseThrow(() -> new EntityNotFoundException("Exam", examId));
    // remove all registered students
    List<Long> userIds = new ArrayList<>();
    exam.getRegisteredUsers().forEach(user -> userIds.add(user.getId()));
    List<User> registeredStudentsList = userRepository.findAllById(userIds);
    registeredStudentsList.forEach(exam::removeRegisteredUser);
    examRepository.save(exam);
    // remove all students exams
    Set<StudentExam> studentExams = studentExamRepository.findAllWithExercisesByExamId(examId);
    studentExams.forEach(studentExam -> removeStudentExam(studentExam, deleteParticipationsAndSubmission));
    User currentUser = userRepository.getUserWithGroupsAndAuthorities();
    AuditEvent auditEvent = new AuditEvent(currentUser.getLogin(), Constants.REMOVE_ALL_USERS_FROM_EXAM, "exam=" + exam.getTitle());
    auditEventRepository.add(auditEvent);
    log.info("User {} has removed all users from the exam {} with id {}. This also deleted potentially existing student exams with all its participations and submissions.", currentUser.getLogin(), exam.getTitle(), exam.getId());
}
Also used : User(de.tum.in.www1.artemis.domain.User) AuditEvent(org.springframework.boot.actuate.audit.AuditEvent) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam)

Aggregations

EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)96 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)42 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)20 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)20 StudentExam (de.tum.in.www1.artemis.domain.exam.StudentExam)16 AccessForbiddenException (de.tum.in.www1.artemis.web.rest.errors.AccessForbiddenException)12 Exam (de.tum.in.www1.artemis.domain.exam.Exam)10 QuizExercise (de.tum.in.www1.artemis.domain.quiz.QuizExercise)10 AuditEvent (org.springframework.boot.actuate.audit.AuditEvent)10 User (de.tum.in.www1.artemis.domain.User)9 ExerciseGroup (de.tum.in.www1.artemis.domain.exam.ExerciseGroup)8 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)8 java.util (java.util)8 Collectors (java.util.stream.Collectors)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 Service (org.springframework.stereotype.Service)8 de.tum.in.www1.artemis.domain (de.tum.in.www1.artemis.domain)6 Course (de.tum.in.www1.artemis.domain.Course)6 Result (de.tum.in.www1.artemis.domain.Result)6