Subversion Repositories Integrator Subversion

Rev

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