Subversion Repositories Integrator Subversion

Rev

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