<commit_before>package nl.hsleiden.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.onehippo.cms7.eforms.hst.beans.FormBean;
import com.onehippo.cms7.eforms.hst.model.AbstractField;
import com.onehippo.cms7.eforms.hst.model.Form;

public class ITextPdfForm {

    private static final Logger LOG = LoggerFactory.getLogger(ITextPdfForm.class);
    
    public InputStream createFormPdf(FormBean formBean, Form form, String introText) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
            PdfWriter.getInstance(doc, baos);

            doc.open();
            
            addTitle(formBean, doc);
            doc.add(Chunk.NEWLINE);

            addIntroduction(formBean, introText, doc);
            doc.add(Chunk.NEWLINE);

            addFormData(form, doc);
            doc.add(Chunk.NEWLINE);

            doc.close();
        } catch (DocumentException e) {
            LOG.error("exception creating pdf out of form data", e);
        }

        return new ByteArrayInputStream(baos.toByteArray());
    }

    private void addFormData(Form form, Document doc) throws DocumentException {
        List<AbstractField> fields = form.getFields();

        PdfPTable tbl = new PdfPTable(2);
        for (AbstractField abstractField : fields) {
            if (abstractField.isMultiple()) {
                addMultivalueFields(tbl, abstractField);
            } else {
                addSingleValueFields(tbl, abstractField);
            }
        }
        doc.add(tbl);
    }

    private void addSingleValueFields(PdfPTable tbl, AbstractField abstractField) {
        if (abstractField != null && abstractField.getValue() != null && !abstractField.getValue().isEmpty()) {
            addTableCell(tbl, abstractField.getName());
            addTableCell(tbl, abstractField.getValue());
        }
    }

    private void addMultivalueFields(PdfPTable tbl, AbstractField abstractField) {
        Map<String, Collection<String>> multiValues = abstractField.getMultiValues();
        for (Entry<String, Collection<String>> multivaluesEntry : multiValues.entrySet()) {
            if (multivaluesEntry.getValue().toString() != null && !multivaluesEntry.getValue().isEmpty()
                    && isRadioValueNotEmpty(abstractField)) {
                addTableCell(tbl, multivaluesEntry.getKey());
                addTableCell(tbl, multivaluesEntry.getValue().toString());
            }
        }
    }

    private boolean isRadioValueNotEmpty(AbstractField abstractField) {
        return "radiogroup".equalsIgnoreCase(abstractField.getType()) && abstractField.getValue() != null
                && !abstractField.getValue().isEmpty();
    }

    private void addTableCell(PdfPTable tbl, String content) {
        PdfPCell cell = new PdfPCell(new Phrase(content));
        cell.disableBorderSide(Rectangle.BOX);
        tbl.addCell(cell);
    }

    private void addIntroduction(FormBean formBean, String introText, Document doc)
            throws DocumentException {
        if (introText != null && !introText.isEmpty()) {
            doc.add(new Paragraph(introText));
        }
    }

    private void addTitle(FormBean formBean, Document doc) throws DocumentException {
        String formName = formBean.getFormName();
        if (formName != null && !formName.isEmpty()) {
            Font font = new Font(FontFamily.HELVETICA, 20, Font.BOLD, BaseColor.BLACK);
            Phrase titlePhrase = new Phrase(formBean.getFormName(), font);
            doc.add(titlePhrase);
            doc.add(Chunk.NEWLINE);
        }
    }
}<commit_msg>fix for radio buttons handling<commit_after>package nl.hsleiden.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.onehippo.cms7.eforms.hst.beans.FormBean;
import com.onehippo.cms7.eforms.hst.model.AbstractField;
import com.onehippo.cms7.eforms.hst.model.Form;

public class ITextPdfForm {

    private static final Logger LOG = LoggerFactory.getLogger(ITextPdfForm.class);

    public InputStream createFormPdf(FormBean formBean, Form form, String introText) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
            PdfWriter.getInstance(doc, baos);

            doc.open();

            addTitle(formBean, doc);
            doc.add(Chunk.NEWLINE);

            addIntroduction(formBean, introText, doc);
            doc.add(Chunk.NEWLINE);

            addFormData(form, doc);
            doc.add(Chunk.NEWLINE);

            doc.close();
        } catch (DocumentException e) {
            LOG.error("exception creating pdf out of form data", e);
        }

        return new ByteArrayInputStream(baos.toByteArray());
    }

    private void addFormData(Form form, Document doc) throws DocumentException {
        List<AbstractField> fields = form.getFields();

        PdfPTable tbl = new PdfPTable(2);
        for (AbstractField abstractField : fields) {
            if (abstractField.isMultiple()) {
                if ("radiogroup".equalsIgnoreCase(abstractField.getType())) {
                    addSingleValueFields(tbl, abstractField);
                } else {
                    addMultivalueFields(tbl, abstractField);
                }
            } else {
                addSingleValueFields(tbl, abstractField);
            }
        }
        doc.add(tbl);
    }

    private void addMultivalueFields(PdfPTable tbl, AbstractField abstractField) {
        Map<String, Collection<String>> multiValues = abstractField.getMultiValues();
        for (Entry<String, Collection<String>> multivaluesEntry : multiValues.entrySet()) {
            if (multivaluesEntry.getValue().toString() != null && !multivaluesEntry.getValue().isEmpty()
                    && !multivaluesEntry.getValue().toString().isEmpty()) {
                addTableCell(tbl, multivaluesEntry.getKey());
                addTableCell(tbl, multivaluesEntry.getValue().toString());
            }
        }
    }

    private void addSingleValueFields(PdfPTable tbl, AbstractField abstractField) {
        if (abstractField != null && abstractField.getValue() != null && !abstractField.getValue().isEmpty()) {
            addTableCell(tbl, abstractField.getName());
            addTableCell(tbl, abstractField.getValue());
        }
    }

    private void addTableCell(PdfPTable tbl, String content) {
        PdfPCell cell = new PdfPCell(new Phrase(content));
        cell.disableBorderSide(Rectangle.BOX);
        tbl.addCell(cell);
    }

    private void addIntroduction(FormBean formBean, String introText, Document doc) throws DocumentException {
        if (introText != null && !introText.isEmpty()) {
            doc.add(new Paragraph(introText));
        }
    }

    private void addTitle(FormBean formBean, Document doc) throws DocumentException {
        String formName = formBean.getFormName();
        if (formName != null && !formName.isEmpty()) {
            Font font = new Font(FontFamily.HELVETICA, 20, Font.BOLD, BaseColor.BLACK);
            Phrase titlePhrase = new Phrase(formBean.getFormName(), font);
            doc.add(titlePhrase);
            doc.add(Chunk.NEWLINE);
        }
    }
}
{'fraction_non_alphanumeric': 0.10862803824444188, 'fraction_numerical': 0.00391659947010713, 'mean_word_length': 2.8398938522777533, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7055321', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2463, 'n_words': 488}