Subversion Repositories Integrator Subversion

Rev

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

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