// ViewProxy.java

package org.objectweb.alarm.beans;

import java.rmi.RemoteException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 * proxy used to access the "View" session bean.
 * must be instantiated by JSP's with a tag like this:
 * <jsp:useBean id="myview" scope=session class=org.objectweb.alarm.beans.ViewProxy >
 */
public class ViewProxy {

    Context ictx = null;
    ViewHome vh = null;
    View ejbview = null;
    String myprofil = null;
    String errorMessage = null;

    private void init() {
	// Get the ViewHome
	try {
	    ictx = new InitialContext();
	    vh = (ViewHome) PortableRemoteObject.narrow(ictx.lookup("viewhome"), ViewHome.class);
	} catch (NamingException e) {
	    errorMessage = "ViewProxy : Cannot get ViewHome:"+e.toString();
	    return;
	}

	// Create the Session Bean
	try {
	    ejbview = vh.create();
	} catch (Exception e) {
	    errorMessage = "ViewProxy : Cannot create EJB:"+e.toString();
	}
    }
    /**
     * constructor: Create here the EJB session.
     */
    public ViewProxy() {
	init();
    }

    /**
     * RO attribute: Get all Profils already created by Alarm Manager.
     */
    public String[] getProfils() {
	String [] ret = new String[0];
	if (ejbview == null) {
	    errorMessage += "ViewProxy : Not initialized?";
	    return ret;
	}
	try {
	    ret = ejbview.getProfils();
	    errorMessage = null;
	} catch (RemoteException e) {
	    errorMessage = "ViewProxy : Cannot get Profil list:"+e.toString();
	}
	return ret;
    }

    /**
     * RW attribute: Profil used.
     */
    public String getProfil() {
	errorMessage = null;
	return myprofil;
    }
    public void setProfil(String p) {
	try {
	    ejbview.setProfil(p);
	    errorMessage = null;
	    // keep it on local for efficiency
	    myprofil = p;
	} catch (RemoteException e) {
	    errorMessage = "ViewProxy : Cannot set Profil:"+e.toString();
	}
    }

    /**
     * RO attribute: list of new alarms on the current Profil
     */
    public AlarmData[] getNewAlarms() {
	AlarmData[] ret = null;
	try {
	    ret = ejbview.getNewAlarms();
	    errorMessage = null;
	} catch (RemoteException e) {
	    errorMessage = "ViewProxy : Cannot get new alarms:"+e.toString();
	}
	return ret;
    }

    /**
     * RO attribute: list of all alarms on the current Profil
     */
    public AlarmData[] getAllAlarms() {
	AlarmData[] ret = null;
	try {
	    ret = ejbview.getAllAlarms();
	    errorMessage = null;
	} catch (RemoteException e) {
	    errorMessage = "ViewProxy : Cannot get all alarms:"+e.toString();
	}
	return ret;
    }

    /**
     * Gets the maximum level of alarm for this Profil.
     */
    public int AlarmLevel(String profilName) {
	int level = 0;
	try {
	    level = ejbview.alarmLevel(profilName);
	    errorMessage = null;
	} catch (RemoteException e) {
	    errorMessage = "ViewProxy : Cannot get AlarmLevel:"+e.toString();
	}
	return level;
    }

    /**
     * Get error message if any
     */
    public String getErrorMessage() {
	return errorMessage;
    }

    /**
     * Forget this Alarm because it has been treated.
     */
    public void forgetAlarm(String pk) {
	try {
	    ejbview.forgetAlarm(pk);
	    errorMessage = null;
	} catch (RemoteException e) {
	    errorMessage = "ViewProxy : Cannot forget Alarm:"+e.toString();
	}
    }

    /**
     * Creates a new Profil
     */
    public String newProfil(String device, String level) {
	String ret = null;
	try {
	    ret = ejbview.newProfil(device, level);
	    errorMessage = null;
	} catch (RemoteException e) {
	    errorMessage = "ViewProxy : Cannot create Profil:"+e.toString();
	}
	return ret;
    }

    /**
     * remove a Profil
     */
    public void removeProfil(String profil) {
	try {
	    ejbview.removeProfil(profil);
	    errorMessage = null;
	} catch (RemoteException e) {
	    errorMessage = "ViewProxy : Cannot remove Profil:"+e.toString();
	}
    }
}
