package org.yacos.core.article;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.yacos.core.users.User;

@Stateless
public class ArticleManagerBean implements IArticleManager, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@PersistenceContext
	EntityManager em;

	public ArticleManagerBean() {
		
	}

	public Article addArticle(Article a) {		
		em.persist(a);
		return a;
	}

	@SuppressWarnings("unchecked")
	public List<Article> getArticles() {
		return em.createQuery("from Article a ORDER BY a.title").getResultList();
	}

	public void updateArticle(Article article) {
		em.merge(article);
		
	}

	public void removeArticle(Article a) {
		em.remove(a);
		em.flush();
	}

	public Article getArticle(Integer id) {
		return em.find(Article.class, id);
	}
	
	public void assignArticleToPCMember(Article article,User pcMember){
		article.assignToPCMember(pcMember);
		em.flush();
	}

	public List<User> getArticlePCMembers(Article article) {
		return (List<User>) article.getPCMembers();
	}
	
	public void delegateArticleToReferee(Article article, User referee, User pcMember){
		article.delegateTo(referee,pcMember);
	}

	public List<User> getArticleReferees(Article article) {
		return (List<User>) article.getReferees();
	}

	public Preference getArticlePreferenceForUser(Integer article_id,
			String pcMemberLogin) {
		PreferencePK preferenceid = new PreferencePK();
		preferenceid.article = em.find(Article.class, article_id);
		preferenceid.pcMember = em.find(User.class, pcMemberLogin);
		try {
			return (Preference) em.find(Preference.class, preferenceid);
		} catch (NoResultException e) {
			return null;
		}
	}

	public void setArticlePreferences(Article article,
			List<Preference> preferences) {
		article.setPreferences(preferences);
	}

	public void addArticlePreference(Preference preference) {
		if(! em.contains(preference)){
			em.persist(preference);
		}
	}

	public void addArticlePreference(Integer article_id, String userLogin, String preferenceValue) {
		Preference preference = new Preference();
		Article article = getArticle(article_id);
		preference.setArticle(article);
		User pcMember = em.find(User.class, userLogin);
		preference.setPcMember(pcMember);
		preference.setPreference(preferenceValue);
		em.persist(preference);
		em.flush();
	}
	
	public void updateArticlePreference(Integer article_id, String userLogin, String preferenceValue) {
		Preference preference = getArticlePreferenceForUser(article_id, userLogin);
		preference.setPreference(preferenceValue);
		em.merge(preference);
	}
	
	public void addOrUpdatePreference(Integer article_id, String userLogin, String preferenceValue){
		Preference preference = getArticlePreferenceForUser(article_id, userLogin);
		if(preference != null){
			preference.setPreference(preferenceValue);
			em.merge(preference);
		} else {
			addArticlePreference(preference);
		}
	}
	
	public void updatePreference(Preference preference){
		em.merge(preference);
	}

	public boolean existsArticle(Article article) {
		return em.find(Article.class, article.getId())!=null;
	}

	public boolean existsPreference(Preference preference) {
		return em.find(Preference.class, preference.getId())!=null;
	}
	
	public EntityManager getEntityManager() {
		return em;
	}
}
