//______________________________________________________________________________
//   |       |
//   | 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: 15127 $
//                        $Date: 2011-06-03 13:12:12 +0200 (Fri, 03 Jun 2011) $
//                        $Author: Klaus.Malorny $
//

package de.knipp.rritoolkit.message;

import java.util.List;
import java.util.ArrayList;

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

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

import de.knipp.rritoolkit.response.ResultMessage;

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

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



/**
 *  common base class for the {@code DomainDeleteMessage} and
 *  {@code AuthInfo2DeleteMessage} classes
 *
 *  @version  $Revision: 15127 $
 *  @author   Klaus Malorny
 */

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

  private String domainName;


  /** the encoded domain name */

  private String encodedDomainName;


  /** list of results */

  private List<ResultMessage> results;





  /**
   *  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);

    results = new ArrayList<ResultMessage> ();

/**/ // Workaround for RRI implementation fix: at the time of writing,
/**/ // the message element uses the transaction XML namespace, which is wrong
/**/ // according to the documentation
/**/ for (Element elResult: getElements (el, NS_TXN, "message"))
/**/   results.add (new ResultMessage (elResult));
    
    for (Element elResult: getElements (el, NS_MESSAGE, "message"))
      results.add (new ResultMessage (elResult));
  }




  /**
   *  setup the object's data from the given list.
   *
   *  @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);

    results = new ArrayList<ResultMessage> ();
    String key = "message";

    for (String result: list.getAll (key))
      results.add (new ResultMessage (key, false, result));
  }




  /**
   *  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  results:\n");

    for (ResultMessage msg: results)
    {
      sb.append ("    ");
      sb.append (msg);
      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 results containing the reason(s)
   *
   *  @return               the list
   */

  public List<ResultMessage> getResults ()
  {
    return results;
  }


}
