package eu.dnetlib.functionality.alert.dao;

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

import eu.dnetlib.domain.functionality.AlertSubscription;
import eu.dnetlib.domain.functionality.AlertTemplate;

/**
 * This interface declares methods for persisting alert templates and subscriptions.
 * @author thanos@di.uoa.gr
 *
 */
public interface AlertDAO {
	/**
	 * Count templates.
	 * @return the total number of available templates.
	 * @throws AlertDAOException if any errors occur
	 */
	public int countTemplates() throws AlertDAOException;
	
	/**
	 * Retrieve some templates.
	 * @param limit the maximum number of templates to retrieve
	 * @param offset the offset to start at
	 * @return a sorted set containing templates
	 * @throws AlertDAOException if any errors occur
	 */
	public SortedSet<AlertTemplate> getTemplates(final int limit, final int offset) throws AlertDAOException; 
	
	/**
	 * Retrieve a template.
	 * @param templateId the unique identifier of the template to retrieve
	 * @return the template specified or null if no such template exists
	 * @throws AlertDAOException if any errors occur
	 */
	public AlertTemplate getTemplate(final String templateId) throws AlertDAOException;
	
	/**
	 * Save a template.
	 * @param template the template to save
	 * @throws AlertDAOException if any errors occur
	 */
	public void saveTemplate(final AlertTemplate template) throws AlertDAOException;
	
	/**
	 * Delete a template.
	 * @param templateId the unique identifier of the template to delete
	 * @throws AlertDAOException if any errors occur
	 */
	public void deleteTemplate(final String templateId) throws AlertDAOException;
	
	/**
	 * Count subscriptions.
	 * @return the total number of available subscriptions
	 * @throws AlertDAOException if any errors occur
	 */
	public int countSubscriptions() throws AlertDAOException;
	
	/**
	 * Retrieve some subscriptions.
	 * @param limit the maximum number of subscriptions to retrieve
	 * @param offset the offset to start at
	 * @return a sorted set containing subscriptions
	 * @throws AlertDAOException if any errors occur
	 */
	public SortedSet<AlertSubscription> getSubscriptions(final int limit, final int offset) throws AlertDAOException;
	
	/**
	 * Retrieve some subscriptions by alert mode and subscriber.
	 * @param alertMode the alert mode of the subscriptions to retrieve
	 * @param subscriber the URI of the subscriber of the subscriptions to retrieve
	 * @param limit the maximum number of subscriptions to retrieve
	 * @param offset the offset to start at
	 * @return a sorted set containing subscriptions
	 * @throws AlertDAOException if any errors occur
	 */
	public SortedSet<AlertSubscription> getSubscriptions(final String alertMode, final String subscriber, final int limit, final int offset) throws AlertDAOException;
	
	/**
	 * Retrieve some enabled subscriptions.
	 * @param notificationService the URL of the notification service of the subscriptions to retrieve
	 * @param queryId the unique identifier of the query of the subscriptions to retrieve
	 * @param limit the maximum number of subscriptions to retrieve
	 * @param offset the offset to start at
	 * @return a sorted set containing subscriptions
	 * @throws AlertDAOException if any errors occur
	 */
	public SortedSet<AlertSubscription> getEnabledSubscriptions(final int limit, final int offset) throws AlertDAOException;
	
	/**
	 * Retrieve some enabled subscriptions by notification service and query identifier.
	 * @param notificationService the URL of the notification service of the subscriptions to retrieve
	 * @param queryId the unique identifier of the query of the subscriptions to retrieve
	 * @param limit the maximum number of subscriptions to retrieve
	 * @param offset the offset to start at
	 * @return a sorted set containing subscriptions
	 * @throws AlertDAOException if any errors occur
	 */
	public SortedSet<AlertSubscription> getEnabledSubscriptions(final URL notificationService, final String queryId, final int limit, final int offset) throws AlertDAOException;
		
	/**
	 * Retrieve a subscription.
	 * @param templateId the unique identifier of the template of the subscription to retrieve
	 * @param notificationService the URL of the notification service of the subscription to retrieve
	 * @param queryId the unique identifier of the query of the subscription to retrieve
	 * @param resultId the unique identifier of the result to retrieve
	 * @param alertMode the alert mode of the subscription to retrieve
	 * @param subscriber the URI of the subscriber to retrieve
	 * @return the specified subscription or null if no such subscription exists
	 * @throws AlertDAOException if any errors occur
	 */
	public AlertSubscription getSubscription(final String templateId, final URL notificationService, final String queryId, final String resultId, final String alertMode, final URI subscriber) throws AlertDAOException;
	
	/**
	 * Save a subscription.
	 * @param subscription the subscription to save
	 * @throws AlertDAOException if any errors occur
	 */
	public void saveSubscription(final AlertSubscription subscription) throws AlertDAOException;
	
	/**
	 * Delete a subscription.
	 * @param templateId the unique identifier of the template of the subscription to delete
	 * @param notificationService the URL of the notification service of the subscription to delete
	 * @param queryId the unique identifier of the query of the subscription to delete
	 * @param resultId the unique identifier of the result of the subscription to delete
	 * @param alertMode the alert mode of the subscription to delete
	 * @param subscriber the URI of the subscriber of the subscription to delete
	 * @throws AlertDAOException if any errors occur
	 */
	public void deleteSubscription(final String templateId, final URL notificationService, final String queryId, final String resultId, final String alertMode, final URI subscriber) throws AlertDAOException;
}
