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());
}
Aggregations