Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ReportGeneratorPDF;
- import Modelos.*;
- import Persistencia.ReportManager;
- import com.itextpdf.html2pdf.HtmlConverter;
- import com.itextpdf.io.image.ImageData;
- import com.itextpdf.io.image.ImageDataFactory;
- import com.itextpdf.kernel.colors.*;
- import com.itextpdf.kernel.font.PdfFont;
- import com.itextpdf.kernel.font.PdfFontFactory;
- import com.itextpdf.kernel.geom.PageSize;
- import com.itextpdf.kernel.geom.Rectangle;
- import com.itextpdf.kernel.pdf.PdfDocument;
- import com.itextpdf.kernel.pdf.PdfPage;
- import com.itextpdf.kernel.pdf.PdfWriter;
- import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
- import com.itextpdf.kernel.pdf.xobject.PdfXObject;
- import com.itextpdf.layout.Canvas;
- import com.itextpdf.layout.Document;
- import com.itextpdf.layout.element.*;
- import com.itextpdf.layout.element.Image;
- import com.itextpdf.layout.layout.LayoutArea;
- import com.itextpdf.layout.layout.LayoutContext;
- import com.itextpdf.layout.layout.LayoutResult;
- import com.itextpdf.layout.property.TextAlignment;
- import com.itextpdf.layout.property.UnitValue;
- import com.itextpdf.layout.property.VerticalAlignment;
- import sun.reflect.generics.reflectiveObjects.NotImplementedException;
- import javax.servlet.Servlet;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletOutputStream;
- import java.nio.file.Paths;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
- public class ReportGenerator {
- /* Constant definitions */
- private static final String imagePath = "/img/pdf_resources";
- private final DeviceRgb C_HEADER_LIGHT_GRAY = new DeviceRgb(230,230,230);
- private final DeviceRgb C_HEADER_LIGHT_BORDER = new DeviceRgb(170,170,170);
- private final DeviceRgb CORPORATIVE_COLOR = new DeviceRgb(170,34,24);
- private final String HEADER_TEXT = "KDNA Genomics";
- private HashMap<Integer, PdfXObject> HashedPdfXObjectCache = null;
- /* Function to check if set of IBlockElement is empty or not */
- protected boolean CheckIfStringEmpty(String str)
- {
- Boolean result = str.replace("\u00A0","")
- .replace("\u2007","")
- .replace("\u202F","")
- .trim().isEmpty();
- return result;
- }
- protected boolean CheckIfBlockEmpty(IElement e) {
- /* Get all the text */
- if (e instanceof Text) return CheckIfStringEmpty(((Text) e).getText());
- else
- {
- BlockElement blk = (BlockElement) e;
- Queue<IElement> elements = new LinkedList<>();
- elements.addAll(blk.getChildren());
- while (!elements.isEmpty())
- {
- IElement el = elements.poll();
- if (el instanceof Text)
- {
- if (!CheckIfStringEmpty(((Text) el).getText()))
- return false;
- }
- else
- {
- blk = (BlockElement) el;
- elements.addAll(blk.getChildren());
- }
- }
- }
- return true;
- }
- protected boolean CheckIfBlocksEmpty(List<IBlockElement> elements)
- {
- for (IElement e : elements) {
- if (!CheckIfBlockEmpty(e)) return false;
- }
- return true;
- }
- /*
- Function to sanitize category string
- */
- protected String Util_SanitizeString(String categoryString){
- if (categoryString.contains(":")){
- return categoryString.substring(0,categoryString.indexOf(":"));
- }else if (categoryString.contains("(")){
- return categoryString.substring(0,categoryString.indexOf("("));
- }else{
- return categoryString;
- }
- }
- /*
- Generic function for drawing a list item with/out icon
- */
- private void DrawGeneric_ListIcon(Canvas canvas, ServletContext context, String title, String iconPath, float x, float y){
- try {
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- if (iconPath != null)
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath + "/" + iconPath)), x, y, 45, false); // Background
- int fontSize = 20;
- // Text of the item
- PdfFont font = PdfFontFactory.createFont();
- Paragraph paragraph = new Paragraph(title.substring(0,1));
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFont(font);
- paragraph.setFontSize(fontSize);
- paragraph.setFixedPosition(x + 70, y, width);
- paragraph.setWidth(font.getWidth(title.substring(0,1),fontSize)+1);
- paragraph.setHeight(45);
- paragraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- paragraph.setTextAlignment(TextAlignment.LEFT);
- paragraph.setBold();
- canvas.add(paragraph);
- Paragraph oldP = paragraph;
- paragraph = new Paragraph(title.substring(1).toUpperCase());
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(fontSize-5);
- paragraph.setFixedPosition(x + 70 + oldP.getWidth().getValue(), y-2, width);
- paragraph.setHeight(45);
- paragraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- paragraph.setTextAlignment(TextAlignment.LEFT);
- canvas.add(paragraph);
- pCanvas.rectangle(x, y - 10, 500, 1).setColor(C_HEADER_LIGHT_GRAY, true).fill(); // Header red stroke
- }catch (Exception e){
- }
- }
- /*
- Generic function for a Content-Page
- */
- private void DrawGeneric_ContentPage(Canvas canvas, ServletContext context, String title, String pageNumber, Boolean drawTopBar){
- try {
- float xMidOfPage = canvas.getRootArea().getWidth()/2;
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- /* Page decoration */
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- pCanvas.rectangle(0,0, 60, height).setColor(C_HEADER_LIGHT_GRAY, true).fill();
- if (drawTopBar) {
- pCanvas.roundRectangle(-100, height - 130, width + 50, 50, 10).setColor(C_HEADER_LIGHT_BORDER, true).fill();
- pCanvas.roundRectangle(-101, height - 129, width + 50, 48, 10).setColor(C_HEADER_LIGHT_GRAY, true).fill();
- }
- pCanvas.rectangle(0, height-75, canvas.getRootArea().getWidth(), 8).setColor(CORPORATIVE_COLOR,true).fill(); // Header red stroke
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/image17.png")), 70, height-60, 40, false); // Background
- /* Header watermark*/
- Paragraph paragraph = new Paragraph(HEADER_TEXT);
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(13);
- paragraph.setFixedPosition(width-140, height-60, 140);
- paragraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(paragraph);
- /* Page title */
- if (drawTopBar) {
- Paragraph tParagraph = new Paragraph(title);
- tParagraph.setFontColor(new DeviceRgb(64, 91, 195));
- tParagraph.setBold();
- tParagraph.setFontSize(25);
- tParagraph.setFixedPosition(0, height - 125, width - 50);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(tParagraph);
- }
- /* Separation line */
- pCanvas.rectangle(70,40, width-100, 1).setColor(C_HEADER_LIGHT_GRAY,true).fill(); // Header red stroke
- /* Page number */
- Paragraph nParagraph = new Paragraph("Página | " + pageNumber);
- nParagraph.setFontColor(ColorConstants.DARK_GRAY);
- nParagraph.setFontSize(11);
- nParagraph.setFixedPosition(width-100, 15, 100);
- nParagraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(nParagraph);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- private void DrawGeneric_ContentPage(Canvas canvas, ServletContext context, String title, String pageNumber){
- DrawGeneric_ContentPage(canvas, context, title, pageNumber, true);
- }
- /* Ad-hoc code to draw pages*/
- /*
- Function to draw the header page
- */
- private void Draw_HeaderPage(Canvas canvas, Informe report, ServletContext context){
- try {
- float xMidOfPage = canvas.getRootArea().getWidth()/2;
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- pCanvas.rectangle(0, 670-150, canvas.getRootArea().getWidth(), 200).setColor(ColorConstants.LIGHT_GRAY,true).fill(); // Gray rect
- pCanvas.circle(xMidOfPage, 730, 90).setColor(ColorConstants.WHITE,true).fill(); // Circle to protect the logo
- canvas.add(new Image(ImageDataFactory.createJpeg(context.getResource(imagePath+"/image4.jpeg")))); // Header image
- pCanvas.rectangle(0, height-10, canvas.getRootArea().getWidth(), 10).setColor(CORPORATIVE_COLOR,true).fill(); // Header red stroke
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/image6.png")), xMidOfPage-65, 670, 130, false); // Logo
- pCanvas.rectangle(0, 670-130, canvas.getRootArea().getWidth(), 40).setColor(ColorConstants.WHITE,true).fill(); // Gray rect
- // Text header of the very first page
- Paragraph paragraph = new Paragraph("ANÁLISIS GENÉTICO");
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(25);
- paragraph.setFixedPosition(0, 670-130, width);
- paragraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(paragraph);
- PdfXObject footer = AddImageCaching(pCanvas,ImageDataFactory.createJpeg(context.getResource(imagePath+"/image1.jpeg")), 0, 0, width, false); // Footer
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/background.png")), 0, 100, width, false); // Background
- // Feature image in the header
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/image3.png")), xMidOfPage-150, 100, 300, false); // Background
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- /*
- Function to draw the last page
- */
- private void Draw_BackPage(Canvas canvas, Informe report, ServletContext context){
- try {
- float xMidOfPage = canvas.getRootArea().getWidth()/2;
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- pCanvas.rectangle(0, 670-150, canvas.getRootArea().getWidth(), 200).setColor(ColorConstants.LIGHT_GRAY,true).fill(); // Gray rect
- pCanvas.circle(xMidOfPage, 730, 90).setColor(ColorConstants.WHITE,true).fill(); // Circle to protect the logo
- canvas.add(new Image(ImageDataFactory.createJpeg(context.getResource(imagePath+"/image4.jpeg")))); // Header image
- pCanvas.rectangle(0, height-10, canvas.getRootArea().getWidth(), 10).setColor(CORPORATIVE_COLOR,true).fill(); // Header red stroke
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/image6.png")), xMidOfPage-65, 670, 130, false); // Logo
- PdfXObject footer = AddImageCaching(pCanvas,ImageDataFactory.createJpeg(context.getResource(imagePath+"/image1.jpeg")), 0, 0, width, false); // Footer
- // Feature image in the header
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/image3.png")), xMidOfPage-150, 115, 300, false);
- /* Text A */
- pCanvas.roundRectangle(xMidOfPage-250, height-265,500, 50, 10).setColor(C_HEADER_LIGHT_BORDER, true).fill();
- pCanvas.roundRectangle(xMidOfPage-250, height-264,500, 48, 10).setColor(C_HEADER_LIGHT_GRAY, true).fill();
- Paragraph paragraph = new Paragraph();
- Text txA_1 = new Text("LOS");
- Text txA_2 = new Text(" AVANCES CIENTÍFICOS");
- txA_2.setFontColor(CORPORATIVE_COLOR);
- Text txA_3 = new Text(" SON CRÍTICOS PARA MEJORAR TU RENDIMIENTO");
- paragraph.add(txA_1);
- paragraph.add(txA_2);
- paragraph.add(txA_3);
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(12);
- paragraph.setFixedPosition(0, height-265, width);
- paragraph.setHeight(50);
- paragraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- paragraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(paragraph);
- /* Text B */
- pCanvas.roundRectangle(xMidOfPage-(450/2), height-330,450, 50, 10).setColor(C_HEADER_LIGHT_BORDER, true).fill();
- pCanvas.roundRectangle(xMidOfPage-(450/2), height-329,450, 48, 10).setColor(C_HEADER_LIGHT_GRAY, true).fill();
- paragraph = new Paragraph();
- Text txB_1 = new Text("CONOCER");
- paragraph.add(txB_1);
- Text txB_2 = new Text(" TU GENÉTICA");
- paragraph.add(txB_2);
- txB_2.setFontColor(CORPORATIVE_COLOR);
- Text txB_3 = new Text(" ES ESENCIAL PARA PERSONALIZAR\n TU ENTRENAMIENTO Y TU NUTRICIÓN");
- paragraph.add(txB_3);
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(12);
- paragraph.setFixedPosition(0, height-325, width);
- paragraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(paragraph);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- /*
- Draw index page
- */
- private void Draw_IndexPage(Canvas canvas, java.util.List<Categoria> categories, ServletContext context, int pn) {
- try {
- float xMidOfPage = canvas.getRootArea().getWidth() / 2;
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- DrawGeneric_ContentPage(canvas, context, "CONTENIDO", Integer.toString(pn));
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- float x = 100;
- float y = height-210;
- DrawGeneric_ListIcon(canvas, context, "Introducción","image7.png",x,y);
- y-=60;
- DrawGeneric_ListIcon(canvas, context, "Resumen","image8.png",x,y);
- y-=60;
- if (categories.size() > 8){
- throw new Exception("This page is not prepared for more than 8 categories");
- }
- for (Categoria category: categories) {
- DrawGeneric_ListIcon(canvas, context, Util_SanitizeString(category.getNombre()), null, x, y);
- y-=60;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /*
- Draw intro page
- */
- private void Draw_IntroPage(Canvas canvas, ServletContext context, int pn) {
- try {
- float xMidOfPage = canvas.getRootArea().getWidth() / 2;
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- ImageData data = ImageDataFactory.createPng(context.getResource(imagePath+"/background_intro.png"));
- AddImageCaching(pCanvas,data, -50, 0, width+300, false); // Background
- DrawGeneric_ContentPage(canvas, context, "INTRODUCCIÓN", Integer.toString(pn));
- // Paragraph 1
- Paragraph paragraph = new Paragraph("El informe incluye el resultado de tu análisis genético para cada fenotipo o característica estudiada, de forma sencilla y accesible.\n");
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(18);
- paragraph.setFixedPosition(65, height-200, width-65);
- paragraph.setTextAlignment(TextAlignment.LEFT);
- canvas.add(paragraph);
- float rect_width = 500;
- float rect_height = 300;
- float rect_x = ((width-rect_width)/2)+30;
- float rect_y = (height-rect_height)/2;
- // Text protection in center
- pCanvas.roundRectangle(rect_x-1,rect_y-1,rect_width+2,rect_height+2, 3).setColor(C_HEADER_LIGHT_BORDER,true).fill();
- pCanvas.roundRectangle(rect_x,rect_y,rect_width,rect_height, 3).setColor(C_HEADER_LIGHT_GRAY,true).fill();
- // Warning
- paragraph = new Paragraph("AVISO IMPORTANTE:");
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontColor(ColorConstants.RED);
- paragraph.setFontSize(15);
- paragraph.setUnderline();
- paragraph.setFixedPosition(rect_x+10, rect_y+rect_height-35, rect_width-10);
- paragraph.setTextAlignment(TextAlignment.LEFT);
- canvas.add(paragraph);
- // Paragraph 1
- paragraph = new Paragraph(
- "Las \"tendencias individuales\" reportadas se basan en los resultados de cientos de estudios científicos realizados en condiciones específicas en cuanto a origen étnico, edad, sexo y estado de forma de los participantes.\n\n" +
- "Así mismo, los fenotipos o características analizadas, además de por la composición genética de cada individuo, están influidas por un complejo grupo de factores no genéticos. Por lo tanto, el presente resultado ha de interpretarse en términos probabilísticos (comparación con la población en general, con el resto de la población y percentil), en ningún caso en términos deterministas.");
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(15);
- paragraph.setFixedPosition(rect_x+10, rect_y, rect_width-10);
- paragraph.setHeight(rect_height-50);
- paragraph.setTextAlignment(TextAlignment.LEFT);
- canvas.add(paragraph);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /*
- Draw category page
- */
- private int Draw_CategoryPage(PdfDocument pdfReport, Canvas canvas, java.util.List<Categoria> categories, Categoria category, java.util.List<ValorInforme> viFenotypes, ReportManager manager, ServletContext context, int pn) {
- try {
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- float xMidOfPage=((width+60)/2);
- final DeviceRgb circleColor = new DeviceRgb(71,155,223);
- Paragraph tParagraph;
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- DrawGeneric_ContentPage(canvas, context, "ANÁLISIS: " + Util_SanitizeString(category.getNombre()), Integer.toString(pn));
- /* Image for the category */
- String url = Paths.get(context.getRealPath("/profile"), category.getUrlFoto().trim()).normalize().toString();
- ImageData data = ImageDataFactory.create(url);
- PdfXObject obj = AddImageCaching(pCanvas,data, xMidOfPage-150, height-340, 300, false);
- /* Draw rectangle for every fenotype */
- float x = xMidOfPage-350/2;
- float y = height-425;
- int numOfFenotypes = 0;
- int numOfRows = 0;
- // Draw rounded rectangle
- float rectWidth = categories.size()*50;
- float xcatRect = xMidOfPage-(rectWidth/2);
- float ycatRect = height-370;
- pCanvas.roundRectangle(xcatRect, ycatRect, rectWidth, 20, 3).setColor(C_HEADER_LIGHT_BORDER, true).fill();
- tParagraph = new Paragraph(category.getNombre());
- tParagraph.setFontColor(ColorConstants.WHITE);
- tParagraph.setBold();
- tParagraph.setFontSize(10);
- tParagraph.setFixedPosition(xcatRect, ycatRect, rectWidth);
- tParagraph.setHeight(20);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- tParagraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- canvas.add(tParagraph);
- float xrect = xcatRect;
- xrect = drawCategories(canvas, categories, category, pCanvas, ycatRect, xrect);
- for (ValorInforme vi : viFenotypes){
- if (numOfFenotypes > 6){
- // Break the list
- java.util.List<ValorInforme> subPhenotypes = viFenotypes.subList(numOfRows,viFenotypes.size());
- if (subPhenotypes.size() == 0) break;
- if (subPhenotypes.stream().allMatch(e -> e.getBarraPoblacion() == null)) break;
- // Split into another page, break this for
- Canvas catPage = CreateCanvasFromDocument(pdfReport);
- return Draw_CategoryPage(pdfReport, catPage,categories, category, subPhenotypes, manager, context,pn+1);
- }
- numOfRows++;
- if (vi.getBarraUsuario() == null) continue;
- DeviceRgb rectColor = null;
- switch (vi.getSemResumen()){
- case 0: // Green
- rectColor = new DeviceRgb(76,175,80);
- break;
- case 1: // Orange
- rectColor = new DeviceRgb(255,152,0);
- break;
- case 2: // Red
- rectColor = new DeviceRgb(244,67,54);
- break;
- }
- y-=50;
- if (rectColor != null) {
- // Text bar
- pCanvas.roundRectangle(xMidOfPage - 260, y, 350, 40, 3).setColor(rectColor, true).fill();
- // Text
- tParagraph = new Paragraph(vi.getFenotipo().getNombre().replace("\n", "").replace("\r", ""));
- tParagraph.setFontColor(ColorConstants.WHITE);
- tParagraph.setBold();
- tParagraph.setFontSize(10);
- tParagraph.setFixedPosition(xMidOfPage - 252.5f, y, 335);
- tParagraph.setHeight(40);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- //tParagraph.setBackgroundColor(ColorConstants.BLUE);
- tParagraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- canvas.add(tParagraph);
- // Percentage gradient bar
- ImageData rb_data = ImageDataFactory.createPng(context.getResource(imagePath+"/bar_resource.png"));
- AddImageCaching(pCanvas,rb_data, width-173, y+7, 100, false);
- // User icon
- ImageData uicon = ImageDataFactory.createPng(context.getResource(imagePath+"/user.png"));
- AddImageCaching(pCanvas,uicon, width-173 + (vi.getBarraUsuario()), y+7, 10, false);
- // Rest of users icon
- ImageData ruicon = ImageDataFactory.createPng(context.getResource(imagePath+"/restUsers.png"));
- AddImageCaching(pCanvas,ruicon, width-173 + (vi.getBarraPoblacion()), y+7, 10, false);
- // Circular progress bar
- float xarg = width-60;
- float widtharg = 40;
- // Draw the internal circle
- pCanvas.circle(xarg+widtharg/2, y+widtharg/2, (widtharg/2)).setColor(ColorConstants.BLACK,false).stroke();
- pCanvas.circle(xarg+widtharg/2, y+widtharg/2, (widtharg/2)-(widtharg*0.1)).setColor(ColorConstants.BLACK,false).stroke();
- pCanvas.arc(xarg+2, y+2, xarg+widtharg-2,y+widtharg-2, 90, vi.getPercentil()*(-3.6)).setLineWidth(widtharg*0.1f).setStrokeColor(circleColor).stroke();
- pCanvas.setLineWidth(1);
- // Draw the text for the percentage
- tParagraph = new Paragraph(vi.getPercentil().toString()+"%");
- tParagraph.setFontColor(ColorConstants.BLACK);
- tParagraph.setBold();
- tParagraph.setFontSize(10);
- tParagraph.setFixedPosition(xarg+1, y+2, widtharg);
- tParagraph.setHeight(34);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- tParagraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- canvas.add(tParagraph);
- }
- numOfFenotypes++;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return pn;
- }
- /*
- Draw intro page 2
- */
- private void Draw_IntroPage2(Canvas canvas, ServletContext context, int pn) {
- try {
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- DrawGeneric_ContentPage(canvas, context, "INTRODUCCIÓN", Integer.toString(pn));
- // Logo at start
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/image11.png")), 80, height-190, 100, false); // Background
- // Zone to protect text
- pCanvas.rectangle(80, height-230,430,30).setColor(ColorConstants.LIGHT_GRAY,true).fill();
- // Text of the header
- Paragraph paragraph = new Paragraph("MI GENÉTICA");
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setBold();
- paragraph.setFontSize(18);
- paragraph.setFixedPosition(90, height-230, 400);
- paragraph.setTextAlignment(TextAlignment.LEFT);
- canvas.add(paragraph);
- /*
- A-Part
- */
- AddImageCaching(pCanvas,ImageDataFactory.createJpeg(context.getResource(imagePath+"/image12.jpeg")), 92, height-336, 130, false); // Background
- paragraph = new Paragraph();
- Text fPart = new Text("Indica el");
- paragraph.add(fPart);
- fPart = new Text(" tamaño del efecto ");
- fPart.setBold();
- paragraph.add(fPart);
- fPart = new Text("de la influencia genética del usuario en comparación con la registrada para la población en general. \n" +
- "\nEsto es, en qué grado la influencia genética del usuario ejerce un efecto mayor o menor que el registrado para la población en general.");
- paragraph.add(fPart);
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(12);
- paragraph.setFixedPosition(240, height-350, width-250);
- paragraph.setTextAlignment(TextAlignment.JUSTIFIED);
- canvas.add(paragraph);
- pCanvas.rectangle(70, height-375, width-70, 1).setColor(C_HEADER_LIGHT_GRAY, true).fill(); // Header red stroke
- /*
- B-Part
- */
- paragraph = new Paragraph();
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/bars.png")), 92, height-536, 130, false); // Background
- fPart = new Text("Indica un ");
- paragraph.add(fPart);
- fPart = new Text("resumen ");
- fPart.setBold();
- paragraph.add(fPart);
- fPart = new Text("del tamaño del efecto de la influencia genética del usuario en comparación con la población en general. \n\n" +
- "Así mismo, en aquellos fenotipos en los que el estado de la Investigación en Genética de la Nutrición y el Deporte lo permiten, se representa también el dato numérico que indica ");
- paragraph.add(fPart);
- fPart = new Text("el tamaño del efecto");
- fPart.setBold();
- paragraph.add(fPart);
- fPart = new Text(" de la influencia genética del usuario.");
- paragraph.add(fPart);
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(12);
- paragraph.setFixedPosition(240, height-550, width-250);
- paragraph.setTextAlignment(TextAlignment.JUSTIFIED);
- canvas.add(paragraph);
- pCanvas.rectangle(70, height-580, width-70, 1).setColor(C_HEADER_LIGHT_GRAY, true).fill(); // Header red stroke
- /*
- C-Part
- */
- paragraph = new Paragraph();
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/image13.png")), 92, height-736, 130, false); // Background
- fPart = new Text("El percentil representa la valoración de la influencia genética del usuario para este fenotipo, en comparación con la del resto de individuos de la población, ");
- paragraph.add(fPart);
- fPart = new Text("ordenada de menor a mayor.");
- fPart.setBold();
- paragraph.add(fPart);
- fPart = new Text("Esto es, ");
- paragraph.add(fPart);
- fPart = new Text("indica que porcentaje de la población presenta una mayor o una menor influencia genética ");
- fPart.setBold();
- paragraph.add(fPart);
- fPart = new Text("para el fenotipo analizado.\n\n" +
- "Por ejemplo, el percentil 20 indica que el valor de la influencia genética del usuario es superior al registrado por el 20 por ciento de la población e inferior al registrado por el 80 por ciento de la población.");
- paragraph.add(fPart);
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(12);
- paragraph.setFixedPosition(240, height-780, width-250);
- paragraph.setTextAlignment(TextAlignment.JUSTIFIED);
- canvas.add(paragraph);
- pCanvas.rectangle(70, height-580, width-70, 1).setColor(C_HEADER_LIGHT_GRAY, true).fill(); // Header red stroke
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /*
- Draw summary intro page
- */
- private void Draw_AnalysisIntro(Canvas canvas, ServletContext context, int pn) {
- try {
- float xMidOfPage = canvas.getRootArea().getWidth()/2;
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- /* Page decoration */
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- pCanvas.rectangle(0,0, 60, height).setColor(C_HEADER_LIGHT_GRAY, true).fill();
- pCanvas.roundRectangle(-100, (height/2)-25,width+50, 50, 10).setColor(new DeviceRgb(65,97,142), true).fill();
- pCanvas.roundRectangle(-101, (height/2)-24,width+50, 48, 10).setColor(new DeviceRgb(35,67,112), true).fill();
- pCanvas.rectangle(0, height-75, canvas.getRootArea().getWidth(), 8).setColor(CORPORATIVE_COLOR,true).fill(); // Header red stroke
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/image17.png")), 70, height-60, 40, false); // Background
- /* Header watermark*/
- Paragraph paragraph = new Paragraph(HEADER_TEXT);
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(13);
- paragraph.setFixedPosition(width-140, height-60, 140);
- paragraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(paragraph);
- /* Page title */
- Paragraph tParagraph = new Paragraph("ANÁLISIS: RESUMEN");
- tParagraph.setFontColor(ColorConstants.WHITE);
- tParagraph.setBold();
- tParagraph.setFontSize(25);
- tParagraph.setFixedPosition(0, (height/2)-20, width-50);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(tParagraph);
- /* Separation line */
- pCanvas.rectangle(70,40, width-100, 1).setColor(C_HEADER_LIGHT_GRAY,true).fill(); // Header red stroke
- /* Page number */
- Paragraph nParagraph = new Paragraph("Página | " + pn);
- nParagraph.setFontColor(ColorConstants.DARK_GRAY);
- nParagraph.setFontSize(11);
- nParagraph.setFixedPosition(width-100, 15, 100);
- nParagraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(nParagraph);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- /*
- Draw detail intro page
- */
- private void Draw_AnalysisIntroDetail(Canvas canvas, ServletContext context, int pn) {
- try {
- float xMidOfPage = canvas.getRootArea().getWidth()/2;
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- /* Page decoration */
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- pCanvas.rectangle(0,0, 60, height).setColor(C_HEADER_LIGHT_GRAY, true).fill();
- pCanvas.roundRectangle(-100, (height/2)-25,width+50, 50, 10).setColor(new DeviceRgb(65,97,142), true).fill();
- pCanvas.roundRectangle(-101, (height/2)-24,width+50, 48, 10).setColor(new DeviceRgb(35,67,112), true).fill();
- pCanvas.rectangle(0, height-75, canvas.getRootArea().getWidth(), 8).setColor(CORPORATIVE_COLOR,true).fill(); // Header red stroke
- AddImageCaching(pCanvas,ImageDataFactory.createPng(context.getResource(imagePath+"/image17.png")), 70, height-60, 40, false); // Background
- /* Header watermark*/
- Paragraph paragraph = new Paragraph(HEADER_TEXT);
- paragraph.setFontColor(ColorConstants.BLACK);
- paragraph.setFontSize(13);
- paragraph.setFixedPosition(width-140, height-60, 140);
- paragraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(paragraph);
- /* Page title */
- Paragraph tParagraph = new Paragraph("ANÁLISIS: DETALLE");
- tParagraph.setFontColor(ColorConstants.WHITE);
- tParagraph.setBold();
- tParagraph.setFontSize(25);
- tParagraph.setFixedPosition(0, (height/2)-20, width-50);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(tParagraph);
- /* Separation line */
- pCanvas.rectangle(70,40, width-100, 1).setColor(C_HEADER_LIGHT_GRAY,true).fill(); // Header red stroke
- /* Page number */
- Paragraph nParagraph = new Paragraph("Página | " + pn);
- nParagraph.setFontColor(ColorConstants.DARK_GRAY);
- nParagraph.setFontSize(11);
- nParagraph.setFixedPosition(width-100, 15, 100);
- nParagraph.setTextAlignment(TextAlignment.CENTER);
- canvas.add(nParagraph);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- /*
- Draw detail page 1
- */
- private int Draw_DetailPage1(Canvas canvas, List<IBlockElement> htmlElements, java.util.List<Categoria> categories, Categoria category, ValorInforme phenotype, ServletContext context, int pn) {
- try {
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- float xMidOfPage=((width+60)/2);
- final DeviceRgb circleColor = new DeviceRgb(71,155,223);
- Paragraph tParagraph;
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- DrawGeneric_ContentPage(canvas, context, "", Integer.toString(pn), false);
- /* Image for the category */
- Fenotipo rPhenotype = phenotype.getFenotipo();
- String url = Paths.get(context.getRealPath("/profile"), rPhenotype.getUrlFoto()).normalize().toString().trim().replace("\n","").replace("\r","").replace("\t","");
- ImageData data = ImageDataFactory.create(url);
- AddImageCaching(pCanvas,data, xMidOfPage-150, height-280, 300, false);
- /* Draw rectangle for every fenotype */
- float y = height-405;
- // Draw rounded rectangle
- float rectWidth = categories.size()*50;
- float xcatRect = xMidOfPage-(rectWidth/2);
- float ycatRect = height-305;
- pCanvas.roundRectangle(xcatRect, ycatRect, rectWidth, 20, 3).setColor(C_HEADER_LIGHT_BORDER, true).fill();
- tParagraph = new Paragraph(category.getNombre());
- tParagraph.setFontColor(ColorConstants.WHITE);
- tParagraph.setBold();
- tParagraph.setFontSize(10);
- tParagraph.setFixedPosition(xcatRect, ycatRect, rectWidth);
- tParagraph.setHeight(20);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- tParagraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- canvas.add(tParagraph);
- xcatRect = drawCategories(canvas, categories, category, pCanvas, ycatRect, xcatRect);
- DeviceRgb rectColor = null;
- String text = "";
- switch (phenotype.getSemResumen()){
- case 0: // Green
- rectColor = new DeviceRgb(76,175,80);
- text = "Favorable";
- break;
- case 1: // Orange
- rectColor = new DeviceRgb(255,152,0);
- text = "Normal";
- break;
- case 2: // Red
- rectColor = new DeviceRgb(244,67,54);
- text = "Desfavorable";
- break;
- }
- if (rectColor != null) {
- // Text bar
- pCanvas.roundRectangle(xcatRect-(categories.size()*50), y, (categories.size()*50), 40, 3).setColor(rectColor, true).fill();
- // Text
- tParagraph = new Paragraph(phenotype.getFenotipo().getNombre().replace("\n", "").replace("\r", ""));
- tParagraph.setFontColor(ColorConstants.WHITE);
- tParagraph.setBold();
- tParagraph.setFontSize(10);
- tParagraph.setFixedPosition(xcatRect-(categories.size()*50), y, (categories.size()*50));
- tParagraph.setHeight(40);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- //tParagraph.setBackgroundColor(ColorConstants.BLUE);
- tParagraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- canvas.add(tParagraph);
- // Percentage gradient bar
- ImageData rb_data = ImageDataFactory.createPng(context.getResource(imagePath+"/bar_resource.png"));
- AddImageCaching(pCanvas,rb_data, 120, y-50, 150, false);
- // User icon
- ImageData uicon = ImageDataFactory.createPng(context.getResource(imagePath+"/user.png"));
- AddImageCaching(pCanvas,uicon, 120 + (phenotype.getBarraUsuario()*1.5f), y-50, 15, false);
- // Rest of users icon
- ImageData ruicon = ImageDataFactory.createPng(context.getResource(imagePath+"/restUsers.png"));
- AddImageCaching(pCanvas,ruicon, 120 + (phenotype.getBarraPoblacion()*1.5f), y-50, 15, false);
- pCanvas.roundRectangle(120, y-90, 150, 30, 15).setColor(rectColor, true).fill();
- tParagraph = new Paragraph(text);
- tParagraph.setFontColor(ColorConstants.WHITE);
- tParagraph.setBold();
- tParagraph.setFontSize(16);
- tParagraph.setFixedPosition(120, y-90, 150);
- tParagraph.setHeight(30);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- //tParagraph.setBackgroundColor(ColorConstants.BLUE);
- tParagraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- canvas.add(tParagraph);
- // Circular progress bar
- y-=100;
- float xarg = width-160;
- float widtharg = 80;
- // Draw the internal circle
- pCanvas.circle(xarg+widtharg/2, y+widtharg/2, (widtharg/2)).setColor(ColorConstants.BLACK,false).stroke();
- pCanvas.circle(xarg+widtharg/2, y+widtharg/2, (widtharg/2)-(widtharg*0.1)).setColor(ColorConstants.BLACK,false).stroke();
- pCanvas.arc(xarg+(widtharg*0.1f)/2, y+(widtharg*0.1f)/2, xarg+widtharg-(widtharg*0.1)/2,y+widtharg-(widtharg*0.1)/2, 90, phenotype.getPercentil()*(-3.6)).setLineWidth(widtharg*0.1f).setStrokeColor(circleColor).stroke();
- pCanvas.setLineWidth(1);
- // Draw the text for the percentage
- tParagraph = new Paragraph(phenotype.getPercentil().toString()+"%");
- tParagraph.setFontColor(ColorConstants.BLACK);
- tParagraph.setBold();
- tParagraph.setFontSize(20);
- tParagraph.setFixedPosition(xarg+1, y+2, widtharg);
- tParagraph.setHeight(widtharg);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- tParagraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- canvas.add(tParagraph);
- Integer separatorNdx = GetSectionSeparatorIndex(htmlElements);
- Integer ndx = DrawHtmlElements(canvas, htmlElements, width-20, height, 80, height-550, separatorNdx);
- if (ndx != null) return ndx;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return -1;
- }
- protected void DrawGenRow(Canvas cv, float x, float y, DeviceRgb color, float percentage){
- // Not necessary for the time being
- throw new NotImplementedException();
- }
- protected Integer DrawHtmlElements(Canvas canvas, List<IBlockElement> htmlElements, float width, float height, float leftMargin, float y) {
- return DrawHtmlElements(canvas, htmlElements, width, height, leftMargin, y, -1);
- }
- protected Integer DrawHtmlElements(Canvas canvas, List<IBlockElement> htmlElements, float width, float height, float leftMargin, float y, int breakOnNdx) {
- int ndx = 0;
- float lastY = y;
- canvas.setFontColor(ColorConstants.BLACK, 1);
- for (IBlockElement block : htmlElements)
- {
- BlockElement par = ((BlockElement) block);
- par.setRelativePosition(leftMargin, height-lastY-40, width - leftMargin, 0);
- par.setWidth(width-leftMargin);
- CorrectBulletPointDrift(width, leftMargin, block);
- LayoutResult result = block.createRendererSubTree()
- .setParent(canvas.getRenderer()).layout(new LayoutContext(new LayoutArea(1, new Rectangle(100, 1000))));
- if (ndx > 0 && (y-result.getOccupiedArea().getBBox().getHeight() < 0 || ndx == breakOnNdx)) // This makes that if a block is bigger than a page, inserts without breaking into another one
- {
- return ndx;
- }
- if (!CheckIfBlockEmpty(par)) {
- y -= result.getOccupiedArea().getBBox().getHeight();
- if (block instanceof GenPdfRow) // Render a gen
- {
- GenPdfRow row = ((GenPdfRow)block);
- DrawGenRow(canvas,width-30, y+(result.getOccupiedArea().getBBox().getHeight()), row.getGenColor(), row.getPercentage());
- }
- canvas.add(par);
- }
- ndx++;
- }
- return ndx;
- }
- protected void CorrectBulletPointDrift(float width, float margin, IElement e) {
- if (e instanceof Paragraph && ((Paragraph) e).getChildren().size() > 0)
- {
- Object posText = ((Paragraph) e).getChildren().get(0);
- if (posText instanceof Text){
- if (((Text) posText).getText().equals("•") || ((Text) posText).getText().equals("·")){
- UnitValue correction = ((Paragraph) e).getProperty(44);
- ((Paragraph)e).setWidth(width-margin-((int)correction.getValue()));
- }
- }
- }
- }
- protected List<IBlockElement> getDetailPhenotypeElements(List<IBlockElement> elements, ValorInforme phenotype)
- {
- try {
- Fenotipo rPhenotype = phenotype.getFenotipo();
- if (2 - phenotype.getSemResumen() >= 0) {
- java.util.List<IElement> elementList = HtmlConverter.convertToElements(getPhenotypeDescription(phenotype, rPhenotype));
- for (IElement e : elementList)
- if (e instanceof IBlockElement) {
- if (e instanceof Paragraph ||
- e instanceof Div ) {
- elements.add((IBlockElement) e);
- }
- }
- }
- return elements;
- } catch (Exception e) {
- return null;
- }
- }
- protected String getPhenotypeDescription(ValorInforme phenotype, Fenotipo rPhenotype) {
- String description = "";
- if (rPhenotype.getDifDescVerde() != null && rPhenotype.getDifDescAmarillo() != null){
- Float difference = phenotype.getBarraUsuario()-phenotype.getBarraPoblacion();
- if (difference < rPhenotype.getDifDescVerde()) description = rPhenotype.getDescVerde();
- else if (difference < rPhenotype.getDifDescAmarillo()) description = rPhenotype.getDescAmarillo();
- else description = rPhenotype.getDescRojo();
- }
- return description;
- }
- /*
- Draw detail page 2
- */
- private int Draw_DetailPage2(Canvas canvas, java.util.List<Categoria> categories, Categoria category, List<IBlockElement> htmlElements, ServletContext context, int pn) {
- try {
- float width = canvas.getRootArea().getWidth();
- float height = canvas.getRootArea().getHeight();
- float xMidOfPage=((width+60)/2);
- Paragraph tParagraph;
- PdfCanvas pCanvas = canvas.getPdfCanvas();
- DrawGeneric_ContentPage(canvas, context, "", Integer.toString(pn), false);
- // Draw rounded rectangle
- float rectWidth = categories.size()*50;
- float xcatRect = xMidOfPage-(rectWidth/2);
- float ycatRect = height-110;
- pCanvas.roundRectangle(xcatRect, ycatRect, rectWidth, 20, 3).setColor(C_HEADER_LIGHT_BORDER, true).fill();
- tParagraph = new Paragraph(category.getNombre());
- tParagraph.setFontColor(ColorConstants.WHITE);
- tParagraph.setBold();
- tParagraph.setFontSize(10);
- tParagraph.setFixedPosition(xcatRect, ycatRect, rectWidth);
- tParagraph.setHeight(20);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- tParagraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- canvas.add(tParagraph);
- drawCategories(canvas, categories, category, pCanvas, ycatRect, xcatRect);
- Integer ndx = DrawHtmlElements(canvas, htmlElements, width-20, height, 80, height-200);
- return ndx;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return -1;
- }
- private float drawCategories(Canvas canvas, List<Categoria> categories, Categoria category, PdfCanvas pCanvas, float ycatRect, float xrect) {
- Paragraph tParagraph;
- for (Categoria ct : categories) {
- pCanvas.roundRectangle(xrect, ycatRect - (ct == category ? 45 : 48), 50, 50, 3).setColor(ct == category ? C_HEADER_LIGHT_BORDER : C_HEADER_LIGHT_GRAY, true).fill();
- tParagraph = new Paragraph(ct.getNombre().substring(0, 1).toUpperCase());
- tParagraph.setFontColor(ColorConstants.BLACK);
- tParagraph.setBold();
- tParagraph.setFontSize(25);
- tParagraph.setFixedPosition(xrect, ycatRect - (ct == category ? 45 : 48), 50);
- tParagraph.setHeight(50);
- tParagraph.setTextAlignment(TextAlignment.CENTER);
- tParagraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
- canvas.add(tParagraph);
- xrect += 50;
- }
- return xrect;
- }
- /* Utils */
- protected Canvas CreateCanvasFromDocument(PdfDocument document){
- PdfPage page = document.addNewPage();
- PdfCanvas pdfCanvas = new PdfCanvas(page);
- return new Canvas(pdfCanvas, document, page.getPageSize());
- }
- private Integer GetSectionSeparatorIndex(List<IBlockElement> e)
- {
- Integer ndx = 0;
- for (IBlockElement bl : e)
- {
- if (bl instanceof Paragraph)
- {
- if (((Paragraph) bl).getChildren().size() > 0) {
- Text tx = (Text) ((Paragraph) bl).getChildren().get(0);
- String txChars = tx.getText().trim();
- if (txChars.startsWith("¿POR QUÉ") || txChars.startsWith("WHY")) {
- return ndx;
- }
- }
- }
- ndx++;
- }
- return -1;
- }
- /* Wrapper helping iText caching the images */
- protected PdfXObject AddImageCaching(PdfCanvas canvas, ImageData image, float x, float y, float width, boolean asInline)
- {
- if (HashedPdfXObjectCache == null)
- {
- HashedPdfXObjectCache = new HashMap<>();
- }
- PdfXObject img = HashedPdfXObjectCache.get(image.hashCode());
- if (img == null)
- {
- img = canvas.addImage(image, x, y, width, asInline);
- HashedPdfXObjectCache.put(image.hashCode(), img);
- }
- else // Ignore asInLine
- {
- canvas.addXObject(img, x, y, width);
- }
- return img;
- }
- public void GeneratorReportFromReportId(ServletOutputStream byteArrayOutputStream, Servlet servlet, ServletContext context, Long reportId){
- /* Configure the report */
- ReportManager manager = new ReportManager(servlet);
- Informe report = manager.getReportbyId(reportId);
- java.util.List<Categoria> categories = manager.getReportCategories(report.getIdInforme());
- PdfWriter writer = new PdfWriter(byteArrayOutputStream);
- PdfDocument pdfReport = new PdfDocument(writer);
- Document doc = new Document(pdfReport);
- pdfReport.setDefaultPageSize(PageSize.A4);
- /* Start with the pages */
- int pageNumber = 1;
- // Header
- Canvas headerPage = CreateCanvasFromDocument(pdfReport);
- Draw_HeaderPage(headerPage, report, context);
- // Index
- Canvas indexPage = CreateCanvasFromDocument(pdfReport);
- Draw_IndexPage(indexPage, categories, context,++pageNumber);
- // Intro Page 1
- Canvas introPage = CreateCanvasFromDocument(pdfReport);
- Draw_IntroPage(introPage, context,++pageNumber);
- // Intro Page 2
- Canvas introPage2 = CreateCanvasFromDocument(pdfReport);
- Draw_IntroPage2(introPage2, context,++pageNumber);
- // Analysis intro page
- Canvas analysisIntro = CreateCanvasFromDocument(pdfReport);
- Draw_AnalysisIntro(analysisIntro,context, ++pageNumber);
- /* DYNAMIC CONTENT STARTS HERE */
- // Use this list for caching the 'ValorInforme' query
- java.util.Map<Categoria, java.util.List<ValorInforme>> valorInformes = new java.util.HashMap<>();
- for (Categoria category: categories){
- Canvas catPage = CreateCanvasFromDocument(pdfReport);
- java.util.List<ValorInforme> vI = manager.getValoresInformeCategoryReport(report.getIdInforme(), category.getId());
- valorInformes.put(category, vI);
- pageNumber = Draw_CategoryPage(pdfReport, catPage, categories, category, vI, manager, context, ++pageNumber);
- }
- // Analysis intro page
- Canvas analysisDetailIntro = CreateCanvasFromDocument(pdfReport);
- Draw_AnalysisIntroDetail(analysisDetailIntro,context, ++pageNumber);
- List<IBlockElement> htmlElements = new LinkedList<>();
- for (Categoria category: categories){
- java.util.List<ValorInforme> vInforme = valorInformes.get(category);
- for (ValorInforme vi : vInforme){
- if (vi.getBarraUsuario() == null) continue;
- Canvas dtlPage1 = CreateCanvasFromDocument(pdfReport);
- getDetailPhenotypeElements(htmlElements, vi);
- int restNdx = 0;
- restNdx = Draw_DetailPage1(dtlPage1,htmlElements, categories, category, vi, context, pageNumber);
- while (restNdx >= 0)
- {
- htmlElements = htmlElements.subList(restNdx, htmlElements.size());
- if (!CheckIfBlocksEmpty(htmlElements)) {
- Canvas dtlPage2 = CreateCanvasFromDocument(pdfReport);
- restNdx = Draw_DetailPage2(dtlPage2, categories, category, htmlElements, context, ++pageNumber);
- }
- else
- {
- restNdx = -1;
- }
- }
- htmlElements.clear();
- }
- }
- /* DYNAMIC CONTENT ENDS HERE */
- // Last page
- Canvas backPage = CreateCanvasFromDocument(pdfReport);
- Draw_BackPage(backPage, report, context);
- pdfReport.close();
- try {
- byteArrayOutputStream.flush();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement