report.pdfs.Feed_PDF_Report.java Source code

Java tutorial

Introduction

Here is the source code for report.pdfs.Feed_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 Feed_PDF_Report {
    SQL_Beef_Handler sql = new SQL_Beef_Handler();

    String end = getDate() + " " + getTime();
    private String FILE = "Reports/Beef Reports/Feed 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<Herd_Data> hData;
    List<Feed_Data> fData;

    int herd_size = 0;
    Double feed_cost = 0.0;
    Double hay_cost = 0.0;
    Double nuts_cost = 0.0;
    Double silage_cost = 0.0;
    Double total = 0.0;
    String month;
    String year;

    public Feed_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("Feed Overview Report");
        document.addSubject("Feed");
        document.addKeywords("Beef, Feed, 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("Feed 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(8);
        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("Herd ID"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

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

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

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

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

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

        c1 = new PdfPCell(new Phrase("Total"));
        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) {
        Feed_Bar_Chart_Report chart = new Feed_Bar_Chart_Report(show, "Feed 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) {
        Feed_Pie_Chart_Report chart = new Feed_Pie_Chart_Report(show, "Comparison", "Feed 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 {
            fData = sql.select_feed_data();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

        //populating the tablemodel
        int i = 0;
        int id = 0;
        int size = fData.size();
        boolean check = false;
        while (i <= size) {
            ListIterator<Feed_Data> data_list = fData.listIterator();

            while (data_list.hasNext()) {
                Feed_Data d = data_list.next();
                if (i == d.getHerd_id()) {
                    id = d.getHerd_id();
                    check = true;
                    getFeedExpense(i);
                    total += feed_cost;
                    herd_size++;
                }
            }
            if (check == true) {
                addToTable(id, table);
            }
            hay_cost = 0.0;
            nuts_cost = 0.0;
            silage_cost = 0.0;
            herd_size = 0;
            feed_cost = 0.0;
            i++;
        }
        total = 0.0;
    }

    private void getFeedExpense(int i) {
        ListIterator<Feed_Data> data_list = fData.listIterator();

        while (data_list.hasNext()) {
            Feed_Data d = data_list.next();
            if (i == d.getHerd_id()) {
                hay_cost += d.getF_hay_expense();
                nuts_cost += d.getF_nuts_expense();
                silage_cost += d.getF_silage_expense();
                month = d.getMonth();
                year = d.getYear();
                feed_cost += hay_cost + nuts_cost + silage_cost;
            }
        }
    }

    private void addToTable(int i, PdfPTable table) {
        table.addCell("");
        table.addCell("" + i);
        table.addCell("" + hay_cost);
        table.addCell("" + nuts_cost);
        table.addCell("" + silage_cost);
        table.addCell("" + month);
        table.addCell("" + year);
        table.addCell("" + feed_cost);
    }
}