// Collection of code snippets by Arne VajhÃ¸j 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package test;

import java.util.*;

import javax.naming.*;
import javax.ejb.*;

/**
 * TestSessionBean.
 */
public class TestSessionBean implements SessionBean {
    private SessionContext sessionContext;
    // diverse standard som skal være der men som man sjældent bruger
    public void ejbCreate() {
        return;
    }
    public void ejbRemove() {
        return;
    }
    public void ejbActivate() {
        return;
    }
    public void ejbPassivate() {
        return;
    }
    public void setSessionContext(SessionContext sessionContext) {
        this.sessionContext = sessionContext;
    }
    // gem
    public void save(int f1, String f2) {
        try {
            TestEntityLocalHome tehome = lookup();
            tehome.create(new Integer(f1), f2);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    // hent F2 for specifik F1
    public String getOne(int f1) {
        try {
            TestEntityLocalHome tehome = lookup();
            TestEntityLocal tebean = tehome.findByPrimaryKey(new Integer(f1));
            return tebean.getF2();
        } catch(Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    // hent alle F1
    public List getAll() {
        List lst = new ArrayList();
        try {
            TestEntityLocalHome tehome = lookup();
            Iterator it = tehome.findAll().iterator();
            while (it.hasNext()) {
                TestEntityLocal tebean = (TestEntityLocal)it.next();
                lst.add(tebean.getF1());
            }
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        return lst;
    }
    // lookup TestEntity
    private TestEntityLocalHome lookup() throws Exception {
        // create JNDI context
        Context ctx = new InitialContext();
        // lookup TestEntity local home interface
        Object temp = ctx.lookup("ejb/LocalTestEntity");
        // cast fra object til local home interface 
        TestEntityLocalHome tehome = (TestEntityLocalHome)temp;
        return tehome;
    }
}
