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

import java.util.Date;

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

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

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

import static de.knipp.rritoolkit.Utils.parseDateTime;

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

/**
 *  class that represents the AuthInfo expire message
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public class AuthInfoExpireMessage extends Message
{
  /** the domain name */

  private String domainName;


  /** the encoded domain name */

  private String encodedDomainName;


  /** the expire message */

  private Date expireDate;





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

    expireDate = parseDateTime (
      getFirstElementText (el, NS_MESSAGE, "authinfoexpire"));

    if (expireDate == null)
      throw new ResponseParseException ("expire date is missing", el);
  }




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

    expireDate = parseDateTime (list.getFirst ("authinfoexpire"));

    if (expireDate == null)
      throw new ResponseParseException ("expire date is missing");
  }


  /**
   *  call the suitable method of the given visitor
   *
   *  @param visitor               the visitor
   */

  @Override
  public void accept (MessageVisitor visitor)
  {
    visitor.visit (this);
  }




  /**
   *  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  expire date: ");
    sb.append (expireDate);
    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 AuthInfo expire date
   *
   *  @return               the expire date
   */

  public Date getExpireDate ()
  {
    return expireDate;
  }


}


