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