/*
 * 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.text.DateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import ilog.views.gantt.IlvActivity;
import ilog.views.gantt.IlvGanttChart;
import ilog.views.gantt.IlvGanttModel;
import ilog.views.gantt.IlvHierarchyChart;
import ilog.views.gantt.servlet.IlvGanttServletSupport;
import ilog.views.gantt.servlet.IlvServletRequestParameters;
import ilog.views.gantt.swing.IlvJTable;
import ilog.views.util.text.IlvUnicodeUtil;

/**
 * <code>BasicServletSupport</code> is a basic implementation of
 * <code>IlvGanttServleSupport</code> that uses a common chart and data model to
 * handle all the HTTP requests for the servlet.
 */
public class BasicServletSupport extends IlvGanttServletSupport {
  /**
   * The chart used to satisfy all HTTP requests.
   */
  private IlvHierarchyChart chart;

  /**
   * The date format used to send the start and end times of the selected
   * activity to the client as additional capabilities.
   */
  private DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);

  /**
   * Creates the Gantt chart that will be used by the servlet to satisfy HTTP
   * requests.
   */
  protected IlvHierarchyChart createChart(IlvGanttModel ganttModel) {
    IlvHierarchyChart chart = new IlvGanttChart();
    chart.setGanttModel(ganttModel);
//    IlvGanttSheet ganttSheet = chart.getGanttSheet();

    // Default column widths are 75. Resize some of the columns so that the
    // text is not truncated.
    IlvJTable table = chart.getTable();
    table.getColumn("Name").setPreferredWidth(200);
    table.getColumn("Start").setPreferredWidth(90);
    table.getColumn("End").setPreferredWidth(90);

    // Expand all first-level activities so their children are visible.
    IlvActivity rootActivity = ganttModel.getRootActivity();
    if (rootActivity != null) {
      for (Iterator<IlvActivity> i = ganttModel.childActivityIterator(rootActivity); i.hasNext();)
        chart.expandRow(i.next());
    }

    return chart;
  }

  /**
   * Returns the chart used for the specified request. This implementation
   * always returns the same chart.
   * 
   * @param request
   *          The current HTTP request.
   * @param params
   *          The parameters parsed from the request.
   */
  Override
  public IlvHierarchyChart getChart(HttpServletRequest request, IlvServletRequestParameters params)
      throws ServletException {
    synchronized (this) {
      if (chart == null) {
        if (isVerbose(params)) {
          System.out.println("Creating Gantt chart");
        }
        chart = createChart(new SimpleProjectDataModel());
        // Fix JV-5416
        setSelectionEnabled(true);
      }
    }
    return chart;
  }

  /**
   * Allows you to add additional capabilities to a capability request This
   * method is overridden to send information on the currently selected activity
   * to the client.
   * 
   * @param chart
   *          The chart returned by {@link #getChart getChart}.
   * @param params
   *          The parameters parsed from the current HTTP request.
   */
  Override
  SuppressWarnings("rawtypes")
  protected Map getAdditionalCapabilities(IlvHierarchyChart chart, IlvServletRequestParameters params)
      throws ServletException {
    Map<String, String> map = new HashMap<String, String>();

    IlvActivity[] selectedActivities = ((IlvGanttChart) chart).getSelectedActivities();
    IlvActivity selectedActivity = null;
    if (selectedActivities.length > 0) {
      selectedActivity = selectedActivities[0];
    }
    if (selectedActivity != null) {
      map.put("selectedActivityName", IlvUnicodeUtil.escape(selectedActivity.getName()));
      map.put("selectedActivityID", IlvUnicodeUtil.escape(selectedActivity.getID()));
      map.put("selectedActivityStart", dateFormat.format(selectedActivity.getStartTime()));
      map.put("selectedActivityEnd", dateFormat.format(selectedActivity.getEndTime()));
    } else {
      map.put("selectedActivityName", null);
    }
    return map;
  }
}