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.Arrays;

/**
 * @since 4.9
 */
@Repository("projectMergeConfigDao")
public class HibernateProjectMergeConfigDao
        extends AbstractHibernateDao<Long, InternalProjectMergeConfig>
        implements ProjectMergeConfigDao {

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

    @Override
    public InternalProjectMergeConfig findByProjectAndScm(int projectId, @Nonnull String scmId) {
        return (InternalProjectMergeConfig) session().byNaturalId(InternalProjectMergeConfig.class)
                .using("projectId", projectId)
                .using("scmId", scmId)
                .load();
    }

    @Override
    protected Iterable<Order> getImplicitOrder() {
        return Arrays.asList(Order.asc("projectId"), Order.asc("scmId"));
    }
}
