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

import java.util.List;

import com.opengamma.engine.function.config.AbstractFunctionConfigurationBean;
import com.opengamma.engine.function.config.FunctionConfiguration;
import com.opengamma.engine.function.config.FunctionConfigurationSource;
import com.opengamma.financial.analytics.curve.CurveDefinitionFunction;
import com.opengamma.financial.analytics.curve.CurveMarketDataFunction;
import com.opengamma.financial.analytics.curve.CurveSpecificationFunction;
import com.opengamma.master.config.ConfigDocument;
import com.opengamma.master.config.ConfigMaster;
import com.opengamma.master.config.ConfigSearchRequest;
import com.opengamma.master.config.impl.ConfigSearchIterator;

/**
 * Function repository configuration source for the functions contained in this package.
 */
public class IRCurveFunctions extends AbstractFunctionConfigurationBean {

  /**
   * Default instance of a repository configuration source exposing the functions from this package.
   *
   * @return the configuration source exposing functions from this package
   */
  public static FunctionConfigurationSource instance() {
    return new IRCurveFunctions().getObjectCreating();
  }

  public static FunctionConfigurationSource providers(final ConfigMaster configMaster) {
    final Providers factory = new Providers();
    factory.setConfigMaster(configMaster);
    return factory.getObjectCreating();
  }

  /**
   * Function repository configuration source for yield curve functions based on the items defined in a Config Master.
   */
  public static class Providers extends AbstractFunctionConfigurationBean {

    private ConfigMaster _configMaster;

    public void setConfigMaster(final ConfigMaster configMaster) {
      _configMaster = configMaster;
    }

    public ConfigMaster getConfigMaster() {
      return _configMaster;
    }

    protected void addYieldCurveFunctions(final List<FunctionConfiguration> functions, final String currency, final String curveName) {
      functions.add(functionConfiguration(YieldCurveMarketDataFunction.class, currency, curveName));
      functions.add(functionConfiguration(YieldCurveInterpolatingFunction.class, currency, curveName));
      functions.add(functionConfiguration(YieldCurveSpecificationFunction.class, currency, curveName));
    }

    @Override
    protected void addAllConfigurations(final List<FunctionConfiguration> functions) {
      final ConfigSearchRequest<YieldCurveDefinition> searchRequest = new ConfigSearchRequest<YieldCurveDefinition>();
      searchRequest.setType(YieldCurveDefinition.class);
      for (final ConfigDocument configDocument : ConfigSearchIterator.iterable(getConfigMaster(), searchRequest)) {
        final String documentName = configDocument.getName();
        final int underscore = documentName.lastIndexOf('_');
        if (underscore <= 0) {
          continue;
        }
        final String curveName = documentName.substring(0, underscore);
        final String currencyISO = documentName.substring(underscore + 1);
        addYieldCurveFunctions(functions, currencyISO, curveName);
      }
    }

  }

  @Override
  protected void addAllConfigurations(final List<FunctionConfiguration> functions) {
    functions.add(functionConfiguration(DefaultYieldCurveMarketDataShiftFunction.class));
    functions.add(functionConfiguration(DefaultYieldCurveShiftFunction.class));
    functions.add(functionConfiguration(YieldCurveMarketDataShiftFunction.class));
    functions.add(functionConfiguration(YieldCurveShiftFunction.class));

    functions.add(functionConfiguration(CurveDefinitionFunction.class, "Test_USD"));
    functions.add(functionConfiguration(CurveSpecificationFunction.class, "Test_USD"));
    functions.add(functionConfiguration(CurveMarketDataFunction.class, "Test_USD"));
  }

}

@@ -426,16 +426,80 @@
 Source;%0A
+import com.opengamma.financial.analytics.curve.CurveDefinition;%0A
 import c
@@ -701,32 +701,108 @@
 cationFunction;%0A
+import com.opengamma.financial.analytics.curve.InterpolatedCurveDefinition;%0A
 import com.openg
@@ -2596,24 +2596,400 @@
 e));%0A    %7D%0A%0A
+    protected void addCurveFunctions(final List%3CFunctionConfiguration%3E functions, final String curveName) %7B%0A      functions.add(functionConfiguration(CurveDefinitionFunction.class, curveName));%0A      functions.add(functionConfiguration(CurveSpecificationFunction.class, curveName));%0A      functions.add(functionConfiguration(CurveMarketDataFunction.class, curveName));%0A    %7D%0A%0A
     @Overrid
@@ -3171,36 +3171,16 @@
 Request%3C
-YieldCurveDefinition
 %3E();%0A   
@@ -3751,24 +3751,485 @@
 me);%0A      %7D
+%0A%0A      // new curves%0A      Class%5B%5D curveClasses = new Class%5B%5D %7BCurveDefinition.class, InterpolatedCurveDefinition.class%7D;%0A      for (final Class klass : curveClasses) %7B%0A        searchRequest.setType(klass);%0A        for (final ConfigDocument configDocument : ConfigSearchIterator.iterable(getConfigMaster(), searchRequest)) %7B%0A          final String documentName = configDocument.getName();%0A          addCurveFunctions(functions, documentName);%0A        %7D%0A      %7D
 %0A    %7D%0A%0A  %7D%0A
@@ -4655,267 +4655,8 @@
 s));
-%0A%0A    functions.add(functionConfiguration(CurveDefinitionFunction.class, %22Test_USD%22));%0A    functions.add(functionConfiguration(CurveSpecificationFunction.class, %22Test_USD%22));%0A    functions.add(functionConfiguration(CurveMarketDataFunction.class, %22Test_USD%22));
 %0A  %7D

Add functions for new curves found in database