Search in sources :

Example 1 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project Artemis by ls1intum.

the class LectureUnitResource method updateLectureUnitsOrder.

/**
 * PUT /lectures/:lectureId/lecture-units-order
 *
 * @param lectureId           the id of the lecture for which to update the lecture unit order
 * @param orderedLectureUnits ordered lecture units
 * @return the ResponseEntity with status 200 (OK) and with body the ordered lecture units
 */
@PutMapping("/lectures/{lectureId}/lecture-units-order")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<List<LectureUnit>> updateLectureUnitsOrder(@PathVariable Long lectureId, @RequestBody List<LectureUnit> orderedLectureUnits) {
    log.debug("REST request to update the order of lecture units of lecture: {}", lectureId);
    Optional<Lecture> lectureOptional = lectureRepository.findByIdWithPostsAndLectureUnitsAndLearningGoals(lectureId);
    if (lectureOptional.isEmpty()) {
        throw new EntityNotFoundException("Lecture", lectureId);
    }
    Lecture lecture = lectureOptional.get();
    if (lecture.getCourse() == null) {
        return conflict();
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.EDITOR, lecture.getCourse(), null);
    // Ensure that exactly as many lecture units have been received as are currently related to the lecture
    if (orderedLectureUnits.size() != lecture.getLectureUnits().size()) {
        return conflict();
    }
    // Ensure that all received lecture units are already related to the lecture
    for (LectureUnit lectureUnit : orderedLectureUnits) {
        if (!lecture.getLectureUnits().contains(lectureUnit)) {
            return conflict();
        }
        // Set the lecture manually as it won't be included in orderedLectureUnits
        lectureUnit.setLecture(lecture);
        // keep bidirectional mapping between attachment unit and attachment
        if (lectureUnit instanceof AttachmentUnit) {
            ((AttachmentUnit) lectureUnit).getAttachment().setAttachmentUnit((AttachmentUnit) lectureUnit);
        }
    }
    lecture.setLectureUnits(orderedLectureUnits);
    Lecture persistedLecture = lectureRepository.save(lecture);
    return ResponseEntity.ok(persistedLecture.getLectureUnits());
}
Also used : Lecture(de.tum.in.www1.artemis.domain.Lecture) LectureUnit(de.tum.in.www1.artemis.domain.lecture.LectureUnit) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) EntityNotFoundException(de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project ArTEMiS by ls1intum.

the class LectureImportService method cloneLectureUnit.

/**
 * This helper function clones the {@code importedLectureUnit} and returns it
 *
 * @param importedLectureUnit The original lecture unit to be copied
 * @param newLecture The new lecture to which the lecture units are appended
 * @return The cloned lecture unit
 */
private LectureUnit cloneLectureUnit(final LectureUnit importedLectureUnit, final Lecture newLecture) {
    log.debug("Creating a new LectureUnit from lecture unit {}", importedLectureUnit);
    if (importedLectureUnit instanceof TextUnit) {
        TextUnit textUnit = new TextUnit();
        textUnit.setName(importedLectureUnit.getName());
        textUnit.setReleaseDate(importedLectureUnit.getReleaseDate());
        textUnit.setContent(((TextUnit) importedLectureUnit).getContent());
        return textUnit;
    } else if (importedLectureUnit instanceof VideoUnit) {
        VideoUnit videoUnit = new VideoUnit();
        videoUnit.setName(importedLectureUnit.getName());
        videoUnit.setReleaseDate(importedLectureUnit.getReleaseDate());
        videoUnit.setDescription(((VideoUnit) importedLectureUnit).getDescription());
        videoUnit.setSource(((VideoUnit) importedLectureUnit).getSource());
        return videoUnit;
    } else if (importedLectureUnit instanceof AttachmentUnit) {
        // Create and save the attachment unit, then the attachment itself, as the id is needed for file handling
        AttachmentUnit attachmentUnit = new AttachmentUnit();
        attachmentUnit.setDescription(((AttachmentUnit) importedLectureUnit).getDescription());
        attachmentUnit.setLecture(newLecture);
        lectureUnitRepository.save(attachmentUnit);
        Attachment attachment = cloneAttachment(((AttachmentUnit) importedLectureUnit).getAttachment());
        attachment.setAttachmentUnit(attachmentUnit);
        attachmentRepository.save(attachment);
        attachmentUnit.setAttachment(attachment);
        return attachmentUnit;
    } else if (importedLectureUnit instanceof ExerciseUnit) {
        // We have a dedicated exercise import system, so this is left out for now
        return null;
    }
    return null;
}
Also used : Attachment(de.tum.in.www1.artemis.domain.Attachment)

Example 3 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project ArTEMiS by ls1intum.

the class FileIntegrationTest method uploadAttachmentUnit.

private AttachmentUnit uploadAttachmentUnit(MockMultipartFile file, Long lectureId, HttpStatus expectedStatus) throws Exception {
    Lecture lecture = lectureRepo.findByIdWithPostsAndLectureUnitsAndLearningGoals(lectureId).get();
    AttachmentUnit attachmentUnit = database.createAttachmentUnit(false);
    // upload file
    JsonNode response = request.postWithMultipartFile("/api/fileUpload?keepFileName=true", file.getOriginalFilename(), "file", file, JsonNode.class, expectedStatus);
    if (expectedStatus != HttpStatus.CREATED) {
        return null;
    }
    String responsePath = response.get("path").asText();
    // move file from temp folder to correct folder
    var targetFolder = Path.of(FilePathService.getAttachmentUnitFilePath(), String.valueOf(attachmentUnit.getId())).toString();
    fileService.manageFilesForUpdatedFilePath(null, responsePath, targetFolder, lecture.getId(), true);
    var attachmentPath = targetFolder + "/" + file.getOriginalFilename();
    attachmentUnit.getAttachment().setLink(attachmentPath);
    attachmentRepo.save(attachmentUnit.getAttachment());
    return attachmentUnit;
}
Also used : AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 4 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project ArTEMiS by ls1intum.

the class AttachmentUnitResource method getAttachmentUnit.

/**
 * GET /lectures/:lectureId/attachment-units/:attachmentUnitId : gets the attachment unit with the specified id
 *
 * @param attachmentUnitId the id of the attachmentUnit 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 attachment unit, or with status 404 (Not Found)
 */
@GetMapping("/lectures/{lectureId}/attachment-units/{attachmentUnitId}")
@PreAuthorize("hasRole('EDITOR')")
public ResponseEntity<AttachmentUnit> getAttachmentUnit(@PathVariable Long attachmentUnitId, @PathVariable Long lectureId) {
    log.debug("REST request to get AttachmentUnit : {}", attachmentUnitId);
    AttachmentUnit attachmentUnit = attachmentUnitRepository.findByIdElseThrow(attachmentUnitId);
    if (attachmentUnit.getLecture() == null || attachmentUnit.getLecture().getCourse() == null) {
        throw new ConflictException("Lecture unit must be associated to a lecture of a course", "AttachmentUnit", "lectureOrCourseMissing");
    }
    if (!attachmentUnit.getLecture().getId().equals(lectureId)) {
        throw new ConflictException("Requested lecture unit is not part of the specified lecture", "AttachmentUnit", "lectureIdMismatch");
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.EDITOR, attachmentUnit.getLecture().getCourse(), null);
    return ResponseEntity.ok().body(attachmentUnit);
}
Also used : ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with AttachmentUnit

use of de.tum.in.www1.artemis.domain.lecture.AttachmentUnit in project ArTEMiS by ls1intum.

the class LectureUnitResource method deleteLectureUnit.

/**
 * DELETE lectures/:lectureId/lecture-units/:lectureUnitId
 *
 * @param lectureId     the id of the lecture to which the unit belongs
 * @param lectureUnitId the id of the lecture unit to remove
 * @return the ResponseEntity with status 200 (OK)
 */
@DeleteMapping("/lectures/{lectureId}/lecture-units/{lectureUnitId}")
@PreAuthorize("hasRole('INSTRUCTOR')")
public ResponseEntity<Void> deleteLectureUnit(@PathVariable Long lectureUnitId, @PathVariable Long lectureId) {
    log.info("REST request to delete lecture unit: {}", lectureUnitId);
    LectureUnit lectureUnit = lectureUnitRepository.findByIdWithLearningGoalsBidirectionalElseThrow(lectureUnitId);
    if (lectureUnit.getLecture() == null || lectureUnit.getLecture().getCourse() == null) {
        throw new ConflictException("Lecture unit must be associated to a lecture of a course", "LectureUnit", "lectureOrCourseMissing");
    }
    if (!lectureUnit.getLecture().getId().equals(lectureId)) {
        throw new ConflictException("Requested lecture unit is not part of the specified lecture", "LectureUnit", "lectureIdMismatch");
    }
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.INSTRUCTOR, lectureUnit.getLecture().getCourse(), null);
    String lectureUnitName;
    if (lectureUnit instanceof ExerciseUnit && ((ExerciseUnit) lectureUnit).getExercise() != null) {
        lectureUnitName = ((ExerciseUnit) lectureUnit).getExercise().getTitle();
    } else if (lectureUnit instanceof AttachmentUnit && ((AttachmentUnit) lectureUnit).getAttachment() != null) {
        lectureUnitName = ((AttachmentUnit) lectureUnit).getAttachment().getName();
    } else {
        lectureUnitName = lectureUnit.getName();
    }
    if (Objects.isNull(lectureUnitName)) {
        lectureUnitName = "lectureUnitWithoutName";
    }
    lectureUnitService.removeLectureUnit(lectureUnit);
    return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, lectureUnitName)).build();
}
Also used : ExerciseUnit(de.tum.in.www1.artemis.domain.lecture.ExerciseUnit) LectureUnit(de.tum.in.www1.artemis.domain.lecture.LectureUnit) ConflictException(de.tum.in.www1.artemis.web.rest.errors.ConflictException) AttachmentUnit(de.tum.in.www1.artemis.domain.lecture.AttachmentUnit) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

AttachmentUnit (de.tum.in.www1.artemis.domain.lecture.AttachmentUnit)16 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 ConflictException (de.tum.in.www1.artemis.web.rest.errors.ConflictException)8 LectureUnit (de.tum.in.www1.artemis.domain.lecture.LectureUnit)6 Lecture (de.tum.in.www1.artemis.domain.Lecture)4 BadRequestException (javax.ws.rs.BadRequestException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Attachment (de.tum.in.www1.artemis.domain.Attachment)2 ExerciseUnit (de.tum.in.www1.artemis.domain.lecture.ExerciseUnit)2 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URI (java.net.URI)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2