report.pdfs.Medical_PDF_Report.java Source code

Java tutorial

Introduction

Here is the source code for report.pdfs.Medical_PDF_Report.java

Source

package report.pdfs;
/**
 * @author Thomas Donegan
 * @number R00044989
 * @e-mail thomas.donegan@mycit.ie
 * @version 0.0.1
 */

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.TimeZone;

import beefmodule.datahandler.*;
import beefmodule.sqlhandler.SQL_Beef_Handler;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import report.barcharts.*;
import report.piecharts.*;

public class Medical_PDF_Report {
    SQL_Beef_Handler sql = new SQL_Beef_Handler();

    String end = getDate() + " " + getTime();
    private String FILE = "Reports/Beef Reports/Medical Reports/" + end + ".pdf";
    private Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);
    private Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);

    boolean show = false;

    List<Medical_Data> mData;

    String cowtag;
    String info;
    String date;
    Double medical_cost = 0.0;

    public Medical_PDF_Report() {
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(FILE));
            document.open();
            addMetaData(document);
            addTitlePage(document);
            addContent(document);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // iText allows to add metadata to the PDF which can be viewed in your Adobe
    // Reader
    // under File -> Properties
    private void addMetaData(Document document) {
        document.addTitle("Medical Overview Report");
        document.addSubject("Medical");
        document.addKeywords("Beef, Medical, Report");
        document.addAuthor("admin");
        document.addCreator(System.getProperty("user.name"));
    }

    private void addTitlePage(Document document) throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 1);
        // Lets write a big header
        preface.add(new Paragraph("Medical Overview", catFont));

        addEmptyLine(preface, 1);
        // Will create: Report generated by: _name, _date
        preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-3$
                smallBold));
        addEmptyLine(preface, 3);
        preface.add(new Paragraph("This document describes something which is very important ", smallBold));

        addEmptyLine(preface, 8);

        preface.add(new Paragraph("This document is a preliminary version and not subject to "
                + "your license agreement or any other agreement with vogella.com ;-).", redFont));

        document.add(preface);
        // Start a new page
        document.newPage();
    }

    private void addContent(Document document) throws DocumentException {
        Anchor anchor = new Anchor("Table Report", catFont);
        anchor.setName("Table Report");

        // Second parameter is the number of the chapter
        Chapter catPart = new Chapter(new Paragraph(anchor), 1);

        Paragraph paragraph = new Paragraph();

        // Add a table
        document.add(new Paragraph());
        createTable(catPart);

        // Now add all this to the document
        document.add(catPart);

        // Start a new page
        document.newPage();

        // Next section
        anchor = new Anchor("Second Chapter", catFont);
        anchor.setName("Second Chapter");

        // Second parameter is the number of the chapter
        catPart = new Chapter(new Paragraph(anchor), 2);

        // Add an image
        addEmptyLine(paragraph, 5);
        addBarChart(catPart);

        // Now add all this to the document
        document.add(catPart);

        // Start a new page
        document.newPage();

        // Next section
        anchor = new Anchor("Third Chapter", catFont);
        anchor.setName("Third Chapter");

        // Second parameter is the number of the chapter
        catPart = new Chapter(new Paragraph(anchor), 3);

        // Add an image
        addEmptyLine(paragraph, 5);
        addPieChart(catPart);

        // Now add all this to the document
        document.add(catPart);
    }

    private void createTable(Section catPart) throws BadElementException {
        PdfPTable table = new PdfPTable(5);
        table.setSpacingBefore(25);
        // t.setBorderColor(BaseColor.GRAY);
        // t.setPadding(4);
        // t.setSpacing(4);
        // t.setBorderWidth(1);

        PdfPCell c1 = new PdfPCell(new Phrase("Type"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Cow Tag"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Info"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Date"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Medical Cost"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        table.setHeaderRows(1);

        selectAll();
        getExpenses(table);

        catPart.add(table);
    }

    private void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

    private void addBarChart(Section catPart) {
        Medical_Bar_Chart_Report chart = new Medical_Bar_Chart_Report(show, "Medical Expenses Overview");
        Image To_be_Added = null;
        try {
            To_be_Added = Image.getInstance("bar_chart.png");
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        To_be_Added.setAlignment(Image.MIDDLE | Image.TEXTWRAP);
        //To_be_Added.setBorder(Image.BOX);
        //To_be_Added.setBorderWidth(15);

        catPart.add(To_be_Added);
    }

    private void addPieChart(Section catPart) {
        Medical_Pie_Chart_Report chart = new Medical_Pie_Chart_Report(show, "Comparison",
                "Medical Expenses Overview");
        Image To_be_Added = null;
        try {
            To_be_Added = Image.getInstance("pie_chart.png");
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        To_be_Added.setAlignment(Image.MIDDLE | Image.TEXTWRAP);
        //To_be_Added.setBorder(Image.BOX);
        //To_be_Added.setBorderWidth(15);

        catPart.add(To_be_Added);
    }

    private final static String getDate() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return (df.format(new Date()));
    }

    private final static String getTime() {
        DateFormat df = new SimpleDateFormat("hh-mm-ss");
        df.setTimeZone(TimeZone.getTimeZone("Ireland"));

        return (df.format(new Date()));
    }

    private void selectAll() {
        try {
            mData = sql.select_medical_data();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getExpenses(PdfPTable table) {
        //populating the tablemodel
        table.addCell("Medical");
        table.addCell("");
        table.addCell("");
        table.addCell("");
        table.addCell("");

        //populating the tablemodel
        ListIterator<Medical_Data> data_list = mData.listIterator();

        while (data_list.hasNext()) {
            Medical_Data d = data_list.next();
            cowtag = d.getCowTag();
            info = d.getInfo();
            date = d.getDate();
            medical_cost = d.getExpense();
            addToTable(table);
        }
    }

    private void addToTable(PdfPTable table) {
        table.addCell("");
        table.addCell("" + cowtag);
        table.addCell("" + info);
        table.addCell("" + date);
        table.addCell("" + medical_cost);
    }
}