/***********************************************************
 Copyright (C) 2004 VeriSign, Inc.

 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 (at your option) 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-0107  USA

 http://www.verisign.com/nds/naming/namestore/techdocs.html
 ***********************************************************/
package com.verisign.epp.codec.change.type;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.verisign.epp.codec.change.EPPChangeMapFactory;
import com.verisign.epp.codec.change.util.ExceptionUtil;
import com.verisign.epp.codec.change.util.TextUtil;
import com.verisign.epp.codec.change.util.TypeUtil;
import com.verisign.epp.codec.gen.EPPCodecComponent;
import com.verisign.epp.codec.gen.EPPDecodeException;
import com.verisign.epp.codec.gen.EPPEncodeException;
import com.verisign.epp.codec.gen.EPPUtil;
import com.verisign.epp.util.EqualityUtil;

/**
 * Encapsulates an Change:Action attached to a Change:Request.
 * 
 * @author jcolosi
 */
public class EPPChangeCheckData implements EPPCodecComponent {
  static public final String ELM_NAME = "change:cd";

  private String requestId = null;
  private boolean exists = false;

  public EPPChangeCheckData() {
  }

  public EPPChangeCheckData(Element element) throws EPPDecodeException {
    decode(element);
  }

  public EPPChangeCheckData(String aRequestId, boolean aExists) {
    this();
    setRequestId(aRequestId);
    setExists(aExists);
  }

  public Object clone() throws CloneNotSupportedException {
    return (EPPChangeCheckData) super.clone();
  }

  /**
   * Decode the EPPChangeAction attributes from the DOM Element tree.
   * 
   * @param aElement
   *           - Root DOM Element to decode
   * @exception EPPDecodeException
   *               Unable to decode aElement
   * @throws EPPDecodeException
   *            On decoding error
   */
  public void decode(Element aElement) throws EPPDecodeException {
    setRequestId(EPPUtil.getTextContent(aElement));
    /**
     * LOGIC: The exists attr is required, EPPDecodeException is thrown here
     */
    setExists(EPPUtil.decodeBooleanAttr(aElement, TypeUtil.ATT_EXISTS));

    /**
     * LOGIC: Assert required params
     */
    ExceptionUtil.assertForDecode(requestId, TypeUtil.ELM_REQUESTID);
  }

  /**
   * Encode a DOM Element tree from the attributes of the EPPChangeTld
   * instance.
   * 
   * @param aDocument
   *           - DOM Document that is being built. Used as an Element factory.
   * @return Element - Root DOM Element representing the EPPChangeTld instance.
   * @exception EPPEncodeException
   *               - Unable to encode EPPChangeTld instance.
   */
  public Element encode(Document aDocument) throws EPPEncodeException {
    Element root = aDocument.createElementNS(EPPChangeMapFactory.NS, ELM_NAME);

    /**
     * LOGIC: Assert required params
     */
    ExceptionUtil.assertForEncode(requestId, TypeUtil.ELM_REQUESTID);

    root.appendChild(aDocument.createTextNode(requestId));
    EPPUtil.encodeBooleanAttr(root, TypeUtil.ATT_EXISTS, this.exists);
    return root;
  }

  /**
   * Implements a deep {@code EPPChangeAction} compare
   * 
   * @param o
   *           {@code EPPChangeAction} instance
   * @return {@code true} if equal; {@code false} otherwise.
   */
  public boolean equals(Object o) {
    if ((o == null) || (!o.getClass().equals(this.getClass())))
      return false;
    EPPChangeCheckData other = (EPPChangeCheckData) o;

    return EqualityUtil.equals(this.requestId, other.requestId) && this.exists == other.exists;
  }

  public String getRequestId() {
    return requestId;
  }

  /**
   * The name of this method is forced because I want to be compliant with Java
   * Bean standards as well as EPP naming conventions.
   * 
   * @return {@code true} if exists; {@code false} otherwise.
   */
  public boolean isExists() {
    return exists;
  }

  public void setExists(boolean exists) {
    this.exists = exists;
  }

  public void setRequestId(String requestId) {
    this.requestId = TextUtil.normalize(requestId);
  }

  /**
   * Implementation of {@code Object.toString}, which will result in an
   * indented XML {@code String} representation of the concrete
   * {@code EPPCodecComponent}.
   * 
   * @return Indented XML {@code String} if successful; {@code ERROR}
   *         otherwise.
   */
  public String toString() {
    return EPPUtil.toString(this);
  }

  /**
   * Returns the XML namespace associated with the
   * {@code EPPCodecComponent}.
   * 
   * @return XML namespace for the {@code EPPCodecComponent}.
   */
  @Override
  public String getNamespace() {
    return EPPChangeMapFactory.NS;
  }

}
