package com.atlassian.stash.internal.merge;

import com.atlassian.stash.internal.AbstractHibernateDao;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collections;

/**
 * @since 4.9
 */
@Repository("repositoryMergeConfigDao")
public class HibernateRepositoryMergeConfigDao
        extends AbstractHibernateDao<Long, InternalRepositoryMergeConfig>
        implements RepositoryMergeConfigDao {

    @Autowired
    public HibernateRepositoryMergeConfigDao(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public InternalRepositoryMergeConfig findByRepository(int repositoryId) {
        return (InternalRepositoryMergeConfig) session().byNaturalId(InternalRepositoryMergeConfig.class)
                .using("repositoryId", repositoryId)
                .load();
    }

    @Override
    protected Iterable<Order> getImplicitOrder() {
        return Collections.singleton(Order.asc("repositoryId"));
    }
}
