
package org.apache.ambari.server.events.jpa;

import javax.persistence.EntityManagerFactory;

import org.apache.ambari.server.events.AmbariEvent;

/**
 * The {@link JPAEvent} class is the base for all JPA events in Ambari. Although
 * this class could inherit from {@link AmbariEvent}, the publishers for
 * {@link AmbariEvent} instances use an asynchronous model. With JPA, we want a
 * synchronous publication model in order to ensure that events are handled
 * within the scope of the method invoking them.
 */
public abstract class JPAEvent {

  /**
   * The {@link JPAEventType} defines the type of JPA event.
   */
  public enum JPAEventType {

    /**
     * An event which instructs an {@link EntityManagerFactory} to evict
     * instances of a particular class.
     */
    CACHE_INVALIDATION;
  }

  /**
   * The concrete event's type.
   */
  protected final JPAEventType m_eventType;

  /**
   * Constructor.
   *
   * @param eventType
   *          the type of event (not {@code null}).
   */
  public JPAEvent(JPAEventType eventType) {
    m_eventType = eventType;
  }

  /**
   * Gets the type of {@link JPAEvent}.
   *
   * @return the event type (never {@code null}).
   */
  public JPAEventType getType() {
    return m_eventType;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString() {
    StringBuilder buffer = new StringBuilder(getClass().getSimpleName());
    buffer.append("{eventType=").append(m_eventType);
    buffer.append("}");
    return buffer.toString();
  }
}
