/*
 * $Header: /var/cvsreps/projects/c450/2002/p01/src/ch/isbiel/oois/infocore/ejb/CompetitionBean.java,v 1.2 2002/08/14 15:07:01 raucm Exp $
 *
 * OOIS - Online Orienteering Information System
 *
 * Copyright (c) 2002 by Michael Rauch, Mario Daepp,
 * University of Applied Sciences, Biel School of Engineering and Architecture
 *
 * OOIS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OOIS 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package ch.isbiel.oois.infocore.ejb;

import java.util.Set;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.naming.NamingException;

/**
 * This bean models a Competition with its properties such as its name.
 *
 * @author $Author: raucm $
 * @version $Revision: 1.2 $ $Date: 2002/08/14 15:07:01 $
 *
 * @ejb:bean
 *     name="Competition"
 *     display-name="CompetitionEJB"
 *     type="CMP"
 *     cmp-version="2.x"
 *     local-jndi-name="local/oois/infocore/ejb/Competition"
 *     view-type="local"
 *     reentrant="false"
 *     primkey-field="id"
 *
 * @ejb:pk
 *     class="java.lang.Integer"
 *
 * @ejb:ejb-ref
 *     ejb-name="SequenceSession"
 *     view-type="local"
 *
 * @ejb:util
 *     generate="physical"
 *
 * @ejb:finder
 *     signature="java.util.Collection findByEvent(java.lang.Integer eventId)"
 *     query="SELECT OBJECT(c) FROM Competition AS c WHERE c.event.id = ?1"
 */
public abstract class CompetitionBean implements EntityBean {

  /**
   * Entity context for this bean
   */
  private EntityContext _entityCtx;

  /**
   * Cache for the SequenceSessionLocal, used to get the primary key
   */
  private SequenceSessionLocal _sequence;


  /**
   * Create competition
   *
   * @ejb:create-method
   */
  public Integer ejbCreate(String name,
      EventLocal event) throws CreateException {
    setId(_sequence.getNextNumberInSequence("CompetitionBean"));
    setName(name);
    return null;
  }

  public void ejbPostCreate(String name,
      EventLocal event) throws CreateException {
    setEvent(event);
  }

 /**
   * A Competition belongs to exactly one Event
   *
   * @ejb:relation
   *     name="Event-Competition"
   *     role-name="Competition-has-a-Event"
   *
   * @jboss:relation
   *     fk-column="event_fk"
   *     related-pk-field="id"
   *     fk-constraint="true"
   *
   * @ejb:interface-method  view-type="local"
   */
  public abstract  EventLocal getEvent();

  /**
   * @ejb:interface-method view-type="local"
   */
  public abstract void setEvent(EventLocal event);

 /**
   * A Competition has many Races
   *
   * @ejb:relation
   *     name="Competition-Race"
   *     role-name="Competition-has-many-Races"
   * @ejb:interface-method view-type="local"
   */
  public abstract Set getRaces();

  /**
   * @ejb:interface-method view-type="local"
   */
  public abstract void setRaces(Set races);


  /**
   * The ID of the competition
   *
   * @ejb:persistent-field
   * @ejb:interface-method view-type="local"
   */
  public abstract Integer getId();

  /**
   * @ejb:interface-method view-type="local"
   */
  public abstract void setId(Integer id);

  /**
   * The name of the competition
   *
   * @ejb:persistent-field
   * @ejb:interface-method view-type="local"
   */
  public abstract String getName();

  /**
   * @ejb:interface-method view-type="local"
   */
  public abstract void setName(String name);



  /**
   * Callback methods
   */
  public void setEntityContext(EntityContext ctx) {
    _entityCtx = ctx;

    SequenceSessionLocalHome sequence_home;
    EJBLocalHomeFactory homeFactory = EJBLocalHomeFactory.getInstance();
    try {
      sequence_home = (SequenceSessionLocalHome) homeFactory.
          lookUpLocalHome(SequenceSessionLocalHome.COMP_NAME);
      _sequence = sequence_home.create();
    } catch (NamingException nex) {
      throw new EJBException(nex);
    } catch (CreateException cex) {
      throw new EJBException(cex);
    }
  }

  public void unsetEntityContext() {
    _entityCtx = null;
  }

  public void ejbRemove() {
  }

  public void ejbActivate() {
  }

  public void ejbPassivate() {
  }

  public void ejbLoad() {
  }

  public void ejbStore() {
  }
}