Java tutorial
/* * 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 managedbeans; import cl.rcehblt.anamnesis.AnamnesisNegocioLocal; import cl.rcehblt.entities.Anamnesis; import cl.rcehblt.entities.Paciente; import cl.rcehblt.entities.Persona; import cl.rcehblt.paciente.PacienteNegocioLocal; import cl.rcehblt.persona.PersonaNegocioLocal; import cl.rcehblt.sessionbeans.AnamnesisFacadeLocal; import cl.rcehblt.sessionbeans.PersonaFacadeLocal; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.IOException; import java.io.PrintWriter; import java.text.DateFormat; import java.util.Date; import java.util.List; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author DevelUser */ @WebServlet(name = "ImprimirAnamnesis", urlPatterns = { "/ImprimirAnamnesis" }) public class ImprimirAnamnesis extends HttpServlet { @EJB private AnamnesisFacadeLocal anamnesisFacade; @EJB private PacienteNegocioLocal pacienteNegocio; @EJB private PersonaNegocioLocal personaNegocio; @EJB private PersonaFacadeLocal personaFacade; private String institution = "Hospital Barros Luco"; private String address = "Jos Miguel Carrera 3604, San Miguel"; private String city = "Santiago"; private String personName = ""; private Integer rut; public Paciente paciente; private Anamnesis anamnesis; private String patientName = ""; private Integer patientRut = 0; private String patientFonasa = ""; private String patientIsapre = ""; private String home = ""; private String commune = ""; private String region = ""; private String phoneNumber = ""; private String celularNumber = ""; private String mail = ""; Date aux = new Date(); DateFormat dfDefault = DateFormat.getInstance(); private String dateHour = dfDefault.format(aux); private String ges = ""; // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/pdf"); String rut = request.getParameter("rut"); String idAnam = request.getParameter("id"); Integer search = Integer.parseInt(rut); Integer anam = Integer.parseInt(idAnam); anamnesis = anamnesisFacade.find(anam); List<Persona> person = personaNegocio.busquedaPersonaRut(search); Persona personSelected; if (person.size() > 0) { System.out.println("La cantidad es:" + person.size()); personSelected = person.get(0); System.out.println("La cantidad es:" + personSelected.getPersNombres()); paciente = pacienteNegocio.busquedaPacienteIdPersona(personSelected.getIdPersona()); patientName = personSelected.getPersNombres() + " " + personSelected.getPersApepaterno() + " " + personSelected.getPersApematerno(); patientRut = search; patientFonasa = ""; patientIsapre = paciente.getPaciOtraprevision(); home = personSelected.getPersDireccion(); commune = ""; region = ""; phoneNumber = personSelected.getPersTelefono(); celularNumber = personSelected.getPersCelular(); mail = personSelected.getPersEmail(); } try { //style Font typeBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, 1); Font type = new Font(Font.FontFamily.TIMES_ROMAN, 10); Font title = new Font(Font.FontFamily.TIMES_ROMAN, 15, 1); Font subTitle = new Font(Font.FontFamily.TIMES_ROMAN, 13, 1); Font subTitle2 = new Font(Font.FontFamily.TIMES_ROMAN, 10, 1); float space = (float) 20; Document document = new Document(); document.setPageSize(PageSize.LETTER); PdfWriter.getInstance(document, response.getOutputStream()); document.open(); //General PdfPTable table; Paragraph p1; Paragraph p2; PdfPCell cellRow1; PdfPCell cellRow2; //row: 1 table = new PdfPTable(2); table.setWidthPercentage(100); p1 = new Paragraph(space, "ANAMNESIS", title); p1.setAlignment(Element.ALIGN_CENTER); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); table.addCell(cellRow1); p1 = new Paragraph(space, "Hospital Barros Luco", subTitle2); p1.setAlignment(Element.ALIGN_CENTER); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); table.addCell(cellRow1); document.add(table); document.add(new Paragraph("\n")); //row 3 table = new PdfPTable(2); table.setWidthPercentage(100); p1 = new Paragraph(space, "ANTECEDENTES DEL PACIENTE", subTitle); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthTop(1); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); p1 = new Paragraph(space, "NOMBRE: " + patientName, type); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); table.addCell(cellRow1); p1 = new Paragraph(space, "RUT: " + patientRut, type); p2 = new Paragraph(space, "FONASA: " + patientFonasa, type); cellRow1 = new PdfPCell(p1); cellRow2 = new PdfPCell(p2); formatCellBorder(cellRow1, 20); formatCellBorder(cellRow2, 20); cellRow1.setBorderWidthLeft(1); cellRow2.setBorderWidthRight(1); table.addCell(cellRow1); table.addCell(cellRow2); p1 = new Paragraph(space, "ISAPRE: " + patientIsapre, type); p2 = new Paragraph(space, "DOMICILIO: " + home, type); cellRow1 = new PdfPCell(p1); cellRow2 = new PdfPCell(p2); formatCellBorder(cellRow1, 20); formatCellBorder(cellRow2, 20); cellRow1.setBorderWidthLeft(1); cellRow2.setBorderWidthRight(1); table.addCell(cellRow1); table.addCell(cellRow2); p1 = new Paragraph(space, "COMUNA: " + commune, type); p2 = new Paragraph(space, "REGION: " + region, type); cellRow1 = new PdfPCell(p1); cellRow2 = new PdfPCell(p2); formatCellBorder(cellRow1, 20); formatCellBorder(cellRow2, 20); cellRow1.setBorderWidthLeft(1); cellRow2.setBorderWidthRight(1); table.addCell(cellRow1); table.addCell(cellRow2); p1 = new Paragraph(space, "N TELFONO FIJO: " + phoneNumber, type); p2 = new Paragraph(space, "N TELFONO CELULAR: " + celularNumber, type); cellRow1 = new PdfPCell(p1); cellRow2 = new PdfPCell(p2); formatCellBorder(cellRow1, 20); formatCellBorder(cellRow2, 20); cellRow1.setBorderWidthLeft(1); cellRow2.setBorderWidthRight(1); table.addCell(cellRow1); table.addCell(cellRow2); p1 = new Paragraph(space, "DIRECCIN CORREO ELECTRNICO (E-MAIL): " + mail, type); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); document.add(table); document.add(new Paragraph("\n")); //row 4 table = new PdfPTable(2); table.setWidthPercentage(100); p1 = new Paragraph(space, "ANAMNESIS", subTitle); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthTop(1); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); p1 = new Paragraph(space, anamnesis.getAnamDescripcion(), type); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); document.add(table); document.add(new Paragraph("\n")); table = new PdfPTable(2); table.setWidthPercentage(100); p1 = new Paragraph(space, "HISTORIA OBSTTRICA", subTitle); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthTop(1); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); p1 = new Paragraph(space, anamnesis.getAnamDescripcion(), type); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); document.add(table); document.add(new Paragraph("\n")); table = new PdfPTable(2); table.setWidthPercentage(100); p1 = new Paragraph(space, "EX?MEN F?SICO", subTitle); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthTop(1); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); p1 = new Paragraph(space, anamnesis.getAnamDescripcion(), type); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); document.add(table); document.add(new Paragraph("\n")); table = new PdfPTable(2); table.setWidthPercentage(100); p1 = new Paragraph(space, "EX?MEN OBSTTRICO", subTitle); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthTop(1); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); p1 = new Paragraph(space, anamnesis.getAnamDescripcion(), type); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); document.add(table); document.add(new Paragraph("\n")); table = new PdfPTable(2); table.setWidthPercentage(100); p1 = new Paragraph(space, "RESUMEN PATOLOG?AS MATERNAS", subTitle); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthTop(1); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); p1 = new Paragraph(space, anamnesis.getAnamDescripcion(), type); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); document.add(table); document.add(new Paragraph("\n")); table = new PdfPTable(2); table.setWidthPercentage(100); p1 = new Paragraph(space, "DIAGNOSTICOS", subTitle); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthTop(1); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); p1 = new Paragraph(space, anamnesis.getAnamDescripcion(), type); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); document.add(table); document.add(new Paragraph("\n")); table = new PdfPTable(2); table.setWidthPercentage(100); p1 = new Paragraph(space, "INDICACIONES", subTitle); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthTop(1); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); p1 = new Paragraph(space, anamnesis.getAnamDescripcion(), type); cellRow1 = new PdfPCell(p1); cellRow1.setColspan(2); formatCellBorder(cellRow1, 20); cellRow1.setBorderWidthLeft(1); cellRow1.setBorderWidthRight(1); cellRow1.setBorderWidthBottom(1); table.addCell(cellRow1); document.add(table); document.close(); } catch (DocumentException de) { throw new IOException(de.getMessage()); } } private void formatCellBorder(PdfPCell cell, int h) { cell.setFixedHeight(h); cell.setBorder(0); } }