/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
package org.richfaces.realworld.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.Image;
import org.richfaces.realworld.domain.User;

@Name("userAction")
@Stateless
@AutoCreate
public class UserAction implements IUserAction {

	@In(value="entityManager")
	EntityManager em;
	@In FlushStrategy flushStrategy;
	@In
	private User user;

	public User login(String username, String password) {
		return (User)em.createNamedQuery(Constants.USER_LOGIN_QUERY)
		.setParameter(Constants.USERNAME_PARAMETER, username)
		.setParameter(Constants.PASSWORD_PARAMETER, password)
		.getSingleResult();
	}

	public void register(User user) {
		em.persist(user);
		if(flushStrategy.isDatabaseStoreStrategy()){
			em.flush();
		}
	}
	
	public User updateUser() {
		if(flushStrategy.isDatabaseStoreStrategy()){
			em.flush();
		}
		return user;
	}
	
	public User resetUser() {
		em.refresh(user);
		if(flushStrategy.isDatabaseStoreStrategy()){
			em.flush();
		}
		return user;
	}
	
	public boolean isUserExist(String login) {
		return em.createNamedQuery(Constants.USER_EXIST_QUERY)
		.setParameter(Constants.LOGIN_PARAMETER, login)
		.getResultList().size() != 0;
	}
	
	public List<Album> getSharedAlbums(User user){
		return (List<Album>)em.createQuery("from Album a where a.shelf.owner=:user and a.shelf.shared=true").setParameter("user", user).getResultList();
	}
	
	public List<Image> getSharedImages(User user){
		return (List<Image>)em.createQuery("from Image i where i.album.shelf.owner=:user and i.album.shelf.shared=true").setParameter("user", user).getResultList();
	}
	
	public Long countAlbums(User user){
		return (Long)em.createQuery("select count(a) from Album a where a.shelf.owner=:user").setParameter("user", user).getSingleResult();
	}
	
	public Long countSharedAlbums(User user){
		return (Long)em.createQuery("select count(a) from Album a where a.shelf.owner=:user and a.shelf.shared=true").setParameter("user", user).getSingleResult();
	}
	
	public Long countImages(User user){
		return (Long)em.createQuery("select count(i) from Image i where i.album.shelf.owner=:user").setParameter("user", user).getSingleResult();
	}
	
	public Long countSharedImages(User user){
		return (Long)em.createQuery("select count(i) from Image i where i.album.shelf.owner=:user and i.album.shelf.shared=true").setParameter("user", user).getSingleResult();
	}
}