/*
 * Created on Apr 2, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.dragonsoft.tryapp.ejb.session;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.dragonsoft.tryapp.common.ActivityObj;
import com.dragonsoft.tryapp.ejb.entity.interfaces.Activity;
import com.dragonsoft.tryapp.ejb.entity.interfaces.ActivityHome;

/**
 * @author Greg
 * Finds the Activities for courses.
 * @ejb.bean name="ActivityFinder"
 *           display-name="Name for ActivityFinder"
 *           description="Description for ActivityFinder"
 *           jndi-name="ejb/ActivityFinder"
 *           type="Stateless"
 *           view-type="remote"
 */
public class ActivityFinderBean implements SessionBean {

	private ActivityHome activityHome;
	/**
	 * 
	 */
	public ActivityFinderBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
	 */
	public void setSessionContext(SessionContext ctx)
		throws EJBException,
		RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbRemove()
	 */
	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		init();

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/**
	 * Default create method
	 * 
	 * @throws CreateException
	 * @ejb.create-method
	 */
	public void ejbCreate() throws CreateException {
		init();
	}

	private void init() {
		Context ctx;
		try {
			ctx = new InitialContext();
			Object obj = ctx.lookup(ActivityHome.JNDI_NAME);
			activityHome = (ActivityHome) PortableRemoteObject.narrow(
				obj,
				ActivityHome.class);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "remote"
	 */
	public com.dragonsoft.tryapp.common.ActivityObj[] assembleActivities(
		String assignmentID) {
		ActivityObj[] obj = null;
		Collection collection = null;
		Iterator iter = null;
		Activity act = null;
		ArrayList list = new ArrayList();
		try {
			collection = activityHome.findByAllByAssignmentID(assignmentID);
			iter = collection.iterator();
			while(iter.hasNext()){
				act =(Activity)iter.next();
				list.add(act.getActivity());
			}
			obj = new ActivityObj[list.size()];
			list.toArray(obj);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			init();
		} catch (FinderException e) {
			obj = new ActivityObj[0];
		}
		return obj;
	}
}
