package org.richfaces.realworld.service;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.richfaces.realworld.domain.Album;
import org.richfaces.realworld.domain.User;

@Name("albumAction")
public class AlbumAction implements IAlbumAction {
	//@PersistenceContext(unitName = "entityManager", type=PersistenceContextType.EXTENDED)
	@In(value="entityManager")
	EntityManager em;
	
	@In @Out
	private User user;

	/* (non-Javadoc)
	 * @see org.richfaces.realworld.service.IAlbumAction#addAlbum()
	 */
	public void addAlbum(Album album) {
		if(album.getChangedName() != album.getName()){
			album.setName(album.getChangedName());
		}
		em.persist(album);
		user.addAlbum(album);
		em.flush();
	}
	
	public void deleteAlbum(Album album){
		user.removeAlbum(album);
		em.flush();
	}
	
	public void editAlbum(Album album){
		em.merge(album);
		em.flush();
	}
	
	/* (non-Javadoc)
	 * @see org.richfaces.realworld.service.IAlbumAction#remove()
	 */
	@Remove
	public void remove(){
		
	}
	
	/* (non-Javadoc)
	 * @see org.richfaces.realworld.service.IAlbumAction#destroy()
	 */
	@Destroy
	public void destroy(){
		
	}
}
