//______________________________________________________________________________
//   |       |
//   | 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.3 (15112), 2011/05/27 16:00:14
//                        $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 static de.knipp.rritoolkit.DomainData.DomainState;

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

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


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

public class DomainCheckResponse extends Response
{
  /** the domain name */
  
  private String domainName;
  
  
  /** the encoded domain name */
  
  private String encodedDomainName;

  
  /** the state of the domain */
  
  DomainState state;

  
  
  //--- 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 ();
      
      domainName = main.getFirst ("domain");
      encodedDomainName = main.getFirst ("domain-ace");

      String temp = main.getFirst ("status");

      if (temp == null)
        throw new ResponseParseException ("missing status field", "status",
          (String) null);

      state = DomainState.fromToken (temp);

      if (state == null) 
        throw new ResponseParseException ("invalid status", "status", temp);
    }
  }

  /**
   *  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_DOMAIN, "checkData");
      
      if (el != null)
      {
        domainName = getFirstElementText (el, NS_DOMAIN, "handle");
        encodedDomainName = getFirstElementText (el, NS_DOMAIN, "ace");

        String temp = getFirstElementText (el, NS_DOMAIN, "status");

        if (temp == null)
          throw new ResponseParseException ("missing status element", el);

        state = DomainState.fromToken (temp);

        if (state == null) 
          throw new ResponseParseException ("invalid status element", 
            getFirstElement (el, NS_DOMAIN, "status"));
      }
    }
  }
  
  /**
   *  call the suitable method of the given visitor
   *
   *  @param visitor               the visitor
   */
  
  @Override
  public void accept (ResponseVisitor visitor)
  {
    visitor.visit (this);
  }
  
  
  
  
  /**
   *  return a string representation for debugging purposes
   *
   *  @return                      the string representation
   */
  
  @Override
  public String toString ()
  {
    return super.toString () + "  domain name: " + domainName + 
      "\n  encoded domain name: " + encodedDomainName + 
      "\n  state: " + state +
      "\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;
  }
  

  
  /**
   *  return the status
   *
   *  @return            the state or {@code null} in case of an error
   */
  
  public DomainState getState ()
  {
    return state;
  }
  
  
}




