//______________________________________________________________________________
//   |       |
//   | 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.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Date;

import java.io.Serializable;

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

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

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

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

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

/**
 *  abstract intermediate class which operates as the base class for 
 *  expire messages
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public abstract class ExpireMessageBase extends Message
{
  /** list of all domains */
  
  private List<Entry> domainList;
  
  
  
  /**
   *  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
  {
    List<Entry> tempList = new ArrayList<Entry> ();
    
    for (Element elDomain: getElements (el, NS_MESSAGE, "domain"))
    {
      String name = getFirstElementText (elDomain, NS_MESSAGE, "handle");
      String encodedName = getFirstElementText (elDomain, NS_MESSAGE, "ace");
      
      Date date = parseDateTime (
        getFirstElementText (elDomain, NS_MESSAGE, "expire"));
      
      tempList.add (new Entry (name, encodedName, date));
    }
    
    domainList = Collections.unmodifiableList (tempList);
  }
  

  
  
  
  /**
   *  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
  {
    List<Entry> tempList = new ArrayList<Entry> ();
    
    Iterator<String> i1 = list.getIterator ("domain");
    Iterator<String> i2 = list.getIterator ("domain-ace");
    Iterator<String> i3 = list.getIterator ("expire");
    
    while (i1.hasNext () && i2.hasNext () && i3.hasNext ())
    {
      tempList.add (new Entry (i1.next (), i2.next (), 
        parseDateTime (i3.next ())));
    }
    
    if (i1.hasNext () || i2.hasNext () || i3.hasNext ())
      throw new ResponseParseException ("corrupt domain list, different " +
        "numbers of domain, domain-ace and expire fields");
    
    domainList = Collections.unmodifiableList (tempList);
  }
  

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

  @Override
  public String toString ()
  {
    StringBuilder sb = new StringBuilder ();
    sb.append (super.toString ());

    int i = 1;
    for (Entry e: domainList)
    {
      sb.append ("  ");
      sb.append (i++);
      sb.append (": ");
      sb.append (e.getDomainName ());
      sb.append ("/");
      sb.append (e.getEncodedDomainName ());
      sb.append ("/");
      sb.append (e.getExpirationDate ());
      sb.append ("\n");
    }
    
    return sb.toString ();
  }
    
    
  //--- access methods ---------------------------------------------------------
  
  /**
   *  return the list of all reported domains
   *
   *  @return                        the list
   */

  public List<Entry> getList ()
  {
    return domainList;
  }
  
    
  
  
 
  //--- inner classes ----------------------------------------------------------
  
  /**
   *  class that holds the domain name as well as the expiration date
   *
   *  @version  $Revision: 14936 $
   *  @author   Klaus Malorny
   */
  
  public static class Entry implements Serializable
  {
    /** the domain name */
    
    private String domainName;
    
    
    /** the encoded domain name */
    
    private String encodedDomainName;
    
    
    /** the expiration date */
    
    private Date expirationDate;
    
    
    
    
    /**
     *  constructor
     *
     *  @param theDomainName                the domain name
     *  @param theEncodedDomainName         the encoded domain name
     *  @param theExpirationDate            the expiration date
     */
    
    Entry (String theDomainName, String theEncodedDomainName, 
      Date theExpirationDate)
    {
      domainName = theDomainName;
      encodedDomainName = theEncodedDomainName;
      expirationDate = theExpirationDate;
    }
   
    
    /**
     *  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 expiration date
     *
     *  @return                              the date
     */
    
    public Date getExpirationDate ()
    {
      return expirationDate;
    }
  }
  
}


