package eu.osci;

import javax.xml.ws.soap.SOAPFaultException;

import eu.osci.messagetypes.OSCIMessage;


/**
 * This class describes all exceptions owned by the OSCI2-StarterKit.<br>
 * Copyright © 2010 Freie Hansestadt Bremen. <br>
 * Created by bremen online services Entwicklungs- und Betriebsgesellschaft mbH & Co. KG. <br>
 * This library (Starter Kit) can be used by anybody according to the Bremer Lizenz für freie
 * Softwarebibliotheken. The license terms can be viewed at www.osci.de or can be ordered at the
 * OSCI-Leitstelle (Die Senatorin für Finanzen, Freie Hansestadt Bremen, Postfach 10 15 40, 28015 Bremen).
 * 
 * @author R. Lindemann
 */
public class OSCIException extends Exception
{

  private static final long serialVersionUID = 1L;

  SOAPFaultException cause;

  String code;

  String subcode;

  String reason;

  public SOAPFaultException getCause()
  {
    return cause;
  }

  public String getCode()
  {
    return code;
  }

  public String getSubcode()
  {
    return subcode;
  }

  public String getReason()
  {
    return reason;
  }

  /**
   * Creates an OSCIException object with the given parameters.
   * 
   * @param osciFault
   * @param _details
   * @param ex
   */
  public OSCIException(OSCIFault osciFault, String _details, Throwable ex)
  {
    init(osciFault, _details, ex);
  }

  private void init(OSCIFault osciFault, String _details, Throwable ex)
  {
    code = osciFault.getCode().getLocalPart();
    subcode = osciFault.getSubcode().getLocalPart();
    if (_details != null)
    {
      reason = osciFault.getReason() + " DETAILS: " + _details;
    }
    else
      reason = osciFault.getReason();
  }

  /**
   * Creates an OSCIException object with the given parameters.
   * 
   * @param osciFault
   * @param ex
   */
  public OSCIException(OSCIFault osciFault, Throwable ex)
  {
    init(osciFault, null, ex);
  }

  /**
   * Creates an OSCIException object with the given parameters.
   * 
   * @param _code
   * @param _subcode
   * @param _reason
   * @param _cause
   */
  public OSCIException(String _code, String _subcode, String _reason, SOAPFaultException _cause)
  {
    code = _code;
    subcode = _subcode;
    reason = _reason;
    cause = _cause;
  }

  @Override
  public String toString()
  {
    return "Found OSCI error code!\nError code: " + code + "\nSubcode: " + subcode + "\nReason: " + reason;
  }

  /**
   * Returns the localised exception message in the set national language.
   */
  public String getLocalizedMessage()
  {
    try
    {
      return OSCIMessage.text.getString(subcode);
    }
    catch (Exception ex)
    {
      return getMessage();
    }
  }

}
