/**
 * Copyright (C) 2012 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.web.server.push.analytics;

import java.util.List;

import com.google.common.collect.Lists;
import com.opengamma.core.position.PortfolioNode;
import com.opengamma.core.position.Position;
import com.opengamma.core.position.impl.PortfolioMapper;
import com.opengamma.core.position.impl.PortfolioMapperFunction;
import com.opengamma.engine.ComputationTargetSpecification;
import com.opengamma.engine.ComputationTargetType;
import com.opengamma.engine.value.ValueProperties;
import com.opengamma.engine.view.ViewCalculationConfiguration;
import com.opengamma.engine.view.compilation.CompiledViewDefinition;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.tuple.Pair;
import com.opengamma.web.server.RequirementBasedColumnKey;

/**
 *
 */
public class PortfolioGridStructure extends MainGridStructure {

  private final AnalyticsNode _root;

  /* package */ PortfolioGridStructure(CompiledViewDefinition compiledViewDef) {
    super(compiledViewDef, rows(compiledViewDef));
    ArgumentChecker.notNull(compiledViewDef, "compiledViewDef");
    _root = AnalyticsNode.portoflioRoot(compiledViewDef);
  }

  private PortfolioGridStructure() {
    _root = AnalyticsNode.emptyRoot();
  }

  /* package */ static PortfolioGridStructure empty() {
    return new PortfolioGridStructure();
  }

  protected List<RequirementBasedColumnKey> buildColumns(ViewCalculationConfiguration calcConfig) {
    List<RequirementBasedColumnKey> columnKeys = Lists.newArrayList();
    for (Pair<String, ValueProperties> portfolioOutput : calcConfig.getAllPortfolioRequirements()) {
      String valueName = portfolioOutput.getFirst();
      ValueProperties constraints = portfolioOutput.getSecond();
      RequirementBasedColumnKey columnKey = new RequirementBasedColumnKey(calcConfig.getName(), valueName, constraints);
      columnKeys.add(columnKey);
    }
    return columnKeys;
  }

  public AnalyticsNode getRoot() {
    return _root;
  }

  private static List<Row> rows(CompiledViewDefinition viewDef) {
    PortfolioMapperFunction<Row> targetFn = new PortfolioMapperFunction<Row>() {
      @Override
      public Row apply(PortfolioNode node) {
        ComputationTargetSpecification target =
            new ComputationTargetSpecification(ComputationTargetType.PORTFOLIO_NODE, node.getUniqueId());
        return new Row(target, node.getName());
      }

      @Override
      public Row apply(Position position) {
        ComputationTargetSpecification target =
            new ComputationTargetSpecification(ComputationTargetType.POSITION, position.getUniqueId());
        return new Row(target, position.getSecurity().getName(), position.getQuantity());
      }
    };
    return PortfolioMapper.map(viewDef.getPortfolio().getRootNode(), targetFn);
  }

}

@@ -2122,16 +2122,22 @@
 w%3E rows(
+final 
 Compiled
@@ -2158,24 +2158,24 @@
  viewDef) %7B%0A
-
     Portfoli
@@ -2239,24 +2239,25 @@
 on%3CRow%3E() %7B%0A
+%0A
       @Overr
@@ -2471,45 +2471,405 @@
     
-return new Row(target, node.getName()
+String nodeName;%0A        // if the parent ID is null it's the root node%0A        if (node.getParentNodeId() == null) %7B%0A          // the root node is called %22Root%22 which isn't any use for displaying in the UI, use the portfolio name instead%0A          nodeName = viewDef.getPortfolio().getName();%0A        %7D else %7B%0A          nodeName = node.getName();%0A        %7D%0A        return new Row(target, nodeName
 );%0A 
@@ -3273,15 +3273,14 @@
 getFn);%0A
+
   %7D%0A
-%0A
 %7D%0A

Use portfolio name for the root node label in the portfolio grid