//______________________________________________________________________________
//   |       |
//   | 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 org.w3c.dom.Node;
import org.w3c.dom.DOMException;

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

import static de.knipp.rritoolkit.DenicConstants.NS_DOMAIN;
import static de.knipp.rritoolkit.DenicConstants.PREFIX_DOMAIN;



/**
 *  base class for all domain request that require a DomainData instance
 *  for operation (create, update, chprov, chholder, delete)
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public abstract class DomainDataRequest extends Request
{
  /** the domain data instance */
  
  protected DomainData domain;
  
  
  /**
   *  constructor
   */
  
  public DomainDataRequest ()
  {
    // empty
  }
  
  
  /**
   *  constructor
   *
   *  @param theDomain             the domain
   */
  
  public DomainDataRequest (DomainData theDomain)
  {
    setDomain (theDomain);
  }
  
  
  //--- implementation of Request ----------------------------------------------

  
  /**
   *  return whether DNS data shall be supplied or not. The default 
   *  implementation returns {@code true}
   *
   *  @return                  {@code true} if the data shall be supplied
   */
  
  protected boolean includeDnsData ()
  {
    return true;
  }
  

  /**
   *  return the XML element name
   *
   *  @return                      the element name
   */
  
  protected abstract String getRequestElementName ();
  
  
  
  
  /**
   *  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
  {
    if (domain == null) throw new EncodeException ("domain not set");
    
    XmlBuilder xb = new XmlBuilder (node);
    xb.es (NS_DOMAIN, PREFIX_DOMAIN, getRequestElementName ());
    domain.toXml (xb.getCurrent (), includeDnsData ());
    xb.ee ();
  }
  


  
  
  
  /**
   *  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
  {
    if (domain == null) throw new EncodeException ("domain not set");
    domain.toList (list, includeDnsData ());
  }
  
  
  
  
  /**
   *  return a string representation for debugging purposes
   *
   *  @return                 the representation
   */
  
  @Override
  public String toString ()
  {
    return super.toString () + domain;
  }
  

  //--- access methods ---------------------------------------------------------
  
  /**
   *  set the domain data
   *
   *  @param theDomain             the domain
   */
  
  public void setDomain (DomainData theDomain)
  {
    domain = theDomain;
  }
  
  
  
  /**
   *  return the domain
   *
   *  @return                       the domain or {@code null} if it
   *                                hasn't been set yet
   */
  
  public DomainData getDomain ()
  {
    return domain;
  }
}



