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

import java.util.Optional;

import com.opengamma.sdk.common.JavaSdkException;

/**
 * Used to indicate that there was a problem with a margin request.
 */
public class MarginException extends JavaSdkException {

  /** The Margin operation which is associated with the current exception. This is optional, as some exceptions are not mapped to a single operation. */
  private Optional<MarginOperation> marginOperation = Optional.empty();

  /** Serialization version. */
  private static final long serialVersionUID = 8789233267870746967L;

  /**
   * Creates an instance from a message and reason.
   * 
   * @param reason  the reason sent by the remote API call
   * @param message  the message
   */
  public MarginException(String message, String reason) {
    super(message, reason);
  }

  /**
   * Creates an instance from a message, HTTP code, reason and description.
   * 
   * @param httpCode  the HTTP code of the remote API call
   * @param reason  the reason sent by the remote API call
   * @param description  the description sent by the remote API call
   * @param message  the message
   */
  public MarginException(String message, int httpCode, String reason, String description) {
    super(message, httpCode, reason, description);
  }

  /**
   * Creates an instance from a message, HTTP code, reason and description.
   *
   * @param httpCode  the HTTP code of the remote API call
   * @param reason  the reason sent by the remote API call
   * @param description  the description sent by the remote API call
   * @param message  the message
   * @param operation  the Margin Service API operation which was being performed while the Exception occurred
   */
  public MarginException(String message, int httpCode, String reason, String description, MarginOperation operation) {
    super(message, httpCode, reason, description);
    this.marginOperation = Optional.of(operation);
  }

  /**
   * Gets the margin operation responsible for the current exception.
   *
   * @return the Margin Operation, wrapped in an {@link Optional}
   */
  public Optional<MarginOperation> getMarginOperation() {
    return marginOperation;
  }
}

@@ -169,36 +169,8 @@
 n;%0A%0A
-import java.util.Optional;%0A%0A
 impo
@@ -512,25 +512,22 @@
 private 
-Optional%3C
+final 
 MarginOp
@@ -533,17 +533,16 @@
 peration
-%3E
  marginO
@@ -553,27 +553,8 @@
 tion
- = Optional.empty()
 ;%0A%0A 
@@ -898,24 +898,52 @@
 e, reason);%0A
+    marginOperation = null;%0A
   %7D%0A%0A  /**%0A 
@@ -1388,24 +1388,52 @@
 scription);%0A
+    marginOperation = null;%0A
   %7D%0A%0A  /**%0A 
@@ -2050,20 +2050,8 @@
 n = 
-Optional.of(
 oper
@@ -2051,25 +2051,24 @@
  = operation
-)
 ;%0A  %7D%0A%0A  /**
@@ -2180,40 +2180,97 @@
 tion
-, wrapped in an %7B@link O
+. This can be null, if there is no margin operation associated with the current exce
 ption
-al%7D
 %0A   
@@ -2285,17 +2285,8 @@
 lic 
-Optional%3C
 Marg
@@ -2300,9 +2300,8 @@
 tion
-%3E
  get

Remove the Optional wrapper from the field type, as this does not follow our code standards