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

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

import com.verisign.epp.codec.change.type.EPPChangeUpdateAttributes;
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.EPPDecodeException;
import com.verisign.epp.codec.gen.EPPEncodeException;
import com.verisign.epp.codec.gen.EPPUpdateCmd;
import com.verisign.epp.codec.gen.EPPUtil;
import com.verisign.epp.util.EqualityUtil;

/**
 * Represents an EPP &lt;change:update&gt; command that is used to modify an
 * existing EPP Change Request object. The &lt;change:update&gt; element MAY
 * contain the following child elements: <br>
 * <br>
 * <ul>
 * <li>A &lt;change:upAttrs&gt; element that contains the Change Request
 * attributes for modification. Use {@code getAttributes} and
 * {@code setAttributes} to get and set the element.</li>
 * <li>An empty &lt;change:clear&gt; element. Use {@code isClear} and
 * {@code setClear} to get and set the element.</li>
 * <li>An empty &lt;change:submit&gt; element. Use {@code isSubmit} and
 * {@code setSubmit} to get and set the element.</li>
 * <li>An empty &lt;change:withdraw&gt; element. Use {@code isWithdraw} and
 * {@code setWithdraw} to get and set the element.</li>
 * </ul>
 * <br>
 * @see com.verisign.epp.codec.change.EPPChangeUpdateResp
 * @author jcolosi
 */
public class EPPChangeUpdateCmd extends EPPUpdateCmd {

  static final String ELM_NAME = "change:update";

  private EPPChangeUpdateAttributes attributes = null;
  private boolean clear = false;
  private boolean submit = false;
  private boolean withdraw = false;
  private String requestId = null;

  public EPPChangeUpdateCmd() {
    this(null);
  }

  public EPPChangeUpdateCmd(String aTransId) {
    super(aTransId);
  }

  public EPPChangeUpdateCmd(String aTransId, String requestId) {
    super(aTransId);
    setRequestId(requestId);
  }

  /**
   * Clone {@code EPPChangeUpdateCmd}.
   * @return clone of {@code EPPChangeUpdateCmd}
   * @exception CloneNotSupportedException standard Object.clone exception
   */
  public Object clone() throws CloneNotSupportedException {
    return (EPPChangeUpdateCmd) super.clone();
  }

  /**
   * Compare an instance of {@code EPPChangeUpdateCmd} with this
   * instance
   */
  public boolean equals(Object o) {
    if ((o == null) || (!o.getClass().equals(this.getClass()))) return false;
    EPPChangeUpdateCmd other = (EPPChangeUpdateCmd) o;

    return EqualityUtil.equals(this.requestId, other.requestId)
        && EqualityUtil.equals(this.attributes, other.attributes)
        && this.clear == other.clear && this.submit == other.submit && this.withdraw == other.withdraw;
  }

  public EPPChangeUpdateAttributes getAttributes() {
    return attributes;
  }

  /**
   * Gets the EPP command Namespace associated with
   * {@code EPPChangeUpdateCmd}.
   * @return {@code EPPChangeMapFactory.NS}
   */
  public String getNamespace() {
    return EPPChangeMapFactory.NS;
  }
  
  /**
   * Gets the key for the change object, which is the request identifier.
   * 
   * @return The request identifier if set; {@code null} otherwise.
   */
  @Override
  public String getKey() {
    return this.getRequestId();
  }
  

  public String getRequestId() {
    return requestId;
  }

  public boolean isClear() {
    return clear;
  }

  public boolean isSubmit() {
    return submit;
  }
  
  public boolean isWithdraw() {
    return withdraw;
  }

  public void setAttributes(EPPChangeUpdateAttributes attributes) {
    this.attributes = attributes;
  }

  public void setClear(boolean clear) {
    this.clear = clear;
  }

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

  public void setSubmit(boolean submit) {
    this.submit = submit;
  }
  
  public void setWithdraw(boolean withdraw) {
    this.withdraw = withdraw;
  }

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

  /**
   * Decode the {@code EPPChangeUpdateCmd} attributes from the aElement
   * DOM Element tree.
   * @param aElement Root DOM Element to decode
   *            {@code EPPChangeUpdateCmd} from.
   * @exception EPPDecodeException Unable to decode aElement
   */
  protected void doDecode(Element aElement) throws EPPDecodeException {
    /**
     * LOGIC: Decode nested elements
     */
    NodeList nodes = aElement.getChildNodes();
    if (nodes != null) {
      int size = nodes.getLength();
      for (int i = 0; i < size; i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
          String name = node.getLocalName();
          if (name.equals(EPPUtil
              .getLocalName(TypeUtil.ELM_REQUESTID))) {
            setRequestId(EPPUtil.getTextContent(node));
          }
          else if (name.equals(EPPUtil
              .getLocalName(EPPChangeUpdateAttributes.ELM_NAME))) {
            setAttributes(new EPPChangeUpdateAttributes(
                (Element) node));
          }
          else if (name.equals(EPPUtil
              .getLocalName(TypeUtil.ELM_CLEAR)))
            setClear(true);
          else if (name.equals(EPPUtil
              .getLocalName(TypeUtil.ELM_SUBMIT)))
            setSubmit(true);
          else if (name.equals(EPPUtil
              .getLocalName(TypeUtil.ELM_WITHDRAW)))
            setWithdraw(true);
        }
      }
    }

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

  /**
   * Encode a DOM Element tree from the attributes of the
   * {@code EPPChangeUpdateCmd} instance.
   * @param aDocument DOM Document that is being built. Used as an Element
   *            factory.
   * @return Root DOM Element representing the {@code EPPChangeUpdateCmd}
   *         instance.
   * @exception EPPEncodeException Unable to encode
   *                {@code EPPChangeUpdateCmd} instance.
   */
  protected Element doEncode(Document aDocument) throws EPPEncodeException {
    Element root = aDocument.createElementNS(EPPChangeMapFactory.NS, ELM_NAME);
    root.setAttribute("xmlns:change", EPPChangeMapFactory.NS);

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

    EPPUtil.encodeString(aDocument, root, requestId, EPPChangeMapFactory.NS,
        TypeUtil.ELM_REQUESTID);
    if (attributes != null) EPPUtil.encodeComp(aDocument, root, attributes);
    if (isClear()) root.appendChild(aDocument.createElementNS(EPPChangeMapFactory.NS, TypeUtil.ELM_CLEAR));
    if (isSubmit()) root.appendChild(aDocument.createElementNS(EPPChangeMapFactory.NS, TypeUtil.ELM_SUBMIT));
    if (isWithdraw()) root.appendChild(aDocument.createElementNS(EPPChangeMapFactory.NS, TypeUtil.ELM_WITHDRAW));

    return root;
  }
}
