package br.com.ec.controller.managedbean;
import java.io.InputStream;
import java.io.Serializable;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;
import org.springframework.context.annotation.Scope;
import br.com.ec.core.util.TipoExtensao;
import br.com.ec.domain.dto.ArquivoDTO;
import br.com.ec.domain.dto.ArquivoFuncionarioDTO;
import br.com.ec.domain.model.tipos.TipoComissionamento;
import br.com.ec.domain.service.arquivo.ArquivoService;
import br.com.ec.relatorio.util.RelatorioUtils;
import br.com.ec.web.exception.VerificadorLancamentoException;
import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean;
import br.com.ec.web.exception.VerificadorLancamentoException.CommandRelatorioStreamedContent;
import br.com.ec.web.message.LancadorMensagem;
@Named
@Scope
("view")
public class ArquivoBean
implements Serializable {
private static final long serialVersionUID = 1L
;
private ArquivoDTO arquivo
;
private String tipoComissionamentoSelecionado
;
private UploadedFile arquivoParaUpload
;
private ArquivoService arquivoService
;
@Inject
public ArquivoBean
(ArquivoService arquivoService
) {
this.
arquivoService = arquivoService
;
}
public ArquivoDTO getArquivo
() {
return arquivo
;
}
public void setArquivo
(ArquivoDTO arquivo
) {
this.
arquivo = arquivo
;
}
public String getTipoComissionamentoSelecionado
() {
return tipoComissionamentoSelecionado
;
}
public void setTipoComissionamentoSelecionado
(String tipoComissionamentoSelecionado
) {
this.
tipoComissionamentoSelecionado = tipoComissionamentoSelecionado
;
}
public UploadedFile getArquivoParaUpload
() {
return arquivoParaUpload
;
}
public void setArquivoParaUpload
(UploadedFile arquivoParaUpload
) {
this.
arquivoParaUpload = arquivoParaUpload
;
}
public void uploadArquivoEspacoCase
(final FileUploadEvent e
) {
new VerificadorLancamentoException
().
tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean
() {
public void execute
() {
setTipoComissionamentoSelecionado
(TipoComissionamento.
COMISSSIONAMENTO_ESPACO_CASE.
getValor());
setArquivo
(arquivoService.
separarRecibos(new ArquivoDTO
(e.
getFile()), getTipoComissionamentoSelecionado
(),
((ServletContext
)FacesContext.
getCurrentInstance().
getExternalContext().
getContext())));
}
});
}
public void uploadArquivoCasaDasCapas
(final FileUploadEvent e
) {
new VerificadorLancamentoException
().
tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean
() {
public void execute
() {
setTipoComissionamentoSelecionado
(TipoComissionamento.
COMISSSIONAMENTO_CASA_DAS_CAPAS.
getValor());
setArquivo
(arquivoService.
separarRecibos(new ArquivoDTO
(e.
getFile()), getTipoComissionamentoSelecionado
(),
((ServletContext
)FacesContext.
getCurrentInstance().
getExternalContext().
getContext())));
}
});
}
public void uploadArquivoVivo
(final FileUploadEvent e
) {
new VerificadorLancamentoException
().
tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean
() {
public void execute
() {
setTipoComissionamentoSelecionado
(TipoComissionamento.
COMISSSIONAMENTO_VIVO.
getValor());
setArquivo
(arquivoService.
separarRecibos(new ArquivoDTO
(e.
getFile()), getTipoComissionamentoSelecionado
(),
((ServletContext
)FacesContext.
getCurrentInstance().
getExternalContext().
getContext())));
}
});
}
public void uploadArquivoFarmacia
(final FileUploadEvent e
) {
new VerificadorLancamentoException
().
tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean
() {
public void execute
() {
setTipoComissionamentoSelecionado
(TipoComissionamento.
COMISSSIONAMENTO_FARMACIA.
getValor());
setArquivo
(arquivoService.
separarRecibos(new ArquivoDTO
(e.
getFile()), getTipoComissionamentoSelecionado
(),
((ServletContext
)FacesContext.
getCurrentInstance().
getExternalContext().
getContext())));
}
});
}
public StreamedContent downloadArquivo
(final ArquivoDTO arquivoDTO
) {
return new VerificadorLancamentoException
().
verificarLancamentoException(new CommandRelatorioStreamedContent
() {
@
Override
public StreamedContent execute
() {
return RelatorioUtils.
gerarArquivo(arquivoDTO.
getArquivo(),
"arquivo", TipoExtensao.
parse(arquivoDTO.
getExtensao()).
getValue());
}
});
}
public StreamedContent downloadArquivo
(final byte[] arquivo,
final String extensao
) {
return new VerificadorLancamentoException
().
verificarLancamentoException(new CommandRelatorioStreamedContent
() {
@
Override
public StreamedContent execute
() {
return RelatorioUtils.
gerarArquivo(arquivo,
"recibo", TipoExtensao.
parse(extensao
).
getValue());
}
});
}
public void retirarArquivo
() {
setArquivo
(null);
}
public StreamedContent emitirRecibos
(final ArquivoDTO arquivoDTO
) throws Exception {
return new VerificadorLancamentoException
().
verificarLancamentoException(new CommandRelatorioStreamedContent
() {
@
Override
public StreamedContent execute
() {
return arquivoService.
gerarRecibos(arquivoDTO,
((ServletContext
)FacesContext.
getCurrentInstance().
getExternalContext().
getContext()));
}
});
}
public void enviarRecibos
(final ArquivoDTO arquivoDTO
) throws Exception {
new VerificadorLancamentoException
().
tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean
() {
public void execute
() {
arquivoService.
enviarRecibos(arquivoDTO, getTipoComissionamentoSelecionado
(),
((ServletContext
)FacesContext.
getCurrentInstance().
getExternalContext().
getContext()));
LancadorMensagem.
lancarSucesso("RECIBOS ENVIADOS COM SUCESSO!");
}
});
}
public StreamedContent processarRecibos
(final ArquivoDTO arquivoDTO
) throws Exception {
return new VerificadorLancamentoException
().
verificarLancamentoException(new CommandRelatorioStreamedContent
() {
@
Override
public StreamedContent execute
() {
arquivoService.
separarEnviarRecibos(arquivoDTO, getTipoComissionamentoSelecionado
(),
((ServletContext
)FacesContext.
getCurrentInstance().
getExternalContext().
getContext()));
return arquivoService.
gerarRecibos(arquivoDTO,
((ServletContext
)FacesContext.
getCurrentInstance().
getExternalContext().
getContext()));
}
});
}
public StreamedContent downloadPastaArquivos
(final String nomeArquivo,
final String extensao
) {
return new VerificadorLancamentoException
().
verificarLancamentoException(new CommandRelatorioStreamedContent
() {
@
Override
public StreamedContent execute
() {
InputStream arquivo = FacesContext.
getCurrentInstance().
getExternalContext().
getResourceAsStream("/arquivos/" + nomeArquivo +
"." + extensao
);
return new DefaultStreamedContent
(arquivo, TipoExtensao.
parse(extensao
).
getDescricao(), nomeArquivo +
"." + extensao
);
// return new DefaultStreamedContent(arquivo, TipoExtensao.PDF.getDescricao(), nomeArquivo + "." + extensao);
}
});
}
public void selecionarTodosParaEnviarEmail
() {
new VerificadorLancamentoException
().
tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean
() {
public void execute
() {
for (ArquivoFuncionarioDTO arquivoFuncionario : getArquivo
().
getArquivosFuncionarios()) {
if (arquivoFuncionario.
getEnviarEmail()) {
arquivoFuncionario.
setEnviarEmail(false);
} else {
arquivoFuncionario.
setEnviarEmail(true);
}
}
}
});
}
}