package org.objectweb.alarm.beans;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.rmi.RemoteException;
import javax.ejb.FinderException;

/**
 * Helper class Profil
 * Profils are managed by AlarmService and used by View session beans
 * We do not use an Entity because we need neither persistence nor
 * remote accesses.
 * We cannot use a Session bean because it's accessed from several
 * clients, and from the AlarmService.
 */
public class Profil {


    // Profil Parameters
    protected String device;	// device name or "all"
    protected int maxsev;	// max severity level
    protected String name;
    protected boolean fromAll;
    protected AlarmRecordHome arh;

    // A list of AlarmRecord objects
    protected LinkedList alarmList = new LinkedList();

    /**
     * constructor
     */
    public Profil(String device, String sev, AlarmRecordHome arh) {

	// init object
	this.device = device;
	this.arh = arh;
	fromAll = device.equals("all");
	if (sev.startsWith("S")) {
	    maxsev = 1;
	    sev = "S";
	} else if (sev.startsWith("W")) {
	    maxsev = 2;
	    sev = "W";
	} else {
	    maxsev = 3;
	    sev = "I";
	}
	name = device+"-"+sev;

	// find alarms already arrived and matching this Profil
	Collection knal = null;
	try {
	    if (fromAll) {
		knal = arh.findBySeverity(maxsev);
	    } else {
		knal = arh.findByInterest(device, maxsev);
	    }
	    alarmList.addAll(knal);
	} catch (FinderException e) {
	    System.out.println( "Profil constructor: No Alarm found");
	} catch (RemoteException e) {
	    System.out.println("Error getting AlarmRecords:"+e);
	}
    }

    /**
     * gets the device
     */
    public String getDevice() {
	return device;
    }

    /**
     * gets the severity watched
     */
    public String getSeverity() {
	switch (maxsev) {
	case 1:
	    return "S";
	case 2:
	    return "W";
	case 3:
	    return "I";
	default:
	    return "A";
	}
    }

    /**
     * gets the profil name
     */
    public String getName() {
	return name;
    }

    /**
     * get Alarms for this Profil.
     * @args true if get all alarms, false if get only new alarms
     */
    synchronized public Collection getAlarms(boolean all) {
	LinkedList ret = new LinkedList();
	Iterator it = alarmList.iterator();
	while (it.hasNext()) {
	    AlarmRecord arec = (AlarmRecord) it.next();
	    AlarmData ad = null;
	    try {
		ad = arec.getAlarmData();
		if (all || (ad.getState() == 1)) {
		    ret.add(ad);
		}
	    } catch (RemoteException e) {
		System.out.println("Error getting AlarmRecord:"+e);
	    }
	}
	return ret;
    }

    /**
     * Gets current Alarm level
     */
    synchronized public int getCurrentLevel() {
	int ret = 1000;
	Iterator it = alarmList.iterator();
	while (it.hasNext()) {
	    AlarmRecord arec = (AlarmRecord) it.next();
	    try {
		AlarmData ad = arec.getAlarmData();
		if (ad.getState() == 1 && ad.getSev() <= ret)
		    ret = ad.getSev();
	    } catch (RemoteException e) {
		System.out.println("Error getting AlarmRecord:"+e);
	    }
	}
	return ret;
    }

    /**
     * return true if this Profil is interested by this AlarmRecord.
     */
    public boolean interestedBy(AlarmData ad) {
	return ((fromAll || ad.getDevice().equals(device)) && ad.getSev() <= maxsev);
    }
	    
    /**
     * add an Alarm Record to this Profil.
     */
    synchronized public void noticeAlarm(AlarmRecord arec) {
	alarmList.add(arec);
    }
}
