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

import java.util.Collections;
import java.util.Set;

import com.opengamma.core.security.Security;
import com.opengamma.engine.ComputationTarget;
import com.opengamma.engine.ComputationTargetType;
import com.opengamma.engine.function.AbstractFunction;
import com.opengamma.engine.function.FunctionCompilationContext;
import com.opengamma.engine.function.FunctionExecutionContext;
import com.opengamma.engine.function.FunctionInputs;
import com.opengamma.engine.value.ComputedValue;
import com.opengamma.engine.value.ValueRequirement;
import com.opengamma.engine.value.ValueRequirementNames;
import com.opengamma.engine.value.ValueSpecification;
import com.opengamma.financial.security.equity.EquitySecurity;
import com.opengamma.financial.security.option.EquityIndexOptionSecurity;
import com.opengamma.financial.security.option.EquityOptionSecurity;
import com.opengamma.livedata.normalization.MarketDataRequirementNames;


/**
 * Provides the market price for the security of a position as a value on the position
 */
public class SecurityMarketPriceFunction extends AbstractFunction.NonCompiledInvoker {

  @Override
  public Set<ComputedValue> execute(FunctionExecutionContext executionContext, FunctionInputs inputs,
      ComputationTarget target, Set<ValueRequirement> desiredValues) {
    double marketValue = (Double) inputs.getValue(getRequirement(target));
    return Collections.singleton(new ComputedValue(getSpecification(target), marketValue));
  }

  @Override
  public ComputationTargetType getTargetType() {
    return ComputationTargetType.POSITION;
  }

  @Override
  public boolean canApplyTo(FunctionCompilationContext context, ComputationTarget target) {
    if (target.getType() != getTargetType()) {
      return false;
    }
    final Security security = target.getPosition().getSecurity();
    //TODO what else?
    return security instanceof EquitySecurity || security instanceof EquityOptionSecurity || security instanceof EquityIndexOptionSecurity;
  }

  @Override
  public Set<ValueRequirement> getRequirements(FunctionCompilationContext context, ComputationTarget target,
      ValueRequirement desiredValue) {
    ValueRequirement valueRequirement = getRequirement(target);
    return Collections.singleton(valueRequirement);
  }

  @Override
  public Set<ValueSpecification> getResults(FunctionCompilationContext context, ComputationTarget target) {
    ValueSpecification spec = getSpecification(target);
    return Collections.singleton(spec);
  }

  private ValueSpecification getSpecification(ComputationTarget target) {
    return new ValueSpecification(new ValueRequirement(ValueRequirementNames.SECURITY_MARKET_PRICE, target.getPosition()), getUniqueId());
  }
  
  private ValueRequirement getRequirement(ComputationTarget target) {
    return new ValueRequirement(MarketDataRequirementNames.MARKET_VALUE, ComputationTargetType.SECURITY, target.getPosition().getSecurity().getUniqueId());
  }

}

@@ -664,16 +664,121 @@
 dValue;%0A
+import com.opengamma.engine.value.ValueProperties;%0Aimport com.opengamma.engine.value.ValuePropertyNames;%0A
 import c
@@ -821,16 +821,16 @@
 rement;%0A
-
 import c
@@ -928,24 +928,88 @@
 cification;%0A
+import com.opengamma.financial.security.FinancialSecurityUtils;%0A
 import com.o
@@ -1194,32 +1194,32 @@
 OptionSecurity;%0A
-
 import com.openg
@@ -1274,16 +1274,58 @@
 tNames;%0A
+import com.opengamma.util.money.Currency;%0A
 %0A%0A/**%0A *
@@ -2945,32 +2945,352 @@
 arget target) %7B%0A
+    final Currency ccy = FinancialSecurityUtils.getCurrency(target.getPosition().getSecurity());%0A    ValueProperties valueProperties;%0A    if (ccy == null) %7B%0A      valueProperties = ValueProperties.none();%0A    %7D else %7B%0A      valueProperties = ValueProperties.with(ValuePropertyNames.CURRENCY, ccy.getCode()).get();%0A    %7D%0A
     return new V
@@ -3372,16 +3372,24 @@
 T_PRICE,
+%0A       
  target.
@@ -3401,16 +3401,33 @@
 sition()
+, valueProperties
 ), getUn

Add currency for Security Market Price