/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © 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 monitoring.web.drilldown;

import ilog.cpl.model.IlpObject;

import java.util.Collections;

import monitoring.web.AbstractSampleContext;
import monitoring.web.model.integration.IntegratedTableDataModelProvider;
import monitoring.web.model.integration.filters.OriginFilter;

/**
 * Extension of the implementation of the "drill-down" mechanism for an <code>IlpEquipment</code> 
 * to address the web based control needs. Namely, it addresses the additional care needed to 
 * update the filter on the tables and their models as the drill-down mechanism takes place.
 */
public class EquipmentDrillDownManager extends monitoring.shared.drilldown.EquipmentDrillDownManager {

  protected AbstractSampleContext sampleContext;
  protected IntegratedTableDataModelProvider inventoryTableModelProvider;

  /**
   * Web equipment implementation.
   * 
   * @see monitoring.shared.drilldown.EquipmentDrillDownManager#clearEquipmentDetails()
   */
  public void clearEquipmentDetails() {
    //1 - Clear the view
    setOrigins(Collections.emptyList(), true);
    setCurrentRoot(null);

    //2- Clear the tables
    updateInventoryTableFilter(OriginFilter.SHOW_NOTHING_MODE);    
  }  

  /**
   * Web equipment implementation.
   * 
   * @see monitoring.shared.drilldown.EquipmentDrillDownManager#setEquipmentRootObject(ilog.cpl.model.IlpObject, ilog.cpl.model.IlpObject)
   */
  protected void setEquipmentRootObject(IlpObject parent, IlpObject detailObj) {

    //1- Show the object in the view 
    super.setEquipmentRootObject(parent, detailObj);

    //2- Show the objects in the tables
    updateInventoryTableFilter(OriginFilter.SHOW_ALL_CHILDREN_MODE);
  }

  /**
   * Updates the table filter mode with the provided mode. 
   */
  protected void updateInventoryTableFilter(int filterMode) {

    //1- Get the model provider
    IntegratedTableDataModelProvider modelProvider = getInventoryTableModelProvider();

    //2- Get the filter itself
    OriginFilter inventoryTableFilter = 
      (OriginFilter)modelProvider.getFilter();

    int previousFilterMode = inventoryTableFilter.getMode();

    //3- Update the filter mode 
    inventoryTableFilter.setMode(filterMode);

    //4- Refresh the model to enforce changes in mode (if needed)
    if(previousFilterMode != filterMode) {
      modelProvider.refreshModel();   
    }
  }

  /**
   * Returns the <code>IntegratedTableDataModelProvider</code> for the inventory 
   * module's managed object tables.
   */
  protected IntegratedTableDataModelProvider getInventoryTableModelProvider() {
    if(inventoryTableModelProvider == null) {
      inventoryTableModelProvider = sampleContext.getDataStructures().getDataModels().
      getInventoryObjectsTableModelProvider();      
    }
    return inventoryTableModelProvider;
  }

  //////////////////////////////////////////////////////////////////////////////
  //Accessors and Modifiers
  //////////////////////////////////////////////////////////////////////////////
  public AbstractSampleContext getSampleContext() {
    return sampleContext;
  }
  public void setSampleContext(AbstractSampleContext sampleContext) {
    this.sampleContext = sampleContext;
  }
}