use new models

This commit is contained in:
Efstratios Giannopoulos 2024-03-06 16:34:34 +02:00
parent 82ac239ebe
commit 8ff0762501
37 changed files with 1853 additions and 2147 deletions

View File

@ -18,7 +18,6 @@
<maven.compiler.target>21</maven.compiler.target> <maven.compiler.target>21</maven.compiler.target>
<maven.compiler.release>21</maven.compiler.release> <maven.compiler.release>21</maven.compiler.release>
<revision>1.0.0-SNAPSHOT</revision> <revision>1.0.0-SNAPSHOT</revision>
<transformer-base.version>0.0.4</transformer-base.version>
</properties> </properties>
<dependencies> <dependencies>

View File

@ -1,30 +0,0 @@
package eu.eudat.file.transformer.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
@ConfigurationProperties(prefix = "file.template")
public class FilePathProperties {
private final String wordTemplate;
private final String pidTemplate;
private final String wordDescriptionTemplate;
@ConstructorBinding
public FilePathProperties(String wordTemplate, String pidTemplate, String wordDescriptionTemplate) {
this.wordTemplate = wordTemplate;
this.pidTemplate = pidTemplate;
this.wordDescriptionTemplate = wordDescriptionTemplate;
}
public String getWordTemplate() {
return wordTemplate;
}
public String getPidTemplate() {
return pidTemplate;
}
public String getWordDescriptionTemplate() {
return wordDescriptionTemplate;
}
}

View File

@ -1,9 +0,0 @@
package eu.eudat.file.transformer.configuration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({FileStorageProperties.class})
public class FileStorageConfiguration {
}

View File

@ -1,9 +0,0 @@
package eu.eudat.file.transformer.configuration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({PdfProperties.class})
public class PdfConfiguration {
}

View File

@ -1,634 +0,0 @@
package eu.eudat.file.transformer.executor;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.file.transformer.configuration.FilePathProperties;
import eu.eudat.file.transformer.configuration.FileStorageProperties;
import eu.eudat.file.transformer.configuration.PdfProperties;
import eu.eudat.file.transformer.enums.DescriptionStatus;
import eu.eudat.file.transformer.enums.DmpBlueprintFieldCategory;
import eu.eudat.file.transformer.enums.DmpStatus;
import eu.eudat.file.transformer.enums.ReferenceType;
import eu.eudat.file.transformer.interfaces.FileTransformerClient;
import eu.eudat.file.transformer.interfaces.FileTransformerConfiguration;
import eu.eudat.file.transformer.models.description.DescriptionFileTransformerModel;
import eu.eudat.file.transformer.models.descriptiontemplate.DescriptionTemplateFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.dmpblueprint.DmpBlueprintFileTransformerModel;
import eu.eudat.file.transformer.models.dmpblueprint.definition.ExtraFieldFileTransformerModelFileTransformerModel;
import eu.eudat.file.transformer.models.dmpblueprint.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.models.dmpblueprint.definition.SectionFileTransformerModel;
import eu.eudat.file.transformer.models.dmpblueprint.definition.SystemFieldFileTransformerModel;
import eu.eudat.file.transformer.model.enums.FileFormats;
import eu.eudat.file.transformer.models.misc.FileEnvelope;
import eu.eudat.file.transformer.model.file.FileEnvelopeInternal;
import eu.eudat.file.transformer.models.misc.FileFormat;
import eu.eudat.file.transformer.models.reference.ReferenceFileTransformerModel;
import eu.eudat.file.transformer.utils.pdf.PDFUtils;
import eu.eudat.file.transformer.utils.storage.FileStorageService;
import eu.eudat.file.transformer.utils.types.ParagraphStyle;
import eu.eudat.file.transformer.utils.word.WordBuilder;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import javax.management.InvalidApplicationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.time.Instant;
import java.util.*;
@Component
public class WordFileTransformer implements FileTransformerClient {
private final static Logger logger = LoggerFactory.getLogger(WordFileTransformer.class);
private final static List<FileFormat> FILE_FORMATS = List.of(
new FileFormat(FileFormats.PDF.getValue(), true, "fa-file-pdf-o"),
new FileFormat(FileFormats.DOCX.getValue(), true, "fa-file-word-o"));
private final FilePathProperties fileTemplateProperties;
private final FileStorageProperties fileStorageProperties;
private final PdfProperties pdfProperties;
private final ApplicationContext applicationContext;
private final ObjectMapper objectMapper;
private final FileStorageService fileStorageService;
@Autowired
public WordFileTransformer(FilePathProperties fileTemplateProperties, FileStorageProperties fileStorageProperties, PdfProperties pdfProperties, ApplicationContext applicationContext, FileStorageService fileStorageService) {
this.fileTemplateProperties = fileTemplateProperties;
this.fileStorageProperties = fileStorageProperties;
this.pdfProperties = pdfProperties;
this.applicationContext = applicationContext;
this.fileStorageService = fileStorageService;
this.objectMapper = new ObjectMapper();
}
@Override
public FileEnvelope exportDmp(DmpFileTransformerModel dmp) throws IOException {
FileFormats fileFormat = FileFormats.of(dmp.getVariant());
return switch (fileFormat) {
case DOCX -> getWordDocument(dmp);
case PDF -> {
FileEnvelopeInternal wordFile = getWordDocument(dmp, true);
yield getPdfDocument(wordFile);
}
};
}
@Override
public FileEnvelope exportDescription(DescriptionFileTransformerModel descriptionFileTransformerModel, String format) throws InvalidApplicationException, IOException {
FileFormats fileFormat = FileFormats.of(format);
return switch (fileFormat) {
case DOCX -> getDescriptionWordDocument(descriptionFileTransformerModel);
case PDF -> {
FileEnvelopeInternal wordFile = getDescriptionWordDocumentInternal(descriptionFileTransformerModel);
yield getPdfDocument(wordFile);
}
};
}
@Override
public DmpFileTransformerModel importDmp(FileEnvelope envelope) {
//Nothing to do here
return null;
}
@Override
public DescriptionFileTransformerModel importDescription(FileEnvelope envelope) {
return null;
}
@Override
public FileTransformerConfiguration getConfiguration() {
FileTransformerConfiguration configuration = new FileTransformerConfiguration();
configuration.setFileTransformerId("docx/pdf");
configuration.setExportVariants(FILE_FORMATS);
return configuration;
}
private FileEnvelope getPdfDocument(FileEnvelopeInternal wordFile) throws IOException {
File pdfFile = PDFUtils.convertToPDF(wordFile, fileStorageProperties.getTemp(), pdfProperties.getUrl());
FileEnvelope result = new FileEnvelope();
result.setFilename(wordFile.getFilename().replaceAll(".docx", ".pdf"));
try (FileInputStream fis = new FileInputStream(pdfFile)) {
result.setFile(fileStorageService.storeFile(fis.readAllBytes()));
}
return result;
}
private FileEnvelope getWordDocument(DmpFileTransformerModel dmp) throws IOException {
FileEnvelopeInternal wordFile = getWordDocument(dmp, true);
FileEnvelope result = new FileEnvelope();
result.setFilename(wordFile.getFilename());
try (FileInputStream fis = new FileInputStream(wordFile.getFile())) {
result.setFile(fileStorageService.storeFile(fis.readAllBytes()));
}
return result;
}
private FileEnvelopeInternal getWordDocument(DmpFileTransformerModel dmpEntity, Boolean versioned) throws IOException {
WordBuilder wordBuilder = new WordBuilder(fileTemplateProperties, fileStorageProperties);
XWPFDocument document = new XWPFDocument(new FileInputStream(ResourceUtils.getFile(fileTemplateProperties.getWordTemplate())));
wordBuilder.fillFirstPage(dmpEntity, null, document, false);
List<ReferenceFileTransformerModel> grants = new ArrayList<>();
List<ReferenceFileTransformerModel> researchers = new ArrayList<>();
List<ReferenceFileTransformerModel> organizations = new ArrayList<>();
List<ReferenceFileTransformerModel> funders = new ArrayList<>();
List<ReferenceFileTransformerModel> projects = new ArrayList<>();
if (dmpEntity.getDmpReferences() != null) {
grants = dmpEntity.getDmpReferences().stream().map(DmpReferenceFileTransformerModel::getReference).filter(referenceFileModel -> referenceFileModel.getType().equals(ReferenceType.Grants)).toList();
researchers = dmpEntity.getDmpReferences().stream().map(DmpReferenceFileTransformerModel::getReference).filter(reference -> reference.getType().equals(ReferenceType.Researcher)).toList();
organizations = dmpEntity.getDmpReferences().stream().map(DmpReferenceFileTransformerModel::getReference).filter(referenceFileModel -> referenceFileModel.getType().equals(ReferenceType.Organizations)).toList();
funders = dmpEntity.getDmpReferences().stream().map(DmpReferenceFileTransformerModel::getReference).filter(referenceFileModel -> referenceFileModel.getType().equals(ReferenceType.Funder)).toList();
projects = dmpEntity.getDmpReferences().stream().filter(referenceFileModel -> referenceFileModel.getReference().getType().equals(ReferenceType.Project)).map(DmpReferenceFileTransformerModel::getReference).toList();
}
// int powered_pos = document.getParagraphs().size() - 3;
int powered_pos = wordBuilder.findPosOfPoweredBy(document);
XWPFParagraph powered_par = null;
XWPFParagraph argos_img_par = null;
if(powered_pos != -1) {
powered_par = document.getParagraphArray(powered_pos);
argos_img_par = document.getParagraphArray(powered_pos + 1);
}
// // DMP info on top of the document.
// wordBuilder.addParagraphContent("Data Management Plan Information", document, ParagraphStyle.HEADER1, BigInteger.ZERO);
// // DMP title custom style.
// wordBuilder.addParagraphContent(dmpEntity.getLabel(), document, ParagraphStyle.HEADER2, BigInteger.ZERO);
// wordBuilder.addParagraphContent(dmpEntity.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO);
//
// wordBuilder.addParagraphContent("Funder", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
// if (dmpEntity.getGrant().getFunder() != null)
// wordBuilder.addParagraphContent(dmpEntity.getGrant().getFunder().getLabel(), document, ParagraphStyle.TEXT, BigInteger.ZERO);
//
// wordBuilder.addParagraphContent("Grant", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
// wordBuilder.addParagraphContent(dmpEntity.getGrant().getLabel(), document, ParagraphStyle.TEXT, BigInteger.ZERO);
//
// wordBuilder.addParagraphContent("Organisations", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
// if (dmpEntity.getOrganisations().size() > 0) {
// wordBuilder.addParagraphContent(dmpEntity.getOrganisations().stream().map(Organisation::getLabel).collect(Collectors.joining(", "))
// , document, ParagraphStyle.TEXT, BigInteger.ZERO);
// }
//
// wordBuilder.addParagraphContent("Researchers", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
// if (dmpEntity.getResearchers().size() > 0) {
// wordBuilder.addParagraphContent(dmpEntity.getResearchers().stream().map(Researcher::getLabel).collect(Collectors.joining(", "))
// , document, ParagraphStyle.TEXT, BigInteger.ZERO);
// }
//
// /*wordBuilder.addParagraphContent("DMP Profile", document, ParagraphStyle.HEADER2, BigInteger.ZERO);
// if (dmpEntity.getProfile() != null){
// wordBuilder.addParagraphContent(dmpEntity.getProfile().getLabel(), document, ParagraphStyle.TEXT, BigInteger.ZERO);
// }*/
//
// // Page break at the end of the DMP title.
// XWPFParagraph parBreakDMP = document.createParagraph();
// parBreakDMP.setPageBreak(true);
//
// wordBuilder.addParagraphContent("Datasets", document, ParagraphStyle.HEADER1, BigInteger.ZERO);
// // Space below Datasets.
// XWPFParagraph parBreakDatasets = document.createParagraph();
DmpBlueprintFileTransformerModel dmpBlueprintFileTransformerModel = dmpEntity.getBlueprint();
for(SectionFileTransformerModel sectionFileTransformerModel : dmpBlueprintFileTransformerModel.getDefinitionFileTransformerModel().getSections()){
wordBuilder.addParagraphContent("Section " + sectionFileTransformerModel.getOrdinal(), document, ParagraphStyle.HEADER1, BigInteger.ZERO, 0);
XWPFParagraph sectionInfoParagraph = document.createParagraph();
sectionInfoParagraph.setSpacingBetween(1.0);
XWPFRun runSectionTitle = sectionInfoParagraph.createRun();
runSectionTitle.setText("Title: ");
runSectionTitle.setColor("000000");
XWPFRun runSectionTitleText = sectionInfoParagraph.createRun();
runSectionTitleText.setText(sectionFileTransformerModel.getLabel());
runSectionTitleText.setColor("116a78");
XWPFParagraph sectionDescriptionParagraph = document.createParagraph();
XWPFRun runSectionDescription = sectionDescriptionParagraph.createRun();
runSectionDescription.setText("Description: ");
runSectionDescription.setColor("000000");
XWPFRun runSectionDescriptionText = sectionDescriptionParagraph.createRun();
runSectionDescriptionText.setText(sectionFileTransformerModel.getDescription());
runSectionDescriptionText.setColor("116a78");
wordBuilder.addParagraphContent("Section Fields", document, ParagraphStyle.HEADER2, BigInteger.ZERO, 0);
if (sectionFileTransformerModel.getFields() != null) {
sectionFileTransformerModel.getFields().sort(Comparator.comparingInt(FieldFileTransformerModel::getOrdinal));
for (FieldFileTransformerModel fieldFileTransformerModel : sectionFileTransformerModel.getFields()) {
if (fieldFileTransformerModel.getCategory() == DmpBlueprintFieldCategory.System) {
SystemFieldFileTransformerModel systemField = (SystemFieldFileTransformerModel) fieldFileTransformerModel;
XWPFParagraph systemFieldParagraph = document.createParagraph();
systemFieldParagraph.setSpacingBetween(1.0);
XWPFRun runSyStemFieldTitle = systemFieldParagraph.createRun();
runSyStemFieldTitle.setText("Title: ");
runSyStemFieldTitle.setColor("000000");
XWPFRun runSystemFieldTitleText = systemFieldParagraph.createRun();
runSystemFieldTitleText.setText(systemField.getLabel());
runSystemFieldTitleText.setColor("116a78");
if (systemField.getDescription() != null && !systemField.getDescription().isEmpty()) {
XWPFParagraph systemFieldDescription = document.createParagraph();
systemFieldDescription.setSpacingBetween(1.0);
XWPFRun runSyStemFieldDescription = systemFieldDescription.createRun();
runSyStemFieldDescription.setText("Description: ");
runSyStemFieldDescription.setColor("000000");
XWPFRun runSystemFieldDescriptionText = systemFieldDescription.createRun();
runSystemFieldDescriptionText.setText(systemField.getDescription());
runSystemFieldDescriptionText.setColor("116a78");
}
XWPFParagraph systemFieldInput = document.createParagraph();
systemFieldInput.setSpacingBetween(1.0);
XWPFRun runInput = systemFieldInput.createRun();
runInput.setText("Input: ");
runInput.setColor("000000");
Map<String, Object> dmpProperties = objectMapper.readValue(dmpEntity.getProperties(), HashMap.class);
switch (systemField.getSystemFieldType()) {
case Text:
XWPFRun runTitle = systemFieldInput.createRun();
runTitle.setText(dmpEntity.getLabel());
runTitle.setColor("116a78");
break;
case HtmlText:
XWPFRun runDescription = systemFieldInput.createRun();
runDescription.setText(dmpEntity.getDescription());
runDescription.setColor("116a78");
break;
case Researchers:
for (ReferenceFileTransformerModel researcher : researchers) {
XWPFRun runResearcher = systemFieldInput.createRun();
runResearcher.setText("" + researcher.getLabel());
runResearcher.setColor("116a78");
}
break;
case Organizations:
for (ReferenceFileTransformerModel organisation : organizations) {
XWPFRun runOrganisation = systemFieldInput.createRun();
runOrganisation.setText("" + organisation.getLabel());
runOrganisation.setColor("116a78");
}
break;
/* case Language:
XWPFRun runLanguage = systemFieldInput.createRun();
runLanguage.setText(dmpProperties.get("language").toString());
runLanguage.setColor("116a78");
break;*/
case Contact:
XWPFRun runContact = systemFieldInput.createRun();
runContact.setText(dmpEntity.getCreator().getName());
runContact.setColor("116a78");
break;
case Funder:
if (!funders.isEmpty()) {
XWPFRun runFunder = systemFieldInput.createRun();
runFunder.setText(funders.get(0).getLabel());
runFunder.setColor("116a78");
}
break;
case Grant:
if (!grants.isEmpty()) {
XWPFRun runGrant = systemFieldInput.createRun();
runGrant.setText(grants.get(0).getLabel());
runGrant.setColor("116a78");
}
break;
case Project:
if (!projects.isEmpty()) {
XWPFRun runProject = systemFieldInput.createRun();
runProject.setText(projects.get(0).getLabel());
runProject.setColor("116a78");
}
break;
case License:
if (dmpProperties.containsKey("license")) {
XWPFRun runLicense = systemFieldInput.createRun();
runLicense.setText(dmpProperties.get("license").toString());
runLicense.setColor("116a78");
}
break;
case AccessRights:
if (dmpProperties.containsKey("visible")) {
XWPFRun runAccessRights = systemFieldInput.createRun();
runAccessRights.setText(dmpProperties.get("visible").toString());
runAccessRights.setColor("116a78");
}
break;
}
document.createParagraph();
} else if (fieldFileTransformerModel.getCategory() == DmpBlueprintFieldCategory.Extra) {
ExtraFieldFileTransformerModelFileTransformerModel extraFieldFileTransformerModel = (ExtraFieldFileTransformerModelFileTransformerModel) fieldFileTransformerModel;
XWPFParagraph extraFieldParagraph = document.createParagraph();
extraFieldParagraph.setSpacingBetween(1.0);
XWPFRun runExtraFieldLabel = extraFieldParagraph.createRun();
runExtraFieldLabel.setText(extraFieldFileTransformerModel.getLabel());
runExtraFieldLabel.setColor("116a78");
if (extraFieldFileTransformerModel.getDescription() != null && !extraFieldFileTransformerModel.getDescription().isEmpty()) {
XWPFRun runExtraFieldDescription = extraFieldParagraph.createRun();
runExtraFieldDescription.setText(extraFieldFileTransformerModel.getDescription());
runExtraFieldDescription.setColor("116a78");
}
XWPFRun runExtraFieldInput = extraFieldParagraph.createRun();
Map dmpProperties = objectMapper.readValue(dmpEntity.getProperties(), HashMap.class);
if (dmpProperties.containsKey(fieldFileTransformerModel.getId()) && dmpProperties.get(fieldFileTransformerModel.getId()) != null) {
runExtraFieldInput.setText((String) dmpProperties.get(fieldFileTransformerModel.getId()));
}
runExtraFieldInput.setColor("116a78");
}
}
}
final Boolean isFinalized = dmpEntity.getStatus() == DmpStatus.Finalized;
final Boolean isPublic = dmpEntity.getPublicAfter() != null && dmpEntity.getPublicAfter().isAfter(Instant.now());
List<DescriptionFileTransformerModel> descriptions = dmpEntity.getDescriptions().stream()
.filter(item -> item.getStatus() != DescriptionStatus.Canceled)
.filter(item -> !isPublic && !isFinalized || item.getStatus() == DescriptionStatus.Finalized)
.filter(item -> item.getSectionId().equals(sectionFileTransformerModel.getId())) //TODO
.sorted(Comparator.comparing(DescriptionFileTransformerModel::getCreatedAt)).toList();
List<DescriptionTemplateFileTransformerModel> descriptionTemplateFileTransformerModels = descriptions.stream().map(DescriptionFileTransformerModel::getDescriptionTemplate).toList();
if(!descriptionTemplateFileTransformerModels.isEmpty()){
wordBuilder.addParagraphContent("Section descriptions", document, ParagraphStyle.HEADER2, BigInteger.ZERO, 0);
wordBuilder.addParagraphContent("Description Templates", document, ParagraphStyle.HEADER4, BigInteger.ZERO, 0);
for(DescriptionTemplateFileTransformerModel descriptionTemplateFileTransformerModelEntity : descriptionTemplateFileTransformerModels){
XWPFParagraph templateParagraph = document.createParagraph();
XWPFRun runTemplateLabel = templateParagraph.createRun();
runTemplateLabel.setText("" + descriptionTemplateFileTransformerModelEntity.getLabel());
runTemplateLabel.setColor("116a78");
}
descriptions
.forEach(datasetEntity -> {
DescriptionTemplateFileTransformerModel descriptionTemplateFileTransformerModelFileModel = datasetEntity.getDescriptionTemplate();
// Dataset Description custom style.
XWPFParagraph datasetDescriptionParagraph = document.createParagraph();
datasetDescriptionParagraph.setStyle("Heading4");
datasetDescriptionParagraph.setSpacingBetween(1.5);
XWPFRun datasetDescriptionRun = datasetDescriptionParagraph.createRun();
datasetDescriptionRun.setText("Dataset Description");
//datasetDescriptionRun.setColor("2E75B6");
//datasetDescriptionRun.setBold(true);
datasetDescriptionRun.setFontSize(15);
// Custom style for the Dataset title.
//wordBuilder.addParagraphContent("Title: " + datasetEntity.getLabel(), document, ParagraphStyle.HEADER1, BigInteger.ZERO);
XWPFParagraph datasetLabelParagraph = document.createParagraph();
// datasetLabelParagraph.setStyle("Heading2");
datasetLabelParagraph.setSpacingBetween(1.0);
XWPFRun runDatasetTitle1 = datasetLabelParagraph.createRun();
runDatasetTitle1.setText("Title: ");
runDatasetTitle1.setColor("000000");
//runDatasetTitle1.setBold(true);
//runDatasetTitle1.setFontSize(12);
XWPFRun runDatasetTitle = datasetLabelParagraph.createRun();
runDatasetTitle.setText(datasetEntity.getLabel());
runDatasetTitle.setColor("116a78");
//runDatasetTitle.setBold(true);
//runDatasetTitle.setFontSize(12);
XWPFParagraph datasetTemplateParagraph = document.createParagraph();
// datasetTemplateParagraph.setStyle("Heading3");
XWPFRun runDatasetTemplate1 = datasetTemplateParagraph.createRun();
runDatasetTemplate1.setText("Template: ");
runDatasetTemplate1.setColor("000000");
//runDatasetTemplate1.setBold(true);
//runDatasetTemplate1.setFontSize(12);
XWPFRun runDatasetTemplate = datasetTemplateParagraph.createRun();
runDatasetTemplate.setText(descriptionTemplateFileTransformerModelFileModel.getLabel());
runDatasetTemplate.setColor("116a78");
//runDatasetTemplate.setBold(true);
//runDatasetTemplate.setFontSize(12);
// /*XWPFParagraph externalReferencesParagraph = document.createParagraph();
// externalReferencesParagraph.setStyle("Heading3");
// XWPFRun externalReferencesRun = externalReferencesParagraph.createRun();
// externalReferencesRun.setText("External References");
// externalReferencesRun.setColor("2E75B6");
// externalReferencesRun.setBold(true);
// externalReferencesRun.setFontSize(12);
//
// wordBuilder.addParagraphContent("Data Repositories", document, ParagraphStyle.HEADER4, BigInteger.ZERO);
// if (datasetEntity.getDatasetDataRepositories().size() > 0) {
// wordBuilder.addParagraphContent(datasetEntity.getDatasetDataRepositories().stream().map(DatasetDataRepository::getDataRepository).map(DataRepository::getLabel).collect(Collectors.joining(", "))
// , document, ParagraphStyle.TEXT, BigInteger.ZERO);
// }
// wordBuilder.addParagraphContent("External Datasets", document, ParagraphStyle.HEADER4, BigInteger.ZERO);
// if (datasetEntity.getDatasetExternalDatasets().size() > 0) {
// wordBuilder.addParagraphContent(datasetEntity.getDatasetExternalDatasets().stream().map(DatasetExternalDataset::getExternalDataset).map(ExternalDataset::getLabel).collect(Collectors.joining(", "))
// , document, ParagraphStyle.TEXT, BigInteger.ZERO);
// }
// wordBuilder.addParagraphContent("Registries", document, ParagraphStyle.HEADER4, BigInteger.ZERO);
// if (datasetEntity.getRegistries().size() > 0) {
// wordBuilder.addParagraphContent(datasetEntity.getRegistries().stream().map(Registry::getLabel).collect(Collectors.joining(", "))
// , document, ParagraphStyle.TEXT, BigInteger.ZERO);
// }
// wordBuilder.addParagraphContent("Services", document, ParagraphStyle.HEADER4, BigInteger.ZERO);
// if (datasetEntity.getServices().size() > 0) {
// wordBuilder.addParagraphContent(datasetEntity.getServices().stream().map(DatasetService::getService).map(Service::getLabel).collect(Collectors.joining(", "))
// , document, ParagraphStyle.TEXT, BigInteger.ZERO);
// }
// *//*wordBuilder.addParagraphContent("Tags", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
// if (datasetEntity.().size() > 0) {
// wordBuilder.addParagraphContent(datasetEntity.getServices().stream().map(DatasetService::getService).map(Service::getLabel).collect(Collectors.joining(", "))
// , document, ParagraphStyle.HEADER4, BigInteger.ZERO);
// }*/
//
//
XWPFParagraph datasetDescParagraph = document.createParagraph();
XWPFRun runDatasetDescription1 = datasetDescParagraph.createRun();
runDatasetDescription1.setText("Description: ");
runDatasetDescription1.setColor("000000");
XWPFRun runDatasetDescription = datasetDescParagraph.createRun();
runDatasetDescription.setText(descriptionTemplateFileTransformerModelFileModel.getLabel());
runDatasetDescription.setColor("116a78");
//wordBuilder.addParagraphContent(datasetEntity.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO, 0);
document.createParagraph();
try {
wordBuilder.build(document, datasetEntity.getDescriptionTemplate());
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
// Page break at the end of the Dataset.
XWPFParagraph parBreakDataset = document.createParagraph();
parBreakDataset.setPageBreak(true);
});
}
}
// // Removes the top empty headings.
// for (int i = 0; i < 6; i++) {
// document.removeBodyElement(0);
// }
if(powered_pos != -1) {
document.getLastParagraph().setPageBreak(false);
document.createParagraph();
document.setParagraph(powered_par, document.getParagraphs().size() - 1);
document.createParagraph();
document.setParagraph(argos_img_par, document.getParagraphs().size() - 1);
document.removeBodyElement(powered_pos + 1);
document.removeBodyElement(powered_pos + 1);
}
wordBuilder.fillFooter(dmpEntity, null, document, false);
String fileName;
if (!grants.isEmpty() && grants.get(0).getLabel() != null) {
fileName = "DMP_" + grants.get(0).getLabel();
}
else {
fileName = "DMP_" + dmpEntity.getLabel();
}
if (versioned) {
fileName += "_" + dmpEntity.getVersion();
}
// fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "");
FileEnvelopeInternal exportEnvelope = new FileEnvelopeInternal();
exportEnvelope.setFilename(fileName + ".docx");
String uuid = UUID.randomUUID().toString();
File exportFile = new File(fileStorageProperties.getTemp() + "\\" + uuid + ".docx");
FileOutputStream out = new FileOutputStream(exportFile);
document.write(out);
out.close();
exportEnvelope.setFile(exportFile);
return exportEnvelope;
}
private FileEnvelope getDescriptionWordDocument(DescriptionFileTransformerModel descriptionFileTransformerModel) throws IOException {
FileEnvelopeInternal wordFile = getDescriptionWordDocumentInternal(descriptionFileTransformerModel);
FileEnvelope fileEnvelope = new FileEnvelope();
fileEnvelope.setFilename(wordFile.getFilename());
try (FileInputStream fis = new FileInputStream(wordFile.getFile())) {
fileEnvelope.setFile(fileStorageService.storeFile(fis.readAllBytes()));
}
return fileEnvelope;
}
private FileEnvelopeInternal getDescriptionWordDocumentInternal(DescriptionFileTransformerModel descriptionFileTransformerModelEntityEntity) throws IOException {
WordBuilder wordBuilder = new WordBuilder(fileTemplateProperties, fileStorageProperties);
DmpFileTransformerModel dmpEntity = descriptionFileTransformerModelEntityEntity.getDmp();
if (dmpEntity == null) {
throw new IllegalArgumentException("Dmp is invalid");
}
XWPFDocument document = new XWPFDocument(new FileInputStream(ResourceUtils.getFile(fileTemplateProperties.getWordDescriptionTemplate())));
wordBuilder.fillFirstPage(dmpEntity, descriptionFileTransformerModelEntityEntity, document, true);
wordBuilder.fillFooter(dmpEntity, descriptionFileTransformerModelEntityEntity, document, true);
int powered_pos = wordBuilder.findPosOfPoweredBy(document);
XWPFParagraph powered_par = null;
XWPFParagraph argos_img_par = null;
if(powered_pos != -1) {
powered_par = document.getParagraphArray(powered_pos);
argos_img_par = document.getParagraphArray(powered_pos + 1);
}
// wordBuilder.addParagraphContent(datasetEntity.getLabel(), document, ParagraphStyle.HEADER1, BigInteger.ZERO);
// Space below Dataset title.
// XWPFParagraph parBreakDataset = document.createParagraph();
//
// XWPFParagraph datasetTemplateParagraph = document.createParagraph();
// datasetTemplateParagraph.setStyle("Heading2");
// XWPFRun runDatasetTemplate1 = datasetTemplateParagraph.createRun();
// runDatasetTemplate1.setText("Template: ");
// runDatasetTemplate1.setBold(true);
// runDatasetTemplate1.setFontSize(12);
// XWPFRun runDatasetTemplate = datasetTemplateParagraph.createRun();
// runDatasetTemplate.setText(datasetEntity.getProfile().getLabel());
// runDatasetTemplate.setColor("2E75B6");
// runDatasetTemplate.setBold(true);
// runDatasetTemplate.setFontSize(12);
//
// wordBuilder.addParagraphContent(datasetEntity.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO);
/*XWPFParagraph externalReferencesParagraph = document.createParagraph();
externalReferencesParagraph.setStyle("Heading2");
XWPFRun externalReferencesRun = externalReferencesParagraph.createRun();
externalReferencesRun.setText("External References");
externalReferencesRun.setColor("2E75B6");
externalReferencesRun.setBold(true);
externalReferencesRun.setFontSize(12);
wordBuilder.addParagraphContent("Data Repositories", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
if (datasetEntity.getDatasetDataRepositories().size() > 0) {
wordBuilder.addParagraphContent(datasetEntity.getDatasetDataRepositories().stream().map(DatasetDataRepository::getDataRepository).map(DataRepository::getLabel).collect(Collectors.joining(", "))
, document, ParagraphStyle.TEXT, BigInteger.ZERO);
}
wordBuilder.addParagraphContent("External Datasets", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
if (datasetEntity.getDatasetExternalDatasets().size() > 0) {
wordBuilder.addParagraphContent(datasetEntity.getDatasetExternalDatasets().stream().map(DatasetExternalDataset::getExternalDataset).map(ExternalDataset::getLabel).collect(Collectors.joining(", "))
, document, ParagraphStyle.TEXT, BigInteger.ZERO);
}
wordBuilder.addParagraphContent("Registries", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
if (datasetEntity.getRegistries().size() > 0) {
wordBuilder.addParagraphContent(datasetEntity.getRegistries().stream().map(Registry::getLabel).collect(Collectors.joining(", "))
, document, ParagraphStyle.TEXT, BigInteger.ZERO);
}
wordBuilder.addParagraphContent("Services", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
if (datasetEntity.getServices().size() > 0) {
wordBuilder.addParagraphContent(datasetEntity.getServices().stream().map(DatasetService::getService).map(Service::getLabel).collect(Collectors.joining(", "))
, document, ParagraphStyle.TEXT, BigInteger.ZERO);
}*/
/*wordBuilder.addParagraphContent("Tags", document, ParagraphStyle.HEADER3, BigInteger.ZERO);
if (datasetEntity.().size() > 0) {
wordBuilder.addParagraphContent(datasetEntity.getServices().stream().map(DatasetService::getService).map(Service::getLabel).collect(Collectors.joining(", "))
, document, ParagraphStyle.HEADER4, BigInteger.ZERO);
}*/
// wordBuilder.addParagraphContent("Dataset Description", document, ParagraphStyle.HEADER2, BigInteger.ZERO);
wordBuilder.build(document, descriptionFileTransformerModelEntityEntity.getDescriptionTemplate());
String label = descriptionFileTransformerModelEntityEntity.getLabel().replaceAll("[^a-zA-Z0-9+ ]", "");
// File exportFile = new File(label + ".docx");
// Removes the top empty headings.
// for (int i = 0; i < 6; i++) {
// document.removeBodyElement(0);
// }
if(powered_pos != -1) {
document.getLastParagraph().setPageBreak(false);
document.createParagraph();
document.setParagraph(powered_par, document.getParagraphs().size() - 1);
document.createParagraph();
document.setParagraph(argos_img_par, document.getParagraphs().size() - 1);
document.removeBodyElement(powered_pos + 1);
document.removeBodyElement(powered_pos + 1);
}
label = descriptionFileTransformerModelEntityEntity.getLabel().replaceAll("[^a-zA-Z0-9+ ]", "");
FileEnvelopeInternal exportEnvelope = new FileEnvelopeInternal();
exportEnvelope.setFilename(label + ".docx");
String uuid = UUID.randomUUID().toString();
File exportFile = new File(fileStorageProperties.getTemp() + uuid + ".docx");
FileOutputStream out = new FileOutputStream(exportFile);
document.write(out);
out.close();
exportEnvelope.setFile(exportFile);
return exportEnvelope;
//FileOutputStream out = new FileOutputStream(exportFile);
// document.write(out);
// out.close();
// return exportFile;
}
}

View File

@ -2,13 +2,12 @@ package eu.eudat.file.transformer.model.enums;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.annotation.JsonValue;
import eu.eudat.file.transformer.enums.DatabaseEnum; import eu.eudat.commonmodels.enums.EnumUtils;
import eu.eudat.file.transformer.enums.DmpVersionStatus; import eu.eudat.commonmodels.enums.EnumValueProvider;
import eu.eudat.file.transformer.enums.EnumUtils;
import java.util.Map; import java.util.Map;
public enum FileFormats implements DatabaseEnum<String> { public enum FileFormats implements EnumValueProvider<String> {
DOCX("docx"), DOCX("docx"),
PDF("pdf"); PDF("pdf");

View File

@ -0,0 +1,31 @@
package eu.eudat.file.transformer.model.enums;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import eu.eudat.commonmodels.enums.EnumUtils;
import eu.eudat.commonmodels.enums.EnumValueProvider;
import java.util.Map;
public enum ParagraphStyle implements EnumValueProvider<Integer> {
TEXT(0), HEADER1(1), HEADER2(2), HEADER3(3), HEADER4(4), TITLE(5), FOOTER(6), COMMENT(7), HEADER5(8), HEADER6(9), HTML(10), IMAGE(11);
private final Integer value;
private ParagraphStyle(Integer value) {
this.value = value;
}
@JsonValue
public Integer getValue() {
return value;
}
private static final Map<Integer, ParagraphStyle> map = EnumUtils.getEnumValueMap(ParagraphStyle.class);
@JsonCreator
public static ParagraphStyle of(Integer i) {
return map.get(i);
}
}

View File

@ -1,28 +0,0 @@
package eu.eudat.file.transformer.model.file;
import java.io.File;
/**
* Created by ikalyvas on 3/6/2018.
*/
public class FileEnvelopeInternal {
private String filename;
private File file;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}

View File

@ -1,4 +1,4 @@
package eu.eudat.file.transformer.utils.interfaces; package eu.eudat.file.transformer.model.interfaces;
/** /**
* Created by ikalyvas on 2/27/2018. * Created by ikalyvas on 2/27/2018.

View File

@ -0,0 +1,5 @@
package eu.eudat.file.transformer.service.pdf;
public interface PdfService {
byte[] convertToPDF(byte[] file);
}

View File

@ -1,9 +1,9 @@
package eu.eudat.file.transformer.configuration; package eu.eudat.file.transformer.service.pdf;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@EnableConfigurationProperties({FilePathProperties.class}) @EnableConfigurationProperties({PdfServiceProperties.class})
public class FilePathConfiguration { public class PdfServiceConfiguration {
} }

View File

@ -0,0 +1,36 @@
package eu.eudat.file.transformer.service.pdf;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import java.util.UUID;
@Component
public class PdfServiceImpl implements PdfService {
private final PdfServiceProperties pdfServiceProperties;
public PdfServiceImpl(PdfServiceProperties pdfServiceProperties) {
this.pdfServiceProperties = pdfServiceProperties;
}
@Override
public byte[] convertToPDF(byte[] file) {
WebClient webClient = WebClient.builder().baseUrl(pdfServiceProperties.getUrl()).build();
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("files", new ByteArrayResource(file)).filename(UUID.randomUUID() + ".docx");
return webClient.post().uri("forms/libreoffice/convert")
.headers(httpHeaders -> {
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
httpHeaders.add("Content-disposition", "attachment; filename=" + UUID.randomUUID() + ".pdf");
httpHeaders.add("Content-type", "application/pdf");
})
.body(BodyInserters.fromMultipartData(builder.build()))
.retrieve().bodyToMono(byte[].class).block();
}
}

View File

@ -1,14 +1,14 @@
package eu.eudat.file.transformer.configuration; package eu.eudat.file.transformer.service.pdf;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.ConstructorBinding; import org.springframework.boot.context.properties.bind.ConstructorBinding;
@ConfigurationProperties(prefix = "pdf.converter") @ConfigurationProperties(prefix = "pdf.converter")
public class PdfProperties { public class PdfServiceProperties {
private final String url; private final String url;
@ConstructorBinding @ConstructorBinding
public PdfProperties(String url) { public PdfServiceProperties(String url) {
this.url = url; this.url = url;
} }

View File

@ -0,0 +1,10 @@
package eu.eudat.file.transformer.service.pid;
import eu.eudat.file.transformer.model.PidLink;
import java.util.List;
public interface PidService {
PidLink getPid(String pidType);
List<PidLink> loadPidLinks();
}

View File

@ -0,0 +1,50 @@
package eu.eudat.file.transformer.service.pid;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.file.transformer.service.wordfiletransformer.WordFileTransformerServiceProperties;
import eu.eudat.file.transformer.model.PidLink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import java.io.IOException;
import java.util.List;
@Component
public class PidServiceImpl implements PidService {
private static final Logger logger = LoggerFactory.getLogger(PidServiceImpl.class);
private final WordFileTransformerServiceProperties properties;
private final ObjectMapper objectMapper = new ObjectMapper();
private List<PidLink> pidLinks = null;
public PidServiceImpl(WordFileTransformerServiceProperties properties) {
this.properties = properties;
}
@Override
public List<PidLink> loadPidLinks() {
if (pidLinks != null) return pidLinks;
try {
pidLinks = objectMapper.readValue(ResourceUtils.getFile(this.properties.getPidTemplate()), PidLinksWrapper.class).getPidLinks();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return pidLinks;
}
@Override
public PidLink getPid(String pidType) {
return this.loadPidLinks().stream().filter(pl -> pl.getPid().equals(pidType)).findFirst().orElse(null);
}
protected static class PidLinksWrapper {
private List<PidLink> pidLinks;
public List<PidLink> getPidLinks() {
return pidLinks;
}
public void setPidLinks(List<PidLink> pidLinks) {
this.pidLinks = pidLinks;
}
}
}

View File

@ -0,0 +1,7 @@
package eu.eudat.file.transformer.service.storage;
public interface FileStorageService {
String storeFile(byte[] data);
byte[] readFile(String fileRef);
}

View File

@ -0,0 +1,9 @@
package eu.eudat.file.transformer.service.storage;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({FileStorageServiceProperties.class})
public class FileStorageServiceConfiguration {
}

View File

@ -1,6 +1,5 @@
package eu.eudat.file.transformer.utils.storage; package eu.eudat.file.transformer.service.storage;
import eu.eudat.file.transformer.configuration.FileStorageProperties;
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;
@ -12,22 +11,24 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.Locale;
import java.util.UUID; import java.util.UUID;
@Service @Service
public class FileStorageService { public class FileStorageServiceImpl implements FileStorageService {
private final static Logger logger = LoggerFactory.getLogger(FileStorageService.class); private final static Logger logger = LoggerFactory.getLogger(FileStorageServiceImpl.class);
private final FileStorageProperties properties; private final FileStorageServiceProperties properties;
@Autowired @Autowired
public FileStorageService(FileStorageProperties properties) { public FileStorageServiceImpl(FileStorageServiceProperties properties) {
this.properties = properties; this.properties = properties;
} }
@Override
public String storeFile(byte[] data) { public String storeFile(byte[] data) {
try { try {
String fileName = UUID.randomUUID().toString(); String fileName = UUID.randomUUID().toString().replace("-", "").toLowerCase(Locale.ROOT);
Path storagePath = Paths.get(properties.getTransientPath() + "/" + fileName); Path storagePath = Paths.get(properties.getTransientPath() + "/" + fileName);
Files.write(storagePath, data, StandardOpenOption.CREATE_NEW); Files.write(storagePath, data, StandardOpenOption.CREATE_NEW);
return fileName; return fileName;
@ -37,12 +38,13 @@ public class FileStorageService {
return null; return null;
} }
@Override
public byte[] readFile(String fileRef) { public byte[] readFile(String fileRef) {
try (FileInputStream inputStream = new FileInputStream(properties.getTransientPath() + "/" + fileRef)) { try (FileInputStream inputStream = new FileInputStream(properties.getTransientPath() + "/" + fileRef)) {
return inputStream.readAllBytes(); return inputStream.readAllBytes();
} catch (IOException e) { } catch (IOException e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} }
return new byte[1]; return new byte[0];
} }
} }

View File

@ -1,15 +1,15 @@
package eu.eudat.file.transformer.configuration; package eu.eudat.file.transformer.service.storage;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.ConstructorBinding; import org.springframework.boot.context.properties.bind.ConstructorBinding;
@ConfigurationProperties(prefix = "file.storage") @ConfigurationProperties(prefix = "file.storage")
public class FileStorageProperties { public class FileStorageServiceProperties {
private final String temp; private final String temp;
private final String transientPath; private final String transientPath;
@ConstructorBinding @ConstructorBinding
public FileStorageProperties(String temp, String transientPath) { public FileStorageServiceProperties(String temp, String transientPath) {
this.temp = temp; this.temp = temp;
this.transientPath = transientPath; this.transientPath = transientPath;
} }

View File

@ -0,0 +1,485 @@
package eu.eudat.file.transformer.service.wordfiletransformer;
import eu.eudat.commonmodels.enums.DescriptionStatus;
import eu.eudat.commonmodels.enums.DmpAccessType;
import eu.eudat.commonmodels.enums.DmpStatus;
import eu.eudat.commonmodels.models.dmp.DmpBlueprintValueModel;
import eu.eudat.commonmodels.models.dmp.DmpModel;
import eu.eudat.commonmodels.models.FileEnvelopeModel;
import eu.eudat.commonmodels.models.description.DescriptionModel;
import eu.eudat.commonmodels.models.descriptiotemplate.DescriptionTemplateModel;
import eu.eudat.commonmodels.models.dmpblueprint.*;
import eu.eudat.commonmodels.models.dmpreference.DmpReferenceModel;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
import eu.eudat.file.transformer.interfaces.FileTransformerClient;
import eu.eudat.file.transformer.interfaces.FileTransformerConfiguration;
import eu.eudat.file.transformer.model.enums.FileFormats;
import eu.eudat.file.transformer.models.misc.FileFormat;
import eu.eudat.file.transformer.service.pdf.PdfService;
import eu.eudat.file.transformer.model.enums.ParagraphStyle;
import eu.eudat.file.transformer.service.storage.FileStorageService;
import eu.eudat.file.transformer.service.wordfiletransformer.word.WordBuilder;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import javax.management.InvalidApplicationException;
import java.io.*;
import java.math.BigInteger;
import java.time.Instant;
import java.util.*;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class WordFileTransformerService implements FileTransformerClient {
private final static Logger logger = LoggerFactory.getLogger(WordFileTransformerService.class);
private final static List<FileFormat> FILE_FORMATS = List.of(
new FileFormat(FileFormats.PDF.getValue(), true, "fa-file-pdf-o"),
new FileFormat(FileFormats.DOCX.getValue(), true, "fa-file-word-o"));
private final WordFileTransformerServiceProperties wordFileTransformerServiceProperties;
private final PdfService pdfService;
private final WordBuilder wordBuilder;
private final FileStorageService storageService;
@Autowired
public WordFileTransformerService(
WordFileTransformerServiceProperties wordFileTransformerServiceProperties,
PdfService pdfService, WordBuilder wordBuilder, FileStorageService storageService) {
this.wordFileTransformerServiceProperties = wordFileTransformerServiceProperties;
this.pdfService = pdfService;
this.wordBuilder = wordBuilder;
this.storageService = storageService;
}
@Override
public FileEnvelopeModel exportDmp(DmpModel dmp, String variant) throws IOException, InvalidApplicationException {
FileFormats fileFormat = FileFormats.of(variant);
byte[] bytes = this.buildDmpWordDocument(dmp);
String filename = switch (fileFormat) {
case DOCX -> this.getDmpFileName(dmp, ".docx");
case PDF -> {
bytes = this.pdfService.convertToPDF(bytes);
yield this.getDmpFileName(dmp, ".pdf");
}
default -> throw new InvalidApplicationException("Invalid type " + fileFormat);
};
String fileRef = this.storageService.storeFile(bytes);
FileEnvelopeModel wordFile = new FileEnvelopeModel();
wordFile.setFileRef(fileRef);
wordFile.setFilename(filename);
return wordFile;
}
@Override
public FileEnvelopeModel exportDescription(DescriptionModel descriptionModel, String variant) throws InvalidApplicationException, IOException {
FileFormats fileFormat = FileFormats.of(variant);
byte[] bytes = this.buildDescriptionWordDocument(descriptionModel);
String filename = switch (fileFormat) {
case DOCX -> this.getDescriptionFileName(descriptionModel, ".docx");
case PDF -> {
bytes = this.pdfService.convertToPDF(bytes);
yield this.getDescriptionFileName(descriptionModel, ".pdf");
}
default -> throw new InvalidApplicationException("Invalid type " + fileFormat);
};
String fileRef = this.storageService.storeFile(bytes);
FileEnvelopeModel wordFile = new FileEnvelopeModel();
wordFile.setFileRef(fileRef);
wordFile.setFilename(filename);
return wordFile;
}
@Override
public DmpModel importDmp(FileEnvelopeModel envelope) {
throw new UnsupportedOperationException("import not supported");
}
@Override
public DescriptionModel importDescription(FileEnvelopeModel envelope) {
throw new UnsupportedOperationException("import not supported");
}
@Override
public FileTransformerConfiguration getConfiguration() {
FileTransformerConfiguration configuration = new FileTransformerConfiguration();
configuration.setFileTransformerId("docx/pdf");
configuration.setExportVariants(FILE_FORMATS);
configuration.setImportVariants(null);
configuration.setUseSharedStorage(true);
return configuration;
}
private List<ReferenceModel> getReferenceModelOfTypeCode(DmpModel dmp, String code, UUID blueprintId){
List<ReferenceModel> response = new ArrayList<>();
if (dmp.getReferences() == null) return response;
for (DmpReferenceModel dmpReferenceModel : dmp.getReferences()){
if (dmpReferenceModel.getReference() != null && dmpReferenceModel.getReference().getType() != null && dmpReferenceModel.getReference().getType().getCode() != null && dmpReferenceModel.getReference().getType().getCode().equals(code)){
if (blueprintId == null || (dmpReferenceModel.getData() != null && blueprintId.equals(dmpReferenceModel.getData().getBlueprintFieldId()))) response.add(dmpReferenceModel.getReference());
}
}
return response;
}
private byte[] buildDmpWordDocument(DmpModel dmpEntity) throws IOException, InvalidApplicationException {
if (dmpEntity == null) throw new IllegalArgumentException("DmpEntity required");
DmpBlueprintModel dmpBlueprintModel = dmpEntity.getDmpBlueprint();
if (dmpBlueprintModel == null) throw new IllegalArgumentException("DmpBlueprint required");
if (dmpBlueprintModel.getDefinition() == null) throw new IllegalArgumentException("DmpBlueprint Definition required");
if (dmpBlueprintModel.getDefinition().getSections() == null) throw new IllegalArgumentException("DmpBlueprint Section required");
XWPFDocument document = new XWPFDocument(new FileInputStream(ResourceUtils.getFile(this.wordFileTransformerServiceProperties.getWordDmpTemplate())));
this.wordBuilder.fillFirstPage(dmpEntity, null, document, false);
int powered_pos = this.wordBuilder.findPosOfPoweredBy(document);
XWPFParagraph powered_par = null;
XWPFParagraph argos_img_par = null;
if (powered_pos != -1) {
powered_par = document.getParagraphArray(powered_pos);
argos_img_par = document.getParagraphArray(powered_pos + 1);
}
for (SectionModel sectionModel : dmpBlueprintModel.getDefinition().getSections()) {
buildDmpSection(dmpEntity, sectionModel, document);
}
if (powered_pos != -1) {
document.getLastParagraph().setPageBreak(false);
document.createParagraph();
document.setParagraph(powered_par, document.getParagraphs().size() - 1);
document.createParagraph();
document.setParagraph(argos_img_par, document.getParagraphs().size() - 1);
document.removeBodyElement(powered_pos + 1);
document.removeBodyElement(powered_pos + 1);
}
this.wordBuilder.fillFooter(dmpEntity, null, document);
ByteArrayOutputStream out = new ByteArrayOutputStream();
document.write(out);
byte[] bytes = out.toByteArray();
out.close();
return bytes;
}
private void buildDmpSection(DmpModel dmpEntity, SectionModel sectionModel, XWPFDocument document) throws InvalidApplicationException {
this.wordBuilder.addParagraphContent("Section " + sectionModel.getOrdinal(), document, ParagraphStyle.HEADER1, BigInteger.ZERO, 0);
XWPFParagraph sectionInfoParagraph = document.createParagraph();
sectionInfoParagraph.setSpacingBetween(1.0);
XWPFRun runSectionTitle = sectionInfoParagraph.createRun();
runSectionTitle.setText("Title: ");
runSectionTitle.setColor("000000");
XWPFRun runSectionTitleText = sectionInfoParagraph.createRun();
runSectionTitleText.setText(sectionModel.getLabel());
runSectionTitleText.setColor("116a78");
XWPFParagraph sectionDescriptionParagraph = document.createParagraph();
XWPFRun runSectionDescription = sectionDescriptionParagraph.createRun();
runSectionDescription.setText("Description: ");
runSectionDescription.setColor("000000");
XWPFRun runSectionDescriptionText = sectionDescriptionParagraph.createRun();
runSectionDescriptionText.setText(sectionModel.getDescription());
runSectionDescriptionText.setColor("116a78");
this.wordBuilder.addParagraphContent("Section Fields", document, ParagraphStyle.HEADER2, BigInteger.ZERO, 0);
if (sectionModel.getFields() != null) {
sectionModel.getFields().sort(Comparator.comparingInt(FieldModel::getOrdinal));
for (FieldModel fieldModel : sectionModel.getFields()) {
buildDmpSectionField(dmpEntity, document, fieldModel);
}
}
final boolean isFinalized = dmpEntity.getStatus() != null && dmpEntity.getStatus().equals(DmpStatus.Finalized);
final boolean isPublic = dmpEntity.getPublicAfter() != null && dmpEntity.getPublicAfter().isAfter(Instant.now());
List<DescriptionModel> descriptions = dmpEntity.getDescriptions().stream()
.filter(item -> item.getStatus() != DescriptionStatus.Canceled)
.filter(item -> !isPublic && !isFinalized || item.getStatus() == DescriptionStatus.Finalized)
.filter(item -> item.getSectionId().equals(sectionModel.getId()))
.sorted(Comparator.comparing(DescriptionModel::getCreatedAt)).toList();
if (!descriptions.isEmpty()) {
buildSectionDescriptions(document, descriptions);
}
}
private void buildSectionDescriptions(XWPFDocument document, List<DescriptionModel> descriptions) {
if (document == null) throw new IllegalArgumentException("Document required");
if (descriptions == null) throw new IllegalArgumentException("Descriptions required");
List<DescriptionTemplateModel> descriptionTemplateModels = descriptions.stream().map(DescriptionModel::getDescriptionTemplate).toList();
if (descriptionTemplateModels.isEmpty()) return;
this.wordBuilder.addParagraphContent("Section descriptions", document, ParagraphStyle.HEADER2, BigInteger.ZERO, 0);
this.wordBuilder.addParagraphContent("Description Templates", document, ParagraphStyle.HEADER4, BigInteger.ZERO, 0);
for (DescriptionTemplateModel descriptionTemplateModelEntity : descriptionTemplateModels) {
XWPFParagraph templateParagraph = document.createParagraph();
XWPFRun runTemplateLabel = templateParagraph.createRun();
runTemplateLabel.setText("" + descriptionTemplateModelEntity.getLabel());
runTemplateLabel.setColor("116a78");
}
for (DescriptionModel descriptionModel : descriptions){
buildSectionDescription(document, descriptionModel);
}
}
private void buildSectionDescription(XWPFDocument document, DescriptionModel descriptionModel) {
if (document == null) throw new IllegalArgumentException("Document required");
if (descriptionModel == null) throw new IllegalArgumentException("DescriptionModel required");
DescriptionTemplateModel descriptionTemplateModelFileModel = descriptionModel.getDescriptionTemplate();
// Dataset Description custom style.
XWPFParagraph datasetDescriptionParagraph = document.createParagraph();
datasetDescriptionParagraph.setStyle("Heading4");
datasetDescriptionParagraph.setSpacingBetween(1.5);
XWPFRun datasetDescriptionRun = datasetDescriptionParagraph.createRun();
datasetDescriptionRun.setText("Dataset Description");
datasetDescriptionRun.setFontSize(15);
// Custom style for the Dataset title.
XWPFParagraph datasetLabelParagraph = document.createParagraph();
datasetLabelParagraph.setSpacingBetween(1.0);
XWPFRun runDatasetTitle1 = datasetLabelParagraph.createRun();
runDatasetTitle1.setText("Title: ");
runDatasetTitle1.setColor("000000");
XWPFRun runDatasetTitle = datasetLabelParagraph.createRun();
runDatasetTitle.setText(descriptionModel.getLabel());
runDatasetTitle.setColor("116a78");
XWPFParagraph datasetTemplateParagraph = document.createParagraph();
XWPFRun runDatasetTemplate1 = datasetTemplateParagraph.createRun();
runDatasetTemplate1.setText("Template: ");
runDatasetTemplate1.setColor("000000");
XWPFRun runDatasetTemplate = datasetTemplateParagraph.createRun();
runDatasetTemplate.setText(descriptionTemplateModelFileModel != null ? descriptionTemplateModelFileModel.getLabel() : "");
runDatasetTemplate.setColor("116a78");
XWPFParagraph datasetDescParagraph = document.createParagraph();
XWPFRun runDatasetDescription1 = datasetDescParagraph.createRun();
runDatasetDescription1.setText("Description: ");
runDatasetDescription1.setColor("000000");
XWPFRun runDatasetDescription = datasetDescParagraph.createRun();
runDatasetDescription.setText(descriptionTemplateModelFileModel != null ? descriptionTemplateModelFileModel.getLabel() : "");
runDatasetDescription.setColor("116a78");
document.createParagraph();
try {
this.wordBuilder.build(document, descriptionModel.getDescriptionTemplate(), descriptionModel.getProperties());
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
// Page break at the end of the Dataset.
XWPFParagraph parBreakDataset = document.createParagraph();
parBreakDataset.setPageBreak(true);
}
private void buildDmpSectionField(DmpModel dmpEntity, XWPFDocument document, FieldModel fieldModel) throws InvalidApplicationException {
if (fieldModel == null) throw new IllegalArgumentException("Field required");
if (fieldModel.getCategory() == null) throw new IllegalArgumentException("Field is required" + fieldModel.getId() + " " + fieldModel.getLabel());
switch (fieldModel.getCategory()){
case System -> {
buildDmpSectionSystemField(dmpEntity, document, (SystemFieldModel) fieldModel);
}
case Extra -> buildDmpSectionExtraField(dmpEntity, document, (ExtraFieldModel) fieldModel);
case ReferenceType -> {
buildDmpSectionFieldTitleDescription(document, fieldModel);
buildDmpSectionReferenceTypeField(dmpEntity, document, (ReferenceTypeFieldModel) fieldModel);
}
default -> throw new InvalidApplicationException("Invalid type " + fieldModel.getCategory());
}
}
private void buildDmpSectionReferenceTypeField(DmpModel dmpEntity, XWPFDocument document, ReferenceTypeFieldModel referenceField) {
if (referenceField == null) throw new IllegalArgumentException("ReferenceField required");
if (dmpEntity == null) throw new IllegalArgumentException("DmpEntity required");
if (document == null) throw new IllegalArgumentException("Document required");
if (referenceField.getReferenceType() == null) throw new IllegalArgumentException("ReferenceField type required");
if (referenceField.getReferenceType().getCode() == null && !referenceField.getReferenceType().getCode().isBlank()) throw new IllegalArgumentException("ReferenceField type code required");
XWPFParagraph systemFieldInput = document.createParagraph();
systemFieldInput.setSpacingBetween(1.0);
XWPFRun runInput = systemFieldInput.createRun();
runInput.setText("Input: ");
runInput.setColor("000000");
List<ReferenceModel> referenceModels = this.getReferenceModelOfTypeCode(dmpEntity, referenceField.getReferenceType().getCode(), referenceField.getId());
if (referenceModels != null) {
for (ReferenceModel reference : referenceModels) {
XWPFRun runResearcher = systemFieldInput.createRun();
runResearcher.setText("" + reference.getLabel());
runResearcher.setColor("116a78");
}
}
document.createParagraph();
}
private void buildDmpSectionFieldTitleDescription(XWPFDocument document, FieldModel fieldModel){
if (fieldModel == null) throw new IllegalArgumentException("FieldModel required");
if (document == null) throw new IllegalArgumentException("Document required");
XWPFParagraph systemFieldParagraph = document.createParagraph();
systemFieldParagraph.setSpacingBetween(1.0);
XWPFRun runSyStemFieldTitle = systemFieldParagraph.createRun();
runSyStemFieldTitle.setText("Title: ");
runSyStemFieldTitle.setColor("000000");
XWPFRun runSystemFieldTitleText = systemFieldParagraph.createRun();
runSystemFieldTitleText.setText(fieldModel.getLabel());
runSystemFieldTitleText.setColor("116a78");
if (fieldModel.getDescription() != null && !fieldModel.getDescription().isEmpty()) {
XWPFParagraph systemFieldDescription = document.createParagraph();
systemFieldDescription.setSpacingBetween(1.0);
XWPFRun runSyStemFieldDescription = systemFieldDescription.createRun();
runSyStemFieldDescription.setText("Description: ");
runSyStemFieldDescription.setColor("000000");
XWPFRun runSystemFieldDescriptionText = systemFieldDescription.createRun();
runSystemFieldDescriptionText.setText(fieldModel.getDescription());
runSystemFieldDescriptionText.setColor("116a78");
}
}
private void buildDmpSectionSystemField(DmpModel dmpEntity, XWPFDocument document, SystemFieldModel systemField) throws InvalidApplicationException {
if (systemField == null) throw new IllegalArgumentException("SystemField required");
if (dmpEntity == null) throw new IllegalArgumentException("DmpEntity required");
if (document == null) throw new IllegalArgumentException("Document required");
XWPFParagraph systemFieldInput = document.createParagraph();
systemFieldInput.setSpacingBetween(1.0);
XWPFRun runInput = systemFieldInput.createRun();
runInput.setText("Input: ");
runInput.setColor("000000");
switch (systemField.getSystemFieldType()) {
case Title:
XWPFRun runTitle = systemFieldInput.createRun();
runTitle.setText(dmpEntity.getLabel());
runTitle.setColor("116a78");
break;
case Description:
XWPFRun runDescription = systemFieldInput.createRun();
runDescription.setText(dmpEntity.getDescription());
runDescription.setColor("116a78");
break;
case AccessRights:
if (dmpEntity.getAccessType() != null) {
XWPFRun runAccessRights = systemFieldInput.createRun();
runAccessRights.setText(dmpEntity.getAccessType().equals(DmpAccessType.Public) ? "Public" : "Restricted"); //TODO
//runAccessRights.setText(dmpProperties.get("visible").toString());
runAccessRights.setColor("116a78");
}
break;
case Contact:
if (dmpEntity.getCreator() != null) {
XWPFRun runContact = systemFieldInput.createRun();
runContact.setText(dmpEntity.getCreator().getName());
runContact.setColor("116a78");
}
break;
case User:
case Language:
break;
default:
throw new InvalidApplicationException("Invalid type " + systemField.getSystemFieldType());
}
document.createParagraph();
}
private void buildDmpSectionExtraField(DmpModel dmpEntity, XWPFDocument document, ExtraFieldModel extraFieldModel) {
if (extraFieldModel == null) throw new IllegalArgumentException("ExtraFieldModel required");
XWPFParagraph extraFieldParagraph = document.createParagraph();
extraFieldParagraph.setSpacingBetween(1.0);
XWPFRun runExtraFieldLabel = extraFieldParagraph.createRun();
runExtraFieldLabel.setText(extraFieldModel.getLabel());
runExtraFieldLabel.setColor("116a78");
if (extraFieldModel.getDescription() != null && !extraFieldModel.getDescription().isEmpty()) {
XWPFRun runExtraFieldDescription = extraFieldParagraph.createRun();
runExtraFieldDescription.setText(extraFieldModel.getDescription());
runExtraFieldDescription.setColor("116a78");
}
XWPFRun runExtraFieldInput = extraFieldParagraph.createRun();
DmpBlueprintValueModel dmpBlueprintValueModel = dmpEntity.getProperties() != null && dmpEntity.getProperties().getDmpBlueprintValues() != null ? dmpEntity.getProperties().getDmpBlueprintValues().stream().filter(x -> extraFieldModel.getId().equals(x.getFieldId())).findFirst().orElse(null) : null;
if (dmpBlueprintValueModel != null && dmpBlueprintValueModel.getValue() != null) {
runExtraFieldInput.setText(dmpBlueprintValueModel.getValue());
}
runExtraFieldInput.setColor("116a78");
}
private String getDmpFileName(DmpModel dmpModel, String extension){
if (dmpModel == null) throw new IllegalArgumentException("DmpEntity required");
List<ReferenceModel> grants = this.getReferenceModelOfTypeCode(dmpModel, this.wordFileTransformerServiceProperties.getGrantReferenceCode(), null);
String fileName;
if (!grants.isEmpty() && grants.getFirst().getLabel() != null) {
fileName = "DMP_" + grants.getFirst().getLabel();
}
else {
fileName = "DMP_" + dmpModel.getLabel();
}
fileName += "_" + dmpModel.getVersion();
return fileName + extension;
}
private byte[] buildDescriptionWordDocument(DescriptionModel descriptionModel) throws IOException {
if (descriptionModel == null) throw new IllegalArgumentException("DmpEntity required");
DmpModel dmpEntity = descriptionModel.getDmp();
if (dmpEntity == null) throw new IllegalArgumentException("Dmp is invalid");
XWPFDocument document = new XWPFDocument(new FileInputStream(ResourceUtils.getFile(this.wordFileTransformerServiceProperties.getWordDescriptionTemplate())));
this.wordBuilder.fillFirstPage(dmpEntity, descriptionModel, document, true);
this.wordBuilder.fillFooter(dmpEntity, descriptionModel, document);
int powered_pos = this.wordBuilder.findPosOfPoweredBy(document);
XWPFParagraph powered_par = null;
XWPFParagraph argos_img_par = null;
if(powered_pos != -1) {
powered_par = document.getParagraphArray(powered_pos);
argos_img_par = document.getParagraphArray(powered_pos + 1);
}
this.wordBuilder.build(document, descriptionModel.getDescriptionTemplate(), descriptionModel.getProperties());
if(powered_pos != -1) {
document.getLastParagraph().setPageBreak(false);
document.createParagraph();
document.setParagraph(powered_par, document.getParagraphs().size() - 1);
document.createParagraph();
document.setParagraph(argos_img_par, document.getParagraphs().size() - 1);
document.removeBodyElement(powered_pos + 1);
document.removeBodyElement(powered_pos + 1);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
document.write(out);
byte[] bytes = out.toByteArray();
out.close();
return bytes;
}
private String getDescriptionFileName(DescriptionModel descriptionModel, String extension){
if (descriptionModel == null) throw new IllegalArgumentException("DmpEntity required");
String fileName = descriptionModel.getLabel().replaceAll("[^a-zA-Z0-9+ ]", "");
return fileName + extension;
}
}

View File

@ -0,0 +1,9 @@
package eu.eudat.file.transformer.service.wordfiletransformer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({WordFileTransformerServiceProperties.class})
public class WordFileTransformerServiceConfiguration {
}

View File

@ -0,0 +1,106 @@
package eu.eudat.file.transformer.service.wordfiletransformer;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "word-file-transformer")
public class WordFileTransformerServiceProperties {
private String wordDmpTemplate;
private String pidTemplate;
private String wordDescriptionTemplate;
private String organizationReferenceCode;
private String grantReferenceCode;
private String funderReferenceCode;
private String researcherReferenceCode;
private String licenceReferenceCode;
private String projectReferenceCode;
private String datasetReferenceCode;
private String publicationReferenceCode;
public String getOrganizationReferenceCode() {
return organizationReferenceCode;
}
public void setOrganizationReferenceCode(String organizationReferenceCode) {
this.organizationReferenceCode = organizationReferenceCode;
}
public String getGrantReferenceCode() {
return grantReferenceCode;
}
public void setGrantReferenceCode(String grantReferenceCode) {
this.grantReferenceCode = grantReferenceCode;
}
public String getFunderReferenceCode() {
return funderReferenceCode;
}
public void setFunderReferenceCode(String funderReferenceCode) {
this.funderReferenceCode = funderReferenceCode;
}
public String getResearcherReferenceCode() {
return researcherReferenceCode;
}
public void setResearcherReferenceCode(String researcherReferenceCode) {
this.researcherReferenceCode = researcherReferenceCode;
}
public String getLicenceReferenceCode() {
return licenceReferenceCode;
}
public void setLicenceReferenceCode(String licenceReferenceCode) {
this.licenceReferenceCode = licenceReferenceCode;
}
public String getProjectReferenceCode() {
return projectReferenceCode;
}
public void setProjectReferenceCode(String projectReferenceCode) {
this.projectReferenceCode = projectReferenceCode;
}
public String getDatasetReferenceCode() {
return datasetReferenceCode;
}
public void setDatasetReferenceCode(String datasetReferenceCode) {
this.datasetReferenceCode = datasetReferenceCode;
}
public String getPublicationReferenceCode() {
return publicationReferenceCode;
}
public void setPublicationReferenceCode(String publicationReferenceCode) {
this.publicationReferenceCode = publicationReferenceCode;
}
public String getWordDmpTemplate() {
return wordDmpTemplate;
}
public void setWordDmpTemplate(String wordDmpTemplate) {
this.wordDmpTemplate = wordDmpTemplate;
}
public String getPidTemplate() {
return pidTemplate;
}
public void setPidTemplate(String pidTemplate) {
this.pidTemplate = pidTemplate;
}
public String getWordDescriptionTemplate() {
return wordDescriptionTemplate;
}
public void setWordDescriptionTemplate(String wordDescriptionTemplate) {
this.wordDescriptionTemplate = wordDescriptionTemplate;
}
}

View File

@ -1,4 +1,4 @@
package eu.eudat.file.transformer.utils.word; package eu.eudat.file.transformer.service.wordfiletransformer.word;
import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlCursor;

View File

@ -0,0 +1,24 @@
package eu.eudat.file.transformer.service.wordfiletransformer.word;
import eu.eudat.commonmodels.models.description.DescriptionModel;
import eu.eudat.commonmodels.models.description.PropertyDefinitionModel;
import eu.eudat.commonmodels.models.descriptiotemplate.DescriptionTemplateModel;
import eu.eudat.commonmodels.models.dmp.DmpModel;
import eu.eudat.file.transformer.model.enums.ParagraphStyle;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import java.io.IOException;
import java.math.BigInteger;
public interface WordBuilder {
void build(XWPFDocument document, DescriptionTemplateModel descriptionTemplate, PropertyDefinitionModel propertyDefinitionModel) throws IOException;
XWPFParagraph addParagraphContent(Object content, XWPFDocument mainDocumentPart, ParagraphStyle style, BigInteger numId, int indent);
int findPosOfPoweredBy(XWPFDocument document);
void fillFirstPage(DmpModel dmpEntity, DescriptionModel descriptionModel, XWPFDocument document, boolean isDescription);
void fillFooter(DmpModel dmpEntity, DescriptionModel descriptionModel, XWPFDocument document);
}

View File

@ -1,36 +0,0 @@
package eu.eudat.file.transformer.utils.pdf;
import eu.eudat.file.transformer.model.file.FileEnvelopeInternal;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.UUID;
public class PDFUtils {
public static File convertToPDF(FileEnvelopeInternal file, String tempPath, String pdfUrl) throws IOException {
WebClient webClient = WebClient.builder().baseUrl(pdfUrl).build();
byte[] queueResult = webClient.post().uri("forms/libreoffice/convert")
.headers(httpHeaders -> httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA))
.body(BodyInserters.fromMultipartData("file", new FileSystemResource(file.getFile())))
.retrieve().bodyToMono(byte[].class).block();
File resultPdf = new File(tempPath + "/" + UUID.randomUUID() + ".pdf");
if (queueResult != null) {
try (FileOutputStream output = new FileOutputStream(resultPdf)) {
output.write(queueResult);
}
}
Files.deleteIfExists(file.getFile().toPath());
return resultPdf;
}
}

View File

@ -1,36 +0,0 @@
package eu.eudat.file.transformer.utils.pid;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.file.transformer.model.PidLink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import java.io.IOException;
import java.util.List;
public class PidLoader {
private static final Logger logger = LoggerFactory.getLogger(PidLoader.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
public static List<PidLink> loadPidLinks(String pidPath) {
try {
return objectMapper.readValue(ResourceUtils.getFile(pidPath), PidLinksWrapper.class).getPidLinks();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
}
protected class PidLinksWrapper {
private List<PidLink> pidLinks;
public List<PidLink> getPidLinks() {
return pidLinks;
}
public void setPidLinks(List<PidLink> pidLinks) {
this.pidLinks = pidLinks;
}
}
}

View File

@ -1,49 +0,0 @@
package eu.eudat.file.transformer.utils.types;
/**
* Created by ikalyvas on 2/26/2018.
*/
public enum ParagraphStyle {
TEXT(0), HEADER1(1), HEADER2(2), HEADER3(3), HEADER4(4), TITLE(5), FOOTER(6), COMMENT(7), HEADER5(8), HEADER6(9), HTML(10), IMAGE(11);
private Integer value;
private ParagraphStyle(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
public static ParagraphStyle fromInteger(Integer value) {
switch (value) {
case 0:
return TEXT;
case 1:
return HEADER1;
case 2:
return HEADER2;
case 3:
return HEADER3;
case 4:
return HEADER4;
case 5:
return TITLE;
case 6:
return FOOTER;
case 7:
return COMMENT;
case 8:
return HEADER5;
case 9:
return HEADER6;
case 10:
return HTML;
case 11:
return IMAGE;
default:
throw new RuntimeException("Unsupported ParagraphStyle Code");
}
}
}

View File

@ -1,31 +0,0 @@
package eu.eudat.file.transformer.utils.types;
/**
* Created by ikalyvas on 2/27/2018.
*/
public enum TextStyle {
ITALIC(0), BOLD(1), CAPS(2);
private Integer value;
private TextStyle(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
public static TextStyle fromInteger(Integer value) {
switch (value) {
case 0:
return ITALIC;
case 1:
return BOLD;
case 2:
return CAPS;
default:
throw new RuntimeException("Unsupported TextStyle Code");
}
}
}

View File

@ -1,63 +0,0 @@
package eu.eudat.file.transformer.utils.word;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.UUID;
public class XWPFHtmlDocument extends POIXMLDocumentPart {
private String html;
private String id;
public XWPFHtmlDocument(PackagePart pkg, String id) {
super(pkg);
this.html = "<!DOCTYPE html><html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"><style></style><title>HTML import</title></head><body><p></p></body>";
this.id = id;
}
public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = this.html.replace("<p></p>", html);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
protected void commit() throws IOException {
PackagePart packagePart = getPackagePart();
OutputStream outputStream = packagePart.getOutputStream();
Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(html);
writer.close();
outputStream.close();
}
public static XWPFHtmlDocument addHtmlDocument(XWPFDocument document) throws InvalidFormatException {
OPCPackage oPCPackage = document.getPackage();
String id = UUID.randomUUID().toString();
PackagePartName partName = PackagingURIHelper.createPartName("/word/" + id + ".html");
PackagePart part = oPCPackage.createPart(partName, "text/html");
XWPFHtmlDocument xWPFHtmlDocument = new XWPFHtmlDocument(part, id);
document.addRelation(xWPFHtmlDocument.getId(), new XWPFHtmlRelation(), xWPFHtmlDocument);
return xWPFHtmlDocument;
}
}

View File

@ -1,11 +0,0 @@
package eu.eudat.file.transformer.utils.word;
import org.apache.poi.ooxml.POIXMLRelation;
public class XWPFHtmlRelation extends POIXMLRelation {
public XWPFHtmlRelation() {
super("text/html",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk",
"/word/htmlDoc#.html");
}
}

View File

@ -1,18 +1,16 @@
package eu.eudat.file.transformer.controller; package eu.eudat.file.transformer.controller;
import eu.eudat.commonmodels.models.FileEnvelopeModel;
import eu.eudat.commonmodels.models.description.DescriptionModel;
import eu.eudat.commonmodels.models.dmp.DmpModel;
import eu.eudat.file.transformer.interfaces.FileTransformerClient; import eu.eudat.file.transformer.interfaces.FileTransformerClient;
import eu.eudat.file.transformer.interfaces.FileTransformerConfiguration; import eu.eudat.file.transformer.interfaces.FileTransformerConfiguration;
import eu.eudat.file.transformer.models.description.DescriptionFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpFileTransformerModel;
import eu.eudat.file.transformer.models.misc.FileEnvelope;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RestController
@RequestMapping("/api/file") @RequestMapping("/api/file-transformer")
public class FileTransformerController { public class FileTransformerController implements eu.eudat.file.transformer.interfaces.FileTransformerController {
private final FileTransformerClient fileTransformerExecutor; private final FileTransformerClient fileTransformerExecutor;
@ -21,27 +19,22 @@ public class FileTransformerController {
this.fileTransformerExecutor = fileTransformerExecutor; this.fileTransformerExecutor = fileTransformerExecutor;
} }
@PostMapping("/export/dmp") public FileEnvelopeModel exportDmp(@RequestBody DmpModel dmpDepositModel, @RequestParam(value = "format",required = false)String format) throws Exception {
public FileEnvelope exportDmp(@RequestBody DmpFileTransformerModel dmpDepositModel) throws Exception { return fileTransformerExecutor.exportDmp(dmpDepositModel, format);
return fileTransformerExecutor.exportDmp(dmpDepositModel);
} }
@PostMapping("/export/description") public FileEnvelopeModel exportDescription(@RequestBody DescriptionModel descriptionModel, @RequestParam(value = "format",required = false)String format) throws Exception {
public FileEnvelope exportDescription(@RequestBody DescriptionFileTransformerModel descriptionFileTransformerModel, @RequestParam(value = "format",required = false)String format, @RequestParam(value = "descriptionId",required = false) String descriptionId) throws Exception { return fileTransformerExecutor.exportDescription(descriptionModel, format);
return fileTransformerExecutor.exportDescription(descriptionFileTransformerModel, format);
} }
@PostMapping("/import/dmp") public DmpModel importFileToDmp(@RequestBody FileEnvelopeModel fileEnvelope) {
public DmpFileTransformerModel importFileToDmp(@RequestBody FileEnvelope fileEnvelope) {
return fileTransformerExecutor.importDmp(fileEnvelope); return fileTransformerExecutor.importDmp(fileEnvelope);
} }
@PostMapping("/import/description") public DescriptionModel importFileToDescription(@RequestBody FileEnvelopeModel fileEnvelope) {
public DescriptionFileTransformerModel importFileToDescription(@RequestBody FileEnvelope fileEnvelope) {
return fileTransformerExecutor.importDescription(fileEnvelope); return fileTransformerExecutor.importDescription(fileEnvelope);
} }
@GetMapping("/formats")
public FileTransformerConfiguration getSupportedFormats() { public FileTransformerConfiguration getSupportedFormats() {
return fileTransformerExecutor.getConfiguration(); return fileTransformerExecutor.getConfiguration();
} }

View File

@ -7,5 +7,6 @@ spring:
optional:classpath:config/storage.yml[.yml], optional:classpath:config/storage-${spring.profiles.active}.yml[.yml], optional:file:../config/storage-${spring.profiles.active}.yml[.yml], optional:classpath:config/storage.yml[.yml], optional:classpath:config/storage-${spring.profiles.active}.yml[.yml], optional:file:../config/storage-${spring.profiles.active}.yml[.yml],
optional:classpath:config/security.yml[.yml], optional:classpath:config/security-${spring.profiles.active}.yml[.yml], optional:file:../config/security-${spring.profiles.active}.yml[.yml], optional:classpath:config/security.yml[.yml], optional:classpath:config/security-${spring.profiles.active}.yml[.yml], optional:file:../config/security-${spring.profiles.active}.yml[.yml],
optional:classpath:config/cache.yml[.yml], optional:classpath:config/cache-${spring.profiles.active}.yml[.yml], optional:file:../config/cache-${spring.profiles.active}.yml[.yml], optional:classpath:config/cache.yml[.yml], optional:classpath:config/cache-${spring.profiles.active}.yml[.yml], optional:file:../config/cache-${spring.profiles.active}.yml[.yml],
optional:classpath:config/word-file-transormer.yml[.yml], optional:classpath:config/word-file-transormer-${spring.profiles.active}.yml[.yml], optional:file:../config/word-file-transormer-${spring.profiles.active}.yml[.yml],
optional:classpath:config/pdf.yml[.yml], optional:classpath:config/pdf-${spring.profiles.active}.yml[.yml], optional:file:../config/pdf-${spring.profiles.active}.yml[.yml] optional:classpath:config/pdf.yml[.yml], optional:classpath:config/pdf-${spring.profiles.active}.yml[.yml], optional:file:../config/pdf-${spring.profiles.active}.yml[.yml]

View File

@ -5,16 +5,11 @@ web:
allowed-endpoints: [ health ] allowed-endpoints: [ health ]
idp: idp:
api-key: api-key:
enabled: true enabled: false
authorization-header: Authorization
client-id: ${IDP_APIKEY_CLIENT_ID:}
client-secret: ${IDP_APIKEY_CLIENT_SECRET:}
scope: ${IDP_APIKEY_SCOPE:}
resource: resource:
token-type: JWT #| opaque token-type: JWT #| opaque
opaque:
client-id: ${IDP_OPAQUE_CLIENT_ID:}
client-secret: ${IDP_OPAQUE_CLIENT_SECRET:}
jwt: jwt:
claims: [ role, x-role ] claims: [ role, x-role ]
issuer-uri: ${IDP_ISSUER_URI:} issuer-uri: ${IDP_ISSUER_URI:}
audiences: [ "dmp_zenodo_bridge" ]
validIssuer: ${IDP_ISSUER_URI:}

View File

@ -1,8 +1,4 @@
file: file:
template:
word-template: classpath:documents/h2020.docx
pid-template: classpath:pidLinks.json
word-description-template: classpath:documents/h2020_dataset.docx
storage: storage:
temp: ${TEMP_PATH} temp: ${STORAGE_PATH}/tmp
transient-path: ${TRANSIENT_PATH} transient-path: ${STORAGE_PATH}/shared

View File

@ -0,0 +1,12 @@
word-file-transformer:
word-dmp-template: classpath:documents/h2020.docx
pid-template: classpath:pidLinks.json
word-description-template: classpath:documents/h2020_dataset.docx
organizationReferenceCode: "organisations"
grantReferenceCode: "grants"
funderReferenceCode: "funders"
researcherReferenceCode: "researchers"
licenceReferenceCode: "licenses"
projectReferenceCode: "projects"
datasetReferenceCode: "datasets"
publicationReferenceCode: "publications"