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

Java tutorial

Introduction

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

Source

 package com.solidmaps.webapp.report;

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

 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.CompanyEntity;
 import com.solidmaps.webapp.entity.LicensePFEntity;
 import com.solidmaps.webapp.entity.UserEntity;
 import com.solidmaps.webapp.enuns.EnableRequerimentSubjectFederalEnum;
 import com.solidmaps.webapp.enuns.EnableRequerimentTypeFederalEnum;
 import com.solidmaps.webapp.utils.DateUtils;

 /**
  * Formulrio: COMUNICADO DE ALTERAO CADASTRAL
  * 
  * @author brunorocca
  *
  */
 public class EnableCompanyRequerimentFederalPDF {

     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 EnableCompanyRequerimentFederalPDF(String filePath) {
         this.filePath = filePath;
     }

     public void generate(LicensePFEntity license, EnableRequerimentTypeFederalEnum type,
             EnableRequerimentSubjectFederalEnum subject, UserEntity user) {

         Document doc = new Document();
         PdfWriter docWriter = null;

         try {

             this.createDocument(doc, docWriter, license);
             this.createTable(doc, type, subject, license, user);

         } 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();
             }
         }

     }

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

         String fileName = "Requerimento de Habilitao de PJ - 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("Requerimento de Habilitao de PJ");
         doc.setPageSize(PageSize.A4);

         // open document
         doc.open();

         return fileName;
     }

     private void createTable(Document doc, EnableRequerimentTypeFederalEnum type,
             EnableRequerimentSubjectFederalEnum subject, LicensePFEntity license, UserEntity user)
             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);

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

         // Dados da Empresa
         this.createCompany(table, license);

         // Dados do Representante
         this.createRepresentant(table, user);

         // Deferimento
         this.createDeferiment(table, license.getCompany());

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

         // 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, EnableRequerimentTypeFederalEnum type, 
      EnableRequerimentSubjectFederalEnum subject) {
   // Itens de alterao
     this.insertCellAlteracao(table, "1 - TIPO DE REQUERIMENTO");
     this.insertCellAlteracao(table, "2 - ASSUNTO");
        
     PdfPTable tableTipoRequerimento = new PdfPTable(2);
        
     this.insertCellAlteracaoOption(tableTipoRequerimento, "01", "EMISSO", EnableRequerimentTypeFederalEnum.EMISSAO.equals(type));
     this.insertCellAlteracaoOption(tableTipoRequerimento, "04" , "RESTITUIO", EnableRequerimentTypeFederalEnum.RESTITUIO.equals(type));
     this.insertCellAlteracaoOption(tableTipoRequerimento, "02", "RENOVAO", EnableRequerimentTypeFederalEnum.RENOVACAO.equals(type));
     this.insertCellAlteracaoOption(tableTipoRequerimento, "05", "ALTERAO", EnableRequerimentTypeFederalEnum.ALTERAO.equals(type));
     this.insertCellAlteracaoOption(tableTipoRequerimento, "03", "2 VIA", EnableRequerimentTypeFederalEnum.VIA_2.equals(type));
     this.insertCellAlteracaoOption(tableTipoRequerimento, "06", "CANCELAMENTO", EnableRequerimentTypeFederalEnum.CANCELAMENTO.equals(type));
        
     PdfPTable tableAssunto = new PdfPTable(1);
     this.insertCellAlteracaoOption(tableAssunto, "01", "CERTIFICADO DE REGISTRO CADASTRAL", 
           EnableRequerimentSubjectFederalEnum.CADASTRAL.equals(subject));
     this.insertCellAlteracaoOption(tableAssunto, "02", "CERTIFICADO DE LICENA DE FUNCIONAMENTO", 
           EnableRequerimentSubjectFederalEnum.FUNCIONAMENTO.equals(subject));
        
     PdfPCell cellAssunto = new PdfPCell(tableAssunto);
     cellAssunto.setColspan(2);
     cellAssunto.setBorder(Rectangle.LEFT);
        
     PdfPCell cellAlteracao = new PdfPCell(tableTipoRequerimento);
     cellAlteracao.setColspan(2);
     cellAlteracao.setBorder(Rectangle.RIGHT);
      
   table.addCell(cellAlteracao);
   table.addCell(cellAssunto);
}

     private void createCompany(PdfPTable table, LicensePFEntity license) {
         // Dados da empresa
         this.insertHeaderCell(table, "3 - 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 createRepresentant(PdfPTable table, UserEntity user) {
         // Dados da empresa
         this.insertHeaderCell(table, "4 - DADOS DO REPRESENTANTE LEGAL", 4);

         this.insertCell(table, "NOME" + SEPARATOR_DOUBLE + user.getName(), 4);

         this.insertCell(table, "CPF" + SEPARATOR_DOUBLE + user.getCpf(), 2);
         this.insertCell(table, "IDENTIDADE (N - RGO - UF)" + SEPARATOR_DOUBLE + user.getRg(), 2);

         this.insertCell(table, "DDD" + SEPARATOR_DOUBLE + user.getPhoneDDD(), 1);
         this.insertCell(table, "TELEFONE" + SEPARATOR_DOUBLE + user.getPhoneNumber(), 1);
         this.insertCell(table, "FAX" + SEPARATOR_DOUBLE + user.getNumFax(), 1);
         this.insertCell(table, "E-MAIL" + SEPARATOR_DOUBLE + user.getEmail(), 1);

     }

     private void createDeferiment(PdfPTable table, CompanyEntity company) {
         // Dados da empresa
         this.insertHeaderCell(table, "5 - PEDE DEFERIMENTO", 4);

         this.insertCell(table, "LOCAL E DATA" + SEPARATOR_DOUBLE + company.getState() + " - "
                 + DateUtils.format(Calendar.getInstance()), 4);

         this.insertCell(table,
                 "ASSINATURA DO REPRESENTANTE LEGAL" + SEPARATOR_DOUBLE + "_______________________________________",
                 4);

         this.insertCell(table,
                 SEPARATOR_DOUBLE
                         + "Declaro, sob as penas da Lei, que as informaes cadastrais esto atualizadas e "
                         + "traduzem a expresso da verdade." + SEPARATOR_DOUBLE,
                 4);

     }

     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) {

         this.insertHeaderCell(table, "6- CONTROLE DE RECEBIMENTO DO PROCESSO", 4);

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

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

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

         Paragraph obsStringPhrase = new Paragraph("- Etiqueta com data e assinatura." + SEPARATOR
                 + "- Renovao: verificar a data de vencimento da licena." + SEPARATOR
                 + "- Alterao Cadastral: apresentar o Anexo VI." + SEPARATOR_DOUBLE, FONT_PARAGRAPH);

         Paragraph phraseProtocol = new Paragraph();
         phraseProtocol.add(officialPhrase);
         phraseProtocol.add(protocolPhrase);

         Paragraph phraseObs = new Paragraph();
         phraseObs.add(obsPhrase);
         phraseObs.add(obsStringPhrase);

         PdfPCell cellProtocol = new PdfPCell(phraseProtocol);
         cellProtocol.setColspan(2);
         cellProtocol.disableBorderSide(Rectangle.RIGHT);

         PdfPCell cellObs = new PdfPCell(phraseObs);
         cellObs.setColspan(2);
         cellObs.disableBorderSide(Rectangle.LEFT);

         table.addCell(cellProtocol);
         table.addCell(cellObs);
     }

     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);
         cell.setBorderWidth(2);
         cell.setPadding(3f);

         // 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.setBorder(Rectangle.RIGHT);
         cell.setPaddingTop(10f);
         cell.setPaddingBottom(10f);
         cell.setPaddingLeft(10f);
         cell.setPaddingRight(10f);

         // 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 IV" + SEPARATOR + "REQUERIMENTO - HABILITAO DE PESSOA JUR?DICA",
                 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;
     }

 }