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


/**
 *  request to retrieve information about a given domain
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public abstract class DomainSimpleRequest extends Request
{
  /** the domain name */
  
  private String domainName;
  
  
  /** the encoded domain name */
  
  private String encodedDomainName;
  
  
  /**
   *  constructor
   */
  
  public DomainSimpleRequest ()
  {
    // empty
  }
  
  

  /**
   *  constructor
   *
   *  @param theDomainName          the domain name
   *  @param theEncodedDomainName   the encoded domain name, may be {@code null}
   *                                for ASCII only domains
   */
  
  public DomainSimpleRequest (String theDomainName, String theEncodedDomainName)
  {
    domainName = theDomainName;
    encodedDomainName = theEncodedDomainName;
  }


  
  /**
   *  constructor
   *
   *  @param theDomainName             the domain name
   */
  
  public DomainSimpleRequest (String theDomainName)
  {
    this (theDomainName, null);
  }
  
  
  //--- implementation of Request ----------------------------------------------
  
    
  /**
   *  return the XML element name
   *
   *  @return                      the element name
   */
  
  protected abstract String getRequestElementName ();
  
  

  /**
   *  allow the addition of attributes to the request. The default 
   *  implementation is empty and should not be called by implementing 
   *  subclasses
   *
   *  @param xb                   the XML builder
   *  @throws DOMException        on DOM exceptions
   *  @throws EncodeException     if the encoding fails for some reason
   */
  
  protected void addAttributes (XmlBuilder xb)
    throws DOMException, EncodeException
  {
    // empty
  }
  
  
  
  /**
   *  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 (domainName == null) throw new EncodeException ("domain name not set");
    
    XmlBuilder xb = new XmlBuilder (node);

    xb.es (NS_DOMAIN, PREFIX_DOMAIN, getRequestElementName ());
    addAttributes (xb);
    
    xb.elt (NS_DOMAIN, PREFIX_DOMAIN, "handle", domainName);
    
    if (encodedDomainName != null) 
      xb.elt (NS_DOMAIN, PREFIX_DOMAIN, "ace", encodedDomainName);
    
    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 (domainName == null) throw new EncodeException ("domain name not set");
    
    list.set ("domain", domainName);
    if (encodedDomainName != null) list.set ("domain-ace", encodedDomainName);
  }
  
  
  
  
  /**
   *  return a string representation for debugging purposes
   *
   *  @return                 the representation
   */

  @Override
  public String toString ()
  {
    return super.toString () + "  domain name: " + domainName +
      "\n  encoded domain name: " + encodedDomainName +
      "\n";
  }
  
  
  //--- access methods ---------------------------------------------------------
  
  /**
   *  return the domain name
   *
   *  @return                    the domain name
   */
  
  public String getDomainName ()
  {
    return domainName;
  }
  
  
  
  /**
   *  return the encoded domain name
   *
   *  @return                    the encoded domain name
   */
  
  public String getEncodedDomainName ()
  {
    return encodedDomainName;
  }
  
  
  
  /**
   *  set the domain name. The encoded domain name is cleared.
   *  This version should be use for non-IDNs only
   *
   *  @param theDomainName             the domain name
   */
  
  public void setDomainName (String theDomainName)
  {
    domainName = theDomainName;
    encodedDomainName = null;
  }
  
  
  
  /**
   *  set the domain name
   *
   *  @param theDomainName          the domain name
   *  @param theEncodedDomainName   the encoded domain name, may be {@code null}
   *                                for ASCII only domains
   */
  
  public void setDomainName (String theDomainName, String theEncodedDomainName)
  {
    domainName = theDomainName;
    encodedDomainName = theEncodedDomainName;
  }
  
}


