Subversion Repositories Integrator Subversion

Rev

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

Rev Author Line No. Line
1 espaco 1
package br.com.ec.controller.managedbean;
2
 
3
import java.io.InputStream;
4
import java.io.Serializable;
5
 
6
import javax.faces.context.FacesContext;
7
import javax.inject.Inject;
8
import javax.inject.Named;
9
import javax.servlet.ServletContext;
10
 
11
import org.primefaces.event.FileUploadEvent;
12
import org.primefaces.model.DefaultStreamedContent;
13
import org.primefaces.model.StreamedContent;
14
import org.primefaces.model.file.UploadedFile;
15
import org.springframework.context.annotation.Scope;
16
 
17
import br.com.ec.domain.dto.ArquivoDTO;
18
import br.com.ec.domain.dto.ArquivoFuncionarioDTO;
19
import br.com.ec.domain.model.tipos.TipoComissionamento;
20
import br.com.ec.domain.service.arquivo.ArquivoService;
21
import br.edu.cesmac.core.util.TipoExtensao;
22
import br.edu.cesmac.relatorio.util.RelatorioUtils;
23
import br.edu.cesmac.web.exception.VerificadorLancamentoException;
24
import br.edu.cesmac.web.exception.VerificadorLancamentoException.CommandBean;
25
import br.edu.cesmac.web.exception.VerificadorLancamentoException.CommandRelatorioStreamedContent;
26
import br.edu.cesmac.web.message.LancadorMensagem;
27
 
28
@Named
29
@Scope("view")
30
public class ArquivoBean implements Serializable {
31
 
32
        private static final long serialVersionUID = 1L;
33
 
34
        private ArquivoDTO arquivo;
35
        private String tipoComissionamentoSelecionado;
36
 
37
        private UploadedFile arquivoParaUpload;
38
 
39
        private ArquivoService arquivoService;
40
 
41
        @Inject
42
        public ArquivoBean(ArquivoService arquivoService) {
43
                this.arquivoService = arquivoService;
44
        }
45
 
46
        public ArquivoDTO getArquivo() {
47
                return arquivo;
48
        }
49
        public void setArquivo(ArquivoDTO arquivo) {
50
                this.arquivo = arquivo;
51
        }
52
 
53
        public String getTipoComissionamentoSelecionado() {
54
                return tipoComissionamentoSelecionado;
55
        }
56
        public void setTipoComissionamentoSelecionado(String tipoComissionamentoSelecionado) {
57
                this.tipoComissionamentoSelecionado = tipoComissionamentoSelecionado;
58
        }
59
 
60
        public UploadedFile getArquivoParaUpload() {
61
                return arquivoParaUpload;
62
        }
63
        public void setArquivoParaUpload(UploadedFile arquivoParaUpload) {
64
                this.arquivoParaUpload = arquivoParaUpload;
65
        }
66
 
67
        public void uploadArquivoEspacoCase(final FileUploadEvent e) {
68
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
69
                        public void execute() {
70
                                setTipoComissionamentoSelecionado(TipoComissionamento.COMISSSIONAMENTO_ESPACO_CASE.getValor());
71
                                setArquivo(arquivoService.separarRecibos(new ArquivoDTO(e.getFile()), getTipoComissionamentoSelecionado(), ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext())));
72
                        }
73
                });
74
    }
75
 
76
        public void uploadArquivoVivo(final FileUploadEvent e) {
77
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
78
                        public void execute() {
79
                                setTipoComissionamentoSelecionado(TipoComissionamento.COMISSSIONAMENTO_VIVO.getValor());
80
                                setArquivo(arquivoService.separarRecibos(new ArquivoDTO(e.getFile()), getTipoComissionamentoSelecionado(), ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext())));
81
                        }
82
                });
83
    }
84
 
85
        public void uploadArquivoFarmacia(final FileUploadEvent e) {
86
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
87
                        public void execute() {
88
                                setTipoComissionamentoSelecionado(TipoComissionamento.COMISSSIONAMENTO_FARMACIA.getValor());
89
                                setArquivo(arquivoService.separarRecibos(new ArquivoDTO(e.getFile()), getTipoComissionamentoSelecionado(), ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext())));
90
                        }
91
                });
92
    }
93
 
94
        public StreamedContent downloadArquivo(final ArquivoDTO arquivoDTO) {
95
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
96
                        @Override
97
                        public StreamedContent execute() {
98
                                return RelatorioUtils.gerarArquivo(arquivoDTO.getArquivo(), "arquivo", TipoExtensao.parse(arquivoDTO.getExtensao()).getValue());
99
                        }
100
                });
101
        }
102
 
103
        public StreamedContent downloadArquivo(final byte[] arquivo, final String extensao) {
104
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
105
                        @Override
106
                        public StreamedContent execute() {
107
                                return RelatorioUtils.gerarArquivo(arquivo, "recibo", TipoExtensao.parse(extensao).getValue());
108
                        }
109
                });
110
        }
111
 
112
        public void retirarArquivo() {
113
                setArquivo(null);
114
        }
115
 
116
        public StreamedContent emitirRecibos(final ArquivoDTO arquivoDTO) throws Exception {
117
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
118
                        @Override
119
                        public StreamedContent execute() {
120
                                return arquivoService.gerarRecibos(arquivoDTO, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()));
121
                        }
122
                });
123
        }
124
 
125
        public void enviarRecibos(final ArquivoDTO arquivoDTO) throws Exception {
126
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
127
                        public void execute() {
128
                                arquivoService.enviarRecibos(arquivoDTO, getTipoComissionamentoSelecionado(), ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()));
129
                                LancadorMensagem.lancarSucesso("RECIBOS ENVIADOS COM SUCESSO!");
130
                        }
131
                });
132
        }
133
 
134
        public StreamedContent processarRecibos(final ArquivoDTO arquivoDTO) throws Exception {
135
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
136
                        @Override
137
                        public StreamedContent execute() {
138
                                arquivoService.separarEnviarRecibos(arquivoDTO, getTipoComissionamentoSelecionado(), ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()));
139
                                return arquivoService.gerarRecibos(arquivoDTO, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()));
140
                        }
141
                });
142
        }
143
 
144
        public StreamedContent downloadPastaArquivos(final String nomeArquivo, final String extensao) {
145
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
146
                        @Override
147
                        public StreamedContent execute() {
148
                                InputStream arquivo = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/arquivos/" + nomeArquivo + "." + extensao);
149
                        return new DefaultStreamedContent(arquivo, TipoExtensao.parse(extensao).getDescricao(), nomeArquivo + "." + extensao);
150
//                      return new DefaultStreamedContent(arquivo, TipoExtensao.PDF.getDescricao(), nomeArquivo + "." + extensao);
151
                        }
152
                });
153
        }
154
 
155
        public void selecionarTodosParaEnviarEmail() {
156
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
157
                        public void execute() {
158
                                for (ArquivoFuncionarioDTO arquivoFuncionario : getArquivo().getArquivosFuncionarios()) {
159
                                        if (arquivoFuncionario.getEnviarEmail()) {
160
                                                arquivoFuncionario.setEnviarEmail(false);
161
                                        } else {
162
                                                arquivoFuncionario.setEnviarEmail(true);
163
                                        }
164
                                }
165
                        }
166
                });
167
        }
168
 
169
}