com.dominion.salud.pedicom.web.controller.PDFController.java Source code

Java tutorial

Introduction

Here is the source code for com.dominion.salud.pedicom.web.controller.PDFController.java

Source

/*
 * Copyright (C) 2016 Dominion Global
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.dominion.salud.pedicom.web.controller;

import com.dominion.salud.pedicom.negocio.configuration.PEDICOMConstantes;
import com.dominion.salud.pedicom.negocio.configuration.RoutingDataSource;
import com.dominion.salud.pedicom.negocio.entities.Pedidos;
import com.dominion.salud.pedicom.negocio.tools.PDFService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import net.sf.jasperreports.engine.JRException;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author p.fernandez-ext
 */
@Controller
@RequestMapping("pdf")
public class PDFController extends AbstractController<Pedidos, Long> {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(PDFController.class);

    @Autowired
    private PDFService pdfService;

    @Autowired
    private RoutingDataSource routingDataSource;

    /**
     * Genera un pdf de un pedido concreto
     *
     * @param pedidos Pedido del que se quiere generar el pdf
     * @return Ruta del pdf
     */
    @ResponseBody
    @RequestMapping(value = "pdfPedidos", method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<String> pdfPedidos(@RequestBody(required = true) Pedidos pedidos) {
        logger.debug("Iniciando generacion del pdf");
        Connection con = null;
        try {
            List<Pedidos> peds = new ArrayList<>();
            peds.add(pedidos);
            con = routingDataSource.getConnection();
            InputStream inputstream = pdfService.crearPDF(peds, con);
            //            File dir = new File(System.getProperty("pedicom.home"));
            File dir = new File(PEDICOMConstantes._HOME);
            String nombre = Long.toString(new Date().getTime());
            logger.debug("      Guardando pdf en : " + dir + "/reports/");
            File tempFile = new File(dir + "/reports/", nombre + "tmp.pdf");
            FileOutputStream out = new FileOutputStream(tempFile);
            IOUtils.copy(inputstream, out);
            logger.debug("Path completo :" + tempFile.getPath());
            return new ResponseEntity(FilenameUtils.getName(tempFile.getPath()), HttpStatus.OK);
        } catch (IOException | SQLException | ClassNotFoundException | JRException ex) {
            logger.error(ex.getMessage());
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            } catch (SQLException ex) {
                logger.error("Error :" + ex.getMessage());
            }
        }
        return new ResponseEntity("No_encontrado.pdf", HttpStatus.OK);
    }

}