/**
 * 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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.engine.view.ViewDefinition;
import com.opengamma.livedata.UserPrincipal;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.money.Currency;

/**
 * Custom JSON builder to convert ViewDefinition to JSON object and back again
 */
public final class ViewDefinitionJSONBuilder extends AbstractJSONBuilder<ViewDefinition> {
 
  private static final String CALCULATION_CONFIGURATION_FIELD = "calculationConfiguration";
  private static final String CURRENCY_FIELD = "currency";

  /**
   * Singleton
   */
  public static final ViewDefinitionJSONBuilder INSTANCE = new ViewDefinitionJSONBuilder();
  
  /**
   * JSON template
   */
  private static final String TEMPLATE = createTemplate();
  
  /**
   * Restricted constructor
   */
  private ViewDefinitionJSONBuilder() {
  }

  @Override
  public ViewDefinition fromJSON(final String json) {
    ArgumentChecker.notNull(json, "JSON document");
    return fromJSON(ViewDefinition.class, json);
  }
 
  @Override
  public String toJSON(final ViewDefinition viewDefinition) {
    ArgumentChecker.notNull(viewDefinition, "viewDefinition");
    return toJSON(viewDefinition, ViewDefinition.class);
  }
  
  private static String createTemplate() {
    ViewDefinitionJSONBuilder builder = ViewDefinitionJSONBuilder.INSTANCE; 
    String result = null;
    try {
      JSONObject jsonObject = new JSONObject(builder.toJSON(getDummyView()));
      jsonObject.put(CURRENCY_FIELD, "");
      jsonObject.put(CALCULATION_CONFIGURATION_FIELD, new JSONArray());
      result = jsonObject.toString();
    } catch (JSONException ex) {
      throw new OpenGammaRuntimeException("invalid json produced from dummy view definition", ex);
    }
    return result;
  }

  private static ViewDefinition getDummyView() {
    ViewDefinition dummy = new ViewDefinition("", new UserPrincipal("", ""));
    dummy.setDefaultCurrency(Currency.GBP);
    dummy.setMaxDeltaCalculationPeriod(0L);
    dummy.setMaxFullCalculationPeriod(0L);
    dummy.setMinDeltaCalculationPeriod(0L);
    dummy.setMinFullCalculationPeriod(0L);
    dummy.addPortfolioRequirementName("", "", "");
    return dummy;
  }

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

@@ -169,143 +169,8 @@
 n;%0A%0A
-import org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport com.opengamma.OpenGammaRuntimeException;%0A
 impo
@@ -214,16 +214,16 @@
 nition;%0A
+
 import c
@@ -525,16 +525,18 @@
 on%3E %7B%0A %0A
+//
   privat
@@ -619,16 +619,18 @@
 ation%22;%0A
+//
   privat
@@ -1475,16 +1475,61 @@
 TANCE; %0A
+    return builder.toJSON(getDummyView());%0A//
     Stri
@@ -1546,16 +1546,18 @@
 = null;%0A
+//
     try 
@@ -1558,16 +1558,18 @@
   try %7B%0A
+//
       JS
@@ -1634,24 +1634,26 @@
 myView()));%0A
+//
       jsonOb
@@ -1678,24 +1678,26 @@
 FIELD, %22%22);%0A
+//
       jsonOb
@@ -1752,24 +1752,26 @@
 ONArray());%0A
+//
       result
@@ -1796,16 +1796,18 @@
 ring();%0A
+//
     %7D ca
@@ -1827,24 +1827,26 @@
 ption ex) %7B%0A
+//
       throw 
@@ -1936,14 +1936,18 @@
 x);%0A
+//
     %7D%0A
+//
     

fix view definition template to use customs json builders