// ViewBean.java
// Stateful Session Bean

package org.objectweb.alarm.beans;

import java.util.Collection;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.RemoveException;
import javax.ejb.EJBObject;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.SessionSynchronization;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.transaction.NotSupportedException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

import org.objectweb.jonas.service.ServiceException;
import org.objectweb.jonas.service.ServiceManager;


/**
 * Session bean View.
 * Does not implement SessionSynchronization.
 * We assume that this bean and AlarmService run in the same JVM.
 * Each Session bean is dedicated to 1 client: Here, the ViewProxy class.
 * Each Session may be connected to 1 Profil or not.
 */
public class ViewBean implements SessionBean {

    SessionContext ejbContext;
    private transient AlarmService als = null;

    // bean state
    Profil prof = null;		// Profil reference

    // ------------------------------------------------------------------
    // SessionBean implementation
    // ------------------------------------------------------------------

    public void setSessionContext(SessionContext ctx) {
	ejbContext = ctx;
    }

    public void ejbRemove() {
    }
	
    /**
     * The Session bean must define 1 or more ejbCreate methods.
     *
     * @throws CreateException Failure to create a session EJB object.
     */
    public void ejbCreate() throws CreateException {

	// Get a reference on the AlarmService.
	try {
	    als = (AlarmService) ServiceManager.getInstance().getService("alarm");
	} catch (ServiceException e) {
	    throw new CreateException("AlarmListenerBean Cannot find AlarmService:"+e);
	}
    }

    public void ejbPassivate() {
    }

    public void ejbActivate() {
    }
    
    // ------------------------------------------------------------------
    // View implementation
    // ------------------------------------------------------------------

    /**
     * Return all messages for the current profil
     */
    public AlarmData[] getAllAlarms() throws RemoteException {
	if (prof == null) {
	    throw new RemoteException("No Profil defined for this session");
	}
	Collection alist = prof.getAlarms(true);
	return (AlarmData[]) alist.toArray(new AlarmData[0]);
    }

    /**
     * Return new messages for the current profil
     */
    public AlarmData[] getNewAlarms() throws RemoteException {
	if (prof == null) {
	    throw new RemoteException("No Profil defined for this session");
	}
	Collection alist = prof.getAlarms(false);
	return (AlarmData[]) alist.toArray(new AlarmData[0]);
    }

    /**
     * Returns an enumeration of all available profils
     */
    public String[] getProfils() throws RemoteException {
	return als.getProfilNames();
    }

    /**
     * Chooses to work on a particular profil. 
     */
    public void setProfil(String name) throws RemoteException {
	if (name == null) {
	    prof = null;
	    return;
	}
	prof = als.getProfil(name);
	if (prof == null) {
	    throw new RemoteException("This Profil does not exist yet: "+name);
	}
    }

    /**
     * Get the Alarm Level for Profil specified
     */
    public int alarmLevel(String name) throws RemoteException {
	Profil prof = als.getProfil(name);
	if (prof == null) {
	    throw new RemoteException("This Profil does not exist yet: "+name);
	}
	return prof.getCurrentLevel();
    }

    /**
     * Forget this message because problem has been taken into account.
     */
    public void forgetAlarm(String pk) throws RemoteException {
	als.forgetAlarm(pk);
    }

    /**
     * creates a new Profil
     */
    public String newProfil(String device, String level) throws RemoteException {

	// Check arg validity
	if (device.length() == 0) {
	    throw new RemoteException("null device string");
	}
	if (!level.startsWith("S") && !level.startsWith("W") && !level.startsWith("I")) {
	    throw new RemoteException("severity must be one of S|W|I");
	}
	
	prof = als.newProfil(device, level);
	if (prof == null) {
	    return null;
	}
	return prof.getName();
    }

    /**
     * remoce a Profil
     */
    public void removeProfil(String name) throws RemoteException {
	if (name == null) {
	    return;
	}
	boolean ok = als.delProfil(name);
	if (!ok) {
	    throw new RemoteException("This Profil does not exist yet: "+name);
	}
    }
}
