MirrorStore.java

/*
 * Copyright © 2023-2025 The CTAN Team and individual licenses
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */
package org.ctan.site.stores;

import java.util.List;
import java.util.Map;

import org.ctan.site.domain.mirrors.Mirrors;
import org.ctan.site.services.search.base.IndexType;
import org.ctan.site.services.search.base.IndexingSession;
import org.ctan.site.stores.base.AbstractIndexingStore;
import org.hibernate.SessionFactory;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import lombok.NonNull;

/**
 * The class <code>MirrorStore</code> contains the repository for mirrors.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
public class MirrorStore extends AbstractIndexingStore<Mirrors> {

    /**
     * This is the constructor for the <code>MirrorStore</code>.
     *
     * @param sessionFactory the session factory
     * @param indexingSession the indexing session
     */
    @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW")
    public MirrorStore(@NonNull SessionFactory sessionFactory,
        @NonNull IndexingSession indexingSession) {

        super(sessionFactory, indexingSession);
    }

    /**
     * The method <code>findAll</code> provides means to collect all mirrors
     * from the database.
     *
     * @return the list of mirrors
     */
    @Override
    public List<Mirrors> findAll() {

        var query = criteriaQuery();
        CriteriaBuilder cb = currentSession().getCriteriaBuilder();
        Root<Mirrors> mirror = query.from(Mirrors.class);
        query.where(cb.notEqual(mirror.get("id"), 0),
            cb.isNull(mirror.get("dateRemoved")))
            .orderBy(cb.asc(mirror.get("continent")));
        return list(query);
    }

    /**
     * The method <code>findAllByNameStartingWith</code> provides means to
     * retrieve mirrors where the sort text is starting with a given string. The
     * comparison is done case-insensitive.
     *
     * @param s the initial segment
     * @return the list of licenses ordered by the sort text
     */
    public List<Mirrors> findAllByKeyStartingWith(String s) {

        var query = criteriaQuery();
        CriteriaBuilder cb = currentSession().getCriteriaBuilder();
        Root<Mirrors> mirror = query.from(Mirrors.class);
        query.where(cb.like(mirror.get("hostname"), s.toLowerCase() + "%"))
            .orderBy(cb.asc(mirror.get("hostname")));
        return list(query);
    }

    /**
     * The method <code>getByHostname</code> provides means to find a mirror by
     * its name.
     *
     * @param key the key
     * @return the license or {@code null}
     */
    public Mirrors getByHostname(String key) {

        var query = criteriaQuery();
        CriteriaBuilder cb = currentSession().getCriteriaBuilder();
        Root<Mirrors> mirror = query.from(Mirrors.class);
        query.where(cb.equal(mirror.get("hostname"), key));
        return uniqueResult(query);
    }

    /**
     * {@inheritDoc}
     *
     * @see org.ctan.site.stores.base.AbstractIndexingStore#indexType()
     */
    @Override
    protected IndexType indexType() {

        return IndexType.MIRRORS;
    }

    /**
     * {@inheritDoc}
     *
     * @see org.ctan.site.stores.base.AbstractStore#listQuery(java.lang.String,
     *     jakarta.persistence.criteria.CriteriaBuilder,
     *     jakarta.persistence.criteria.CriteriaQuery)
     */
    @Override
    protected Root<Mirrors> listQuery(String term, CriteriaBuilder cb,
        CriteriaQuery<Mirrors> query) {

        // TODO unimplemented
        throw new UnsupportedOperationException();
    }

    /**
     * {@inheritDoc}
     *
     * @see org.ctan.site.stores.base.AbstractStore#map(java.util.List)
     */
    @Override
    protected List<Map<String, Object>> map(List<Mirrors> list) {

        // TODO unimplemented
        throw new UnsupportedOperationException();
    }
}