package eu.osci.messagetypes;

import java.util.logging.Logger;

import javax.xml.bind.JAXBException;

import com.sun.xml.ws.api.message.Header;
import com.sun.xml.ws.api.message.HeaderList;
import com.sun.xml.ws.api.message.Message;

import eu.osci.ErrorTextTranslation;
import eu.osci.messageparts.OSCIFragmentParser;
import eu.osci.ws._2008._05.transport.MsgBoxResponseType;
import eu.osci.ws._2008._05.transport.MsgStatusListType;


/**
 * This class will be returned if the client sends a MsgBoxStatusListRequest. It includes a list of status
 * information for the according search parameter.<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 OSCIMsgBoxStatusListResponse extends OSCIMessage
{

  /** Logger to log the messages. */
  private static Logger LOG = Logger.getLogger(OSCIMsgBoxStatusListResponse.class.getName());

  private MsgBoxResponseType msgBoxResponseType;

  private MsgStatusListType msgStatusList;

  protected Message soapMessage;

  /**
   * This constructor will be used to parse the incoming SOAP header and the body element. It will be called
   * by the send method of the associated request message.
   * 
   * @param _soapMessage The incoming SOAP message with the given header and body elements.
   * @throws JAXBException In case that the header elements or the body could not be parsed.
   */
  public OSCIMsgBoxStatusListResponse(Message _soapMessage) throws JAXBException
  {
    LOG.fine("Entry constructor with SOAO message");
    soapMessage = _soapMessage;
    interpretHeaders(soapMessage.getHeaders());
  }

  /**
   * <b>For internal usage only!</b>
   */
  protected OSCIMsgBoxStatusListResponse()
  {
    super();
  }

  /**
   * Interprets the incoming header elements and sets the WS-Addressing and OSCI element on this method.
   * 
   * @param headerList The header list that will be parsed.
   * @throws JAXBException In case of parsing errors.
   */
  protected void interpretHeaders(HeaderList headerList) throws JAXBException
  {
    for ( Header header : headerList )
    {
      LOG.fine("Found header: " + header.getLocalPart());
    }
    // ### Addressing Header ############
    interpretWSAHeaders(headerList);
    // ##### OSCI header ##########
    msgBoxResponseType = OSCIFragmentParser.getMsgBoxResponseType(headerList);
    setAdditionalHeaderList(headerList);
  }

  /**
   * Returns the count of the next available messages.
   * 
   * @return The count of pending messages.
   */
  public long getItemsPending()
  {
    if ((soapMessage == null) || (msgBoxResponseType == null))
    {
      throw new IllegalStateException(
                                      OSCIMessage.text.getString(ErrorTextTranslation.OBJECT_NOT_INITIALIZED.getConstant()));
    }
    return msgBoxResponseType.getItemsPending().longValue();
  }

  /**
   * Returns true if there are no messages found for the selection.
   * 
   * @return False if messages are found, otherwise true.
   */
  public boolean noMessageFoundForSelection()
  {
    if ((soapMessage == null) || (msgBoxResponseType == null))
    {
      throw new IllegalStateException(
                                      OSCIMessage.text.getString(ErrorTextTranslation.OBJECT_NOT_INITIALIZED.getConstant()));
    }
    if (msgBoxResponseType.getNoMessageAvailable() != null)
    {
      return true;
    }
    return false;
  }

  /**
   * The fetched information with all the associated elements (or the limited count).
   * 
   * @return The result object of the status list information.
   * @throws JAXBException
   */
  public MsgStatusListType getMsgStatusList() throws JAXBException
  {
    if (soapMessage == null)
    {
      throw new IllegalStateException(
                                      OSCIMessage.text.getString(ErrorTextTranslation.OBJECT_NOT_INITIALIZED.getConstant()));
    }
    if (msgStatusList == null)
    {
      msgStatusList = OSCIFragmentParser.getMsgStatusListType(soapMessage);
    }
    return msgStatusList;
  }

  /**
   * Returns the OSCI MsgBoxRepsonse object with additional information about the fetched list (ItemsPending,
   * fetchIteratorID, NoMessageFound)
   * 
   * @return The OSCI object with additional information.
   */
  public MsgBoxResponseType getMsgBoxResponseType()
  {
    return msgBoxResponseType;
  }

  /**
   * Returns the id that was generated by the server for the search parameter of the request message. The
   * fetchIteratorId could be used for getNext requests.
   * 
   * @return The generated id for next requests.
   */
  public String getFetchIteratorID()
  {
    if ((soapMessage == null) || (msgBoxResponseType == null))
    {
      throw new IllegalStateException(
                                      OSCIMessage.text.getString(ErrorTextTranslation.OBJECT_NOT_INITIALIZED.getConstant()));
    }
    return msgBoxResponseType.getMsgBoxRequestID();
  }
}
