package org.objectweb.jonas.jtests.beans.relation.lcp;

import javax.naming.Context;
import javax.naming.InitialContext;


public class Session2Bean implements javax.ejb.SessionBean {

    // Variables
    public  javax.ejb.SessionContext ejbctx;

    // Methods



    /*****************************************************************************
     *
     * No args constructor
     *
     *****************************************************************************/
    public  Session2Bean() {
    }

    /*****************************************************************************
     *
     * No args creator
     *
     *****************************************************************************/
    public void ejbCreate() {
    }

    /*****************************************************************************
     *
     * Remove bean
     *
     *****************************************************************************/
    public void ejbRemove() {
    }

    /*****************************************************************************
     *
     * Passivate bean
     *
     *****************************************************************************/
    public void ejbPassivate() {

    }

    /*****************************************************************************
     *
     * Activate bean
     *
     *****************************************************************************/
    public void ejbActivate() {

    }

    /*****************************************************************************
     *
     * Sets the bean session context
     *
     *****************************************************************************/
    public void setSessionContext(javax.ejb.SessionContext ctx) {
        ejbctx = ctx;

    }

    public String getSimpleChildren(String id) {
        try {
            Context ic = new InitialContext();
            SIMPLEPARENTLocalHome home = (SIMPLEPARENTLocalHome) ic.lookup("java:comp/env/ejb/SIMPLEPARENT-local");
            SIMPLEPARENTPK pk = new SIMPLEPARENTPK();
            pk.spPkId = id;
            SIMPLEPARENTLocal bean = home.findByPrimaryKey(pk);
            if (bean != null) {
                java.util.Collection childs = bean.getSimpleparentSimplechild();
                if (childs != null) {
                    StringBuffer ret = new StringBuffer();
                    //ret.append("Parent desc:" + bean.getSpDesc() + "<br>");
                    int ct = 1;
                    SIMPLECHILDLocal bean2 = null;
                    java.util.Iterator i = childs.iterator();
                    while (i.hasNext()) {
                        bean2 = (SIMPLECHILDLocal) i.next();
                        //ret.append("Child" + ct + ":" + bean2.getScDesc() + "<br>");
                        if (ct != 1) {
                            ret.append(",");
                        }
                        ret.append(bean2.getScDesc());
                        ct++;
                    }
                    return ret.toString();
                } else {
                    return "Unable to find parent & child details (null childs bean)";
                }
            } else {
                return "Unable to find parent & child details (null parent bean)";
            }
        } catch (Exception ne) {
            return "Error: Unable to find parent & child (" + ne.getClass().getName() + ") - " + ne.getMessage();
        }

    }

    public String getChildParent(String id) {
        try {
            Context ic = new InitialContext();
            SIMPLECHILDLocalHome home = (SIMPLECHILDLocalHome) ic.lookup("java:comp/env/ejb/SIMPLECHILD-local");
            SIMPLECHILDPK pk = new SIMPLECHILDPK();
            pk.scPkId = id;
            SIMPLECHILDLocal bean = home.findByPrimaryKey(pk);
            if (bean != null) {
                SIMPLEPARENTLocal par = bean.getSimplechildSimpleparent();
                StringBuffer ret = new StringBuffer();
                if (par != null) {
                    //ret.append("Child desc:" + bean.getScDesc() + "<br>");
                    //ret.append("Parent desc:" + par.getSpDesc() + "<br>");
                    ret.append(par.getSpDesc());
                }
                return ret.toString();
            } else {
                return "Unable to find parent of child details (null child bean)";
            }
        } catch (Exception ne) {
            return "Error: Unable to find parent & child (" + ne.getClass().getName() + ") - " + ne.getMessage();
        }

    }


}
