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