Skip to content
Snippets Groups Projects
Commit 65ce9b94 authored by Baptiste Toulemonde's avatar Baptiste Toulemonde
Browse files

Merge branch 'master' of...

Merge branch 'master' of https://git.garr.it/eosc-pillar/ffds/ffds-smart-harvester into feature/getValues
parents 4c6f9c77 dc9fa982
No related branches found
No related tags found
1 merge request!11Feature/get values
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();
}
}
}
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);
}
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;
}
}
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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment