package eu.dnetlib.efg1914.commons.dao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import eu.dnetlib.efg1914.commons.store.XMLStoreException;

/**
 * A class used to access a data source and perform basic CRUD actions.
 * 
 * @author kiatrop
 * 
 * @param <T>  The type of element manages by this DAO.
 */
public interface GenericDAO<T> {

	/**
	 * Saves the given element
	 * 
	 * @param entity The element to save
	 * @return the id of the element
	 * @throws DAOException
	 */
	public String save(T entity) throws DAOException;

	/**
	 * Returns the element with the given name.
	 * 
	 * @param id The name of the element.
	 * @return the element.
	 * @throws DAOException
	 */
	public T getByName(String name) throws DAOException;
	
	/**
	 * Returns the element with the given id.
	 * 
	 * @param id The id of the element.
	 * @return the element.
	 * @throws DAOException
	 */
	public T getById(String id) throws DAOException;
	 
	/**
	 * Returns all elements of given type. .
	 * 
	 * @return List of elements.
	 * @throws DAOException
	 */
	public ArrayList<T> getAll() throws DAOException;

	/**
	 * Deletes the element with the given id.
	 * 
	 * @param id The id of the element.
	 * @return true if the element is successfully deleted.
	 * @throws XMLStoreException
	 * @throws DAOException
	 */
	public boolean delete(String id) throws DAOException;

	/**
	 * Update the given element
	 * 
	 * @param entity The element to be updated
	 * @return true if the element is successfully updated
	 * @throws DAOException
	 */
	public boolean update(T entity) throws DAOException;

	/**
	 * Search elements given a query
	 * 
	 * @param query the query
	 * @return
	 * @throws DAOException
	 */
	public List<T> search(String query) throws DAOException;

	/**
	 * Search elements given a prepared query
	 * 
	 * @param query
	 * @param bindings
	 * @return
	 */
	public List<T> search(String query, LinkedHashMap<String, String> bindings) throws DAOException;

	/**
	 * Gets elements that are created by the given user
	 * @param userId the id of the user
	 * @return a list with all the elements created by the given user
	 * @throws DAOException
	 */
	public ArrayList<T> getByUser(String userId) throws DAOException;

	/**
	 * Gets elements that are created by any other user except the given user
	 * @param userId the id of the user that we want to exclude
	 * @return a list with all the elements by any other user except the given user
	 * @throws DAOException
	 */
	public ArrayList<T> getByOtherUsers(String userId) throws DAOException;

	public ArrayList<T> getAllAfterTime(String time) throws DAOException;

	public String getAllAlias() throws XMLStoreException;

    public T getByIdOrAlias(String alias)  throws XMLStoreException, DAOException;

}
