package eu.dnetlib.functionality.alert.alerter;

import java.net.URL;
import java.util.SortedSet;

import eu.dnetlib.domain.functionality.AlertSubscription;

/**
 * This abstract class represents objects that can send alerts in a certain alert mode.
 * @author thanos@di.uoa.gr
 * @see eu.dnetlib.domain.functionality.AlertSubscription
 * @see AlerterException
 *
 */
public abstract class Alerter {
	/**
	 * Initialize the alerter.
	 * @throws AlerterException if any errors occur
	 */
	public abstract void init() throws AlerterException;
	
	/**
	 * Get the alert modes supported by this alerter.
	 * @return a sorted set containing the alert modes that this alerter supports
	 */
	public abstract SortedSet<String> getSupportedAlertModes();
	
	/**
	 * Send an alert for a subscription.
	 * @param subscription the subscription for which to send the alert
	 * @param title the title of the alert
	 * @param message the message of the alert
	 * @param link the link of the alert
	 * @throws AlerterException if any errors occur
	 */
	public abstract void alert(final AlertSubscription subscription, final String title, final String message, final URL link) throws AlerterException;
	
	/**
	 * Retrieve the URI schemes supported by this alerter.
	 * @return a sorted set containing all the URI schemes supported by this alerter
	 */
	protected abstract SortedSet<String> getSupportedUriSchemes();
	
	/**
	 * Assert that this alerter can alert a subscription by checking its alert mode and scheme of subscriber URI. 
	 * @param subscription the subscription to validate
	 * @throws AlerterException if the subscription can not be alerted by this alerter
	 */
	protected void validateSubscription(final AlertSubscription subscription) throws AlerterException {
		if (!getSupportedAlertModes().contains(subscription.getAlertMode()))
			throw new AlerterException("unsupported alert mode " + subscription.getAlertMode());
		if (!getSupportedUriSchemes().contains(subscription.getSubscriber().getScheme()))
			throw new AlerterException("unsupported subscriber URI scheme " + subscription.getSubscriber().getScheme());
	}
}
