/**
 * ContextManager.java
 * Created on May 16, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.util;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.tandbergtv.cms.portal.util.transaction.Transactional;
import com.tandbergtv.watchpoint.pmm.dao.ContextHDAO;
import com.tandbergtv.watchpoint.pmm.dao.IContextDAO;
import com.tandbergtv.watchpoint.pmm.dao.hibernate.HibernateContext;
import com.tandbergtv.watchpoint.pmm.entities.Context;

/**
 * @author spuranik
 *
 */
public class ContextManager implements IContextManager {

	private static IContextManager instance;
	private static final Logger logger = Logger.getLogger(ContextManager.class);

	public static synchronized IContextManager getInstance() {
		if (instance == null) {
			instance = new ContextManager();
		}
		return instance;
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.util.IContextManager#getContext(long)
	 */
	@Transactional
	public Context getContext(long contextId) {
		Session session = null;
		Context context = new Context();

		try {
			session = HibernateContext.getContext().getCurrentSession();
			IContextDAO contextDAO = new ContextHDAO(session);
			context = contextDAO.findByKey(contextId);
			
		} catch (Exception e) {
			logger.error("Error while getting context(id: " + contextId + "): " + e.getMessage());
		} 
		return context;
	}
}
