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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.verisign.epp.codec.change.EPPChangeCheckCmd;
import com.verisign.epp.codec.change.EPPChangeCreateCmd;
import com.verisign.epp.codec.change.EPPChangeDeleteCmd;
import com.verisign.epp.codec.change.EPPChangeInfoCmd;
import com.verisign.epp.codec.change.EPPChangeMapFactory;
import com.verisign.epp.codec.change.EPPChangeUpdateCmd;
import com.verisign.epp.codec.changeext.EPPChangeExtFactory;
import com.verisign.epp.codec.gen.EPPCodecException;
import com.verisign.epp.codec.gen.EPPFactory;
import com.verisign.epp.codec.gen.EPPMessage;

/**
 * The <code>EPPChangeHandler</code> class provides an interface for handling
 * EPP Change Commands in a Server implementation. EPPEvents are handled by the
 * handleEvent() method here and routed to the appropriate abstract member
 * function. Subclasses should override the abstract methods to define the
 * desired behavior of a particular command when it is received. A subclassed
 * instance of <code>EPPChangeHandler</code> should be registered with the
 * <code>EPPDispatcher</code> so that EEPEvents related to the Change Mapping
 * will be handled there. <br>
 * <br>
 * 
 * @author $Author: jim $
 * @version $Revision: 1.1.1.1 $
 * @see EPPEventHandler
 * @see EPPEvent
 */
public abstract class EPPChangeHandler implements EPPEventHandler {
  /** The Namespace that this handler supports. In this case, Change. */
  private static final String NS = EPPChangeMapFactory.NS;

  /** Category for logging */
  private static Logger cat = LoggerFactory.getLogger(EPPChangeHandler.class);

  /**
   * Construct an instance of <code>EPPChangeHandler</code>
   */
  /**
   * Whenever an EPPChangeHandler instance is created load the corresponsding
   * Map Factory into the Codec
   */
  public EPPChangeHandler() {
    try {
      EPPFactory.getInstance().addMapFactory(EPPChangeMapFactory.class.getName());
    }
    catch (EPPCodecException e) {
      cat.error("Couldn't load the Map Factory for the Change Mapping", e);
      System.exit(1);
    }
    try {
      EPPFactory.getInstance().addExtFactory(EPPChangeExtFactory.class.getName());
    }
    catch (EPPCodecException e) {
      cat.error("Couldn't load the Ext Factory for the ChangeExt Mapping", e);
      System.exit(1);
    }
  }

  /**
   * Returns the Namespace that this handler supports. In this case, Change.
   * 
   * @return String The Namespace that this handler supports
   */
  public final String getNamespace() {
    return NS;
  }

  /**
   * This method receives an <code>EPPEvent</code> and routes it to the
   * appropriate abstract method.
   * 
   * @param aEvent
   *           An <code>EPPEvent</code> that contains the
   *           <code>EPPCommand</code>
   * @param aData
   *           Any data that a Server needs to send to this
   *           <code>EPPContactHandler</code> instance
   * @return DOCUMENT ME!
   * @exception EPPEventException
   *               Thrown if an unrecognized <code>EPPEvent</code> is received
   */
  public final EPPEventResponse handleEvent(EPPEvent aEvent, Object aData) throws EPPEventException {
    try {
      this.preHandleEvent(aEvent, aData);
    }
    catch (EPPHandleEventException e) {
      return new EPPEventResponse(e.getResponse());
    }

    EPPMessage msg = aEvent.getMessage();
    EPPEventResponse rsp;

    if (msg instanceof EPPChangeCheckCmd)
      rsp = doChangeCheck(aEvent, aData);
    else if (msg instanceof EPPChangeInfoCmd)
      rsp = doChangeInfo(aEvent, aData);
    else if (msg instanceof EPPChangeCreateCmd)
      rsp = doChangeCreate(aEvent, aData);
    else if (msg instanceof EPPChangeUpdateCmd)
      rsp = doChangeUpdate(aEvent, aData);
    else if (msg instanceof EPPChangeDeleteCmd)
      rsp = doChangeDelete(aEvent, aData);
    else {
      String out = "EPPChangeHandler.handleEvent(): unsupported event";
      throw new EPPEventException(out);
    }

    try {
      this.postHandleEvent(aEvent, aData);
    }
    catch (EPPHandleEventException e) {
      return new EPPEventResponse(e.getResponse());
    }

    return rsp;
  }

  /**
   * Handles any common behavior that all Change commands need to execute
   * before they execute their command specific behavior. The default
   * implementation does nothing.
   * 
   * @param aEvent
   *           The <code>EPPEvent</code> that is being handled
   * @param aData
   *           Any data that a Server needs to send to this
   *           <code>EPPChangeHandler</code>
   * @exception EPPHandleEventException
   *               Thrown if an error condition occurs. It must contain an
   *               <code>EPPEventResponse</code>
   */
  protected void preHandleEvent(EPPEvent aEvent, Object aData) throws EPPHandleEventException {
  }

  /**
   * Handles any common behavior that all Change commands need to execute after
   * they execute their command specific behavior. The default implementation
   * does nothing
   * 
   * @param aEvent
   *           The <code>EPPEvent</code> that is being handled
   * @param aData
   *           Any data that a Server needs to send to this
   *           <code>EPPChangeHandler</code>
   * @exception EPPHandleEventException
   *               Thrown if an error condition occurs. It must contain an
   *               <code>EPPEventResponse</code>
   */
  protected void postHandleEvent(EPPEvent aEvent, Object aData) throws EPPHandleEventException {
  }

  protected abstract EPPEventResponse doChangeCheck(EPPEvent aEvent, Object aData);

  protected abstract EPPEventResponse doChangeInfo(EPPEvent aEvent, Object aData);

  protected abstract EPPEventResponse doChangeCreate(EPPEvent aEvent, Object aData);

  protected abstract EPPEventResponse doChangeUpdate(EPPEvent aEvent, Object aData);

  protected abstract EPPEventResponse doChangeDelete(EPPEvent aEvent, Object aData);

}
