package org.opentosca.csarrepo.model.repository;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.opentosca.csarrepo.exception.PersistenceException;
import org.opentosca.csarrepo.model.Csar;
import org.opentosca.csarrepo.model.CsarFile;

/**
 * Class to avoid direct access of the hibernate active records for CSAR
 * 
 * @author eiselems (marcus.eisele@gmail.com), Dennis Przytarski
 *
 */
public class CsarRepository {

	/**
	 * Returns csar matching the given id
	 * 
	 * @param id
	 * @return csar
	 * @throws PersistenceException
	 *             upon problems committing the underlying transaction
	 */
	public Csar getbyId(long id) throws PersistenceException {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		Csar csar = null;
		try {
			tx = session.beginTransaction();
			csar = (Csar) session.get(Csar.class, id);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw new PersistenceException(e);
		} finally {
			session.close();
		}

		return csar;
	}

	/**
	 * Gets all csars
	 * 
	 * @return List of csars
	 * @throws PersistenceException
	 *             upon problems committing the underlying transaction
	 */
	public List<Csar> getAll() throws PersistenceException {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		List<Csar> csarList = null;
		try {
			tx = session.beginTransaction();
			csarList = session.createQuery("from Csar").list();
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw new PersistenceException(e);
		} finally {
			session.close();
		}
		return csarList;
	}

	/**
	 * @param csar
	 *            to be stored
	 * @return id of the just saved csar
	 * @throws PersistenceException
	 *             upon problems committing the underlying transaction
	 */
	public long save(Csar csar) throws PersistenceException {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(csar);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw new PersistenceException(e);
		} finally {
			session.close();
		}
		return csar.getId();
	}

	/**
	 * @param csar
	 * @throws PersistenceException
	 *             upon problems committing the underlying transaction
	 */
	public void delete(Csar csar) throws PersistenceException {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.delete(csar);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw new PersistenceException(e);
		} finally {
			session.close();
		}
	}

	/**
	 * Returns the last CSAR file with the highest id.
	 * 
	 * @param csar
	 * @return csarFile
	 * @throws PersistenceException
	 */
	public CsarFile getLastCsarFile(Csar csar) throws PersistenceException {
		Session session = HibernateUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(CsarFile.class);
			criteria.add(Restrictions.eq("csar", csar));
			criteria.addOrder(Order.desc("version"));
			criteria.setMaxResults(1);
			return (CsarFile) criteria.uniqueResult();
		} catch (HibernateException e) {
			throw new PersistenceException(e);
		} finally {
			session.close();
		}
	}
	
	public List<Csar> getByNamespaceAndServicetemplate(String namespace, String servicetemplate) 
			throws PersistenceException {
		
		Session session = HibernateUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(Csar.class);
			criteria.add(Restrictions.eq("namespace", namespace));
			criteria.add(Restrictions.and(Restrictions.eq("serviceTemplateId", servicetemplate)));
			
			return criteria.list();
		} catch (HibernateException e) {
			throw new PersistenceException(e);
		} finally {
			session.close();
		}
	}
}

@@ -3578,16 +3578,204 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09/**%0A%09 * returns list of csars with given namespace and servicetemplateId%0A%09 * %0A%09 * @param namespace%0A%09 * @param servicetemplate%0A%09 * @return List%3CCsar%3E%0A%09 * @throws PersistenceException%0A%09 */%0A
 %09public 

add comments, refs #335