From 8c149d327f48cf3a418063df76c7f73ae81cbf83 Mon Sep 17 00:00:00 2001 From: BilledTrain380 Date: Sun, 26 Aug 2018 14:46:35 +0200 Subject: [PATCH] Add file parser controller --- .../business/group/GroupFileParser.kt | 1 + .../business/group/GroupFileParserImpl.kt | 1 + .../web/groupimport/GroupImportController.kt | 74 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/main/kotlin/ch/schulealtendorf/sporttagpsa/controller/web/groupimport/GroupImportController.kt diff --git a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/group/GroupFileParser.kt b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/group/GroupFileParser.kt index e4ee631d..ef862328 100644 --- a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/group/GroupFileParser.kt +++ b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/group/GroupFileParser.kt @@ -54,6 +54,7 @@ interface GroupFileParser { * @param file the file to parse * * @return the content of the parsed file in form of a list of {@link FlatParticipant} + * @throws IllegalArgumentException if the file does not match the format csv or is empty * @throws CSVParsingException if the given file can not be parsed */ fun parseCSV(file: MultipartFile): List diff --git a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/group/GroupFileParserImpl.kt b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/group/GroupFileParserImpl.kt index 5a569784..66fd23e8 100644 --- a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/group/GroupFileParserImpl.kt +++ b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/group/GroupFileParserImpl.kt @@ -64,6 +64,7 @@ class GroupFileParserImpl : GroupFileParser { * @param file the file to parse * * @return the content of the parsed file in form of a list of {@link FlatParticipant} + * @throws IllegalArgumentException if the file does not match the format csv or is empty * @throws CSVParsingException if the given file can not be parsed */ override fun parseCSV(file: MultipartFile): List { diff --git a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/controller/web/groupimport/GroupImportController.kt b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/controller/web/groupimport/GroupImportController.kt new file mode 100644 index 00000000..e9641d1d --- /dev/null +++ b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/controller/web/groupimport/GroupImportController.kt @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2018 by Nicolas Märchy + * + * This file is part of Sporttag PSA. + * + * Sporttag PSA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Sporttag PSA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Sporttag PSA. If not, see . + * + * Diese Datei ist Teil von Sporttag PSA. + * + * Sporttag PSA ist Freie Software: Sie können es unter den Bedingungen + * der GNU General Public License, wie von der Free Software Foundation, + * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren + * veröffentlichten Version, weiterverbreiten und/oder modifizieren. + * + * Sporttag PSA wird in der Hoffnung, dass es nützlich sein wird, aber + * OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite + * Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK. + * Siehe die GNU General Public License für weitere Details. + * + * Sie sollten eine Kopie der GNU General Public License zusammen mit diesem + * Programm erhalten haben. Wenn nicht, siehe . + * + * + */ + +package ch.schulealtendorf.sporttagpsa.controller.web.groupimport + +import ch.schulealtendorf.sporttagpsa.business.group.CSVParsingException +import ch.schulealtendorf.sporttagpsa.business.group.GroupFileParser +import ch.schulealtendorf.sporttagpsa.business.group.GroupManager +import ch.schulealtendorf.sporttagpsa.controller.rest.BadRequestException +import org.springframework.stereotype.Controller +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.multipart.MultipartFile + +/** + * @author nmaerchy + * @since 2.0.0 + */ +@Controller +class GroupImportController( + private val fileParser: GroupFileParser, + private val groupManager: GroupManager +) { + + @PostMapping("/import-group") + fun importGroup(@RequestParam("group-input") file: MultipartFile) { + + try { + + val participants = fileParser.parseCSV(file) + + participants.forEach(groupManager::import) + + } catch (exception: CSVParsingException) { + // we increment the line, so its not zero based line number for the user + throw BadRequestException("${exception.message} (at line ${exception.line+1}:${exception.column})") + } catch (exception: IllegalArgumentException) { + throw BadRequestException(exception.message) + } + } +} \ No newline at end of file