Skip to content
Snippets Groups Projects

Resolve "File import for TA training"

Files

@@ -17,6 +17,9 @@
*/
package nl.tudelft.tam.controller;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Map;
import nl.tudelft.labracore.api.dto.PersonSummaryDTO;
@@ -27,11 +30,19 @@ import nl.tudelft.tam.service.PersonService;
import nl.tudelft.tam.service.TrainingApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.opencsv.exceptions.CsvValidationException;
@Controller
@RequestMapping("training-approval")
public class TrainingApprovalController {
@@ -107,4 +118,37 @@ public class TrainingApprovalController {
return "redirect:/coordinator/training/" + programId;
}
@GetMapping("/import/template")
@PreAuthorize("@authorisationService.isCoordinatorOfAny()")
public ResponseEntity<Resource> downloadTemplateFile() throws IOException, URISyntaxException {
File file = new File(
this.getClass()
.getResource("/static/import_templates/import_training_template.csv")
.toURI());
UrlResource resource = new UrlResource("file:" + file.getPath());
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
@PostMapping("import/{programId}/csv")
@PreAuthorize("@authorisationService.canEditTaTrainingForProgram(#programId)")
public String importTrainingCsv(@RequestParam("file") MultipartFile file,
@PathVariable Long programId,
RedirectAttributes rAttrs) throws IOException, CsvValidationException {
var result = trainingApprovalService.importTraining(programId, file);
rAttrs.addFlashAttribute("successfulImportSize", result.result().size());
rAttrs.addFlashAttribute("importWarnings", result.warnings());
rAttrs.addFlashAttribute("importErrors", result.errors());
return "redirect:/coordinator/training/" + programId;
}
}
Loading