Subversion Repositories Integrator Subversion

Rev

Rev 200 | Details | Compare with Previous | Last modification | View Log | RSS feed

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