diff --git a/fda-table-service/api/src/main/java/at/tuwien/dto/table/columns/TableCSVInformation.java b/fda-table-service/api/src/main/java/at/tuwien/dto/table/TableCSVInformation.java similarity index 84% rename from fda-table-service/api/src/main/java/at/tuwien/dto/table/columns/TableCSVInformation.java rename to fda-table-service/api/src/main/java/at/tuwien/dto/table/TableCSVInformation.java index 65a1b40fc2407f8859872a0080820899c60fccd4..78e94e2bdb1ef12f26f4e55aa5626aa104bcc35f 100644 --- a/fda-table-service/api/src/main/java/at/tuwien/dto/table/columns/TableCSVInformation.java +++ b/fda-table-service/api/src/main/java/at/tuwien/dto/table/TableCSVInformation.java @@ -1,5 +1,6 @@ -package at.tuwien.dto.table.columns; +package at.tuwien.dto.table; +import at.tuwien.dto.table.columns.ColumnTypeDto; import io.swagger.annotations.ApiModelProperty; import io.swagger.v3.oas.annotations.Parameter; import lombok.*; @@ -25,4 +26,6 @@ public class TableCSVInformation { @NotBlank private List<ColumnTypeDto> columns; + + private String fileLocation; } diff --git a/fda-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java b/fda-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java index 3b8a30dda01221ddb481264461a0f83552664557..38fd548c5d7915cbaec31b2a5b6b80fa57923a93 100644 --- a/fda-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java +++ b/fda-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java @@ -3,7 +3,7 @@ package at.tuwien.endpoints; import at.tuwien.dto.table.TableBriefDto; import at.tuwien.dto.table.TableCreateDto; import at.tuwien.dto.table.TableDto; -import at.tuwien.dto.table.columns.TableCSVInformation; +import at.tuwien.dto.table.TableCSVInformation; import at.tuwien.entity.Table; import at.tuwien.exception.*; import at.tuwien.mapper.QueryResultMapper; @@ -21,6 +21,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; import java.util.List; import java.util.stream.Collectors; @@ -75,7 +76,7 @@ public class TableEndpoint { } @PostMapping("/table/csv") - @ApiOperation(value = "Create a table", notes = "Creates a new table for a database, requires a running container. For the colum definition use the following example: [{\"name\": \"Ticker Symbol\", \"primaryKey\": true, \"type\": \"STRING\", \"nullAllowed\": false, \"checkExpression\": null, \"foreignKey\": null},{\"name\": \"Accounts Payable\", \"primaryKey\": false, \"type\": \"NUMBER\", \"nullAllowed\": false, \"checkExpression\": \"Accounts Payable > 0\", \"foreignKey\": null},{\"name\": \"Company\", \"primaryKey\": false, \"type\": \"STRING\", \"nullAllowed\": false, \"checkExpression\": null, \"foreignKey\": null}]") + @ApiOperation(value = "Create a table", notes = "Creates a file, which is given as a multipart file.") @ApiResponses({ @ApiResponse(code = 201, message = "The table was created."), @ApiResponse(code = 400, message = "The creation form contains invalid data."), @@ -84,13 +85,29 @@ public class TableEndpoint { @ApiResponse(code = 405, message = "The container is not running."), @ApiResponse(code = 409, message = "The container image is not supported."), }) - public ResponseEntity<QueryResultDto> createViaCsv(@PathVariable("id") Long databaseId, @RequestPart("file") MultipartFile file, @RequestPart TableCSVInformation headers) - throws ImageNotSupportedException, DatabaseConnectionException, TableMalformedException, DatabaseNotFoundException, TableNotFoundException { + public ResponseEntity<QueryResultDto> createViaCsv(@PathVariable("id") Long databaseId, @RequestPart("file") MultipartFile file, @RequestPart TableCSVInformation headers) { final QueryResult queryResult = tableService.create(databaseId, file, headers); return ResponseEntity.status(HttpStatus.CREATED) .body(queryResultMapper.queryResultToQueryResultDto(queryResult)); } + @PostMapping("/table/csv/local") + @ApiOperation(value = "Create a table", notes = "This is done by saving a file on the shared docker filesystem and then sending the link to the file.") + @ApiResponses({ + @ApiResponse(code = 201, message = "The table was created."), + @ApiResponse(code = 400, message = "The creation form contains invalid data."), + @ApiResponse(code = 401, message = "Not authorized to create a tables."), + @ApiResponse(code = 404, message = "The database does not exist."), + @ApiResponse(code = 405, message = "The container is not running."), + @ApiResponse(code = 409, message = "The container image is not supported."), + }) + public ResponseEntity<QueryResultDto> createViaCsv(@PathVariable("id") Long databaseId, @RequestBody TableCSVInformation tableCSVInformation) throws IOException { + final QueryResult queryResult = tableService.create(databaseId, tableCSVInformation); + return ResponseEntity.status(HttpStatus.CREATED) + .body(queryResultMapper.queryResultToQueryResultDto(queryResult)); + } + + @GetMapping("/table/{tableId}") @ApiOperation(value = "List all tables", notes = "Lists the tables in the metadata database for this database.") @ApiResponses({ diff --git a/fda-table-service/services/pom.xml b/fda-table-service/services/pom.xml index 0d3d956fedb7cc8288a58c6ba2f95eef6848a6d8..3c55b33c4ea223af82f7aad52edc8d01724ad8c1 100644 --- a/fda-table-service/services/pom.xml +++ b/fda-table-service/services/pom.xml @@ -24,6 +24,10 @@ <artifactId>super-csv</artifactId> <version>2.4.0</version> </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + </dependency> </dependencies> <build> @@ -52,4 +56,4 @@ </plugins> </build> -</project> \ No newline at end of file +</project> diff --git a/fda-table-service/services/src/main/java/at/tuwien/service/TableService.java b/fda-table-service/services/src/main/java/at/tuwien/service/TableService.java index 7b299496e7091c4ee7abb1d4c08df1cebd0d822e..1977e7196cea223bc27256ee7c17e2f07dee5a7d 100644 --- a/fda-table-service/services/src/main/java/at/tuwien/service/TableService.java +++ b/fda-table-service/services/src/main/java/at/tuwien/service/TableService.java @@ -2,7 +2,7 @@ package at.tuwien.service; import at.tuwien.dto.table.TableCreateDto; import at.tuwien.dto.table.columns.ColumnCreateDto; -import at.tuwien.dto.table.columns.TableCSVInformation; +import at.tuwien.dto.table.TableCSVInformation; import at.tuwien.entity.Database; import at.tuwien.entity.Table; import at.tuwien.entity.TableColumn; @@ -13,6 +13,7 @@ import at.tuwien.repository.DatabaseRepository; import at.tuwien.repository.TableRepository; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import org.supercsv.cellprocessor.constraint.NotNull; @@ -26,6 +27,9 @@ import javax.transaction.Transactional; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -211,23 +215,23 @@ public class TableService { return queryResult; } - public QueryResult create(Long databaseId, MultipartFile file, TableCSVInformation headers) throws TableNotFoundException, DatabaseNotFoundException, ImageNotSupportedException { + public QueryResult create(Long databaseId, MultipartFile file, TableCSVInformation tableCSVInformation) { try { String[] header = readHeader(file); for (String s : header) { System.out.println(s); } - System.out.println(headers.toString()); + System.out.println(tableCSVInformation.toString()); TableCreateDto tcd = new TableCreateDto(); - tcd.setName(headers.getName()); - tcd.setDescription(headers.getDescription()); + tcd.setName(tableCSVInformation.getName()); + tcd.setDescription(tableCSVInformation.getDescription()); ColumnCreateDto[] cdtos = new ColumnCreateDto[header.length]; - System.out.println(headers.getColumns().toString()); + System.out.println(tableCSVInformation.getColumns().toString()); System.out.println(header.toString()); for (int i = 0; i < header.length; i++) { ColumnCreateDto c = new ColumnCreateDto(); c.setName(header[i]); - c.setType(headers.getColumns().get(i)); + c.setType(tableCSVInformation.getColumns().get(i)); c.setNullAllowed(true); //TODO FIX THAT not only id is primary key if(header[i].equals("id")) { @@ -247,4 +251,20 @@ public class TableService { } return null; } + + public QueryResult create(Long databaseId, TableCSVInformation tableCSVInformation) throws IOException { + Path path = Paths.get("/tmp/" + tableCSVInformation.getFileLocation()); + String contentType = "multipart/form-data"; + byte[] content = null; + try { + content = Files.readAllBytes(path); + } catch (final IOException e) { + } + MultipartFile multipartFile = new MockMultipartFile(tableCSVInformation.getFileLocation(), + tableCSVInformation.getFileLocation(), contentType, content); + Files.deleteIfExists(path); + return create(databaseId, multipartFile,tableCSVInformation); + } + + }