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

import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.opengamma.engine.ComputationTargetSpecification;
import com.opengamma.engine.target.ComputationTargetReference;
import com.opengamma.engine.target.ComputationTargetReferenceVisitor;
import com.opengamma.engine.target.ComputationTargetRequirement;
import com.opengamma.engine.target.ComputationTargetType;
import com.opengamma.engine.value.ValueProperties;
import com.opengamma.engine.value.ValueRequirement;
import com.opengamma.engine.view.ViewCalculationConfiguration;
import com.opengamma.engine.view.ViewDefinition;
import com.opengamma.engine.view.compilation.CompiledViewDefinition;
import com.opengamma.id.ExternalId;

/**
 *
 */
public final class PrimitivesGridStructure extends MainGridStructure {

  /** Target type for anything that isn't part of a portoflio structure. */
  private static final ComputationTargetType NON_PRIMITIVE =
      ComputationTargetType.PORTFOLIO_NODE
          .or(ComputationTargetType.POSITION)
          .or(ComputationTargetType.TRADE)
          .or(ComputationTargetType.SECURITY);

  /** Creates names for the label column in the grid. */
  private static final ComputationTargetReferenceVisitor<String> s_computationTargetDisplayNameVisitor =
      new ComputationTargetReferenceVisitor<String>() {

    @Override
    public String visitComputationTargetRequirement(ComputationTargetRequirement requirement) {
      // TODO can't this just be done with StringUtils.join(requirement.getIdentifiers(), ", ")
      StringBuilder sb = new StringBuilder();
      boolean first = true;
      for (ExternalId externalId : requirement.getIdentifiers()) {
        if (!first) {
          sb.append(", ");
        }
        sb.append(externalId.toString());
        first = false;
      }
      return sb.toString();
    }

    @Override
    public String visitComputationTargetSpecification(ComputationTargetSpecification specification) {
      return specification.getUniqueId().toString();
    }
    
  };

  private PrimitivesGridStructure() {
  }

  private PrimitivesGridStructure(GridColumnGroup fixedColumns,
                                  GridColumnGroups nonFixedColumns,
                                  TargetLookup targetLookup) {
    super(fixedColumns, nonFixedColumns, targetLookup);
  }

  /* package */static PrimitivesGridStructure create(CompiledViewDefinition compiledViewDef) {
    List<MainGridStructure.Row> rows = rows(compiledViewDef);
    GridColumn labelColumn = new GridColumn("Name", "", String.class, new PrimitivesLabelRenderer(rows));
    GridColumnGroup fixedColumns = new GridColumnGroup("fixed", ImmutableList.of(labelColumn), false);
    TargetLookup targetLookup = new TargetLookup(new ValueMappings(compiledViewDef), rows);
    List<GridColumnGroup> analyticsColumns = buildAnalyticsColumns(compiledViewDef.getViewDefinition(), targetLookup);
    return new PrimitivesGridStructure(fixedColumns, new GridColumnGroups(analyticsColumns), targetLookup);
  }

  private static List<GridColumnGroup> buildAnalyticsColumns(ViewDefinition viewDef, TargetLookup targetLookup) {
    List<GridColumnGroup> columnGroups = Lists.newArrayList();
    Set<ColumnSpecification> columnSpecs = Sets.newHashSet();
    for (ViewCalculationConfiguration calcConfig : viewDef.getAllCalculationConfigurations()) {
      List<GridColumn> columns = Lists.newArrayList();
      for (ValueRequirement specificRequirement : calcConfig.getSpecificRequirements()) {
        if (!specificRequirement.getTargetReference().getType().isTargetType(NON_PRIMITIVE)) {
          String valueName = specificRequirement.getValueName();
          Class<?> columnType = ValueTypes.getTypeForValueName(valueName);
          ValueProperties constraints = specificRequirement.getConstraints();
          ColumnSpecification columnSpec = new ColumnSpecification(calcConfig.getName(), valueName, constraints);
          // ensure columnSpec isn't a duplicate
          if (columnSpecs.add(columnSpec)) {
            columns.add(GridColumn.forSpec(columnSpec, columnType, targetLookup));
          }
        }
      }
      if (!columns.isEmpty()) {
        columnGroups.add(new GridColumnGroup(calcConfig.getName(), columns, true));
      }
    }
    return columnGroups;
  }

  private static List<MainGridStructure.Row> rows(CompiledViewDefinition compiledViewDef) {
    Set<ComputationTargetReference> targetRefs = Sets.newLinkedHashSet();
    for (ViewCalculationConfiguration calcConfig : compiledViewDef.getViewDefinition().getAllCalculationConfigurations()) {
      for (ValueRequirement specificRequirement : calcConfig.getSpecificRequirements()) {
        targetRefs.add(specificRequirement.getTargetReference());
      }
    }
    List<MainGridStructure.Row> rows = Lists.newArrayList();
    for (ComputationTargetReference targetRef : targetRefs) {
      rows.add(new Row(targetRef, targetRef.accept(s_computationTargetDisplayNameVisitor)));
    }
    return rows;
  }

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

@@ -215,16 +215,61 @@
 l.Set;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A%0A
 import c
@@ -990,44 +990,8 @@
 ion;
-%0Aimport com.opengamma.id.ExternalId;
 %0A%0A/*
@@ -1516,33 +1516,9 @@
 %3E s_
-computationTargetDisplayN
+n
 ameV
@@ -1525,22 +1525,16 @@
 isitor =
-%0A     
  new Com
@@ -1697,44 +1697,14 @@
     
-// TODO can't this just be done with
+return
  Str
@@ -1749,314 +1749,25 @@
 rs()
-, %22, %22)%0A      StringBuilder sb = new StringBuilder();%0A      boolean first = true;%0A      for (ExternalId externalId : requirement.getIdentifiers()) %7B%0A        if (!first) %7B%0A          sb.append(%22, %22);%0A        %7D%0A        sb.append(externalId.toString());%0A        first = false;%0A      %7D%0A      return sb.toString(
+.iterator(), %22, %22
 );%0A 
@@ -1951,13 +1951,8 @@
   %7D%0A
-    %0A
   %7D;
@@ -4856,33 +4856,9 @@
 t(s_
-computationTargetDisplayN
+n
 ameV

Use StringUtils to build primitives target name