Search in sources :

Example 6 with ArchivalReportEntry

use of de.tum.in.www1.artemis.service.archival.ArchivalReportEntry in project Artemis by ls1intum.

the class SubmissionExportService method createZipFileFromParticipations.

/**
 * Creates a zip file from a list of participations for an exercise.
 *
 * The outputDir is used to store the zip file and temporary files used for zipping so make
 * sure to delete it if it's no longer used.
 *
 * @param exercise the exercise in question
 * @param participations a list of participations to include
 * @param enableFilterAfterDueDate true, if all submissions that have been submitted after the due date should not be included in the file
 * @param lateSubmissionFilter an optional date filter for submissions
 * @param outputDir directory to store the temporary files in
 * @param exportErrors a list of errors for submissions that couldn't be exported and are not included in the file
 * @param reportData   a list of all exercises and their statistics
 * @return the zipped file
 * @throws IOException if an error occurred while zipping
 */
private Optional<File> createZipFileFromParticipations(Exercise exercise, List<StudentParticipation> participations, boolean enableFilterAfterDueDate, @Nullable ZonedDateTime lateSubmissionFilter, Path outputDir, List<String> exportErrors, List<ArchivalReportEntry> reportData) throws IOException {
    Course course = exercise.getCourseViaExerciseGroupOrCourseMember();
    // Create unique name for directory
    String zipGroupName = course.getShortName() + "-" + exercise.getTitle() + "-" + exercise.getId();
    String cleanZipGroupName = fileService.removeIllegalCharacters(zipGroupName);
    String zipFileName = cleanZipGroupName + "-" + ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-Hmss")) + ".zip";
    // Create directory
    Path submissionsFolderPath = Path.of(outputDir.toString(), "zippedSubmissions", zipGroupName);
    Path zipFilePath = Path.of(outputDir.toString(), "zippedSubmissions", zipFileName);
    File submissionFolder = submissionsFolderPath.toFile();
    if (!submissionFolder.exists() && !submissionFolder.mkdirs()) {
        log.error("Couldn't create dir: {}", submissionFolder);
        exportErrors.add("Cannot create directory: " + submissionFolder.toPath());
        return Optional.empty();
    }
    // Create counter for log entry
    MutableInt skippedEntries = new MutableInt();
    // Save all Submissions
    List<Path> submissionFilePaths = participations.stream().map(participation -> {
        Submission latestSubmission = latestSubmission(participation, enableFilterAfterDueDate, lateSubmissionFilter);
        if (latestSubmission == null) {
            skippedEntries.increment();
            return Optional.<Path>empty();
        }
        // create file path
        String submissionFileName = exercise.getTitle() + "-" + participation.getParticipantIdentifier() + "-" + latestSubmission.getId() + this.getFileEndingForSubmission(latestSubmission);
        Path submissionFilePath = Path.of(submissionsFolderPath.toString(), submissionFileName);
        // store file
        try {
            this.saveSubmissionToFile(exercise, latestSubmission, submissionFilePath.toFile());
            return Optional.of(submissionFilePath);
        } catch (Exception ex) {
            String message = "Could not create file " + submissionFilePath + "  for exporting: " + ex.getMessage();
            log.error(message, ex);
            exportErrors.add(message);
            return Optional.<Path>empty();
        }
    }).flatMap(Optional::stream).collect(Collectors.toList());
    // Add report entry
    reportData.add(new ArchivalReportEntry(exercise, fileService.removeIllegalCharacters(exercise.getTitle()), participations.size(), submissionFilePaths.size(), skippedEntries.intValue()));
    if (submissionFilePaths.isEmpty()) {
        return Optional.empty();
    }
    // zip stores submissions
    try {
        zipFileService.createZipFile(zipFilePath, submissionFilePaths, submissionsFolderPath);
    } finally {
        log.debug("Delete all temporary files");
        fileService.deleteFiles(submissionFilePaths);
    }
    return Optional.of(zipFilePath.toFile());
}
Also used : Path(java.nio.file.Path) ArchivalReportEntry(de.tum.in.www1.artemis.service.archival.ArchivalReportEntry) Submission(de.tum.in.www1.artemis.domain.Submission) MutableInt(org.apache.commons.lang.mutable.MutableInt) Course(de.tum.in.www1.artemis.domain.Course) File(java.io.File) BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) IOException(java.io.IOException)

Aggregations

ArchivalReportEntry (de.tum.in.www1.artemis.service.archival.ArchivalReportEntry)4 File (java.io.File)4 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 Course (de.tum.in.www1.artemis.domain.Course)2 Exercise (de.tum.in.www1.artemis.domain.Exercise)2 Submission (de.tum.in.www1.artemis.domain.Submission)2 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)2 GitException (de.tum.in.www1.artemis.exception.GitException)2 RepositoryExportOptionsDTO (de.tum.in.www1.artemis.web.rest.dto.RepositoryExportOptionsDTO)2 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)2 UncheckedIOException (java.io.UncheckedIOException)2 ZonedDateTime (java.time.ZonedDateTime)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 XPath (javax.xml.xpath.XPath)2 XPathException (javax.xml.xpath.XPathException)2 MutableInt (org.apache.commons.lang.mutable.MutableInt)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 SAXException (org.xml.sax.SAXException)2