/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

package jdbcdemo;

import java.sql.SQLException;
import java.text.MessageFormat;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIPanel;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.ServletContext;

import ilog.views.chart.data.IlvJDBCDataSource;
import ilog.views.util.IlvResourceUtil;

public class JDBCBean {

  private IlvJDBCDataSource JDBCDS;
  private String nameDS;
  private UIPanel panel = new UIPanel();

  public JDBCBean() {
    panel.setRendered(false); // hides the chart panel
  }

  protected void initJDBCDataSource(String xlsDSName) throws Exception {
    ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
    JDBCDS = new IlvJDBCDataSource("jdbc:relique:csv:" + ctx.getRealPath("data"), "", "", "org.relique.jdbc.csv.CsvDriver",
        "select * from " + xlsDSName, -1, 3);
    try {
      JDBCDS.executeQuery();
      JDBCDS.endConnection();
      panel.setRendered(true);
    } catch (IllegalArgumentException e) {
      panel.setRendered(false);
      Exception newExc = new Exception(e.getMessage());
      newExc.initCause(e);
      throw newExc;
    } catch (SQLException e) {
      panel.setRendered(false);
      String msg = IlvResourceUtil.getCurrentLocaleString(JDBCBean.class, "wrongDataSourceName");
      Exception newExc = new Exception(MessageFormat.format(msg, xlsDSName));
      newExc.initCause(e);
      throw newExc;
    }
  }

  public void nameEntered(ValueChangeEvent evt) {
    try {
      if (evt.getNewValue() != null) {
        nameDS = evt.getNewValue().toString();
        initJDBCDataSource(nameDS);
      }
    } catch (Exception e) {
      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
    }
  }

  /**
   * @return Returns the jDBCDS.
   */
  public IlvJDBCDataSource getJDBCDS() {
    return JDBCDS;
  }

  /**
   * @param jdbcds
   *          The jDBCDS to set.
   */
  public void setJDBCDS(IlvJDBCDataSource jdbcds) {
    JDBCDS = jdbcds;
  }

  /**
   * JSF Action to query a refresh of the datas.
   */
  public String refreshDataSource(ActionEvent evt) throws Exception {
    try {
      initJDBCDataSource(nameDS);
    } catch (Exception e) {
      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
    }
    return null;
  }

  /**
   * @return Returns the panel.
   */
  public UIPanel getPanel() {
    return panel;
  }

  /**
   * @param panel
   *          The panel to set.
   */
  public void setPanel(UIPanel panel) {
    this.panel = panel;
  }
}