/*
  Copyright (c) 1993-2003 Tumbleweed Communications Corp. All Rights Reserved.
 */
 
//  
//  $Log: $
//
//

package com.tumbleweed.messenger.server.ejb.impl.cmrtest;


import javax.ejb.CreateException;

import org.apache.log4j.Logger;

import com.tumbleweed.messenger.server.ejb.api.cmrtest.BarLocal;
import com.tumbleweed.messenger.server.util.AbstractEntityBean;

/**
 * Entity Bean representing a Foo (sorry about the naming it's late).
 * Implemented with CMP2.0.
 *
 *   @ejb:bean
 *      name="Foo"
 *      type="CMP"
 *      cmp-version="2.x"
 *      primkey-field="id"
 *      view-type="local"
 *   @ejb:interface
 *      local-class="com.tumbleweed.messenger.server.ejb.api.cmrtest.FooLocal"
 *      local-extends="javax.ejb.EJBLocalObject"
 *   @ejb:home
 *      local-extends="javax.ejb.EJBLocalHome"
 *   @ejb:pk class="java.lang.Long"
 *
 *  findFoosByFooValue()
 *      Just a finder to get Foo's where fooValue == someValue
 *
 *   @ejb:finder
 *      signature="Collection findFoosByFooValue(java.lang.String fooValue)"
 *      unchecked="true"
 *      query="SELECT OBJECT(f) FROM Foo as f WHERE f.fooValue = ?1"
 *      transaction-type="Required"
 *
 *   @ejb:persistence
 *      table-name="Foo"
 *
 *   @jboss:container-configuration name="Instance Per Transaction CMP 2.x EntityBean"
 *   @jboss:create-table "false"
 *   @jboss:remove-table "false"
 *   @jboss:read-only read-only="false"
 *   @jboss:read-ahead strategy="on-find"
 * 
 *   @jboss:unknown-pk
 *      class="java.lang.Long"
 *      field-name="id"
 *      column-name="id"
 *      readonly="false"
 *      jdbc-type="BIGINT"
 *      sql-type="BIGINT" 
 *      auto-increment
 * 
 *   @jboss:entity-command name="mssql-identity-fetch-key"
 *      class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCSQLServerCreateCommand"
 * 
 */
public abstract class FooEJB extends AbstractEntityBean {
    // -----------------------
    // Members
    // -----------------------

    /**
     * Logger
     */
    private static Logger sLog = Logger.getLogger(FooEJB.class);

    /**
     * Returns the ID for the Foo
     *
     * @return The ID of the Foo.
     *
     * @ejb:interface-method
     *
     * @ejb:persistence
     *   column-name="id"
     */
    public abstract Long getId();

    /**
     *
     * Sets the ID for the Foo.
     *
     * @param mailboxId The ID of the Foo.
     */
    public abstract void setId(Long fooId);

    /**
     * Returns the value for the Foo
     *
     * @return The value of the Foo.
     *
     * @ejb:interface-method
     *
     * @ejb:persistence
     *   column-name="fooValue"
     */
    public abstract String getFooValue();

    /**
     *
     * Sets the value for the Foo
     *
     * @param fooValue the value for the Foo
     */
    public abstract void setFooValue(String fooValue);


    /**
     * Returns the Bar for the Foo.
     *
     * @return Bar for the Foo
     *
     * @ejb:relation
     *    name="Bar-Foo"
     *    role-name="N-Foo-1-Bar"
     *    cascade-delete="yes"
     * @jboss:relation
     *    fk-column="barId"
     *    related-pk-field="id"
     * 
     * @ejb:interface-method
     */
    public abstract BarLocal getBar();

    /**
     * Sets the Bar of this Foo
     *
     * @param bar The bar of this Foo
     */
    public abstract void setBar(BarLocal bar);

    /**
     * Returns the string fooValue + barValue.
     *
     * @return the string fooValue + barValue.
     *
     * @ejb:interface-method
     *
     */
    public String getFooBarValue() {
        return getFooValue() + getBar().getBarValue();
    }
       
    /**
     * Creates an instance of a Foo.
     *
     * @param bar the bar this foo is associated with
     * @param fooValue the value of this foo.
     * @throws CreateException if the creation fails.
     * @return null to have the application server fill-in primary key (foo ID).
     * 
     * @ejb:create-method
     */
    public Long ejbCreate(BarLocal bar, String fooValue) throws CreateException {
        sLog.debug("ejbCreate(" + fooValue + ")");
        setFooValue(fooValue);
        return null;
    }

    /**
     * Post-create method that corresponds to {@link #ejbCreate}.
     *
     * @param bar the bar this foo is associated with
     * @param fooValue the value of this foo.
     * @throws CreateException if the creation fails.
     */
    public void ejbPostCreate(BarLocal bar, String fooValue) throws CreateException {
        sLog.debug("ejbPostCreate(" + getId() + ")");
        
        this.setBar(bar);
    }

}
