Rev 528 | 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; |
||
| 16 | import br.com.ec.core.validador.Validador; |
||
| 17 | import br.com.ec.domain.dto.ImagemDTO; |
||
| 18 | import br.com.ec.domain.service.ImagemService; |
||
| 19 | import br.com.ec.domain.shared.ConstantesSEC; |
||
| 20 | |||
| 21 | @Service |
||
| 22 | public class ImagemServiceImpl extends AbstractService<ImagemDTO> implements ImagemService { |
||
| 23 | |||
| 24 | @Autowired |
||
| 25 | public ImagemServiceImpl(Validador validador) { |
||
| 26 | super(validador); |
||
| 27 | } |
||
| 28 | |||
| 29 | @Override |
||
| 30 | protected GenericRepository<ImagemDTO> getRepository() { |
||
| 31 | return null; |
||
| 32 | } |
||
| 33 | |||
| 34 | @Override |
||
| 35 | public String caminhoPadraoFotos() { |
||
| 36 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? ConstantesSEC.Foto.CAMINHO_PADRAO_LOCAL : ConstantesSEC.Foto.CAMINHO_PADRAO_SERVIDOR; |
||
| 37 | } |
||
| 38 | |||
| 39 | @Override |
||
| 40 | public String caminhoPastaFotos() { |
||
| 41 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 42 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? |
||
| 43 | servletContext.getRealPath("") + File.separator + ConstantesSEC.Foto.NOME_PASTA_PADRAO + File.separator : |
||
| 44 | servletContext.getRealPath("") + File.separator + ".." + File.separator + ConstantesSEC.Foto.NOME_PASTA_PADRAO + File.separator; |
||
| 45 | } |
||
| 46 | |||
| 47 | @Override |
||
| 48 | public String caminhoPadraoImagens() { |
||
| 49 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? ConstantesSEC.Imagem.CAMINHO_PADRAO_LOCAL : ConstantesSEC.Imagem.CAMINHO_PADRAO_SERVIDOR; |
||
| 50 | } |
||
| 51 | |||
| 52 | @Override |
||
| 53 | public String caminhoPastaImagens() { |
||
| 54 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 55 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? |
||
| 56 | servletContext.getRealPath("") + File.separator + ConstantesSEC.Imagem.NOME_PASTA_PADRAO + File.separator : |
||
| 57 | servletContext.getRealPath("") + File.separator + ".." + File.separator + ConstantesSEC.Imagem.NOME_PASTA_PADRAO + File.separator; |
||
| 58 | } |
||
| 59 | |||
| 60 | @Override |
||
| 61 | public String caminhoPadraoEstampas() { |
||
| 62 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? ConstantesSEC.Estampa.CAMINHO_PADRAO_LOCAL : ConstantesSEC.Estampa.CAMINHO_PADRAO_SERVIDOR; |
||
| 63 | } |
||
| 64 | |||
| 65 | @Override |
||
| 66 | public String caminhoPastaEstampas() { |
||
| 67 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 68 | return ConstantesSEC.TESTE_EMISSAO_LOCAL? |
||
| 69 | servletContext.getRealPath("") + File.separator + ConstantesSEC.Estampa.NOME_PASTA_PADRAO + File.separator : |
||
| 70 | servletContext.getRealPath("") + File.separator + ".." + File.separator + ConstantesSEC.Estampa.NOME_PASTA_PADRAO + File.separator; |
||
| 71 | } |
||
| 72 | |||
| 73 | /*****************************************/ |
||
| 74 | |||
| 75 | @Override |
||
| 530 | blopes | 76 | public String retornarCaminhoImagem(String pasta, String nomeArquivo, String extensaoArquivo) { |
| 77 | File foto = new File(pasta + nomeArquivo + "." + extensaoArquivo); |
||
| 78 | return foto.exists()? pasta + nomeArquivo + "." + extensaoArquivo : pasta + "SEM_IMAGEM.png"; |
||
| 79 | } |
||
| 80 | |||
| 81 | @Override |
||
| 82 | public ImagemDTO uploadImagem(UploadedFile e, String nomeArquivo, String caminhoPasta) { |
||
| 83 | ImagemDTO imagemAdicionada = gerarArquivo(e, nomeArquivo); |
||
| 84 | uploadFisico(imagemAdicionada, caminhoPasta); |
||
| 528 | blopes | 85 | // lancarExcecaoCasoArquivoExcedeuLimite(imagemAdicionada); |
| 530 | blopes | 86 | // imagemAdicionada.compactarImagem(); |
| 87 | // imagemAdicionada.converterPngParaJpg(); |
||
| 88 | return imagemAdicionada; |
||
| 528 | blopes | 89 | } |
| 90 | |||
| 530 | blopes | 91 | public ImagemDTO gerarArquivo(UploadedFile file, String nomeArquivo) { |
| 528 | blopes | 92 | ImagemDTO arquivo = new ImagemDTO(); |
| 93 | arquivo.setArquivo(file.getContent()); |
||
| 530 | blopes | 94 | arquivo.setDescricao(nomeArquivo); |
| 528 | blopes | 95 | arquivo.setExtensao(ArquivoUtil.retornarExtensaoDoArquivo(file.getFileName())); |
| 96 | return arquivo; |
||
| 97 | } |
||
| 98 | |||
| 530 | blopes | 99 | public void uploadFisico(ImagemDTO arquivoParaUpload, String caminhoPasta) { |
| 100 | String caminho = caminhoPasta + arquivoParaUpload.getDescricao() + "." + arquivoParaUpload.getExtensao(); |
||
| 101 | FileOutputStream fos; |
||
| 102 | try { |
||
| 103 | fos = new FileOutputStream(caminho); |
||
| 104 | fos.write(arquivoParaUpload.getArquivo()); |
||
| 105 | fos.close(); |
||
| 106 | } catch (Exception e1) { |
||
| 107 | e1.printStackTrace(); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 528 | blopes | 111 | } |