635 lines
38 KiB
Java
635 lines
38 KiB
Java
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);
|
|
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;
|
|
}
|
|
}
|