/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.examples.simulated.loader;

import java.util.Collections;

import com.google.common.collect.ImmutableList;
import com.opengamma.component.tool.AbstractTool;
import com.opengamma.core.config.impl.ConfigItem;
import com.opengamma.engine.function.config.FunctionConfigurationDefinition;
import com.opengamma.engine.function.config.FunctionConfigurationSource;
import com.opengamma.engine.function.config.ParameterizedFunctionConfiguration;
import com.opengamma.engine.function.config.StaticFunctionConfiguration;
import com.opengamma.examples.simulated.function.ExampleStandardFunctionConfiguration;
import com.opengamma.examples.simulated.function.SyntheticVolatilityCubeFunctions;
import com.opengamma.financial.aggregation.AggregationFunctions;
import com.opengamma.financial.analytics.AnalyticsFunctions;
import com.opengamma.financial.analytics.ircurve.IRCurveFunctions;
import com.opengamma.financial.currency.CurrencyFunctions;
import com.opengamma.financial.property.PropertyFunctions;
import com.opengamma.financial.tool.ToolContext;
import com.opengamma.financial.value.ValueFunctions;
import com.opengamma.financial.view.ViewFunctions;
import com.opengamma.master.config.ConfigMasterUtils;
import com.opengamma.scripts.Scriptable;

/**
 * Example code to create the function configurations
 * <p>
 */
@Scriptable
public class ExampleFunctionConfigurationPopulator extends AbstractTool<ToolContext> {

  private static final String CURVE = "CURVE_FUNCTIONS";
  private static final String STANDARD = "STANDARD_FUNCTIONS";
  private static final String VIEW = "VIEW_FUNCTIONS";
  private static final String VALUE = "VALUE_FUNCTIONS";
  private static final String PROPERTY = "PROPERTY_FUNCTIONS";
  private static final String CURRENCY = "CURRENCY_FUNCTIONS";
  private static final String ANALYTICS = "ANALYTICS_FUNCTIONS";
  private static final String AGGREGATION = "AGGREGATION_FUNCTIONS";
  private static final String FINANCIAL = "FINANCIAL_FUNCTIONS";
  private static final String EXAMPLE = "EXAMPLE_FUNCTIONS";
  private static final String CUBE = "CUBE_FUNCTIONS";

  //-------------------------------------------------------------------------
  /**
   * Main method to run the tool. No arguments are needed.
   * 
   * @param args the arguments, unused
   */
  public static void main(final String[] args) { // CSIGNORE
    new ExampleFunctionConfigurationPopulator().initAndRun(args, ToolContext.class);
    System.exit(0);
  }

  //-------------------------------------------------------------------------
  @Override
  protected void doRun() {
    storeFunctionDefinition(AGGREGATION, AggregationFunctions.instance());
    storeFunctionDefinition(ANALYTICS, AnalyticsFunctions.instance());
    storeFunctionDefinition(CURRENCY, CurrencyFunctions.instance());
    storeFunctionDefinition(PROPERTY, PropertyFunctions.instance());
    storeFunctionDefinition(VALUE, ValueFunctions.instance());
    storeFunctionDefinition(VIEW, ViewFunctions.instance());

    FunctionConfigurationDefinition financialFunc = new FunctionConfigurationDefinition(FINANCIAL,
        ImmutableList.of(AGGREGATION, ANALYTICS, CURRENCY, PROPERTY, VALUE, VIEW),
        Collections.<StaticFunctionConfiguration>emptyList(),
        Collections.<ParameterizedFunctionConfiguration>emptyList());
    storeFunctionDefinition(financialFunc);

    storeFunctionDefinition(STANDARD, ExampleStandardFunctionConfiguration.instance());
    storeFunctionDefinition(CURVE, IRCurveFunctions.providers(getToolContext().getConfigMaster()));
    storeFunctionDefinition(CUBE, SyntheticVolatilityCubeFunctions.instance());

    FunctionConfigurationDefinition exampleFunc = new FunctionConfigurationDefinition(EXAMPLE,
        ImmutableList.of(FINANCIAL, STANDARD, CURVE, CUBE),
        Collections.<StaticFunctionConfiguration>emptyList(),
        Collections.<ParameterizedFunctionConfiguration>emptyList());
    storeFunctionDefinition(exampleFunc);

  }

  private void storeFunctionDefinition(final FunctionConfigurationDefinition definition) {
    final ConfigItem<FunctionConfigurationDefinition> config = ConfigItem.of(definition, definition.getName(), FunctionConfigurationDefinition.class);
    ConfigMasterUtils.storeByName(getToolContext().getConfigMaster(), config);
  }

  private void storeFunctionDefinition(final String name, final FunctionConfigurationSource funcConfigSource) {
    FunctionConfigurationDefinition definition = FunctionConfigurationDefinition.of(name, funcConfigSource);
    final ConfigItem<FunctionConfigurationDefinition> config = ConfigItem.of(definition, name, FunctionConfigurationDefinition.class);
    ConfigMasterUtils.storeByName(getToolContext().getConfigMaster(), config);
  }

}

@@ -963,75 +963,8 @@
 ns;%0A
-import com.opengamma.financial.analytics.ircurve.IRCurveFunctions;%0A
 impo
@@ -1010,32 +1010,32 @@
 rencyFunctions;%0A
+
 import com.openg
@@ -1069,32 +1069,87 @@
 pertyFunctions;%0A
+import com.opengamma.financial.target.TargetFunctions;%0A
 import com.openg
@@ -1554,65 +1554,8 @@
  %7B%0A%0A
-  private static final String CURVE = %22CURVE_FUNCTIONS%22;%0A
   pr
@@ -2165,16 +2165,75 @@
 CTIONS%22;
+%0A  private static final String TARGET = %22TARGET_FUNCTIONS%22;
 %0A%0A  //--
@@ -3107,32 +3107,97 @@
 ons.instance());
+%0A    storeFunctionDefinition(TARGET, TargetFunctions.instance());
 %0A%0A    FunctionCo
@@ -3349,16 +3349,24 @@
 ROPERTY,
+ TARGET,
  VALUE, 
@@ -3641,108 +3641,8 @@
 ));%0A
-    storeFunctionDefinition(CURVE, IRCurveFunctions.providers(getToolContext().getConfigMaster()));%0A
     
@@ -3813,16 +3813,16 @@
 XAMPLE,%0A
+
         
@@ -3862,15 +3862,8 @@
 ARD,
- CURVE,
  CUB

Remove curve functions and add Target functions to standard financial functions