com.solidmaps.webapp.service.ApostilamentosServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.solidmaps.webapp.service.ApostilamentosServiceImpl.java

Source

package com.solidmaps.webapp.service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Service;

import com.solidmaps.webapp.dao.CharterTypeDAO;
import com.solidmaps.webapp.dao.CompanyDAO;
import com.solidmaps.webapp.dao.LicenseEXDAO;
import com.solidmaps.webapp.dao.LicensePCDAO;
import com.solidmaps.webapp.dao.LicensePFDAO;
import com.solidmaps.webapp.dao.ProductOfficialTypeDAO;
import com.solidmaps.webapp.entity.CharterTypeEntity;
import com.solidmaps.webapp.entity.CompanyEntity;
import com.solidmaps.webapp.entity.LicenseEXEntity;
import com.solidmaps.webapp.entity.LicenseExCharterProductEntity;
import com.solidmaps.webapp.entity.LicensePCEntity;
import com.solidmaps.webapp.entity.LicensePCProductEntity;
import com.solidmaps.webapp.entity.LicensePFEntity;
import com.solidmaps.webapp.entity.ProductOfficialEntity;
import com.solidmaps.webapp.entity.ProductOfficialTypeEntity;
import com.solidmaps.webapp.enuns.MapTypeEnum;
import com.solidmaps.webapp.enuns.RequerimentFederalEnum;
import com.solidmaps.webapp.report.LicenseCivilIncludeProductPDF;
import com.solidmaps.webapp.report.RequerimentAlterLicenseExercitoPDF;
import com.solidmaps.webapp.report.RequerimentAlterLicenseFederalPDF;
import com.solidmaps.webapp.utils.NumberUtils;
import com.solidmaps.webapp.vo.DocumentationSettings;
import com.solidmaps.webapp.vo.LicenseProductQtdJSON;

@Service
public class ApostilamentosServiceImpl implements ApostilamentoService {

    private static final Logger logger = Logger.getLogger(ApostilamentosServiceImpl.class);

    @Value("${path.map.file.path}")
    private String FILE_PATH;

    @Autowired
    private CompanyDAO companyDAO;

    @Autowired
    private LicenseEXDAO licenseEXDAO;

    @Autowired
    private ProductOfficialTypeDAO productOfficialTypeDAO;

    @Autowired
    private CharterTypeDAO charterTypeDAO;

    @Autowired
    private LicensePCDAO licensePCDAO;

    @Autowired
    private LicensePFDAO licensePFDAO;

    private String fileExercito;
    private String fileCivil;
    private String fileFederal;

    @Override
    public FileSystemResource generateDocs(DocumentationSettings settings) {

        logger.info("[apostilamento generateDocs] init");

        CompanyEntity companyEntity = companyDAO.findById(settings.getIdCompany());

        this.generateFederal(companyEntity, settings);
        this.generateExercito(companyEntity, settings);
        this.generateCivil(companyEntity, settings);

        return this.generateZip(companyEntity);
    }

    private FileSystemResource generateZip(CompanyEntity companyEntity) {

        String fileName = FILE_PATH + "Apostilamento-" + companyEntity.getCnpj() + ".zip";
        FileOutputStream fos = null;

        try {

            fos = new FileOutputStream(fileName);
            ZipOutputStream zos = new ZipOutputStream(fos);
            this.addToZip(zos, fileFederal);
            this.addToZip(zos, fileCivil);
            this.addToZip(zos, fileExercito);

            zos.closeEntry();
            zos.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileSystemResource fileSystemResource = new FileSystemResource(fileName);

        return fileSystemResource;
    }

    private void addToZip(ZipOutputStream zos, String file) {

        if (StringUtils.isBlank(file)) {
            return;
        }

        byte[] buffer = new byte[1024];

        try {
            ZipEntry ze = new ZipEntry(file);
            zos.putNextEntry(ze);

            FileInputStream in = new FileInputStream(FILE_PATH + file);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            in.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    /**
     * Sempre consulta os Produtos pelo CdigoMeyre. Isso  necessrio para usar
     * o nome correto na documentao.
     * 
     * @param companyEntity
     * @param productsInclude
     * @param productsExclude
     * @param idAlvara
     * @param adressText
     * @param type
     */
    private void generateExercito(CompanyEntity companyEntity, DocumentationSettings settings) {

        List<LicenseExCharterProductEntity> listProductExercitoInclude = new ArrayList<LicenseExCharterProductEntity>();

        List<CharterTypeEntity> listCharterEntity = charterTypeDAO.findByListId(settings.getListCharters());

        LicenseEXEntity licenseExercito = licenseEXDAO.findByCompanyExpirationDate(companyEntity.getIdCompany(),
                Calendar.getInstance());

        // Consulta os Produtos para Incluso
        for (LicenseProductQtdJSON productJSON : settings.getListJsonProductsInclude()) {

            List<ProductOfficialTypeEntity> productType = productOfficialTypeDAO
                    .findByNameCodMeyre(MapTypeEnum.EXERCITO.getType(), productJSON.getIdProduct());

            if (productType != null && !productType.isEmpty()) {
                LicenseExCharterProductEntity productEntity = new LicenseExCharterProductEntity();
                productEntity.setProduct(productType.get(0).getProductOfficial());
                productEntity.setQtdProduct(NumberUtils.formatToDouble(productJSON.getValue()));
                productEntity.setTypeQtdProduct(productJSON.getTypeQtdProduct());

                listProductExercitoInclude.add(productEntity);
            }

        }

        RequerimentAlterLicenseExercitoPDF apostilamentoExercito = new RequerimentAlterLicenseExercitoPDF(
                FILE_PATH);
        fileExercito = apostilamentoExercito.generate(licenseExercito, listCharterEntity,
                listProductExercitoInclude, "", "");
    }

    private void generateCivil(CompanyEntity companyEntity, DocumentationSettings settings) {

        LicensePCEntity license = licensePCDAO.findByCompanyExpirationDate(companyEntity.getIdCompany(),
                Calendar.getInstance());

        List<LicensePCProductEntity> listProducts = new ArrayList<LicensePCProductEntity>();
        List<CharterTypeEntity> listCharterEntity = new ArrayList<CharterTypeEntity>();

        // Consulta os Produtos para Incluso
        for (LicenseProductQtdJSON productJSON : settings.getListJsonProductsInclude()) {

            List<ProductOfficialTypeEntity> productType = productOfficialTypeDAO
                    .findByNameCodMeyre(companyEntity.getState(), productJSON.getIdProduct());

            if (productType != null && !productType.isEmpty()) {
                LicensePCProductEntity productEntity = new LicensePCProductEntity();
                productEntity.setProduct(productType.get(0).getProductOfficial());
                productEntity.setQtdProduct(NumberUtils.formatToDouble(productJSON.getValue()));
                productEntity.setTypeQtdProduct(productJSON.getTypeQtdProduct());

                listProducts.add(productEntity);
            }

        }

        // Consulta as Atividades
        for (Integer idCharter : settings.getListCharters()) {
            listCharterEntity.add(charterTypeDAO.findById(idCharter));
        }

        LicenseCivilIncludeProductPDF civilIncludeProductPDF = new LicenseCivilIncludeProductPDF(FILE_PATH);
        fileCivil = civilIncludeProductPDF.generate(license, "", listProducts);
    }

    private void generateFederal(CompanyEntity companyEntity, DocumentationSettings settings) {

        LicensePFEntity license = licensePFDAO.findByCompanyExpirationDate(companyEntity.getIdCompany(),
                Calendar.getInstance());

        List<ProductOfficialEntity> listProducts = new ArrayList<ProductOfficialEntity>();

        // Consulta os Produtos para Incluso
        for (LicenseProductQtdJSON productJSON : settings.getListJsonProductsInclude()) {

            List<ProductOfficialTypeEntity> productType = productOfficialTypeDAO
                    .findByNameCodMeyre(MapTypeEnum.POLICIA_FEDERAL.getType(), productJSON.getIdProduct());

            if (productType != null && !productType.isEmpty()) {

                listProducts.add(productType.get(0).getProductOfficial());
            }

        }

        RequerimentAlterLicenseFederalPDF alterLicenseFederalPDF = new RequerimentAlterLicenseFederalPDF(FILE_PATH);
        fileFederal = alterLicenseFederalPDF.generate(license, RequerimentFederalEnum.INCLUSAO_PRODUTO,
                listProducts);
    }

}