//______________________________________________________________________________
//   |       |
//   | 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.request;

import java.io.IOException;

import org.w3c.dom.Node;
import org.w3c.dom.DOMException;

import de.knipp.rritoolkit.KVList;
import de.knipp.rritoolkit.XmlBuilder;
import de.knipp.rritoolkit.Port;
import de.knipp.rritoolkit.EncodeException;

import de.knipp.rritoolkit.response.QueueDeleteResponse;

import de.knipp.rritoolkit.message.Message;

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


/**
 *  implementation of the queue read request
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public class QueueDeleteRequest extends Request
{
  
  /** the message ID */
  
  private String messageID;
  
  
  /** the message type */
  
  private Message.Type messageType;
  
  
  
  /**
   *  constructor
   */
  
  public QueueDeleteRequest ()
  {
    // empty
  }

  
  
  /**
   *  constructor
   *
   *  @param id                 the message ID
   */
  
  public QueueDeleteRequest (String id)
  {
    messageID = id;
  }
  
  
  /**
   *  constructor
   *
   *  @param id                 the message ID
   *  @param type               the type (may be {@code null})
   */
  
  public QueueDeleteRequest (String id, Message.Type type)
  {
    messageID = id;
    messageType = type;
  }
  
  
  
  /**
   *  return the type of the message that shall be deleted
   *
   *  @return                   the type or {@code null}
   *                            if the type is not set
   */
  
  public Message.Type getMessageType ()
  {
    return messageType;
  }
  
  
  
  /**
   *  set the message type of the message that shall be deleted. It is not
   *  required to specify the type, though.
   *
   *  @param type               the type or {@code null} if the type is
   *                            unknown
   */
  
  public void setMessageType (Message.Type type)
  {
    messageType = type;
  }
  

  
  /**
   *  return the message ID
   *
   *  @return                   the message ID
   */
  
  public String getMessageID ()
  {
    return messageID;
  }
  
  
  
  /**
   *  set the message ID
   *
   *  @param theMessageID        the message ID of the message
   */
  
  public void setMessageID (String theMessageID)
  {
    messageID = theMessageID;
  }
  
  
  
  /**
   *  take the type and ID from the given message
   *
   *  @param msg                 the message to be deleted
   */
  
  public void setMessage (Message msg)
  {
    messageID = msg.getMessageID ();
    messageType = msg.getType ();
  }
  
  
  
  //--- implementation of Request ----------------------------------------------
  

  /**
   *  execute the this request on the given port. 
   *
   *  @param port                 the port on which this request shall
   *                              be executed
   *  @return                     the response
   *  @throws IOException         if the execution failed
   */
  
  @Override
  public QueueDeleteResponse execute (Port port)
    throws IOException
  {
    return (QueueDeleteResponse) super.execute (port);
  }

  

  
  
  /**
   *  method that needs to be implemented by subclasses for the XML conversion
   *
   *  @param node                 the node where the request shall be added to
   *  @throws DOMException        on DOM exceptions
   *  @throws EncodeException     if the encoding fails for some reason
   */

  @Override
  protected void requestToXml (Node node)
    throws DOMException, EncodeException
  {
    XmlBuilder xb = new XmlBuilder (node);
    
    xb.es (NS_MESSAGE, "delete").attr ("msgid", messageID);
    if (messageType != null) xb.attr ("msgType", messageType.getDenicToken ());
    xb.ee ();
  }
  
  
  
  
  /**
   *  return the action string for this request
   *
   *  @return                     the action string
   */

  @Override
  protected String getActionCode ()
  {
    return "queue-delete";
  }
  

  
  /**
   *  method that needs to be implemented by subclasses for the 
   *  key-value list conversion
   *
   *  @param list                   the list that has to be filled
   *  @throws EncodeException     if the encoding fails for some reason
   */

  @Override
  protected void requestToKVList (KVList list)
    throws EncodeException
  {
    list.add ("msgid", messageID);
    if (messageType != null) list.add ("msgtype", messageType.getDenicToken ());
  }
  

 
  /**
   *  return a string representation for debugging purposes
   *
   *  @return                      the string representation
   */
  
  @Override
  public String toString ()
  {
    return super.toString () + "  id: " + messageID + 
      "\n  type: " + messageType + "\n";
  }
}



