2023-12-18 10:05:27 +01:00
package eu.eudat.file.transformer.utils.word ;
import com.fasterxml.jackson.core.JsonProcessingException ;
import com.fasterxml.jackson.databind.DeserializationFeature ;
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.enums.FieldType ;
import eu.eudat.file.transformer.enums.ReferenceType ;
import eu.eudat.file.transformer.model.* ;
2024-01-25 09:17:44 +01:00
import eu.eudat.file.transformer.models.descriptiontemplate.DescriptionTemplateFileTransformerModel ;
import eu.eudat.file.transformer.models.description.DescriptionFileTransformerModel ;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel ;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldSetFileTransformerModel ;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.PageFileTransformerModel ;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.SectionFileTransformerModel ;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.fielddata.* ;
import eu.eudat.file.transformer.models.dmp.DmpFileTransformerModel ;
import eu.eudat.file.transformer.models.dmp.DmpReferenceFileTransformerModel ;
import eu.eudat.file.transformer.models.reference.ReferenceFileTransformerModel ;
2023-12-18 10:05:27 +01:00
import eu.eudat.file.transformer.utils.interfaces.ApplierWithValue ;
import eu.eudat.file.transformer.utils.pid.PidLoader ;
import eu.eudat.file.transformer.utils.types.ParagraphStyle ;
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.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.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import javax.imageio.ImageIO ;
import javax.imageio.ImageReader ;
import javax.imageio.stream.ImageInputStream ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.IOException ;
import java.math.BigInteger ;
import java.time.Instant ;
import java.time.LocalDate ;
import java.time.ZoneId ;
import java.time.ZoneOffset ;
import java.time.format.DateTimeFormatter ;
import java.time.format.DateTimeParseException ;
import java.util.* ;
import java.util.stream.Collectors ;
import static org.apache.poi.xwpf.usermodel.Document.* ;
public class WordBuilder {
private static final Logger logger = LoggerFactory . getLogger ( WordBuilder . class ) ;
private static final Map < String , Integer > IMAGE_TYPE_MAP = Map . of (
" image/jpeg " , PICTURE_TYPE_JPEG ,
" image/png " , PICTURE_TYPE_PNG ,
" image/gif " , PICTURE_TYPE_GIF ,
" image/tiff " , PICTURE_TYPE_TIFF ,
" image/bmp " , PICTURE_TYPE_BMP ,
" image/wmf " , PICTURE_TYPE_WMF
) ;
private Map < ParagraphStyle , ApplierWithValue < XWPFDocument , Object , XWPFParagraph > > options = new HashMap < > ( ) ;
private Map < ParagraphStyle , ApplierWithValue < XWPFTableCell , Object , XWPFParagraph > > optionsInTable = new HashMap < > ( ) ;
private CTAbstractNum cTAbstractNum ;
private BigInteger numId ;
private Integer indent ;
private final ObjectMapper mapper ;
private Integer imageCount ;
private final FilePathProperties fileTemplateProperties ;
private final FileStorageProperties fileStorageProperties ;
public WordBuilder ( FilePathProperties fileTemplateProperties , FileStorageProperties fileStorageProperties ) {
this . fileTemplateProperties = fileTemplateProperties ;
this . fileStorageProperties = fileStorageProperties ;
this . cTAbstractNum = CTAbstractNum . Factory . newInstance ( ) ;
this . cTAbstractNum . setAbstractNumId ( BigInteger . valueOf ( 1 ) ) ;
this . indent = 0 ;
this . imageCount = 0 ;
this . mapper = new ObjectMapper ( ) ;
this . buildOptions ( ) ;
this . buildOptionsInTable ( ) ;
}
private void buildOptionsInTable ( ) {
this . optionsInTable . put ( ParagraphStyle . TEXT , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . addParagraph ( ) ;
XWPFRun run = paragraph . createRun ( ) ;
if ( item ! = null )
run . setText ( " " + item ) ;
run . setFontSize ( 11 ) ;
return paragraph ;
} ) ;
this . optionsInTable . put ( ParagraphStyle . HTML , ( mainDocumentPart , item ) - > {
Document htmlDoc = Jsoup . parse ( ( ( String ) item ) . replaceAll ( " \ n " , " <br> " ) ) ;
HtmlToWorldBuilder htmlToWorldBuilder = HtmlToWorldBuilder . convertInTable ( mainDocumentPart , htmlDoc , 0 ) ;
return htmlToWorldBuilder . getParagraph ( ) ;
} ) ;
this . optionsInTable . put ( ParagraphStyle . TITLE , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . addParagraph ( ) ;
paragraph . setStyle ( " Title " ) ;
paragraph . setAlignment ( ParagraphAlignment . CENTER ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( ( String ) item ) ;
run . setBold ( true ) ;
run . setFontSize ( 14 ) ;
return paragraph ;
} ) ;
this . optionsInTable . put ( ParagraphStyle . IMAGE , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . addParagraph ( ) ;
XWPFRun run = paragraph . createRun ( ) ;
if ( item ! = null )
run . setText ( ( ( Map < String , String > ) item ) . get ( " name " ) ) ;
run . setFontSize ( 11 ) ;
run . setItalic ( true ) ;
return paragraph ;
} ) ;
}
private void buildOptions ( ) {
this . options . put ( ParagraphStyle . TEXT , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
XWPFRun run = paragraph . createRun ( ) ;
if ( item ! = null )
run . setText ( " " + item ) ;
run . setFontSize ( 11 ) ;
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . HTML , ( mainDocumentPart , item ) - > {
Document htmlDoc = Jsoup . parse ( ( ( String ) item ) . replaceAll ( " \ n " , " <br> " ) ) ;
HtmlToWorldBuilder htmlToWorldBuilder = HtmlToWorldBuilder . convert ( mainDocumentPart , htmlDoc , this . indent ) ;
return htmlToWorldBuilder . getParagraph ( ) ;
} ) ;
this . options . put ( ParagraphStyle . TITLE , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
paragraph . setStyle ( " Title " ) ;
paragraph . setAlignment ( ParagraphAlignment . CENTER ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( ( String ) item ) ;
run . setBold ( true ) ;
run . setFontSize ( 14 ) ;
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . HEADER1 , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
paragraph . setStyle ( " Heading1 " ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( ( String ) item ) ;
// run.setBold(true);
// run.setFontSize(12);
// run.setStyle("0");
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . HEADER2 , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
paragraph . setStyle ( " Heading2 " ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( " " + item ) ;
// run.setBold(true);
// run.setFontSize(12);
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . HEADER3 , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
paragraph . setStyle ( " Heading3 " ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( " " + item ) ;
// run.setBold(true);
// run.setFontSize(11);
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . HEADER4 , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
paragraph . setStyle ( " Heading4 " ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( ( String ) item ) ;
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . HEADER5 , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
paragraph . setStyle ( " Heading5 " ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( " " + item ) ;
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . HEADER6 , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
paragraph . setStyle ( " Heading6 " ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( " " + item ) ;
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . FOOTER , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( ( String ) item ) ;
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . COMMENT , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( " " + item ) ;
run . setItalic ( true ) ;
return paragraph ;
} ) ;
this . options . put ( ParagraphStyle . IMAGE , ( mainDocumentPart , item ) - > {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
paragraph . setPageBreak ( true ) ;
paragraph . setSpacingAfter ( 0 ) ;
paragraph . setAlignment ( ParagraphAlignment . CENTER ) ; //GK: Center the image if it is too small
XWPFRun run = paragraph . createRun ( ) ;
String imageId = ( ( Map < String , String > ) item ) . get ( " id " ) ;
String fileName = ( ( Map < String , String > ) item ) . get ( " name " ) ;
String fileType = ( ( Map < String , String > ) item ) . get ( " type " ) ;
int format ;
format = IMAGE_TYPE_MAP . getOrDefault ( fileType , 0 ) ;
try {
FileInputStream image = new FileInputStream ( fileStorageProperties . getTemp ( ) + imageId ) ;
ImageInputStream iis = ImageIO . createImageInputStream ( new File ( fileStorageProperties . getTemp ( ) + imageId ) ) ;
Iterator < ImageReader > readers = ImageIO . getImageReaders ( iis ) ;
if ( readers . hasNext ( ) ) {
ImageReader reader = readers . next ( ) ;
reader . setInput ( iis ) ;
int initialImageWidth = reader . getWidth ( 0 ) ;
int initialImageHeight = reader . getHeight ( 0 ) ;
float ratio = initialImageHeight / ( float ) initialImageWidth ;
int marginLeftInDXA = ( int ) mainDocumentPart . getDocument ( ) . getBody ( ) . getSectPr ( ) . getPgMar ( ) . getLeft ( ) ;
int marginRightInDXA = ( int ) mainDocumentPart . getDocument ( ) . getBody ( ) . getSectPr ( ) . getPgMar ( ) . getRight ( ) ;
int pageWidthInDXA = ( int ) mainDocumentPart . getDocument ( ) . getBody ( ) . getSectPr ( ) . getPgSz ( ) . getW ( ) ;
int pageWidth = Math . round ( ( pageWidthInDXA - marginLeftInDXA - marginRightInDXA ) / ( float ) 20 ) ; // /20 converts dxa to points
int imageWidth = Math . round ( initialImageWidth * ( float ) 0 . 75 ) ; // *0.75 converts pixels to points
int width = Math . min ( imageWidth , pageWidth ) ;
int marginTopInDXA = ( int ) mainDocumentPart . getDocument ( ) . getBody ( ) . getSectPr ( ) . getPgMar ( ) . getTop ( ) ;
int marginBottomInDXA = ( int ) mainDocumentPart . getDocument ( ) . getBody ( ) . getSectPr ( ) . getPgMar ( ) . getBottom ( ) ;
int pageHeightInDXA = ( int ) mainDocumentPart . getDocument ( ) . getBody ( ) . getSectPr ( ) . getPgSz ( ) . getH ( ) ;
int pageHeight = Math . round ( ( pageHeightInDXA - marginTopInDXA - marginBottomInDXA ) / ( float ) 20 ) ; // /20 converts dxa to points
int imageHeight = Math . round ( initialImageHeight * ( ( float ) 0 . 75 ) ) ; // *0.75 converts pixels to points
int height = Math . round ( width * ratio ) ;
if ( height > pageHeight ) {
// height calculated with ratio is too large. Image may have Portrait (vertical) orientation. Recalculate image dimensions.
height = Math . min ( imageHeight , pageHeight ) ;
width = Math . round ( height / ratio ) ;
}
XWPFPicture picture = run . addPicture ( image , format , fileName , Units . toEMU ( width ) , Units . toEMU ( height ) ) ;
paragraph . setPageBreak ( false ) ;
imageCount + + ;
XWPFParagraph captionParagraph = mainDocumentPart . createParagraph ( ) ;
captionParagraph . setAlignment ( ParagraphAlignment . CENTER ) ;
captionParagraph . setSpacingBefore ( 0 ) ;
captionParagraph . setStyle ( " Caption " ) ;
XWPFRun captionRun = captionParagraph . createRun ( ) ;
captionRun . setText ( " Image " + imageCount ) ;
}
} catch ( IOException | InvalidFormatException e ) {
logger . error ( e . getMessage ( ) , e ) ;
}
return paragraph ;
} ) ;
}
2024-01-25 09:17:44 +01:00
public XWPFDocument build ( XWPFDocument document , DescriptionTemplateFileTransformerModel descriptionTemplate ) throws IOException {
2023-12-18 10:05:27 +01:00
// createPages(pagedDatasetProfile.getPages(), document, true, visibilityRuleService);
// XWPFNumbering numbering = document.createNumbering();
// BigInteger tempNumId = BigInteger.ONE;
// boolean found = false;
// while (!found) {
// Object o = numbering.getAbstractNum(tempNumId);
// found = (o == null);
// if (!found) tempNumId = tempNumId.add(BigInteger.ONE);
// }
// cTAbstractNum.setAbstractNumId(tempNumId);
// XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
// BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
// this.numId = numbering.addNum(abstractNumID);
2024-01-25 09:17:44 +01:00
createPages ( descriptionTemplate . getDefinition ( ) . getPages ( ) , document , false ) ;
2023-12-18 10:05:27 +01:00
return document ;
}
2024-01-25 09:17:44 +01:00
private void createPages ( List < PageFileTransformerModel > datasetProfilePages , XWPFDocument mainDocumentPart , Boolean createListing ) {
2024-02-02 16:45:11 +01:00
datasetProfilePages . stream ( ) . filter ( item - > item . getSections ( ) ! = null ) . forEach ( item - > {
2023-12-18 10:05:27 +01:00
try {
2024-01-25 09:17:44 +01:00
createSections ( item . getSections ( ) , mainDocumentPart , ParagraphStyle . HEADER5 , 0 , createListing , item . getOrdinal ( ) + 1 , null ) ;
2023-12-18 10:05:27 +01:00
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
}
} ) ;
}
2024-01-25 09:17:44 +01:00
private void createSections ( List < SectionFileTransformerModel > sections , XWPFDocument mainDocumentPart , ParagraphStyle style , Integer indent , Boolean createListing , Integer page , String sectionString ) {
2023-12-18 10:05:27 +01:00
if ( createListing ) this . addListing ( mainDocumentPart , indent , false , true ) ;
boolean hasValue = false ;
2024-02-02 16:45:11 +01:00
2024-01-25 09:17:44 +01:00
for ( SectionFileTransformerModel section : sections ) {
2023-12-18 10:05:27 +01:00
int paragraphPos = - 1 ;
String tempSectionString = sectionString ! = null ? sectionString + " . " + ( section . getOrdinal ( ) + 1 ) : " " + ( section . getOrdinal ( ) + 1 ) ;
2024-01-25 09:17:44 +01:00
if ( ! createListing ) {
XWPFParagraph paragraph = addParagraphContent ( page + " . " + tempSectionString + " " + section . getTitle ( ) , mainDocumentPart , style , numId , indent ) ;
2023-12-18 10:05:27 +01:00
// CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
// number.setVal(BigInteger.valueOf(indent));
2024-01-25 09:17:44 +01:00
paragraphPos = mainDocumentPart . getPosOfParagraph ( paragraph ) ;
}
2024-02-02 16:45:11 +01:00
if ( section . getSections ( ) ! = null ) {
createSections ( section . getSections ( ) , mainDocumentPart , ParagraphStyle . HEADER5 , indent + 1 , createListing , page , tempSectionString ) ;
}
2024-01-25 09:17:44 +01:00
hasValue = createCompositeFields ( section . getFieldSets ( ) , mainDocumentPart , indent + 1 , createListing , page , tempSectionString ) ;
2023-12-18 10:05:27 +01:00
2024-01-25 09:17:44 +01:00
if ( ! hasValue & & paragraphPos > - 1 ) {
mainDocumentPart . removeBodyElement ( paragraphPos ) ;
2023-12-18 10:05:27 +01:00
}
}
}
2024-01-25 09:17:44 +01:00
private Boolean createCompositeFields ( List < FieldSetFileTransformerModel > compositeFields , XWPFDocument mainDocumentPart , Integer indent , Boolean createListing , Integer page , String section ) {
2023-12-18 10:05:27 +01:00
if ( createListing ) this . addListing ( mainDocumentPart , indent , true , true ) ;
boolean hasValue = false ;
boolean returnedValue = false ;
2024-01-25 09:17:44 +01:00
for ( FieldSetFileTransformerModel compositeField : compositeFields ) {
2023-12-18 10:05:27 +01:00
char c = 'a' ;
int multiplicityItems = 0 ;
boolean hasMultiplicityItems = false ;
int paragraphPos = - 1 ;
int paragraphPosInner = - 1 ;
if ( compositeField . getTitle ( ) ! = null & & ! compositeField . getTitle ( ) . isEmpty ( ) & & ! createListing ) {
XWPFParagraph paragraph = addParagraphContent ( page + " . " + section + " . " + ( compositeField . getOrdinal ( ) + 1 ) + " " + compositeField . getTitle ( ) , mainDocumentPart , ParagraphStyle . HEADER6 , numId , indent ) ;
// CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
// number.setVal(BigInteger.valueOf(indent));
paragraphPos = mainDocumentPart . getPosOfParagraph ( paragraph ) ;
2024-01-25 09:17:44 +01:00
//TODO: dtziotzios implement multiplicity
// if(compositeField.getMultiplicity() != null && !compositeField.getMultiplicity().getTableView() && compositeField.getMultiplicityItems() != null && !compositeField.getMultiplicityItems().isEmpty()){
// XWPFParagraph paragraphInner = addParagraphContent(c + ". ", mainDocumentPart, ParagraphStyle.TEXT, numId, indent);
// paragraphPosInner = mainDocumentPart.getPosOfParagraph(paragraphInner);
// hasMultiplicityItems = true;
// multiplicityItems++;
// }
2023-12-18 10:05:27 +01:00
}
XWPFTable tbl = null ;
XWPFTableRow row = null ;
int numOfRows = 0 ;
if ( compositeField . getMultiplicity ( ) ! = null & & compositeField . getMultiplicity ( ) . getTableView ( ) ) {
tbl = mainDocumentPart . createTable ( ) ;
tbl . setTableAlignment ( TableRowAlign . CENTER ) ;
mainDocumentPart . createParagraph ( ) ;
2024-01-25 09:17:44 +01:00
createHeadersInTable ( compositeField . getFields ( ) , tbl ) ;
2023-12-18 10:05:27 +01:00
numOfRows = tbl . getRows ( ) . size ( ) ;
row = tbl . createRow ( ) ;
}
if ( compositeField . getMultiplicity ( ) ! = null & & compositeField . getMultiplicity ( ) . getTableView ( ) ) {
2024-01-25 09:17:44 +01:00
hasValue = createFieldsInTable ( compositeField . getFields ( ) , row , indent , createListing , hasMultiplicityItems , numOfRows ) ;
2023-12-18 10:05:27 +01:00
numOfRows + + ;
} else {
2024-01-25 09:17:44 +01:00
hasValue = createFields ( compositeField . getFields ( ) , mainDocumentPart , indent , createListing , hasMultiplicityItems ) ;
2023-12-18 10:05:27 +01:00
}
if ( hasValue ) {
returnedValue = true ;
} else if ( paragraphPosInner > - 1 ) {
mainDocumentPart . removeBodyElement ( paragraphPosInner ) ;
c - - ;
multiplicityItems - - ;
}
//TODO: Multiplicity Items???
2024-01-25 09:17:44 +01:00
//TODO: dtziotzios implement multiplicity
// if (compositeField.getMultiplicityItems() != null && !compositeField.getMultiplicityItems().isEmpty()) {
// List<FieldSetFileTransformerModel> list = compositeField.getMultiplicityItems().stream().sorted(Comparator.comparingInt(FieldSetFileTransformerModel::getOrdinal)).collect(Collectors.toList());
// for (FieldSetFileTransformerModel multiplicityFieldset : list) {
// paragraphPosInner = -1;
// if(!compositeField.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++;
// }
//// hasValue = createFields(multiplicityFieldset.getFields(), mainDocumentPart, 3, createListing, visibilityRuleService, hasMultiplicityItems);
// boolean hasValueInner = false;
// if(compositeField.getMultiplicity().getTableView()) {
// row = tbl.createRow();
// hasValueInner = createFieldsInTable(multiplicityFieldset.getFields(), row, indent, createListing, visibilityRuleService, hasMultiplicityItems, numOfRows);
// numOfRows++;
// } else {
// hasValueInner = createFields(multiplicityFieldset.getFields(), mainDocumentPart, indent, createListing, visibilityRuleService, hasMultiplicityItems);
// }
//// 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().get(0).getText(0);
// if(text.equals("a. ")) {
// mainDocumentPart.getLastParagraph().removeRun(0);
// }
// }
// }
2023-12-18 10:05:27 +01:00
//TODO: Dataset Profile
// if (hasValue && compositeField.getHasCommentField() && compositeField.getCommentFieldValue() != null && !compositeField.getCommentFieldValue().isEmpty() && !createListing) {
// XWPFParagraph paragraph = addParagraphContent("<i>Comment:</i>\n"+compositeField.getCommentFieldValue(), mainDocumentPart, ParagraphStyle.HTML, numId, indent);
//// CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
//// number.setVal(BigInteger.valueOf(indent));
// }
if ( ! hasValue & & paragraphPos > - 1 ) {
mainDocumentPart . removeBodyElement ( paragraphPos ) ;
}
}
return returnedValue ;
}
2024-01-25 09:17:44 +01:00
private void createHeadersInTable ( List < FieldFileTransformerModel > fields , XWPFTable table ) {
2023-12-18 10:05:27 +01:00
boolean atLeastOneHeader = false ;
2024-01-25 09:17:44 +01:00
List < FieldFileTransformerModel > tempFields = fields . stream ( ) . sorted ( Comparator . comparingInt ( FieldFileTransformerModel : : getOrdinal ) ) . collect ( Collectors . toList ( ) ) ;
2023-12-18 10:05:27 +01:00
int index = 0 ;
XWPFTableRow row = table . getRow ( 0 ) ;
2024-01-25 09:17:44 +01:00
for ( FieldFileTransformerModel field : tempFields ) {
if ( field . getIncludeInExport ( ) ) {
2023-12-18 10:05:27 +01:00
XWPFTableCell cell ;
2024-01-25 09:17:44 +01:00
if ( index = = 0 ) {
2023-12-18 10:05:27 +01:00
cell = row . getCell ( 0 ) ;
} else {
cell = row . createCell ( ) ;
}
cell . setVerticalAlignment ( XWPFTableCell . XWPFVertAlign . valueOf ( " CENTER " ) ) ;
2024-02-02 16:45:11 +01:00
String label = field . getData ( ) . getLabel ( ) ;
2024-01-25 09:17:44 +01:00
if ( label ! = null & & label ! = " " ) {
2023-12-18 10:05:27 +01:00
XWPFParagraph paragraph = cell . getParagraphs ( ) . get ( 0 ) ;
paragraph . setIndentationFirstLine ( 50 ) ;
XWPFRun run = paragraph . createRun ( ) ;
run . setText ( label ) ;
run . setBold ( true ) ;
run . setFontSize ( 12 ) ;
paragraph . setAlignment ( ParagraphAlignment . CENTER ) ;
paragraph . setSpacingBefore ( 100 ) ;
atLeastOneHeader = true ;
}
}
index + + ;
}
if ( ! atLeastOneHeader ) {
table . removeRow ( 0 ) ;
}
}
2024-01-25 09:17:44 +01:00
private Boolean createFieldsInTable ( List < FieldFileTransformerModel > fields , XWPFTableRow mainDocumentPart , Integer indent , Boolean createListing , boolean hasMultiplicityItems , int numOfRows ) {
2023-12-18 10:05:27 +01:00
int numOfCells = 0 ;
boolean hasValue = false ;
2024-01-25 09:17:44 +01:00
List < FieldFileTransformerModel > tempFields = fields . stream ( ) . sorted ( Comparator . comparingInt ( FieldFileTransformerModel : : getOrdinal ) ) . collect ( Collectors . toList ( ) ) ;
for ( FieldFileTransformerModel field : tempFields ) {
if ( field . getIncludeInExport ( ) ) {
2023-12-18 10:05:27 +01:00
if ( ! createListing ) {
try {
2024-02-02 16:45:11 +01:00
if ( field . getData ( ) . getFieldType ( ) . equals ( FieldType . UPLOAD ) ) {
2023-12-18 10:05:27 +01:00
boolean isImage = false ;
2024-02-02 16:45:11 +01:00
for ( UploadDataFileTransformerModel . UploadDataOptionFileTransformerModel type : ( ( UploadDataFileTransformerModel ) field . getData ( ) ) . getTypes ( ) ) {
2023-12-18 10:05:27 +01:00
String fileFormat = type . getValue ( ) ;
if ( IMAGE_TYPE_MAP . containsKey ( fileFormat ) ) {
isImage = true ;
break ;
}
}
if ( isImage ) {
2024-01-25 09:17:44 +01:00
if ( field . getData ( ) . getValue ( ) ! = null & & ! field . getData ( ) . getValue ( ) . toString ( ) . isEmpty ( ) ) {
XWPFParagraph paragraph = addCellContent ( mapper . convertValue ( field . getData ( ) . getValue ( ) , Map . class ) , mainDocumentPart , ParagraphStyle . IMAGE , numId , 0 , numOfRows , numOfCells , 0 ) ;
2023-12-18 10:05:27 +01:00
if ( paragraph ! = null ) {
hasValue = true ;
}
if ( hasMultiplicityItems ) {
hasMultiplicityItems = false ;
}
}
}
}
2024-01-25 09:17:44 +01:00
else if ( field . getData ( ) . getValue ( ) ! = null & & ! field . getData ( ) . getValue ( ) . toString ( ) . isEmpty ( ) ) {
2023-12-18 10:05:27 +01:00
this . indent = indent ;
String format = this . formatter ( field ) ;
2024-01-25 09:17:44 +01:00
if ( ( ( BaseFieldDataFileTransformerModel ) field . getData ( ) ) . getFieldType ( ) . equals ( FieldType . TAGS ) ) {
2023-12-18 10:05:27 +01:00
format = getCommaSeparatedFormatsFromJson ( format , " name " ) ;
2024-02-02 16:45:11 +01:00
} else if ( ( ( BaseFieldDataFileTransformerModel ) field . getData ( ) ) . getFieldType ( ) . equals ( FieldType . EXTERNAL_SELECT ) ) {
2023-12-18 10:05:27 +01:00
format = getCommaSeparatedFormatsFromJson ( format , " label " ) ;
}
2024-01-25 09:17:44 +01:00
boolean isResearcher = ( ( BaseFieldDataFileTransformerModel ) field . getData ( ) ) . getFieldType ( ) . equals ( FieldType . RESEARCHERS ) ;
2023-12-18 10:05:27 +01:00
if ( format ! = null & & ! format . isEmpty ( ) ) {
Object hasMultiAutoComplete = mapper . convertValue ( field . getData ( ) , Map . class ) . get ( " multiAutoComplete " ) ;
boolean isMultiAutoComplete = hasMultiAutoComplete ! = null & & ( boolean ) hasMultiAutoComplete ;
boolean arrayStringFormat = format . charAt ( 0 ) = = '[' ;
if ( arrayStringFormat | | isMultiAutoComplete ) {
List < String > values = ( arrayStringFormat ) ? Arrays . asList ( format . substring ( 1 , format . length ( ) - 1 ) . split ( " ,[ ]* " ) ) : Arrays . asList ( format . split ( " ,[ ]* " ) ) ;
if ( values . size ( ) > 1 ) {
boolean orcidResearcher ;
int numOfValuesInCell = 0 ;
for ( String val : values ) {
orcidResearcher = false ;
String orcId = null ;
if ( isResearcher & & val . contains ( " orcid: " ) ) {
orcId = val . substring ( val . indexOf ( ':' ) + 1 , val . indexOf ( ')' ) ) ;
val = val . substring ( 0 , val . indexOf ( ':' ) + 1 ) + " " ;
orcidResearcher = true ;
}
format = " • " + val ;
if ( hasMultiplicityItems ) {
XWPFParagraph paragraph = mainDocumentPart . getCell ( mainDocumentPart . getTableCells ( ) . size ( ) ) . addParagraph ( ) ;
paragraph . createRun ( ) . setText ( format ) ;
if ( orcidResearcher ) {
XWPFHyperlinkRun run = paragraph . createHyperlinkRun ( " https://orcid.org/ " + orcId ) ;
run . setText ( orcId ) ;
run . setUnderline ( UnderlinePatterns . SINGLE ) ;
run . setColor ( " 0000FF " ) ;
paragraph . createRun ( ) . setText ( " ) " ) ;
}
hasMultiplicityItems = false ;
}
else {
2024-01-25 09:17:44 +01:00
XWPFParagraph paragraph = addCellContent ( format , mainDocumentPart , ( ( BaseFieldDataFileTransformerModel ) field . getData ( ) ) . getFieldType ( ) . equals ( FieldType . RICH_TEXT_AREA ) ? ParagraphStyle . HTML : ParagraphStyle . TEXT , numId , indent , numOfRows , numOfCells , numOfValuesInCell ) ;
2023-12-18 10:05:27 +01:00
numOfValuesInCell + + ;
if ( orcidResearcher ) {
XWPFHyperlinkRun run = paragraph . createHyperlinkRun ( " https://orcid.org/ " + orcId ) ;
run . setText ( orcId ) ;
run . setUnderline ( UnderlinePatterns . SINGLE ) ;
run . setColor ( " 0000FF " ) ;
paragraph . createRun ( ) . setText ( " ) " ) ;
}
if ( paragraph ! = null ) {
hasValue = true ;
}
}
format = null ;
}
}
else if ( values . size ( ) = = 1 ) {
format = values . get ( 0 ) ;
}
}
}
if ( hasMultiplicityItems & & format ! = null ) {
XWPFParagraph paragraph = mainDocumentPart . getCell ( mainDocumentPart . getTableCells ( ) . size ( ) ) . addParagraph ( ) ;
paragraph . createRun ( ) . setText ( format ) ;
hasMultiplicityItems = false ;
hasValue = true ;
}
else {
2024-01-25 09:17:44 +01:00
XWPFParagraph paragraph = addCellContent ( format , mainDocumentPart , ( ( BaseFieldDataFileTransformerModel ) field . getData ( ) ) . getFieldType ( ) . equals ( FieldType . RICH_TEXT_AREA ) ? ParagraphStyle . HTML : ParagraphStyle . TEXT , numId , indent , numOfRows , numOfCells , 0 ) ;
2023-12-18 10:05:27 +01:00
if ( paragraph ! = null ) {
hasValue = true ;
}
}
}
} catch ( IOException e ) {
logger . error ( e . getMessage ( ) , e ) ;
}
}
numOfCells + + ;
}
}
return hasValue ;
}
private void createHypeLink ( XWPFDocument mainDocumentPart , String format , String pidType , String pid , boolean hasMultiplicityItems , boolean isMultiAutoComplete ) {
PidLink pidLink = PidLoader . loadPidLinks ( this . fileTemplateProperties . getPidTemplate ( ) ) . stream ( ) . filter ( pl - > pl . getPid ( ) . equals ( pidType ) ) . findFirst ( ) . orElse ( null ) ;
if ( pidLink ! = null ) {
if ( ! hasMultiplicityItems ) {
XWPFParagraph paragraph = mainDocumentPart . createParagraph ( ) ;
paragraph . setIndentFromLeft ( 400 * indent ) ;
if ( numId ! = null ) {
paragraph . setNumID ( numId ) ;
}
}
if ( isMultiAutoComplete ) {
XWPFRun r = mainDocumentPart . getLastParagraph ( ) . createRun ( ) ;
r . setText ( " • " ) ;
}
XWPFHyperlinkRun run = mainDocumentPart . getLastParagraph ( ) . createHyperlinkRun ( pidLink . getLink ( ) . replace ( " {pid} " , pid ) ) ;
run . setText ( format ) ;
run . setUnderline ( UnderlinePatterns . SINGLE ) ;
run . setColor ( " 0000FF " ) ;
run . setFontSize ( 11 ) ;
}
else {
String newFormat = ( isMultiAutoComplete ) ? " • " + format : format ;
if ( hasMultiplicityItems ) {
mainDocumentPart . getLastParagraph ( ) . createRun ( ) . setText ( newFormat ) ;
}
else {
addParagraphContent ( newFormat , mainDocumentPart , ParagraphStyle . TEXT , numId , indent ) ;
}
}
}
2024-01-25 09:17:44 +01:00
private Boolean createFields ( List < FieldFileTransformerModel > fields , XWPFDocument mainDocumentPart , Integer indent , Boolean createListing , boolean hasMultiplicityItems ) {
2023-12-18 10:05:27 +01:00
if ( createListing ) this . addListing ( mainDocumentPart , indent , false , false ) ;
boolean hasValue = false ;
2024-01-25 09:17:44 +01:00
List < FieldFileTransformerModel > tempFields = fields . stream ( ) . sorted ( Comparator . comparingInt ( FieldFileTransformerModel : : getOrdinal ) ) . collect ( Collectors . toList ( ) ) ;
for ( FieldFileTransformerModel field : tempFields ) {
if ( field . getIncludeInExport ( ) ) {
2023-12-18 10:05:27 +01:00
if ( ! createListing ) {
try {
2024-01-31 16:49:49 +01:00
if ( field . getData ( ) ! = null ) {
if ( field . getData ( ) . getFieldType ( ) . equals ( FieldType . UPLOAD ) ) {
boolean isImage = false ;
2024-02-02 16:45:11 +01:00
for ( UploadDataFileTransformerModel . UploadDataOptionFileTransformerModel type : ( ( UploadDataFileTransformerModel ) field . getData ( ) ) . getTypes ( ) ) {
2024-01-31 16:49:49 +01:00
String fileFormat = type . getValue ( ) ;
if ( IMAGE_TYPE_MAP . containsKey ( fileFormat ) ) {
isImage = true ;
break ;
}
2023-12-18 10:05:27 +01:00
}
2024-01-31 16:49:49 +01:00
if ( isImage ) {
if ( field . getData ( ) . getValue ( ) ! = null & & ! field . getData ( ) . getValue ( ) . isEmpty ( ) ) {
XWPFParagraph paragraph = addParagraphContent ( mapper . convertValue ( field . getData ( ) . getValue ( ) , Map . class ) , mainDocumentPart , ParagraphStyle . IMAGE , numId , 0 ) ;
if ( paragraph ! = null ) {
2023-12-18 10:05:27 +01:00
// CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
// number.setVal(BigInteger.valueOf(indent));
2024-01-31 16:49:49 +01:00
hasValue = true ;
}
if ( hasMultiplicityItems ) {
hasMultiplicityItems = false ;
}
2023-12-18 10:05:27 +01:00
}
}
2024-01-31 16:49:49 +01:00
} else if ( field . getData ( ) . getValue ( ) ! = null & & ! field . getData ( ) . getValue ( ) . isEmpty ( ) ) {
this . indent = indent ;
String format = this . formatter ( field ) ;
if ( field . getData ( ) . getFieldType ( ) . equals ( FieldType . TAGS ) ) {
format = getCommaSeparatedFormatsFromJson ( format , " name " ) ;
2024-02-02 16:45:11 +01:00
} else if ( field . getData ( ) . getFieldType ( ) . equals ( FieldType . EXTERNAL_SELECT ) ) {
2024-01-31 16:49:49 +01:00
format = getCommaSeparatedFormatsFromJson ( format , " label " ) ;
}
switch ( ( ( BaseFieldDataFileTransformerModel ) field . getData ( ) ) . getFieldType ( ) ) {
case ORGANIZATIONS :
case EXTERNAL_DATASETS :
case PUBLICATIONS :
if ( format ! = null & & ! format . isEmpty ( ) ) {
Object hasMultiAutoComplete = mapper . convertValue ( field . getData ( ) , Map . class ) . get ( " multiAutoComplete " ) ;
boolean isMultiAutoComplete = hasMultiAutoComplete ! = null & & ( boolean ) hasMultiAutoComplete ;
if ( ! isMultiAutoComplete ) {
Map < String , String > value = mapper . readValue ( field . getData ( ) . getValue ( ) , Map . class ) ;
if ( hasMultiplicityItems ) {
createHypeLink ( mainDocumentPart , format , value . get ( " pidTypeField " ) , value . get ( " pid " ) , true , false ) ;
2023-12-18 10:05:27 +01:00
hasMultiplicityItems = false ;
2024-01-31 16:49:49 +01:00
} else {
createHypeLink ( mainDocumentPart , format , value . get ( " pidTypeField " ) , value . get ( " pid " ) , false , false ) ;
2023-12-18 10:05:27 +01:00
}
2024-01-31 16:49:49 +01:00
} else {
mapper . configure ( DeserializationFeature . ACCEPT_SINGLE_VALUE_AS_ARRAY , true ) ;
List < Map < String , Object > > values = new ArrayList < > ( ) ;
try {
values = Arrays . asList ( mapper . readValue ( field . getData ( ) . getValue ( ) , HashMap [ ] . class ) ) ;
} catch ( Exception e ) {
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " label " , field . getData ( ) . getValue ( ) ) ;
values . add ( map ) ;
2023-12-18 10:05:27 +01:00
}
2024-01-31 16:49:49 +01:00
if ( values . size ( ) > 1 ) {
for ( Map < String , Object > value : values ) {
if ( hasMultiplicityItems ) {
createHypeLink ( mainDocumentPart , ( String ) value . get ( " name " ) , ( String ) value . get ( " pidTypeField " ) , ( String ) value . get ( " pid " ) , true , true ) ;
hasMultiplicityItems = false ;
} else {
createHypeLink ( mainDocumentPart , ( String ) value . get ( " name " ) , ( String ) value . get ( " pidTypeField " ) , ( String ) value . get ( " pid " ) , false , true ) ;
2023-12-18 10:05:27 +01:00
}
2024-01-31 16:49:49 +01:00
}
} else if ( values . size ( ) = = 1 ) {
if ( hasMultiplicityItems ) {
createHypeLink ( mainDocumentPart , format , ( String ) values . get ( 0 ) . get ( " pidTypeField " ) , ( String ) values . get ( 0 ) . get ( " pid " ) , true , false ) ;
2023-12-18 10:05:27 +01:00
hasMultiplicityItems = false ;
2024-01-31 16:49:49 +01:00
} else {
createHypeLink ( mainDocumentPart , format , ( String ) values . get ( 0 ) . get ( " pidTypeField " ) , ( String ) values . get ( 0 ) . get ( " pid " ) , false , false ) ;
2023-12-18 10:05:27 +01:00
}
2024-01-31 16:49:49 +01:00
}
}
hasValue = true ;
}
break ;
default :
boolean isResearcher = field . getData ( ) . getFieldType ( ) . equals ( FieldType . RESEARCHERS ) ;
if ( format ! = null & & ! format . isEmpty ( ) ) {
Object hasMultiAutoComplete = mapper . convertValue ( field . getData ( ) , Map . class ) . get ( " multiAutoComplete " ) ;
boolean isMultiAutoComplete = hasMultiAutoComplete ! = null & & ( boolean ) hasMultiAutoComplete ;
boolean arrayStringFormat = format . charAt ( 0 ) = = '[' ;
if ( arrayStringFormat | | isMultiAutoComplete ) {
List < String > values = ( arrayStringFormat ) ? Arrays . asList ( format . substring ( 1 , format . length ( ) - 1 ) . split ( " ,[ ]* " ) ) : Arrays . asList ( format . split ( " ,[ ]* " ) ) ;
if ( values . size ( ) > 1 ) {
boolean orcidResearcher ;
for ( String val : values ) {
orcidResearcher = false ;
String orcId = null ;
if ( isResearcher & & val . contains ( " orcid: " ) ) {
orcId = val . substring ( val . indexOf ( ':' ) + 1 , val . indexOf ( ')' ) ) ;
val = val . substring ( 0 , val . indexOf ( ':' ) + 1 ) + " " ;
orcidResearcher = true ;
2023-12-18 10:05:27 +01:00
}
2024-01-31 16:49:49 +01:00
format = " • " + val ;
if ( hasMultiplicityItems ) {
mainDocumentPart . getLastParagraph ( ) . createRun ( ) . setText ( format ) ;
if ( orcidResearcher ) {
XWPFHyperlinkRun run = mainDocumentPart . getLastParagraph ( ) . createHyperlinkRun ( " https://orcid.org/ " + orcId ) ;
run . setText ( orcId ) ;
run . setUnderline ( UnderlinePatterns . SINGLE ) ;
run . setColor ( " 0000FF " ) ;
mainDocumentPart . getLastParagraph ( ) . createRun ( ) . setText ( " ) " ) ;
}
hasMultiplicityItems = false ;
} else {
XWPFParagraph paragraph = addParagraphContent ( format , mainDocumentPart , ( ( BaseFieldDataFileTransformerModel ) field . getData ( ) ) . getFieldType ( ) . equals ( FieldType . RICH_TEXT_AREA ) ? ParagraphStyle . HTML : ParagraphStyle . TEXT , numId , indent ) ;
if ( orcidResearcher ) {
XWPFHyperlinkRun run = paragraph . createHyperlinkRun ( " https://orcid.org/ " + orcId ) ;
run . setText ( orcId ) ;
run . setUnderline ( UnderlinePatterns . SINGLE ) ;
run . setColor ( " 0000FF " ) ;
paragraph . createRun ( ) . setText ( " ) " ) ;
}
if ( paragraph ! = null ) {
2023-12-18 10:05:27 +01:00
// CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
// number.setVal(BigInteger.valueOf(indent));
2024-01-31 16:49:49 +01:00
hasValue = true ;
}
2023-12-18 10:05:27 +01:00
}
2024-01-31 16:49:49 +01:00
format = null ;
2023-12-18 10:05:27 +01:00
}
2024-01-31 16:49:49 +01:00
} else if ( values . size ( ) = = 1 ) {
format = values . get ( 0 ) ;
2023-12-18 10:05:27 +01:00
}
}
}
2024-01-31 16:49:49 +01:00
if ( format ! = null ) {
if ( hasMultiplicityItems ) {
mainDocumentPart . getLastParagraph ( ) . createRun ( ) . setText ( format ) ;
hasMultiplicityItems = false ;
hasValue = true ;
} else {
XWPFParagraph paragraph = addParagraphContent ( format , mainDocumentPart , field . getData ( ) . getFieldType ( ) . equals ( FieldType . RICH_TEXT_AREA ) ? ParagraphStyle . HTML : ParagraphStyle . TEXT , numId , indent ) ;
if ( paragraph ! = null ) {
2023-12-18 10:05:27 +01:00
// CTDecimalNumber number = paragraph.getCTP().getPPr().getNumPr().addNewIlvl();
// number.setVal(BigInteger.valueOf(indent));
2024-01-31 16:49:49 +01:00
hasValue = true ;
}
2023-12-18 10:05:27 +01:00
}
}
2024-01-31 16:49:49 +01:00
}
2023-12-18 10:05:27 +01:00
}
}
} catch ( IOException e ) {
logger . error ( e . getMessage ( ) , e ) ;
}
}
}
}
return hasValue ;
}
private String getCommaSeparatedFormatsFromJson ( String format , String attribute ) {
if ( ( format = = null | | format . isEmpty ( ) ) | | ( attribute = = null | | attribute . isEmpty ( ) ) ) {
return null ;
}
try {
2023-12-19 17:03:58 +01:00
List < Map < String , Object > > array = mapper . readValue ( format , ArrayList . class ) ;
2023-12-18 10:05:27 +01:00
StringBuilder multipleFormats = new StringBuilder ( ) ;
2023-12-19 17:03:58 +01:00
for ( int i = 0 ; i < array . size ( ) ; i + + ) {
multipleFormats . append ( array . get ( i ) . get ( attribute ) ) . append ( " , " ) ;
2023-12-18 10:05:27 +01:00
}
if ( multipleFormats . length ( ) > 0 ) {
multipleFormats . setLength ( multipleFormats . length ( ) - 2 ) ;
}
return multipleFormats . toString ( ) ;
2023-12-19 17:03:58 +01:00
} catch ( JsonProcessingException e ) {
2023-12-18 10:05:27 +01:00
return format ;
}
}
public XWPFParagraph addCellContent ( Object content , XWPFTableRow mainDocumentPart , ParagraphStyle style , BigInteger numId , int indent , int numOfRows , int numOfCells , int numOfValuesInCell ) {
if ( content ! = null ) {
if ( content instanceof String & & ( ( String ) content ) . isEmpty ( ) ) {
return null ;
}
this . indent = indent ;
XWPFTableCell cell ;
if ( numOfRows > 0 | | numOfValuesInCell > 0 ) {
cell = mainDocumentPart . getCell ( numOfCells ) ;
} else {
cell = mainDocumentPart . createCell ( ) ;
}
cell . setVerticalAlignment ( XWPFTableCell . XWPFVertAlign . valueOf ( " CENTER " ) ) ;
if ( numOfValuesInCell = = 0 ) {
cell . removeParagraph ( 0 ) ;
}
XWPFParagraph paragraph = this . optionsInTable . get ( style ) . apply ( cell , content ) ;
if ( paragraph ! = null ) {
paragraph . setAlignment ( ParagraphAlignment . CENTER ) ;
paragraph . setSpacingBefore ( 100 ) ;
if ( numId ! = null ) {
paragraph . setNumID ( numId ) ;
}
return paragraph ;
}
}
return null ;
}
public XWPFParagraph addParagraphContent ( Object content , XWPFDocument mainDocumentPart , ParagraphStyle style , BigInteger numId , int indent ) {
// this.indent = 0;
if ( content ! = null ) {
if ( content instanceof String & & ( ( String ) content ) . isEmpty ( ) ) {
return null ;
}
this . indent = indent ;
XWPFParagraph paragraph = this . options . get ( style ) . apply ( mainDocumentPart , content ) ;
if ( paragraph ! = null ) {
paragraph . setIndentFromLeft ( 400 * indent ) ;
if ( numId ! = null ) {
paragraph . setNumID ( numId ) ;
}
return paragraph ;
}
}
return null ;
}
private void addListing ( XWPFDocument document , int indent , Boolean question , Boolean hasIndication ) {
CTLvl cTLvl = this . cTAbstractNum . addNewLvl ( ) ;
String textLevel = " " ;
for ( int i = 0 ; i < = indent ; i + + ) {
textLevel + = " % " + ( i + 1 ) + " . " ;
}
if ( question ) {
cTLvl . addNewNumFmt ( ) . setVal ( STNumberFormat . DECIMAL ) ;
// cTLvl.addNewLvlText().setVal("");
cTLvl . setIlvl ( BigInteger . valueOf ( indent ) ) ;
} else if ( ! question & & hasIndication ) {
cTLvl . addNewNumFmt ( ) . setVal ( STNumberFormat . DECIMAL ) ;
// cTLvl.addNewLvlText().setVal("");
cTLvl . setIlvl ( BigInteger . valueOf ( indent ) ) ;
}
if ( ! question & & ! hasIndication ) {
cTLvl . addNewNumFmt ( ) . setVal ( STNumberFormat . NONE ) ;
// cTLvl.addNewLvlText().setVal("");
cTLvl . setIlvl ( BigInteger . valueOf ( indent ) ) ;
}
}
2024-01-25 09:17:44 +01:00
private String formatter ( FieldFileTransformerModel field ) throws IOException {
if ( field . getData ( ) . getValue ( ) = = null ) {
2023-12-18 10:05:27 +01:00
return null ;
}
2024-01-31 16:49:49 +01:00
switch ( field . getData ( ) . getFieldType ( ) ) {
2023-12-18 10:05:27 +01:00
case RESEARCHERS :
// case "projects": //TODO: Description Templatedefinition
case ORGANIZATIONS :
case EXTERNAL_DATASETS :
case DATA_REPOSITORIES :
case PUB_REPOSITORIES :
case JOURNAL_REPOSITORIES :
case TAXONOMIES :
case LICENSES :
case PUBLICATIONS :
case REGISTRIES :
case SERVICES :
case TAGS :
case CURRENCY :
2024-02-02 16:45:11 +01:00
case EXTERNAL_SELECT : {
2024-01-25 09:17:44 +01:00
mapper . configure ( DeserializationFeature . ACCEPT_SINGLE_VALUE_AS_ARRAY , true ) ;
if ( field . getData ( ) . getValue ( ) = = null ) return null ;
List < Map < String , Object > > mapList = new ArrayList < > ( ) ;
if ( ! field . getData ( ) . getValue ( ) . equals ( " " ) & & field . getData ( ) . getValue ( ) . toString ( ) ! = null ) {
try {
mapList = Arrays . asList ( mapper . readValue ( field . getData ( ) . getValue ( ) . toString ( ) , HashMap [ ] . class ) ) ;
} catch ( Exception e ) {
// logger.warn(e.getMessage(), e);
// logger.info("Moving to fallback parsing");
Map < String , Object > map = new HashMap < > ( ) ;
map . put ( " label " , field . getData ( ) . getValue ( ) . toString ( ) ) ;
mapList . add ( map ) ;
2023-12-18 10:05:27 +01:00
}
2024-01-25 09:17:44 +01:00
}
StringBuilder sb = new StringBuilder ( ) ;
int index = 0 ;
for ( Map < String , Object > map : mapList ) {
for ( Map . Entry < String , Object > entry : map . entrySet ( ) ) {
if ( entry . getValue ( ) ! = null & & ( entry . getKey ( ) . equals ( " label " ) | | entry . getKey ( ) . equals ( " description " ) | | entry . getKey ( ) . equals ( " name " ) ) ) {
sb . append ( entry . getValue ( ) ) ;
break ;
2023-12-18 10:05:27 +01:00
}
}
2024-01-25 09:17:44 +01:00
if ( index ! = mapList . size ( ) - 1 ) sb . append ( " , " ) ;
index + + ;
}
return sb . toString ( ) ;
}
2024-02-02 16:45:11 +01:00
case SELECT : {
SelectDataFileTransformerModel wordListDataEntity = ( SelectDataFileTransformerModel ) field . getData ( ) ;
2024-01-25 09:17:44 +01:00
if ( field . getData ( ) . getValue ( ) ! = null ) {
2024-02-02 16:45:11 +01:00
SelectDataFileTransformerModel . OptionFileTransformerModel selectedOption = null ;
2024-01-25 09:17:44 +01:00
if ( ! wordListDataEntity . getOptions ( ) . isEmpty ( ) ) {
2024-02-02 16:45:11 +01:00
for ( SelectDataFileTransformerModel . OptionFileTransformerModel option : wordListDataEntity . getOptions ( ) ) {
2024-01-25 09:17:44 +01:00
if ( option . getValue ( ) . equals ( field . getData ( ) . getValue ( ) ) ) {
selectedOption = option ;
2023-12-18 10:05:27 +01:00
}
}
}
2024-01-25 09:17:44 +01:00
return selectedOption ! = null ? selectedOption . getLabel ( ) : field . getData ( ) . getValue ( ) . toString ( ) ;
2023-12-18 10:05:27 +01:00
}
2024-01-25 09:17:44 +01:00
return " " ;
2023-12-18 10:05:27 +01:00
}
case BOOLEAN_DECISION :
2024-01-25 09:17:44 +01:00
if ( field . getData ( ) . getValue ( ) ! = null & & field . getData ( ) . getValue ( ) . equals ( " true " ) ) return " Yes " ;
if ( field . getData ( ) . getValue ( ) ! = null & & field . getData ( ) . getValue ( ) . equals ( " false " ) ) return " No " ;
2023-12-18 10:05:27 +01:00
return null ;
case RADIO_BOX :
2024-01-25 09:17:44 +01:00
return field . getData ( ) . getValue ( ) ! = null ? field . getData ( ) . getValue ( ) . toString ( ) : null ;
2023-12-18 10:05:27 +01:00
case CHECK_BOX :
2024-02-02 16:45:11 +01:00
LabelDataFileTransformerModel data = ( LabelDataFileTransformerModel ) field . getData ( ) ;
2024-01-25 09:17:44 +01:00
if ( field . getData ( ) . getValue ( ) = = null | | field . getData ( ) . getValue ( ) . equals ( " false " ) ) return null ;
2023-12-18 10:05:27 +01:00
return data . getLabel ( ) ;
case DATE_PICKER : {
Instant instant ;
2024-01-25 09:17:44 +01:00
if ( ! ( ( String ) field . getData ( ) . getValue ( ) ) . isEmpty ( ) ) {
2023-12-18 10:05:27 +01:00
try {
2024-01-25 09:17:44 +01:00
instant = Instant . parse ( ( String ) field . getData ( ) . getValue ( ) ) ;
2023-12-18 10:05:27 +01:00
} catch ( DateTimeParseException ex ) {
2024-01-25 09:17:44 +01:00
instant = LocalDate . parse ( ( String ) field . getData ( ) . getValue ( ) ) . atStartOfDay ( ) . toInstant ( ZoneOffset . UTC ) ;
2023-12-18 10:05:27 +01:00
}
2024-01-25 09:17:44 +01:00
return field . getData ( ) . getValue ( ) ! = null ? DateTimeFormatter . ofPattern ( " yyyy-MM-dd " ) . withZone ( ZoneId . systemDefault ( ) ) . format ( instant ) : " " ;
2023-12-18 10:05:27 +01:00
}
2024-01-25 09:17:44 +01:00
return ( String ) field . getData ( ) . getValue ( ) ;
2023-12-18 10:05:27 +01:00
}
case FREE_TEXT :
case TEXT_AREA :
case RICH_TEXT_AREA :
2024-01-25 09:17:44 +01:00
return field . getData ( ) . getValue ( ) ! = null ? field . getData ( ) . getValue ( ) . toString ( ) : " " ;
2023-12-18 10:05:27 +01:00
case DATASET_IDENTIFIER :
case VALIDATION :
2024-01-25 09:17:44 +01:00
if ( field . getData ( ) . getValue ( ) ! = null & & ! field . getData ( ) . getValue ( ) . toString ( ) . isEmpty ( ) ) {
2023-12-18 10:05:27 +01:00
Map < String , String > identifierData ;
try {
2024-01-25 09:17:44 +01:00
identifierData = mapper . readValue ( field . getData ( ) . getValue ( ) . toString ( ) , HashMap . class ) ;
2023-12-18 10:05:27 +01:00
} catch ( Exception ex ) {
// logger.warn(ex.getLocalizedMessage(), ex);
// logger.info("Reverting to custom parsing");
2024-01-25 09:17:44 +01:00
identifierData = customParse ( field . getData ( ) . getValue ( ) . toString ( ) ) ;
2023-12-18 10:05:27 +01:00
}
return " id: " + identifierData . get ( " identifier " ) + " , Type: " + identifierData . get ( " type " ) ;
}
return " " ;
}
return null ;
}
private Map < String , String > customParse ( String value ) {
Map < String , String > result = new LinkedHashMap < > ( ) ;
String parsedValue = value . replaceAll ( " [^a-zA-Z0-9 \\ s:=,] " , " " ) ;
StringTokenizer commaTokens = new StringTokenizer ( parsedValue , " , " ) ;
String delimeter = parsedValue . contains ( " = " ) ? " = " : " : " ;
while ( commaTokens . hasMoreTokens ( ) ) {
String token = commaTokens . nextToken ( ) ;
StringTokenizer delimiterTokens = new StringTokenizer ( token , delimeter ) ;
result . put ( delimiterTokens . nextToken ( ) , delimiterTokens . nextToken ( ) ) ;
}
return result ;
}
public int findPosOfPoweredBy ( XWPFDocument document ) {
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 . equals ( " Powered by " ) ) {
return document . getPosOfParagraph ( p ) - 1 ;
}
}
}
}
}
return - 1 ;
}
public void fillFirstPage ( DmpFileTransformerModel dmpEntity , DescriptionFileTransformerModel descriptionFileTransformerModelEntityEntity , XWPFDocument document , boolean isDataset ) {
int parPos = 0 ;
int descrParPos = - 1 ;
XWPFParagraph descrPar = null ;
2024-01-25 09:17:44 +01:00
List < ReferenceFileTransformerModel > grants = new ArrayList < > ( ) ;
List < ReferenceFileTransformerModel > researchers = new ArrayList < > ( ) ;
List < ReferenceFileTransformerModel > organizations = new ArrayList < > ( ) ;
List < ReferenceFileTransformerModel > funders = new ArrayList < > ( ) ;
2023-12-19 17:03:58 +01:00
if ( dmpEntity . getDmpReferences ( ) ! = null ) {
2024-01-25 09:17:44 +01:00
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 ( ) ;
2023-12-19 17:03:58 +01:00
}
2023-12-18 10:05:27 +01:00
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 ( descriptionFileTransformerModelEntityEntity ! = null & & text . contains ( " {ARGOS.DATASET.TITLE} " ) ) {
text = text . replace ( " {ARGOS.DATASET.TITLE} " , descriptionFileTransformerModelEntityEntity . getLabel ( ) ) ;
r . setText ( text , 0 ) ;
// } else if(text.equals("Description") && ((!isDataset && (dmpEntity == null || dmpEntity.getDescription() != null)) || (isDataset && (datasetEntity == null || datasetEntity.getDescription() == null)))) {
// r.setText("", 0);
} else if ( ( dmpEntity ! = null & & text . contains ( " {ARGOS.DMP.DESCRIPTION} " ) & & ! isDataset ) | | ( descriptionFileTransformerModelEntityEntity ! = null & & text . contains ( " {ARGOS.DATASET.DESCRIPTION} " ) & & isDataset ) ) {
descrParPos = parPos ;
descrPar = p ;
if ( dmpEntity ! = null & & ! isDataset ) {
text = text . replace ( " {ARGOS.DMP.DESCRIPTION} " , " " ) ;
} else if ( descriptionFileTransformerModelEntityEntity ! = null & & isDataset ) {
text = text . replace ( " {ARGOS.DATASET.DESCRIPTION} " , " " ) ;
}
r . setText ( text , 0 ) ;
} else if ( text . equals ( " {ARGOS.DMP.RESEARCHERS} " ) ) {
String researchersNames = " " ;
int i = 0 ;
2024-01-25 09:17:44 +01:00
for ( ReferenceFileTransformerModel researcher : researchers ) {
2023-12-18 10:05:27 +01:00
i + + ;
researchersNames + = researcher . getLabel ( ) + ( i < researchers . size ( ) ? " , " : " " ) ;
}
text = text . replace ( " {ARGOS.DMP.RESEARCHERS} " , researchersNames ) ;
r . setText ( text , 0 ) ;
r . setFontSize ( 17 ) ;
} else if ( text . equals ( " {ARGOS.DMP.ORGANIZATIONS} " ) ) {
String organisationsNames = " " ;
int i = 0 ;
2024-01-25 09:17:44 +01:00
for ( ReferenceFileTransformerModel organisation : organizations ) {
2023-12-18 10:05:27 +01:00
i + + ;
organisationsNames + = organisation . getLabel ( ) + ( i < organizations . size ( ) ? " , " : " " ) ;
}
text = text . replace ( " {ARGOS.DMP.ORGANIZATIONS} " , organisationsNames ) ;
r . setText ( text , 0 ) ;
r . setFontSize ( 17 ) ;
}
}
}
}
parPos + + ;
}
if ( ( descrParPos ! = - 1 ) & & ( dmpEntity ! = null ) & & ( dmpEntity . getDescription ( ) ! = null ) & & ! isDataset ) {
XmlCursor cursor = descrPar . getCTP ( ) . newCursor ( ) ;
cursor . toNextSibling ( ) ;
Document htmlDoc = Jsoup . parse ( ( ( String ) dmpEntity . getDescription ( ) ) . replaceAll ( " \ n " , " <br> " ) ) ;
HtmlToWorldBuilder htmlToWorldBuilder = new HtmlToWorldBuilder ( descrPar , 0 , cursor ) ;
NodeTraversor . traverse ( htmlToWorldBuilder , htmlDoc ) ;
}
if ( ( descrParPos ! = - 1 ) & & ( descriptionFileTransformerModelEntityEntity ! = null ) & & ( descriptionFileTransformerModelEntityEntity . getDescription ( ) ! = null ) & & isDataset ) {
XmlCursor cursor = descrPar . getCTP ( ) . newCursor ( ) ;
cursor . toNextSibling ( ) ;
Document htmlDoc = Jsoup . parse ( ( ( String ) descriptionFileTransformerModelEntityEntity . getDescription ( ) ) . replaceAll ( " \ n " , " <br> " ) ) ;
HtmlToWorldBuilder htmlToWorldBuilder = new HtmlToWorldBuilder ( descrPar , 0 , cursor ) ;
NodeTraversor . traverse ( htmlToWorldBuilder , htmlDoc ) ;
}
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 ( ) . get ( 0 ) ;
XWPFRun run = p . createRun ( ) ;
run . setText ( funders . get ( 0 ) . getLabel ( ) ) ;
run . setFontSize ( 17 ) ;
p . setAlignment ( ParagraphAlignment . CENTER ) ;
}
it = tbl . getRows ( ) . iterator ( ) ;
it . next ( ) ;
if ( it . hasNext ( ) & & ! grants . isEmpty ( ) ) {
XWPFParagraph p = it . next ( ) . getCell ( 1 ) . getParagraphs ( ) . get ( 0 ) ;
XWPFRun run = p . createRun ( ) ;
String text = grants . get ( 0 ) . getLabel ( ) ;
String reference = grants . get ( 0 ) . getReference ( ) ;
if ( reference ! = null ) {
String [ ] parts = reference . split ( " :: " ) ;
text + = parts . length > 1 ? " / No " + parts [ parts . length - 1 ] : " " ;
}
run . setText ( text ) ;
run . setFontSize ( 17 ) ;
p . setAlignment ( ParagraphAlignment . CENTER ) ;
}
}
public void fillFooter ( DmpFileTransformerModel dmpEntity , DescriptionFileTransformerModel descriptionFileTransformerModelEntityEntity , XWPFDocument document , boolean isDataset ) {
document . getFooterList ( ) . forEach ( xwpfFooter - > {
List < XWPFRun > runs = xwpfFooter . getParagraphs ( ) . get ( 0 ) . 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 ) ;
}
if ( text . contains ( " {ARGOS.DATASET.TITLE} " ) & & descriptionFileTransformerModelEntityEntity ! = null ) {
text = text . replace ( " {ARGOS.DATASET.TITLE} " , descriptionFileTransformerModelEntityEntity . getLabel ( ) ) ;
r . setText ( text , 0 ) ;
}
if ( text . contains ( " {ARGOS.DMP.LICENSE} " ) ) {
try {
Map < String , String > license = ( ( Map < String , String > ) mapper . readValue ( dmpEntity . getProperties ( ) , Map . class ) . get ( " license " ) ) ;
text = text . replace ( " {ARGOS.DMP.LICENSE} " , license . get ( " pid " ) ) ;
}
catch ( JsonProcessingException | NullPointerException e ) {
text = text . replace ( " {ARGOS.DMP.LICENSE} " , " License: - " ) ;
}
r . setText ( text , 0 ) ;
}
if ( text . contains ( " {ARGOS.DMP.DOI} " ) ) {
if ( dmpEntity . getEntityDois ( ) ! = null & & ! dmpEntity . getEntityDois ( ) . isEmpty ( ) )
text = text . replace ( " {ARGOS.DMP.DOI} " , dmpEntity . getEntityDois ( ) . iterator ( ) . next ( ) . getDoi ( ) ) ;
else
text = text . replace ( " {ARGOS.DMP.DOI} " , " - " ) ;
r . setText ( text , 0 ) ;
}
if ( text . contains ( " {ARGOS.DMP.LAST_MODIFIED} " ) ) {
DateTimeFormatter formatter = DateTimeFormatter . ofPattern ( " dd/MM/yyyy " ) . withZone ( ZoneId . systemDefault ( ) ) ;
text = text . replace ( " {ARGOS.DMP.LAST_MODIFIED} " , formatter . format ( dmpEntity . getUpdatedAt ( ) ) ) ;
r . setText ( text , 0 ) ;
}
}
}
}
} ) ;
}
}