/*
 * 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.model.integration.beans;

import java.util.ArrayList;
import java.util.List;

import org.apache.myfaces.trinidad.model.RowKeySet;

/**
 * A hierarchical extension of the <code>TGOObject</code> that exposes a property 
 * <code>children</code> that contains the <code>List</code> of <code>TGOObjectHierarchical</code>
 * that are considered children of this instance.
 * <p>
 * This implementation is in line with the more simplistic tree data model integration 
 * path provided MyFaces via its <code>org.apache.myfaces.trinidad.model.ChildPropertyTreeModel</code>.
 */
public class TGOObjectHierarchical extends TGOObject {

  /**
   * The children of this instance in the data model.
   */
  private List<TGOObjectHierarchical> children;
  
  /**
   * A reference to the parent object
   */
  private TGOObjectHierarchical parent;
  
  /**
   * The ChildPropertyTreeModel index
   */
  private Integer modelIndex;

  /**
   * Property name use to identify the children on this instance in the data model.
   */
  public static final String BRANCHING_PROPERTY_NAME = "children";
  
  /**
   * Constructor
   * 
   * @param index   The index of this object in the ChildPropertyTreeModel.
   */
  public TGOObjectHierarchical(int index) {
    modelIndex = index;
  }
  
  //////////////////////////////////////////////////////////////////////////////
  //Accessors and Modifiers
  //////////////////////////////////////////////////////////////////////////////

  /**
   * Returns the children of this instance in the data model. 
   */
  public List<TGOObjectHierarchical> getChildren() {
    return children;
  }

  /**
   * Sets the children of this instance in the data model.
   */
  public void setChildren(List<TGOObjectHierarchical> children) {
    this.children = children;
    if (null != children) {
      for (TGOObjectHierarchical child : children) {
        child.setParent(this);
      }
    }
  }
  
  /**
   * Gets the parent object
   */
  public TGOObjectHierarchical getParent() {
    return parent;
  }
  
  /**
   * Sets the parent of this instance in the data model.
   */
  public void setParent(TGOObjectHierarchical parent) {
    this.parent = parent;
  }
  
  /**
   * Accesses the model index for this object.
   */
  protected Integer getModelIndex() {
    return modelIndex;
  }
  
  /**
   * Gets the model path for this object.
   */
  public List<Integer> getModelPath() {
    List<Integer> ret = null;
    if (null == parent) {
      ret = new ArrayList<Integer>();
    } else {
      ret = parent.getModelPath();
    }
    ret.add(modelIndex);
    return ret;
  }
  
  /**
   * Sets the disclosed rows for this object.
   * 
   * @param RowKeySet  The RowKeySet implementation to store the results
   */
  public void updateModelDisclosedRows(RowKeySet disclosedRows) {
    if (null != parent) {
      parent.updateModelDisclosedRows(disclosedRows);
      disclosedRows.add(parent.getModelPath());
    }
  }
}