Skip to content
Snippets Groups Projects
Commit c84e97cd authored by Moritz Staudinger's avatar Moritz Staudinger
Browse files

Added file read from tmp folder - untested

parent 61cd8bd1
No related branches found
No related tags found
3 merge requests!23Sprint results,!18Merge Conflicts,!17UI sprint 2
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.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import lombok.*; import lombok.*;
...@@ -25,4 +26,6 @@ public class TableCSVInformation { ...@@ -25,4 +26,6 @@ public class TableCSVInformation {
@NotBlank @NotBlank
private List<ColumnTypeDto> columns; private List<ColumnTypeDto> columns;
private String fileLocation;
} }
...@@ -3,7 +3,7 @@ package at.tuwien.endpoints; ...@@ -3,7 +3,7 @@ package at.tuwien.endpoints;
import at.tuwien.dto.table.TableBriefDto; import at.tuwien.dto.table.TableBriefDto;
import at.tuwien.dto.table.TableCreateDto; import at.tuwien.dto.table.TableCreateDto;
import at.tuwien.dto.table.TableDto; 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.entity.Table;
import at.tuwien.exception.*; import at.tuwien.exception.*;
import at.tuwien.mapper.QueryResultMapper; import at.tuwien.mapper.QueryResultMapper;
...@@ -21,6 +21,7 @@ import org.springframework.http.ResponseEntity; ...@@ -21,6 +21,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -75,7 +76,7 @@ public class TableEndpoint { ...@@ -75,7 +76,7 @@ public class TableEndpoint {
} }
@PostMapping("/table/csv") @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({ @ApiResponses({
@ApiResponse(code = 201, message = "The table was created."), @ApiResponse(code = 201, message = "The table was created."),
@ApiResponse(code = 400, message = "The creation form contains invalid data."), @ApiResponse(code = 400, message = "The creation form contains invalid data."),
...@@ -84,13 +85,29 @@ public class TableEndpoint { ...@@ -84,13 +85,29 @@ public class TableEndpoint {
@ApiResponse(code = 405, message = "The container is not running."), @ApiResponse(code = 405, message = "The container is not running."),
@ApiResponse(code = 409, message = "The container image is not supported."), @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) public ResponseEntity<QueryResultDto> createViaCsv(@PathVariable("id") Long databaseId, @RequestPart("file") MultipartFile file, @RequestPart TableCSVInformation headers) {
throws ImageNotSupportedException, DatabaseConnectionException, TableMalformedException, DatabaseNotFoundException, TableNotFoundException {
final QueryResult queryResult = tableService.create(databaseId, file, headers); final QueryResult queryResult = tableService.create(databaseId, file, headers);
return ResponseEntity.status(HttpStatus.CREATED) return ResponseEntity.status(HttpStatus.CREATED)
.body(queryResultMapper.queryResultToQueryResultDto(queryResult)); .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}") @GetMapping("/table/{tableId}")
@ApiOperation(value = "List all tables", notes = "Lists the tables in the metadata database for this database.") @ApiOperation(value = "List all tables", notes = "Lists the tables in the metadata database for this database.")
@ApiResponses({ @ApiResponses({
......
...@@ -24,6 +24,10 @@ ...@@ -24,6 +24,10 @@
<artifactId>super-csv</artifactId> <artifactId>super-csv</artifactId>
<version>2.4.0</version> <version>2.4.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -2,7 +2,7 @@ package at.tuwien.service; ...@@ -2,7 +2,7 @@ package at.tuwien.service;
import at.tuwien.dto.table.TableCreateDto; import at.tuwien.dto.table.TableCreateDto;
import at.tuwien.dto.table.columns.ColumnCreateDto; 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.Database;
import at.tuwien.entity.Table; import at.tuwien.entity.Table;
import at.tuwien.entity.TableColumn; import at.tuwien.entity.TableColumn;
...@@ -13,6 +13,7 @@ import at.tuwien.repository.DatabaseRepository; ...@@ -13,6 +13,7 @@ import at.tuwien.repository.DatabaseRepository;
import at.tuwien.repository.TableRepository; import at.tuwien.repository.TableRepository;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.supercsv.cellprocessor.constraint.NotNull; import org.supercsv.cellprocessor.constraint.NotNull;
...@@ -26,6 +27,9 @@ import javax.transaction.Transactional; ...@@ -26,6 +27,9 @@ import javax.transaction.Transactional;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -211,23 +215,23 @@ public class TableService { ...@@ -211,23 +215,23 @@ public class TableService {
return queryResult; 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 { try {
String[] header = readHeader(file); String[] header = readHeader(file);
for (String s : header) { for (String s : header) {
System.out.println(s); System.out.println(s);
} }
System.out.println(headers.toString()); System.out.println(tableCSVInformation.toString());
TableCreateDto tcd = new TableCreateDto(); TableCreateDto tcd = new TableCreateDto();
tcd.setName(headers.getName()); tcd.setName(tableCSVInformation.getName());
tcd.setDescription(headers.getDescription()); tcd.setDescription(tableCSVInformation.getDescription());
ColumnCreateDto[] cdtos = new ColumnCreateDto[header.length]; ColumnCreateDto[] cdtos = new ColumnCreateDto[header.length];
System.out.println(headers.getColumns().toString()); System.out.println(tableCSVInformation.getColumns().toString());
System.out.println(header.toString()); System.out.println(header.toString());
for (int i = 0; i < header.length; i++) { for (int i = 0; i < header.length; i++) {
ColumnCreateDto c = new ColumnCreateDto(); ColumnCreateDto c = new ColumnCreateDto();
c.setName(header[i]); c.setName(header[i]);
c.setType(headers.getColumns().get(i)); c.setType(tableCSVInformation.getColumns().get(i));
c.setNullAllowed(true); c.setNullAllowed(true);
//TODO FIX THAT not only id is primary key //TODO FIX THAT not only id is primary key
if(header[i].equals("id")) { if(header[i].equals("id")) {
...@@ -247,4 +251,20 @@ public class TableService { ...@@ -247,4 +251,20 @@ public class TableService {
} }
return null; 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);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment