/*
 * 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.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import ilog.views.gantt.model.jdbc.IlvJDBCGanttModel;
import ilog.views.gantt.model.table.IlvTableActivity;
import ilog.views.gantt.model.table.IlvTableReservation;
import ilog.views.gantt.model.table.IlvTableResource;
import ilog.views.util.IlvProductUtil;
import ilog.views.util.data.IlvTableModelMappingException;


public class JDBCSample
{
  public static void main(String[] args) throws SQLException, ClassNotFoundException
  {
    // This sample uses JViews Gantt features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Gantt Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(
        IlvProductUtil.JViews_Gantt_Deployment);

    IlvJDBCGanttModel jdbcModel = new IlvJDBCGanttModel();
    Class.forName("org.relique.jdbc.csv.CsvDriver");
    Connection databaseConnection = DriverManager.getConnection("jdbc:relique:csv:data", null, null);
    
    String activitiesQuery = "select ActivityID, ShipName, StartTime, EndTime, Null as ParentID from harbor";
    
    Map<String, String> activitiesMapping = new HashMap<String, String>();  
    activitiesMapping.put(IlvTableActivity.ID_PROPERTY, "ActivityID");
    activitiesMapping.put(IlvTableActivity.NAME_PROPERTY, "ShipName");
    activitiesMapping.put(IlvTableActivity.START_TIME_PROPERTY, "StartTime");
    activitiesMapping.put(IlvTableActivity.END_TIME_PROPERTY, "EndTime");
    activitiesMapping.put(IlvTableActivity.PARENT_ID_PROPERTY, "ParentID");
    jdbcModel.setActivitiesQuery(databaseConnection, activitiesQuery, activitiesMapping);
    String resourcesQuery = "select Dock, '1' as Quantity, Null as ParentID from harbor group by Dock";
    Map<String, String> resourcesMapping = new HashMap<String, String>();
    // as said above, we use the DockName as ID
    resourcesMapping.put(IlvTableResource.ID_PROPERTY, "Dock");
    resourcesMapping.put(IlvTableResource.NAME_PROPERTY, "Dock");
    resourcesMapping.put(IlvTableResource.PARENT_ID_PROPERTY, "ParentID");
    resourcesMapping.put(IlvTableResource.QUANTITY_PROPERTY, "Quantity");
    jdbcModel.setResourcesQuery(databaseConnection, resourcesQuery, resourcesMapping);
    String reservationsQuery = "select ActivityID , Dock from harbor";
    Map<String, String> reservationsMapping = new HashMap<String, String>();
    reservationsMapping.put(IlvTableReservation.ACTIVITY_ID_PROPERTY, "ActivityID");
    reservationsMapping.put(IlvTableReservation.RESOURCE_ID_PROPERTY, "DockID");
    jdbcModel.setReservationsQuery(databaseConnection, reservationsQuery, reservationsMapping);
    try {
      jdbcModel.initializeMapping();
    } catch (IlvTableModelMappingException e) {
      // in case something went wrong                  
    }
  }
}