diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterTsvController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterTsvController.java new file mode 100644 index 0000000000000000000000000000000000000000..9acf461b1a4c469f7f93f309c93e7dcf5af24c71 --- /dev/null +++ b/src/main/java/com/smartharvester/controller/SmartHarvesterTsvController.java @@ -0,0 +1,66 @@ +package com.smartharvester.controller; + +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import com.smartharvester.dao.TsvDaoRepository; +import com.smartharvester.model.TsvModel; +import com.smartharvester.service.TsvService; +import io.swagger.v3.oas.annotations.tags.Tag; + +@CrossOrigin(origins = "*") +@RestController +@Tag(name = "SmartHarvester mapping description", description = "Mapping description management") +@RequestMapping("/harvester/api/storeMapping") +public class SmartHarvesterTsvController { + + private static final Logger LOGGER = LoggerFactory.getLogger(SmartHarvesterTsvController.class); + + @Autowired + private TsvService tsvService; + + @Autowired + private TsvDaoRepository daoRepository; + + + @PostMapping("/upload/{catalogId}") + public ResponseEntity<String> addFile(@RequestParam("file") MultipartFile file, @PathVariable("catalogId") String catalogId) { + try { + this.tsvService.addTsv(file, catalogId); + LOGGER.info("mapping from catalog id " + catalogId + " successfully stored"); + return ResponseEntity.ok().body("mapping saved successfully "); + } catch (IOException e) { + LOGGER.warn(e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } + } + + @GetMapping("/download/{catalogId}") + public ResponseEntity<?> download(@PathVariable("catalogId") String catalogId) { + if( this.daoRepository.findByCatalogId(catalogId) != null) { + TsvModel tsv = this.tsvService.getFileById(catalogId); + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType(tsv.getFileType())) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachement; filename=\"dcatMapping\"") + .body(new ByteArrayResource((tsv.getFile().getData()))); + } else { + return ResponseEntity.notFound().build(); + } + + } +} diff --git a/src/main/java/com/smartharvester/dao/TsvDaoRepository.java b/src/main/java/com/smartharvester/dao/TsvDaoRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..66967a846158c85fa36290d41f374bb53a3c4009 --- /dev/null +++ b/src/main/java/com/smartharvester/dao/TsvDaoRepository.java @@ -0,0 +1,24 @@ +package com.smartharvester.dao; + + + + + +import java.util.Optional; + +import org.springframework.data.mongodb.repository.DeleteQuery; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; + +import com.smartharvester.model.TsvModel; + +public interface TsvDaoRepository extends MongoRepository<TsvModel, String>{ + + Optional<TsvModel> findByCatalogId(String catalogId); + + + + @DeleteQuery + void deleteByCatalogId(String catalogId); + +} diff --git a/src/main/java/com/smartharvester/model/TsvModel.java b/src/main/java/com/smartharvester/model/TsvModel.java new file mode 100644 index 0000000000000000000000000000000000000000..973c8c4125e0d0584565f2531d02f5c1fdea3c53 --- /dev/null +++ b/src/main/java/com/smartharvester/model/TsvModel.java @@ -0,0 +1,64 @@ +package com.smartharvester.model; + + + +import org.bson.types.Binary; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + + +public class TsvModel { + + + @Id + private String id; + + private String fileType; + + private Binary file; + + private String catalogId; + + + public String getCatalogId() { + return catalogId; + } + + public void setCatalogId(String catalogId) { + this.catalogId = catalogId; + } + + public TsvModel(Binary file, String catalogId, String fileType) { + super(); + this.file = file; + this.catalogId = catalogId; + this.fileType = fileType; + } + + public String getId() { + return id; + } + + public void setUuid(String id) { + this.id = id; + } + + public Binary getFile() { + return file; + } + + public void setFile(Binary file) { + this.file = file; + } + + public String getFileType() { + return fileType; + } + + public void setFileType(String fileType) { + this.fileType = fileType; + } + + + +} diff --git a/src/main/java/com/smartharvester/service/TsvService.java b/src/main/java/com/smartharvester/service/TsvService.java new file mode 100644 index 0000000000000000000000000000000000000000..dd84ddc14a1fbf068ea0b20e747c340f6d9a778b --- /dev/null +++ b/src/main/java/com/smartharvester/service/TsvService.java @@ -0,0 +1,55 @@ +package com.smartharvester.service; + +import java.io.File; +import java.io.IOException; +import java.util.Base64; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.bson.BsonBinarySubType; +import org.bson.types.Binary; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import org.webjars.NotFoundException; + +import com.smartharvester.dao.TsvDaoRepository; +import com.smartharvester.model.TsvModel; + +@Service +public class TsvService { + + @Autowired + private TsvDaoRepository daoRepository; + + public String addTsv(MultipartFile file, String catalogId) throws IOException { + + TsvModel tsv = new TsvModel(new Binary(BsonBinarySubType.BINARY, file.getBytes()), catalogId, file.getContentType().toString()); + + +// FileUtils.writeByteArrayToFile(new File("./test.tsv"), tsv.getFile().getData()); +// System.out.println( Base64.getEncoder().encodeToString(tsv.getFile().getData())); + + if (this.daoRepository.findByCatalogId(catalogId) != null) { + this.daoRepository.deleteByCatalogId(catalogId); + } + + this.daoRepository.save(tsv); + + return tsv.getId(); + + } + + public TsvModel getFileById(String catalogId) { + + return this.daoRepository.findByCatalogId(catalogId).orElseThrow(() -> new NotFoundException("File not found")); + + + } + + public List<Binary> getAll() { + return this.daoRepository.findAll().stream().map(e -> e.getFile()).collect(Collectors.toList()); + } + +}