//______________________________________________________________________________
//   |       |
//   | knipp |           Knipp Medien und Kommunikation GmbH
//    -------
//
//
//     Knipp RRI Toolkit
//
//
//     Copyright (C) 2005 by Knipp Medien und Kommunikation GmbH
//
//     This library is free software; you can redistribute it and/or
//     modify it under the terms of the GNU Lesser General Public License 
//     as published by the Free Software Foundation; either version 2.1 
//     of the License, or any later version.
// 
//     This library is distributed in the hope that it will be useful, 
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
//     GNU Lesser General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General
//     Public License along with this library; if not, write to the 
//     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
//     Boston, MA 02111-1307 USA 
//
//
//     initial author:    Klaus Malorny
//
//     version:           1.4 (15127), 2011/06/03 14:52:49
//                        $Revision: 14936 $ 
//                        $Date: 2010-11-05 15:57:40 +0100 (Fri, 05 Nov 2010) $
//                        $Author: Klaus.Malorny $
//

package de.knipp.rritoolkit.response;

import org.w3c.dom.Element;
import org.w3c.dom.DOMException;

import de.knipp.rritoolkit.KVList;
import de.knipp.rritoolkit.KVMultiList;
import de.knipp.rritoolkit.ResponseParseException;

import de.knipp.rritoolkit.message.Message;

import static de.knipp.rritoolkit.DenicConstants.NS_MESSAGE;

import static de.knipp.rritoolkit.XmlHelper.getFirstElement;



/**
 *  response of a queue read request
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public class QueueReadResponse extends Response
{
  /** the number of elements in the queue */
  
  private int messageCount;
  
  
  /** the read message, if any */
  
  private Message message;
  
  
  //--- implementation of Response ---------------------------------------------
  
  
  /**
   *  subclasses that need to read additional data from the key-value pair
   *  response should override this method. The default implementation is empty 
   *  and need not to be called.
   *
   *  @param list                     the list
   *  @throws ResponseParseException  if the document does not contain the
   *                                  expected content
   */
  
  @Override
  protected void dataFromList (KVMultiList list)
    throws ResponseParseException
  {
    if (isSuccess ())
    {
      KVList main = list.getMainSection ();
      String temp = main.getFirst ("msgcnt");
      
      try
      {
        messageCount = Integer.parseInt (temp);
        
      } catch (NumberFormatException e)
      {
        throw new ResponseParseException ("invalid message count", "msgcount", 
          temp, e);
      }
      
      if (main.getFirst ("msgid") != null)
      {
        message = Message.fromList (main);
      }
    }
  }

  /**
   *  subclasses that need to read additional data from the XML response should
   *  override this method. The default implementation is empty and need not
   *  to be called.
   *
   *  @param data                     the data element or {@code null}
   *                                  if the data contains no additional data
   *  @throws DOMException            on DOM exceptions
   *  @throws ResponseParseException  if the document does not contain the
   *                                  expected content
   */
  
  @Override
  protected void dataFromXml (Element data)
    throws DOMException, ResponseParseException
  {
    if (data != null && isSuccess ())
    {
      Element el = getFirstElement (data, NS_MESSAGE, "message");
      
      if (el == null) 
        throw new ResponseParseException ("message element missing", data);
      
      String temp = el.getAttribute ("msgcnt");
      
      if (temp == null)
        throw new ResponseParseException ("msgcnt attribute missing", el);
      
      try
      {
        messageCount = Integer.parseInt (temp);
        
      } catch (NumberFormatException e)
      {
        throw new ResponseParseException ("invalid message count", 
          el.getAttributeNode ("msgcnt"), e); 
      }

      if (el.getAttribute ("msgid") != null)
      {
        // there is obviously a message, parse it
        message = Message.fromXml (el);
      }
    }
  }
  
  /**
   *  calls the suitable method of the given visitor
   *
   *  @param visitor               the visitor
   */
  
  @Override
  public void accept (ResponseVisitor visitor)
  {
    visitor.visit (this);
  }
  
  
  
  
  /**
   *  returns a string representation for debugging purposes
   *
   *  @return                      the string representation
   */
  
  @Override
  public String toString ()
  {
    StringBuilder sb = new StringBuilder ();
    sb.append (super.toString ());
    sb.append ("  message count: ");
    sb.append (messageCount);
    sb.append ("\n");
    
    if (message != null)
    {
      sb.append ("  message:\n");
      sb.append (message);
    }
    
    return sb.toString ();
  }  
  
  
  
  //--- access methods ---------------------------------------------------------
  
  
  /**
   *  return the number of messages in the queue
   *
   *  @return                        the number of messages
   */
  
  public int getMessageCount ()
  {
    return messageCount;
  }
  
  
  
  /**
   *  return the retrieved message
   *
   *  @return               the message or {@code null} if no message is
   *                        in the queue
   */
  
  public Message getMessage ()
  {
    return message;
  }
  
  
}




