Created
February 7, 2017 12:54
-
-
Save maikelsperandio/7f6d8d8f858181aee9984ea64579cb97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EntregaController extends ControllerBase { | |
private String retornoIndex = "/pages/entregas/index?faces-redirect=true"; | |
private @Getter @Setter Date data; | |
private @Getter @Setter Entregador entregador; | |
private @Getter @Setter String periodoInicial; | |
private @Getter @Setter String periodoFinal; | |
private StreamedContent relatorio; | |
private @Getter @Setter List<Entrega> listaEntregas; | |
private @Autowired PedidoService pedidoService; | |
private @Autowired EntregadorService entregadorService; | |
private @Autowired EntregaService entregaService; | |
private @Autowired RelatorioBase relatorioBase; | |
@Override | |
public String setUp() { | |
this.data = null; | |
this.entregador = null; | |
this.listaEntregas = null; | |
this.relatorio = null; | |
return retornoIndex; | |
} | |
public List<Entregador> entregadorAutoComplete(String query){ | |
return entregadorService.consultaEntregadoresPorNomeParcial(query); | |
} | |
private boolean isPeriodoValido(String periodo){ | |
String array[] = periodo.split(":"); | |
if( (Integer.parseInt(array[0]) >= 24) || (Integer.parseInt(array[1]) >= 59) ){ | |
errorMessage("Período informado é inválido: " + periodo); | |
return false; | |
} | |
return true; | |
} | |
public void consultaEntregas(){ | |
if(this.data == null){ | |
warnMessage("Informe a data."); | |
return; | |
} | |
if(this.entregador == null){ | |
warnMessage("Informe o entregador."); | |
return; | |
} | |
if(StringUtils.isEmpty(this.periodoInicial)){ | |
warnMessage("Informe o período inicial."); | |
return; | |
} | |
if(StringUtils.isEmpty(this.periodoFinal)){ | |
warnMessage("Informe o período final."); | |
return; | |
} | |
if(this.isPeriodoValido(this.periodoInicial) && this.isPeriodoValido(this.periodoFinal)){ | |
this.listaEntregas = pedidoService.carregaEntregasPorDataEEntregador(this.data, this.entregador, this.periodoInicial, this.periodoFinal); | |
if(CollectionUtils.isEmpty(this.listaEntregas)){ | |
warnMessage("Não foram encontrados registros com os parâmetros informados."); | |
return; | |
} | |
} | |
} | |
public StreamedContent getRelatorio(){ | |
Map<String, Object> parameters = new HashMap<String, Object>(); | |
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(this.listaEntregas); | |
parameters.put("dataEntrega", new SimpleDateFormat("dd/MM/yyyy").format(this.data)); | |
parameters.put("entregador", this.entregador.getNome()); | |
try { | |
relatorio = new DefaultStreamedContent(relatorioBase.geraRelatorioPDF(parameters, "entrega", ds), "application/pdf", "relatorio"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return relatorio; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment