Fixed and improved file export system

This commit is contained in:
George Kalampokis 2020-04-02 17:22:41 +03:00
parent 88e34e1ec1
commit 71ccd76896
8 changed files with 83 additions and 63 deletions

View File

@ -19,6 +19,7 @@ import eu.eudat.logic.proxy.config.configloaders.ConfigLoader;
import eu.eudat.logic.security.claims.ClaimedAuthorities; import eu.eudat.logic.security.claims.ClaimedAuthorities;
import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.operations.DatabaseRepository; import eu.eudat.logic.services.operations.DatabaseRepository;
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
import eu.eudat.models.data.datasetprofile.DatasetProfileListingModel; import eu.eudat.models.data.datasetprofile.DatasetProfileListingModel;
import eu.eudat.models.data.datasetwizard.DatasetsToBeFinalized; import eu.eudat.models.data.datasetwizard.DatasetsToBeFinalized;
import eu.eudat.models.data.dmp.DataManagementPlan; import eu.eudat.models.data.dmp.DataManagementPlan;
@ -209,22 +210,22 @@ public class DMPs extends BaseController {
public @ResponseBody public @ResponseBody
ResponseEntity<byte[]> getPDFDocument(@PathVariable String id, @RequestHeader("Content-Type") String contentType, ResponseEntity<byte[]> getPDFDocument(@PathVariable String id, @RequestHeader("Content-Type") String contentType,
@ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException, InterruptedException { @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException, InterruptedException {
File file = this.dataManagementPlanManager.getWordDocument(id, principal, configLoader); FileEnvelope file = this.dataManagementPlanManager.getWordDocument(id, principal, configLoader);
String name = file.getName().substring(0, file.getName().length() - 5); String name = file.getFilename().substring(0, file.getFilename().length() - 5);
File pdffile = datasetManager.convertToPDF(file, environment, name); File pdffile = datasetManager.convertToPDF(file, environment);
InputStream resource = new FileInputStream(pdffile); InputStream resource = new FileInputStream(pdffile);
logger.info("Mime Type of " + file.getName() + " is " + logger.info("Mime Type of " + name + " is " +
new MimetypesFileTypeMap().getContentType(file)); new MimetypesFileTypeMap().getContentType(file.getFile()));
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(pdffile.length()); responseHeaders.setContentLength(pdffile.length());
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.set("Content-Disposition", "attachment;filename=" + pdffile.getName()); responseHeaders.set("Content-Disposition", "attachment;filename=" + name + ".pdf");
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition"); responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type"); responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource); byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource);
resource.close(); resource.close();
Files.deleteIfExists(file.toPath()); Files.deleteIfExists(file.getFile().toPath());
Files.deleteIfExists(pdffile.toPath()); Files.deleteIfExists(pdffile.toPath());
return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK); return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK);
} }

View File

@ -12,6 +12,7 @@ import eu.eudat.logic.proxy.config.configloaders.ConfigLoader;
import eu.eudat.logic.security.claims.ClaimedAuthorities; import eu.eudat.logic.security.claims.ClaimedAuthorities;
import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.forms.VisibilityRuleService; import eu.eudat.logic.services.forms.VisibilityRuleService;
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
import eu.eudat.models.data.datasetwizard.DataManagentPlanListingModel; import eu.eudat.models.data.datasetwizard.DataManagentPlanListingModel;
import eu.eudat.models.data.datasetwizard.DatasetWizardModel; import eu.eudat.models.data.datasetwizard.DatasetWizardModel;
import eu.eudat.models.data.dmp.AssociatedProfile; import eu.eudat.models.data.dmp.AssociatedProfile;
@ -20,6 +21,7 @@ import eu.eudat.models.data.listingmodels.DataManagementPlanOverviewModel;
import eu.eudat.models.data.security.Principal; import eu.eudat.models.data.security.Principal;
import eu.eudat.models.data.user.composite.PagedDatasetProfile; import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import eu.eudat.types.ApiMessageCode; import eu.eudat.types.ApiMessageCode;
import eu.eudat.types.Authorities;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -82,24 +84,24 @@ public class DatasetWizardController extends BaseController {
@Transactional @Transactional
@RequestMapping(method = RequestMethod.GET, value = {"{id}"}, produces = "application/json") @RequestMapping(method = RequestMethod.GET, value = {"{id}"}, produces = "application/json")
public @ResponseBody public @ResponseBody
ResponseEntity getSingle(@PathVariable String id, @RequestHeader("Content-Type") String contentType, Principal principal) throws IllegalAccessException, IOException, InstantiationException { ResponseEntity getSingle(@PathVariable String id, @RequestHeader("Content-Type") String contentType, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException {
try { try {
if (contentType.equals("application/xml")) { if (contentType.equals("application/xml")) {
VisibilityRuleService visibilityRuleService = this.getApiContext().getUtilitiesService().getVisibilityRuleService(); VisibilityRuleService visibilityRuleService = this.getApiContext().getUtilitiesService().getVisibilityRuleService();
return this.datasetManager.getDocument(id, visibilityRuleService, contentType, principal); return this.datasetManager.getDocument(id, visibilityRuleService, contentType, principal);
} else if (contentType.equals("application/msword")) { } else if (contentType.equals("application/msword")) {
File file = datasetManager.getWordDocumentFile(this.configLoader, id, this.getApiContext().getUtilitiesService().getVisibilityRuleService(), principal); FileEnvelope file = datasetManager.getWordDocumentFile(this.configLoader, id, this.getApiContext().getUtilitiesService().getVisibilityRuleService(), principal);
InputStream resource = new FileInputStream(file); InputStream resource = new FileInputStream(file.getFile());
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(file.length()); responseHeaders.setContentLength(file.getFile().length());
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.set("Content-Disposition", "attachment;filename=" + file.getName()); responseHeaders.set("Content-Disposition", "attachment;filename=" + file.getFilename());
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition"); responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type"); responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
byte[] content = IOUtils.toByteArray(resource); byte[] content = IOUtils.toByteArray(resource);
resource.close(); resource.close();
Files.deleteIfExists(file.toPath()); Files.deleteIfExists(file.getFile().toPath());
return new ResponseEntity<>(content, return new ResponseEntity<>(content,
responseHeaders, responseHeaders,
HttpStatus.OK); HttpStatus.OK);
@ -151,25 +153,25 @@ public class DatasetWizardController extends BaseController {
@RequestMapping(method = RequestMethod.GET, value = {"/getPDF/{id}"}) @RequestMapping(method = RequestMethod.GET, value = {"/getPDF/{id}"})
public @ResponseBody public @ResponseBody
ResponseEntity<byte[]> getPDFDocument(@PathVariable String id, Principal principal) throws IllegalAccessException, IOException, InstantiationException, InterruptedException { ResponseEntity<byte[]> getPDFDocument(@PathVariable String id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException, InterruptedException {
File file = datasetManager.getWordDocumentFile(this.configLoader, id, this.getApiContext().getUtilitiesService().getVisibilityRuleService(), principal); FileEnvelope file = datasetManager.getWordDocumentFile(this.configLoader, id, this.getApiContext().getUtilitiesService().getVisibilityRuleService(), principal);
String fileName = file.getName(); String fileName = file.getFilename();
if (fileName.endsWith(".docx")){ if (fileName.endsWith(".docx")){
fileName = fileName.substring(0, fileName.length() - 5); fileName = fileName.substring(0, fileName.length() - 5);
} }
File pdffile = datasetManager.convertToPDF(file, environment, fileName); File pdffile = datasetManager.convertToPDF(file, environment);
InputStream resource = new FileInputStream(pdffile); InputStream resource = new FileInputStream(pdffile);
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(pdffile.length()); responseHeaders.setContentLength(pdffile.length());
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.set("Content-Disposition", "attachment;filename=" + pdffile.getName()); responseHeaders.set("Content-Disposition", "attachment;filename=" + fileName + ".pdf");
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition"); responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type"); responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
byte[] content = IOUtils.toByteArray(resource); byte[] content = IOUtils.toByteArray(resource);
resource.close(); resource.close();
Files.deleteIfExists(file.toPath()); Files.deleteIfExists(file.getFile().toPath());
Files.deleteIfExists(pdffile.toPath()); Files.deleteIfExists(pdffile.toPath());
return new ResponseEntity<>(content, return new ResponseEntity<>(content,
responseHeaders, responseHeaders,

View File

@ -235,11 +235,11 @@ public class DataManagementPlanManager {
return; return;
} }
public File getWordDocument(String id, Principal principal, ConfigLoader configLoader) throws IOException { public FileEnvelope getWordDocument(String id, Principal principal, ConfigLoader configLoader) throws IOException {
return this.getWordDocument(id, principal, configLoader, false); return this.getWordDocument(id, principal, configLoader, false);
} }
public File getWordDocument(String id, Principal principal, ConfigLoader configLoader, Boolean versioned) throws IOException { public FileEnvelope getWordDocument(String id, Principal principal, ConfigLoader configLoader, Boolean versioned) throws IOException {
WordBuilder wordBuilder = new WordBuilder(); WordBuilder wordBuilder = new WordBuilder();
VisibilityRuleService visibilityRuleService = this.utilitiesService.getVisibilityRuleService(); VisibilityRuleService visibilityRuleService = this.utilitiesService.getVisibilityRuleService();
DatasetWizardModel dataset = new DatasetWizardModel(); DatasetWizardModel dataset = new DatasetWizardModel();
@ -392,12 +392,16 @@ public class DataManagementPlanManager {
fileName = dmpEntity.getLabel(); fileName = dmpEntity.getLabel();
} }
fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", ""); fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "");
File exportFile = new File(this.environment.getProperty("temp.temp") + fileName + ".docx"); FileEnvelope exportEnvelope = new FileEnvelope();
exportEnvelope.setFilename(fileName + ".docx");
String uuid = UUID.randomUUID().toString();
File exportFile = new File(this.environment.getProperty("temp.temp") + uuid + ".docx");
FileOutputStream out = new FileOutputStream(exportFile); FileOutputStream out = new FileOutputStream(exportFile);
document.write(out); document.write(out);
out.close(); out.close();
exportEnvelope.setFile(exportFile);
return exportFile; return exportEnvelope;
} }
/*public File getPdfDocument(String id) throws InstantiationException, IllegalAccessException, InterruptedException, IOException { /*public File getPdfDocument(String id) throws InstantiationException, IllegalAccessException, InterruptedException, IOException {
@ -1033,9 +1037,10 @@ public class DataManagementPlanManager {
if (!dmp.isPublic() && dmp.getUsers().stream().filter(userInfo -> userInfo.getUser().getId() == principal.getId()).collect(Collectors.toList()).size() == 0) if (!dmp.isPublic() && dmp.getUsers().stream().filter(userInfo -> userInfo.getUser().getId() == principal.getId()).collect(Collectors.toList()).size() == 0)
throw new UnauthorisedException(); throw new UnauthorisedException();
List<Dataset> datasets = dmp.getDataset().stream().collect(Collectors.toList()); List<Dataset> datasets = dmp.getDataset().stream().collect(Collectors.toList());
String fileName = dmp.getLabel(); /*String fileName = dmp.getLabel();
fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", ""); fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "");*/
File xmlFile = new File(this.environment.getProperty("temp.temp") + fileName + ".xml"); String uuid = UUID.randomUUID().toString();
File xmlFile = new File(this.environment.getProperty("temp.temp") + uuid + ".xml");
BufferedWriter writer = new BufferedWriter(new FileWriter(xmlFile, true)); BufferedWriter writer = new BufferedWriter(new FileWriter(xmlFile, true));
Document xmlDoc = XmlBuilder.getDocument(); Document xmlDoc = XmlBuilder.getDocument();
Element dmpElement = xmlDoc.createElement("dmp"); Element dmpElement = xmlDoc.createElement("dmp");
@ -1156,7 +1161,7 @@ public class DataManagementPlanManager {
writer.close(); writer.close();
FileEnvelope fileEnvelope = new FileEnvelope(); FileEnvelope fileEnvelope = new FileEnvelope();
fileEnvelope.setFile(xmlFile); fileEnvelope.setFile(xmlFile);
fileEnvelope.setFilename(dmp.getLabel()); fileEnvelope.setFilename(dmp.getLabel() + ".xml");
return fileEnvelope; return fileEnvelope;
} }
@ -1172,7 +1177,8 @@ public class DataManagementPlanManager {
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String fileName = dmp.getLabel(); String fileName = dmp.getLabel();
fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", ""); fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "");
File file = new File(this.environment.getProperty("temp.temp") + fileName + ".json"); String uuid = UUID.randomUUID().toString();
File file = new File(this.environment.getProperty("temp.temp") + uuid + ".json");
OutputStream output = new FileOutputStream(file); OutputStream output = new FileOutputStream(file);
try { try {
// mapper.writeValue(file, rdaExportModel); // mapper.writeValue(file, rdaExportModel);
@ -1187,7 +1193,7 @@ public class DataManagementPlanManager {
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(file.length()); responseHeaders.setContentLength(file.length());
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.set("Content-Disposition", "attachment;filename=" + file.getName()); responseHeaders.set("Content-Disposition", "attachment;filename=" + fileName + ".json");
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition"); responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type"); responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
@ -1198,28 +1204,28 @@ public class DataManagementPlanManager {
} }
public ResponseEntity<byte[]> getDocument(String id, String contentType, Principal principal, ConfigLoader configLoader) throws InstantiationException, IllegalAccessException, IOException { public ResponseEntity<byte[]> getDocument(String id, String contentType, Principal principal, ConfigLoader configLoader) throws InstantiationException, IllegalAccessException, IOException {
File file; FileEnvelope file;
switch (contentType) { switch (contentType) {
case "application/xml": case "application/xml":
file = getXmlDocument(id, principal).getFile(); file = getXmlDocument(id, principal);
break; break;
case "application/msword": case "application/msword":
file = getWordDocument(id, principal, configLoader); file = getWordDocument(id, principal, configLoader);
break; break;
default: default:
file = getXmlDocument(id, principal).getFile(); file = getXmlDocument(id, principal);
} }
InputStream resource = new FileInputStream(file); InputStream resource = new FileInputStream(file.getFile());
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(file.length()); responseHeaders.setContentLength(file.getFile().length());
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.set("Content-Disposition", "attachment;filename=" + file.getName()); responseHeaders.set("Content-Disposition", "attachment;filename=" + file.getFilename());
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition"); responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type"); responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource); byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource);
resource.close(); resource.close();
Files.deleteIfExists(file.toPath()); Files.deleteIfExists(file.getFile().toPath());
return new ResponseEntity<>(content, return new ResponseEntity<>(content,
responseHeaders, responseHeaders,
HttpStatus.OK); HttpStatus.OK);
@ -1598,15 +1604,15 @@ public class DataManagementPlanManager {
fileHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); fileHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
LinkedMultiValueMap<String, Object> addFileMap = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, Object> addFileMap = new LinkedMultiValueMap<>();
File file = getWordDocument(id.toString(), principal, configLoader); FileEnvelope file = getWordDocument(id.toString(), principal, configLoader);
addFileMap.add("filename", file.getName()); addFileMap.add("filename", file.getFilename());
FileSystemResource fileSystemResource = new FileSystemResource(file); FileSystemResource fileSystemResource = new FileSystemResource(file.getFile());
addFileMap.add("file", fileSystemResource); addFileMap.add("file", fileSystemResource);
HttpEntity<MultiValueMap<String, Object>> addFileMapRequest = new HttpEntity<>(addFileMap, fileHeaders); HttpEntity<MultiValueMap<String, Object>> addFileMapRequest = new HttpEntity<>(addFileMap, fileHeaders);
String addFileUrl = links.get("files") + "?access_token=" + this.environment.getProperty("zenodo.access_token"); String addFileUrl = links.get("files") + "?access_token=" + this.environment.getProperty("zenodo.access_token");
ResponseEntity<String> addFileResponse = restTemplate.postForEntity(addFileUrl, addFileMapRequest, String.class); ResponseEntity<String> addFileResponse = restTemplate.postForEntity(addFileUrl, addFileMapRequest, String.class);
Files.deleteIfExists(file.toPath()); Files.deleteIfExists(file.getFile().toPath());
// Third post call to Zenodo to publish the entry and return the DOI. // Third post call to Zenodo to publish the entry and return the DOI.
String publishUrl = links.get("publish") + "?access_token=" + this.environment.getProperty("zenodo.access_token"); String publishUrl = links.get("publish") + "?access_token=" + this.environment.getProperty("zenodo.access_token");

View File

@ -24,6 +24,7 @@ import eu.eudat.logic.services.ApiContext;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -49,11 +50,13 @@ public class DataManagementProfileManager {
private ApiContext apiContext; private ApiContext apiContext;
private DatabaseRepository databaseRepository; private DatabaseRepository databaseRepository;
private Environment environment;
@Autowired @Autowired
public DataManagementProfileManager(ApiContext apiContext) { public DataManagementProfileManager(ApiContext apiContext, Environment environment) {
this.apiContext = apiContext; this.apiContext = apiContext;
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository(); this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
this.environment = environment;
} }
public DataTableData<DataManagementPlanProfileListingModel> getPaged(DataManagementPlanProfileTableRequest dataManagementPlanProfileTableRequest, Principal principal) throws Exception { public DataTableData<DataManagementPlanProfileListingModel> getPaged(DataManagementPlanProfileTableRequest dataManagementPlanProfileTableRequest, Principal principal) throws Exception {
@ -114,7 +117,7 @@ public class DataManagementProfileManager {
public FileEnvelope getXmlDocument(DataManagementPlanProfileListingModel dmpProfile, String label) throws InstantiationException, IllegalAccessException, IOException { public FileEnvelope getXmlDocument(DataManagementPlanProfileListingModel dmpProfile, String label) throws InstantiationException, IllegalAccessException, IOException {
ExportXmlBuilderDmpProfile xmlBuilder = new ExportXmlBuilderDmpProfile(); ExportXmlBuilderDmpProfile xmlBuilder = new ExportXmlBuilderDmpProfile();
File file = xmlBuilder.build(dmpProfile); File file = xmlBuilder.build(dmpProfile, environment);
FileEnvelope fileEnvelope = new FileEnvelope(); FileEnvelope fileEnvelope = new FileEnvelope();
fileEnvelope.setFile(file); fileEnvelope.setFile(file);
fileEnvelope.setFilename(label); fileEnvelope.setFilename(label);

View File

@ -402,7 +402,7 @@ public class DatasetManager {
visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules()); visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules());
wordBuilder.build(document, pagedDatasetProfile, visibilityRuleService); wordBuilder.build(document, pagedDatasetProfile, visibilityRuleService);
String label = datasetEntity.getLabel().replaceAll("[^a-zA-Z0-9+ ]", ""); String label = datasetEntity.getLabel().replaceAll("[^a-zA-Z0-9+ ]", "");
File exportFile = new File(label + ".docx"); // File exportFile = new File(label + ".docx");
// Removes the top empty headings. // Removes the top empty headings.
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
@ -446,19 +446,23 @@ public class DatasetManager {
// return exportFile; // return exportFile;
} }
public File getWordDocumentFile(ConfigLoader configLoader, String id, VisibilityRuleService visibilityRuleService, Principal principal) throws IOException { public FileEnvelope getWordDocumentFile(ConfigLoader configLoader, String id, VisibilityRuleService visibilityRuleService, Principal principal) throws IOException {
eu.eudat.data.entities.Dataset datasetEntity = databaseRepository.getDatasetDao().find(UUID.fromString(id), HintedModelFactory.getHint(DatasetWizardModel.class)); eu.eudat.data.entities.Dataset datasetEntity = databaseRepository.getDatasetDao().find(UUID.fromString(id), HintedModelFactory.getHint(DatasetWizardModel.class));
if (!datasetEntity.getDmp().isPublic() && datasetEntity.getDmp().getUsers() if (!datasetEntity.getDmp().isPublic() && datasetEntity.getDmp().getUsers()
.stream().filter(userInfo -> userInfo.getUser().getId() == principal.getId()) .stream().filter(userInfo -> userInfo.getUser().getId() == principal.getId())
.collect(Collectors.toList()).size() == 0) .collect(Collectors.toList()).size() == 0)
throw new UnauthorisedException(); throw new UnauthorisedException();
String label = datasetEntity.getLabel().replaceAll("[^a-zA-Z0-9+ ]", ""); String label = datasetEntity.getLabel().replaceAll("[^a-zA-Z0-9+ ]", "");
File exportFile = new File(this.environment.getProperty("temp.temp") + label + ".docx"); FileEnvelope exportEnvelope = new FileEnvelope();
exportEnvelope.setFilename(label + ".docx");
String uuid = UUID.randomUUID().toString();
File exportFile = new File(this.environment.getProperty("temp.temp") + uuid + ".docx");
XWPFDocument document = getWordDocument(configLoader, datasetEntity, visibilityRuleService); XWPFDocument document = getWordDocument(configLoader, datasetEntity, visibilityRuleService);
FileOutputStream out = new FileOutputStream(exportFile); FileOutputStream out = new FileOutputStream(exportFile);
document.write(out); document.write(out);
out.close(); out.close();
return exportFile; exportEnvelope.setFile(exportFile);
return exportEnvelope;
} }
public String getWordDocumentText (Dataset datasetEntity) throws Exception { public String getWordDocumentText (Dataset datasetEntity) throws Exception {
@ -488,17 +492,19 @@ public class DatasetManager {
File file = xmlBuilder.build(pagedDatasetProfile, datasetEntity.getProfile().getId(), visibilityRuleService, environment); File file = xmlBuilder.build(pagedDatasetProfile, datasetEntity.getProfile().getId(), visibilityRuleService, environment);
FileEnvelope fileEnvelope = new FileEnvelope(); FileEnvelope fileEnvelope = new FileEnvelope();
fileEnvelope.setFile(file); fileEnvelope.setFile(file);
fileEnvelope.setFilename(datasetEntity.getLabel()); String label = datasetEntity.getLabel().replaceAll("[^a-zA-Z0-9+ ]", "");
fileEnvelope.setFilename(label);
return fileEnvelope; return fileEnvelope;
} }
public File convertToPDF(File file, Environment environment, String label) throws IOException, InterruptedException { public File convertToPDF(FileEnvelope file, Environment environment) throws IOException, InterruptedException {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("files", new FileSystemResource(file)); String uuid = UUID.randomUUID().toString();
map.add("filename", label + ".pdf"); map.add("files", new FileSystemResource(file.getFile()));
map.add("filename", uuid + ".pdf");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("Content-disposition", "attachment; filename=" + label + ".pdf"); headers.add("Content-disposition", "attachment; filename=" + uuid + ".pdf");
headers.add("Content-type", "application/pdf"); headers.add("Content-type", "application/pdf");
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>( HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(
@ -507,11 +513,11 @@ public class DatasetManager {
byte[] queueResult = new RestTemplate().postForObject(environment.getProperty("pdf.converter.url") + "convert/office" byte[] queueResult = new RestTemplate().postForObject(environment.getProperty("pdf.converter.url") + "convert/office"
, requestEntity, byte[].class); , requestEntity, byte[].class);
File resultPdf = new File(label + ".pdf"); File resultPdf = new File(environment.getProperty("temp.temp") + uuid + ".pdf");
FileOutputStream output = new FileOutputStream(resultPdf); FileOutputStream output = new FileOutputStream(resultPdf);
IOUtils.write(queueResult, output); IOUtils.write(queueResult, output);
output.close(); output.close();
Files.deleteIfExists(file.toPath()); Files.deleteIfExists(file.getFile().toPath());
return resultPdf; return resultPdf;
} }

View File

@ -21,10 +21,10 @@ import eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.Field;
import eu.eudat.models.data.externaldataset.ExternalAutocompleteFieldModel; import eu.eudat.models.data.externaldataset.ExternalAutocompleteFieldModel;
import eu.eudat.models.data.helpers.common.DataTableData; import eu.eudat.models.data.helpers.common.DataTableData;
import eu.eudat.queryable.QueryableList; import eu.eudat.queryable.QueryableList;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -34,12 +34,10 @@ import org.w3c.dom.Element;
import javax.activation.MimetypesFileTypeMap; import javax.activation.MimetypesFileTypeMap;
import javax.xml.xpath.*; import javax.xml.xpath.*;
import java.io.IOException; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.*; import java.util.*;
import java.io.*;
@Component @Component
public class DatasetProfileManager { public class DatasetProfileManager {
@ -47,11 +45,13 @@ public class DatasetProfileManager {
private ApiContext apiContext; private ApiContext apiContext;
private DatabaseRepository databaseRepository; private DatabaseRepository databaseRepository;
private Environment environment;
@Autowired @Autowired
public DatasetProfileManager(ApiContext apiContext) { public DatasetProfileManager(ApiContext apiContext, Environment environment) {
this.apiContext = apiContext; this.apiContext = apiContext;
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository(); this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
this.environment = environment;
} }
public eu.eudat.models.data.admin.composite.DatasetProfile getDatasetProfile(String id) { public eu.eudat.models.data.admin.composite.DatasetProfile getDatasetProfile(String id) {
@ -142,7 +142,7 @@ public class DatasetProfileManager {
public FileEnvelope getXmlDocument(eu.eudat.models.data.user.composite.DatasetProfile datatasetProfile, String label) throws InstantiationException, IllegalAccessException, IOException { public FileEnvelope getXmlDocument(eu.eudat.models.data.user.composite.DatasetProfile datatasetProfile, String label) throws InstantiationException, IllegalAccessException, IOException {
ExportXmlBuilderDatasetProfile xmlBuilder = new ExportXmlBuilderDatasetProfile(); ExportXmlBuilderDatasetProfile xmlBuilder = new ExportXmlBuilderDatasetProfile();
File file = xmlBuilder.build(datatasetProfile); File file = xmlBuilder.build(datatasetProfile, environment);
FileEnvelope fileEnvelope = new FileEnvelope(); FileEnvelope fileEnvelope = new FileEnvelope();
fileEnvelope.setFile(file); fileEnvelope.setFile(file);
fileEnvelope.setFilename(label); fileEnvelope.setFilename(label);

View File

@ -8,6 +8,7 @@ import eu.eudat.models.data.user.components.datasetprofile.FieldSet;
import eu.eudat.models.data.user.components.datasetprofile.Section; import eu.eudat.models.data.user.components.datasetprofile.Section;
import eu.eudat.logic.utilities.builders.XmlBuilder; import eu.eudat.logic.utilities.builders.XmlBuilder;
import org.springframework.core.env.Environment;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -22,9 +23,9 @@ import java.util.UUID;
public class ExportXmlBuilderDatasetProfile { public class ExportXmlBuilderDatasetProfile {
public File build(eu.eudat.models.data.user.composite.DatasetProfile datasetProfile) throws IOException { public File build(eu.eudat.models.data.user.composite.DatasetProfile datasetProfile, Environment environment) throws IOException {
File xmlFile = new File(UUID.randomUUID() + ".xml"); File xmlFile = new File(environment.getProperty("temp.temp") + UUID.randomUUID() + ".xml");
BufferedWriter writer = new BufferedWriter(new FileWriter(xmlFile, true)); BufferedWriter writer = new BufferedWriter(new FileWriter(xmlFile, true));
Document xmlDoc = XmlBuilder.getDocument(); Document xmlDoc = XmlBuilder.getDocument();
// Element root = xmlDoc.createElement("root"); // Element root = xmlDoc.createElement("root");

View File

@ -4,6 +4,7 @@ import eu.eudat.logic.utilities.builders.XmlBuilder;
import eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile; import eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile;
import eu.eudat.models.data.listingmodels.DataManagementPlanProfileListingModel; import eu.eudat.models.data.listingmodels.DataManagementPlanProfileListingModel;
import org.springframework.core.env.Environment;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -16,9 +17,9 @@ import java.util.UUID;
public class ExportXmlBuilderDmpProfile { public class ExportXmlBuilderDmpProfile {
public File build(DataManagementPlanProfileListingModel dmpProfile) throws IOException { public File build(DataManagementPlanProfileListingModel dmpProfile, Environment environment) throws IOException {
File xmlFile = new File(UUID.randomUUID() + ".xml"); File xmlFile = new File(environment.getProperty("temp.temp") + UUID.randomUUID() + ".xml");
BufferedWriter writer = new BufferedWriter(new FileWriter(xmlFile, true)); BufferedWriter writer = new BufferedWriter(new FileWriter(xmlFile, true));
Document xmlDoc = XmlBuilder.getDocument(); Document xmlDoc = XmlBuilder.getDocument();
Element root = xmlDoc.createElement("root"); Element root = xmlDoc.createElement("root");