file-transformer-docx/core/src/main/java/eu/eudat/file/transformer/executor/WordFileTransformer.java

624 lines
37 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.entities.user.composite.PagedDatasetProfile;
import eu.eudat.file.transformer.enums.*;
import eu.eudat.file.transformer.enums.ReferenceType;
import eu.eudat.file.transformer.model.*;
import eu.eudat.file.transformer.model.dmpblueprintdefinition.ExtraField;
import eu.eudat.file.transformer.model.dmpblueprintdefinition.Field;
import eu.eudat.file.transformer.model.dmpblueprintdefinition.Section;
import eu.eudat.file.transformer.model.dmpblueprintdefinition.SystemField;
import eu.eudat.file.transformer.model.enums.FileFormats;
import eu.eudat.file.transformer.model.file.FileEnvelope;
import eu.eudat.file.transformer.model.file.FileEnvelopeInternal;
import eu.eudat.file.transformer.services.visibility.VisibilityRuleService;
import eu.eudat.file.transformer.services.visibility.VisibilityRuleServiceImpl;
import eu.eudat.file.transformer.utils.descriptionTemplate.DescriptionTemplateService;
import eu.eudat.file.transformer.utils.pdf.PDFUtils;
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 FileTransformerExecutor {
private final static Logger logger = LoggerFactory.getLogger(WordFileTransformer.class);
private final FilePathProperties fileTemplateProperties;
private final FileStorageProperties fileStorageProperties;
private final PdfProperties pdfProperties;
private final ApplicationContext applicationContext;
private final ObjectMapper objectMapper;
@Autowired
public WordFileTransformer(FilePathProperties fileTemplateProperties, FileStorageProperties fileStorageProperties, PdfProperties pdfProperties, ApplicationContext applicationContext) {
this.fileTemplateProperties = fileTemplateProperties;
this.fileStorageProperties = fileStorageProperties;
this.pdfProperties = pdfProperties;
this.applicationContext = applicationContext;
this.objectMapper = new ObjectMapper();
}
@Override
public FileEnvelope exportDmp(DmpFileTransformerModel dmp, ExtraPropertiesModel properties) throws IOException {
FileFormats fileFormat = FileFormats.of(properties.getFormat());
return switch (fileFormat) {
case DOCX -> getWordDocument(dmp);
case PDF -> {
FileEnvelopeInternal wordFile = getWordDocument(dmp, true);
yield getPdfDocument(wordFile);
}
};
}
@Override
public FileEnvelope exportDescription(DescriptionFileTransformerModel descriptionFileTransformerModel, ExtraPropertiesModel properties) throws InvalidApplicationException, IOException {
FileFormats fileFormat = FileFormats.of(properties.getFormat());
return switch (fileFormat) {
case DOCX -> getDescriptionWordDocument(descriptionFileTransformerModel);
case PDF -> {
FileEnvelopeInternal wordFile = getDescriptionWordDocumentInternal(descriptionFileTransformerModel);
yield getPdfDocument(wordFile);
}
};
}
@Override
public DmpFileTransformerModel importFileToDmp(FileEnvelope envelope) {
//Nothing to do here
return null;
}
@Override
public DescriptionFileTransformerModel importFileToDescription(FileEnvelope envelope) {
return null;
}
@Override
public List<String> getSupportedFileFormats() {
return Arrays.stream(FileFormats.values()).map(FileFormats::getValue).toList();
}
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(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(fis.readAllBytes());
}
return result;
}
private FileEnvelopeInternal getWordDocument(DmpFileTransformerModel dmpEntity, Boolean versioned) throws IOException {
WordBuilder wordBuilder = new WordBuilder(fileTemplateProperties, fileStorageProperties);
VisibilityRuleService visibilityRuleService = new VisibilityRuleServiceImpl();
XWPFDocument document = new XWPFDocument(new FileInputStream(ResourceUtils.getFile(fileTemplateProperties.getWordTemplate())));
wordBuilder.fillFirstPage(dmpEntity, null, document, false);
List<Reference> funders = dmpEntity.getDmpReferences().stream().filter(dmpReference -> dmpReference.getReference().getType().equals(ReferenceType.Funder) && dmpReference.getReference().getIsActive().equals(IsActive.Active)).map(DmpReference::getReference).toList();
List<Reference> grants = dmpEntity.getDmpReferences().stream().filter(referenceFileModel -> referenceFileModel.getReference().getType().equals(ReferenceType.Grants) && referenceFileModel.getReference().getIsActive().equals(IsActive.Active)).map(DmpReference::getReference).toList();
List<Reference> researchers = dmpEntity.getDmpReferences().stream().filter(referenceFileModel -> referenceFileModel.getReference().getType().equals(ReferenceType.Researcher)).map(DmpReference::getReference).toList();
List<Reference> organizations = dmpEntity.getDmpReferences().stream().filter(referenceFileModel -> referenceFileModel.getReference().getType().equals(ReferenceType.Organizations)).map(DmpReference::getReference).toList();
List<Reference> projects = dmpEntity.getDmpReferences().stream().filter(referenceFileModel -> referenceFileModel.getReference().getType().equals(ReferenceType.Project) && referenceFileModel.getReference().getIsActive().equals(IsActive.Active)).map(DmpReference::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();
DmpBlueprint dmpBlueprint = dmpEntity.getBlueprint();
for(Section section: dmpBlueprint.getDefinition().getSections()){
wordBuilder.addParagraphContent("Section " + section.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(section.getLabel());
runSectionTitleText.setColor("116a78");
XWPFParagraph sectionDescriptionParagraph = document.createParagraph();
XWPFRun runSectionDescription = sectionDescriptionParagraph.createRun();
runSectionDescription.setText("Description: ");
runSectionDescription.setColor("000000");
XWPFRun runSectionDescriptionText = sectionDescriptionParagraph.createRun();
runSectionDescriptionText.setText(section.getDescription());
runSectionDescriptionText.setColor("116a78");
wordBuilder.addParagraphContent("Section Fields", document, ParagraphStyle.HEADER2, BigInteger.ZERO, 0);
section.getFields().sort(Comparator.comparingInt(Field::getOrdinal));
for(Field field: section.getFields()){
if(field.getCategory() == DmpBlueprintFieldCategory.System){
SystemField systemField = (SystemField) field;
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(Reference researcher: researchers){
XWPFRun runResearcher = systemFieldInput.createRun();
runResearcher.setText("" + researcher.getLabel());
runResearcher.setColor("116a78");
}
break;
case Organizations:
for(Reference 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(field.getCategory() == DmpBlueprintFieldCategory.Extra){
ExtraField extraField = (ExtraField) field;
XWPFParagraph extraFieldParagraph = document.createParagraph();
extraFieldParagraph.setSpacingBetween(1.0);
XWPFRun runExtraFieldLabel = extraFieldParagraph.createRun();
runExtraFieldLabel.setText(extraField.getLabel());
runExtraFieldLabel.setColor("116a78");
if(extraField.getDescription() != null && !extraField.getDescription().isEmpty()){
XWPFRun runExtraFieldDescription = extraFieldParagraph.createRun();
runExtraFieldDescription.setText(extraField.getDescription());
runExtraFieldDescription.setColor("116a78");
}
XWPFRun runExtraFieldInput = extraFieldParagraph.createRun();
Map dmpProperties = objectMapper.readValue(dmpEntity.getProperties(), HashMap.class);
if (dmpProperties.containsKey(field.getId()) && dmpProperties.get(field.getId()) != null) {
runExtraFieldInput.setText((String) dmpProperties.get(field.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 -> item.getIsActive() != IsActive.Inactive)
.filter(item -> !isPublic && !isFinalized || item.getStatus() == DescriptionStatus.Finalized)
.filter(item -> item.getDmpDescriptionTemplate().getSectionId().equals(section.getId())) //TODO
.sorted(Comparator.comparing(DescriptionFileTransformerModel::getCreatedAt)).toList();
List<eu.eudat.file.transformer.model.DescriptionTemplate> descriptionTemplates = descriptions.stream().map(DescriptionFileTransformerModel::getDescriptionTemplate).toList();
if(!descriptionTemplates.isEmpty()){
wordBuilder.addParagraphContent("Section descriptions", document, ParagraphStyle.HEADER2, BigInteger.ZERO, 0);
wordBuilder.addParagraphContent("Description Templates", document, ParagraphStyle.HEADER4, BigInteger.ZERO, 0);
for(DescriptionTemplate descriptionTemplateEntity : descriptionTemplates){
XWPFParagraph templateParagraph = document.createParagraph();
XWPFRun runTemplateLabel = templateParagraph.createRun();
runTemplateLabel.setText("" + descriptionTemplateEntity.getLabel());
runTemplateLabel.setColor("116a78");
}
descriptions
.forEach(datasetEntity -> {
eu.eudat.file.transformer.model.DescriptionTemplate descriptionTemplateFileModel = 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(descriptionTemplateFileModel.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(descriptionTemplateFileModel.getLabel());
runDatasetDescription.setColor("116a78");
//wordBuilder.addParagraphContent(datasetEntity.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO, 0);
document.createParagraph();
try {
PagedDatasetProfile pagedDatasetProfile = DescriptionTemplateService.getPagedProfile(datasetEntity);
visibilityRuleService.setProperties(datasetEntity.getProperties());
visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules());
//pagedDatasetProfile = this.objectMapper.readValue(this.objectMapper.writeValueAsString(pagedDatasetProfile), eu.eudat.file.transformer.model.descriptiontemplatedefinition.Definition.class);
wordBuilder.build(document, pagedDatasetProfile, visibilityRuleService);
} 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(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())));
VisibilityRuleService visibilityRuleService = new VisibilityRuleServiceImpl();
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);
PagedDatasetProfile pagedDatasetProfile = DescriptionTemplateService.getPagedProfile(descriptionFileTransformerModelEntityEntity);
visibilityRuleService.setProperties(descriptionFileTransformerModelEntityEntity.getProperties());
visibilityRuleService.buildVisibilityContext(pagedDatasetProfile.getRules());
wordBuilder.build(document, pagedDatasetProfile, visibilityRuleService);
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;
}
}