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

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

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

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

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

/**
 *  base class for both the messages related to the old provider change
 *  process and related to the new AuthInfo based mechanism
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public abstract class ChangeProvMessageBase extends Message
{
  /** the domain name */

  private String domainName;


  /** the encoded domain name */

  private String encodedDomainName;


  /** the new (gaining) provider */

  private String newProvider;


  /** the old (losing) provider */

  private String oldProvider;


  /**
   *  setup the object's data from the given XML element.
   *
   *  @param el                       the element
   *  @throws DOMException            on DOM exceptions
   *  @throws ResponseParseException  if the data was not recognized
   */

  @Override
  protected void dataFromXml (Element el)
    throws DOMException, ResponseParseException
  {
    Element elDomain = getFirstElement (el, NS_MESSAGE, "domain");

    if (elDomain == null)
      throw new ResponseParseException ("domain element missing", el);

    domainName = getFirstElementText (elDomain, NS_MESSAGE, "handle");
    encodedDomainName = getFirstElementText (elDomain, NS_MESSAGE, "ace");

    if (domainName == null || encodedDomainName == null)
      throw new ResponseParseException ("domain name elements missing",
        elDomain);

    oldProvider = getFirstElementText (el, NS_MESSAGE, "old");
    newProvider = getFirstElementText (el, NS_MESSAGE, "new");

    if (oldProvider == null || newProvider == null)
      throw new ResponseParseException ("either the old, the new or both " +
        "provider(s) is/are missing", el);
  }



  /**
   *  setup the object's data from the given list. Subclasses should
   *  override this method and call the superclass first.
   *
   *  @param list                     the list
   *  @throws ResponseParseException  if the list does not contain the
   *                                  expected content
   */

  @Override
  protected void dataFromList (KVList list)
    throws ResponseParseException
  {
    domainName = list.getFirst ("domain");
    encodedDomainName = list.getFirst ("domain-ace");

    if (domainName == null)
      throw new ResponseParseException ("domain name missing", "domain",
        (String) null);

    if (encodedDomainName == null)
      throw new ResponseParseException ("encoded domain name missing",
        "domain-ace", (String) null);

    oldProvider = list.getFirst ("chprov-from");
    newProvider = list.getFirst ("chprov-to");

    if (oldProvider == null)
      throw new ResponseParseException ("old provider is missing",
        "chprov-from", (String) null);

    if (newProvider == null)
      throw new ResponseParseException ("new provider is missing",
        "chprov-to", (String) null);
  }


  /**
   *  return a string representation for debugging purposes
   *
   *  @return                      the string representation
   */

  @Override
  public String toString ()
  {
    StringBuilder sb = new StringBuilder ();
    sb.append (super.toString ());
    sb.append ("  domain name: ");
    sb.append (domainName);
    sb.append ("\n  encoded domain name: ");
    sb.append (encodedDomainName);
    sb.append ("\n  old provider: ");
    sb.append (oldProvider);
    sb.append ("\n  new provider: ");
    sb.append (newProvider);
    sb.append ("\n");

    return sb.toString ();
  }

  //--- 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 designated new provider
   *
   *  @return                       the new provider
   */

  public String getNewProvider ()
  {
    return newProvider;
  }



  /**
   *  return the old/current provider
   *
   *  @return                       the old provider
   */

  public String getOldProvider ()
  {
    return oldProvider;
  }



  
}




