/*
 * 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.controls.configurator;

import java.util.logging.Level;

import ilog.cpl.model.IlpObject;
import ilog.cpl.service.IlpContext;
import ilog.tgo.model.IltNetworkElement;
import monitoring.shared.CommonUtils;
import monitoring.shared.LoggingUtils;
import monitoring.web.SampleConstants;
import monitoring.web.controls.EquipmentControls;

/**
 * The component configurator that ensures that the appropriate 
 * style files are loaded on the inventory module's equipment after drill downs
 * take place.
 */
public class EquipmentConfigurator extends AbstractComponentConfigurator {

  //Equipment controls
  private EquipmentControls equipmentControls;

  /**
   * Creates an equipment configurator and saves the <code>EquipmentControls</code>
   * reference.
   */
  public EquipmentConfigurator(EquipmentControls equipmentControls) {
    super();
    this.equipmentControls = equipmentControls;
  }

  /**
   * Equipment implementation.
   * 
   * @see monitoring.web.controls.configurator.AbstractComponentConfigurator#getCurrentStyleSheets()
   */
  protected String getCurrentStyleSheets() {
    return equipmentControls.getInventoryEquipmentView().getStyleSheets();
  }

  /**
   * Equipment implementation.
   * 
   * @see monitoring.web.controls.configurator.AbstractComponentConfigurator#getDetailsConfiguration(ilog.cpl.model.IlpObject)
   */
  protected String getDetailsConfiguration(IlpObject object) {
    if (object instanceof IltNetworkElement) {
      return 
      CommonUtils.computeConfigurationFileNameForEquipment((IltNetworkElement) object, getContext());
    }
    return null;
  }

  /**
   * Equipment implementation.
   * 
   * @see monitoring.web.controls.configurator.AbstractComponentConfigurator#getRootConfiguration()
   */
  protected String getRootConfiguration() {
    return SampleConstants.INVENTORY_EQUIPMENT_ROOT_CONFIG_FILE;
  }

  /**
   * Equipment implementation.
   * 
   * @see monitoring.web.controls.configurator.AbstractComponentConfigurator#updateStyleSheets(java.lang.String)
   */
  protected void updateStyleSheets(String styleSheets) {
    equipmentControls.getInventoryEquipmentView().setStyleSheets(styleSheets);
    try {
      equipmentControls.getInventoryEquipmentView().getEquipment().setStyleSheets(getStringArrayForStyleString(styleSheets));      
    } catch (Exception e) {
      LoggingUtils.getSampleLogger().log(
          Level.SEVERE, 
          "Could not update style sheets on equipment with this exception:", e.getLocalizedMessage());
    }
  }

  private IlpContext getContext() {
    return equipmentControls.getInventoryEquipmentView().getContext();
  }

}