Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.conceito.frota.controller;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStream;
- import java.util.Date;
- import java.util.HashMap;
- import javax.faces.bean.ManagedBean;
- import javax.faces.bean.ManagedProperty;
- import javax.faces.bean.ViewScoped;
- import javax.faces.component.UIComponent;
- import javax.faces.context.FacesContext;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.FormulaEvaluator;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.ss.usermodel.WorkbookFactory;
- import com.conceito.common.controller.GenericDynamicDataTablePaginatorCrudControllerImpl;
- import com.conceito.common.model.Cliente;
- import com.conceito.frota.model.Abastecimento;
- import com.conceito.frota.model.Combustivel;
- import com.conceito.frota.model.Modelo;
- import com.conceito.frota.model.Motorista;
- import com.conceito.frota.model.Veiculo;
- import com.conceito.frota.service.AbastecimentoService;
- import com.conceito.frota.service.CombustivelService;
- import com.conceito.frota.service.ModeloService;
- import com.conceito.frota.service.VeiculoService;
- import com.conceito.staff.model.Setor;
- import com.conceito.util.FacesUtil;
- import com.conceito.util.Formatador;
- import com.conceito.util.ReportUtil;
- import com.conceito.util.backbeanutil.QueryControl;
- import com.conceito.util.dataFilter.TypeOrder;
- @ManagedBean(name="abastecimentoController")
- @ViewScoped
- public class AbastecimentoController extends GenericDynamicDataTablePaginatorCrudControllerImpl<Abastecimento>{
- private static final long serialVersionUID = 1L;
- @ManagedProperty(name="abastecimentoService", value="#{abastecimentoService}")
- private AbastecimentoService abastecimentoService;
- @ManagedProperty(name="modeloService", value="#{modeloService}")
- private ModeloService modeloService;
- @ManagedProperty(name="veiculoService", value="#{veiculoService}")
- private VeiculoService veiculoService;
- @ManagedProperty(name="combustivelService", value="#{combustivelService}")
- private CombustivelService combustivelService;
- private Date dataInicial;
- private Date dataFinal;
- private Date dataInicio;
- private Date dataFim;
- private String placaRelatorio;
- private Combustivel combustivelRelatorio;
- private Motorista motoristaRelatorio;
- private Setor setorRelatorio;
- private boolean renderedEscolherPeriodo;
- private boolean renderedEscolherFiltro;
- private String acompanhamentoPor = "1";
- private boolean renderedEscolherRelatorio;
- private String tipoRelatorio = "1";
- private QueryControl queryMotoristas;
- private QueryControl queryVeiculo;
- private QueryControl queryCombustivel;
- public void migrarAbastecimentos(InputStream inputstream) throws Exception {
- QueryControl qc = new QueryControl(Cliente.class, this.getQueryService());
- qc.getDataFilter().addEq("id", new Long(106), false);
- qc.atualizarList();
- Cliente cliente = null;
- if(qc.getList().size() > 0) {
- cliente = (Cliente)qc.getList().get(0);
- }else {
- return;
- }
- Workbook excel = WorkbookFactory.create(inputstream);
- inputstream.close();
- int numeroPlanilhas = excel.getNumberOfSheets();
- for(int p = 0; p < numeroPlanilhas; p++) {
- Sheet planilha = excel.getSheetAt(p);
- if(planilha == null) {
- throw new Exception("Planilha não encontrada");
- }
- System.err.println("imax = " + planilha.getLastRowNum());
- for(int i = 1; i < planilha.getLastRowNum(); i++) {
- Row linha = planilha.getRow(i);
- if(linha == null) {
- break;
- }
- FormulaEvaluator evaluator = excel.getCreationHelper().createFormulaEvaluator();
- String modeloTexto = this.getValorCelula(evaluator, linha.getCell(0));
- if(modeloTexto == null || modeloTexto.equals("")) {
- System.err.println("i = " + i);
- return;
- }
- String placaTexto = this.getValorCelula(evaluator, linha.getCell(1));
- String combustivelTexto = this.getValorCelula(evaluator, linha.getCell(2));
- double litros = Double.parseDouble(this.getValorCelula(evaluator, linha.getCell(3)));
- double valor = Double.parseDouble(this.getValorCelula(evaluator, linha.getCell(4)));
- String setorTexto = this.getValorCelula(evaluator, linha.getCell(5));
- Date data = linha.getCell(6).getDateCellValue();
- //Date data = Formatador.toDate(this.getValorCelula(evaluator, linha.getCell(6)));
- qc = new QueryControl(Setor.class, this.getQueryService());
- qc.getDataFilter().addEq("cliente.id", cliente.getId(), false);
- qc.getDataFilter().addEq("nome", setorTexto, false);
- qc.atualizarList();
- Setor setor = null;
- if(qc.getList().size() > 0) {
- setor = (Setor)qc.getList().get(0);
- qc = new QueryControl(Combustivel.class, this.getQueryService());
- qc.getDataFilter().addEq("nome", combustivelTexto, false);
- qc.atualizarList();
- Combustivel combustivel = null;
- if(qc.getList().size() > 0) {
- combustivel = (Combustivel)qc.getList().get(0);
- qc = new QueryControl(Modelo.class, this.getQueryService());
- qc.getDataFilter().addEq("nome", modeloTexto, false);
- qc.atualizarList();
- Modelo modelo = null;
- if(qc.getList().size() > 0) {
- modelo = (Modelo)qc.getList().get(0);
- }else {
- modelo = new Modelo();
- modelo.setNome(modeloTexto);
- modelo.setCombustivel(combustivel);
- this.getModeloService().save(modelo);
- System.out.println("Modelo \"" + modeloTexto + "\" salvo com sucesso");
- }
- qc = new QueryControl(Veiculo.class, this.getQueryService());
- qc.getDataFilter().addEq("cliente.id", cliente.getId(), false);
- qc.getDataFilter().addEq("modelo.id", modelo.getId(), false);
- qc.getDataFilter().addEq("placa", placaTexto, false);
- qc.atualizarList();
- Veiculo veiculo = null;
- if(qc.getList().size() > 0) {
- veiculo = (Veiculo)qc.getList().get(0);
- }else {
- veiculo = new Veiculo();
- veiculo.setModelo(modelo);
- veiculo.setPlaca(placaTexto);
- veiculo.setSetor(setor);
- veiculo.setCliente(cliente);
- this.getVeiculoService().save(veiculo);
- System.out.println("Veículo \"" + placaTexto + "\" salvo com sucesso");
- }
- qc = new QueryControl(Abastecimento.class, this.getQueryService());
- qc.getDataFilter().addEq("valor", valor, false);
- qc.getDataFilter().addEq("quantidadeLitros", litros, false);
- qc.getDataFilter().addEq("veiculo.id", veiculo.getId(), false);
- qc.getDataFilter().addEq("combustivel.id", combustivel.getId(), false);
- qc.getDataFilter().addEq("data", data, false);
- qc.atualizarList();
- Abastecimento abastecimento = null;
- if(qc.getList().size() > 0) {
- abastecimento = (Abastecimento)qc.getList().get(0);
- System.err.println("Já existe abastecimento (" + i + ", " + abastecimento.getId() + ", " + placaTexto + ", " + Formatador.getDate(data) + ", " + combustivelTexto + ", " + valor + ", " + litros + ")");
- }else {
- abastecimento = new Abastecimento();
- abastecimento.setValor(valor);
- abastecimento.setQuantidadeLitros(litros);
- abastecimento.setVeiculo(veiculo);
- abastecimento.setCombustivel(combustivel);
- abastecimento.setData(data);
- this.getAbastecimentoService().save(abastecimento);
- System.out.println("Abastecimento (" + i + ", " + placaTexto + ", " + Formatador.getDate(data) + ", " + combustivelTexto + ", " + valor + ", " + litros + ") salvo com sucesso");
- }
- }else {
- System.err.println("Não foi possível encontrar o combustível \"" + combustivelTexto + "\"");
- }
- }else {
- System.err.println("Não foi possível encontrar o setor \"" + setorTexto + "\"");
- }
- }
- }
- }
- public String getValorCelula(FormulaEvaluator evaluator, Cell celula) {
- Cell cell = evaluator.evaluateInCell(celula);
- if(cell == null) return "";
- String retorno = "";
- if(cell.getCellType() == Cell.CELL_TYPE_BLANK) {
- retorno = "";
- }else if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
- retorno = cell.getStringCellValue();
- }else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
- retorno = cell.getNumericCellValue() + "";
- }
- return retorno;
- }
- @Override
- public void init() {
- super.init();
- File file = new File("C:\\Users\\Carlos\\Desktop\\Igarassu_Combustivel_2015.xlsx");
- try {
- FileInputStream is = new FileInputStream(file);
- this.migrarAbastecimentos(is);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- this.setQueryMotoristas(new QueryControl(Motorista.class, this.getQueryService()));
- this.getQueryMotoristas().getDataFilter().addEq("pessoa.cliente.id", this.getCliente().getId(), false);
- this.getQueryMotoristas().getDataFilter().addOrder("pessoa.nome", TypeOrder.ASC);
- this.getQueryMotoristas().atualizarList();
- this.setQueryVeiculo(new QueryControl(Veiculo.class, this.getQueryService()));
- this.getQueryVeiculo().getDataFilter().addEq("cliente.id", this.getCliente().getId(), false);
- this.getQueryVeiculo().getDataFilter().addOrder("placa", TypeOrder.ASC);
- this.getQueryVeiculo().atualizarList();
- }
- @Override
- public void incluirShow() {
- if (this.getQueryVeiculo().getList().size() > 0) {
- super.incluirShow();
- this.getSelected().setVeiculo((Veiculo) this.getQueryVeiculo().getList().get(0));
- this.atualizarCombustivelFiltrado();
- }else {
- FacesUtil.exibirMensagemAlerta("Nenhum veículo cadastrado");
- }
- }
- public void alterarShow() {
- Abastecimento itemSelected = this.getOneItemSelected();
- if (itemSelected != null) {
- FacesUtil.cleanSubmittedValues((UIComponent) FacesContext.getCurrentInstance().getViewRoot());
- this.setSelected(itemSelected);
- this.setCurrentState(ALTERACAO_STATE);
- this.atualizarCombustivelFiltrado();
- }
- this.afterShow();
- }
- public void atualizarCombustivelFiltrado() {
- this.setQueryCombustivel(new QueryControl(Combustivel.class, this.getQueryService()));
- Combustivel combustivel = this.getSelected().getVeiculo().getModelo().getCombustivel();
- if(combustivel != null) {
- if(combustivel.getId() == 13) {
- this.getQueryCombustivel().getDataFilter().addEq("nome", "Gasolina", false);
- this.getQueryCombustivel().getDataFilter().addEq("nome", "Álcool", false);
- }else {
- this.getQueryCombustivel().getDataFilter().addEq("id", combustivel.getId(), false);
- }
- }
- this.getQueryCombustivel().getDataFilter().addOrder("nome", TypeOrder.ASC);
- this.getQueryCombustivel().atualizarList();
- if (this.getQueryCombustivel().getList().size() > 0) {
- if(this.isInclusaoState()){
- this.getSelected().setCombustivel((Combustivel) this.getQueryCombustivel().getList().get(0));
- }else {
- if(this.getSelected().getCombustivel() == null) {
- this.getSelected().setCombustivel((Combustivel) this.getQueryCombustivel().getList().get(0));
- }
- }
- } else {
- FacesUtil.exibirMensagemAlerta("Nenhum veículo adicionado");
- }
- }
- @Override
- public void clearInputs() {
- this.setSelected(new Abastecimento());
- }
- @Override
- public void setFilterDefinition() {
- this.getDataFilter().clear();
- this.getDataFilter().addEq("veiculo.cliente.id", this.getCliente().getId());
- this.getDataFilter().addOrder("data", TypeOrder.DESC);
- }
- @Override
- public void configureDataColumns() {
- this.addDataColumn("id", "id");
- this.addDataColumn("Data", "dataAsString");
- this.addDataColumn("Placa", "veiculo.placa");
- this.addDataColumn("Setor do Veículo", "veiculo.setor.nome");
- this.addDataColumn("Motorista", "nomeMotorista");
- this.addDataColumn("Combustível", "combustivel.nome");
- this.addDataColumn("Valor do Abastecimento", "valorAsString");
- this.addDataColumn("Qtd Litros", "quantidadeLitrosAsString");
- this.addDataColumn("Quilometragem Atual", "kilometragemAsString");
- this.addDataColumn("Quilometragem Aquisição", "kmAquisicaoAsString");
- }
- public void imprimirRelatorioShow() {
- this.setTipoRelatorio("1");
- this.setRenderedEscolherRelatorio(true);
- }
- public void imprimirRelatorio() {
- if(this.getTipoRelatorio().equals("1")) {
- this.setRenderedEscolherFiltro(true);
- }else if(this.getTipoRelatorio().equals("2")) {
- this.setRenderedEscolherFiltro(true);
- }else if(this.getTipoRelatorio().equals("3")) {
- this.setRenderedEscolherPeriodo(true);
- this.setAcompanhamentoPor("1");
- }
- this.setRenderedEscolherRelatorio(false);
- }
- public void imprimirAbastecimento() {
- if(this.getTipoRelatorio().equals("1")) {
- try {
- ReportUtil.printWithConnection("abastecimento_frota.jrxml", this.getParameters(), this.getDataSource().getConnection(), "Resumo de Abastecimento de Frota", "pdf");
- this.setRenderedEscolherFiltro(false);
- } catch (Exception e) {
- FacesUtil.exibirMensagemAlerta("Não foi possível imprimir o relatório");
- }
- }else if(this.getTipoRelatorio().equals("2")) {
- try {
- ReportUtil.printWithConnection("relacao_abastecimentos_frota.jrxml", this.getParameters(), this.getDataSource().getConnection(), "Relação de Abastecimentos de Frota", "pdf");
- this.setRenderedEscolherFiltro(false);
- } catch (Exception e) {
- FacesUtil.exibirMensagemAlerta("Não foi possível imprimir o relatório");
- }
- }else if(this.getTipoRelatorio().equals("3")) {
- if(this.getAcompanhamentoPor().equals("1")) {
- this.imprimirAcompanhamentoPorCombustivel();
- }else if(this.getAcompanhamentoPor().equals("2")) {
- this.imprimirAcompanhamentoPorSetor();
- }
- this.setRenderedEscolherPeriodo(false);
- }
- }
- public void imprimirAcompanhamentoPorCombustivel() {
- try {
- ReportUtil.printWithConnection("acompanhamento_combustiveis_abastecimento.jrxml", this.getParametersAcompanhamento(), this.getDataSource().getConnection(), "Acompanhamento de Abastecimentos por Combustível", "pdf");
- this.setRenderedEscolherPeriodo(false);
- } catch (Exception e) {
- FacesUtil.exibirMensagemAlerta("Não foi possível imprimir o relatório");
- }
- }
- public void imprimirAcompanhamentoPorSetor() {
- try {
- ReportUtil.printWithConnection("acompanhamento_setores_abastecimento.jrxml", this.getParametersAcompanhamento(), this.getDataSource().getConnection(), "Acompanhamento de Abastecimentos por Setor", "pdf");
- this.setRenderedEscolherPeriodo(false);
- } catch (Exception e) {
- FacesUtil.exibirMensagemAlerta("Não foi possível imprimir o relatório");
- }
- }
- @Override
- public void salvar() {
- super.salvar();
- }
- public HashMap<String, Object> getParameters() {
- HashMap<String, Object> hash = new HashMap<String, Object>();
- hash.put("dataInicio", this.getDataInicial());
- hash.put("dataFim", this.getDataFinal());
- hash.put("clienteId", getCliente().getId());
- hash.put("nomeCliente", getCliente().getNome());
- hash.put("logoCliente", this.getCliente().getCaminhoLogoReport());
- String filtro = "";
- if (getPlacaRelatorio() != "") {
- filtro = filtro + "Placa: " + getPlacaRelatorio() + ". ";
- }
- if (getCombustivelRelatorio() != null) {
- hash.put("combustivel", getCombustivelRelatorio().getId());
- filtro = filtro + "Combustivel: " + getCombustivelRelatorio().getNome() + ". ";
- } else {
- hash.put("combustivel", new Long(-1));
- }
- if (getMotoristaRelatorio() != null) {
- hash.put("motorista", getMotoristaRelatorio().getPessoa().getId());
- filtro = filtro + "Motorista: " + getMotoristaRelatorio().getPessoa().getNome() + ". ";
- } else {
- hash.put("motorista", new Long(-1));
- }
- if (getSetorRelatorio() != null) {
- hash.put("setor", getSetorRelatorio().getId());
- filtro = filtro + "Setor: " + getSetorRelatorio().getNome() + ". ";
- } else {
- hash.put("setor", new Long(-1));
- }
- hash.put("placa", "%" + getPlacaRelatorio() + "%");
- hash.put("filtro", filtro);
- return hash;
- }
- public HashMap<String, Object> getParametersAcompanhamento() {
- HashMap<String, Object> hash = new HashMap<String, Object>();
- hash.put("idCliente", getCliente().getId());
- hash.put("cliente", getCliente().getNome());
- hash.put("data_inicio", Formatador.getDate(this.getDataInicio()));
- hash.put("data_fim", Formatador.getDate(this.getDataFim()));
- hash.put("logoCliente", this.getCliente().getCaminhoLogoReport());
- return hash;
- }
- /*public List<Combustivel> getCombustivelFiltrado() {
- List<Combustivel> retorno = new ArrayList<Combustivel>();
- QueryControl qc = new QueryControl(Combustivel.class, this.getQueryService());
- if(this.getSelected().getVeiculo().getModelo().getCombustivel().getId() == 13) {
- }else {
- }
- qc.getDataFilter().addEq("abastece", true, false);
- qc.getDataFilter().addOrder("nome", TypeOrder.ASC);
- qc.atualizarList();
- Iterator<Object> iterator = qc.getList().iterator();
- while(iterator.hasNext()) {
- retorno.add((Combustivel)iterator.next());
- }
- return retorno;
- }*/
- public void voltar2() {
- this.setRenderedEscolherPeriodo(false);
- this.setRenderedEscolherFiltro(false);
- this.setRenderedEscolherRelatorio(false);
- }
- public void setAbastecimentoService(AbastecimentoService abastecimentoService) {
- this.abastecimentoService = abastecimentoService;
- this.setService(abastecimentoService);
- }
- public AbastecimentoService getAbastecimentoService() {
- return this.abastecimentoService;
- }
- public Date getDataInicial() {
- return this.dataInicial;
- }
- public void setDataInicial(Date dataInicial) {
- this.dataInicial = dataInicial;
- }
- public Date getDataFinal() {
- return this.dataFinal;
- }
- public void setDataFinal(Date dataFinal) {
- this.dataFinal = dataFinal;
- }
- public boolean isRenderedEscolherFiltro() {
- return this.renderedEscolherFiltro;
- }
- public void setRenderedEscolherFiltro(boolean renderedEscolherFiltro) {
- this.renderedEscolherFiltro = renderedEscolherFiltro;
- }
- public Combustivel getCombustivelRelatorio() {
- return combustivelRelatorio;
- }
- public void setCombustivelRelatorio(Combustivel combustivelRelatorio) {
- this.combustivelRelatorio = combustivelRelatorio;
- }
- public String getPlacaRelatorio() {
- return placaRelatorio;
- }
- public void setPlacaRelatorio(String placaRelatorio) {
- this.placaRelatorio = placaRelatorio;
- }
- public Motorista getMotoristaRelatorio() {
- return motoristaRelatorio;
- }
- public void setMotoristaRelatorio(Motorista motoristaRelatorio) {
- this.motoristaRelatorio = motoristaRelatorio;
- }
- public Setor getSetorRelatorio() {
- return setorRelatorio;
- }
- public void setSetorRelatorio(Setor setorRelatorio) {
- this.setorRelatorio = setorRelatorio;
- }
- public Date getDataInicio() {
- return dataInicio;
- }
- public void setDataInicio(Date dataInicio) {
- this.dataInicio = dataInicio;
- }
- public Date getDataFim() {
- return dataFim;
- }
- public void setDataFim(Date dataFim) {
- this.dataFim = dataFim;
- }
- public boolean isRenderedEscolherPeriodo() {
- return renderedEscolherPeriodo;
- }
- public void setRenderedEscolherPeriodo(boolean renderedEscolherPeriodo) {
- this.renderedEscolherPeriodo = renderedEscolherPeriodo;
- }
- public String getAcompanhamentoPor() {
- return acompanhamentoPor;
- }
- public void setAcompanhamentoPor(String acompanhamentoPor) {
- this.acompanhamentoPor = acompanhamentoPor;
- }
- public boolean isRenderedEscolherRelatorio() {
- return renderedEscolherRelatorio;
- }
- public void setRenderedEscolherRelatorio(boolean renderedEscolherRelatorio) {
- this.renderedEscolherRelatorio = renderedEscolherRelatorio;
- }
- public String getTipoRelatorio() {
- return tipoRelatorio;
- }
- public void setTipoRelatorio(String tipoRelatorio) {
- this.tipoRelatorio = tipoRelatorio;
- }
- public QueryControl getQueryMotoristas() {
- return queryMotoristas;
- }
- public void setQueryMotoristas(QueryControl queryMotoristas) {
- this.queryMotoristas = queryMotoristas;
- }
- public QueryControl getQueryVeiculo() {
- return queryVeiculo;
- }
- public void setQueryVeiculo(QueryControl queryVeiculo) {
- this.queryVeiculo = queryVeiculo;
- }
- public QueryControl getQueryCombustivel() {
- return queryCombustivel;
- }
- public void setQueryCombustivel(QueryControl queryCombustivel) {
- this.queryCombustivel = queryCombustivel;
- }
- public ModeloService getModeloService() {
- return modeloService;
- }
- public void setModeloService(ModeloService modeloService) {
- this.modeloService = modeloService;
- }
- public VeiculoService getVeiculoService() {
- return veiculoService;
- }
- public void setVeiculoService(VeiculoService veiculoService) {
- this.veiculoService = veiculoService;
- }
- public CombustivelService getCombustivelService() {
- return combustivelService;
- }
- public void setCombustivelService(CombustivelService combustivelService) {
- this.combustivelService = combustivelService;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement