Subversion Repositories Integrator Subversion

Rev

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