Rev 468 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 468 | blopes | 1 | package br.com.ec.controller; |
| 2 | |||
| 3 | import java.io.File; |
||
| 4 | import java.io.FileOutputStream; |
||
| 5 | import java.util.ArrayList; |
||
| 6 | import java.util.List; |
||
| 7 | |||
| 8 | import javax.faces.context.FacesContext; |
||
| 9 | import javax.inject.Named; |
||
| 10 | import javax.persistence.Transient; |
||
| 11 | import javax.servlet.ServletContext; |
||
| 12 | |||
| 13 | import org.primefaces.event.FileUploadEvent; |
||
| 14 | import org.primefaces.model.file.UploadedFile; |
||
| 15 | import org.springframework.context.annotation.Scope; |
||
| 16 | |||
| 17 | import br.com.ec.core.exception.NegocioException; |
||
| 18 | import br.com.ec.core.util.ArquivoUtil; |
||
| 19 | import br.com.ec.core.util.VerificadorUtil; |
||
| 20 | import br.com.ec.domain.dto.FotoDTO; |
||
| 21 | import br.com.ec.domain.dto.ProdutoDTO; |
||
| 22 | import br.com.ec.domain.model.Produto; |
||
| 23 | import br.com.ec.domain.shared.ConstantesSEC; |
||
| 24 | import br.com.ec.web.exception.VerificadorLancamentoException; |
||
| 25 | import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean; |
||
| 26 | import br.com.ec.web.message.LancadorMensagem; |
||
| 27 | |||
| 28 | @Named |
||
| 29 | @Scope("view") |
||
| 30 | public class FotoBean { |
||
| 31 | |||
| 32 | private FotoDTO fotoNova; |
||
| 33 | private UploadedFile fotoParaUpload; |
||
| 34 | |||
| 35 | public FotoDTO getFotoNova() { |
||
| 36 | return fotoNova; |
||
| 37 | } |
||
| 38 | public void setFotoNova(FotoDTO fotoNova) { |
||
| 39 | this.fotoNova = fotoNova; |
||
| 40 | } |
||
| 41 | |||
| 42 | public UploadedFile getFotoParaUpload() { |
||
| 43 | return fotoParaUpload; |
||
| 44 | } |
||
| 45 | public void setFotoParaUpload(UploadedFile fotoParaUpload) { |
||
| 46 | this.fotoParaUpload = fotoParaUpload; |
||
| 47 | } |
||
| 48 | |||
| 472 | blopes | 49 | /**********************************************/ |
| 50 | |||
| 51 | public String getCaminhoPadraoFotos() { |
||
| 52 | return ConstantesSEC.TESTE_LOCAL? ConstantesSEC.Foto.CAMINHO_PADRAO_LOCAL : ConstantesSEC.Foto.CAMINHO_PADRAO_SERVIDOR; |
||
| 53 | } |
||
| 54 | |||
| 55 | public String getCaminhoPastaFotos() { |
||
| 56 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 57 | return ConstantesSEC.TESTE_LOCAL? |
||
| 58 | servletContext.getRealPath("") + File.separator + "fotos" + File.separator : |
||
| 59 | servletContext.getRealPath("") + File.separator + ".." + File.separator + "fotos" + File.separator; |
||
| 60 | } |
||
| 61 | |||
| 468 | blopes | 62 | public String fotoPadraoProduto(ProdutoDTO produtoDTO) { |
| 63 | try { |
||
| 64 | if (VerificadorUtil.naoEstaNulo(produtoDTO)) { |
||
| 65 | if (VerificadorUtil.naoEstaNuloOuVazio(produtoDTO.getCodigo())) { |
||
| 66 | File foto = new File(getCaminhoPastaFotos() + produtoDTO.getCodigoProdutoPadrao() + ".jpg"); |
||
| 472 | blopes | 67 | return foto.exists()? getCaminhoPadraoFotos() + produtoDTO.getCodigoProdutoPadrao() + ".jpg" : getCaminhoPadraoFotos() + "SEM_FOTO.jpg"; |
| 468 | blopes | 68 | } |
| 69 | } |
||
| 70 | } catch (Exception e) { |
||
| 71 | e.printStackTrace(); |
||
| 72 | } |
||
| 73 | return getCaminhoPadraoFotos() + "SEM_FOTO.jpg"; |
||
| 74 | } |
||
| 75 | |||
| 76 | public String fotoPadraoProdutoComCodigo(String codigo) { |
||
| 472 | blopes | 77 | ProdutoDTO produtoDTO = new ProdutoDTO(); |
| 78 | produtoDTO.setCodigo(codigo); |
||
| 79 | return fotoPadraoProduto(produtoDTO); |
||
| 80 | } |
||
| 81 | |||
| 82 | public String fotoPadraoProduto(Produto produto) { |
||
| 468 | blopes | 83 | try { |
| 472 | blopes | 84 | if (VerificadorUtil.naoEstaNulo(produto)) { |
| 85 | if (VerificadorUtil.naoEstaNuloOuVazio(produto.getCodigo())) { |
||
| 86 | File foto = new File(getCaminhoPastaFotos() + produto.getCodigoProdutoPadrao() + ".jpg"); |
||
| 87 | return foto.exists()? getCaminhoPadraoFotos() + produto.getCodigoProdutoPadrao() + ".jpg" : getCaminhoPadraoFotos() + "SEM_FOTO.jpg"; |
||
| 468 | blopes | 88 | } |
| 89 | } |
||
| 90 | } catch (Exception e) { |
||
| 91 | e.printStackTrace(); |
||
| 92 | } |
||
| 93 | return getCaminhoPadraoFotos() + "SEM_FOTO.jpg"; |
||
| 94 | } |
||
| 95 | |||
| 472 | blopes | 96 | /********************************************** |
| 97 | |||
| 468 | blopes | 98 | @Transient |
| 99 | public List<String> fotosProdutoComFotoPadrao(Produto produto) { |
||
| 100 | List<String> fotos = new ArrayList<String>(); |
||
| 101 | if (VerificadorUtil.naoEstaNulo(produto)) { |
||
| 102 | if (VerificadorUtil.naoEstaNuloOuVazio(produto.getCodigo())) { |
||
| 103 | File fotoPadrao = new File(getCaminhoPastaFotos() + produto.getCodigoProdutoPadrao() + ".jpg"); |
||
| 104 | if (fotoPadrao.exists()) { |
||
| 105 | fotos.add(getCaminhoPadraoFotos() + produto.getCodigoProdutoPadrao() + ".jpg"); |
||
| 106 | } |
||
| 107 | fotos.addAll(fotosProduto(produto)); |
||
| 108 | return fotos; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | return fotos; |
||
| 112 | } |
||
| 113 | |||
| 114 | @Transient |
||
| 115 | public List<String> fotosProduto(Produto produto) { |
||
| 116 | List<String> fotos = new ArrayList<String>(); |
||
| 117 | if (VerificadorUtil.naoEstaNulo(produto)) { |
||
| 118 | if (VerificadorUtil.naoEstaNuloOuVazio(produto.getCodigo())) { |
||
| 119 | File foto = new File(getCaminhoPastaFotos() + produto.getCodigoProdutoPadrao() + "_1.jpg"); |
||
| 120 | Integer quantidadeFotos = 1; |
||
| 121 | if (foto.exists()) { |
||
| 122 | while (foto.exists()) { |
||
| 123 | fotos.add(getCaminhoPadraoFotos() + produto.getCodigoProdutoPadrao() + "_" + quantidadeFotos + ".jpg"); |
||
| 124 | quantidadeFotos++; |
||
| 125 | foto = new File(getCaminhoPastaFotos() + produto.getCodigoProdutoPadrao() + "_" + quantidadeFotos + ".jpg"); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | return fotos; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | return fotos; |
||
| 132 | } |
||
| 133 | |||
| 134 | public void prepararUploadFoto(Produto produto) { |
||
| 135 | FotoDTO fotoNova = new FotoDTO(); |
||
| 136 | fotoNova.setDescricao(produto.getCodigoProdutoPadrao()); |
||
| 137 | setFotoNova(fotoNova); |
||
| 138 | } |
||
| 139 | |||
| 140 | public void uploadArquivoPadrao(final FileUploadEvent e) { |
||
| 141 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 142 | public void execute() { |
||
| 143 | FotoDTO arquivoGerado = gerarArquivo(e.getFile()); |
||
| 144 | arquivoGerado.setDescricao(getFotoNova().getDescricao()); |
||
| 145 | lancarExcecaoCasoArquivoExcedeuLimite(arquivoGerado); |
||
| 146 | arquivoGerado.compactarImagem(); |
||
| 147 | setFotoParaUpload(e.getFile()); |
||
| 148 | uploadFisico(arquivoGerado); |
||
| 149 | LancadorMensagem.lancarSucesso("Imagem adicionada com sucesso."); |
||
| 150 | } |
||
| 151 | }); |
||
| 152 | } |
||
| 153 | |||
| 154 | public void uploadArquivo(final FileUploadEvent e) { |
||
| 155 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 156 | public void execute() { |
||
| 157 | FotoDTO arquivoGerado = gerarArquivo(e.getFile()); |
||
| 158 | arquivoGerado.setDescricao(gerarNovaDescricao(getFotoNova().getDescricao())); |
||
| 159 | lancarExcecaoCasoArquivoExcedeuLimite(arquivoGerado); |
||
| 160 | arquivoGerado.compactarImagem(); |
||
| 161 | setFotoParaUpload(e.getFile()); |
||
| 162 | uploadFisico(arquivoGerado); |
||
| 163 | LancadorMensagem.lancarSucesso("Imagem adicionada com sucesso."); |
||
| 164 | } |
||
| 165 | }); |
||
| 166 | } |
||
| 167 | |||
| 168 | protected String gerarNovaDescricao(String codigo) { |
||
| 169 | Produto produto = new Produto(); |
||
| 170 | produto.setCodigo(codigo); |
||
| 171 | List<String> fotos = fotosProduto(produto); |
||
| 172 | Integer proximoCodigo = 1; |
||
| 173 | for (String foto : fotos) { |
||
| 174 | if (foto.contains("_" + proximoCodigo)) { |
||
| 175 | proximoCodigo++; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | return produto.getCodigoProdutoPadrao() + "_" + proximoCodigo.toString(); |
||
| 179 | } |
||
| 180 | |||
| 181 | public void uploadFisico(FotoDTO arquivoGerado) { |
||
| 182 | String caminho = getCaminhoPastaFotos() + arquivoGerado.getDescricao() + "." + arquivoGerado.getExtensao(); |
||
| 183 | FileOutputStream fos; |
||
| 184 | try { |
||
| 185 | fos = new FileOutputStream(caminho); |
||
| 186 | fos.write(arquivoGerado.getArquivo()); |
||
| 187 | fos.close(); |
||
| 188 | } catch (Exception e1) { |
||
| 189 | e1.printStackTrace(); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | private void lancarExcecaoCasoArquivoExcedeuLimite(FotoDTO arquivoGerado) { |
||
| 194 | if (arquivoGerado.isExcedeuLimite()) { |
||
| 195 | throw new NegocioException("Arquivo: " + arquivoGerado.getDescricao() + " excedeu o limite de 3000 kbytes. "); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | private FotoDTO gerarArquivo(UploadedFile file) { |
||
| 200 | FotoDTO arquivo = new FotoDTO(); |
||
| 201 | arquivo.setArquivo(file.getContent()); |
||
| 202 | arquivo.setDescricao(file.getFileName()); |
||
| 203 | arquivo.setExtensao(ArquivoUtil.retornarExtensaoDoArquivo(file.getFileName())); |
||
| 204 | return arquivo; |
||
| 205 | } |
||
| 206 | |||
| 207 | public void excluirFotoPadrao(final Produto produto) { |
||
| 208 | try { |
||
| 209 | File fotoParaExcluir = new File(getCaminhoPastaFotos() + produto.getCodigoProdutoPadrao() + ".jpg"); |
||
| 210 | if (fotoParaExcluir.exists()) { |
||
| 211 | fotoParaExcluir.delete(); |
||
| 212 | LancadorMensagem.lancarSucesso("Imagem excluída com sucesso."); |
||
| 213 | } else { |
||
| 214 | LancadorMensagem.lancarErro("Erro ao excluir imagem"); |
||
| 215 | } |
||
| 216 | } catch (Exception e1) { |
||
| 217 | LancadorMensagem.lancarErro("Erro ao excluir imagem"); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | public void excluirFoto(final String foto) { |
||
| 222 | try { |
||
| 223 | String codigoFoto = foto.substring(foto.indexOf("fotos/")+6, foto.indexOf("_")); |
||
| 224 | String ordenadorFoto = foto.substring(foto.indexOf("_")+1, foto.indexOf(".")); |
||
| 225 | String caminhoFoto = foto.substring(foto.indexOf("fotos/")+6); |
||
| 226 | File fotoParaExcluir = new File(getCaminhoPastaFotos() + caminhoFoto); |
||
| 227 | if (fotoParaExcluir.exists()) { |
||
| 228 | fotoParaExcluir.delete(); |
||
| 229 | // REORGANIZAR LISTA |
||
| 230 | Integer proximoCodigo = new Integer(ordenadorFoto); |
||
| 231 | proximoCodigo++; |
||
| 232 | File fotoRenomear = new File(getCaminhoPastaFotos() + codigoFoto + "_" + proximoCodigo + ".jpg"); |
||
| 233 | while (fotoRenomear.exists()) { |
||
| 234 | proximoCodigo--; |
||
| 235 | File fotoRenomeada = new File(getCaminhoPastaFotos() + codigoFoto + "_" + proximoCodigo + ".jpg"); |
||
| 236 | fotoRenomear.renameTo(fotoRenomeada); |
||
| 237 | proximoCodigo = proximoCodigo + 2; |
||
| 238 | fotoRenomear = new File(getCaminhoPastaFotos() + codigoFoto + "_" + proximoCodigo + ".jpg"); |
||
| 239 | } |
||
| 240 | LancadorMensagem.lancarSucesso("Imagem excluída com sucesso."); |
||
| 241 | } else { |
||
| 242 | LancadorMensagem.lancarErro("Erro ao excluir imagem"); |
||
| 243 | } |
||
| 244 | } catch (Exception e1) { |
||
| 245 | LancadorMensagem.lancarErro("Erro ao excluir imagem"); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 472 | blopes | 249 | ****/ |
| 250 | |||
| 468 | blopes | 251 | /*********************************/ |
| 252 | |||
| 472 | blopes | 253 | /* |
| 468 | blopes | 254 | private static Integer quantidadeFotosDisponiveis() { |
| 255 | // ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 256 | // String caminhoArquivoPadrao = servletContext.getRealPath("") + File.separator + "imagens" + File.separator + "temp" + File.separator; |
||
| 257 | ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); |
||
| 258 | String caminhoArquivoPadrao = servletContext.getRealPath("") + File.separator + "fotos" + File.separator + "SEM_FOTO.jpg"; |
||
| 259 | |||
| 260 | Integer quantidadeFotos = 0; |
||
| 261 | // String caminhoArquivoPadrao = "src/main/resources/fotos/"; |
||
| 262 | File file = new File(caminhoArquivoPadrao); |
||
| 263 | File[] arquivos = file.listFiles(); |
||
| 264 | for (File fileTmp : arquivos) { |
||
| 265 | quantidadeFotos++; |
||
| 266 | // System.out.println(fileTmp.getName()); |
||
| 267 | } |
||
| 268 | return quantidadeFotos; |
||
| 269 | } |
||
| 270 | |||
| 271 | public static void main(String[] args) { |
||
| 272 | String foto = "espacocase/fotos/010632_1.jpg"; |
||
| 273 | String caminhoFoto = foto.substring(foto.indexOf("fotos/")+6); |
||
| 274 | System.out.println(caminhoFoto); |
||
| 275 | } |
||
| 472 | blopes | 276 | */ |
| 468 | blopes | 277 | |
| 278 | } |