/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package plate.wellmann.shop.business;

import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
import org.apache.log4j.Logger;
import plate.wellmann.shop.persistence.UserDomain;

/**
 *
 * @author Patrick
 */
@Named
@RequestScoped
public class UserDomainManager implements Serializable{
    
    final static Logger logger = Logger.getLogger(UserDomainManager.class);
    
    @PersistenceContext(name = "MySQL-Wellmann")
    private EntityManager em;
    
    @Transactional
    public UserDomain getDomain(String domain){
        
        if (domain == null) {
            logger.error("Tried to load a domain with domain == Null");
            return null;
        }
        
        TypedQuery<UserDomain> query = em.createNamedQuery("UserDomain.findByName", UserDomain.class);
        query.setParameter("userDomain", domain);
        try {
            UserDomain userDomain;
            logger.info("Loading Domain: " + domain);
            userDomain = query.getSingleResult();
            logger.info("Domain " + domain + "succesfully loaded!");
            return userDomain;
        } catch (NoResultException e){
            logger.error("Could'nt find domain " + e.toString());
            return null;
        }
    }
}
