/***********************************************************
 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 java.util.ArrayList;
import java.util.List;

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.util.ExceptionUtil;
import com.verisign.epp.codec.change.util.TextUtil;
import com.verisign.epp.codec.change.util.TypeUtil;
import com.verisign.epp.codec.gen.EPPCreateCmd;
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;

/**
 * Represents an EPP &lt;change:create&gt; command that is used to create an EPP
 * Change Request object. The &lt;change:create&gt; element MUST contain the
 * following child elements: <br>
 * <br>
 * <ul>
 * <li>A &lt;change:requestID&gt; element that contains the Request Id. Use
 * {@code getRequest} and {@code setRequest} to get and set the
 * element.</li>
 * <li>A &lt;change:category&gt; element that contains the Request Category.
 * Use {@code getCategory} and {@code setCategory} to get and set
 * the element.</li>
 * <li>A &lt;desc&gt; element that contains the Request Description. Use
 * {@code getDescription} and {@code setDescription} to get and
 * set the element.</li>
 * </ul>
 * <br>
 * @author jcolosi
 */
public class EPPChangeCreateCmd extends EPPCreateCmd {
  static final String ELM_NAME = "change:create";

  private String requestId = null;
  private String priority = null;
  private List categories = null;
  private String description = null;

  public EPPChangeCreateCmd() {
    this(null);
  }

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

  public EPPChangeCreateCmd(String requestId, String category, String description) {
    this(null, requestId, null, category, description);
  }

  public EPPChangeCreateCmd(String aTransId, String requestId, String category,
      String description) {
    this(aTransId, requestId, null, category, description);
  }

  public EPPChangeCreateCmd(String aTransId, String requestId, String priority,
      String category, String description) {
    this(aTransId);
    setRequestId(requestId);
    setPriority(priority);
    addCategory(category);
    setDescription(description);
  }

  public EPPChangeCreateCmd(String aTransId, String requestId, String priority,
      List categories, String description) {
    this(aTransId);
    setRequestId(requestId);
    setPriority(priority);
    setCategories(categories);
    setDescription(description);
  }

  public void addCategory(String category) {
    if (category != null) {
      if (categories == null) categories = new ArrayList();
      categories.add(TextUtil.normalize(category));
    }
  }

  public void clearCategories() {
    this.categories = null;
  }

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

  /**
   * Compare an instance of {@code EPPChangeCreateCmd} with this
   * instance
   */
  public boolean equals(Object o) {
    if ((o == null) || (!o.getClass().equals(this.getClass()))) return false;
    EPPChangeCreateCmd other = (EPPChangeCreateCmd) o;
    return EqualityUtil.equals(this.requestId, other.requestId)
        && EqualityUtil.equals(this.priority, other.priority)
        && EqualityUtil.equals(this.categories, other.categories)
        && EqualityUtil.equals(this.description, other.description);
  }

  public List getCategories() {
    return categories;
  }

  public String getDescription() {
    return description;
  }

  /**
   * Gets the EPP command Namespace associated with
   * {@code EPPChangeCreateCmd}.
   * @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 getPriority() {
    return priority;
  }

  public String getRequestId() {
    return requestId;
  }

  public void setCategories(List categories) {
    this.categories = categories;
  }

  public void setDescription(String description) {
    this.description = TextUtil.normalize(description);
  }

  public void setPriority(String priority) {
    this.priority = TextUtil.normalize(priority);
  }

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

  /**
   * Decode the {@code EPPChangeCreateCmd} attributes from the aElement
   * DOM Element tree.
   * @param aElement Root DOM Element to decode
   *            {@code EPPChangeCreateCmd} 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(TypeUtil.ELM_PRIORITY))) {
            setPriority(EPPUtil.getTextContent(node));
          } else if (name.equals(EPPUtil.getLocalName(TypeUtil.ELM_CATEGORY))) {
            addCategory(EPPUtil.getTextContent(node));
          } else if (name.equals(EPPUtil.getLocalName(TypeUtil.ELM_DESCRIPTION))) {
            setDescription(EPPUtil.getTextContent(node));
          }
        }
      }
    }

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

  /**
   * Encode a DOM Element tree from the attributes of the
   * {@code EPPChangeCreateCmd} instance.
   * @param aDocument DOM Document that is being built. Used as an Element
   *            factory.
   * @return Root DOM Element representing the {@code EPPChangeCreateCmd}
   *         instance.
   * @exception EPPEncodeException Unable to encode
   *                {@code EPPChangeCreateCmd} 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);
    ExceptionUtil.assertForEncode(categories, TypeUtil.ELM_CATEGORY);
    ExceptionUtil.assertForEncode(description, TypeUtil.ELM_DESCRIPTION);

    EPPUtil.encodeString(aDocument, root, requestId, EPPChangeMapFactory.NS,
        TypeUtil.ELM_REQUESTID);
    EPPUtil.encodeString(aDocument, root, priority, EPPChangeMapFactory.NS,
        TypeUtil.ELM_PRIORITY);
    EPPUtil.encodeList(aDocument, root, categories, EPPChangeMapFactory.NS,
        TypeUtil.ELM_CATEGORY);
    EPPUtil.encodeString(aDocument, root, description, EPPChangeMapFactory.NS,
        TypeUtil.ELM_DESCRIPTION);

    return root;
  }

}
