Rev 182 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package br.com.ec.domain.service.cliente.impl; |
| 2 | |||
| 3 | import java.io.ByteArrayOutputStream; |
||
| 4 | import java.io.FileNotFoundException; |
||
| 5 | import java.io.IOException; |
||
| 6 | import java.util.ArrayList; |
||
| 7 | import java.util.HashMap; |
||
| 8 | |||
| 9 | import org.apache.poi.hssf.usermodel.HSSFSheet; |
||
| 10 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
||
| 11 | import org.apache.poi.ss.usermodel.CellStyle; |
||
| 12 | import org.apache.poi.ss.usermodel.Row; |
||
| 13 | import org.primefaces.model.DefaultStreamedContent; |
||
| 14 | import org.primefaces.model.StreamedContent; |
||
| 15 | import org.springframework.beans.factory.annotation.Autowired; |
||
| 16 | import org.springframework.stereotype.Service; |
||
| 17 | |||
| 18 | import br.com.ec.controller.util.ExcelUtil; |
||
| 19 | import br.com.ec.domain.model.Cliente; |
||
| 20 | import br.com.ec.domain.model.Pedido; |
||
| 21 | import br.com.ec.domain.model.PedidoProduto; |
||
| 22 | import br.com.ec.domain.service.cliente.ClienteService; |
||
| 23 | import br.com.ec.domain.service.pedido.PedidoService; |
||
| 24 | import br.com.ec.infrastructure.repository.ClienteRepository; |
||
| 25 | import br.edu.cesmac.core.consulta.ParametrosConsulta; |
||
| 26 | import br.edu.cesmac.core.exception.NegocioException; |
||
| 27 | import br.edu.cesmac.core.generic.AbstractService; |
||
| 28 | import br.edu.cesmac.core.generic.GenericRepository; |
||
| 29 | import br.edu.cesmac.core.util.ArquivoUtil; |
||
| 30 | import br.edu.cesmac.core.util.DataUtils; |
||
| 31 | import br.edu.cesmac.core.util.TipoExtensao; |
||
| 32 | import br.edu.cesmac.core.util.VerificadorUtil; |
||
| 33 | import br.edu.cesmac.core.validador.Validador; |
||
| 34 | |||
| 35 | @Service |
||
| 36 | public class ClienteServiceImpl extends AbstractService<Cliente> implements ClienteService { |
||
| 37 | |||
| 38 | private PedidoService pedidoService; |
||
| 39 | private ClienteRepository clienteRepository; |
||
| 40 | |||
| 41 | @Autowired |
||
| 42 | public ClienteServiceImpl(Validador validador, PedidoService pedidoService, ClienteRepository clienteRepository) { |
||
| 43 | super(validador); |
||
| 44 | this.pedidoService = pedidoService; |
||
| 45 | this.clienteRepository = clienteRepository; |
||
| 46 | } |
||
| 47 | |||
| 48 | @Override |
||
| 49 | protected GenericRepository<Cliente> getRepository() { |
||
| 50 | return clienteRepository; |
||
| 51 | } |
||
| 52 | |||
| 53 | @Override |
||
| 54 | protected void regrasNegocioCadastrar(Cliente cliente) { |
||
| 55 | cliente.setAtivo(true); |
||
| 56 | cliente.setDataCadastro(DataUtils.getDataAtual()); |
||
| 57 | verificarSeCpfCnpjJaExiste(cliente); |
||
| 58 | } |
||
| 59 | |||
| 60 | @Override |
||
| 61 | protected void regrasNegocioAlterar(Cliente cliente) { |
||
| 62 | super.regrasNegocioAlterar(cliente); |
||
| 63 | verificarSeCpfCnpjJaExiste(cliente); |
||
| 64 | } |
||
| 65 | |||
| 66 | private void verificarSeCpfCnpjJaExiste(Cliente cliente) { |
||
| 67 | if (VerificadorUtil.naoEstaNulo(clienteRepository.consultarClientePorCpfCnpj(cliente.getCpfCnpj(), cliente.getSequencial()))) { |
||
| 68 | throw new NegocioException("CPF/CNPJ JÁ EXISTE."); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | @Override |
||
| 73 | public Cliente detalharCliente(Cliente cliente) { |
||
| 74 | Cliente clienteDetalhado = clienteRepository.detalharCliente(cliente); |
||
| 75 | clienteDetalhado.setPedidos(pedidoService.consultarPedidosDoCliente(cliente)); |
||
| 76 | return clienteDetalhado; |
||
| 77 | } |
||
| 78 | |||
| 79 | @Override |
||
| 80 | public Cliente consultarClientePorCpfCnpj(String cpfCnpj) { |
||
| 81 | return clienteRepository.consultarClientePorCpfCnpj(cpfCnpj, null); |
||
| 82 | } |
||
| 83 | |||
| 84 | @Override |
||
| 85 | public Cliente consultarClientePorCpfCnpjOuContato(String cpfCnpj, String contatoCliente) { |
||
| 86 | return clienteRepository.consultarClientePorCpfCnpjOuContato(cpfCnpj, contatoCliente); |
||
| 87 | } |
||
| 88 | |||
| 89 | @Override |
||
| 90 | protected void regrasNegocioExcluir(Cliente cliente) { |
||
| 91 | if (verificarSeClientePossuiVendas(cliente)) { |
||
| 92 | throw new NegocioException("Não é permitido excluir o cliente que possui vendas"); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | private boolean verificarSeClientePossuiVendas(Cliente cliente) { |
||
| 97 | return clienteRepository.verificarSeClientePossuiVendas(cliente); |
||
| 98 | } |
||
| 99 | |||
| 100 | @Override |
||
| 101 | public StreamedContent gerarArquivoExcel(ParametrosConsulta<Cliente> parametrosConsulta) { |
||
| 102 | HSSFWorkbook wb = new HSSFWorkbook(); |
||
| 103 | HSSFSheet aba = ExcelUtil.criarAbas(wb, "CLIENTES"); |
||
| 104 | |||
| 105 | CellStyle headerStyle = ExcelUtil.configurarCelulaCabecalho(wb); |
||
| 106 | Row linhaHeader = aba.createRow(0); |
||
| 107 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 0, headerStyle, "NOME"); |
||
| 108 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 1, headerStyle, "CPF/CNPJ"); |
||
| 109 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 2, headerStyle, "CONTATO"); |
||
| 110 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 3, headerStyle, "TELEFONE"); |
||
| 111 | ExcelUtil.criarCelula(wb, aba, linhaHeader, 4, headerStyle, "EMAIL"); |
||
| 112 | |||
| 113 | // CONSULTAR |
||
| 114 | ArrayList<Cliente> clientes = (ArrayList<Cliente>) clienteRepository.consultarPassandoParametrosConsulta(parametrosConsulta, 0, |
||
| 115 | clienteRepository.obterQuantidadeDeRegistrosParametrosConsulta(parametrosConsulta), null, null, new HashMap<String, Object>()); |
||
| 116 | |||
| 117 | // PREENCHER |
||
| 118 | Integer numeroLinha = 1; |
||
| 119 | for (Cliente cliente : clientes) { |
||
| 120 | Row linha = aba.createRow(numeroLinha); |
||
| 121 | ExcelUtil.criarCelula(wb, aba, linha, 0, ExcelUtil.configurarCelulaTexto(wb), cliente.getNome()); |
||
| 122 | ExcelUtil.criarCelula(wb, aba, linha, 1, ExcelUtil.configurarCelulaTexto(wb), cliente.getCpfCnpjFormatado()); |
||
| 123 | ExcelUtil.criarCelula(wb, aba, linha, 2, ExcelUtil.configurarCelulaTexto(wb), cliente.getContato()); |
||
| 124 | ExcelUtil.criarCelula(wb, aba, linha, 3, ExcelUtil.configurarCelulaTexto(wb), cliente.getTelefone()); |
||
| 125 | ExcelUtil.criarCelula(wb, aba, linha, 4, ExcelUtil.configurarCelulaTexto(wb), cliente.getEmail()); |
||
| 126 | numeroLinha++; |
||
| 127 | } |
||
| 128 | |||
| 129 | try { |
||
| 130 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
||
| 131 | wb.write(stream); |
||
| 132 | stream.close(); |
||
| 133 | wb.close(); |
||
| 134 | return new DefaultStreamedContent(ArquivoUtil.gerarInputStreamDeArquivo(stream.toByteArray()), TipoExtensao.EXCEL.getDescricao(), "clientes.xls"); |
||
| 135 | } catch (FileNotFoundException e) { |
||
| 136 | e.printStackTrace(); |
||
| 137 | } catch (IOException e) { |
||
| 138 | e.printStackTrace(); |
||
| 139 | } |
||
| 140 | return null; |
||
| 141 | } |
||
| 142 | |||
| 143 | } |