/*
 * 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 demo.gantt.faces;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.faces.model.SelectItem;

import ilog.views.faces.IlvFacesPageIdUtil;
import ilog.views.faces.IlvFacesUtil;
import ilog.views.gantt.IlvActivity;
import ilog.views.gantt.faces.component.IlvFacesHierarchyChartView;
import ilog.views.gantt.faces.component.data.IlvFacesGanttXMLDataSource;
import ilog.views.gantt.faces.dhtml.component.IlvFacesDHTMLGanttChartView;

/**
 * This bean stores all contextual data for every page from a browser accessing
 * this web application. The data remains in memory until the session expires.
 */
public class ContextDataBean {
  // Structure used to store the context data in memory
  private Map<String, ContextData> _contextMap;

  /**
   * Constructor: Instantiates the context map.
   */
  public ContextDataBean() {
    _contextMap = new ConcurrentHashMap<String, ContextData>();
  }

  /**
   * Access the {@link ContextData} for the current web page.
   */
  static ContextData getContextData() {
    // Access the ContextDataBean from the session
    ContextDataBean bean = (ContextDataBean) IlvFacesUtil.getSessionAttribute("dataBean");
    if (null == bean) {
      // Create a new bean for this session
      bean = new ContextDataBean();
      IlvFacesUtil.setSessionAttribute("dataBean", bean);
    }

    // Retrieve the identifier for the web page that originated
    // the request that is being handled
    String pageId = IlvFacesPageIdUtil.getPageId();

    ContextData data = bean._contextMap.get(pageId);
    if (null == data) {
      // Create a new context data
      data = new ContextData();
      bean._contextMap.put(pageId, data);
    }
    return data;
  }

  /**
   * Inner class to aggregate all context data into one structure
   */
  static class ContextData {
    /**
     * The XML Data Source binding component.
     */
    IlvFacesGanttXMLDataSource xmlDS = null;

    /**
     * The gantt view component binding.
     */
    IlvFacesHierarchyChartView ganttView = new IlvFacesDHTMLGanttChartView();

    /**
     * The list of activity used as the selectItems displayed by the
     * selectOneMenu.
     */
    ArrayList<SelectItem> activityItems = null;

    /**
     * The map that sort the activities by their id.
     */
    HashMap<String, IlvActivity> activityById = null;

    /**
     * The start time of the current activity.
     */
    Date startActivityDate = null;

    /**
     * The end time of the current activity.
     */
    Date endActivityDate = null;

    /**
     * The activity that is currently selected.
     */
    IlvActivity currentActivity = null;

    /**
     * The activity ID = the value of the combo.
     */
    String activityId = null;
  }
}