/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * 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 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-1307
 * USA
 *
 * Initial developer(s): Helene Joanin, Christophe Ney
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
*/

package org.objectweb.jonas_ejb.deployment.api;

import java.lang.Class;
import java.util.List;

import org.objectweb.jonas_ejb.deployment.xml.*;


/**
 * Base class for descriptor of an entity bean 
 */
public abstract class EntityDesc extends BeanDesc {

    Class primaryKeyClass;
    boolean reentrant;
    int passivationTimeout = 0;
    boolean shared = false;
    int poolMin = 0;
    int cacheMax = 0;
    protected static final String METHODS_REMOTE_NO_TX = ",getEJBHome,getHandle,getPrimaryKey,isIdentical,";
    protected static final String METHODS_HOME_NO_TX = ",getEJBMetaData,getHomeHandle,";

    /**
     * Package protected constructor used by API
     */
    EntityDesc(ClassLoader classLoader, Entity ent, AssemblyDescriptor asd, JonasEntity jEnt)
	throws DeploymentDescException {

        super(classLoader, ent.getDisplayName(), ent.getEjbName(),
	      jEnt.getJndiName() ,ent.getHome(), ent.getRemote(), ent.getLocalHome(), ent.getLocal(), 
	      ent.getEjbClass(), ent.getEnvEntryList(), ent.getResourceRefList(),
	      jEnt.getJonasResourceList(), ent.getResourceEnvRefList(), jEnt.getJonasResourceEnvList(),
	      ent.getEjbRefList(), jEnt.getJonasEjbRefList(), ent.getEjbLocalRefList(), ent.getSecurityRoleRefList(), asd);

        // primary Key class
        try {
            primaryKeyClass = classLoader.loadClass(ent.getPrimKeyClass().getValue());
        } catch (ClassNotFoundException e) {
            throw new DeploymentDescException("Primary Key class not found for bean "+this.ejbName,e);
        }
        
        // passivation timeout
        if (jEnt.getPassivationTimeout() != null) {
            String tstr = jEnt.getPassivationTimeout().getValue();
            Integer tval = new Integer(tstr);
            passivationTimeout = tval.intValue();
        }

        // reentrant
        if (ent.getReentrant().getValue().equalsIgnoreCase("True")) {
            reentrant = true;
        } else if (ent.getReentrant().getValue().equalsIgnoreCase("False")) {
            reentrant = false;
        } else
            throw new DeploymentDescException("Invalid reentrant value for bean "+this.ejbName);

        // shared
        if (jEnt.getShared() != null) {
            if (jEnt.getShared().getValue().equalsIgnoreCase("True")) {
                shared = true;
            } else if (jEnt.getShared().getValue().equalsIgnoreCase("False")) {
                shared = false;
            } else
                throw new DeploymentDescException("Invalid shared value for bean "+this.ejbName);
        }

        // min-pool-size
        if (jEnt.getMinPoolSize() != null) {
            String tstr = jEnt.getMinPoolSize().getValue();
            Integer tval = new Integer(tstr);
            poolMin = tval.intValue();
        }

        // max-cache-size
        if (jEnt.getMaxCacheSize() != null) {
            String tstr = jEnt.getMaxCacheSize().getValue();
            Integer tval = new Integer(tstr);
            cacheMax = tval.intValue();
        }
    }

    /**
     * check that trans-attribute is valid for bean
     */
    protected void checkTxAttribute(java.lang.reflect.Method m) throws DeploymentDescException {
        MethodDesc md = (MethodDesc) methodDesc.get(m);
        if (md.getTxAttribute() == MethodDesc.TX_NOT_SET) {
            // exclude method list for home interface
            if (javax.ejb.EJBHome.class.isAssignableFrom(m.getDeclaringClass()) 
                && (METHODS_HOME_NO_TX.indexOf(","+m.getName()+",") != -1))
                return;
            // exclude method list for remote interface
            if (javax.ejb.EJBObject.class.isAssignableFrom(m.getDeclaringClass())
                && (METHODS_REMOTE_NO_TX.indexOf(","+m.getName()+",") != -1))
                return;
            // trans-attribute not set !
            throw new DeploymentDescException("trans-attribute missing for method "+m.toString()+" in entity bean "+getEjbName());
        }
    }
    
    /**
     * Get the passivation timeout value
     */
    public int getPassivationTimeout() {
        return passivationTimeout;
    }

    /**
     * Get the entity's primary key class.
     * @return Class for the primary key
     */
    public Class getPrimaryKeyClass() {
            return primaryKeyClass;
    }

    /**
     * Assessor for reentrant entity bean
     * @return true for reentrant entity bean
     */
    public boolean isReentrant() {
        return reentrant;
    }

    /**
     * @return true for shared entity bean
     */
    public boolean isShared() {
        return shared;
    }

    /**
     * @return min size of the pool
     */
    public int getPoolMin() {
        return poolMin;
    }

    /**
     * @return max size of the cache
     */
    public int getCacheMax() {
        return cacheMax;
    }
    
    /**
     * String representation of the object for test purpose
     * @return String representation of this object
     */
    public String toString() {
        StringBuffer ret = new StringBuffer();
        ret.append(super.toString());
        ret.append("\ngetPrimaryKeyClass()="+getPrimaryKeyClass().toString());
        ret.append("\nisReentrant()="+new Boolean(isReentrant()).toString());
        ret.append("\ngetPassivationTimeout()="+getPassivationTimeout());
        ret.append("\nisShared()="+new Boolean(isShared()).toString());
        ret.append("\ngetPoolMin()="+getPoolMin());
        ret.append("\ngetCacheMax()="+getCacheMax());
        return ret.toString();
    }

}

