org.fatec.lpbd.projetocurriculo.controller.CurriculoHelper.java Source code

Java tutorial

Introduction

Here is the source code for org.fatec.lpbd.projetocurriculo.controller.CurriculoHelper.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fatec.lpbd.projetocurriculo.controller;

import com.itextpdf.text.Chunk;
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.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.fatec.lpbd.projetocurriculo.model.Employee.AcademicHistory;
import org.fatec.lpbd.projetocurriculo.model.Employee.Email;
import org.fatec.lpbd.projetocurriculo.model.Employee.Employee;
import org.fatec.lpbd.projetocurriculo.model.Employee.Experience;
import org.fatec.lpbd.projetocurriculo.model.Employee.Language;
import org.fatec.lpbd.projetocurriculo.model.Employee.Phone;
import org.fatec.lpbd.projetocurriculo.model.Employee.Profile;

/**
 *
 * @author imamura
 */
public class CurriculoHelper {

    public CurriculoHelper(Employee emp, String path) {

        Document doc = null;
        OutputStream os = null;

        try {
            //cria o documento tamanho A4, margens de 2,54cm
            doc = new Document(PageSize.A4, 52, 52, 52, 52);

            //cria a stream de sada
            os = new FileOutputStream(path);

            try {
                //associa a stream de sada ao
                PdfWriter.getInstance(doc, os);
            } catch (DocumentException ex) {
                Logger.getLogger(CurriculoHelper.class.getName()).log(Level.SEVERE, null, ex);
            }

            //abre o documento
            doc.open();

            //adiciona o texto ao PDF
            String fullName = String.format("%1s %2s", emp.getName(), emp.getLastName());

            doc.add(titleString(fullName));
            doc.add(separatorText("INFORMAES PESSOAIS"));
            doc.add(lineSeparator());
            doc.add(simpleText("    - Idade: " + emp.getAge()));
            doc.add(simpleText(streetStringFormat(emp)));
            doc.add(simpleText(cityStringFormat(emp)));
            doc.add(simpleText(phonesStringFormat(emp)));
            doc.add(simpleText(emailStringFormat(emp)));

            doc.add(separatorText("OBJETIVO"));
            doc.add(lineSeparator());
            doc.add(simpleText(objectivesStringFormat(emp)));

            doc.add(separatorText("IDIOMA"));
            doc.add(lineSeparator());
            doc.add(simpleText(languagesStringFormat(emp)));

            doc.add(separatorText("FORMAO"));
            doc.add(lineSeparator());
            doc.add(simpleText(academicsStringFormat(emp)));

            doc.add(separatorText("EXPERINCIA PROFISSIONAL"));
            doc.add(lineSeparator());
            doc.add(simpleText(experiencesStringFormat(emp)));

        } catch (FileNotFoundException | DocumentException ex) {
            Logger.getLogger(CurriculoHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (doc != null) {
                //fechamento do documento
                doc.close();
            }
            if (os != null) {
                try {
                    //fechamento da stream de sada
                    os.close();
                } catch (IOException ex) {
                    Logger.getLogger(CurriculoHelper.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }

    public static Paragraph simpleText(String text) {
        Font f = new Font(FontFamily.COURIER, 10, Font.NORMAL);
        Paragraph p = new Paragraph(text, f);
        p.setAlignment(Element.PARAGRAPH);
        return p;
    }

    public static Paragraph titleString(String text) {
        Font f = new Font(FontFamily.COURIER, 20, Font.BOLD);
        Paragraph p = new Paragraph(text, f);
        p.setAlignment(Element.ALIGN_CENTER);
        p.setSpacingAfter(20);
        return p;
    }

    public static Paragraph separatorText(String text) {
        Font f = new Font(FontFamily.COURIER, 12, Font.BOLD);
        Paragraph p = new Paragraph(text, f);
        //        p.setSpacingBefore(5);
        p.setSpacingAfter(-15);
        return p;
    }

    public static Chunk lineSeparator() {
        LineSeparator ls = new LineSeparator();
        return (new Chunk(ls));
    }

    public static Paragraph spaceLine() {
        //        Font f = new Font(FontFamily.COURIER, 14, Font.BOLD);
        Paragraph p = new Paragraph("");
        //        p.setSpacingAfter(-15);
        return p;
    }

    public static String cityStringFormat(Employee emp) {
        String formStr = String.format("    - Cidade: %1s/%3s / Bairro: %2s ", emp.getAddress().getCity(),
                emp.getAddress().getNeighborhood(), emp.getAddress().getState());
        return formStr;
    }

    public static String streetStringFormat(Employee emp) {
        String formStr = String.format("    - %1s n %2s ", emp.getAddress().getStreet(),
                emp.getAddress().getNumber());
        return formStr;
    }

    public static String phonesStringFormat(Employee emp) {
        String formStr = "    - Telefone: ";
        List<Phone> all = emp.getPhones();
        for (Phone ph : all) {
            formStr = formStr + ph.getType() + ": " + ph.getNumber() + " ";
        }
        return formStr;
    }

    public static String emailStringFormat(Employee emp) {
        String formStr = "    - Email: ";
        List<Email> all = emp.getEmails();
        for (Email em : all) {
            formStr = formStr + em.getEmail() + " / ";
        }
        return formStr;
    }

    public static String objectivesStringFormat(Employee emp) {
        String formStr = "    ";
        List<Profile> all = emp.getProfiles();
        for (Profile pf : all) {
            formStr = formStr + pf.getProfile() + " / ";
        }
        return formStr;
    }

    public static String languagesStringFormat(Employee emp) {
        String formStr = "    ";
        List<Language> all = emp.getLanguages();
        for (Language lg : all) {
            formStr = formStr + lg.getLanguage() + "  - Nivel: " + lg.getFluency() + "\n    ";
        }
        return formStr;
    }

    public static String academicsStringFormat(Employee emp) {
        String formStr = "    ";
        List<AcademicHistory> all = emp.getAcademics();
        for (AcademicHistory aca : all) {
            formStr = formStr + "Instituio: " + aca.getInstitution() + "\n    Curso: " + aca.getCourse()
                    + "\n    Status: " + aca.getStatus() + "\n    Data de Inicio: " + aca.getStarDate()
                    + "\n    Data de Concluso: " + aca.getEndDate() + "\n\n    ";
        }
        return formStr;
    }

    public static String experiencesStringFormat(Employee emp) {
        String formStr = "    ";
        List<Experience> all = emp.getExperiences();
        for (Experience exp : all) {
            formStr = formStr + "Empresa: " + exp.getCompanyName() + "\n    Funo: " + exp.getDescription()
                    + "\n    Data de inicio: " + exp.getStarDate() + "\n    Data da sada: " + exp.getEndDate()
                    + "\n\n    ";
        }
        return formStr;
    }
}