model.PDFModel.java Source code

Java tutorial

Introduction

Here is the source code for model.PDFModel.java

Source

/*
 * The MIT License
 *
 * Copyright 2014 bbZoftware.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package model;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Damian Leniak
 * @version 1.0
 */
public final class PDFModel {

    private String pdfPath;
    private DateFormat dateFormat;
    private DateFormat timeFormat;
    private Date date;
    private QueryModel query = new QueryModel();
    private double totalTablePayment[] = new double[11];

    public PDFModel() throws DocumentException, FileNotFoundException, BadElementException, IOException {
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        timeFormat = new SimpleDateFormat("HH;mm;ss");
        date = new Date();
        pdfPath = "Wystawiono " + dateFormat.format(date) + " " + timeFormat.format(date) + ".pdf";
    }

    public String getPdfPath() {
        return pdfPath;
    }

    public void createDailyPDF(String fileName, String date)
            throws DocumentException, FileNotFoundException, BadElementException, IOException {
        Document pdf = new Document();
        PdfWriter.getInstance(pdf, new FileOutputStream(fileName));

        Paragraph tittle = new Paragraph("RESTAURACJA LITERA");
        tittle.setAlignment(Element.ALIGN_CENTER);

        Image logo = Image.getInstance("logo.png");
        logo.setAlignment(Element.ALIGN_RIGHT);

        Paragraph headerParagraph = new Paragraph("Raport Dzienny " + date);
        headerParagraph.setAlignment(Element.ALIGN_CENTER);

        pdf.open();
        pdf.add(logo);
        pdf.add(tittle);
        pdf.add(new Paragraph(" "));
        pdf.add(headerParagraph);
        for (int i = 0; i < 10; i++) {
            pdf.add(createTablePDFTable(i + 1, date));
        }
        pdf.add(createDailyTable(date));
        pdf.close();
        System.out.println("closed");
    }

    public PdfPTable createTablePDFTable(int counter, String raportDate) {

        JTable resultTable = query.getProductsInTablePDFTable(raportDate, counter);

        PdfPTable[] tableTable = new PdfPTable[11];
        PdfPCell cell, productNameCell, paymentCell, countCell, priceCell;

        tableTable[counter] = new PdfPTable(4);
        tableTable[counter].setSpacingBefore(5f);
        tableTable[counter].setSpacingAfter(5f);

        totalTablePayment[counter] = 0.00;
        if (resultTable != null) {
            DefaultTableModel resultTableModel = (DefaultTableModel) resultTable.getModel();
            if (resultTableModel.getRowCount() > 0) {
                cell = new PdfPCell(new Phrase("Stolik " + (counter)));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBackgroundColor(new BaseColor(80, 255, 50));
                tableTable[counter].addCell(cell);
                cell = new PdfPCell();
                cell.setBorder(Rectangle.NO_BORDER);
                tableTable[counter].addCell(cell);
                cell = new PdfPCell();
                cell.setBorder(Rectangle.NO_BORDER);
                tableTable[counter].addCell(cell);
                cell = new PdfPCell();
                cell.setBorder(Rectangle.NO_BORDER);
                tableTable[counter].addCell(cell);

                cell = new PdfPCell(new Phrase("Produkt"));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBackgroundColor(new BaseColor(80, 172, 8));
                tableTable[counter].addCell(cell);
                cell = new PdfPCell(new Phrase("Ilosc"));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBackgroundColor(new BaseColor(80, 172, 8));
                tableTable[counter].addCell(cell);
                cell = new PdfPCell(new Phrase("Cena jednostkowa"));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBackgroundColor(new BaseColor(80, 172, 8));
                tableTable[counter].addCell(cell);
                cell = new PdfPCell(new Phrase("Zaplata"));
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBackgroundColor(new BaseColor(80, 172, 8));
                tableTable[counter].addCell(cell);

                for (int i = 0; i < resultTableModel.getRowCount(); i++) {
                    productNameCell = new PdfPCell(new Phrase(resultTableModel.getValueAt(i, 0).toString(),
                            FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.NORMAL, BaseColor.BLACK)));
                    productNameCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    productNameCell.setBackgroundColor(new BaseColor(80, 135, 8));
                    tableTable[counter].addCell(productNameCell);
                    countCell = new PdfPCell(new Phrase(String.valueOf(resultTableModel.getValueAt(i, 2))));
                    countCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tableTable[counter].addCell(countCell);
                    double payment = Double.parseDouble(resultTableModel.getValueAt(i, 1).toString())
                            * Double.parseDouble(resultTableModel.getValueAt(i, 2).toString());
                    totalTablePayment[counter] += payment;
                    paymentCell = new PdfPCell(new Phrase(String.valueOf(resultTableModel.getValueAt(i, 1))));
                    paymentCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    tableTable[counter].addCell(paymentCell);
                    priceCell = new PdfPCell(new Phrase(String.valueOf(payment)));
                    priceCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    priceCell.setBackgroundColor(new BaseColor(255, 160, 160));
                    tableTable[counter].addCell(priceCell);
                }
            }
        }
        if (totalTablePayment[counter] > 0.00) {
            cell = new PdfPCell();
            cell.setBorder(Rectangle.NO_BORDER);
            tableTable[counter].addCell(cell);
            cell = new PdfPCell();
            cell.setBorder(Rectangle.NO_BORDER);
            tableTable[counter].addCell(cell);
            cell = new PdfPCell(new Phrase("SUMA:",
                    FontFactory.getFont(FontFactory.defaultEncoding, 12, Font.NORMAL, BaseColor.BLACK)));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            tableTable[counter].addCell(cell);
            cell = new PdfPCell(new Phrase(String.valueOf(totalTablePayment[counter]),
                    FontFactory.getFont(FontFactory.defaultEncoding, 12, Font.BOLD, BaseColor.BLACK)));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setBackgroundColor(new BaseColor(255, 50, 50));
            tableTable[counter].addCell(cell);
        }

        return tableTable[counter];
    }

    public PdfPTable createDailyTable(String date) throws DocumentException {
        PdfPTable dailyTable = new PdfPTable(3);
        PdfPCell cell;

        dailyTable.setSpacingBefore(5f);

        cell = new PdfPCell(new Phrase("Stolik dnia"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(new BaseColor(80, 172, 8));
        dailyTable.addCell(cell);

        cell = new PdfPCell(new Phrase("Potrawa dnia"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(new BaseColor(80, 172, 8));
        dailyTable.addCell(cell);

        cell = new PdfPCell(new Phrase("Suma rachunkw"));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBackgroundColor(new BaseColor(80, 172, 8));
        dailyTable.addCell(cell);

        double theBestTableEarnings = 0.00;
        int theBestTableNumber = 0;
        for (int i = 1; i < totalTablePayment.length; i++) {
            if (theBestTableEarnings < totalTablePayment[i]) {
                theBestTableEarnings = totalTablePayment[i];
                theBestTableNumber = i;
            }
        }
        cell = new PdfPCell(new Phrase("Stolik " + String.valueOf(theBestTableNumber)));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        dailyTable.addCell(cell);

        cell = new PdfPCell(new Phrase(query.getDailyBestSellingProduct(date)));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        dailyTable.addCell(cell);

        cell = new PdfPCell(new Phrase(query.getDailyTakings(date)));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        dailyTable.addCell(cell);

        return dailyTable;
    }
}