/*
 * JBoss, the OpenSource EJB server
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.ejb3.test.entitycallback;

import javax.persistence.Entity;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.GeneratorType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import java.util.HashSet;
import java.util.Set;

/**
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 * @version $Revision: 1.5.2.4 $
 */
@Entity
public class Customer
{
   Long id;
   String name;
   Set<Journey> journeys = new HashSet<Journey>();

   public Customer()
   {

   }

   public Customer(String name)
   {
      this.name = name;
   }

   @Id(generate = GeneratorType.AUTO)
   public Long getId()
   {
      return id;
   }

   public void setId(Long id)
   {
      this.id = id;
   }

   public String getName()
   {
      return name;
   }

   public void setName(String name)
   {
      this.name = name;
   }

   @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy="customer")
   public Set<Journey> getJourneys()
   {
      return journeys;
   }

   public void setJourneys(Set<Journey> journeys)
   {
      this.journeys = journeys;
   }

   public void addJourney(Journey journey)
   {
      journeys.add(journey);
      journey.setCustomer(this);
   }

   @PrePersist
   public void doPreCreate()
   {
      System.out.println("Customer doPreCreate");
      CallbackCounterBean.addCallback("Customer", PrePersist.class);
   }

   @PostPersist
   public void doPostCreate()
   {
      System.out.println("Customer doPostCreate");
      CallbackCounterBean.addCallback("Customer", PostPersist.class);
   }

   @PreRemove
   public void doPreRemove()
   {
      System.out.println("Customer doPreRemove");
      CallbackCounterBean.addCallback("Customer", PreRemove.class);
   }

   @PostRemove
   public void doPostRemove()
   {
      System.out.println("Customer doPostRemove");
      CallbackCounterBean.addCallback("Customer", PostRemove.class);
   }

   @PreUpdate
   public void doPreUpdate()
   {
      System.out.println("Customer doPreUpdate");
      CallbackCounterBean.addCallback("Customer", PreUpdate.class);
   }

   @PostUpdate
   public void doPostUpdate()
   {
      System.out.println("Customer doPostUpdate");
      CallbackCounterBean.addCallback("Customer", PostUpdate.class);
   }

   @PostLoad
   public void doPostLoad()
   {
      System.out.println("Customer doPostLoad");
      CallbackCounterBean.addCallback("Customer", PostLoad.class);
   }
}
