com.solidmaps.webapp.report.RequerimentAlterLicenseFederalPDF.java Source code

Java tutorial

Introduction

Here is the source code for com.solidmaps.webapp.report.RequerimentAlterLicenseFederalPDF.java

Source

package com.solidmaps.webapp.report;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;

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.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.solidmaps.webapp.entity.LicensePFEntity;
import com.solidmaps.webapp.entity.ProductOfficialEntity;
import com.solidmaps.webapp.enuns.RequerimentFederalEnum;
import com.solidmaps.webapp.utils.DateUtils;

/**
 * Formulrio: PF01 - incluso produto
 * 
 * @author brunorocca
 *
 */
public class RequerimentAlterLicenseFederalPDF {

    private String filePath;

    private final String SEPARATOR_DOUBLE = "\r\n\r\n";
    private final String SEPARATOR = "\r\n";

    private final Font FONT_PARAGRAPH = new Font(FontFamily.UNDEFINED, 8, Font.NORMAL);
    private final Font FONT_HEADER = new Font(FontFamily.UNDEFINED, 8, Font.BOLD);
    private final Font FONT_PROTOCOL = new Font(FontFamily.UNDEFINED, 11, Font.BOLD, BaseColor.LIGHT_GRAY);
    private final Font FONT_BIG_HEADER_BOLD = new Font(FontFamily.UNDEFINED, 11, Font.BOLD);
    private final Font FONT_BIG_HEADER = new Font(FontFamily.UNDEFINED, 11);

    public RequerimentAlterLicenseFederalPDF(String filePath) {
        this.filePath = filePath;
    }

    public String generate(LicensePFEntity license, RequerimentFederalEnum type,
            List<ProductOfficialEntity> listProducts) {

        if (listProducts == null || listProducts.isEmpty()) {
            return null;
        }

        Document doc = new Document();
        PdfWriter docWriter = null;
        String fileDocPath = "";

        try {

            fileDocPath = this.createDocument(doc, docWriter, license);
            this.createTable(doc, type, license, listProducts);

        } catch (DocumentException dex) {
            dex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (doc != null) {
                // close the document
                doc.close();
            }
            if (docWriter != null) {
                // close the writer
                docWriter.close();
            }
        }

        return fileDocPath;
    }

    private String createDocument(Document doc, PdfWriter docWriter, LicensePFEntity license)
            throws FileNotFoundException, DocumentException {

        String fileName = "Comunicado de alterao cadastral - Cnpj: " + license.getCompany().getCnpj() + ".pdf";

        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(filePath + fileName));

        // document header attributes
        doc.addAuthor("EnforceMaps");
        doc.addCreationDate();
        doc.addProducer();
        doc.addCreator("EnforceMaps");
        doc.addTitle("Comunicado de alterao cadastral: ");
        doc.setPageSize(PageSize.A4);

        // open document
        doc.open();

        return fileName;
    }

    private void createTable(Document doc, RequerimentFederalEnum type, LicensePFEntity license,
            List<ProductOfficialEntity> listProducts) throws DocumentException {

        // create PDF table with the given widths
        PdfPTable table = new PdfPTable(4);
        // set table width a percentage of the page width
        table.setWidthPercentage(100f);

        this.createCompany(table, license);

        // Alterao Cadastral
        this.createAlteracaoCadastral(table, type);

        // Descrio
        this.insertHeaderCell(table, "3 - DESCREVER AS PRINCIPAIS ALTERAES OCORRIDAS", 4);
        this.insertCell(table, this.createProductsText(listProducts, type), 4);

        // Uso Oficial
        this.insertCellProtocol(table, license);

        // Footer
        this.createFooter(table);

        // Table pai
        PdfPTable tableFather = new PdfPTable(1);
        tableFather.setWidthPercentage(100f);
        PdfPCell cellFather = new PdfPCell(table);
        cellFather.setBorderWidth(2);
        tableFather.addCell(cellFather);

        doc.add(this.createHeader());
        doc.add(tableFather);
    }

    private void createAlteracaoCadastral(PdfPTable table, RequerimentFederalEnum type) {
        this.insertHeaderCell(table, "2 - ALTERAO CADASTRAL", 4);

        // Itens de alterao
        this.insertCell(table, "", 4);
        this.insertCellAlteracao(table, "COM TAXA   (Portaria 1.274/03 - Art. 6 )");
        this.insertCellAlteracao(table, "SEM TAXA");

        this.insertCellAlteracaoOption(table, "01", "RAZO SOCIAL",
                RequerimentFederalEnum.RAZAO_SOCIAL.equals(type));
        this.insertCellAlteracaoOption(table, "07", "RESPONS?VEL TCNICO",
                RequerimentFederalEnum.RESPONSAVEL_TECNICO.equals(type));
        this.insertCellAlteracaoOption(table, "02", "INSCRIO ESTADUAL",
                RequerimentFederalEnum.INCRICAO.equals(type));
        this.insertCellAlteracaoOption(table, "08", "ALTERAO DA COMPOSIO DO PRODUTO",
                RequerimentFederalEnum.COMPOSICAO_PRODUTOS.equals(type));
        this.insertCellAlteracaoOption(table, "03", "ENDEREOS", RequerimentFederalEnum.ENDERECOS.equals(type));
        this.insertCellAlteracaoOption(table, "09", "INCLUSO DE PRODUTO QU?MICO",
                RequerimentFederalEnum.INCLUSAO_PRODUTO.equals(type));
        this.insertCellAlteracaoOption(table, "04", "QUADRO SOCIAL",
                RequerimentFederalEnum.QUADRO_SOCIAL.equals(type));
        this.insertCellAlteracaoOption(table, "10", "EXCLUSO DE PRODUTO QU?MICO",
                RequerimentFederalEnum.EXCLUSAO_PRODUTO.equals(type));
        this.insertCellAlteracaoOption(table, "05", "REPRESENTANTE LEGAL",
                RequerimentFederalEnum.REPRESENTANTE_LEGAL.equals(type));
        this.insertCellAlteracaoOption(table, "11", "FROTA", RequerimentFederalEnum.FROTA.equals(type));
        this.insertCellAlteracaoOption(table, "06", "MUDANA DE ATIVIDADE (CNAE)",
                RequerimentFederalEnum.MUDANCA_CNAE.equals(type));
        this.insertCellAlteracaoOption(table, "12", "FILIAL", RequerimentFederalEnum.FILIAL.equals(type));
        this.insertCellAlteracaoOption(table, "", "", false);
        this.insertCellAlteracaoOption(table, "13", "OUTROS (especificar)",
                RequerimentFederalEnum.OUTROS.equals(type));

    }

    private void createCompany(PdfPTable table, LicensePFEntity license) {
        // Dados da empresa
        this.insertHeaderCell(table, "1 - DADOS GERAIS DA EMPRESA OU PESSOA F?SICA", 4);

        // Linha 1
        this.insertCell(table, "NOME/RAZO SOCIAL" + SEPARATOR_DOUBLE + license.getCompany().getName(), 2);
        this.insertCell(table, "CNPJ/CPF" + SEPARATOR_DOUBLE + license.getCompany().getCnpjFormatted(), 1);
        this.insertCell(table, "CLF/AE" + SEPARATOR_DOUBLE + license.getCompany().getCodCnae(), 1);

        // Linha 2
        this.insertCell(table, "ENDEREO" + SEPARATOR_DOUBLE + license.getCompany().getStreet(), 3);
        this.insertCell(table, "BAIRRO / DISTRITO" + SEPARATOR_DOUBLE + license.getCompany().getCity(), 1);

        // Linha 3
        this.insertCell(table, "MUNIC?PIO" + SEPARATOR_DOUBLE + license.getCompany().getCity(), 1);
        this.insertCell(table, "UF" + SEPARATOR_DOUBLE + license.getCompany().getState(), 1);
        this.insertCell(table, "CEP" + SEPARATOR_DOUBLE + license.getCompany().getCep(), 1);
        this.insertCell(table, "INSCRIO ESTADUAL" + SEPARATOR_DOUBLE + license.getCompany().getNumInscription(),
                1);

        // Linha 3
        this.insertCell(table, "DDD" + SEPARATOR_DOUBLE + license.getCompany().getPhoneNumber(), 1);
        this.insertCell(table, "TELEFONE" + SEPARATOR_DOUBLE + license.getCompany().getPhoneNumber(), 1);
        this.insertCell(table, "FAX" + SEPARATOR_DOUBLE + license.getCompany().getFaxNumber(), 1);
        this.insertCell(table, "E-MAIL" + SEPARATOR_DOUBLE + license.getCompany().getUserResponsable().getEmail(),
                1);

        // Linha 3
        this.insertCell(table, "ATIVIDADE PRINCIPAL (DE ACORDO COM A CLASSIFICAO CNAE FISCAL)"
                + SEPARATOR_DOUBLE + license.getCompany().getCodCnae(), 3);
        this.insertCell(table, "CDIGO CNAE FISCAL" + SEPARATOR_DOUBLE + license.getCompany().getCodCnae(), 1);
    }

    private void createFooter(PdfPTable table) {
        this.insertHeaderCell(table, "Portaria 1.274/03", 4);

        Paragraph artigo = new Paragraph("Art. 6", FONT_HEADER);
        Paragraph text1 = new Paragraph(
                "A pessoa jurdica possuidora de Certificado de Registro Cadastral dever", FONT_PARAGRAPH);
        Paragraph text2 = new Paragraph(" comunicar ao DPF, no prazo de trinta dias, todo e qualquer "
                + "fato que justifique a atualizao de seu cadastro", FONT_HEADER);
        Paragraph text3 = new Paragraph(
                ", mediante preenchimento de formulrio prprio (Anexo VI). " + SEPARATOR_DOUBLE, FONT_PARAGRAPH);

        Paragraph text4 = new Paragraph("Pargrafo nico", FONT_HEADER);
        Paragraph text5 = new Paragraph("O pedido de atualizao do registro cadastral dever ser",
                FONT_PARAGRAPH);
        Paragraph text6 = new Paragraph(
                "formalizado no prazo mximo de noventa dias, a partir da data do comunicado", FONT_HEADER);
        Paragraph text7 = new Paragraph(" a que se refere o caput, por meio de requerimento (Anexo IV).",
                FONT_PARAGRAPH);

        Paragraph paragraphFull = new Paragraph();
        paragraphFull.add(artigo);
        paragraphFull.add(text1);
        paragraphFull.add(text2);
        paragraphFull.add(text3);
        paragraphFull.add(text4);
        paragraphFull.add(text5);
        paragraphFull.add(text6);
        paragraphFull.add(text7);

        PdfPCell cell = new PdfPCell(paragraphFull);
        cell.setColspan(4);

        table.addCell(cell);

    }

    private void insertCell(PdfPTable table, String text, Integer colSpan) {

        if (text == null) {
            text = "";
        }

        // create a new cell with the specified Text and Font
        PdfPCell cell = new PdfPCell(new Phrase(text.trim(), FONT_PARAGRAPH));
        // set the cell alignment
        // in case there is no text and you wan to create an empty row
        if (text.trim().equalsIgnoreCase("")) {
            cell.setMinimumHeight(10f);
        }

        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setColspan(colSpan);
        cell.setPadding(5f);

        // add the call to the table
        table.addCell(cell);
    }

    private void insertCellProtocol(PdfPTable table, LicensePFEntity license) {

        PdfPTable tableProtocol = new PdfPTable(1);
        tableProtocol.setWidthPercentage(100f);

        Paragraph officialPhrase = new Paragraph("USO OFICIAL" + SEPARATOR, FONT_PARAGRAPH);
        officialPhrase.setAlignment(Element.ALIGN_LEFT);

        Paragraph protocolPhrase = new Paragraph("ETIQUETA PROTOCOLO" + SEPARATOR_DOUBLE + SEPARATOR_DOUBLE
                + SEPARATOR_DOUBLE + SEPARATOR_DOUBLE + SEPARATOR_DOUBLE + SEPARATOR, FONT_PROTOCOL);
        protocolPhrase.setAlignment(Element.ALIGN_LEFT);

        Paragraph obsPhrase = new Paragraph("Observao Importante!" + SEPARATOR, FONT_HEADER);

        Paragraph obsStringPhrase = new Paragraph(
                "Protocolar este Anexo somente quando no conjugado com a alterao efetiva ou a renovao da licena.",
                FONT_PARAGRAPH);

        Paragraph fullPhrase = new Paragraph();
        fullPhrase.add(officialPhrase);
        fullPhrase.add(protocolPhrase);
        fullPhrase.add(obsPhrase);
        fullPhrase.add(obsStringPhrase);

        PdfPCell cell = new PdfPCell(fullPhrase);

        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPadding(5f);

        tableProtocol.addCell(cell);

        PdfPCell cellProtocol = new PdfPCell(tableProtocol);
        cellProtocol.setColspan(2);

        table.addCell(cellProtocol);
        table.addCell(this.createCellEtiqueta(license));
    }

    private PdfPCell createCellEtiqueta(LicensePFEntity license) {
        PdfPTable tableProtocol = new PdfPTable(3);
        tableProtocol.setWidthPercentage(100f);

        this.insertCell(tableProtocol, "LOCAL E DATA" + SEPARATOR_DOUBLE + license.getCompany().getState() + ", "
                + DateUtils.format(Calendar.getInstance()), 3);

        this.insertCell(tableProtocol,
                "REPRESENTANTE LEGAL" + SEPARATOR_DOUBLE + license.getCompany().getUserResponsable().getName(), 3);

        this.insertCell(tableProtocol, "IDENTIDADE (N - RGO - UF)" + SEPARATOR_DOUBLE
                + license.getCompany().getUserResponsable().getRg(), 2);
        this.insertCell(tableProtocol,
                "CPF" + SEPARATOR_DOUBLE + license.getCompany().getUserResponsable().getCpf(), 1);

        this.insertCell(tableProtocol,
                "DDD" + SEPARATOR_DOUBLE + license.getCompany().getUserResponsable().getPhoneDDD(), 1);
        this.insertCell(tableProtocol,
                "TELEFONE" + SEPARATOR_DOUBLE + license.getCompany().getUserResponsable().getPhoneNumber(), 1);
        this.insertCell(tableProtocol,
                "FAX" + SEPARATOR_DOUBLE + license.getCompany().getUserResponsable().getNumFax(), 1);

        this.insertCell(tableProtocol, "ASSINATURA" + SEPARATOR_DOUBLE + "________________________________________",
                3);

        PdfPCell cell = new PdfPCell(tableProtocol);
        cell.setColspan(4);

        return cell;
    }

    private void insertCellAlteracao(PdfPTable table, String text) {

        if (text == null) {
            text = "";
        }

        // create a new cell with the specified Text and Font
        PdfPCell cell = new PdfPCell(new Phrase(text.trim(), FONT_HEADER));
        // set the cell alignment
        // in case there is no text and you wan to create an empty row
        if (text.trim().equalsIgnoreCase("")) {
            cell.setMinimumHeight(10f);
        }

        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(2);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);

        // add the call to the table
        table.addCell(cell);
    }

    private void insertCellAlteracaoOption(PdfPTable table, String number, String text, Boolean selected) {

        String fullText = "";
        if (selected) {
            fullText += number + " |X|" + " " + text;
        } else {
            fullText += number + " |  |" + " " + text;
        }

        if (StringUtils.isBlank(number) && StringUtils.isBlank(text)) {
            fullText = "";
        }

        // create a new cell with the specified Text and Font
        PdfPCell cell = new PdfPCell(new Phrase(fullText.trim(), FONT_PARAGRAPH));
        // set the cell alignment
        // in case there is no text and you wan to create an empty row
        if (fullText.trim().equalsIgnoreCase("")) {
            cell.setMinimumHeight(10f);
        }

        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setColspan(2);
        cell.setBorder(Rectangle.RIGHT);
        cell.setPaddingTop(10f);
        cell.setPaddingBottom(10f);
        cell.setPaddingLeft(25f);
        cell.setPaddingRight(25f);

        // add the call to the table
        table.addCell(cell);
    }

    private void insertHeaderCell(PdfPTable table, String text, Integer colSpan) {

        if (text == null) {
            text = "";
        }

        // create a new cell with the specified Text and Font
        PdfPCell cell = new PdfPCell(new Phrase(text.trim(), FONT_HEADER));
        // set the cell alignment
        // in case there is no text and you wan to create an empty row
        if (text.trim().equalsIgnoreCase("")) {
            cell.setMinimumHeight(10f);
        }

        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setColspan(colSpan);
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
        cell.setBorderWidth(2);
        cell.setPadding(3f);

        // add the call to the table
        table.addCell(cell);
    }

    private Paragraph createHeader() {
        Paragraph anexo = new Paragraph("ANEXO VI" + SEPARATOR + "COMUNICADO DE ALTERAO CADASTRAL",
                FONT_BIG_HEADER_BOLD);
        anexo.setAlignment(Element.ALIGN_CENTER);

        Paragraph law = new Paragraph("( Lei n 10.357, de 27 de dezembro de 2001)" + SEPARATOR_DOUBLE,
                FONT_BIG_HEADER);
        law.setAlignment(Element.ALIGN_CENTER);

        Paragraph full = new Paragraph();
        full.add(anexo);
        full.add(law);

        return full;
    }

    private String createProductsText(List<ProductOfficialEntity> listProducts, RequerimentFederalEnum type) {
        StringBuilder sbProduct = new StringBuilder();

        if (type.equals(RequerimentFederalEnum.INCLUSAO_PRODUTO)) {
            sbProduct.append("Por favor, incluir os produtos abaixo: ").append(SEPARATOR_DOUBLE);
        }

        if (type.equals(RequerimentFederalEnum.EXCLUSAO_PRODUTO)) {
            sbProduct.append("Por favor, excluir os produtos abaixo: ").append(SEPARATOR_DOUBLE);
        }

        for (ProductOfficialEntity product : listProducts) {
            sbProduct.append("- ").append(product.getName()).append(SEPARATOR);
        }

        return sbProduct.toString();
    }

}