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

import org.json.JSONException;
import org.json.JSONObject;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.financial.analytics.ircurve.FixedIncomeStrip;
import com.opengamma.financial.analytics.ircurve.StripInstrumentType;
import com.opengamma.financial.analytics.ircurve.YieldCurveDefinition;
import com.opengamma.id.ExternalId;
import com.opengamma.id.ExternalIdFudgeBuilder;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.money.Currency;
import com.opengamma.util.time.Tenor;

/**
 * Custom JSON builder to convert YieldCurveDefinition to JSON object and back again
 */
public final class YieldCurveDefinitionJSONBuilder extends AbstractJSONBuilder<YieldCurveDefinition> {
  
  private static final String REGION_FIELD = "region";
  private static final String CURRENCY_FIELD = "currency";
  /**
   * Singleton
   */
  public static final YieldCurveDefinitionJSONBuilder INSTANCE = new YieldCurveDefinitionJSONBuilder();
  /**
   * JSON template
   */
  private static final String TEMPLATE = createTemplate();

  /**
   * Restricted constructor 
   */
  private YieldCurveDefinitionJSONBuilder() {
  }
  
  @Override
  public YieldCurveDefinition fromJSON(final String json) {
    ArgumentChecker.notNull(json, "JSON document");
    return fromJSON(YieldCurveDefinition.class, json);
  }

  @Override
  public String toJSON(final YieldCurveDefinition object) {
    ArgumentChecker.notNull(object, "yield curve definition");
    return toJSON(object, YieldCurveDefinition.class);
  }
  
  private static String createTemplate() {
    YieldCurveDefinitionJSONBuilder builder = YieldCurveDefinitionJSONBuilder.INSTANCE; 
    String result = null;
    try {
      JSONObject jsonObject = new JSONObject(builder.toJSON(getDummyYieldCurveDefinition()));
      jsonObject.put(CURRENCY_FIELD, "");
      jsonObject.put(REGION_FIELD, getBlankIdentifier());
      result = jsonObject.toString();
    } catch (JSONException ex) {
      throw new OpenGammaRuntimeException("invalid json produced from dummy yield curve definition", ex);
    }
    return result;
  }

  private static JSONObject getBlankIdentifier() {
    JSONObject blankIdentifier = null;
    try {
      blankIdentifier = new JSONObject();
      blankIdentifier.put(ExternalIdFudgeBuilder.SCHEME_FIELD_NAME, "");
      blankIdentifier.put(ExternalIdFudgeBuilder.VALUE_FIELD_NAME, "");
    } catch (JSONException ex) {
      throw new OpenGammaRuntimeException("invalid json produced from blank region identifier", ex);
    }
    return blankIdentifier;
  }

  private static YieldCurveDefinition getDummyYieldCurveDefinition() {
    YieldCurveDefinition dummy = new YieldCurveDefinition(Currency.GBP, ExternalId.of("dummy", "dummy"), "", "");
    dummy.addStrip(new FixedIncomeStrip(StripInstrumentType.LIBOR, Tenor.DAY, ""));
    return dummy;
  }

  @Override
  public String getTemplate() {
    return TEMPLATE;
  }

}

@@ -176,107 +176,45 @@
 ort 
-org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport com.opengamma.OpenGammaRuntimeException
+com.opengamma.core.region.RegionUtils
 ;%0Aim
@@ -423,92 +423,8 @@
 on;%0A
-import com.opengamma.id.ExternalId;%0Aimport com.opengamma.id.ExternalIdFudgeBuilder;%0A
 impo
@@ -738,24 +738,26 @@
 ition%3E %7B%0A  %0A
+//
   private st
@@ -799,16 +799,18 @@
 egion%22;%0A
+//
   privat
@@ -1596,32 +1596,39 @@
 emplate() %7B%0A    
+return 
 YieldCurveDefini
@@ -1646,62 +1646,60 @@
 lder
- builder = YieldCurveDefinitionJSONBuilder.INSTANCE; %0A
+.INSTANCE.toJSON(getDummyYieldCurveDefinition());%0A//
     
@@ -1712,32 +1712,34 @@
  result = null;%0A
+//
     try %7B%0A      
@@ -1728,24 +1728,26 @@
 //    try %7B%0A
+//
       JSONOb
@@ -1824,24 +1824,26 @@
 nition()));%0A
+//
       jsonOb
@@ -1868,24 +1868,26 @@
 FIELD, %22%22);%0A
+//
       jsonOb
@@ -1928,24 +1928,26 @@
 ntifier());%0A
+//
       result
@@ -1968,24 +1968,26 @@
 toString();%0A
+//
     %7D catch 
@@ -1999,32 +1999,34 @@
 Exception ex) %7B%0A
+//
       throw new 
@@ -2107,38 +2107,42 @@
 finition%22, ex);%0A
+//
     %7D%0A
+//
     return resul
@@ -2145,24 +2145,26 @@
 esult;%0A  %7D%0A%0A
+//
   private st
@@ -2198,24 +2198,26 @@
 ntifier() %7B%0A
+//
     JSONObje
@@ -2243,16 +2243,18 @@
 = null;%0A
+//
     try 
@@ -2255,16 +2255,18 @@
   try %7B%0A
+//
       bl
@@ -2299,16 +2299,18 @@
 ject();%0A
+//
       bl
@@ -2370,24 +2370,26 @@
 _NAME, %22%22);%0A
+//
       blankI
@@ -2444,24 +2444,26 @@
 _NAME, %22%22);%0A
+//
     %7D catch 
@@ -2479,24 +2479,26 @@
 ption ex) %7B%0A
+//
       throw 
@@ -2586,22 +2586,26 @@
 %22, ex);%0A
+//
     %7D%0A
+//
     retu
@@ -2624,16 +2624,18 @@
 tifier;%0A
+//
   %7D%0A%0A  p
@@ -2778,38 +2778,49 @@
 BP, 
-ExternalId.of(%22dummy%22, %22dummy%22
+RegionUtils.currencyRegionId(Currency.USD
 ), %22

fix YieldCurveDefinition template