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 javax.annotation.Nonnull;
import java.util.Collections;

/**
 * @since 4.9
 */
@Repository("scmMergeConfigDao")
public class HibernateScmMergeConfigDao
        extends AbstractHibernateDao<Long, InternalScmMergeConfig>
        implements ScmMergeConfigDao {

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

    @Override
    public InternalScmMergeConfig findByScm(@Nonnull String scmId) {
        return (InternalScmMergeConfig) session().byNaturalId(InternalScmMergeConfig.class)
                .using("scmId", scmId)
                .load();
    }

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