Rev 531 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 528 | blopes | 1 | package br.com.ec.domain.service.impl; |
| 2 | |||
| 3 | import java.io.File; |
||
| 530 | blopes | 4 | import java.io.FileOutputStream; |
| 528 | blopes | 5 | |
| 6 | import javax.faces.context.FacesContext; |
||
| 7 | import javax.servlet.ServletContext; |
||
| 8 | |||
| 9 | import org.primefaces.model.file.UploadedFile; |
||
| 10 | import org.springframework.beans.factory.annotation.Autowired; |
||
| 11 | import org.springframework.stereotype.Service; |
||
| 12 | |||
| 13 | import br.com.ec.core.generic.AbstractService; |
||
| 14 | import br.com.ec.core.generic.GenericRepository; |
||
| 15 | import br.com.ec.core.util.ArquivoUtil; |
||
| 531 | blopes | 16 | import br.com.ec.core.util.StringUtil; |
| 528 | blopes | 17 | import br.com.ec.core.validador.Validador; |
| 18 | import br.com.ec.domain.dto.ImagemDTO; |
||
| 19 | import br.com.ec.domain.service.ImagemService; |
||
| 20 | import br.com.ec.domain.shared.ConstantesSEC; |
||
| 21 | |||
| 22 | @Service |
||
| 23 | public class ImagemServiceImpl extends AbstractService<ImagemDTO> implements ImagemService { |
||
| 24 | |||
| 25 | @Autowired |
||
| 26 | public ImagemServiceImpl(Validador validador) { |
||
| 27 | super(validador); |
||
| 28 | } |
||
| 29 | |||
| 30 | @Override |
||
| 31 | protected GenericRepository<ImagemDTO> getRepository() { |
||
| 32 | return null; |
||
| 33 | } |
||
| 34 | |||
| 35 | @Override |
||
| 36 | public String caminhoPadraoFotos() { |
||
| 37 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? ConstantesSEC.Foto.CAMINHO_PADRAO_LOCAL : ConstantesSEC.Foto.CAMINHO_PADRAO_SERVIDOR; |
||
| 38 | } |
||
| 39 | |||
| 40 | @Override |
||
| 41 | public String caminhoPastaFotos() { |
||
| 42 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 43 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? |
||
| 44 | servletContext.getRealPath("") + File.separator + ConstantesSEC.Foto.NOME_PASTA_PADRAO + File.separator : |
||
| 45 | servletContext.getRealPath("") + File.separator + ".." + File.separator + ConstantesSEC.Foto.NOME_PASTA_PADRAO + File.separator; |
||
| 46 | } |
||
| 47 | |||
| 48 | @Override |
||
| 49 | public String caminhoPadraoImagens() { |
||
| 50 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? ConstantesSEC.Imagem.CAMINHO_PADRAO_LOCAL : ConstantesSEC.Imagem.CAMINHO_PADRAO_SERVIDOR; |
||
| 51 | } |
||
| 52 | |||
| 53 | @Override |
||
| 54 | public String caminhoPastaImagens() { |
||
| 55 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 56 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? |
||
| 57 | servletContext.getRealPath("") + File.separator + ConstantesSEC.Imagem.NOME_PASTA_PADRAO + File.separator : |
||
| 58 | servletContext.getRealPath("") + File.separator + ".." + File.separator + ConstantesSEC.Imagem.NOME_PASTA_PADRAO + File.separator; |
||
| 59 | } |
||
| 60 | |||
| 61 | @Override |
||
| 62 | public String caminhoPadraoEstampas() { |
||
| 63 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? ConstantesSEC.Estampa.CAMINHO_PADRAO_LOCAL : ConstantesSEC.Estampa.CAMINHO_PADRAO_SERVIDOR; |
||
| 64 | } |
||
| 65 | |||
| 66 | @Override |
||
| 67 | public String caminhoPastaEstampas() { |
||
| 68 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 69 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? |
||
| 70 | servletContext.getRealPath("") + File.separator + ConstantesSEC.Estampa.NOME_PASTA_PADRAO + File.separator : |
||
| 71 | servletContext.getRealPath("") + File.separator + ".." + File.separator + ConstantesSEC.Estampa.NOME_PASTA_PADRAO + File.separator; |
||
| 72 | } |
||
| 73 | |||
| 74 | /*****************************************/ |
||
| 75 | |||
| 76 | @Override |
||
| 533 | blopes | 77 | public String retornarCaminhoImagem(String pasta, String pastaPadrao, String nomeArquivo, String extensaoArquivo) { |
| 531 | blopes | 78 | File foto = new File(pasta + nomeArquivo + "." + StringUtil.setarLowerCase(extensaoArquivo)); |
| 533 | blopes | 79 | return foto.exists()? pastaPadrao + nomeArquivo + "." + StringUtil.setarLowerCase(extensaoArquivo) : pastaPadrao + "SEM_IMAGEM.png"; |
| 530 | blopes | 80 | } |
| 81 | |||
| 82 | @Override |
||
| 83 | public ImagemDTO uploadImagem(UploadedFile e, String nomeArquivo, String caminhoPasta) { |
||
| 84 | ImagemDTO imagemAdicionada = gerarArquivo(e, nomeArquivo); |
||
| 85 | uploadFisico(imagemAdicionada, caminhoPasta); |
||
| 528 | blopes | 86 | // lancarExcecaoCasoArquivoExcedeuLimite(imagemAdicionada); |
| 530 | blopes | 87 | // imagemAdicionada.compactarImagem(); |
| 88 | // imagemAdicionada.converterPngParaJpg(); |
||
| 89 | return imagemAdicionada; |
||
| 528 | blopes | 90 | } |
| 91 | |||
| 530 | blopes | 92 | public ImagemDTO gerarArquivo(UploadedFile file, String nomeArquivo) { |
| 528 | blopes | 93 | ImagemDTO arquivo = new ImagemDTO(); |
| 94 | arquivo.setArquivo(file.getContent()); |
||
| 530 | blopes | 95 | arquivo.setDescricao(nomeArquivo); |
| 528 | blopes | 96 | arquivo.setExtensao(ArquivoUtil.retornarExtensaoDoArquivo(file.getFileName())); |
| 97 | return arquivo; |
||
| 98 | } |
||
| 99 | |||
| 530 | blopes | 100 | public void uploadFisico(ImagemDTO arquivoParaUpload, String caminhoPasta) { |
| 101 | String caminho = caminhoPasta + arquivoParaUpload.getDescricao() + "." + arquivoParaUpload.getExtensao(); |
||
| 102 | FileOutputStream fos; |
||
| 103 | try { |
||
| 104 | fos = new FileOutputStream(caminho); |
||
| 105 | fos.write(arquivoParaUpload.getArquivo()); |
||
| 106 | fos.close(); |
||
| 107 | } catch (Exception e1) { |
||
| 108 | e1.printStackTrace(); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 528 | blopes | 112 | } |