builder changes

This commit is contained in:
Efstratios Giannopoulos 2024-03-22 18:32:14 +02:00
parent f69fde95de
commit 519e15bc89
5 changed files with 314 additions and 223 deletions

View File

@ -2,6 +2,7 @@ package eu.eudat.file.transformer.service.wordfiletransformer;
import eu.eudat.commonmodels.enums.DescriptionStatus;
import eu.eudat.commonmodels.enums.DmpAccessType;
import eu.eudat.commonmodels.enums.DmpBlueprintSystemFieldType;
import eu.eudat.commonmodels.enums.DmpStatus;
import eu.eudat.commonmodels.models.dmp.DmpBlueprintValueModel;
import eu.eudat.commonmodels.models.dmp.DmpModel;
@ -189,24 +190,8 @@ public class WordFileTransformerService implements FileTransformerClient {
}
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(sectionModel.getOrdinal() + ". " + sectionModel.getLabel(), document, ParagraphStyle.HEADER1, BigInteger.ZERO, 0);
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()) {
@ -235,14 +220,13 @@ public class WordFileTransformerService implements FileTransformerClient {
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");
}
wordBuilder.addParagraphContent("Descriptions", document, ParagraphStyle.HEADER2, 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);
@ -260,40 +244,32 @@ public class WordFileTransformerService implements FileTransformerClient {
datasetDescriptionParagraph.setStyle("Heading4");
datasetDescriptionParagraph.setSpacingBetween(1.5);
XWPFRun datasetDescriptionRun = datasetDescriptionParagraph.createRun();
datasetDescriptionRun.setText("Dataset Description");
datasetDescriptionRun.setText(descriptionModel.getLabel());
datasetDescriptionRun.setFontSize(15);
XWPFParagraph descriptionParagraph = document.createParagraph();
wordBuilder.addParagraphContent(descriptionModel.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO, 0);
// 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.setText("Type: ");
runDatasetDescription1.setColor("000000");
XWPFRun runDatasetDescription = datasetDescParagraph.createRun();
runDatasetDescription.setText(descriptionTemplateModelFileModel != null ? descriptionTemplateModelFileModel.getLabel() : "");
//runDatasetDescription.setText(descriptionTemplateModelFileModel != null && descriptionTemplateModelFileModel.getType() != null ? descriptionTemplateModelFileModel.getType().getName() : ""); //TODO
runDatasetDescription.setColor("116a78");
document.createParagraph();
try {
this.wordBuilder.build(document, descriptionModel.getDescriptionTemplate(), descriptionModel.getProperties(), new VisibilityServiceImpl(descriptionModel.getVisibilityStates()));
} catch (IOException e) {
@ -314,7 +290,6 @@ public class WordFileTransformerService implements FileTransformerClient {
}
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());
@ -327,78 +302,59 @@ public class WordFileTransformerService implements FileTransformerClient {
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");
XWPFParagraph systemFieldParagraph = document.createParagraph();
XWPFRun runSyStemFieldTitle = systemFieldParagraph.createRun();
runSyStemFieldTitle.setText(referenceField.getLabel() + ": ");
runSyStemFieldTitle.setColor("000000");
List<ReferenceModel> referenceModels = this.getReferenceModelOfTypeCode(dmpEntity, referenceField.getReferenceType().getCode(), referenceField.getId());
for (ReferenceModel reference : referenceModels) {
XWPFRun runResearcher = systemFieldInput.createRun();
runResearcher.setText("" + reference.getLabel());
XWPFRun runResearcher = systemFieldParagraph.createRun();
if (this.wordFileTransformerServiceProperties.getResearcherReferenceCode().equalsIgnoreCase(referenceField.getReferenceType().getCode()) ||
this.wordFileTransformerServiceProperties.getOrganizationReferenceCode().equalsIgnoreCase(referenceField.getReferenceType().getCode())
) runResearcher.addBreak();
if (this.wordFileTransformerServiceProperties.getLicenceReferenceCode().equalsIgnoreCase(referenceField.getReferenceType().getCode())) runResearcher.setText(reference.getReference());
else 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");
if (DmpBlueprintSystemFieldType.Language.equals(systemField.getSystemFieldType())) return;
XWPFParagraph systemFieldParagraph = document.createParagraph();
XWPFRun runSyStemFieldTitle = systemFieldParagraph.createRun();
runSyStemFieldTitle.setText(systemField.getLabel() + ": ");
runSyStemFieldTitle.setColor("000000");
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();
XWPFRun runTitle = systemFieldParagraph.createRun();
runTitle.setText(dmpEntity.getLabel());
runTitle.setColor("116a78");
break;
case Description:
XWPFRun runDescription = systemFieldInput.createRun();
runDescription.setText(dmpEntity.getDescription());
runDescription.setColor("116a78");
wordBuilder.addParagraphContent(dmpEntity.getDescription(), document, ParagraphStyle.HTML, BigInteger.ZERO, 0);
break;
case AccessRights:
if (dmpEntity.getAccessType() != null) {
XWPFRun runAccessRights = systemFieldInput.createRun();
XWPFRun runAccessRights = systemFieldParagraph.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());
XWPFRun runContact = systemFieldParagraph.createRun();
runContact.setText(dmpEntity.getCreator() == null ? "" : dmpEntity.getCreator().getName());
runContact.setColor("116a78");
}
break;
@ -411,24 +367,31 @@ public class WordFileTransformerService implements FileTransformerClient {
document.createParagraph();
}
private void buildDmpSectionExtraField(DmpModel dmpEntity, XWPFDocument document, ExtraFieldModel extraFieldModel) {
private void buildDmpSectionExtraField(DmpModel dmpEntity, XWPFDocument document, ExtraFieldModel extraFieldModel) throws InvalidApplicationException {
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");
}
runExtraFieldLabel.setText(extraFieldModel.getLabel() + ": ");
runExtraFieldLabel.setColor("000000");
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());
switch (extraFieldModel.getDataType()) {
case RichTex:
wordBuilder.addParagraphContent(dmpBlueprintValueModel.getValue(), document, ParagraphStyle.HTML, BigInteger.ZERO, 0);
break;
case Number:
case Text:
case Date:
runExtraFieldInput.setText(dmpBlueprintValueModel.getValue());
runExtraFieldInput.setColor("116a78");
break;
default:
throw new InvalidApplicationException("Invalid type " + extraFieldModel.getDataType());
}
}
runExtraFieldInput.setColor("116a78");
}
private String getDmpFileName(DmpModel dmpModel, String extension){

View File

@ -22,12 +22,11 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.NodeTraversor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLvl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STNumberFormat;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@ -45,6 +44,7 @@ import java.math.BigInteger;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import static org.apache.poi.xwpf.usermodel.Document.*;
@ -301,82 +301,103 @@ public class WordBuilderImpl implements WordBuilder {
}
}
}
private Boolean createFieldSetFields(List<FieldSetModel> fieldSetModels, PropertyDefinitionModel propertyDefinitionModel, XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, Integer page, String section, VisibilityService visibilityService) {
if (createListing) this.addListing(indent, true, true);
private Boolean createFieldSetFields(List<FieldSetModel> fieldSetModels, PropertyDefinitionModel propertyDefinitionModel,XWPFDocument mainDocumentPart, Integer indent, Boolean createListing, Integer page, String section, VisibilityService visibilityService) {
if (createListing) this.addListing(indent,true, true);
boolean hasValue = false;
boolean returnedValue = false;
for (FieldSetModel fieldSetModel : fieldSetModels) {
if(!visibilityService.isVisible(fieldSetModel.getId(), fieldSetModel.getOrdinal())) continue;
for (FieldSetModel fieldSetModel: fieldSetModels) {
PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel = propertyDefinitionModel.getFieldSets().getOrDefault(fieldSetModel.getId(), null);
List<PropertyDefinitionFieldSetItemModel> propertyDefinitionFieldSetItemModels = propertyDefinitionFieldSetModel != null && propertyDefinitionFieldSetModel.getItems() != null ? propertyDefinitionFieldSetModel.getItems() : new ArrayList<>();
propertyDefinitionFieldSetItemModels = propertyDefinitionFieldSetItemModels.stream().sorted(Comparator.comparingInt(PropertyDefinitionFieldSetItemModel::getOrdinal)).toList();
if (propertyDefinitionFieldSetItemModels.stream().anyMatch(x-> visibilityService.isVisible(fieldSetModel.getId(), x.getOrdinal())) ) {
char c = 'a';
int multiplicityItems = 0;
boolean hasMultiplicityItems = false;
int paragraphPos = -1;
if (fieldSetModel.getTitle() != null && !fieldSetModel.getTitle().isEmpty() && !createListing) {
XWPFParagraph paragraph = addParagraphContent(page + "." + section + "." + (fieldSetModel.getOrdinal() + 1) + " " + fieldSetModel.getTitle(), mainDocumentPart, ParagraphStyle.HEADER6, numId, indent);
paragraphPos = mainDocumentPart.getPosOfParagraph(paragraph);
}
if (!propertyDefinitionFieldSetItemModels.isEmpty()) {
if (fieldSetModel.getMultiplicity() != null) {
if (fieldSetModel.getMultiplicity().getTableView()) {
XWPFTable tbl = mainDocumentPart.createTable();
int numOfRows = 0;
tbl.setTableAlignment(TableRowAlign.CENTER);
mainDocumentPart.createParagraph();
createHeadersInTable(fieldSetModel.getFields(), tbl, visibilityService);
numOfRows = tbl.getRows().size();
for (PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel : propertyDefinitionFieldSetItemModels) {
XWPFTableRow row = tbl.createRow();
hasValue = createFieldsInTable(fieldSetModel, propertyDefinitionFieldSetItemModel, row, indent, createListing, false, numOfRows, visibilityService);
numOfRows++;
if (hasValue) returnedValue = true;
char c = 'a';
int multiplicityItems = 0;
boolean hasMultiplicityItems = false;
int paragraphPos = -1;
int paragraphPosInner = -1;
if (fieldSetModel.getTitle() != null && !fieldSetModel.getTitle().isEmpty() && !createListing) {
XWPFParagraph paragraph = addParagraphContent(page + "." + section + "." + (fieldSetModel.getOrdinal() +1) + " " + fieldSetModel.getTitle(), mainDocumentPart, ParagraphStyle.HEADER6, numId, indent);
// CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
// number.setVal(BigInteger.valueOf(indent));
paragraphPos = mainDocumentPart.getPosOfParagraph(paragraph);
if(!fieldSetModel.getMultiplicity().getTableView() && propertyDefinitionFieldSetItemModels.size() > 1){
XWPFParagraph paragraphInner = addParagraphContent(c + ". ", mainDocumentPart, ParagraphStyle.TEXT, numId, indent);
paragraphPosInner = mainDocumentPart.getPosOfParagraph(paragraphInner);
hasMultiplicityItems = true;
multiplicityItems++;
}
}
XWPFTable tbl = null;
XWPFTableRow row = null;
int numOfRows = 0;
if(fieldSetModel.getMultiplicity().getTableView()) {
tbl = mainDocumentPart.createTable();
tbl.setTableAlignment(TableRowAlign.CENTER);
mainDocumentPart.createParagraph();
createHeadersInTable(fieldSetModel.getFields(), tbl, visibilityService);
numOfRows = tbl.getRows().size();
row = tbl.createRow();
}
if(fieldSetModel.getMultiplicity().getTableView()) {
hasValue = createFieldsInTable(fieldSetModel, propertyDefinitionFieldSetItemModels.getFirst(), row, indent, createListing, hasMultiplicityItems, numOfRows, visibilityService);
numOfRows++;
} else {
hasValue = createFields(fieldSetModel, propertyDefinitionFieldSetItemModels.getFirst(), mainDocumentPart, indent, createListing, hasMultiplicityItems, visibilityService);
}
if(hasValue){
returnedValue = true;
} else if(paragraphPosInner > -1){
mainDocumentPart.removeBodyElement(paragraphPosInner);
c--;
multiplicityItems--;
}
if (propertyDefinitionFieldSetItemModels.size() > 1) {
for (PropertyDefinitionFieldSetItemModel multiplicityFieldset : propertyDefinitionFieldSetItemModels.stream().skip(1).toList()) {
paragraphPosInner = -1;
if(!fieldSetModel.getMultiplicity().getTableView() && !createListing){
c++;
// addParagraphContent(c + ". ", mainDocumentPart, ParagraphStyle.HEADER6, numId);
XWPFParagraph paragraphInner = addParagraphContent(c + ". ", mainDocumentPart, ParagraphStyle.TEXT, numId, indent);
paragraphPosInner = mainDocumentPart.getPosOfParagraph(paragraphInner);
hasMultiplicityItems = true;
multiplicityItems++;
}
} else {
for (PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel : propertyDefinitionFieldSetItemModels) {
int paragraphPosInner = -1;
if (!fieldSetModel.getMultiplicity().getTableView() && !createListing) {
XWPFParagraph paragraphInner = addParagraphContent(c + ". ", mainDocumentPart, ParagraphStyle.TEXT, numId, indent);
c++;
paragraphPosInner = mainDocumentPart.getPosOfParagraph(paragraphInner);
hasMultiplicityItems = true;
multiplicityItems++;
}
hasValue = createFields(fieldSetModel, propertyDefinitionFieldSetItemModel, mainDocumentPart, indent, createListing, hasMultiplicityItems, visibilityService);
if (hasValue) {
returnedValue = true;
} else if (paragraphPosInner > -1) {
mainDocumentPart.removeBodyElement(paragraphPosInner);
c--;
multiplicityItems--;
}
// hasValue = createFields(multiplicityFieldset.getFields(), mainDocumentPart, 3, createListing, visibilityRuleService, hasMultiplicityItems);
boolean hasValueInner = false;
if(fieldSetModel.getMultiplicity().getTableView()) {
row = tbl.createRow();
hasValueInner = createFieldsInTable(fieldSetModel, multiplicityFieldset, row, indent, createListing, hasMultiplicityItems, numOfRows, visibilityService);
numOfRows++;
} else {
hasValueInner = createFields(fieldSetModel,multiplicityFieldset, mainDocumentPart, indent, createListing, hasMultiplicityItems, visibilityService);
}
// if(hasValue){
if(hasValueInner){
hasValue = true;
returnedValue = true;
} else if(paragraphPosInner > -1){
mainDocumentPart.removeBodyElement(paragraphPosInner);
c--;
multiplicityItems--;
}
}
if (multiplicityItems == 1) {
String text = mainDocumentPart.getLastParagraph().getRuns().getFirst().getText(0);
if (text.equals("a. ")) {
if(multiplicityItems == 1) {
String text = mainDocumentPart.getLastParagraph().getRuns().get(0).getText(0);
if(text.equals("a. ")) {
mainDocumentPart.getLastParagraph().removeRun(0);
}
}
} else {
hasValue = createFields(fieldSetModel, propertyDefinitionFieldSetItemModels.getFirst(), mainDocumentPart, indent, createListing, false, visibilityService);
}
if (hasValue && propertyDefinitionFieldSetItemModels.getFirst().getComment() != null && !propertyDefinitionFieldSetItemModels.getFirst().getComment().isEmpty() && !createListing) {
addParagraphContent("<i>Comment:</i>\n"+ propertyDefinitionFieldSetItemModels.getFirst().getComment(), mainDocumentPart, ParagraphStyle.HTML, numId, indent);
addParagraphContent("<i>Comment:</i>\n"+ propertyDefinitionFieldSetItemModels.getFirst().getComment(), mainDocumentPart, ParagraphStyle.HTML, numId, indent);
}
if (!hasValue && paragraphPos > -1) {
mainDocumentPart.removeBodyElement(paragraphPos);
}
}
if (!hasValue && paragraphPos > -1) {
mainDocumentPart.removeBodyElement(paragraphPos);
}
}
@ -389,9 +410,9 @@ public class WordBuilderImpl implements WordBuilder {
int index = 0;
XWPFTableRow row = table.getRow(0);
for (FieldModel field: tempFields) {
if (field.getIncludeInExport() && visibilityService.isVisible(field.getId(), field.getOrdinal())) {
if (field.getIncludeInExport() && visibilityService.isVisible(field.getId(), 0)) {
XWPFTableCell cell;
if (index == 0) {
if(index == 0) {
cell = row.getCell(0);
} else {
cell = row.createCell();
@ -425,7 +446,7 @@ public class WordBuilderImpl implements WordBuilder {
boolean hasValue = false;
List<FieldModel> tempFields = fieldSetModel.getFields().stream().sorted(Comparator.comparingInt(FieldModel::getOrdinal)).toList();
for (FieldModel field: tempFields) {
if (field.getIncludeInExport() && visibilityService.isVisible(field.getId(), fieldSetModel.getOrdinal())) {
if (field.getIncludeInExport() && visibilityService.isVisible(field.getId(), propertyDefinitionFieldSetItemModel.getOrdinal())) {
if (!createListing) {
try {
eu.eudat.commonmodels.models.description.FieldModel fieldValueModel = propertyDefinitionFieldSetItemModel.getFields().getOrDefault(field.getId(), null);
@ -576,7 +597,7 @@ public class WordBuilderImpl implements WordBuilder {
boolean hasValue = false;
List<FieldModel> tempFields = fieldSetModel.getFields().stream().sorted(Comparator.comparingInt(FieldModel::getOrdinal)).toList();
for (FieldModel field: tempFields) {
if (field.getIncludeInExport() && visibilityService.isVisible(field.getId(), fieldSetModel.getOrdinal())) {
if (field.getIncludeInExport() && visibilityService.isVisible(field.getId(), propertyDefinitionFieldSetItemModel.getOrdinal())) {
if (!createListing) {
try {
eu.eudat.commonmodels.models.description.FieldModel fieldValueModel = propertyDefinitionFieldSetItemModel.getFields().getOrDefault(field.getId(), null);
@ -604,7 +625,7 @@ public class WordBuilderImpl implements WordBuilder {
} else if (fieldValueModel != null) {
this.indent = indent;
String format = this.formatter(field, fieldValueModel);
Boolean isMultiAutoComplete = false;
boolean isMultiAutoComplete = false;
boolean isResearcher = false;
boolean isOrganization = false;
boolean isExternalDataset = false;
@ -885,94 +906,79 @@ public class WordBuilderImpl implements WordBuilder {
int parPos = 0;
int descrParPos = -1;
XWPFParagraph descrPar = null;
List<ReferenceModel> grants = this.getReferenceModelOfTypeCode(dmpEntity, this.wordFileTransformerServiceProperties.getGrantReferenceCode());
List<ReferenceModel> researchers = this.getReferenceModelOfTypeCode(dmpEntity, this.wordFileTransformerServiceProperties.getResearcherReferenceCode());
List<ReferenceModel> organizations = this.getReferenceModelOfTypeCode(dmpEntity, this.wordFileTransformerServiceProperties.getOrganizationReferenceCode());
List<ReferenceModel> funders = this.getReferenceModelOfTypeCode(dmpEntity, this.wordFileTransformerServiceProperties.getFunderReferenceCode());
for(XWPFParagraph p: document.getParagraphs()){
List<XWPFRun> runs = p.getRuns();
if(runs != null){
for(XWPFRun r : runs){
String text = r.getText(0);
if(text != null){
if(text.contains("{ARGOS.DMP.TITLE}")) {
text = text.replace("{ARGOS.DMP.TITLE}", dmpEntity.getLabel());
r.setText(text, 0);
} else if(text.contains("{ARGOS.DMP.VERSION}")) {
text = text.replace("{ARGOS.DMP.VERSION}", "Version " + dmpEntity.getVersion());
r.setText(text, 0);
} else if(descriptionModel != null && text.contains("{ARGOS.DATASET.TITLE}")) {
text = text.replace("{ARGOS.DATASET.TITLE}", descriptionModel.getLabel());
r.setText(text, 0);
} else if((text.contains("{ARGOS.DMP.DESCRIPTION}") && !isDescription) || (descriptionModel != null && text.contains("{ARGOS.DATASET.DESCRIPTION}") && isDescription)) {
descrParPos = parPos;
descrPar = p;
if(!isDescription) {
text = text.replace("{ARGOS.DMP.DESCRIPTION}", "");
} else {
text = text.replace("{ARGOS.DATASET.DESCRIPTION}", "");
}
r.setText(text, 0);
} else if(text.equals("{ARGOS.DMP.RESEARCHERS}")) {
StringBuilder researchersNames = new StringBuilder();
int i = 0;
for(ReferenceModel researcher : researchers){
i++;
researchersNames.append(researcher.getLabel()).append(i < researchers.size() ? ", " : "");
}
text = text.replace("{ARGOS.DMP.RESEARCHERS}", researchersNames.toString());
r.setText(text, 0);
r.setFontSize(17);
} else if(text.equals("{ARGOS.DMP.ORGANIZATIONS}")) {
StringBuilder organisationsNames = new StringBuilder();
int i = 0;
for(ReferenceModel organisation : organizations){
i++;
organisationsNames.append(organisation.getLabel()).append(i < organizations.size() ? ", " : "");
}
text = text.replace("{ARGOS.DMP.ORGANIZATIONS}", organisationsNames.toString());
r.setText(text, 0);
r.setFontSize(17);
}
}
}
}
parPos++;
}
if((descrParPos != -1) && (dmpEntity.getDescription() != null) && !isDescription) {
XWPFParagraph descrPar = null;
for(XWPFParagraph p: document.getParagraphs()){
if( dmpEntity != null) {
this.replaceTextSegment(p, "'{ARGOS.DMP.TITLE}'", dmpEntity.getLabel());
this.replaceTextSegment(p, "'{ARGOS.DMP.VERSION}'", "Version " + dmpEntity.getVersion());
}
if( descriptionModel != null) {
this.replaceTextSegment(p, "'{ARGOS.DATASET.TITLE}'", descriptionModel.getLabel());
}
String researchersNames = "";
int i = 0;
for(ReferenceModel researcher : researchers){
i++;
researchersNames += researcher.getLabel() + (i < researchers.size() ? ", " : "");
}
this.replaceTextSegment(p, "'{ARGOS.DMP.RESEARCHERS}'", researchersNames, 15);
String organisationsNames = "";
i = 0;
for(ReferenceModel organisation : organizations){
i++;
organisationsNames += organisation.getLabel() + (i < organizations.size() ? ", " : "");
}
this.replaceTextSegment(p, "'{ARGOS.DMP.ORGANIZATIONS}'", organisationsNames, 15);
if(this.textSegmentExists(p,"'{ARGOS.DMP.DESCRIPTION}'")) {
descrParPos = parPos;
descrPar = p;
this.replaceTextSegment(p, "'{ARGOS.DMP.DESCRIPTION}'", "");
}
if(this.textSegmentExists(p,"'{ARGOS.DATASET.DESCRIPTION}'")) {
descrParPos = parPos;
descrPar = p;
this.replaceTextSegment(p, "'{ARGOS.DATASET.DESCRIPTION}'", "");
}
}
if((descrParPos != -1) && (dmpEntity!=null) && (dmpEntity.getDescription() != null) && !isDescription) {
XmlCursor cursor = descrPar.getCTP().newCursor();
cursor.toNextSibling();
Document htmlDoc = Jsoup.parse((dmpEntity.getDescription()).replaceAll("\n", "<br>"));
Document htmlDoc = Jsoup.parse(((String)dmpEntity.getDescription()).replaceAll("\n", "<br>"));
HtmlToWorldBuilder htmlToWorldBuilder = new HtmlToWorldBuilder(descrPar, 0, cursor);
NodeTraversor.traverse(htmlToWorldBuilder, htmlDoc);
}
if((descrParPos != -1) && (descriptionModel != null) && (descriptionModel.getDescription() != null) && isDescription) {
XmlCursor cursor = descrPar.getCTP().newCursor();
cursor.toNextSibling();
Document htmlDoc = Jsoup.parse(( descriptionModel.getDescription()).replaceAll("\n", "<br>"));
Document htmlDoc = Jsoup.parse(((String)descriptionModel.getDescription()).replaceAll("\n", "<br>"));
HtmlToWorldBuilder htmlToWorldBuilder = new HtmlToWorldBuilder(descrPar, 0, cursor);
NodeTraversor.traverse(htmlToWorldBuilder, htmlDoc);
}
XWPFTable tbl = document.getTables().getFirst();
XWPFTable tbl = document.getTables().get(0);
Iterator<XWPFTableRow> it = tbl.getRows().iterator();
it.next(); // skip first row
if(it.hasNext() && !funders.isEmpty()){
XWPFParagraph p = it.next().getCell(0).getParagraphs().getFirst();
XWPFParagraph p = it.next().getCell(0).getParagraphs().get(0);
XWPFRun run = p.createRun();
run.setText(funders.getFirst().getLabel());
run.setFontSize(17);
run.setFontSize(15);
p.setAlignment(ParagraphAlignment.CENTER);
}
it = tbl.getRows().iterator();
it.next();
if(it.hasNext() && !grants.isEmpty()){
XWPFParagraph p = it.next().getCell(1).getParagraphs().getFirst();
XWPFParagraph p = it.next().getCell(1).getParagraphs().get(0);
XWPFRun run = p.createRun();
String text = grants.getFirst().getLabel();
String reference = grants.getFirst().getReference();
@ -981,11 +987,133 @@ public class WordBuilderImpl implements WordBuilder {
text += parts.length > 1 ? "/ No "+parts[parts.length - 1] : "";
}
run.setText(text);
run.setFontSize(17);
run.setFontSize(15);
p.setAlignment(ParagraphAlignment.CENTER);
}
}
private boolean textSegmentExists(XWPFParagraph paragraph, String textToFind) {
TextSegment foundTextSegment = null;
PositionInParagraph startPos = new PositionInParagraph(0, 0, 0);
while((foundTextSegment = this.searchText(paragraph, textToFind, startPos)) != null) {
return true;
}
return false;
}
private void replaceTextSegment(XWPFParagraph paragraph, String textToFind, String replacement) {
this.replaceTextSegment(paragraph, textToFind, replacement, null);
}
private void replaceTextSegment(XWPFParagraph paragraph, String textToFind, String replacement, Integer fontSize) {
TextSegment foundTextSegment = null;
PositionInParagraph startPos = new PositionInParagraph(0, 0, 0);
while((foundTextSegment = this.searchText(paragraph, textToFind, startPos)) != null) { // search all text segments having text to find
System.out.println(foundTextSegment.getBeginRun()+":"+foundTextSegment.getBeginText()+":"+foundTextSegment.getBeginChar());
System.out.println(foundTextSegment.getEndRun()+":"+foundTextSegment.getEndText()+":"+foundTextSegment.getEndChar());
// maybe there is text before textToFind in begin run
XWPFRun beginRun = paragraph.getRuns().get(foundTextSegment.getBeginRun());
String textInBeginRun = beginRun.getText(foundTextSegment.getBeginText());
String textBefore = textInBeginRun.substring(0, foundTextSegment.getBeginChar()); // we only need the text before
// maybe there is text after textToFind in end run
XWPFRun endRun = paragraph.getRuns().get(foundTextSegment.getEndRun());
String textInEndRun = endRun.getText(foundTextSegment.getEndText());
String textAfter = textInEndRun.substring(foundTextSegment.getEndChar() + 1); // we only need the text after
if (foundTextSegment.getEndRun() == foundTextSegment.getBeginRun()) {
textInBeginRun = textBefore + replacement + textAfter; // if we have only one run, we need the text before, then the replacement, then the text after in that run
} else {
textInBeginRun = textBefore + replacement; // else we need the text before followed by the replacement in begin run
endRun.setText(textAfter, foundTextSegment.getEndText()); // and the text after in end run
}
beginRun.setText(textInBeginRun, foundTextSegment.getBeginText());
if (fontSize != null) {
beginRun.setFontSize(fontSize);
}
// runs between begin run and end run needs to be removed
for (int runBetween = foundTextSegment.getEndRun() - 1; runBetween > foundTextSegment.getBeginRun(); runBetween--) {
paragraph.removeRun(runBetween); // remove not needed runs
}
}
}
private TextSegment searchText(XWPFParagraph paragraph, String searched, PositionInParagraph startPos) {
int startRun = startPos.getRun(),
startText = startPos.getText(),
startChar = startPos.getChar();
int beginRunPos = 0, candCharPos = 0;
boolean newList = false;
//CTR[] rArray = paragraph.getRArray(); //This does not contain all runs. It lacks hyperlink runs for ex.
java.util.List<XWPFRun> runs = paragraph.getRuns();
int beginTextPos = 0, beginCharPos = 0; //must be outside the for loop
//for (int runPos = startRun; runPos < rArray.length; runPos++) {
for (int runPos = startRun; runPos < runs.size(); runPos++) {
//int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos; //int beginTextPos = 0, beginCharPos = 0 must be outside the for loop
int textPos = 0, charPos;
//CTR ctRun = rArray[runPos];
CTR ctRun = runs.get(runPos).getCTR();
XmlCursor c = ctRun.newCursor();
c.selectPath("./*");
try {
while (c.toNextSelection()) {
XmlObject o = c.getObject();
if (o instanceof CTText) {
if (textPos >= startText) {
String candidate = ((CTText) o).getStringValue();
if (runPos == startRun) {
charPos = startChar;
} else {
charPos = 0;
}
for (; charPos < candidate.length(); charPos++) {
if ((candidate.charAt(charPos) == searched.charAt(0)) && (candCharPos == 0)) {
beginTextPos = textPos;
beginCharPos = charPos;
beginRunPos = runPos;
newList = true;
}
if (candidate.charAt(charPos) == searched.charAt(candCharPos)) {
if (candCharPos + 1 < searched.length()) {
candCharPos++;
} else if (newList) {
TextSegment segment = new TextSegment();
segment.setBeginRun(beginRunPos);
segment.setBeginText(beginTextPos);
segment.setBeginChar(beginCharPos);
segment.setEndRun(runPos);
segment.setEndText(textPos);
segment.setEndChar(charPos);
return segment;
}
} else {
candCharPos = 0;
}
}
}
textPos++;
} else if (o instanceof CTProofErr) {
c.removeXml();
} else if (o instanceof CTRPr) {
//do nothing
} else {
candCharPos = 0;
}
}
} finally {
c.dispose();
}
}
return null;
}
@Override
public void fillFooter(DmpModel dmpEntity, DescriptionModel descriptionModel, XWPFDocument document) {
if (dmpEntity == null) throw new IllegalArgumentException("DmpEntity required");

View File

@ -25,7 +25,7 @@
<dependency>
<groupId>gr.cite.opendmp</groupId>
<artifactId>file-transformer-base</artifactId>
<version>0.0.8</version>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>