From 5658d28ff64900405c78df29706b94131be498a2 Mon Sep 17 00:00:00 2001 From: Joel Date: Tue, 23 Nov 2021 09:55:44 -0300 Subject: [PATCH] generar pdf de detalle postulante desde backend. estilo basico --- curriculumsearch/pom.xml | 7 +++++++ curriculumsearch/src/main/java/com/roshka/controller/PostulanteRRHHController.java | 22 ++++++++++++++++++++++ curriculumsearch/src/main/java/com/roshka/service/PdfGenerator.java | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ curriculumsearch/src/main/java/com/roshka/utils/Helper.java | 31 ++++++++++++++++++++++++++++--- curriculumsearch/src/main/webapp/jsp/convocatorias.jsp | 6 ++++-- curriculumsearch/src/main/webapp/jsp/detallepostulante.jsp | 5 ++++- curriculumsearch/src/main/webapp/jsp/dp.jsp | 16 ++++++++++++++++ 7 files changed, 250 insertions(+), 6 deletions(-) create mode 100644 curriculumsearch/src/main/java/com/roshka/service/PdfGenerator.java create mode 100644 curriculumsearch/src/main/webapp/jsp/dp.jsp diff --git a/curriculumsearch/pom.xml b/curriculumsearch/pom.xml index 4575652..e483d7c 100644 --- a/curriculumsearch/pom.xml +++ b/curriculumsearch/pom.xml @@ -92,6 +92,13 @@ compile + + com.itextpdf + itextpdf + 5.5.13 + + + diff --git a/curriculumsearch/src/main/java/com/roshka/controller/PostulanteRRHHController.java b/curriculumsearch/src/main/java/com/roshka/controller/PostulanteRRHHController.java index 3e75ebb..12856e1 100644 --- a/curriculumsearch/src/main/java/com/roshka/controller/PostulanteRRHHController.java +++ b/curriculumsearch/src/main/java/com/roshka/controller/PostulanteRRHHController.java @@ -20,6 +20,7 @@ import com.roshka.repositorio.ExperienciaRepository; import com.roshka.repositorio.InstitucionRepository; import com.roshka.repositorio.PostulanteRepository; import com.roshka.repositorio.TecnologiaRepository; +import com.roshka.service.PdfGenerator; import com.roshka.utils.Helper; @@ -169,5 +170,26 @@ public class PostulanteRRHHController { } } + + @GetMapping("/postulantes/{id}/pdf") + public ResponseEntity downloadPDF(@PathVariable Long id) { + // Load file from database + PdfGenerator pdf = new PdfGenerator(); + + + try { + Postulante postulante = post.findById(id) + .orElseThrow(() -> new Exception("Postulante no encontrado")); + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/pdf")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "aver.pdf" + "\"") + .body(new ByteArrayResource(pdf.generatePdfReport(postulante))); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return ResponseEntity.notFound().build(); + } + + } } \ No newline at end of file diff --git a/curriculumsearch/src/main/java/com/roshka/service/PdfGenerator.java b/curriculumsearch/src/main/java/com/roshka/service/PdfGenerator.java new file mode 100644 index 0000000..0d75e82 --- /dev/null +++ b/curriculumsearch/src/main/java/com/roshka/service/PdfGenerator.java @@ -0,0 +1,169 @@ +package com.roshka.service; + +import java.io.ByteArrayOutputStream; +import java.util.Date; +import java.util.stream.Collectors; + +import com.itextpdf.text.List; +import com.itextpdf.text.ListItem; + + +import org.springframework.stereotype.Component; + + +import com.itextpdf.text.Document; +import com.itextpdf.text.DocumentException; +import com.itextpdf.text.Element; +import com.itextpdf.text.Font; +import com.itextpdf.text.Paragraph; +import com.itextpdf.text.pdf.PdfWriter; +import com.roshka.modelo.Estudio; +import com.roshka.modelo.Experiencia; +import com.roshka.modelo.Postulante; +import com.roshka.utils.Helper; + +@Component("pdfGenerator") +public class PdfGenerator { + + private static Font COURIER = new Font(Font.FontFamily.COURIER, 20, Font.BOLD); + private static Font COURIER_MEDIUM = new Font(Font.FontFamily.COURIER, 16, Font.BOLD); + private static Font COURIER_SMALL = new Font(Font.FontFamily.COURIER, 14, Font.UNDERLINE); + + public byte[] generatePdfReport(Postulante postulante) { + + Document document = new Document(); + + try { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + PdfWriter.getInstance(document, byteArrayOutputStream); + document.open(); + + addDocTitle(document, postulante); + createTable(document,postulante); + + + document.close(); + System.out.println("------------------Your PDF Report is ready!-------------------------"); + return byteArrayOutputStream.toByteArray(); + + } catch ( DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + + } + + private void addDocTitle(Document document, Postulante postulante) throws DocumentException { + Paragraph p1 = new Paragraph(); + leaveEmptyLine(p1, 1); + p1.add(new Paragraph("Detalles del postulante", COURIER)); + p1.setAlignment(Element.ALIGN_CENTER); + document.add(p1); + + } + private void agregarEncabezado(Document document, String titulo) throws DocumentException { + + Paragraph p1 = new Paragraph(); + p1.setAlignment(Element.ALIGN_CENTER); + p1.add(new Paragraph(titulo, COURIER_MEDIUM)); + document.add(p1); + + } + private void agregarSubEncabezado(Document document, String titulo) throws DocumentException { + + Paragraph p1 = new Paragraph(); + p1.setAlignment(Element.ALIGN_CENTER); + p1.add(new Paragraph(titulo, COURIER_SMALL)); + document.add(p1); + + } + + private void agregarLabelTexto(Document document, String label, Object texto) throws DocumentException{ + if(texto == null) return; + if(texto instanceof Date) texto = Helper.formatDate((Date)texto, "dd-MM-yyyy"); + Paragraph paragraph = new Paragraph(); + paragraph.add(label + ": "); + paragraph.add(texto.toString()); + document.add(paragraph); + } + + private void createTable(Document document, Postulante postulante) throws DocumentException { + + Paragraph paragraph = new Paragraph(); + leaveEmptyLine(paragraph, 3); + //paragraph.add(); + agregarEncabezado(document, "Datos personales"); + agregarLabelTexto(document, "Nombre", postulante.getNombre() + " " + postulante.getApellido()); + agregarLabelTexto(document, "Nro de Documento", postulante.getNroDocument()); + agregarLabelTexto(document, "Correo", postulante.getCorreo()); + agregarLabelTexto(document, "Direccion", postulante.getDireccion()); + agregarLabelTexto(document, "Ciudad", postulante.getCiudad().getNombre()); + agregarLabelTexto(document, "Telefono", postulante.getTelefono()); + agregarLabelTexto(document, "Edad", Helper.calculateAge(postulante.getFechaNacimiento()) ); + agregarLabelTexto(document, "Nivel de Ingles", postulante.getNivelIngles()); + agregarLabelTexto(document, "Nacionalidad", postulante.getNacionalidad().getDescripcion()); + + agregarEncabezado(document, "Experiencias"); + + for(Experiencia ex : postulante.getExperiencias()){ + agregarSubEncabezado(document, ex.getInstitucion()); + agregarLabelTexto(document, "Fecha Inicio", ex.getFechaDesde()); + agregarLabelTexto(document, "Fecha Fin", ex.getFechaHasta()); + agregarLabelTexto(document, "Cargo", ex.getCargo()); + agregarLabelTexto(document, "Tipo Experiencia", ex.getTipoExperiencia()); + agregarLabelTexto(document, "Descripcion", ex.getDescripcion()); + agregarLabelTexto(document, "Nombre Referencia", ex.getNombreReferencia()); + agregarLabelTexto(document, "Telefono Referencia", ex.getTelefonoReferencia()); + agregarLabelTexto(document, "Motivo Salida", ex.getMotivoSalida()); + } + agregarEncabezado(document, "Estudios"); + + for(Estudio ex : postulante.getEstudios()){ + agregarSubEncabezado(document, ex.getInstitucion().getNombre()); + agregarLabelTexto(document, "Fecha Inicio", ex.getFechaDesde()); + agregarLabelTexto(document, "Fecha Fin", ex.getFechaHasta()); + agregarLabelTexto(document, "Tema de Estudio", ex.getTemaDeEstudio()); + agregarLabelTexto(document, "Tipo de Estudio", ex.getTipoDeEstudio()); + agregarLabelTexto(document, "Descripcion", ex.getEstado().toString()); + } + + agregarEncabezado(document, "Cargo al cual postula"); + + agregarList(postulante.getPostulaciones().stream() + .map(cc -> cc.getCargo().getNombre()) + .collect(Collectors.toList()), document); + + agregarEncabezado(document, "Tecnologias"); + + agregarList(postulante.getTecnologias().stream() + .map(cc -> cc.getTecnologia().getNombre()) + .collect(Collectors.toList()), + document); + + agregarEncabezado(document, "Referencias Personales"); + + agregarList(postulante.getReferencias().stream() + .map(cc -> "Nombre: " + cc.getNombre() + ". Relacion: " + cc.getRelacion() + ". Telefono: " + cc.getTelefono() ) + .collect(Collectors.toList()), + document); + + + } + + private void agregarList(java.util.List items, Document document) throws DocumentException{ + List list = new List(false); + for(String item : items){ + list.add(new ListItem(item)); + } + document.add(list); + + + } + private static void leaveEmptyLine(Paragraph paragraph, int number) { + for (int i = 0; i < number; i++) { + paragraph.add(new Paragraph(" ")); + } + } + +} diff --git a/curriculumsearch/src/main/java/com/roshka/utils/Helper.java b/curriculumsearch/src/main/java/com/roshka/utils/Helper.java index 189aa82..426686e 100644 --- a/curriculumsearch/src/main/java/com/roshka/utils/Helper.java +++ b/curriculumsearch/src/main/java/com/roshka/utils/Helper.java @@ -3,9 +3,9 @@ package com.roshka.utils; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.time.YearMonth; -import java.time.ZoneOffset; -import java.time.temporal.ChronoUnit; +import java.time.LocalDate; +import java.time.Period; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.concurrent.TimeUnit; @@ -46,6 +46,31 @@ public class Helper { return Math.round(diff/30.d) ; } + public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) { + return new java.sql.Date(dateToConvert.getTime()).toLocalDate(); + } + + public static int calculateAge(LocalDate birthDate, LocalDate currentDate) { + if ((birthDate != null) && (currentDate != null)) { + return Period.between(birthDate, currentDate).getYears(); + } else { + return 0; + } + } + + public static int calculateAge(Date birthDate) { + LocalDate currentDate = LocalDate.now(); + return calculateAge(convertToLocalDateViaSqlDate(birthDate),currentDate); + } + + public static String formatDate(LocalDate fecha, String format){ + if(fecha == null || format == null) return null; + return fecha.format(DateTimeFormatter.ofPattern(format)); + } + public static String formatDate(Date fecha, String format){ + return formatDate(convertToLocalDateViaSqlDate(fecha), format); + } + public static DBFile createFile(MultipartFile file) { // Normalize file name String fileName = StringUtils.cleanPath(file.getOriginalFilename()); diff --git a/curriculumsearch/src/main/webapp/jsp/convocatorias.jsp b/curriculumsearch/src/main/webapp/jsp/convocatorias.jsp index 8eee2a4..f1effb4 100644 --- a/curriculumsearch/src/main/webapp/jsp/convocatorias.jsp +++ b/curriculumsearch/src/main/webapp/jsp/convocatorias.jsp @@ -2,6 +2,8 @@ <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://kwonnam.pe.kr/jsp/template-inheritance" prefix="layout"%> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> + @@ -44,8 +46,8 @@ ${sta.index+1} ${convocatoria.getCargo().getNombre()} ${convocatoria.getEstado().getDescripcion()} - ${convocatoria.getFechaInicio().toString().split(" ")[0]} - ${convocatoria.getFechaFin().toString().split(" ")[0]} + + Ver postulantes diff --git a/curriculumsearch/src/main/webapp/jsp/detallepostulante.jsp b/curriculumsearch/src/main/webapp/jsp/detallepostulante.jsp index f191f7f..7578473 100644 --- a/curriculumsearch/src/main/webapp/jsp/detallepostulante.jsp +++ b/curriculumsearch/src/main/webapp/jsp/detallepostulante.jsp @@ -142,7 +142,10 @@ - + + diff --git a/curriculumsearch/src/main/webapp/jsp/dp.jsp b/curriculumsearch/src/main/webapp/jsp/dp.jsp new file mode 100644 index 0000000..5d0a8fd --- /dev/null +++ b/curriculumsearch/src/main/webapp/jsp/dp.jsp @@ -0,0 +1,16 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib uri="http://kwonnam.pe.kr/jsp/template-inheritance" prefix="layout"%> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> + + + + +

+ DETALLE POSTULANTE +

+ ${postulante.nombre} ${postulante.apellido} +
+
+ -- libgit2 0.26.0